Back to index

moin  1.9.0~rc2
globaledit.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003 MoinMoin - do global changes to all pages in a wiki.
00004 
00005 @copyright: 2004-2006 MoinMoin:ThomasWaldmann
00006 @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 debug = False
00010 
00011 from MoinMoin import PageEditor
00012 from MoinMoin.script import MoinScript
00013 
00014 class PluginScript(MoinScript):
00015     """\
00016 Purpose:
00017 ========
00018 This tool allows you to edit all the pages in a wiki.
00019 
00020 Detailed Instructions:
00021 ======================
00022 General syntax: moin [options] maint globaledit [globaledit-options]
00023 
00024 [options] usually should be:
00025     --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
00026 
00027 [globaledit-options] see below:
00028     0. The changes that will be performed are hardcoded in the function
00029        do_edit.
00030 
00031     1. This script takes no command line arguments.
00032 """
00033 
00034     def __init__(self, argv, def_values):
00035         MoinScript.__init__(self, argv, def_values)
00036 
00037     def do_edit(self, pagename, origtext):
00038         if pagename in ['LocalSpellingWords', 'LocalBadContent', ]:
00039             return origtext
00040         language_line = format_line = masterpage = None
00041         acl_lines = []
00042         master_lines = []
00043         pragma_lines = []
00044         comment_lines = []
00045         content_lines = []
00046         lines = origtext.splitlines()
00047         header = True
00048         for l in lines:
00049             if not l.startswith('#'):
00050                 header = False
00051             if header:
00052                 if l.startswith('#acl '):
00053                     acl_lines.append(l)
00054                 elif l.startswith('#language '):
00055                     language_line = l
00056                 elif l.startswith('#format '):
00057                     format_line = l
00058                 elif l.startswith('##master-page:'):
00059                     masterpage = l.split(':', 1)[1].strip()
00060                     master_lines.append(l)
00061                 elif l.startswith('##master-date:'):
00062                     master_lines.append(l)
00063                 elif l.startswith('##'):
00064                     comment_lines.append(l)
00065                 elif l.startswith('#'):
00066                     pragma_lines.append(l)
00067             else:
00068                 content_lines.append(l)
00069 
00070         if not language_line:
00071             language_line = '#language en'
00072         if not format_line:
00073             format_line = '#format wiki'
00074         aclold = '#acl MoinPagesEditorGroup:read,write,delete,revert All:read'
00075         if aclold in acl_lines:
00076             acl_lines.remove(aclold)
00077         if not acl_lines and (
00078             masterpage is None and not pagename.endswith('Template') or
00079             masterpage not in ['FrontPage', 'WikiSandBox', ] and not (pagename.endswith('Template') or masterpage.endswith('Template'))):
00080             acl_lines = ['#acl -All:write Default']
00081         if not master_lines:
00082             master_lines = ['##master-page:Unknown-Page', '##master-date:Unknown-Date', ]
00083 
00084         cold = [u"## Please edit system and help pages ONLY in the moinmaster wiki! For more",
00085                 u"## information, please see MoinMaster:MoinPagesEditorGroup.",
00086         ]
00087         cnew = [u"## Please edit system and help pages ONLY in the master wiki!",
00088                 u"## For more information, please see MoinMoin:MoinDev/Translation.",
00089         ]
00090         for c in cold + cnew:
00091             if c in comment_lines:
00092                 comment_lines.remove(c)
00093 
00094         comment_lines = cnew + comment_lines
00095 
00096         if content_lines and content_lines[-1].strip(): # not an empty line at EOF
00097             content_lines.append('')
00098 
00099         if masterpage and masterpage.endswith('Template'):
00100             changedtext = master_lines + [format_line, language_line, ] + pragma_lines + content_lines
00101         else:
00102             changedtext = comment_lines + master_lines + acl_lines + [format_line, language_line, ] + pragma_lines + content_lines
00103         changedtext = '\n'.join(changedtext)
00104         return changedtext
00105 
00106     def mainloop(self):
00107         if debug:
00108             import codecs
00109             origtext = codecs.open('origtext', 'r', 'utf-8').read()
00110             origtext = origtext.replace('\r\n', '\n')
00111             changedtext = self.do_edit("", origtext)
00112             changedtext = changedtext.replace('\n', '\r\n')
00113             f = codecs.open('changedtext', 'w', 'utf-8')
00114             f.write(changedtext)
00115             f.close()
00116         else:
00117             self.init_request()
00118             request = self.request
00119 
00120             # Get all existing pages in the wiki
00121             pagelist = request.rootpage.getPageList(user='')
00122 
00123             for pagename in pagelist:
00124                 #request = CLI.Request(url=url, pagename=pagename.encode('utf-8'))
00125                 p = PageEditor.PageEditor(request, pagename, do_editor_backup=0)
00126                 origtext = p.get_raw_body()
00127                 changedtext = self.do_edit(pagename, origtext)
00128                 if changedtext and changedtext != origtext:
00129                     print "Writing %s ..." % repr(pagename)
00130                     p._write_file(changedtext)
00131