198 lines
7.1 KiB
Python
Executable file
198 lines
7.1 KiB
Python
Executable file
#! /usr/bin/env python
|
|
# -*- coding: iso-8859-15 -*-
|
|
#
|
|
# Dumpe une partie du Wiki dans le répertoire donné pour créer
|
|
# une copie "statique".
|
|
#
|
|
# Exemple : python dump-wiki.py --regex WiFi/AvoirLeWifi".*" ~/macopie
|
|
# Les pages correspondant à la regex donnée seront placées dans
|
|
# le répertoire ~/macopie/wiki. Les attachements seront dans
|
|
# ~/macopie/attach.
|
|
#
|
|
# Actuellement, j'utilise :
|
|
# sudo python /usr/scripts/dump-wiki.py --regex 'WiFi(/PositionnementDesBornes|/AvoirLeWifi.*)?' ~/mointest
|
|
|
|
|
|
WIKIDIR='/var/local/lib/wiki'
|
|
|
|
__version__ = '0.1'
|
|
|
|
import sys
|
|
sys.path.append(WIKIDIR) # moin_config
|
|
sys.path.append('/usr/lib/python2.3/site-packages/MoinMoin') # MoinMoin
|
|
|
|
# On peut modifier cette template
|
|
page_template = '''<html>
|
|
<head>
|
|
<title>%(pagename)s</title>
|
|
<link rel="stylesheet" type="text/css" href="../wiki.css">
|
|
</head>
|
|
<body>
|
|
<p class="avertissement">
|
|
|
|
Ce site est une copie statique et partielle de ce que l'on peut trouver sur le
|
|
<a href="http://wiki.crans.org">wiki</a> de l'association. Si vous êtes ici, alors
|
|
que vous avez demandé un autre site, c'est sans doute que vous êtes connecté
|
|
au réseau wifi de l'association mais que vous n'avez pas encore complété toutes
|
|
les étapes nécessaires pour avoir une connexion pleinement fonctionnelle. Ce site
|
|
contient donc les infos pour configurer correctement votre connexion.
|
|
</p>
|
|
|
|
<h1>%(pagenamewithlinks)s</h1>
|
|
|
|
%(pagehtml)s
|
|
|
|
<p class="creation">
|
|
Cette page a été extraite du wiki le %(timestamp)s.
|
|
</p>
|
|
</body>
|
|
</html>
|
|
'''
|
|
#'
|
|
|
|
|
|
import os, time, cStringIO
|
|
from MoinMoin.scripts import _util
|
|
|
|
class MoinDump(_util.Script):
|
|
def __init__(self):
|
|
_util.Script.__init__(self, __name__, "[options] <target-directory>")
|
|
|
|
# --regex=REGEX
|
|
self.parser.add_option(
|
|
"--regex", metavar="REGEX", dest="regex",
|
|
help="Ne copie que les pages correspondant à cette regex"
|
|
)
|
|
|
|
|
|
def mainloop(self):
|
|
""" moin-dump's main code. """
|
|
|
|
if len(sys.argv) == 1:
|
|
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)
|
|
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)
|
|
|
|
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.form = request.args = request.setup_args()
|
|
|
|
from MoinMoin import wikiutil, Page
|
|
pages = list(wikiutil.getPageList(config.text_dir))
|
|
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.sort()
|
|
|
|
wikiutil.quoteWikiname = lambda pagename, qfn=wikiutil.quoteWikiname: qfn(pagename) + ".html"
|
|
|
|
errfile = os.path.join(outputdir, '..', 'error.log')
|
|
errlog = open(errfile, 'w')
|
|
errcnt = 0
|
|
|
|
for pagename in pages:
|
|
file = wikiutil.quoteWikiname(pagename)
|
|
# On construit le nom de la page avec les liens (peut sans doute mieux faire)
|
|
pagenamewithlinks = ['']
|
|
for composant in pagename.split("/"):
|
|
pagenamewithlinks.append(pagenamewithlinks[-1]+'/'+composant)
|
|
pagenamewithlinks = " / ".join(map(lambda x: '<a href="/wiki/%s">%s</a>' % (
|
|
wikiutil.quoteWikiname(x[1:]), x[1:].split("/")[-1]), pagenamewithlinks[1:]))
|
|
_util.log('Writing "%s"...' % file)
|
|
try:
|
|
pagehtml = ''
|
|
page = Page.Page(pagename)
|
|
try:
|
|
request.reset()
|
|
out = cStringIO.StringIO()
|
|
request.redirect(out)
|
|
page.send_page(request, count_hit=0, content_only=1)
|
|
pagehtml = out.getvalue()
|
|
request.redirect()
|
|
except:
|
|
errcnt = errcnt + 1
|
|
print >>sys.stderr, "*** Caught exception while writing page!"
|
|
print >>errlog, "~" * 78
|
|
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 % {
|
|
'pagename': pagename,
|
|
'pagenamewithlinks': pagenamewithlinks,
|
|
'pagehtml': pagehtml.replace('%s/rightsidebar/img' % config.url_prefix, '/img'),
|
|
'timestamp': timestamp,
|
|
})
|
|
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:
|
|
print >>sys.stderr, "*** %d error(s) occurred, see '%s'!" % (errcnt, errfile)
|
|
|
|
def run():
|
|
MoinDump().run()
|
|
|
|
if __name__ == "__main__":
|
|
run()
|
|
|