table name for querying the mediawiki mysql db
[ikiwiki] / 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 Another set of instructions and conversion tools (which imports the full history)
12 can be found at <http://github.com/mithro/media2iki>
13
14 ## Step 1: Getting a list of pages
15
16 The first bit of information you require is a list of pages in the Mediawiki.
17 There are several different ways of obtaining these.
18
19 ### Parsing the output of `Special:Allpages`
20
21 Mediawikis have a special page called `Special:Allpages` which list all the
22 pages for a given namespace on the wiki.
23
24 If you fetch the output of this page to a local file with something like
25
26     wget -q -O tmpfile 'http://your-mediawiki/wiki/Special:Allpages'
27
28 You can extract the list of page names using the following python script. Note
29 that this script is sensitive to the specific markup used on the page, so if
30 you have tweaked your mediawiki theme a lot from the original, you will need
31 to adjust this script too:
32
33     import sys
34     from xml.dom.minidom import parse, parseString
35     
36     dom = parse(sys.argv[1])
37     tables = dom.getElementsByTagName("table")
38     pagetable = tables[-1]
39     anchors = pagetable.getElementsByTagName("a")
40     for a in anchors:
41         print a.firstChild.toxml().\
42             replace('&amp;','&').\
43             replace('&lt;','<').\
44             replace('&gt;','>')
45
46 Also, if you have pages with titles that need to be encoded to be represented
47 in HTML, you may need to add further processing to the last line.
48
49 Note that by default, `Special:Allpages` will only list pages in the main
50 namespace. You need to add a `&namespace=XX` argument to get pages in a
51 different namespace. The following numbers correspond to common namespaces:
52
53  * 10 - templates (`Template:foo`)
54  * 14 - categories (`Category:bar`)
55
56 Note that the page names obtained this way will not include any namespace
57 specific prefix: e.g. `Category:` will be stripped off.
58
59 ### Querying the database
60
61 If you have access to the relational database in which your mediawiki data is
62 stored, it is possible to derive a list of page names from this. With mediawiki's
63 MySQL backend, the page table is, appropriately enough, called `table`:
64
65  SELECT page_namespace, page_title FROM page;
66
67 As with the previous method, you will need to do some filtering based on the
68 namespace.
69
70 ## Step 2: fetching the page data
71
72 Once you have a list of page names, you can fetch the data for each page.
73
74 ### Method 1: via HTTP and `action=raw`
75
76 You need to create two derived strings from the page titles: the
77 destination path for the page and the source URL. Assuming `$pagename` 
78 contains a pagename obtained above, and `$wiki` contains the URL to your
79 mediawiki's `index.php` file:
80
81     src=`echo "$pagename" | tr ' ' _ | sed 's,&,&amp;,g'`
82     dest=`"$pagename" | tr ' ' _ | sed 's,&,__38__,g'`
83     
84     mkdir -p `dirname "$dest"`
85     wget -q "$wiki?title=$src&action=raw" -O "$dest"
86
87 You may need to add more conversions here depending on the precise page titles
88 used in your wiki.
89
90 If you are trying to fetch pages from a different namespace to the default,
91 you will need to prefix the page title with the relevant prefix, e.g.
92 `Category:` for category pages. You probably don't want to prefix it to the
93 output page, but you may want to vary the destination path (i.e. insert an
94 extra directory component corresponding to your ikiwiki's `tagbase`).
95
96 ### Method 2: via HTTP and `Special:Export`
97
98 Mediawiki also has a special page `Special:Export` which can be used to obtain
99 the source of the page and other metadata such as the last contributor, or the
100 full history, etc.
101
102 You need to send a `POST` request to the `Special:Export` page. See the source
103 of the page fetched via `GET` to determine the correct arguments.
104
105 You will then need to write an XML parser to extract the data you need from
106 the result.
107
108 ### Method 3: via the database
109
110 It is possible to extract the page data from the database with some
111 well-crafted queries.
112
113 ## Step 3: format conversion
114
115 The next step is to convert Mediawiki conventions into Ikiwiki ones.
116
117 ### categories
118
119 Mediawiki uses a special page name prefix to define "Categories", which
120 otherwise behave like ikiwiki tags. You can convert every Mediawiki category
121 into an ikiwiki tag name using a script such as
122
123     import sys, re
124     pattern =  r'\[\[Category:([^\]]+)\]\]'
125     
126     def manglecat(mo):
127             return '\[[!tag %s]]' % mo.group(1).strip().replace(' ','_')
128             
129     for line in sys.stdin.readlines():
130             res = re.match(pattern, line)
131             if res:
132                     sys.stdout.write(re.sub(pattern, manglecat, line))
133             else: sys.stdout.write(line)
134
135 ## Step 4: Mediawiki plugin
136
137 The [[plugins/contrib/mediawiki]] plugin can be used by ikiwiki to interpret
138 most of the Mediawiki syntax.
139
140 ## External links
141
142 [[sabr]] used to explain how to [import MediaWiki content into
143 git](http://u32.net/Mediawiki_Conversion/index.html?updated), including full
144 edit history, but as of 2009/10/16 that site is not available. A copy of the 
145 information found on this website is stored at <http://github.com/mithro/media2iki>
146
147