Ruben Laguna's blog

Jun 29, 2007 - 2 minute read - Comments - import merge mysql ruby script tikiwiki wiki

Merging two TikiWiki's

I’ve created a ruby script to merge the content of a TikiWiki into another one. This script will read the tiki_pages, tiki_history and tiki_links tables from the MySQL backend of the source TikiWiki and import the contents into the destination TikiWiki. The script is ‘safe’, meaning that it will not overwrite any page if it already exists in the destination. The history of the page will be merged as well if the page exists at the destination. The script doesn’t work with page attachments yet.

Here is the script, be sure to change the script parameters depending on your user/password on the databases (search oldtiki and newtiki variables in the script) and the charsets (search SET_CHARSET_NAME in the script).

#  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)}',"
values += "NULL,"
col = col[0..-2] + ")"
values = values[0..-2] + ")"
insertSt += col + " " + values
insertSt +";"

$conflictNotif = {}
def addToConflictNotifList (user, pagename)

pageList = $conflictNotif[user]
if not pageList then
$conflictNotif[user] = []
pageList = $conflictNotif[user]
pageList << pagename

$notif = {}
def addToNotifList (user, pagename)

pageList = $notif[user]
if not pageList then
$notif[user] = []
pageList = $notif[user]
pageList << pagename

def printToFile (path, hash)
fileConflicts =, File::WRONLY|File::TRUNC|File::CREAT)
hash.each { |user, list|
fileConflicts.print "#{user}: "
fileConflicts.print list.join(',')
fileConflicts.print "\n"

Log4r::Logger.root.level = Log4r::DEBUG

l = 'tiki_pages'
l.outputters = Outputter.stdout,"tiki_pages", :filename => "tiki_pages.txt", :trunc => true, :level => Log4r::DEBUG)

lh = 'tiki_history'

lh.outputters = Outputter.stdout,"tiki_history", :filename => "tiki_history.txt", :trunc => true, :level => Log4r::DEBUG)

lm = 'mysqlstatements'
lm.outputters ="sqlfile", :filename => "commands.sql", :trunc => true, :level=>Log4r::DEBUG)

l.debug "Starting migration script"



#select login,email from users_users;
#mysql> select tiki_pages.pagename, from tiki_pages,users_users where tiki_pages.user=users_users=login;

#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 "#{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) "#{newtiki} mysql version: " + dbhnew.get_server_info "retrieving all pagenames from old tiki..."

res= dbhold.query("select * from tiki_pages");

num_pages_old_tiki = res.num_rows "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] "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
if (res2.num_rows > 1) then
l.error "database invariant violated: entry for pagename #{pagename} not found in the new tiki"
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

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"]        "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"
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
insertSt = createInsert(dbhnew, "tiki_history", oldTikiHistoryEntry ) "#{insertSt}"
dbhnew.query "#{insertSt}"
history_updates += 1
anyHistoryUpdate = true;
lh.debug "The history entry with modification date #{m} of pageName=#{pagename} was already present in the tiki_history of #{newtiki}. Skipping it"
end "History of page #{pagename} updated." if anyHistoryUpdate
end "number of pages in old tiki : #{num_pages_old_tiki}" "number of insertions in the new wiki #{insertions}" "number of conflicts in the new wiki #{conflicts}" "number of history updates #{history_updates}" "number of history version conflicts #{history_version_conflicts}"

dateString =
filename = "conflicts"*dateString*".txt"
printToFile(filename, $conflictNotif) "#{filename} created"
filename = "notifications"*dateString*".txt"
printToFile(filename, $notif) "#{filename} created"

#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) "#{insertSt}"
dbhnew.query insertSt
link_insertions += 1
end "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")
# disconnect from server
l.debug "disconnecting from database"
dbhold.close if dbhold
dbhnew.close if dbhnew

The code is also available as a gist

You’ll need to install rubygems and Mysql module gem first.

Lions, buffalos and crocodiles Inspecting Tomcat HTTPS connection with Wireshark