Back to index

moin  1.9.0~rc2
reducewiki.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003 MoinMoin - reducewiki script
00004 
00005 @copyright: 2005-2006 MoinMoin:ThomasWaldmann
00006 @license: GPL, see COPYING for details
00007 """
00008 
00009 import os, shutil, codecs
00010 
00011 from MoinMoin import config, wikiutil
00012 from MoinMoin.Page import Page
00013 from MoinMoin.action import AttachFile
00014 
00015 from MoinMoin.script import MoinScript
00016 
00017 class PluginScript(MoinScript):
00018     """\
00019 Purpose:
00020 ========
00021 This tool allows you to reduce a data/ directory to just the latest page
00022 revision of each non-deleted page (plus all attachments).
00023 
00024 This is used to make the distributed underlay directory, but can also be
00025 used for other purposes.
00026 
00027 So we change like this:
00028     * data/pages/PageName/revisions/{1,2,3,4}
00029         -> data/pages/revisions/1  (with content of 4)
00030     * data/pages/PageName/current (pointing to e.g. 4)
00031         -> same (pointing to 1)
00032     * data/pages/PageName/edit-log and data/edit-log
00033         -> do not copy
00034     * data/pages/PageName/attachments/*
00035         -> just copy
00036 
00037 Detailed Instructions:
00038 ======================
00039 General syntax: moin [options] maint reducewiki [reducewiki-options]
00040 
00041 [options] usually should be:
00042     --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
00043 
00044 [reducewiki-options] see below:
00045     0. To create a wiki data/ directory with just the latest revisions in the
00046        directory '/mywiki'
00047        moin ... maint reducewiki --target-dir=/mywiki
00048 """
00049 
00050     def __init__(self, argv, def_values):
00051         MoinScript.__init__(self, argv, def_values)
00052         self.parser.add_option(
00053             "-t", "--target-dir", dest="target_dir",
00054             help="Write reduced wiki data to DIRECTORY."
00055         )
00056 
00057     def copypage(self, request, rootdir, pagename):
00058         """ quick and dirty! """
00059         pagedir = os.path.join(rootdir, 'pages', wikiutil.quoteWikinameFS(pagename))
00060         os.makedirs(pagedir)
00061 
00062         # write a "current" file with content "00000001"
00063         revstr = '%08d' % 1
00064         cf = os.path.join(pagedir, 'current')
00065         file(cf, 'w').write(revstr+'\n')
00066 
00067         # create a single revision 00000001
00068         revdir = os.path.join(pagedir, 'revisions')
00069         os.makedirs(revdir)
00070         tf = os.path.join(revdir, revstr)
00071         p = Page(request, pagename)
00072         text = p.get_raw_body().replace("\n", "\r\n")
00073         codecs.open(tf, 'wb', config.charset).write(text)
00074 
00075         source_dir = AttachFile.getAttachDir(request, pagename)
00076         if os.path.exists(source_dir):
00077             dest_dir = os.path.join(pagedir, "attachments")
00078             os.makedirs(dest_dir)
00079             for filename in os.listdir(source_dir):
00080                 source_file = os.path.join(source_dir, filename)
00081                 dest_file = os.path.join(dest_dir, filename)
00082                 shutil.copyfile(source_file, dest_file)
00083 
00084     def mainloop(self):
00085         self.init_request()
00086         request = self.request
00087         destdir = self.options.target_dir
00088         pagelist = list(request.rootpage.getPageList(user=''))
00089         for pagename in pagelist:
00090             self.copypage(request, destdir, pagename)
00091