Back to index

moin  1.9.0~rc2
12_to_13_mig04.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """
00003     migration from moin 1.3 < patch-196 to moin 1.3 >= patch-196
00004     Because of trouble with float timestamps, we migrate to usec timestamp resolution here.
00005     * data/pages/PageName/backup/<UTC timestamp> -> .../<UTC timestamp in usecs>
00006     * data/user/<uid>.bookmark -> convert to usecs
00007     * data/edit-log and data/pages/PageName/edit-log -> convert to usecs
00008     * data/event-log -> convert to usecs
00009 
00010     Steps for a successful migration:
00011 
00012         1. Stop your wiki and make a backup of old data and code
00013 
00014         2. Make a copy of the wiki's "data" directory to your working dir
00015 
00016         3. Run this script from your working dir
00017 
00018         4. If there was no error, you will find:
00019             data.pre-mig4 - the script renames your data directory copy to that name
00020             data - converted data dir
00021 
00022         5. Verify conversion results (number of pages, size of logs, attachments,
00023            number of backup copies) - everything should be reasonable before
00024            you proceed.
00025 
00026         6. Copy additional files from data.pre-mig4 to data (maybe intermaps, logs,
00027            etc.). Be aware that the file contents AND file names of wiki content
00028            may have changed, so DO NOT copy the files inside the cache/ directory,
00029            let the wiki refill it.
00030 
00031         7. Replace the data directory your wiki uses with the data directory
00032            you created by previous steps. DO NOT simply copy the converted stuff
00033            into the original or you will duplicate pages and create chaos!
00034 
00035         8. Test it - if something has gone wrong, you still have your backup.
00036 
00037 
00038     @copyright: 2004 Thomas Waldmann
00039     @license: GPL, see COPYING for details
00040 """
00041 
00042 
00043 import os.path, sys, urllib
00044 
00045 sys.path.insert(0, '../../../..')
00046 from MoinMoin import wikiutil
00047 
00048 from MoinMoin.script.migration.migutil import opj, listdir, copy_file, copy_dir
00049 
00050 def convert_ts(ts_from):
00051     if ts_from > 5000000000: # far more than 32bits?
00052         ts_to = ts_from # we already have usec kind of timestamp
00053     else:
00054         ts_to = wikiutil.timestamp2version(ts_from)
00055     return long(ts_to) # must be long for py 2.2.x
00056 
00057 def convert_eventlog(file_from, file_to):
00058     if not os.path.exists(file_from):
00059         return
00060     f = open(file_to, 'a')
00061     for l in open(file_from):
00062         if not l.strip():
00063             continue
00064         data = l.split('\t')
00065         data[0] = str(convert_ts(float(data[0]))) # we want usecs
00066         data = '\t'.join(data)
00067         f.write(data)
00068     f.close()
00069 
00070 def convert_editlog(file_from, file_to):
00071     if not os.path.exists(file_from):
00072         return
00073     f = open(file_to, 'a')
00074     for l in open(file_from):
00075         data = l.split('\t')
00076         pagename = data[0]
00077         timestamp = data[2]
00078         data[2] = str(convert_ts(float(timestamp))) # we want usecs
00079         data = '\t'.join(data)
00080         f.write(data)
00081     f.close()
00082 
00083 def convert_pagedir(dir_from, dir_to, is_backupdir=0):
00084     os.mkdir(dir_to)
00085     for pagedir in listdir(dir_from):
00086         text_from = opj(dir_from, pagedir, 'text')
00087         text_to = opj(dir_to, pagedir, 'text')
00088         os.mkdir(opj(dir_to, pagedir))
00089         copy_file(text_from, text_to)
00090 
00091         backupdir_from = opj(dir_from, pagedir, 'backup')
00092         backupdir_to = opj(dir_to, pagedir, 'backup')
00093         if os.path.exists(backupdir_from):
00094             os.mkdir(backupdir_to)
00095             for ts in listdir(backupdir_from):
00096                 ts_usec = str(convert_ts(float(ts)))
00097                 backup_from = opj(backupdir_from, ts)
00098                 backup_to = opj(backupdir_to, ts_usec)
00099                 copy_file(backup_from, backup_to)
00100 
00101         editlog_from = opj(dir_from, pagedir, 'edit-log')
00102         editlog_to = opj(dir_to, pagedir, 'edit-log')
00103         convert_editlog(editlog_from, editlog_to)
00104 
00105         #cachedir_from = opj(dir_from, pagedir, 'cache')
00106         #cachedir_to = opj(dir_to, pagedir, 'cache')
00107         #if os.path.exists(cachedir_from):
00108         #    os.mkdir(cachedir_to)
00109         #    try:
00110         #        copy_file(
00111         #            opj(cachedir_from, 'hitcounts'),
00112         #            opj(cachedir_to, 'hitcounts'))
00113         #    except: pass
00114 
00115         attachdir_from = opj(dir_from, pagedir, 'attachments')
00116         attachdir_to = opj(dir_to, pagedir, 'attachments')
00117         if os.path.exists(attachdir_from):
00118             try:
00119                 copy_dir(attachdir_from, attachdir_to)
00120             except: pass
00121 
00122 
00123 def convert_userdir(dir_from, dir_to):
00124     os.mkdir(dir_to)
00125     for fname in listdir(dir_from):
00126         if fname.endswith('.bookmark'):
00127             bm = open(opj(dir_from, fname)).read().strip()
00128             bm = str(wikiutil.timestamp2version(float(bm)))
00129             f = open(opj(dir_to, fname), 'w')
00130             f.write(bm)
00131             f.close()
00132         else:
00133             copy_file(opj(dir_from, fname), opj(dir_to, fname))
00134 
00135 
00136 origdir = 'data.pre-mig4'
00137 
00138 # Backup original dir and create new empty dir
00139 try:
00140     os.rename('data', origdir)
00141     os.mkdir('data')
00142 except OSError:
00143     print "You need to be in the directory where your copy of the 'data' directory is located."
00144     sys.exit(1)
00145 
00146 convert_pagedir(opj(origdir, 'pages'), opj('data', 'pages'))
00147 
00148 convert_editlog(opj(origdir, 'edit-log'), opj('data', 'edit-log'))
00149 
00150 convert_eventlog(opj(origdir, 'event.log'), opj('data', 'event-log'))
00151 
00152 convert_userdir(opj(origdir, 'user'), opj('data', 'user'))
00153 
00154 copy_dir(opj(origdir, 'plugin'), opj('data', 'plugin'))
00155 
00156 copy_file(opj(origdir, 'intermap.txt'), opj('data', 'intermap.txt'))
00157