1
1
# -*- coding: iso-8859-1 -*-
3
MoinMoin - Dump a MoinMoin wiki to static pages
5
You must run this script as owner of the wiki files, usually this is the
8
@copyright: 2002-2004 by J�rgen Hermann <jh@web.de>,
9
2005-2006 by Thomas Waldmann
10
@license: GNU GPL, see COPYING for details.
3
MoinMoin - Dump a MoinMoin wiki to static pages
5
@copyright: 2002-2004 Juergen Hermann <jh@web.de>,
6
2005-2006 MoinMoin:ThomasWaldmann
7
@license: GNU GPL, see COPYING for details.
14
import sys, os, time, StringIO, codecs, shutil, re, errno
10
import sys, os, time, codecs, shutil, re, errno
16
from MoinMoin import config, wikiutil, Page
17
from MoinMoin.script import _util
18
from MoinMoin.script._util import MoinScript
12
from MoinMoin import config, wikiutil, Page, user
13
from MoinMoin import script
19
14
from MoinMoin.action import AttachFile
16
url_prefix_static = "."
22
17
logo_html = '<img src="logo.png">'
23
18
HTML_SUFFIX = ".html"
56
def _attachment(request, pagename, filename, outputdir):
70
def _attachment(request, pagename, filename, outputdir, **kw):
57
71
filename = filename.encode(config.charset)
58
72
source_dir = AttachFile.getAttachDir(request, pagename)
59
73
source_file = os.path.join(source_dir, filename)
66
80
os.makedirs(dest_dir)
68
_util.fatal("Cannot create attachment directory '%s'" % dest_dir)
82
script.fatal("Cannot create attachment directory '%s'" % dest_dir)
69
83
elif not os.path.isdir(dest_dir):
70
_util.fatal("'%s' is not a directory" % dest_dir)
84
script.fatal("'%s' is not a directory" % dest_dir)
72
86
shutil.copyfile(source_file, dest_file)
73
_util.log('Writing "%s"...' % dest_url)
87
script.log('Writing "%s"...' % dest_url)
79
class PluginScript(MoinScript):
80
""" Dump script class """
93
class PluginScript(script.MoinScript):
97
This tool allows you to dump MoinMoin wiki pages to static HTML files.
99
Detailed Instructions:
100
======================
101
General syntax: moin [options] export dump [dump-options]
103
[options] usually should be:
104
--config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
106
[dump-options] see below:
107
0. You must run this script as owner of the wiki files, usually this is the
110
1. To dump all the pages on the wiki to the directory '/mywiki'
111
moin ... export dump --target-dir=/mywiki
113
2. To dump all the pages readable by 'JohnSmith' on the wiki to the directory
115
moin ... export dump --target-dir=/mywiki --username JohnSmith
82
118
def __init__(self, argv=None, def_values=None):
83
MoinScript.__init__(self, argv, def_values)
84
self.parser.add_option(
85
"-t", "--target-dir", dest="target_dir",
86
help="Write html dump to DIRECTORY"
119
script.MoinScript.__init__(self, argv, def_values)
120
self.parser.add_option(
121
"-t", "--target-dir", dest = "target_dir",
122
help = "Write html dump to DIRECTORY"
124
self.parser.add_option(
125
"-u", "--username", dest = "dump_user",
126
help = "User the dump will be performed as (for ACL checks, etc)"
89
129
def mainloop(self):
92
132
# Prepare output directory
93
133
if not self.options.target_dir:
94
_util.fatal("you must use --target-dir=/your/output/path to specify the directory we write the html files to")
134
script.fatal("you must use --target-dir=/your/output/path to specify the directory we write the html files to")
95
135
outputdir = os.path.abspath(self.options.target_dir)
97
137
os.mkdir(outputdir)
98
_util.log("Created output directory '%s'!" % outputdir)
138
script.log("Created output directory '%s'!" % outputdir)
99
139
except OSError, err:
100
140
if err.errno != errno.EEXIST:
101
_util.fatal("Cannot create output directory '%s'!" % outputdir)
141
script.fatal("Cannot create output directory '%s'!" % outputdir)
103
143
# Insert config dir or the current directory to the start of the path.
104
144
config_dir = self.options.config_dir
105
145
if config_dir and os.path.isfile(config_dir):
106
146
config_dir = os.path.dirname(config_dir)
107
147
if config_dir and not os.path.isdir(config_dir):
108
_util.fatal("bad path given to --config-dir option")
148
script.fatal("bad path given to --config-dir option")
109
149
sys.path.insert(0, os.path.abspath(config_dir or os.curdir))
111
151
self.init_request()
112
152
request = self.request
114
# fix url_prefix so we get relative paths in output html
115
request.cfg.url_prefix = url_prefix
154
# fix url_prefix_static so we get relative paths in output html
155
request.cfg.url_prefix_static = url_prefix_static
157
# use this user for permissions checks
158
request.user = user.User(request, name=self.options.dump_user)
117
160
pages = request.rootpage.getPageList(user='') # get list of all pages in wiki
128
171
wikiutil.quoteWikinameURL = lambda pagename, qfn=wikiutil.quoteWikinameFS: (qfn(pagename) + HTML_SUFFIX)
130
AttachFile.getAttachUrl = lambda pagename, filename, request, addts=0, escaped=0: (_attachment(request, pagename, filename, outputdir))
173
AttachFile.getAttachUrl = lambda pagename, filename, request, **kw: _attachment(request, pagename, filename, outputdir, **kw)
132
175
errfile = os.path.join(outputdir, 'error.log')
133
176
errlog = open(errfile, 'w')
136
page_front_page = wikiutil.getSysPage(request, request.cfg.page_front_page).page_name
137
page_title_index = wikiutil.getSysPage(request, 'TitleIndex').page_name
138
page_word_index = wikiutil.getSysPage(request, 'WordIndex').page_name
179
page_front_page = wikiutil.getLocalizedPage(request, request.cfg.page_front_page).page_name
180
page_title_index = wikiutil.getLocalizedPage(request, 'TitleIndex').page_name
181
page_word_index = wikiutil.getLocalizedPage(request, 'WordIndex').page_name
140
183
navibar_html = ''
141
184
for p in [page_front_page, page_title_index, page_word_index]:
142
navibar_html += ' [<a href="%s">%s</a>]' % (wikiutil.quoteWikinameURL(p), wikiutil.escape(p))
185
navibar_html += '[<a href="%s">%s</a>] ' % (wikiutil.quoteWikinameURL(p), wikiutil.escape(p))
144
187
urlbase = request.url # save wiki base url
145
188
for pagename in pages:
146
189
# we have the same name in URL and FS
147
file = wikiutil.quoteWikinameURL(pagename)
148
_util.log('Writing "%s"...' % file)
190
file = wikiutil.quoteWikinameURL(pagename)
191
script.log('Writing "%s"...' % file)
151
request.url = urlbase + pagename # add current pagename to url base
194
request.url = urlbase + pagename # add current pagename to url base
152
195
page = Page.Page(request, pagename)
153
196
request.page = page
156
pagehtml = request.redirectedOutput(page.send_page, request, count_hit=0, content_only=1)
199
pagehtml = request.redirectedOutput(page.send_page, count_hit=0, content_only=1)
158
201
errcnt = errcnt + 1
159
print >>sys.stderr, "*** Caught exception while writing page!"
160
print >>errlog, "~" * 78
161
print >>errlog, file # page filename
202
print >> sys.stderr, "*** Caught exception while writing page!"
203
print >> errlog, "~" * 78
204
print >> errlog, file # page filename
163
206
traceback.print_exc(None, errlog)