view MoinMoin/script/maint/ @ 670:687633456631

MoinMoin.script: move _util stuff to __init__
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 20 May 2006 23:40:21 +0200
parents 80d0ad85a2d8
children da5623bc6819
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
    MoinMoin - do global changes to all pages in a wiki.

    The changes being done are hardcoded in function do_edit.
    As it is, this script is mostly useful for the MoinMoin release maintainer
    using: moin ... maint globaledit
    @copyright: 2004-2006 by MoinMoin:ThomasWaldmann
    @license: GNU GPL, see COPYING for details.
debug = False

from MoinMoin import PageEditor
from MoinMoin.script import MoinScript

class PluginScript(MoinScript):
    def __init__(self, argv, def_values):
        MoinScript.__init__(self, argv, def_values)
    def do_edit(self, pagename, origtext):
        if pagename in ['LocalSpellingWords', 'LocalBadContent',] or pagename.endswith('Template'):
            return origtext
        language_line = format_line = masterpage = None
        acl_lines = []
        master_lines = []
        pragma_lines = []
        comment_lines = []
        content_lines = []
        lines = origtext.splitlines()
        header = True
        for l in lines:
            if not l.startswith('#'):
                header = False
            if header:
                if l.startswith('#acl '):
                elif l.startswith('#language '):
                    language_line = l
                elif l.startswith('#format '):
                    format_line = l
                elif l.startswith('##master-page:'):
                    masterpage = l.split(':',1)[1].strip()
                elif l.startswith('##master-date:'):
                elif l.startswith('##'):
                elif l.startswith('#'):

        if not language_line:
            language_line = '#language en'
        if not format_line:
            format_line = '#format wiki'
        if not acl_lines and (
            masterpage is None or masterpage not in ['FrontPage', 'WikiSandBox',] and not masterpage.endswith('Template')):
            acl_lines = ['#acl MoinPagesEditorGroup:read,write,delete,revert All:read']
        if not master_lines:
            master_lines = ['##master-page:Unknown-Page', '##master-date:Unknown-Date',]

        c1old = "## Please edit (or translate) system/help pages on the moinmaster wiki ONLY."
        c2old = "## For more information, please see MoinMaster:MoinPagesEditorGroup."
        c1 = "## Please edit system and help pages ONLY in the moinmaster wiki! For more"
        c2 = "## information, please see MoinMaster:MoinPagesEditorGroup."
        for c in (c1old, c2old, c1, c2):
            if c in comment_lines:
        comment_lines = [c1, c2, ] + comment_lines

        if content_lines and content_lines[-1].strip(): # not an empty line at EOF

        if masterpage and masterpage.endswith('Template'):
            changedtext = master_lines + [format_line, language_line,] + pragma_lines + content_lines
            changedtext = comment_lines + master_lines + acl_lines + [format_line, language_line,] + pragma_lines + content_lines
        changedtext = '\n'.join(changedtext)
        return changedtext

    def mainloop(self):
        if debug:
            import codecs
            origtext ='origtext', 'r', 'utf-8').read()
            origtext = origtext.replace('\r\n','\n')
            changedtext = self.do_edit("", origtext)
            changedtext = changedtext.replace('\n','\r\n')
            f ='changedtext', 'w', 'utf-8')
            request = self.request

            # Get all existing pages in the wiki
            pagelist = request.rootpage.getPageList(user='')

            for pagename in pagelist:
                #request = CLI.Request(url=url, pagename=pagename.encode('utf-8'))
                p = PageEditor.PageEditor(request, pagename, do_editor_backup=0)
                origtext = p.get_raw_body()
                changedtext = self.do_edit(pagename, origtext)
                if changedtext and changedtext != origtext:
                    print "Writing %s ..." % repr(pagename)