Repository navigation
Expand file tree
/
Copy pathexport-comments.rb
More file actions
executable file
·50 lines (39 loc) · 1.67 KB
/
Copy pathexport-comments.rb
File metadata and controls
executable file
·50 lines (39 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/setup"
# FIXME: We shouldn't be dependent on GIDs staying the same as replication fallover may change them
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/lib"
require "csv"
require_relative "lib/configuration"
require "mysql"
class ExportComments
def initialize(args)
@args = args
end
def run
puts "WARNING: [mlander:] These are Very rough and ready scripts for importing/exporting comments when gid's might change!"
if ENV['BE-DANGEROUS']
puts "Continuing..."
else
puts "Set BE-DANGEROUS=1 if you have read these scripts and know what you are doing!"
return 1
end
conf = Configuration.new
db = Mysql.real_connect(conf.database_host, conf.database_user, conf.database_password,
conf.database_name)
res = db.query("select comments.*, comments.body as comment_body, epobject.body as hansard_body, hdate from comments, epobject, hansard where hansard.epobject_id = epobject.epobject_id and comments.epobject_id = epobject.epobject_id")
puts "Creating exported-comments.csv from selected rows in comments table..."
outfile = File.open("exported-comments.csv", "wb")
CSV::Writer.generate(outfile) do |csv|
res.each_hash do |row|
csv << [row["comment_id"], row["user_id"], row["visible"], row["modflagged"], row["posted"], row["hdate"],
row["comment_body"], row["hansard_body"][0..300]]
end
end
outfile.close
puts "Clearing comments table..."
db.query("DELETE FROM comments")
0
end
end
exit ExportComments.new(ARGV).run if File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__)