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

« back to all changes in this revision

Viewing changes to contrib/phpwiki_migration/phpwiki2moinmoin.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:
61
61
db = "wiki"
62
62
user = "wiki"
63
63
passwd = "wiki"
64
 
 
64
#
65
65
# This is a part of an SQL request (part of the WHERE, actually) that
66
66
# allows you to select which pages get moved over
67
 
 
67
#
68
68
# do only those pages:
69
69
pagename = ""
70
 
# Example: 
 
70
# Example:
71
71
#pagename = "pagename='ParseTest' AND"
72
 
 
72
#
73
73
# the path to the moinmoin wiki, leave empty if you know what you're
74
74
# doing
75
75
wikipath = '/var/local/lib/wiki'
81
81
# safe.
82
82
editExistingPages = 0
83
83
 
84
 
 
84
#
85
85
# END OF CONFIGURATION DIRECTIVES
86
 
 
86
#
87
87
# XXX: the interactive way will be something like this:
88
 
 
88
#
89
89
# print "I will need the host/database/username/password of the phpwiki"
90
90
# print "Host:",
91
91
# host = raw_input()
97
97
# passwd = getpass()
98
98
# print "Pagename:"
99
99
# pagename = raw_input()
100
 
 
100
#
101
101
# But right now this script DOES NOT COMPLETELY WORK, so it often has
102
102
# to be ran and reran.
103
103
 
110
110
        sys.path.append(wikipath)
111
111
 
112
112
from MoinMoin.PageEditor import PageEditor
113
 
from MoinMoin.request import RequestCLI
 
113
from MoinMoin.request.request_cli import Request
114
114
 
115
115
# the block parser deals with the whole text to be converted
116
116
#
140
140
                                        result += [match.group(1)]
141
141
                                result += ['}}}']
142
142
                                if match.group(2) != '':
143
 
                                        result += [lineparser(match.group(2),markup)]
 
143
                                        result += [lineparser(match.group(2), markup)]
144
144
                                pre = 0
145
145
                        else:
146
146
                                # don't parse pre data
155
155
                                # not what's after
156
156
                                pre = match.group(2)
157
157
                                if match.group(1) != '':
158
 
                                        result += [lineparser(match.group(1),markup)]
 
158
                                        result += [lineparser(match.group(1), markup)]
159
159
                                result += ['{{{']
160
 
                                if match.group(3) != '':        
 
160
                                if match.group(3) != '':
161
161
                                        result += [match.group(3)]
162
162
 
163
163
                        else:
164
164
                                # "common case": normal line parsing
165
 
                                result += [lineparser(line,markup)]
 
165
                                result += [lineparser(line, markup)]
166
166
 
167
167
        text = "\n".join(result)
168
168
        return text
175
175
# markup is 1 (old style) or 2 (new style)
176
176
def lineparser(text, markup):
177
177
        # headlines
178
 
        p=re.compile('^!!!(.*)$',re.MULTILINE)
179
 
        text = p.sub(r'= \1 =',text)
180
 
        p=re.compile('^!!(.*)$',re.MULTILINE)
181
 
        text = p.sub(r'== \1 ==',text)
182
 
        p=re.compile('^!(.*)$',re.MULTILINE)
183
 
        text = p.sub(r'=== \1 ===',text)
 
178
        p = re.compile('^!!!(.*)$', re.MULTILINE)
 
179
        text = p.sub(r'= \1 =', text)
 
180
        p = re.compile('^!!(.*)$', re.MULTILINE)
 
181
        text = p.sub(r'== \1 ==', text)
 
182
        p = re.compile('^!(.*)$', re.MULTILINE)
 
183
        text = p.sub(r'=== \1 ===', text)
184
184
 
185
185
        # pictures
186
 
        p=re.compile('^\s*\[(http:.*(png|jpg|gif))\]',re.MULTILINE)
187
 
        text = p.sub(r'\n\1',text)
 
186
        p = re.compile('^\s*\[(http:.*(png|jpg|gif))\]', re.MULTILINE)
 
187
        text = p.sub(r'\n{{\1}}', text)
188
188
 
189
189
        # links
190
190
        # the links like [http://]
191
 
        p=re.compile('(?<!~)\[(http|https|ftp|nntp|news|mailto|telnet|file)(://.*?)\]',re.MULTILINE)
192
 
        text = p.sub(r'\1\2',text)
193
 
        
 
191
        p = re.compile('(?<!~)\[(http|https|ftp|nntp|news|mailto|telnet|file)(://.*?)\]', re.MULTILINE)
 
192
        text = p.sub(r'\1\2', text)
 
193
 
194
194
        # the [links like this]
195
 
        p=re.compile('(?<!~|#)\[([^]\|]*)\]',re.MULTILINE)
196
 
        text = p.sub(r'["\1"]',text)
 
195
        p = re.compile('(?<!~|#)\[([^]\|]*)\]', re.MULTILINE)
 
196
        text = p.sub(r'[[\1]]', text)
197
197
 
198
198
        # links like [foo | http://...]
199
 
        q=re.compile('(?<!~|#)\[([^]#]*?)\s*\|\s*([^]#\s]+?://[^]\s]+?)\]',re.MULTILINE)
200
 
        text = q.sub(r'[\2 \1]',text)
 
199
        q = re.compile('(?<!~|#)\[([^]#]*?)\s*\|\s*([^]#\s]+?://[^]\s]+?)\]', re.MULTILINE)
 
200
        text = q.sub(r'[[\2|\1]]', text)
201
201
 
202
202
        # [fooo | bar]
203
 
        p=re.compile('(?<!~|#)\[([^]]*?)\s*\|\s*([^]\s]+?)\]',re.MULTILINE)
204
 
        text = p.sub(r'[:\2:\1]',text)
 
203
        p = re.compile('(?<!~|#)\[([^]]*?)\s*\|\s*([^]\s]+?)\]', re.MULTILINE)
 
204
        text = p.sub(r'[[\2|\1]]', text)
205
205
 
206
206
        # XXX: the following constructs are broken. I don't know how
207
207
        # to express that in Moin
208
208
        # [OtherPage#foo] [named|OtherPage#foo]
209
209
 
210
210
        # anchors
211
 
        # #[foo] => [[Anchor(foo)]]foo
212
 
        # #[|foo] => [[Anchor(foo)]]
213
 
        # #[howdy|foo] => [[Anchor(foo)]]howdy
 
211
        # #[foo] => <<Anchor(foo)>>foo
 
212
        # #[|foo] => <<Anchor(foo)>>
 
213
        # #[howdy|foo] => <<Anchor(foo)>>howdy
214
214
        #
215
215
        # rest should just go along
216
 
        p=re.compile('#\[([^]|]*)\]', re.MULTILINE)
217
 
        text = p.sub(r'[[Anchor(\1)]]\1', text)
218
 
        p=re.compile('#\[\|+([^]\|]*)\]', re.MULTILINE)
219
 
        text = p.sub(r'[[Anchor(\1)]]', text)
220
 
        p=re.compile('#\[([^]\|]*)\|+([^]\|]*)\]', re.MULTILINE)
221
 
        text = p.sub(r'[[Anchor(\2)]]\1', text)
 
216
        p = re.compile('#\[([^]|]*)\]', re.MULTILINE)
 
217
        text = p.sub(r'<<Anchor(\1)>>\1', text)
 
218
        p = re.compile('#\[\|+([^]\|]*)\]', re.MULTILINE)
 
219
        text = p.sub(r'<<Anchor(\1)>>', text)
 
220
        p = re.compile('#\[([^]\|]*)\|+([^]\|]*)\]', re.MULTILINE)
 
221
        text = p.sub(r'<<Anchor(\2)>>\1', text)
222
222
 
223
223
        # indented text
224
224
        # this might work for old style
225
225
        if markup == 1:
226
 
                p=re.compile('^ (.*)$')
227
 
                text = p.sub(r'{{{\n\1\n}}}',text)
 
226
                p = re.compile('^ (.*)$')
 
227
                text = p.sub(r'{{{\n\1\n}}}', text)
228
228
 
229
229
        # lists (regexp taken from phpwiki/lib/BlockParser.php:1.37)
230
 
        p=re.compile('^\ {0,4}\
 
230
        p = re.compile('^\ {0,4}\
231
231
                (?: \+\
232
232
                  | -(?!-)\
233
233
                  | [o](?=\ )\
234
234
                  | [*] (?!(?=\S)[^*]*(?<=\S)[*](?:\\s|[-)}>"\'\\/:.,;!?_*=]) )\
235
 
                )\ *(?=\S)',re.MULTILINE|re.VERBOSE)
236
 
        text = p.sub(r' * ',text)
237
 
        p=re.compile(' {0,4}(?:\\# (?!\[.*\])) *(?=\S)',re.MULTILINE)
238
 
        text = p.sub(r' 1. ',text)
 
235
                )\ *(?=\S)', re.MULTILINE|re.VERBOSE)
 
236
        text = p.sub(r' * ', text)
 
237
        p = re.compile(' {0,4}(?:\\# (?!\[.*\])) *(?=\S)', re.MULTILINE)
 
238
        text = p.sub(r' 1. ', text)
239
239
 
240
240
        if markup == 1:
241
241
                # bold (old style)
242
 
                p=re.compile('__(\w*)',re.MULTILINE)
243
 
                text = p.sub(r"'''\1",text)
244
 
                p=re.compile('(\w*)__',re.MULTILINE)
245
 
                text = p.sub(r"\1'''",text)
 
242
                p = re.compile('__(\w*)', re.MULTILINE)
 
243
                text = p.sub(r"'''\1", text)
 
244
                p = re.compile('(\w*)__', re.MULTILINE)
 
245
                text = p.sub(r"\1'''", text)
246
246
                # emphasis is the same
247
247
        else:
248
248
                # XXX: this doesn't do anything yet
249
 
                # 
 
249
                #
250
250
                # translated from getStartRegexp() in
251
251
                # phpwiki/lib/InlineParser.php:418
252
 
                i = "_ (?! _)";
253
 
                b = "\\* (?! \\*)";
254
 
                tt = "= (?! =)";
 
252
                i = "_ (?! _)"
 
253
                b = "\\* (?! \\*)"
 
254
                tt = "= (?! =)"
255
255
 
256
256
                # any of the three.
257
 
                any = "(?: " + i + "|" + b + "|" + tt + ")";
 
257
                any = "(?: " + i + "|" + b + "|" + tt + ")"
258
258
 
259
259
                # Any of [_*=] is okay if preceded by space or one of [-"'/:]
260
260
                # _ or * is okay after = as long as not immediately followed by =
270
270
                        "(?<= < ) " + any + " (?! > )|" + \
271
271
                        "(?<= \\( ) " + any + " (?! \\) )" + \
272
272
                        ")"
273
 
                
 
273
 
274
274
                # Any of the above must be immediately followed by non-whitespace.
275
 
                start_regexp = start + "(?= \S)";
276
 
                
 
275
                start_regexp = start + "(?= \S)"
 
276
 
277
277
 
278
278
        # PLUGINS
279
279
 
280
280
        # calendar plugin
281
 
        p=re.compile('<\?plugin Calendar month=(\d*) year=(\d*)\?>',re.MULTILINE)
282
 
        text = p.sub(r'[[MonthCalendar(,\2,\1)]]',text)
283
 
        p=re.compile('<\?plugin Calendar\?>',re.MULTILINE)
284
 
        text = p.sub(r'[[MonthCalendar]]',text)
 
281
        p = re.compile('<\?plugin Calendar month=(\d*) year=(\d*)\?>', re.MULTILINE)
 
282
        text = p.sub(r'<<MonthCalendar(,\2,\1)>>', text)
 
283
        p = re.compile('<\?plugin Calendar\?>', re.MULTILINE)
 
284
        text = p.sub(r'<<MonthCalendar>>', text)
285
285
 
286
286
        # BackLinks
287
 
        p=re.compile('<\?plugin\s+BackLinks.*?\?>', re.MULTILINE)
288
 
        text = p.sub(r"[[FullSearch()]]",text)
 
287
        p = re.compile('<\?plugin\s+BackLinks.*?\?>', re.MULTILINE)
 
288
        text = p.sub(r"<<FullSearch()>>", text)
289
289
 
290
290
        # FullSearch
291
 
        p=re.compile('<\?plugin\s+FullTextSearch.*?(s=.*?)?\?>', re.MULTILINE)
292
 
        text = p.sub(r'[[FullSearch()]]',text)
 
291
        p = re.compile('<\?plugin\s+FullTextSearch.*?(s=.*?)?\?>', re.MULTILINE)
 
292
        text = p.sub(r'<<FullSearch()>>', text)
293
293
 
294
294
        # RecentChanges
295
 
        p=re.compile('<\?plugin\s+RecentChanges.*?\?>', re.MULTILINE)
296
 
        text = p.sub(r'[[RecentChanges]]',text)
297
 
        
 
295
        p = re.compile('<\?plugin\s+RecentChanges.*?\?>', re.MULTILINE)
 
296
        text = p.sub(r'<<RecentChanges>>', text)
 
297
 
298
298
        # tables (old style)
299
 
        p=re.compile('^(\|.*)$',re.MULTILINE)
300
 
        text = p.sub(r'\1|',text)
301
 
        p=re.compile('\|',re.MULTILINE)
302
 
        text = p.sub(r'||',text)
303
 
        p=re.compile('\|\|<',re.MULTILINE)
304
 
        text = p.sub(r'||<(>',text)
305
 
        p=re.compile('\|\|>',re.MULTILINE)
306
 
        text = p.sub(r'||<)>',text)
 
299
        p = re.compile('^(\|.*)$', re.MULTILINE)
 
300
        text = p.sub(r'\1|', text)
 
301
        p = re.compile('\|', re.MULTILINE)
 
302
        text = p.sub(r'||', text)
 
303
        p = re.compile('\|\|<', re.MULTILINE)
 
304
        text = p.sub(r'||<(>', text)
 
305
        p = re.compile('\|\|>', re.MULTILINE)
 
306
        text = p.sub(r'||<)>', text)
307
307
 
308
308
        if markup == 2:
309
309
                # moinmoin tables are on one line
310
 
                p=re.compile('\|\|\s*\n',re.MULTILINE)
311
 
                text = p.sub(r'||',text)
312
 
        
 
310
                p = re.compile('\|\|\s*\n', re.MULTILINE)
 
311
                text = p.sub(r'||', text)
 
312
 
313
313
        # mailto
314
 
        p=re.compile('mailto:',re.MULTILINE)
315
 
        text = p.sub(r'',text)
 
314
        p = re.compile('mailto:', re.MULTILINE)
 
315
        text = p.sub(r'', text)
316
316
 
317
317
        # line breaks
318
 
        p=re.compile('(?<!~)%%%',re.MULTILINE)
319
 
        text = p.sub(r'[[BR]]',text)
 
318
        p = re.compile('(?<!~)%%%', re.MULTILINE)
 
319
        text = p.sub(r'<<BR>>', text)
320
320
 
321
321
        # unescape
322
322
        # This must stay the last filter or things will break real bad
323
 
        p=re.compile('~(~?)',re.MULTILINE)
324
 
        text = p.sub(r'\1',text)
 
323
        p = re.compile('~(~?)', re.MULTILINE)
 
324
        text = p.sub(r'\1', text)
325
325
 
326
326
        return text
327
327
 
345
345
                host=host,
346
346
                db=db,
347
347
                user=user,
348
 
                passwd=passwd
 
348
                passwd=passwd,
349
349
        )
350
 
cursor = db.cursor();
 
350
cursor = db.cursor()
351
351
 
352
 
stmt="SELECT pagename,content,versiondata FROM page,recent,version WHERE " + pagename + \
353
 
      " page.id=version.id AND version.id=recent.id AND version=latestversion" + \
354
 
      " ORDER BY pagename"
 
352
stmt = "SELECT pagename,content,versiondata FROM page,recent,version WHERE " + pagename + \
 
353
       " page.id=version.id AND version.id=recent.id AND version=latestversion" + \
 
354
       " ORDER BY pagename"
355
355
cursor.execute(stmt)
356
356
 
357
357
# loop over the matching phpwiki pages
358
358
result = cursor.fetchall()
359
 
for pagename,content,versiondata in result:
 
359
for pagename, content, versiondata in result:
360
360
        utf8pagename = unicode(pagename, 'latin-1')
361
 
        request = RequestCLI(utf8pagename)
 
361
        request = Request(utf8pagename)
362
362
        page = PageEditor(utf8pagename, request)
363
363
        print pagename,
364
364
        # overwriting pages if selecting only some
365
365
        if not editExistingPages and page.exists():
366
366
                print " already exists, skipping"
367
367
                continue
368
 
        
 
368
 
369
369
        # find out in the serialized field what markup type (old
370
370
        # style?) this page is in
371
371
        match = re.compile('s:6:"markup";s:[0-9]*:"([0-9]*)";').search(versiondata)
372
 
        if match != None:
 
372
        if match is not None:
373
373
                # markup is 1 (old style) or 2 (new style)
374
374
                markup = match.group(1)
375
375
        else:
382
382
        print (markup == 2 and [""] or ["(old style)"])[0],
383
383
 
384
384
        # do the actual parsing of this page and save it
385
 
        text=blockparser(content,markup)
 
385
        text = blockparser(content, markup)
386
386
        try:
387
 
                err = page.saveText(unicode(text,'latin-1'), '0')
 
387
                err = page.saveText(unicode(text, 'latin-1'), '0')
388
388
        # the exceptions thrown by saveText are errors or messages, so
389
389
        # just print both
390
390
        except Exception, msg:
392
392
        else:
393
393
                if err:
394
394
                        print err,
395
 
                
 
395
 
396
396
        # the EOL
397
397
        print
398
 
        
 
398
 
399
399
db.close
 
400