tw-public@0: #!/usr/bin/env python tw-public@0: # -*- coding: iso-8859-1 -*- tw-public@0: """ tw-public@0: MoinMoin - Dump a MoinMoin wiki to static pages tw-public@0: tw-public@0: You must run this script as owner of the wiki files, usually this is the tw-public@0: web server user. tw-public@0: tw-public@0: @copyright: 2002-2004 by Jürgen Hermann tw-public@0: @license: GNU GPL, see COPYING for details. tw-public@0: """ tw-public@0: tw-public@0: import os, time, StringIO, codecs, shutil, errno tw-public@0: tw-public@0: # Insert the path to MoinMoin in the start of the path tw-public@0: import sys tw-public@0: sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), tw-public@0: os.pardir, os.pardir)) tw-public@0: tw-public@0: from MoinMoin import config, wikiutil, Page tw-public@0: from MoinMoin.scripts import _util tw-public@0: from MoinMoin.request import RequestCLI tw-public@0: tw-public@0: logo_html = '' tw-public@0: tw-public@0: url_prefix = "." tw-public@0: HTML_SUFFIX = ".html" tw-public@0: tw-public@0: page_template = u''' tw-public@0: tw-public@0: tw-public@0: tw-public@0: %(pagename)s tw-public@0: tw-public@0: tw-public@0: tw-public@0: tw-public@0: tw-public@0: tw-public@0: tw-public@0: tw-public@0: tw-public@0: tw-public@0:
tw-public@0: %(logo_html)s tw-public@0: tw-public@0: %(navibar_html)s tw-public@0:
tw-public@0:
tw-public@0: %(pagehtml)s tw-public@0:
tw-public@0: %(timestamp)s tw-public@0: tw-public@0: tw-public@0: ''' tw-public@0: tw-public@0: tw-public@0: class MoinDump(_util.Script): tw-public@0: tw-public@0: def __init__(self): tw-public@0: _util.Script.__init__(self, __name__, "[options] ") tw-public@0: self.parser.add_option( tw-public@0: "--config-dir", metavar="DIR", dest="config_dir", tw-public@0: help=("Path to the directory containing the wiki " tw-public@0: "configuration files. [default: current directory]") tw-public@0: ) tw-public@0: self.parser.add_option( tw-public@0: "--wiki-url", metavar="WIKIURL", dest="wiki_url", tw-public@0: help="URL of wiki e.g. localhost/mywiki/ [default: CLI]" tw-public@0: ) tw-public@0: self.parser.add_option( tw-public@0: "--page", metavar="NAME", dest="page", tw-public@0: help="Dump a single page (with possibly broken links)" tw-public@0: ) tw-public@0: tw-public@0: def mainloop(self): tw-public@0: """ moin-dump's main code. """ tw-public@0: tw-public@0: if len(sys.argv) == 1: tw-public@0: self.parser.print_help() tw-public@0: sys.exit(1) tw-public@0: tw-public@0: # Prepare output directory tw-public@0: outputdir = self.args[0] tw-public@0: outputdir = os.path.abspath(outputdir) tw-public@0: try: tw-public@0: os.mkdir(outputdir) tw-public@0: _util.log("Created output directory '%s'!" % outputdir) tw-public@0: except OSError, err: tw-public@0: if err.errno != errno.EEXIST: tw-public@0: _util.fatal("Cannot create output directory '%s'!" % outputdir) tw-public@0: tw-public@0: # Insert config dir or the current directory to the start of the tw-public@0: # path. tw-public@0: config_dir = self.options.config_dir tw-public@0: if config_dir and os.path.isfile(config_dir): tw-public@0: config_dir = os.path.dirname(config_dir) tw-public@0: if config_dir and not os.path.isdir(config_dir): tw-public@0: _util.fatal("bad path given to --config-dir option") tw-public@0: sys.path.insert(0, os.path.abspath(config_dir or os.curdir)) tw-public@0: tw-public@0: # Create request tw-public@0: if self.options.wiki_url: tw-public@0: request = RequestCLI(self.options.wiki_url) tw-public@0: else: tw-public@0: request = RequestCLI() tw-public@0: tw-public@0: # fix url_prefix so we get relative paths in output html tw-public@0: request.cfg.url_prefix = url_prefix tw-public@0: tw-public@0: if self.options.page: tw-public@0: pages = [self.options.page] tw-public@0: else: tw-public@0: # Get all existing pages in the wiki tw-public@0: pages = request.rootpage.getPageList(user='') tw-public@0: pages.sort() tw-public@0: tw-public@0: wikiutil.quoteWikinameURL = lambda pagename, qfn=wikiutil.quoteWikinameFS: (qfn(pagename) + HTML_SUFFIX) tw-public@0: tw-public@0: errfile = os.path.join(outputdir, 'error.log') tw-public@0: errlog = open(errfile, 'w') tw-public@0: errcnt = 0 tw-public@0: tw@35: page_front_page = wikiutil.getSysPage(request, request.cfg.page_front_page).page_name tw-public@0: page_title_index = wikiutil.getSysPage(request, 'TitleIndex').page_name tw-public@0: page_word_index = wikiutil.getSysPage(request, 'WordIndex').page_name tw-public@0: tw-public@0: navibar_html = '' tw-public@0: for p in [page_front_page, page_title_index, page_word_index]: tw-public@0: navibar_html += ' [%s]' % (wikiutil.quoteWikinameFS(p), wikiutil.escape(p)) tw-public@0: tw-public@0: for pagename in pages: tw-public@0: # we have the same name in URL and FS tw-public@0: file = wikiutil.quoteWikinameURL(pagename) tw-public@0: _util.log('Writing "%s"...' % file) tw-public@0: try: tw-public@0: pagehtml = '' tw-public@0: page = Page.Page(request, pagename) tw-public@0: request.page = page tw-public@0: try: tw-public@0: request.reset() tw-public@0: out = StringIO.StringIO() tw-public@0: request.redirect(out) tw-public@0: page.send_page(request, count_hit=0, content_only=1) tw-public@0: pagehtml = out.getvalue() tw-public@0: request.redirect() tw-public@0: except: tw-public@0: errcnt = errcnt + 1 tw-public@0: print >>sys.stderr, "*** Caught exception while writing page!" tw-public@0: print >>errlog, "~" * 78 tw-public@0: print >>errlog, file # page filename tw-public@0: import traceback tw-public@0: traceback.print_exc(None, errlog) tw-public@0: finally: tw-public@0: timestamp = time.strftime("%Y-%m-%d %H:%M") tw-public@0: filepath = os.path.join(outputdir, file) tw-public@0: fileout = codecs.open(filepath, 'w', config.charset) tw-public@0: fileout.write(page_template % { tw-public@0: 'charset': config.charset, tw-public@0: 'pagename': pagename, tw-public@0: 'pagehtml': pagehtml, tw-public@0: 'logo_html': logo_html, tw-public@0: 'navibar_html': navibar_html, tw-public@0: 'timestamp': timestamp, tw-public@0: 'theme': request.cfg.theme_default, tw-public@0: }) tw-public@0: fileout.close() tw-public@0: tw-public@0: # copy FrontPage to "index.html" tw-public@0: indexpage = page_front_page tw-public@0: if self.options.page: tw-public@0: indexpage = self.options.page tw-public@0: shutil.copyfile( tw-public@0: os.path.join(outputdir, wikiutil.quoteWikinameFS(indexpage) + HTML_SUFFIX), tw-public@0: os.path.join(outputdir, 'index' + HTML_SUFFIX) tw-public@0: ) tw-public@0: tw-public@0: errlog.close() tw-public@0: if errcnt: tw-public@0: print >>sys.stderr, "*** %d error(s) occurred, see '%s'!" % (errcnt, errfile) tw-public@0: tw-public@0: tw-public@0: def run(): tw-public@0: MoinDump().run() tw-public@0: tw-public@0: tw-public@0: if __name__ == "__main__": tw-public@0: run() tw-public@0: