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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
| #
# script to merge two tikiwiki s
#
require "rubygems"
require "mysql"
require "log4r"
require "iconv"
include Log4r
def createInsert(dbh, table, row )
insertSt = "insert into #{table} "
col ="("
values = "VALUES("
row.each { |key, value|
col += "#{key},"
if value and not value.empty? then
values += "'#{dbh.escape_string(value)}',"
else
values += "NULL,"
end
}
col = col[0..-2] + ")"
values = values[0..-2] + ")"
insertSt += col + " " + values
insertSt +";"
insertSt
end
$conflictNotif = {}
def addToConflictNotifList (user, pagename)
pageList = $conflictNotif[user]
if not pageList then
$conflictNotif[user] = []
pageList = $conflictNotif[user]
end
pageList << pagename
end
$notif = {}
def addToNotifList (user, pagename)
pageList = $notif[user]
if not pageList then
$notif[user] = []
pageList = $notif[user]
end
pageList << pagename
end
def printToFile (path, hash)
fileConflicts = File.open(path, File::WRONLY|File::TRUNC|File::CREAT)
hash.each { |user, list|
fileConflicts.print "#{user}: "
fileConflicts.print list.join(',')
fileConflicts.print "\n"
}
fileConflicts.close
end
Log4r::Logger.root.level = Log4r::DEBUG
l = Logger.new 'tiki_pages'
l.outputters = Outputter.stdout,FileOutputter.new("tiki_pages", :filename => "tiki_pages.txt", :trunc => true, :level => Log4r::DEBUG)
lh = Logger.new 'tiki_history'
lh.outputters = Outputter.stdout,FileOutputter.new("tiki_history", :filename => "tiki_history.txt", :trunc => true, :level => Log4r::DEBUG)
lm = Logger.new 'mysqlstatements'
lm.outputters = FileOutputter.new("sqlfile", :filename => "commands.sql", :trunc => true, :level=>Log4r::DEBUG)
l.debug "Starting migration script"
oldtiki="sql1.example.com"
olduser="root"
olddbname="tiki"
oldpwd="secret"
newtiki="sql2.example.com"
newuser="root"
newdbname="rd_tiki_wiki"
newpwd="secret"
#select login,email from users_users;
#mysql> select tiki_pages.pagename,users_users.email from tiki_pages,users_users where tiki_pages.user=users_users=login;
begin
#connect to the MySQL server
l.debug "trying to connect..."
dbhold = Mysql.init
dbhold.options(Mysql::SET_CHARSET_DIR, "/root/tikiWikiScript/charsets/")
dbhold.options(Mysql::SET_CHARSET_NAME, "utf8")
dbhold.real_connect(oldtiki,olduser, oldpwd,olddbname)
# get server version string and display it
l.info "#{oldtiki} mysql version: " + dbhold.get_server_info
dbhnew = Mysql.init
dbhnew.options(Mysql::SET_CHARSET_DIR, "/root/tikiWikiScript/charsets/")
dbhnew.options(Mysql::SET_CHARSET_NAME, "latin1")
dbhnew.real_connect(newtiki,newuser,newpwd,newdbname,3307)
l.info "#{newtiki} mysql version: " + dbhnew.get_server_info
l.info "retrieving all pagenames from old tiki..."
res= dbhold.query("select * from tiki_pages");
num_pages_old_tiki = res.num_rows
l.info "number of pages in old tiki : #{num_pages_old_tiki}"
insertions = 0
conflicts = 0
history_updates = 0
history_version_conflicts = 0
while row = res.fetch_hash do
#l.debug row
pagename = row["pageName"]
lastModif = row["lastModif"]
user = row["user"]
creator = row["creator"]
l.debug "checking pageName='#{pagename}'"
escapePageName = dbhnew.escape_string(pagename)
l.debug "escapePageName='#{escapePageName}'"
query = "select lastModif,user,creator from tiki_pages where pageName='#{escapePageName}'"
res2 = dbhnew.query(query)
if (res2.num_rows == 0) then
pageid = dbhnew.query("select max(page_id)+1 from tiki_pages").fetch_row[0]
l.info "Creating page '#{pagename}' with pageid #{pageid}"
row["page_id"] = pageid;
insertSt = createInsert(dbhnew, "tiki_pages", row )
lm.debug "#{insertSt}"
dbhnew.query "#{insertSt}"
addToNotifList( user, pagename);
addToNotifList( creator, pagename) unless user == creator
insertions += 1
else
if (res2.num_rows > 1) then
l.error "database invariant violated: entry for pagename #{pagename} not found in the new tiki"
fail
end
row2 = res2.fetch_hash
lastModif2 = row2["lastModif"]
l.debug "Comparing last modification of page #{pagename} in old tiki with same page in new tiki"
if (lastModif > lastModif2) then
l.warn "pagename \"#{pagename}\" is newer in #{oldtiki} than in #{newtiki}"
l.warn "we should send an email to #{user} and #{creator}"
addToConflictNotifList( user, pagename)
addToConflictNotifList( creator, pagename) unless user == creator
conflicts += 1
end
end
res2.free
historyRes = dbhold.query("select * from tiki_history where pageName='#{escapePageName}' ORDER BY version ASC")
anyHistoryUpdate = false;
while oldTikiHistoryEntry = historyRes.fetch_hash do
m = oldTikiHistoryEntry["lastModif"]
historyRes2 = dbhnew.query("select pageName from tiki_history where pageName='#{escapePageName}' and lastModif='#{m}'")
if (historyRes2.num_rows == 0) then
# this history entry was not present we must add new entry
version = oldTikiHistoryEntry["version"]
lh.info "adding version #{version} last updated on #{oldTikiHistoryEntry["lastModif"]} to tiki_history for page '#{pagename}'"
historyRes3 = dbhnew.query("select lastModif from tiki_history where pageName='#{escapePageName}' and version='#{version}'")
if (historyRes3.num_rows == 0) then
lh.debug "version: #{version} not present in #{newtiki}. Insert the entry"
else
lh.debug "version: #{version} of '#{pagename}' already exists in #{newtiki}. Now we have to insert the entry in the middle."
dbhnew.query("update tiki_history set version=version+1 where pageName='#{escapePageName}' and version >= '#{version}' ORDER BY version DESC")
history_version_conflicts += 1
end
insertSt = createInsert(dbhnew, "tiki_history", oldTikiHistoryEntry )
lm.info "#{insertSt}"
dbhnew.query "#{insertSt}"
history_updates += 1
anyHistoryUpdate = true;
else
lh.debug "The history entry with modification date #{m} of pageName=#{pagename} was already present in the tiki_history of #{newtiki}. Skipping it"
end
end
l.info "History of page #{pagename} updated." if anyHistoryUpdate
historyRes.free
end
l.info "number of pages in old tiki : #{num_pages_old_tiki}"
l.info "number of insertions in the new wiki #{insertions}"
l.info "number of conflicts in the new wiki #{conflicts}"
l.info "number of history updates #{history_updates}"
l.info "number of history version conflicts #{history_version_conflicts}"
dateString = Time.now.to_s
filename = "conflicts"+dateString+".txt"
printToFile(filename, $conflictNotif)
l.info "#{filename} created"
filename = "notifications"+dateString+".txt"
printToFile(filename, $notif)
l.info "#{filename} created"
res.free
#Update tiki_links that is responsible for the backlinks feature
link_insertions = 0
l.debug "retrieve all links"
linkRes = dbhold.query "select * from tiki_links"
while linkRow = linkRes.fetch_hash do
fromPage = dbhnew.escape_string(linkRow["fromPage"])
toPage = dbhnew.escape_string(linkRow["toPage"])
#check if this link already exists
checkRes = dbhnew.query "select * from tiki_links where fromPage='#{fromPage}' and toPage='#{toPage}'"
if (checkRes.num_rows == 0) then
insertSt = createInsert(dbhnew, "tiki_links", linkRow)
lm.info "#{insertSt}"
dbhnew.query insertSt
link_insertions += 1
end
checkRes.free
end
linkRes.free
l.info "number of link_insertions #{link_insertions}"
rescue Mysql::Error => e
l.error "Error code: #{e.errno}"
l.error "Error message: #{e.error}"
l.error "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
ensure
# disconnect from server
l.debug "disconnecting from database"
dbhold.close if dbhold
dbhnew.close if dbhnew
end
|