Sophie

Sophie

distrib > Mandriva > 2009.0 > i586 > by-pkgid > dd5ff701fdd2df6775e5cf25b2dd9e7c > files > 12

moin-1.1-3mdv2007.1.src.rpm

#!/usr/bin/python
"""
This script allows you to change pages from the commandline.

(c) 2003-11-16 Michael Reinsch <mr@uue.org>
Licensed under the GPL
"""

import getopt, os, sys, cgi


def usage(warn = None):
    if warn != None:
        print "\n warning: " + warn + "\n"

    print """ available options:
 -h, --help     - shows this help
 -b, --base=    - (req) the base path of your moin wiki, e.g. /var/www/moin
 -p, --page=    - (req) the name of the page you want to change
 --quotedpage=  - use this instead of --page if you have the quoted page name
 -a, --action=  - (req) which action to perform; supported actions:
                   edit, delete
 -u, --user=    - name of, default: "System"
 --content=     - the new content, required by the edit action
 --conten-file= - read content from given file
 --comment=     - optional comment for action
"""


class AllPermissions:
    def read(self, pagename, **kw):
        return True
    def edit(self, pagename, **kw):
        return True
    def save(self, editor, newtext, datestamp, **kw):
        return True
    def delete(self, pagename, **kw):
        return True
    def revert(self, pagename, **kw):
        return True
    def getACL(self, pagename, **kw):
        return None


def initRequest(basepath, username):
    # setup moin system
    from MoinMoin import cgimain, config, user
    request = cgimain.createRequest()
    request.form = cgi.FieldStorage(environ = {'QUERY_STRING': 'action=print'})
    os.environ['HTTP_COOKIE'] = ''
    request.user = user.User(request)
    request.user.may = AllPermissions()
    request.user.name = username
    return request


def editPage(request, pagename, newcontent, comment):
    from MoinMoin import PageEditor
    page = PageEditor.PageEditor(pagename, request)
    page.saveText(newcontent, '0', notify=1, comment=comment)

def deletePage(request, pagename, comment):
    from MoinMoin import PageEditor
    page = PageEditor.PageEditor(pagename, request)
    page.deletePage(comment)


def main():
    basepath = None
    pagename = None
    quotedpagename = None
    action = None
    content = None
    username = "System"
    comment = ""

    try:
        shortparams = "hb:p:a:u:"
        longparams = ["help", "base=", "page=", "quotedpage=", "action=",
            "user=", "content=", "content-file=", "comment="]
        opts, args = getopt.getopt(sys.argv[1:], shortparams, longparams)
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)

    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-b", "--base"):
            basepath = a
        elif o in ("-p", "--page"):
            pagename = a
        elif o in ("--quotedpage"):
            quotedpagename = a
        elif o in ("-a", "--action"):
            action = a
        elif o in ("-u", "--user"):
            username = a
        elif o in ("--content"):
            content = a
        elif o in ("--content-file"):
            content = file(a).read()
        elif o in ("--comment"):
            comment = a

    if basepath == None or (pagename == None and quotedpagename == None) or action == None:
        usage("parameters missing")
        sys.exit(2)

    try:
        os.stat(basepath + '/moin_config.py')
    except OSError:
        usage(basepath + '/moin_config.py no found')
        sys.exit(2)

    # make sure we find everything
    sys.path.append(basepath)
    os.chdir(basepath)

    # we cannot do this much earlier because the path should be adjusted first
    if quotedpagename != None:
        from MoinMoin import wikiutil
        pagename = wikiutil.unquoteWikiname(quotedpagename)

    req = initRequest(basepath, username)

    if action == 'edit':
        if content == None:
            usage("content missing")
            sys.exit(2)
        else:
            editPage(req, pagename, content, comment)
    elif action == 'delete':
        deletePage(req, pagename, comment)
    else:
        usage("uknown action: " + action)
        sys.exit(2)
    sys.exit()


def run():
    main()

if __name__ == "__main__":
    run()