Version compatible avec Moin 1.3
darcs-hash:20050904152524-d1718-17014e8b0aa29b63f9e23013ffecaf32a858006e.gz
This commit is contained in:
parent
61a9102174
commit
c34b6b7a8c
1 changed files with 62 additions and 78 deletions
138
dump-wiki.py
138
dump-wiki.py
|
@ -12,18 +12,29 @@
|
||||||
# Actuellement, j'utilise :
|
# Actuellement, j'utilise :
|
||||||
# sudo python /usr/scripts/dump-wiki.py --regex 'WiFi(/PositionnementDesBornes|/AvoirLeWifi.*)?' ~/mointest
|
# 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 <jh@web.de>
|
||||||
|
@license: GNU GPL, see COPYING for details.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__version__ = "20040329"
|
||||||
|
|
||||||
|
# use this if your moin installation is not in sys.path:
|
||||||
import sys
|
import sys
|
||||||
sys.path.append(WIKIDIR) # moin_config
|
sys.path.insert(0, '../..') # path to MoinMoin
|
||||||
sys.path.append('/usr/lib/python2.3/site-packages/MoinMoin') # MoinMoin
|
sys.path.insert(0, '/etc/moin')
|
||||||
|
|
||||||
# On peut modifier cette template
|
url_prefix = "."
|
||||||
page_template = '''<html>
|
HTML_SUFFIX = ".html"
|
||||||
|
|
||||||
|
page_template = u'''<html>
|
||||||
<head>
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=%(charset)s">
|
||||||
<title>%(pagename)s</title>
|
<title>%(pagename)s</title>
|
||||||
<link rel="stylesheet" type="text/css" href="../wiki.css">
|
<link rel="stylesheet" type="text/css" href="../wiki.css">
|
||||||
</head>
|
</head>
|
||||||
|
@ -50,9 +61,11 @@ Cette page a
|
||||||
'''
|
'''
|
||||||
#'
|
#'
|
||||||
|
|
||||||
|
import os, time, StringIO, codecs, shutil, re
|
||||||
import os, time, cStringIO
|
from MoinMoin import config, wikiutil, Page
|
||||||
from MoinMoin.scripts import _util
|
from MoinMoin.scripts import _util
|
||||||
|
from MoinMoin.request import RequestCLI
|
||||||
|
from MoinMoin.action import AttachFile
|
||||||
|
|
||||||
class MoinDump(_util.Script):
|
class MoinDump(_util.Script):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -64,7 +77,6 @@ class MoinDump(_util.Script):
|
||||||
help="Ne copie que les pages correspondant à cette regex"
|
help="Ne copie que les pages correspondant à cette regex"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def mainloop(self):
|
def mainloop(self):
|
||||||
""" moin-dump's main code. """
|
""" moin-dump's main code. """
|
||||||
|
|
||||||
|
@ -72,71 +84,54 @@ class MoinDump(_util.Script):
|
||||||
self.parser.print_help()
|
self.parser.print_help()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if len(self.args) != 1:
|
|
||||||
self.parser.error("Nombre incorrect d'arguments")
|
|
||||||
|
|
||||||
# Prepare output directory
|
# Prepare output directory
|
||||||
outputdir = self.args[0]
|
outputdir = self.args[0]
|
||||||
outputdir = os.path.abspath("%s/wiki" % outputdir)
|
outputdir = os.path.abspath(outputdir)
|
||||||
attachdir = os.path.abspath("%s/../attach" % outputdir)
|
|
||||||
if not os.path.isdir(outputdir):
|
if not os.path.isdir(outputdir):
|
||||||
try:
|
try:
|
||||||
os.mkdir(outputdir)
|
os.mkdir(outputdir)
|
||||||
_util.log("Created output directory '%s'!" % outputdir)
|
_util.log("Created output directory '%s'!" % outputdir)
|
||||||
except OSError:
|
except OSError:
|
||||||
_util.fatal("Cannot create output directory '%s'!" % outputdir)
|
_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
|
AttachFile.getAttachUrl = lambda pagename, filename, request, addts=0, escaped=0: (get_attachment(request, pagename, filename, outputdir))
|
||||||
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
|
# Dump the wiki
|
||||||
from MoinMoin.request import RequestCGI
|
request = RequestCLI(u"wiki.crans.org/")
|
||||||
request = RequestCGI({'script_name': '.'})
|
|
||||||
request.form = request.args = request.setup_args()
|
request.form = request.args = request.setup_args()
|
||||||
|
|
||||||
from MoinMoin import wikiutil, Page
|
# fix url_prefix so we get relative paths in output html
|
||||||
pages = list(wikiutil.getPageList(config.text_dir))
|
request.cfg.url_prefix = url_prefix
|
||||||
|
|
||||||
|
# Get all existing pages in the wiki
|
||||||
|
pages = list(request.rootpage.getPageList(user=''))
|
||||||
if self.options.regex:
|
if self.options.regex:
|
||||||
# On ne garde que les pages matchant la regexp
|
pages = list(filter(lambda x: re.match(self.options.regex, x), pages))
|
||||||
import re
|
|
||||||
pages = filter(lambda x: re.match(self.options.regex, x), pages)
|
|
||||||
pages.sort()
|
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')
|
errlog = open(errfile, 'w')
|
||||||
errcnt = 0
|
errcnt = 0
|
||||||
|
|
||||||
for pagename in pages:
|
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)
|
# On construit le nom de la page avec les liens (peut sans doute mieux faire)
|
||||||
pagenamewithlinks = ['']
|
pagenamewithlinks = [u'']
|
||||||
for composant in pagename.split("/"):
|
for composant in pagename.split("/"):
|
||||||
pagenamewithlinks.append(pagenamewithlinks[-1]+'/'+composant)
|
pagenamewithlinks.append(pagenamewithlinks[-1]+'/'+composant)
|
||||||
pagenamewithlinks = " / ".join(map(lambda x: '<a href="/wiki/%s">%s</a>' % (
|
pagenamewithlinks = u" / ".join(map(lambda x: u'<a href="/wiki/%s">%s</a>' % (
|
||||||
wikiutil.quoteWikiname(x[1:]), x[1:].split("/")[-1]), pagenamewithlinks[1:]))
|
wikiutil.quoteWikinameURL(x[1:]), x[1:].split("/")[-1]), pagenamewithlinks[1:]))
|
||||||
_util.log('Writing "%s"...' % file)
|
_util.log('Writing "%s"...' % file)
|
||||||
try:
|
try:
|
||||||
pagehtml = ''
|
pagehtml = ''
|
||||||
page = Page.Page(pagename)
|
page = Page.Page(request, pagename)
|
||||||
try:
|
try:
|
||||||
request.reset()
|
request.reset()
|
||||||
out = cStringIO.StringIO()
|
out = StringIO.StringIO()
|
||||||
request.redirect(out)
|
request.redirect(out)
|
||||||
page.send_page(request, count_hit=0, content_only=1)
|
page.send_page(request, count_hit=0, content_only=1)
|
||||||
pagehtml = out.getvalue()
|
pagehtml = out.getvalue()
|
||||||
|
@ -145,46 +140,23 @@ class MoinDump(_util.Script):
|
||||||
errcnt = errcnt + 1
|
errcnt = errcnt + 1
|
||||||
print >>sys.stderr, "*** Caught exception while writing page!"
|
print >>sys.stderr, "*** Caught exception while writing page!"
|
||||||
print >>errlog, "~" * 78
|
print >>errlog, "~" * 78
|
||||||
|
print >>errlog, file # page filename
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc(None, errlog)
|
traceback.print_exc(None, errlog)
|
||||||
finally:
|
finally:
|
||||||
import locale
|
timestamp = time.strftime("%Y-%m-%d %H:%M")
|
||||||
locale.setlocale(locale.LC_ALL, '')
|
filepath = os.path.join(outputdir, 'wiki', file)
|
||||||
timestamp = time.strftime("%A %d %B %Y à %H:%M")
|
fileout = codecs.open(filepath, 'w', config.charset)
|
||||||
filepath = os.path.join(outputdir, file)
|
fileout.write((page_template % {
|
||||||
fileout = open(filepath, 'w')
|
'charset': config.charset,
|
||||||
fileout.write(page_template % {
|
|
||||||
'pagename': pagename,
|
'pagename': pagename,
|
||||||
'pagenamewithlinks': pagenamewithlinks,
|
'pagenamewithlinks': pagenamewithlinks,
|
||||||
'pagehtml': pagehtml.replace('%s/rightsidebar/img' % config.url_prefix, '/img'),
|
'pagehtml': pagehtml,
|
||||||
'timestamp': timestamp,
|
'timestamp': timestamp,
|
||||||
})
|
'theme': request.cfg.theme_default,
|
||||||
|
}).replace("./monobook", ".."))
|
||||||
fileout.close()
|
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()
|
errlog.close()
|
||||||
if errcnt:
|
if errcnt:
|
||||||
|
@ -193,6 +165,18 @@ class MoinDump(_util.Script):
|
||||||
def run():
|
def run():
|
||||||
MoinDump().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__":
|
if __name__ == "__main__":
|
||||||
run()
|
run()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue