]> sipb.mit.edu Git - ikiwiki.git/blob - doc/tips/convert_mediawiki_to_ikiwiki/discussion.mdwn
15ddccb92e53540bc8b5daa39aefaed3d9f25d09
[ikiwiki.git] / doc / tips / convert_mediawiki_to_ikiwiki / discussion.mdwn
1 The u32 page is excellent, but I wonder if documenting the procedure here
2 would be worthwhile. Who knows, the remote site might disappear. But also
3 there are some variations on the approach that might be useful:
4
5  * using a python script and the dom library to extract the page names from
6    Special:Allpages (such as
7    <http://www.staff.ncl.ac.uk/jon.dowland/unix/docs/get_pagenames.py>)
8  * Or, querying the mysql back-end to get the names
9  * using WWW::MediaWiki for importing/exporting pages from the wiki, instead
10    of Special::Export
11
12 Also, some detail on converting mediawiki transclusion to ikiwiki inlines...
13
14 -- [[users/Jon]]
15
16 > "Who knows, the remote site might disappear.". Right now, it appears to
17 > have done just that. -- [[users/Jon]]
18
19
20 The iki-fast-load ruby script from the u32 page is given below:
21
22         #!/usr/bin/env ruby
23
24         # This script is called on the final sorted, de-spammed revision
25         # XML file.
26         #
27         # It doesn't currently check for no-op revisions...  I believe
28         # that git-fast-load will dutifully load them even though nothing
29         # happened.  I don't care to solve this by adding a file cache
30         # to this script.  You can run iki-diff-next.rb to highlight any
31         # empty revisions that need to be removed.
32         #
33         # This turns each node into an equivalent file.
34         #    It does not convert spaces to underscores in file names.
35         #       This would break wikilinks.
36         #       I suppose you could fix this with mod_speling or mod_rewrite.
37         #
38         # It replaces nodes in the Image: namespace with the files themselves.
39
40
41         require 'rubygems'
42         require 'node-callback'
43         require 'time'
44         require 'ostruct'
45
46
47         # pipe is the stream to receive the git-fast-import commands
48         # putfrom is true if this branch has existing commits on it, false if not.
49         def format_git_commit(pipe, f)
50            # Need to escape backslashes and double-quotes for git?
51            # No, git breaks when I do this. 
52            # For the filename "path with \\", git sez: bad default revision 'HEAD'
53            # filename = '"' + filename.gsub('\\', '\\\\\\\\').gsub('"', '\\"') + '"'
54
55            # In the calls below, length must be the size in bytes!!
56            # TODO: I haven't figured out how this works in the land of UTF8 and Ruby 1.9.
57            pipe.puts "commit #{f.branch}"
58            pipe.puts "committer #{f.username} <#{f.email}> #{f.timestamp.rfc2822}"
59            pipe.puts "data #{f.message.length}\n#{f.message}\n"
60            pipe.puts "from #{f.branch}^0" if f.putfrom
61            pipe.puts "M 644 inline #{f.filename}"
62            pipe.puts "data #{f.content.length}\n#{f.content}\n"
63            pipe.puts
64         end
65
66
67 Mediawiki.pm - A plugin which supports mediawiki format.
68
69         #!/usr/bin/perl
70         # By Scott Bronson.  Licensed under the GPLv2+ License.
71         # Extends Ikiwiki to be able to handle Mediawiki markup.
72         #
73         # To use the Mediawiki Plugin:
74         # - Install Text::MediawikiFormat
75         # - Turn of prefix_directives in your setup file.
76         #     (TODO: we probably don't need to do this anymore?)
77         #        prefix_directives => 1,
78         # - Add this plugin on Ikiwiki's path (perl -V, look for @INC)
79         #       cp mediawiki.pm something/IkiWiki/Plugin
80         # - And enable it in your setup file
81         #        add_plugins => [qw{mediawiki}],
82         # - Finally, turn off the link plugin in setup (this is important)
83         #        disable_plugins => [qw{link}],
84         # - Rebuild everything (actually, this should be automatic right?)
85         # - Now all files with a .mediawiki extension should be rendered properly.
86         
87         
88         package IkiWiki::Plugin::mediawiki;
89         
90         use warnings;
91         use strict;
92         use IkiWiki 2.00;
93         use URI;
94         
95         
96         # This is a gross hack...  We disable the link plugin so that our
97         # linkify routine is always called.  Then we call the link plugin
98         # directly for all non-mediawiki pages.  Ouch...  Hopefully Ikiwiki
99         # will be updated soon to support multiple link plugins.
100         require IkiWiki::Plugin::link;
101         
102         # Even if T:MwF is not installed, we can still handle all the linking.
103         # The user will just see Mediawiki markup rather than formatted markup.
104         eval q{use Text::MediawikiFormat ()};
105         my $markup_disabled = $@;
106         
107         # Work around a UTF8 bug in Text::MediawikiFormat
108         # http://rt.cpan.org/Public/Bug/Display.html?id=26880
109         unless($markup_disabled) {
110            no strict 'refs';
111            no warnings;
112            *{'Text::MediawikiFormat::uri_escape'} = \&URI::Escape::uri_escape_utf8;
113         }
114         
115         my %metaheaders;    # keeps track of redirects for pagetemplate.
116         my %tags;      # keeps track of tags for pagetemplate.
117         
118         
119         sub import { #{{{
120            hook(type => "checkconfig", id => "mediawiki", call => \&checkconfig);
121            hook(type => "scan", id => "mediawiki", call => \&scan);
122            hook(type => "linkify", id => "mediawiki", call => \&linkify);
123            hook(type => "htmlize", id => "mediawiki", call => \&htmlize);
124            hook(type => "pagetemplate", id => "mediawiki", call => \&pagetemplate);
125         } # }}}
126         
127         
128         sub checkconfig
129         {
130            return IkiWiki::Plugin::link::checkconfig(@_);
131         }
132         
133         
134         my $link_regexp = qr{
135             \[\[(?=[^!])        # beginning of link
136             ([^\n\r\]#|<>]+)      # 1: page to link to
137             (?:
138                 \#              # '#', beginning of anchor
139                 ([^|\]]+)       # 2: anchor text
140             )?                  # optional
141         
142             (?:
143                 \|              # followed by '|'
144                 ([^\]\|]*)      # 3: link text
145             )?                  # optional
146             \]\]                # end of link
147                 ([a-zA-Z]*)   # optional trailing alphas
148         }x;
149         
150         
151         # Convert spaces in the passed-in string into underscores.
152         # If passed in undef, returns undef without throwing errors.
153         sub underscorize
154         {
155            my $var = shift;
156            $var =~ tr{ }{_} if $var;
157            return $var;
158         }
159         
160         
161         # Underscorize, strip leading and trailing space, and scrunch
162         # multiple runs of spaces into one underscore.
163         sub scrunch
164         {
165            my $var = shift;
166            if($var) {
167               $var =~ s/^\s+|\s+$//g;      # strip leading and trailing space
168               $var =~ s/\s+/ /g;      # squash multiple spaces to one
169            }
170            return $var;
171         }
172         
173         
174         # Translates Mediawiki paths into Ikiwiki paths.
175         # It needs to be pretty careful because Mediawiki and Ikiwiki handle
176         # relative vs. absolute exactly opposite from each other.
177         sub translate_path
178         {
179            my $page = shift;
180            my $path = scrunch(shift);
181         
182            # always start from root unless we're doing relative shenanigans.
183            $page = "/" unless $path =~ /^(?:\/|\.\.)/;
184         
185            my @result = ();
186            for(split(/\//, "$page/$path")) {
187               if($_ eq '..') {
188                  pop @result;
189               } else {
190                  push @result, $_ if $_ ne "";
191               }
192            }
193         
194            # temporary hack working around http://ikiwiki.info/bugs/Can__39__t_create_root_page/index.html?updated
195            # put this back the way it was once this bug is fixed upstream.
196            # This is actually a major problem because now Mediawiki pages can't link from /Git/git-svn to /git-svn.  And upstream appears to be uninterested in fixing this bug.  :(
197            # return "/" . join("/", @result);
198            return join("/", @result);
199         }
200         
201         
202         # Figures out the human-readable text for a wikilink
203         sub linktext
204         {
205            my($page, $inlink, $anchor, $title, $trailing) = @_;
206            my $link = translate_path($page,$inlink);
207         
208            # translate_path always produces an absolute link.
209            # get rid of the leading slash before we display this link.
210            $link =~ s#^/##;
211         
212            my $out = "";
213            if($title) {
214                $out = IkiWiki::pagetitle($title);
215            } else {
216               $link = $inlink if $inlink =~ /^\s*\//;
217                $out = $anchor ? "$link#$anchor" : $link;
218               if(defined $title && $title eq "") {
219                  # a bare pipe appeared in the link...
220                  # user wants to strip namespace and trailing parens.
221                  $out =~ s/^[A-Za-z0-9_-]*://;
222                  $out =~ s/\s*\(.*\)\s*$//;
223               }
224               # A trailing slash suppresses the leading slash
225               $out =~ s#^/(.*)/$#$1#;
226            }
227            $out .= $trailing if defined $trailing;
228            return $out;
229         }
230         
231         
232         sub tagpage ($)
233         {
234            my $tag=shift;
235         
236            if (exists $config{tagbase} && defined $config{tagbase}) {
237               $tag=$config{tagbase}."/".$tag;
238            }
239         
240            return $tag;
241         }
242         
243         
244         # Pass a URL and optional text associated with it.  This call turns
245         # it into fully-formatted HTML the same way Mediawiki would.
246         # Counter is used to number untitled links sequentially on the page.
247         # It should be set to 1 when you start parsing a new page.  This call
248         # increments it automatically.
249         sub generate_external_link
250         {
251            my $url = shift;
252            my $text = shift;
253            my $counter = shift;
254         
255            # Mediawiki trims off trailing commas.
256            # And apparently it does entity substitution first.
257            # Since we can't, we'll fake it.
258         
259            # trim any leading and trailing whitespace
260            $url =~ s/^\s+|\s+$//g;
261         
262            # url properly terminates on > but must special-case &gt;
263            my $trailer = "";
264            $url =~ s{(\&(?:gt|lt)\;.*)$}{ $trailer = $1, ''; }eg;
265         
266            # Trim some potential trailing chars, put them outside the link.
267            my $tmptrail = "";
268            $url =~ s{([,)]+)$}{ $tmptrail .= $1, ''; }eg;
269            $trailer = $tmptrail . $trailer;
270         
271            my $title = $url;
272            if(defined $text) {
273               if($text eq "") {
274                  $text = "[$$counter]";
275                  $$counter += 1;
276               }
277               $text =~ s/^\s+|\s+$//g;
278               $text =~ s/^\|//;
279            } else {
280               $text = $url;
281            }
282         
283            return "<a href='$url' title='$title'>$text</a>$trailer";
284         }
285         
286         
287         # Called to handle bookmarks like [[#heading]] or <span class="createlink"><a href="http://u32.net/cgi-bin/ikiwiki.cgi?page=%20text%20&amp;from=Mediawiki_Plugin%2Fmediawiki&amp;do=create" rel="nofollow">?</a>#a</span>
288         sub generate_fragment_link
289         {
290            my $url = shift;
291            my $text = shift;
292         
293            my $inurl = $url;
294            my $intext = $text;
295            $url = scrunch($url);
296         
297            if(defined($text) && $text ne "") {
298               $text = scrunch($text);
299            } else {
300               $text = $url;
301            }
302         
303            $url = underscorize($url);
304         
305            # For some reason Mediawiki puts blank titles on all its fragment links.
306            # I don't see why we would duplicate that behavior here.
307            return "<a href='$url'>$text</a>";
308         }
309         
310         
311         sub generate_internal_link
312         {
313            my($page, $inlink, $anchor, $title, $trailing, $proc) = @_;
314         
315            # Ikiwiki's link link plugin wrecks this line when displaying on the site.
316            # Until the code highlighter plugin can turn off link finding,
317            # always escape double brackets in double quotes: [[
318            if($inlink eq '..') {
319               # Mediawiki doesn't touch links like [[..#hi|ho]].
320               return "[[" . $inlink . ($anchor?"#$anchor":"") .
321                  ($title?"|$title":"") . "]]" . $trailing;
322            }
323         
324            my($linkpage, $linktext);
325            if($inlink =~ /^ (:?) \s* Category (\s* \: \s*) ([^\]]*) $/x) {
326               # Handle category links
327               my $sep = $2;
328               $inlink = $3;
329               $linkpage = IkiWiki::linkpage(translate_path($page, $inlink));
330               if($1) {
331                  # Produce a link but don't add this page to the given category.
332                  $linkpage = tagpage($linkpage);
333                  $linktext = ($title ? '' : "Category$sep") .
334                     linktext($page, $inlink, $anchor, $title, $trailing);
335                  $tags{$page}{$linkpage} = 1;
336               } else {
337                  # Add this page to the given category but don't produce a link.
338                  $tags{$page}{$linkpage} = 1;
339                  &$proc(tagpage($linkpage), $linktext, $anchor);
340                  return "";
341               }
342            } else {
343               # It's just a regular link
344               $linkpage = IkiWiki::linkpage(translate_path($page, $inlink));
345               $linktext = linktext($page, $inlink, $anchor, $title, $trailing);
346            }
347         
348            return &$proc($linkpage, $linktext, $anchor);
349         }
350         
351         
352         sub check_redirect
353         {
354            my %params=@_;
355         
356            my $page=$params{page};
357            my $destpage=$params{destpage};
358            my $content=$params{content};
359         
360            return "" if $page ne $destpage;
361         
362            if($content !~ /^ \s* \#REDIRECT \s* \[\[ ( [^\]]+ ) \]\]/x) {
363               # this page isn't a redirect, render it normally.
364               return undef;
365            }
366         
367            # The rest of this function is copied from the redir clause
368            # in meta::preprocess and actually handles the redirect.
369         
370            my $value = $1;
371            $value =~ s/^\s+|\s+$//g;
372         
373            my $safe=0;
374            if ($value !~ /^\w+:\/\//) {
375               # it's a local link
376               my ($redir_page, $redir_anchor) = split /\#/, $value;
377         
378               add_depends($page, $redir_page);
379               my $link=bestlink($page, underscorize(translate_path($page,$redir_page)));
380               if (! length $link) {
381                  return "<b>Redirect Error:</b> <nowiki>[[$redir_page]] not found.</nowiki>";
382               }
383         
384               $value=urlto($link, $page);
385               $value.='#'.$redir_anchor if defined $redir_anchor;
386               $safe=1;
387         
388               # redir cycle detection
389               $pagestate{$page}{mediawiki}{redir}=$link;
390               my $at=$page;
391               my %seen;
392               while (exists $pagestate{$at}{mediawiki}{redir}) {
393                  if ($seen{$at}) {
394                     return "<b>Redirect Error:</b> cycle found on <nowiki>[[$at]]</nowiki>";
395                  }
396                  $seen{$at}=1;
397                  $at=$pagestate{$at}{mediawiki}{redir};
398               }
399            } else {
400               # it's an external link
401               $value = encode_entities($value);
402            }
403         
404            my $redir="<meta http-equiv=\"refresh\" content=\"0; URL=$value\" />";
405            $redir=scrub($redir) if !$safe;
406            push @{$metaheaders{$page}}, $redir;
407         
408            return "Redirecting to $value ...";
409         }
410         
411         
412         # Feed this routine a string containing <nowiki>...</nowiki> sections,
413         # this routine calls your callback for every section not within nowikis,
414         # collecting its return values and returning the rewritten string.
415         sub skip_nowiki
416         {
417            my $content = shift;
418            my $proc = shift;
419         
420            my $result = "";
421            my $state = 0;
422         
423            for(split(/(<nowiki[^>]*>.*?<\/nowiki\s*>)/s, $content)) {
424               $result .= ($state ? $_ : &$proc($_));
425               $state = !$state;
426            }
427         
428            return $result;
429         }
430         
431         
432         # Converts all links in the page, wiki and otherwise.
433         sub linkify (@)
434         {
435            my %params=@_;
436         
437            my $page=$params{page};
438            my $destpage=$params{destpage};
439            my $content=$params{content};
440         
441            my $file=$pagesources{$page};
442            my $type=pagetype($file);
443            my $counter = 1;
444         
445            if($type ne 'mediawiki') {
446               return IkiWiki::Plugin::link::linkify(@_);
447            }
448         
449            my $redir = check_redirect(%params);
450            return $redir if defined $redir;
451         
452            # this code was copied from MediawikiFormat.pm.
453            # Heavily changed because MF.pm screws up escaping when it does
454            # this awful hack: $uricCheat =~ tr/://d;
455            my $schemas = [qw(http https ftp mailto gopher)];
456            my $re = join "|", map {qr/\Q$_\E/} @$schemas;
457            my $schemes = qr/(?:$re)/;
458            # And this is copied from URI:
459            my $reserved   = q(;/?@&=+$,);   # NOTE: no colon or [] !
460            my $uric       = quotemeta($reserved) . $URI::unreserved . "%#";
461         
462            my $result = skip_nowiki($content, sub {
463               $_ = shift;
464         
465               # Escape any anchors
466               #s/<(a[\s>\/])/&lt;$1/ig;
467               # Disabled because this appears to screw up the aggregate plugin.
468               # I guess we'll rely on Iki to post-sanitize this sort of stuff.
469         
470               # Replace external links, http://blah or [http://blah]
471               s{\b($schemes:[$uric][:$uric]+)|\[($schemes:[$uric][:$uric]+)([^\]]*?)\]}{
472                  generate_external_link($1||$2, $3, \$counter)
473               }eg;
474         
475               # Handle links that only contain fragments.
476               s{ \[\[ \s* (\#[^|\]'"<>&;]+) (?:\| ([^\]'"<>&;]*))? \]\] }{
477                  generate_fragment_link($1, $2)
478               }xeg;
479         
480               # Match all internal links
481               s{$link_regexp}{
482                  generate_internal_link($page, $1, $2, $3, $4, sub {
483                     my($linkpage, $linktext, $anchor) = @_;
484                     return htmllink($page, $destpage, $linkpage,
485                        linktext => $linktext,
486                        anchor => underscorize(scrunch($anchor)));
487                  });
488               }eg;
489            
490               return $_;
491            });
492         
493            return $result;
494         }
495         
496         
497         # Find all WikiLinks in the page.
498         sub scan (@)
499         {
500            my %params = @_;
501            my $page=$params{page};
502            my $content=$params{content};
503         
504            my $file=$pagesources{$page};
505            my $type=pagetype($file);
506         
507            if($type ne 'mediawiki') {
508               return IkiWiki::Plugin::link::scan(@_);
509            }
510         
511            skip_nowiki($content, sub {
512               $_ = shift;
513               while(/$link_regexp/g) {
514                  generate_internal_link($page, $1, '', '', '', sub {
515                     my($linkpage, $linktext, $anchor) = @_;
516                     push @{$links{$page}}, $linkpage;
517                     return undef;
518                  });
519               }
520               return '';
521            });
522         }
523         
524         
525         # Convert the page to HTML.
526         sub htmlize (@)
527         {
528            my %params=@_;
529            my $page = $params{page};
530            my $content = $params{content};
531         
532         
533            return $content if $markup_disabled;
534         
535            # Do a little preprocessing to babysit Text::MediawikiFormat
536            # If a line begins with tabs, T:MwF won't convert it into preformatted blocks.
537            $content =~ s/^\t/    /mg;
538         
539            my $ret = Text::MediawikiFormat::format($content, {
540         
541                allowed_tags    => [#HTML
542                         # MediawikiFormat default
543                         qw(b big blockquote br caption center cite code dd
544                            div dl dt em font h1 h2 h3 h4 h5 h6 hr i li ol p
545                            pre rb rp rt ruby s samp small strike strong sub
546                            sup table td th tr tt u ul var),
547                          # Mediawiki Specific
548                          qw(nowiki),
549                          # Our additions
550                          qw(del ins),   # These should have been added all along.
551                          qw(span),   # Mediawiki allows span but that's rather scary...?
552                          qw(a),      # this is unfortunate; should handle links after rendering the page.
553                        ],
554         
555                allowed_attrs   => [
556                         qw(title align lang dir width height bgcolor),
557                         qw(clear), # BR
558                         qw(noshade), # HR
559                         qw(cite), # BLOCKQUOTE, Q
560                         qw(size face color), # FONT
561                         # For various lists, mostly deprecated but safe
562                         qw(type start value compact),
563                         # Tables
564                         qw(summary width border frame rules cellspacing
565                            cellpadding valign char charoff colgroup col
566                            span abbr axis headers scope rowspan colspan),
567                         qw(id class name style), # For CSS
568                         # Our additions
569                         qw(href),
570                        ],
571         
572               }, {
573               extended => 0,
574               absolute_links => 0,
575               implicit_links => 0
576               });
577         
578            return $ret;
579         }
580         
581         
582         # This is only needed to support the check_redirect call.
583         sub pagetemplate (@)
584         {
585            my %params = @_;
586            my $page = $params{page};
587            my $destpage = $params{destpage};
588            my $template = $params{template};
589         
590            # handle metaheaders for redirects
591            if (exists $metaheaders{$page} && $template->query(name => "meta")) {
592            # avoid duplicate meta lines
593               my %seen;
594               $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
595            }
596         
597            $template->param(tags => [
598               map {
599                  link => htmllink($page, $destpage, tagpage($_), rel => "tag")
600               }, sort keys %{$tags{$page}}
601            ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
602         
603            # It's an rss/atom template. Add any categories.
604            if ($template->query(name => "categories")) {
605               if (exists $tags{$page} && %{$tags{$page}}) {
606                  $template->param(categories => [map { category => $_ },
607                     sort keys %{$tags{$page}}]);
608               }
609            }
610         }
611         
612         1