]> sipb.mit.edu Git - ikiwiki.git/blob - doc/tips/convert_mediawiki_to_ikiwiki.mdwn
ce0c684e7615e17784e9f6e1d34b666028e464db
[ikiwiki.git] / doc / tips / convert_mediawiki_to_ikiwiki.mdwn
1 [[!toc levels=2]]
2
3 Mediawiki is a dynamically-generated wiki which stores it's data in a
4 relational database. Pages are marked up using a proprietary markup. It is
5 possible to import the contents of a Mediawiki site into an ikiwiki,
6 converting some of the Mediawiki conventions into Ikiwiki ones.
7
8 The following instructions describe ways of obtaining the current version of
9 the wiki. We do not yet cover importing the history of edits.
10
11 ## Step 1: Getting a list of pages
12
13 The first bit of information you require is a list of pages in the Mediawiki.
14 There are several different ways of obtaining these.
15
16 ### Parsing the output of `Special:Allpages`
17
18 Mediawikis have a special page called `Special:Allpages` which list all the
19 pages for a given namespace on the wiki.
20
21 If you fetch the output of this page to a local file with something like
22
23     wget -q -O tmpfile 'http://your-mediawiki/wiki/Special:Allpages'
24
25 You can extract the list of page names using the following python script. Note
26 that this script is sensitive to the specific markup used on the page, so if
27 you have tweaked your mediawiki theme a lot from the original, you will need
28 to adjust this script too:
29
30     from xml.dom.minidom import parse, parseString
31     
32     dom = parse(argv[1])
33     tables = dom.getElementsByTagName("table")
34     pagetable = tables[-1]
35     anchors = pagetable.getElementsByTagName("a")
36     for a in anchors:
37         print a.firstChild.toxml().\
38             replace('&,'&').\
39             replace('&lt;','<').\
40             replace('&gt;','>')
41
42 Also, if you have pages with titles that need to be encoded to be represented
43 in HTML, you may need to add further processing to the last line.
44
45 ### Querying the database
46
47 If you have access to the relational database in which your mediawiki data is
48 stored, it is possible to derive a list of page names from this.
49
50 ## Step 2: fetching the page data
51
52 Once you have a list of page names, you can fetch the data for each page.
53
54 ### Method 1: via HTTP and `action=raw`
55
56 You need to create two derived strings from the page titles already: the
57 destination path for the page and the source URL. Assuming `$pagename` 
58 contains a pagename obtained above, and `$wiki` contains the URL to your
59 mediawiki's `index.php` file:
60
61     src=`echo "$pagename" | tr ' ' _ | sed 's,&,&amp;,g'`
62     dest=`"$pagename" | tr ' ' _ | sed 's,&,__38__,g'`
63     
64     mkdir -p `dirname "$dest"`
65     wget -q "$wiki?title=$src&action=raw" -O "$dest"
66
67 ### Method 2: via HTTP and `Special:Export`
68
69 Mediawiki also has a special page `Special:Export` which can be used to obtain
70 the source of the page and other metadata such as the last contributor, or the
71 full history, etc.
72
73 You need to send a `POST` request to the `Special:Export` page. See the source
74 of the page fetched via `GET` to determine the correct arguments.
75
76 You will then need to write an XML parser to extract the data you need from
77 the result.
78
79 ### Method 3: via the database
80
81 It is possible to extract the page data from the database with some
82 well-crafted queries.
83
84 ## Step 2: format conversion
85
86 The next step is to convert Mediawiki conventions into Ikiwiki ones. These
87 include
88
89  * convert Categories into tags
90
91     import sys, re
92     pattern =  r'\[\[Category:([^\]]+)\]\]'
93     
94     def manglecat(mo):
95             return '[[!tag %s]]' % mo.group(1).strip().replace(' ','_')
96             
97     for line in sys.stdin.readlines():
98             res = re.match(pattern, line)
99             if res:
100                     sys.stdout.write(re.sub(pattern, manglecat, line))
101             else: sys.stdout.write(line)
102
103 ## Step 3: Mediawiki plugin
104
105 The [[plugins/contrib/mediawiki]] plugin can be used by ikiwiki to interpret
106 most of the Mediawiki syntax.
107
108 ## External links
109
110 [[sabr]] used to explain how to [import MediaWiki content into
111 git](http://u32.net/Mediawiki_Conversion/index.html?updated), including full
112 edit history, but as of 2009/10/16 that site is not available.
113