Back to index

moin  1.9.0~rc2
repair_language.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """ repair-language - repair page language setting.
00003 
00004 Usage:
00005 
00006     repair-language option
00007 
00008 Options:
00009 
00010     verify - verify pages, does not change anything, print page revision
00011         that should be repaired.
00012 
00013     repair - repair all page revisions.
00014 
00015 Step by step instructions:
00016 
00017  1. Stop your wiki.
00018 
00019  2. Make a backup of 'data' directory.
00020 
00021  3. Run this script from your wiki data directory, where your pages
00022     directory lives.
00023 
00024  4. Fix permissions on the data directory, as explained in HelpOnInstalling.
00025 
00026  5. Verify that pages are fine after repair, if you find a problem,
00027     restore your data directory from backup.
00028 
00029 Why run this script?
00030 
00031     In patch-325 a new #language processing instruction has been added.
00032     Pages that specify the language with it are displayed using correct
00033     direction, even if language_default use different direction.
00034 
00035     In the past, pages used to have ##language:xx comment. This comment
00036     has no effect, and should be replaced with newer #language xx
00037     processing instruction.
00038 
00039     This script replace ##language:xx to #language xx  in page headers.
00040     It convert all page revisions, so you can safely revert back to old
00041     revision and get correct page direction.
00042 
00043     You can run the script multiple times if needed.
00044 
00045 @copyright: 2004 Nir Soffer <nirs AT freeshell DOT org>
00046 @license: GPL, see COPYING for details
00047 """
00048 
00049 import codecs
00050 import os, sys
00051 
00052 # Insert THIS moin dir first into sys path, or you would run another
00053 # version of moin!
00054 sys.path.insert(0, '../..')
00055 
00056 from MoinMoin import i18n
00057 valid_languages = i18n.wikiLanguages()
00058 
00059 
00060 def listdir(path):
00061     """ Return list of files in path, filtering certain files """
00062     names = [name for name in os.listdir(path)
00063              if not name.startswith('.') and
00064              not name.endswith('.pickle') and
00065              name != 'CVS']
00066     return names
00067 
00068 
00069 def repairText(text):
00070     """ Repair page text
00071 
00072     We change only this type of lines that currently are in moinmaster
00073     ##language:\s*xx
00074 
00075     Warning: will not repair the language if there is more text on the
00076     same line, e.g. ##language:fr make it french!
00077 
00078     @param text: the page text, unicode
00079     @rtype: 2 tuple, (unicode, int)
00080     @return: text after replacement, lines changed
00081     """
00082     lineend = u'\r\n'
00083     needle = u'##language:'
00084     changed = 0
00085 
00086     # Get text lines
00087     lines = text.splitlines()
00088 
00089     # Look in page header
00090     for i in range(len(lines)):
00091         line = lines[i]
00092         if not line.startswith(u'#'):
00093             break # end of header
00094 
00095         if line.startswith(needle):
00096             # Get language from rest of line
00097             lang = line[len(needle):].strip()
00098 
00099             # Validate lang, make new style language processing
00100             # instruction.
00101             if lang in valid_languages:
00102                 line = u'#language %s' % lang
00103                 lines[i] = line
00104                 changed += 1
00105 
00106     if changed:
00107         # Join lines back, make sure there is trailing line end
00108         text = lineend.join(lines) + lineend
00109     return text, changed
00110 
00111 
00112 def processPages(path, repair):
00113     """ Process page directory
00114 
00115     @param repair: repair or just test
00116     """
00117     charset = 'utf-8'
00118 
00119     pages = [p for p in listdir(path) if os.path.isdir(os.path.join(path, p))]
00120     for page in pages:
00121         revdir = os.path.join(path, page, 'revisions')
00122         if not os.path.isdir(revdir):
00123             print 'Error: %s: missing revisions directory' % page
00124             continue
00125 
00126         for rev in listdir(revdir):
00127             revpath = os.path.join(revdir, rev)
00128             # Open file, read text
00129             f = codecs.open(revpath, 'rb', charset)
00130             text = f.read()
00131             f.close()
00132             text, changed = repairText(text)
00133 
00134             if changed and repair:
00135                 # Save converted text
00136                 f = codecs.open(revpath, 'wb', charset)
00137                 f.write(text)
00138                 f.close()
00139                 print 'Repaired %s revision %s' % (page, rev)
00140             elif changed:
00141                 print 'Should repair %s revision %s' % (page, rev)
00142 
00143 
00144 if __name__ == '__main__':
00145 
00146     # Check for pages directory in current directory
00147     path = os.path.abspath('pages')
00148     if not os.path.isdir(path):
00149         print "Error: could not find 'pages' directory"
00150         print 'Run this script from your wiki data directory'
00151         print __doc__
00152         sys.exit(1)
00153 
00154     options = {'verify': 0, 'repair': 1, }
00155 
00156     if len(sys.argv) != 2 or sys.argv[1] not in options:
00157         print __doc__
00158         sys.exit(1)
00159 
00160     processPages(path, repair=options[sys.argv[1]])
00161 
00162 
00163 
00164