~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/script/export/dump.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- coding: iso-8859-1 -*-
2
2
"""
3
 
    MoinMoin - Dump a MoinMoin wiki to static pages
4
 
 
5
 
    You must run this script as owner of the wiki files, usually this is the
6
 
    web server user.
7
 
 
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.
11
 
 
 
3
MoinMoin - Dump a MoinMoin wiki to static pages
 
4
 
 
5
@copyright: 2002-2004 Juergen Hermann <jh@web.de>,
 
6
            2005-2006 MoinMoin:ThomasWaldmann
 
7
@license: GNU GPL, see COPYING for details.
12
8
"""
13
9
 
14
 
import sys, os, time, StringIO, codecs, shutil, re, errno
 
10
import sys, os, time, codecs, shutil, re, errno
15
11
 
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
20
15
 
21
 
url_prefix = "."
 
16
url_prefix_static = "."
22
17
logo_html = '<img src="logo.png">'
23
18
HTML_SUFFIX = ".html"
24
19
 
30
25
<link rel="stylesheet" type="text/css" media="all" charset="utf-8" href="%(theme)s/css/common.css">
31
26
<link rel="stylesheet" type="text/css" media="screen" charset="utf-8" href="%(theme)s/css/screen.css">
32
27
<link rel="stylesheet" type="text/css" media="print" charset="utf-8" href="%(theme)s/css/print.css">
 
28
<style type="text/css">
 
29
ul.pagetitle{
 
30
  display: inline;
 
31
  margin: 0;
 
32
  padding: 0;
 
33
  font-size: 1.5em;
 
34
}
 
35
li.pagetitle{
 
36
  display: inline;
 
37
  margin: 0;
 
38
}
 
39
td.noborder {
 
40
  border: 0;
 
41
}
 
42
</style>
33
43
</head>
34
44
<body>
35
45
<table>
36
46
<tr>
37
 
<td>
 
47
<td class="noborder">
38
48
%(logo_html)s
39
49
</td>
40
 
<td>
 
50
<td class="noborder">
 
51
<ul class="pagetitle">
 
52
<li class="pagetitle"><a class="backlink">%(pagename)s</a>
 
53
</ul>
 
54
<br><br>
41
55
%(navibar_html)s
42
56
</td>
43
57
</tr>
44
58
</table>
45
59
<hr>
46
60
<div id="page">
47
 
<h1 id="title">%(pagename)s</h1>
48
61
%(pagehtml)s
49
62
</div>
50
63
<hr>
53
66
</html>
54
67
'''
55
68
 
56
 
def _attachment(request, pagename, filename, outputdir):
 
69
 
 
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)
65
79
            try:
66
80
                os.makedirs(dest_dir)
67
81
            except:
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)
71
85
 
72
86
        shutil.copyfile(source_file, dest_file)
73
 
        _util.log('Writing "%s"...' % dest_url)
 
87
        script.log('Writing "%s"...' % dest_url)
74
88
        return dest_url
75
89
    else:
76
90
        return ""
77
 
  
78
 
 
79
 
class PluginScript(MoinScript):
80
 
    """ Dump script class """
81
 
    
 
91
 
 
92
 
 
93
class PluginScript(script.MoinScript):
 
94
    """\
 
95
Purpose:
 
96
========
 
97
This tool allows you to dump MoinMoin wiki pages to static HTML files.
 
98
 
 
99
Detailed Instructions:
 
100
======================
 
101
General syntax: moin [options] export dump [dump-options]
 
102
 
 
103
[options] usually should be:
 
104
    --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
 
105
 
 
106
[dump-options] see below:
 
107
    0. You must run this script as owner of the wiki files, usually this is the
 
108
       web server user.
 
109
 
 
110
    1. To dump all the pages on the wiki to the directory '/mywiki'
 
111
       moin ... export dump --target-dir=/mywiki
 
112
 
 
113
    2. To dump all the pages readable by 'JohnSmith' on the wiki to the directory
 
114
       '/mywiki'
 
115
       moin ... export dump --target-dir=/mywiki --username JohnSmith
 
116
"""
 
117
 
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"
 
123
        )
 
124
        self.parser.add_option(
 
125
            "-u", "--username", dest = "dump_user",
 
126
            help = "User the dump will be performed as (for ACL checks, etc)"
87
127
        )
88
128
 
89
129
    def mainloop(self):
91
131
 
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)
96
136
        try:
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)
102
142
 
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))
110
150
 
111
151
        self.init_request()
112
152
        request = self.request
113
153
 
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
 
156
 
 
157
        # use this user for permissions checks
 
158
        request.user = user.User(request, name=self.options.dump_user)
116
159
 
117
160
        pages = request.rootpage.getPageList(user='') # get list of all pages in wiki
118
161
        pages.sort()
127
170
 
128
171
        wikiutil.quoteWikinameURL = lambda pagename, qfn=wikiutil.quoteWikinameFS: (qfn(pagename) + HTML_SUFFIX)
129
172
 
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)
131
174
 
132
175
        errfile = os.path.join(outputdir, 'error.log')
133
176
        errlog = open(errfile, 'w')
134
177
        errcnt = 0
135
178
 
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
139
 
        
 
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
 
182
 
140
183
        navibar_html = ''
141
184
        for p in [page_front_page, page_title_index, page_word_index]:
142
 
            navibar_html += '&nbsp;[<a href="%s">%s</a>]' % (wikiutil.quoteWikinameURL(p), wikiutil.escape(p))
 
185
            navibar_html += '[<a href="%s">%s</a>]&nbsp;' % (wikiutil.quoteWikinameURL(p), wikiutil.escape(p))
143
186
 
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)
149
192
            try:
150
193
                pagehtml = ''
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
154
197
                try:
155
198
                    request.reset()
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)
157
200
                except:
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
162
205
                    import traceback
163
206
                    traceback.print_exc(None, errlog)
164
207
            finally:
187
230
 
188
231
        errlog.close()
189
232
        if errcnt:
190
 
            print >>sys.stderr, "*** %d error(s) occurred, see '%s'!" % (errcnt, errfile)
 
233
            print >> sys.stderr, "*** %d error(s) occurred, see '%s'!" % (errcnt, errfile)
191
234