Back to index

moin  1.9.0~rc2
package.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003 MoinMoin - Create a MoinMoin package from wiki pages specified.
00004 
00005 @copyright: 2002-2004 Juergen Hermann <jh@web.de>
00006             2005-2006 MoinMoin:ThomasWaldmann,
00007             2007 Federico Lorenzi
00008 @license: GNU GPL, see COPYING for details.
00009 """
00010 
00011 import os
00012 
00013 from MoinMoin import wikiutil, user
00014 from MoinMoin import script
00015 
00016 
00017 class PluginScript(script.MoinScript):
00018     """\
00019 Purpose:
00020 ========
00021 This tool allows you to create a package of certain wiki pages.
00022 
00023 Detailed Instructions:
00024 ======================
00025 General syntax: moin [options] export package [package-options]
00026 
00027 [options] usually should be:
00028     --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
00029 
00030 [package-options] see below:
00031     0. You must run this script as owner of the wiki files, usually this is the
00032        web server user.
00033 
00034     1. To package all non-system and non-underlay pages on a wiki to the file '~/mywiki.zip'
00035        moin ... export package --output ~/mywiki.zip
00036 
00037     2. To package the pages 'FooBar' and 'TestPage' on a wiki to the file '~/mywiki.zip'
00038        moin ... export package --pages FooBar,TestPage --output ~/mywiki.zip
00039 
00040     3. To package all pages matching the search term 'MoinMoin' on a wiki to the file '~/mywiki.zip'
00041        moin ... export package --search MoinMoin --output ~/mywiki.zip
00042 
00043     4. Optionally, the --user argument could be added to any of the above examples,
00044        causing the script to respect ACLs.
00045 
00046     5. Optionally, the --include_attachments argument could be added to any of the above examples,
00047        causing the script to include attachments into the output file.
00048 """
00049 
00050     def __init__(self, argv=None, def_values=None):
00051         script.MoinScript.__init__(self, argv, def_values)
00052         self.parser.add_option(
00053             "-p", "--pages", dest="pages",
00054             help="List of pages to package. Can be regular expressions, comma seperated lists, or a lone * for everything."
00055         )
00056         self.parser.add_option(
00057             "-a", "--include_attachments", action="store_true", dest="attachment",
00058             help="Include attachments from each page"
00059         )
00060         self.parser.add_option(
00061             "-o", "--output", dest="output",
00062             help="Output file for the package."
00063         )
00064         self.parser.add_option(
00065             "-s", "--search", dest="search",
00066             help="Search string to match."
00067         )
00068 
00069         self.parser.add_option(
00070             "-u", "--user", dest="package_user",
00071             help="User as whom the package operation will be performed as. "
00072             )
00073 
00074     def mainloop(self):
00075         """ moin-package's main code. """
00076 
00077         # Initalize request
00078         self.init_request()
00079         request = self.request
00080         _ = self.request.getText
00081 
00082         # Check our command line args
00083         if self.options.pages and self.options.search:
00084             script.fatal(_("Options --pages and --search are mutually exclusive!"))
00085         elif not self.options.output:
00086             script.fatal(_("You must specify an output file!"))
00087         elif not self.options.pages and not self.options.search:
00088             script.log(_("No pages specified using --pages or --search, assuming full package."))
00089 
00090         include_attachments = self.options.attachment or False
00091         if include_attachments:
00092             script.log(_("All attachments included into the package."))
00093 
00094         # Sanity checks
00095         if os.path.exists(self.options.output):
00096             script.fatal(_("Output file already exists! Cowardly refusing to continue!"))
00097 
00098         # Check for user
00099         if self.options.package_user:
00100             request.user = user.User(request, name=self.options.package_user)
00101 
00102         # Import PackagePages here, as we now have an initalized request.
00103         from MoinMoin.action.PackagePages import PackagePages
00104 
00105         # Perform actual packaging.
00106         package = PackagePages(request.rootpage.page_name, request)
00107         packageoutput = open(self.options.output, "wb")
00108         if self.options.search:
00109             packagedata = package.collectpackage(package.searchpackage(request,
00110                                                                        self.options.search), packageoutput,
00111                                                                        include_attachments=include_attachments)
00112         elif self.options.pages:
00113             packagedata = package.collectpackage(self.options.pages.split(","), packageoutput, include_attachments=include_attachments)
00114         else:
00115             packagedata = package.collectpackage(request.rootpage.getPageList(
00116                                 include_underlay=False,
00117                                 filter=lambda name: not wikiutil.isSystemPage(request, name)),
00118                                 packageoutput, include_attachments=include_attachments)
00119         if packagedata:
00120             script.fatal(packagedata)
00121