diff --git a/dump-wiki.py b/dump-wiki.py index 15930a65..26c7cd7a 100755 --- a/dump-wiki.py +++ b/dump-wiki.py @@ -12,18 +12,29 @@ # Actuellement, j'utilise : # sudo python /usr/scripts/dump-wiki.py --regex 'WiFi(/PositionnementDesBornes|/AvoirLeWifi.*)?' ~/mointest +#!/usr/bin/python2.3 -WIKIDIR='/var/local/lib/wiki' +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - Dump a MoinMoin wiki to static pages -__version__ = '0.1' + @copyright: 2002-2004 by Jürgen Hermann + @license: GNU GPL, see COPYING for details. +""" +__version__ = "20040329" + +# use this if your moin installation is not in sys.path: import sys -sys.path.append(WIKIDIR) # moin_config -sys.path.append('/usr/lib/python2.3/site-packages/MoinMoin') # MoinMoin +sys.path.insert(0, '../..') # path to MoinMoin +sys.path.insert(0, '/etc/moin') -# On peut modifier cette template -page_template = ''' +url_prefix = "." +HTML_SUFFIX = ".html" + +page_template = u''' + %(pagename)s @@ -50,9 +61,11 @@ Cette page a ''' #' - -import os, time, cStringIO +import os, time, StringIO, codecs, shutil, re +from MoinMoin import config, wikiutil, Page from MoinMoin.scripts import _util +from MoinMoin.request import RequestCLI +from MoinMoin.action import AttachFile class MoinDump(_util.Script): def __init__(self): @@ -64,7 +77,6 @@ class MoinDump(_util.Script): help="Ne copie que les pages correspondant à cette regex" ) - def mainloop(self): """ moin-dump's main code. """ @@ -72,71 +84,54 @@ class MoinDump(_util.Script): self.parser.print_help() sys.exit(1) - if len(self.args) != 1: - self.parser.error("Nombre incorrect d'arguments") - # Prepare output directory outputdir = self.args[0] - outputdir = os.path.abspath("%s/wiki" % outputdir) - attachdir = os.path.abspath("%s/../attach" % outputdir) + outputdir = os.path.abspath(outputdir) if not os.path.isdir(outputdir): try: os.mkdir(outputdir) _util.log("Created output directory '%s'!" % outputdir) except OSError: _util.fatal("Cannot create output directory '%s'!" % outputdir) - if not os.path.isdir(attachdir): - try: - os.mkdir(attachdir) - _util.log("Created attach directory '%s'!" % attachdir) - except OSError: - _util.fatal("Cannot create attach directory '%s'!" % attachdir) + + AttachFile.getAttachUrl = lambda pagename, filename, request, addts=0, escaped=0: (get_attachment(request, pagename, filename, outputdir)) - from MoinMoin import config - if config.default_config: - _util.fatal("Fichier de configuration moin_config.py introuvable.") - - # fix some values so we get relative paths in output html - config.url_prefix = "/wiki" - - # avoid spoiling the cache with url_prefix == "." - # we do not use nor update the cache because of that - config.caching_formats = [] # Dump the wiki - from MoinMoin.request import RequestCGI - request = RequestCGI({'script_name': '.'}) + request = RequestCLI(u"wiki.crans.org/") request.form = request.args = request.setup_args() - from MoinMoin import wikiutil, Page - pages = list(wikiutil.getPageList(config.text_dir)) + # fix url_prefix so we get relative paths in output html + request.cfg.url_prefix = url_prefix + + # Get all existing pages in the wiki + pages = list(request.rootpage.getPageList(user='')) if self.options.regex: - # On ne garde que les pages matchant la regexp - import re - pages = filter(lambda x: re.match(self.options.regex, x), pages) + pages = list(filter(lambda x: re.match(self.options.regex, x), pages)) + pages.sort() - wikiutil.quoteWikiname = lambda pagename, qfn=wikiutil.quoteWikiname: qfn(pagename) + ".html" + wikiutil.quoteWikinameURL = lambda pagename, qfn=wikiutil.quoteWikinameFS: (qfn(pagename) + HTML_SUFFIX) - errfile = os.path.join(outputdir, '..', 'error.log') + errfile = os.path.join(outputdir, 'error.log') errlog = open(errfile, 'w') errcnt = 0 for pagename in pages: - file = wikiutil.quoteWikiname(pagename) + file = wikiutil.quoteWikinameURL(pagename) # we have the same name in URL and FS # On construit le nom de la page avec les liens (peut sans doute mieux faire) - pagenamewithlinks = [''] + pagenamewithlinks = [u''] for composant in pagename.split("/"): pagenamewithlinks.append(pagenamewithlinks[-1]+'/'+composant) - pagenamewithlinks = " / ".join(map(lambda x: '%s' % ( - wikiutil.quoteWikiname(x[1:]), x[1:].split("/")[-1]), pagenamewithlinks[1:])) + pagenamewithlinks = u" / ".join(map(lambda x: u'%s' % ( + wikiutil.quoteWikinameURL(x[1:]), x[1:].split("/")[-1]), pagenamewithlinks[1:])) _util.log('Writing "%s"...' % file) try: pagehtml = '' - page = Page.Page(pagename) + page = Page.Page(request, pagename) try: request.reset() - out = cStringIO.StringIO() + out = StringIO.StringIO() request.redirect(out) page.send_page(request, count_hit=0, content_only=1) pagehtml = out.getvalue() @@ -145,46 +140,23 @@ class MoinDump(_util.Script): errcnt = errcnt + 1 print >>sys.stderr, "*** Caught exception while writing page!" print >>errlog, "~" * 78 + print >>errlog, file # page filename import traceback traceback.print_exc(None, errlog) finally: - import locale - locale.setlocale(locale.LC_ALL, '') - timestamp = time.strftime("%A %d %B %Y à %H:%M") - filepath = os.path.join(outputdir, file) - fileout = open(filepath, 'w') - fileout.write(page_template % { + timestamp = time.strftime("%Y-%m-%d %H:%M") + filepath = os.path.join(outputdir, 'wiki', file) + fileout = codecs.open(filepath, 'w', config.charset) + fileout.write((page_template % { + 'charset': config.charset, 'pagename': pagename, 'pagenamewithlinks': pagenamewithlinks, - 'pagehtml': pagehtml.replace('%s/rightsidebar/img' % config.url_prefix, '/img'), + 'pagehtml': pagehtml, 'timestamp': timestamp, - }) + 'theme': request.cfg.theme_default, + }).replace("./monobook", "..")) fileout.close() - # On copie les attachements - try: - source = "%s/data/pages/%s/attachments" % (WIKIDIR, file.replace(".html","")) - if os.path.isdir(source): - # Il y a des attachements - try: - os.makedirs("%s/%s/attachments" % (attachdir, file.replace(".html",""))) - except OSError, e: - # On va dire qu'il existe déjà - pass - os.system("cp -r %s %s/%s/." % - (source, attachdir, file.replace(".html",""))) - except: - errcnt = errcnt + 1 - print >>sys.stderr, "*** Caught exception while copying attachments!" - print >>errlog, "~" * 78 - import traceback - traceback.print_exc(None, errlog) - # On va copier les images - os.system("rsync -r --delete /usr/share/moin/htdocs/rightsidebar/img %s/.." % outputdir) - - # On finalise - os.system('chmod -R a+r %s/..' % outputdir) - os.system('find %s/.. -type d -exec chmod a+x {} \\;' % outputdir) errlog.close() if errcnt: @@ -193,6 +165,18 @@ class MoinDump(_util.Script): def run(): MoinDump().run() +def get_attachment(request, pagename, filename, outputdir): + """Traitement des attachements""" + source_dir = AttachFile.getAttachDir(request, pagename) + source_file = os.path.join(source_dir, filename) + if not os.path.isfile(source_file): + print "%s n'existe pas !" % source_file + return + dest_file = os.path.join(outputdir, "attach", + "%s_%s" % (wikiutil.quoteWikinameFS(pagename), filename)) + shutil.copyfile(source_file, dest_file) + return os.path.join("..", "attach", + "%s_%s" % (wikiutil.quoteWikinameFS(pagename), filename)) + if __name__ == "__main__": run() -