view MoinMoin/script/import/ @ 5921:38b39fafc5dc

Added feature to set optional mtime (number of seconds since the epoch) for wiki pages
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Fri, 18 Jan 2013 09:53:55 +0100
parents bbb286a9c138
children 575660fc7a5a
line wrap: on
line source

# coding: utf-8
MoinMoin - import wiki pages from local files into the wiki.

@copyright: 2010 MoinMoin:PascalVolk,
            2013 MoinMoin:ReimarBauer

@license: GNU GPL, see COPYING for details.
import calendar
import time

from MoinMoin.PageEditor import PageEditor
from MoinMoin.script import MoinScript, fatal, log
from MoinMoin.wikiutil import clean_input, decodeUnknownInput, timestamp2version

class IAmRoot(object):
    def __getattr__(self, name):
        return lambda *args, **kwargs: True

class PluginScript(MoinScript):
This script imports the wiki page from given file into the wiki.

Detailed Instructions:
General syntax: moin [options] import wikipage [wikipage-options]

[options] usually should be:
    --config-dir=/path/to/cfg --wiki-url= --page=Page

    def __init__(self, argv, def_values):
        MoinScript.__init__(self, argv, def_values)
        self.parser.add_option('--acl', dest='acl', default='', metavar='ACL',
                help='Set a specific ACL for the wiki page')
        self.parser.add_option('--author', dest='author', metavar='AUTHOR',
                help='Use AUTHOR for edit history / RecentChanges')
        self.parser.add_option('--mtime', dest='mtime', metavar='mtime',
                help='Use TIME (YYYY-MM-DD HH:MM:SS) in UTC for edit history / RecentChanges. Default value is the current UTC time')
        self.parser.add_option('--comment', dest='comment', metavar='COMMENT',
                default='', help='COMMENT for edit history / RecentChanges')
        self.parser.add_option('--file', dest='file', default='',
                metavar='FILE', help='Read the wiki page from the given file')
        self.parser.add_option('--no-backup', dest='revision_backup',
                default=True, action='store_false',
                help="Suppress making a page backup per revision")
                'Name of the wiki page which should be imported')

    def mainloop(self):
        request = self.request
        request.user.may = IAmRoot()

        if not
            fatal('You must specify a wiki page name (--page=Page)!')
        if not self.options.file:
            fatal('You must specify a FILE to read from (--file=FILE)!')

            fileObj = open(self.options.file, 'rb')
        except IOError, err:
        page_content = decodeUnknownInput(

        if not self.options.acl:
            acl = ''
            acl = '#acl %s\n' % self.options.acl
        comment = clean_input(self.options.comment)

        if self.options.mtime:
            mtime = timestamp2version(calendar.timegm(time.strptime(self.options.mtime, "%Y-%m-%d %H:%M:%S")))
            mtime = timestamp2version(time.time())

        pe = PageEditor(request,, do_editor_backup=0,
              , mtime=mtime,
            pe.saveText(acl + page_content, 0, comment=comment)
        except PageEditor.Unchanged:
            log("info: wikipage was not modified - ignored update.")
        except PageEditor.SaveError, err:
            log("error: %r" % err)