File: wikipage.py

package info (click to toggle)
moin 1.9.9-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 76,024 kB
  • sloc: python: 143,896; java: 10,704; php: 2,385; perl: 1,574; xml: 371; makefile: 214; sh: 81; sed: 5
file content (93 lines) | stat: -rw-r--r-- 3,468 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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


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


class PluginScript(MoinScript):
    """Purpose:
========
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=http://wiki.example.org/ --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',
                default='PageImporter',
                help='Use AUTHOR for edit history / RecentChanges')
        self.parser.add_option('--mtime', dest='mtime', metavar='mtime',
                default=None,
                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")
        self._update_option_help('--page',
                'Name of the wiki page which should be imported')

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

        if not self.options.page:
            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)!')

        try:
            fileObj = open(self.options.file, 'rb')
        except IOError, err:
            fatal(str(err))
        page_content = decodeUnknownInput(fileObj.read()).rstrip()
        fileObj.close()

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

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


        pe = PageEditor(request, self.options.page, do_editor_backup=0,
                        uid_override=self.options.author, mtime=mtime,
                        do_revision_backup=int(self.options.revision_backup))
        try:
            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)