Projet

Général

Profil

Paste
Télécharger (5,41 ko) Statistiques
| Branche: | Révision:

root / scripts_divers / migrer_taches_vers_redmine / fix-db.rb @ f5ddb21e

1
#!/usr/bin/ruby -w
2
#encoding: UTF-8
3

    
4
require "dbi"
5
require "uri"
6

    
7
###### Definition of functions
8
def custom_chomp(string)
9
  # Chomp line completely
10
  previous_string = ''
11
  while string != previous_string
12
    previous_string = string.dup # = makes a reference to the original string
13
    string.chomp!
14
  end
15
  return string
16
end
17

    
18

    
19
def fix_issues_link(text, name2iid)
20
  # Find all links in text
21
  scan_regex =  /:http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t%C3%A2che)?|node)\/([^ \n)]*)/
22

    
23
  # Non flattened output is [['some'], ['text']]
24
  # flattened output is ['some', 'text']
25
  names = text.scan(scan_regex).flatten
26

    
27
  if not names.empty?
28
    names.each do |name|
29
      name = custom_chomp(name)
30
      unescaped_name = URI.unescape(name)
31
      gsub_regex = /"http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t%C3%A2che)?|node)\/[^ ]*":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t%C3%A2che)?|node)\/#{name}/
32
      remplace_regex = "#{unescaped_name} : ##{name2iid[name]}"
33
      text.gsub!(gsub_regex, remplace_regex)
34
    end
35
  end
36
  return text
37
end
38

    
39

    
40
def fix_comments_link(text, cid2iid, cid2post_nb)
41
  scan_regex = /:http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/|\/portail\/)comment\/(\d+)/
42

    
43
  cids = text.scan(scan_regex).flatten
44

    
45
  if not cids.empty?
46
    cids.each do |cid|
47
      cid = custom_chomp(cid)
48
      gsub_regex = /"http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)comment\/.*":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)comment\/#{cid}#comment-\d+/
49
      post_nb = custom_chomp(cid2post_nb[cid])
50
      remplace_regex = "##{cid2iid[cid]}#note-#{post_nb}"
51
      text.gsub!(gsub_regex, remplace_regex)
52
    end
53
  end
54
  return text
55
end
56

    
57

    
58

    
59
###### DATABASE
60
begin
61
  ##### Create DB connection
62
  db = 'redmine_default'
63
  dbuser = 'root'
64
  dbpwd = 'tata'
65
  host = 'localhost'
66
  dbh = DBI.connect("DBI:Mysql:#{db}:#{host}", dbuser, dbpwd)
67
  dbh.do("SET NAMES 'UTF8'")
68

    
69
  ###### Initialisation of hashes needed to fix URL
70
  name2iid = Hash.new
71
  nid2iid = Hash.new
72
  cid2iid = Hash.new
73
  cid2post_nb = Hash.new
74

    
75
  fix_url_issues_csv = File.open("fix_url_issues.csv", "r")
76
  fix_url_issues_csv.each do |line|
77
    line = line.chomp
78
    nid, name, iid = line.split(',')
79
    name2iid[name] = nid
80
    nid2iid[nid] = iid
81
  end
82
  fix_url_issues_csv.close
83

    
84
  fix_url_comments_csv = File.open("fix_url_comments.csv", "r")
85
  fix_url_comments_csv.each do |line|
86
    cid, iid, post_nb = line.split(',')
87
    cid2iid[cid] = iid
88
    cid2post_nb[cid] = post_nb
89
  end
90
  fix_url_comments_csv.close
91

    
92

    
93

    
94
  ###### Update issues
95
  sql_update_issue = "UPDATE issues SET author_id = ?, created_on = ?, is_private = 1 WHERE  id = ?"
96
  sql_fix_links_issue = "UPDATE issues SET description = ? WHERE id = ?"
97
  select_description = "SELECT description FROM issues WHERE id = ?"
98

    
99
  req_update_issue = dbh.prepare(sql_update_issue)
100
  req_fix_links_issue = dbh.prepare(sql_fix_links_issue)
101
  req_select_description = dbh.prepare(select_description)
102

    
103
  # Reading file
104
  issues = File.open('issues.csv', 'r')
105
  issues.each do |line|
106
    # Update author and date of creation
107
    line = line.chomp
108
    iid, author_id, created_on, nid, name = line.split(',')
109
    req_update_issue.execute(author_id, created_on, iid)
110

    
111
    ## Fix links
112
    # issues
113
    req_select_description.execute(iid)
114

    
115
    description = req_select_description.fetch[0]
116
    description = fix_issues_link(description, name2iid)
117

    
118
    # Comments
119
    description = fix_comments_link(description, cid2iid, cid2post_nb)
120

    
121
    # We update
122
    req_fix_links_issue.execute(description, iid)
123
  end
124

    
125
  req_select_description.finish
126
  req_update_issue.finish
127
  req_fix_links_issue.finish
128

    
129

    
130

    
131
  ###### Update journals
132
  sql_get_ids = "SELECT id FROM journals WHERE journalized_id = ? ORDER BY id ASC"
133
  sql_update_journals = "UPDATE journals SET created_on = ?, user_id = ? WHERE id = ?"
134
  sql_updated_on = "UPDATE issues SET  updated_on = ? WHERE id = ?"
135
  sql_fix_links_comment = "UPDATE journals SET notes = ? WHERE id = ?"
136

    
137
  req_get_ids = dbh.prepare(sql_get_ids)
138
  req_update_journals = dbh.prepare(sql_update_journals)
139
  req_updated_on = dbh.prepare(sql_updated_on)
140
  req_fix_links_comment = dbh.prepare(sql_fix_links_comment)
141

    
142
  # Reading file
143
  comments = File.open('comments.csv', 'r')
144

    
145
  # We must remember current iid in order to fetch result or execute query: req_get_id return
146
  # more than one value
147
  current_iid = -1
148
  select_notes = "SELECT notes FROM journals WHERE id = ?"
149
  req_select_notes = dbh.prepare(select_notes)
150

    
151
  comments.each do |line|
152
    line = line.chomp
153
    iid, user_id, created_on = line.split(',')
154

    
155
    # We get the id of the comment
156
    if iid != current_iid
157
      current_iid = iid
158
      req_get_ids.execute(iid)
159
    end
160

    
161
    id = req_get_ids.fetch[0]
162

    
163
    # We do the update
164
    req_update_journals.execute(created_on, user_id, id)
165
    req_updated_on.execute(created_on, iid)
166

    
167
    ## Fix links
168
    # issues
169
    req_select_notes.execute(id)
170
    notes = req_select_notes.fetch[0]
171
    notes = fix_issues_link(notes, name2iid)
172

    
173
    # Comments
174
    notes = fix_comments_link(notes, cid2iid, cid2post_nb)
175

    
176
    req_fix_links_comment.execute(notes, id)
177
  end
178
  comments.close
179

    
180
  req_select_notes.finish
181
  req_get_ids.finish
182
  req_update_journals.finish
183
  req_updated_on.finish
184
rescue DBI::DatabaseError => e
185
     puts "An error occurred"
186
     puts "Error code:    #{e.err}"
187
     puts "Error message: #{e.errstr}"
188
ensure
189
     # disconnect from server
190
     dbh.disconnect if dbh
191
end