Back to index

moin  1.9.0~rc2
12_to_13_mig09.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """
00003 Migration from moin--main--1.3 pre patch-332 to post patch-332.
00004 
00005 In patch-332 we changed the format of page lists in user data file. They
00006 are now tab separated instead of comma separated, and page names are not
00007 quoted using file system quoting.
00008 
00009 You can run the script multiple times with no damage.
00010 
00011 
00012 Steps for a successful migration:
00013 
00014  1. Stop your wiki
00015 
00016  2. Make a backup of your wiki 'data' directory
00017 
00018     WARNING: THIS SCRIPT MIGHT CORRUPT YOUR 'DATA' DIRECTORY. DON'T
00019     COMPLAIN LATER, MAKE BACKUP NOW!
00020 
00021  3. Move the wiki's 'data' directory to your working dir
00022 
00023  4. Run this script from your working dir
00024 
00025  5. If there was no error, you will find:
00026     data.pre-mig9   - backup of original data directory
00027     data            - converted data dir
00028 
00029  6. Verify conversion results (number of pages, size of logs,
00030     attachments, number of backup copies) - everything should be
00031     reasonable before you proceed.
00032 
00033     NOTE: THE CACHE DIRECTORY IS NOT COPIED - DO NOT COPY IT, IT WILL BE
00034     CREATED AND FILLED BY THE WIKI AUTOMATICALLY.
00035 
00036  7. Move the converted data directory into your wiki. Do not simply copy
00037     the converted stuff into the original or you will duplicate pages
00038     and create chaos!
00039 
00040  8. Fix permissions on your data directory, see HelpOnInstalling.
00041 
00042  9. Test it - if something has gone wrong, you still have your backup.
00043 
00044 
00045 @copyright: 2004 Thomas Waldmann
00046 @license: GPL, see COPYING for details
00047 """
00048 
00049 import os, sys, codecs
00050 join = os.path.join
00051 
00052 # Insert THIS moin dir first into sys path, or you might run another
00053 # version of moin and get unpredicted results!
00054 sys.path.insert(0, '../../../..')
00055 
00056 from MoinMoin import wikiutil, user
00057 from MoinMoin.script.migration import migutil
00058 
00059 
00060 def convert_quicklinks(string):
00061     """ Convert quicklinks from pre patch-332 to new format """
00062     # No need to convert new style list
00063     if '\t' in string:
00064         return string
00065 
00066     names = [name.strip() for name in string.split(',')]
00067     names = [wikiutil.unquoteWikiname(name) for name in names if name != '']
00068     string = user.encodeList(names)
00069     return string
00070 
00071 
00072 def convert_subscribed_pages(string):
00073     """ Convert subscribed pages from pre patch-332 to new format """
00074     # No need to convert new style list
00075     if '\t' in string:
00076         return string
00077 
00078     # This might break pages that contain ',' in the name, we can't do
00079     # anything about it. This was the reason we changed the format.
00080     names = [name.strip() for name in string.split(',')]
00081     string = user.encodeList(names)
00082     return string
00083 
00084 
00085 def convertUserData(text):
00086     """ Convert user data
00087 
00088     @param text: text of user file, unicode
00089     @rtype: unicode
00090     @return: convected user data
00091     """
00092     lines = text.splitlines()
00093     for i in range(len(lines)):
00094         line = lines[i]
00095         try:
00096             key, value = line.split('=', 1)
00097         except ValueError:
00098             continue
00099         if key == u'quicklinks':
00100             value = convert_quicklinks(value)
00101         elif key == u'subscribed_pages':
00102             value = convert_subscribed_pages(value)
00103         lines[i] = u'%s=%s' % (key, value)
00104 
00105     # Join back, append newline to last line
00106     text = u'\n'.join(lines) + u'\n'
00107     return text
00108 
00109 
00110 def convertUsers(srcdir, dstdir):
00111     """ Convert users files
00112 
00113     @param srcdir: old users dir
00114     @param dstdir: new users dir
00115     """
00116     charset = 'utf-8'
00117 
00118     # Create dstdir
00119     if not os.path.exists(dstdir):
00120         try:
00121             os.mkdir(dstdir)
00122         except OSError:
00123             migutil.fatalError("can't create user directory at '%s'" % dstdir)
00124 
00125     if not os.path.isdir(srcdir):
00126         migutil.fatalError("can't find user directory at '%s'" % srcdir)
00127 
00128     for name in migutil.listdir(srcdir):
00129         if name == 'README' or name.endswith('.trail'):
00130             # Copy as is
00131             migutil.copy_file(join(srcdir, name), join(dstdir, name))
00132         else:
00133             srcfile = join(srcdir, name)
00134             f = codecs.open(srcfile, 'rb', charset)
00135             text = f.read()
00136             f.close()
00137             text = convertUserData(text)
00138             dstfile = join(dstdir, name)
00139             f = codecs.open(dstfile, 'wb', charset)
00140             f.write(text)
00141             f.close()
00142             print "Converted '%s' to '%s'" % (srcfile, dstfile)
00143 
00144 
00145 if __name__ == '__main__':
00146 
00147     # Backup original dir
00148     datadir = 'data'
00149     origdir = 'data.pre-mig9'
00150     migutil.backup(datadir, origdir)
00151 
00152     # Copy ALL stuff from original dir into new data dir. Don't change
00153     # or drop anything from the original directory expect cache files.
00154     names = ['edit-log', 'event-log', 'intermap.txt', 'pages', 'plugin']
00155     migutil.copy(names, origdir, datadir)
00156 
00157     # Convert user directory
00158     convertUsers(join(origdir, 'user'), join(datadir, 'user'))
00159