1 20100428 - I just wrote a simple ruby script which will connect to a mysql server and then recreate the pages and their revision histories with Grit. It also does one simple conversion of equals titles to pounds. Enjoy!
3 <http://github.com/docunext/mediawiki2gitikiwiki>
9 The u32 page is excellent, but I wonder if documenting the procedure here
10 would be worthwhile. Who knows, the remote site might disappear. But also
11 there are some variations on the approach that might be useful:
13 * using a python script and the dom library to extract the page names from
14 Special:Allpages (such as
15 <http://www.staff.ncl.ac.uk/jon.dowland/unix/docs/get_pagenames.py>)
16 * Or, querying the mysql back-end to get the names
17 * using WWW::MediaWiki for importing/exporting pages from the wiki, instead
20 Also, some detail on converting mediawiki transclusion to ikiwiki inlines...
26 > "Who knows, the remote site might disappear.". Right now, it appears to
27 > have done just that. -- [[users/Jon]]
29 I have manage to recover most of the site using the Internet Archive. What
30 I was unable to retrieve I have rewritten. You can find a copy of the code
31 at <http://github.com/mithro/media2iki>
33 > This is excellent news. However, I'm still keen on there being a
34 > comprehensive and up-to-date set of instructions on *this* site. I wouldn't
35 > suggest importing that material into ikiwiki like-for-like (not least for
36 > [[licensing|freesoftware]] reasons), but it's excellent to have it available
37 > for reference, especially since it (currently) is the only set of
38 > instructions that gives you the whole history.
40 > The `mediawiki.pm` that was at u32.net is licensed GPL-2. I'd like to see it
41 > cleaned up and added to IkiWiki proper (although I haven't requested this
42 > yet, I suspect the way it (ab)uses linkify would disqualify it at present).
44 > I've imported Scott's initial `mediawiki.pm` into a repository at
45 > <http://github.com/jmtd/mediawiki.pm> as a start.
50 The iki-fast-load ruby script from the u32 page is given below:
54 # This script is called on the final sorted, de-spammed revision
57 # It doesn't currently check for no-op revisions... I believe
58 # that git-fast-load will dutifully load them even though nothing
59 # happened. I don't care to solve this by adding a file cache
60 # to this script. You can run iki-diff-next.rb to highlight any
61 # empty revisions that need to be removed.
63 # This turns each node into an equivalent file.
64 # It does not convert spaces to underscores in file names.
65 # This would break wikilinks.
66 # I suppose you could fix this with mod_speling or mod_rewrite.
68 # It replaces nodes in the Image: namespace with the files themselves.
72 require 'node-callback'
77 # pipe is the stream to receive the git-fast-import commands
78 # putfrom is true if this branch has existing commits on it, false if not.
79 def format_git_commit(pipe, f)
80 # Need to escape backslashes and double-quotes for git?
81 # No, git breaks when I do this.
82 # For the filename "path with \\", git sez: bad default revision 'HEAD'
83 # filename = '"' + filename.gsub('\\', '\\\\\\\\').gsub('"', '\\"') + '"'
85 # In the calls below, length must be the size in bytes!!
86 # TODO: I haven't figured out how this works in the land of UTF8 and Ruby 1.9.
87 pipe.puts "commit #{f.branch}"
88 pipe.puts "committer #{f.username} <#{f.email}> #{f.timestamp.rfc2822}"
89 pipe.puts "data #{f.message.length}\n#{f.message}\n"
90 pipe.puts "from #{f.branch}^0" if f.putfrom
91 pipe.puts "M 644 inline #{f.filename}"
92 pipe.puts "data #{f.content.length}\n#{f.content}\n"
96 > Would be nice to know where you could get "node-callbacks"... this thing is useless without it. --[[users/simonraven]]
99 Mediawiki.pm - A plugin which supports mediawiki format.
102 # By Scott Bronson. Licensed under the GPLv2+ License.
103 # Extends Ikiwiki to be able to handle Mediawiki markup.
105 # To use the Mediawiki Plugin:
106 # - Install Text::MediawikiFormat
107 # - Turn of prefix_directives in your setup file.
108 # (TODO: we probably don't need to do this anymore?)
109 # prefix_directives => 1,
110 # - Add this plugin on Ikiwiki's path (perl -V, look for @INC)
111 # cp mediawiki.pm something/IkiWiki/Plugin
112 # - And enable it in your setup file
113 # add_plugins => [qw{mediawiki}],
114 # - Finally, turn off the link plugin in setup (this is important)
115 # disable_plugins => [qw{link}],
116 # - Rebuild everything (actually, this should be automatic right?)
117 # - Now all files with a .mediawiki extension should be rendered properly.
120 package IkiWiki::Plugin::mediawiki;
128 # This is a gross hack... We disable the link plugin so that our
129 # linkify routine is always called. Then we call the link plugin
130 # directly for all non-mediawiki pages. Ouch... Hopefully Ikiwiki
131 # will be updated soon to support multiple link plugins.
132 require IkiWiki::Plugin::link;
134 # Even if T:MwF is not installed, we can still handle all the linking.
135 # The user will just see Mediawiki markup rather than formatted markup.
136 eval q{use Text::MediawikiFormat ()};
137 my $markup_disabled = $@;
139 # Work around a UTF8 bug in Text::MediawikiFormat
140 # http://rt.cpan.org/Public/Bug/Display.html?id=26880
141 unless($markup_disabled) {
144 *{'Text::MediawikiFormat::uri_escape'} = \&URI::Escape::uri_escape_utf8;
147 my %metaheaders; # keeps track of redirects for pagetemplate.
148 my %tags; # keeps track of tags for pagetemplate.
152 hook(type => "checkconfig", id => "mediawiki", call => \&checkconfig);
153 hook(type => "scan", id => "mediawiki", call => \&scan);
154 hook(type => "linkify", id => "mediawiki", call => \&linkify);
155 hook(type => "htmlize", id => "mediawiki", call => \&htmlize);
156 hook(type => "pagetemplate", id => "mediawiki", call => \&pagetemplate);
162 return IkiWiki::Plugin::link::checkconfig(@_);
166 my $link_regexp = qr{
167 \[\[(?=[^!]) # beginning of link
168 ([^\n\r\]#|<>]+) # 1: page to link to
170 \# # '#', beginning of anchor
171 ([^|\]]+) # 2: anchor text
176 ([^\]\|]*) # 3: link text
179 ([a-zA-Z]*) # optional trailing alphas
183 # Convert spaces in the passed-in string into underscores.
184 # If passed in undef, returns undef without throwing errors.
188 $var =~ tr{ }{_} if $var;
193 # Underscorize, strip leading and trailing space, and scrunch
194 # multiple runs of spaces into one underscore.
199 $var =~ s/^\s+|\s+$//g; # strip leading and trailing space
200 $var =~ s/\s+/ /g; # squash multiple spaces to one
206 # Translates Mediawiki paths into Ikiwiki paths.
207 # It needs to be pretty careful because Mediawiki and Ikiwiki handle
208 # relative vs. absolute exactly opposite from each other.
212 my $path = scrunch(shift);
214 # always start from root unless we're doing relative shenanigans.
215 $page = "/" unless $path =~ /^(?:\/|\.\.)/;
218 for(split(/\//, "$page/$path")) {
222 push @result, $_ if $_ ne "";
226 # temporary hack working around http://ikiwiki.info/bugs/Can__39__t_create_root_page/index.html?updated
227 # put this back the way it was once this bug is fixed upstream.
228 # 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. :(
229 # return "/" . join("/", @result);
230 return join("/", @result);
234 # Figures out the human-readable text for a wikilink
237 my($page, $inlink, $anchor, $title, $trailing) = @_;
238 my $link = translate_path($page,$inlink);
240 # translate_path always produces an absolute link.
241 # get rid of the leading slash before we display this link.
246 $out = IkiWiki::pagetitle($title);
248 $link = $inlink if $inlink =~ /^\s*\//;
249 $out = $anchor ? "$link#$anchor" : $link;
250 if(defined $title && $title eq "") {
251 # a bare pipe appeared in the link...
252 # user wants to strip namespace and trailing parens.
253 $out =~ s/^[A-Za-z0-9_-]*://;
254 $out =~ s/\s*\(.*\)\s*$//;
256 # A trailing slash suppresses the leading slash
257 $out =~ s#^/(.*)/$#$1#;
259 $out .= $trailing if defined $trailing;
268 if (exists $config{tagbase} && defined $config{tagbase}) {
269 $tag=$config{tagbase}."/".$tag;
276 # Pass a URL and optional text associated with it. This call turns
277 # it into fully-formatted HTML the same way Mediawiki would.
278 # Counter is used to number untitled links sequentially on the page.
279 # It should be set to 1 when you start parsing a new page. This call
280 # increments it automatically.
281 sub generate_external_link
287 # Mediawiki trims off trailing commas.
288 # And apparently it does entity substitution first.
289 # Since we can't, we'll fake it.
291 # trim any leading and trailing whitespace
292 $url =~ s/^\s+|\s+$//g;
294 # url properly terminates on > but must special-case >
296 $url =~ s{(\&(?:gt|lt)\;.*)$}{ $trailer = $1, ''; }eg;
298 # Trim some potential trailing chars, put them outside the link.
300 $url =~ s{([,)]+)$}{ $tmptrail .= $1, ''; }eg;
301 $trailer = $tmptrail . $trailer;
306 $text = "[$$counter]";
309 $text =~ s/^\s+|\s+$//g;
315 return "<a href='$url' title='$title'>$text</a>$trailer";
319 # Called to handle bookmarks like \[[#heading]] or <span class="createlink"><a href="http://u32.net/cgi-bin/ikiwiki.cgi?page=%20text%20&from=Mediawiki_Plugin%2Fmediawiki&do=create" rel="nofollow">?</a>#a</span>
320 sub generate_fragment_link
327 $url = scrunch($url);
329 if(defined($text) && $text ne "") {
330 $text = scrunch($text);
335 $url = underscorize($url);
337 # For some reason Mediawiki puts blank titles on all its fragment links.
338 # I don't see why we would duplicate that behavior here.
339 return "<a href='$url'>$text</a>";
343 sub generate_internal_link
345 my($page, $inlink, $anchor, $title, $trailing, $proc) = @_;
347 # Ikiwiki's link link plugin wrecks this line when displaying on the site.
348 # Until the code highlighter plugin can turn off link finding,
349 # always escape double brackets in double quotes: \[[
350 if($inlink eq '..') {
351 # Mediawiki doesn't touch links like \[[..#hi|ho]].
352 return "\[[" . $inlink . ($anchor?"#$anchor":"") .
353 ($title?"|$title":"") . "]]" . $trailing;
356 my($linkpage, $linktext);
357 if($inlink =~ /^ (:?) \s* Category (\s* \: \s*) ([^\]]*) $/x) {
358 # Handle category links
361 $linkpage = IkiWiki::linkpage(translate_path($page, $inlink));
363 # Produce a link but don't add this page to the given category.
364 $linkpage = tagpage($linkpage);
365 $linktext = ($title ? '' : "Category$sep") .
366 linktext($page, $inlink, $anchor, $title, $trailing);
367 $tags{$page}{$linkpage} = 1;
369 # Add this page to the given category but don't produce a link.
370 $tags{$page}{$linkpage} = 1;
371 &$proc(tagpage($linkpage), $linktext, $anchor);
375 # It's just a regular link
376 $linkpage = IkiWiki::linkpage(translate_path($page, $inlink));
377 $linktext = linktext($page, $inlink, $anchor, $title, $trailing);
380 return &$proc($linkpage, $linktext, $anchor);
388 my $page=$params{page};
389 my $destpage=$params{destpage};
390 my $content=$params{content};
392 return "" if $page ne $destpage;
394 if($content !~ /^ \s* \#REDIRECT \s* \[\[ ( [^\]]+ ) \]\]/x) {
395 # this page isn't a redirect, render it normally.
399 # The rest of this function is copied from the redir clause
400 # in meta::preprocess and actually handles the redirect.
403 $value =~ s/^\s+|\s+$//g;
406 if ($value !~ /^\w+:\/\//) {
408 my ($redir_page, $redir_anchor) = split /\#/, $value;
410 add_depends($page, $redir_page);
411 my $link=bestlink($page, underscorize(translate_path($page,$redir_page)));
412 if (! length $link) {
413 return "<b>Redirect Error:</b> <nowiki>\[[$redir_page]] not found.</nowiki>";
416 $value=urlto($link, $page);
417 $value.='#'.$redir_anchor if defined $redir_anchor;
420 # redir cycle detection
421 $pagestate{$page}{mediawiki}{redir}=$link;
424 while (exists $pagestate{$at}{mediawiki}{redir}) {
426 return "<b>Redirect Error:</b> cycle found on <nowiki>\[[$at]]</nowiki>";
429 $at=$pagestate{$at}{mediawiki}{redir};
432 # it's an external link
433 $value = encode_entities($value);
436 my $redir="<meta http-equiv=\"refresh\" content=\"0; URL=$value\" />";
437 $redir=scrub($redir) if !$safe;
438 push @{$metaheaders{$page}}, $redir;
440 return "Redirecting to $value ...";
444 # Feed this routine a string containing <nowiki>...</nowiki> sections,
445 # this routine calls your callback for every section not within nowikis,
446 # collecting its return values and returning the rewritten string.
455 for(split(/(<nowiki[^>]*>.*?<\/nowiki\s*>)/s, $content)) {
456 $result .= ($state ? $_ : &$proc($_));
464 # Converts all links in the page, wiki and otherwise.
469 my $page=$params{page};
470 my $destpage=$params{destpage};
471 my $content=$params{content};
473 my $file=$pagesources{$page};
474 my $type=pagetype($file);
477 if($type ne 'mediawiki') {
478 return IkiWiki::Plugin::link::linkify(@_);
481 my $redir = check_redirect(%params);
482 return $redir if defined $redir;
484 # this code was copied from MediawikiFormat.pm.
485 # Heavily changed because MF.pm screws up escaping when it does
486 # this awful hack: $uricCheat =~ tr/://d;
487 my $schemas = [qw(http https ftp mailto gopher)];
488 my $re = join "|", map {qr/\Q$_\E/} @$schemas;
489 my $schemes = qr/(?:$re)/;
490 # And this is copied from URI:
491 my $reserved = q(;/?@&=+$,); # NOTE: no colon or [] !
492 my $uric = quotemeta($reserved) . $URI::unreserved . "%#";
494 my $result = skip_nowiki($content, sub {
498 #s/<(a[\s>\/])/<$1/ig;
499 # Disabled because this appears to screw up the aggregate plugin.
500 # I guess we'll rely on Iki to post-sanitize this sort of stuff.
502 # Replace external links, http://blah or [http://blah]
503 s{\b($schemes:[$uric][:$uric]+)|\[($schemes:[$uric][:$uric]+)([^\]]*?)\]}{
504 generate_external_link($1||$2, $3, \$counter)
507 # Handle links that only contain fragments.
508 s{ \[\[ \s* (\#[^|\]'"<>&;]+) (?:\| ([^\]'"<>&;]*))? \]\] }{
509 generate_fragment_link($1, $2)
512 # Match all internal links
514 generate_internal_link($page, $1, $2, $3, $4, sub {
515 my($linkpage, $linktext, $anchor) = @_;
516 return htmllink($page, $destpage, $linkpage,
517 linktext => $linktext,
518 anchor => underscorize(scrunch($anchor)));
529 # Find all WikiLinks in the page.
533 my $page=$params{page};
534 my $content=$params{content};
536 my $file=$pagesources{$page};
537 my $type=pagetype($file);
539 if($type ne 'mediawiki') {
540 return IkiWiki::Plugin::link::scan(@_);
543 skip_nowiki($content, sub {
545 while(/$link_regexp/g) {
546 generate_internal_link($page, $1, '', '', '', sub {
547 my($linkpage, $linktext, $anchor) = @_;
548 push @{$links{$page}}, $linkpage;
557 # Convert the page to HTML.
561 my $page = $params{page};
562 my $content = $params{content};
565 return $content if $markup_disabled;
567 # Do a little preprocessing to babysit Text::MediawikiFormat
568 # If a line begins with tabs, T:MwF won't convert it into preformatted blocks.
569 $content =~ s/^\t/ /mg;
571 my $ret = Text::MediawikiFormat::format($content, {
573 allowed_tags => [#HTML
574 # MediawikiFormat default
575 qw(b big blockquote br caption center cite code dd
576 div dl dt em font h1 h2 h3 h4 h5 h6 hr i li ol p
577 pre rb rp rt ruby s samp small strike strong sub
578 sup table td th tr tt u ul var),
582 qw(del ins), # These should have been added all along.
583 qw(span), # Mediawiki allows span but that's rather scary...?
584 qw(a), # this is unfortunate; should handle links after rendering the page.
588 qw(title align lang dir width height bgcolor),
591 qw(cite), # BLOCKQUOTE, Q
592 qw(size face color), # FONT
593 # For various lists, mostly deprecated but safe
594 qw(type start value compact),
596 qw(summary width border frame rules cellspacing
597 cellpadding valign char charoff colgroup col
598 span abbr axis headers scope rowspan colspan),
599 qw(id class name style), # For CSS
614 # This is only needed to support the check_redirect call.
618 my $page = $params{page};
619 my $destpage = $params{destpage};
620 my $template = $params{template};
622 # handle metaheaders for redirects
623 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
624 # avoid duplicate meta lines
626 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
629 $template->param(tags => [
631 link => htmllink($page, $destpage, tagpage($_), rel => "tag")
632 }, sort keys %{$tags{$page}}
633 ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
635 # It's an rss/atom template. Add any categories.
636 if ($template->query(name => "categories")) {
637 if (exists $tags{$page} && %{$tags{$page}}) {
638 $template->param(categories => [map { category => $_ },
639 sort keys %{$tags{$page}}]);
648 Hello. Got ikiwiki running and I'm planning to convert my personal
649 Mediawiki wiki to ikiwiki so I can take offline copies around. If anyone
650 has an old copy of the instructions, or any advice on where to start I'd be
651 glad to hear it. Otherwise I'm just going to chronicle my journey on the
652 page.--[[users/Chadius]]
654 > Today I saw that someone is working to import wikipedia into git.
655 > <http://www.gossamer-threads.com/lists/wiki/foundation/181163>
656 > Since wikipedia uses mediawiki, perhaps his importer will work
657 > on mediawiki in general. It seems to produce output that could be
658 > used by the [[plugins/contrib/mediawiki]] plugin, if the filenames
659 > were fixed to use the right extension. --[[Joey]]
661 >> Here's another I found while browsing around starting from the link you gave Joey<br />
662 >> <http://github.com/scy/levitation><br />
663 >> As I don't run mediawiki anymore, but I still have my xz/gzip-compressed XML dumps,
664 >> it's certainly easier for me to do it this way; also a file or a set of files is easier to lug
665 >> around on some medium than a full mysqld or postgres master and relevant databases.