Table of Contents

MediaWiki to DokuWiki Converter

Automatic script

This script will automatically convert a MediaWiki install to DokuWiki. No configuration is required as all it needs is the path to LocalSettings.php. The shell script, as was presented in sections below, did not work as expected as DokuWiki did not find the pages after they were injected manually. Instead, this script uses DokuWiki's own API to programmatically insert pages from MediaWiki.

The mw2dw-conv_sed.sh script, which you can find below, has been converted into native PHP so shell access is not required. Also runs from the web server if desired.

Find on GitHub:
https://github.com/tetsuo13/MediaWiki-to-DokuWiki-Importer

yamdwe tool

“yamdwe” (Yet Another Mediawiki to DokuWiki Exporter) is another export tool.

https://github.com/projectgus/yamdwe/

Pros:

Cons:

Older scripts:

Updated Scripts for Converting Mediawiki 1.15.1 to Anteater.

Thanks to all of the great scripts on here I was able to do a bit more work towards making something a bit more automated. It's not perfect, but it converted over my 200 page site + files to Dokuwiki pretty well. I figured at this point any other little bugs I find I can change by hand. I didn't have time to perfect it - but hopefully someone else does.

Here's the download: http://www.passportparking.info/Download/mediawiki2dokuwiki2.tar.gz

As I said, it's got some bugs still. You need to run this on your actual mediawiki/dokuwiki server as it copies files between the two file locations.

4/16/2011 - Charlie Youakim (charlie.youakim@passportparking.com)

Bugfix! UPPER to lower convert charset, change getContent.sh:

#	lowertitle="$(echo $title | tr "[:upper:]" "[:lower:]").txt" 
	lowertitle="$(echo $title | awk '{print tolower(substr($0,0))}').txt" 

Web Based Version

Hey, I was playing with AWK and Perl a little bit. I created a MediaWiki to DokuWiki Converter. A online converter is now hosted at http://johbuc6.coconia.net/mediawiki2dokuwiki.php.FIXME

Requirements

Capabilities

It is able to transform

Limitations

Missing features (yet)

It is not able to transform

Bugs

Source

File mediawiki2dokuwiki.sh:

mediawiki2dokuwiki.sh
#! /bin/sh
# Mediawiki2Dokuwiki Converter
# originally by Johannes Buchner <buchner.johannes [at] gmx.at>
# License: GPL (http://www.gnu.org/licenses/gpl.txt)
 
# Headings
cat mediawiki | \
   perl -pe 's/^[ ]*=([^=])/<h1> ${1}/g' | \
   perl -pe 's/([^=])=[ ]*$/${1} <\/h1>/g' | \
   perl -pe 's/^[ ]*==([^=])/<h2> ${1}/g' | \
   perl -pe 's/([^=])==[ ]*$/${1} <\/h2>/g' | \
   perl -pe 's/^[ ]*===([^=])/<h3> ${1}/g' | \
   perl -pe 's/([^=])===[ ]*$/${1} <\/h3>/g' | \
   perl -pe 's/^[ ]*====([^=])/<h4> ${1}/g' | \
   perl -pe 's/([^=])====[ ]*$/${1} <\/h4>/g' | \
   perl -pe 's/^[ ]*=====([^=])/<h5> ${1}/g' | \
   perl -pe 's/([^=])=====[ ]*$/${1} <\/h5>/g' | \
   perl -pe 's/^[ ]*======([^=])/<h6> ${1}/g' | \
   perl -pe 's/([^=])======[ ]*$/${1} <\/h6>/g' \
    > mediawiki1
 
cat mediawiki1 | \
   perl -pe 's/<\/?h1>/======/g' | \
   perl -pe 's/<\/?h2>/=====/g' | \
   perl -pe 's/<\/?h3>/====/g' | \
   perl -pe 's/<\/?h4>/===/g' | \
   perl -pe 's/<\/?h5>/==/g' | \
   perl -pe 's/<\/?h6>/=/g' | \
   cat > mediawiki2
 
# lists
cat mediawiki2 |
  perl -pe 's/^[\*#]{4}\*/          * /g'  | \
  perl -pe 's/^[\*#]{3}\*/        * /g'    | \
  perl -pe 's/^[\*#]{2}\*/      * /g'      | \
  perl -pe 's/^[\*#]{1}\*/    * /g'        | \
  perl -pe 's/^\*/  * /g'                  | \
  perl -pe 's/^[\*#]{4}#/          \- /g'  | \
  perl -pe 's/^[\*\#]{3}\#/      \- /g'    | \
  perl -pe 's/^[\*\#]{2}\#/    \- /g'      | \
  perl -pe 's/^[\*\#]{1}\#/  \- /g'        | \
  perl -pe 's/^\#/  - /g'                  | \
  cat > mediawiki3
 
#[link] => [[link]]
cat mediawiki3 |
  perl -pe 's/([^\[])\[([^\[])/${1}[[${2}/g' |
  perl -pe 's/^\[([^\[])/[[${1}/g' |
  perl -pe 's/([^\]])\]([^\]])/${1}]]${2}/g' |
  perl -pe 's/([^\]])\]$/${1}]]/g' \
  > mediawiki4
 
#[[url text]] => [[url|text]]
cat mediawiki4 |
  perl -pe 's/(\[\[[^| \]]*) ([^|\]]*\]\])/${1}|${2}/g' \
  > mediawiki5
 
# bold, italic
cat mediawiki5 |
  perl -pe "s/'''/**/g" |
  perl -pe "s/''/\/\//g" \
  > mediawiki6
 
# talks
cat mediawiki6 |
  perl -pe "s/^[ ]*:/>/g" |
  perl -pe "s/>:/>>/g" |
  perl -pe "s/>>:/>>>/g" |
  perl -pe "s/>>>:/>>>>/g" |
  perl -pe "s/>>>>:/>>>>>/g" |
  perl -pe "s/>>>>>:/>>>>>>/g" |
  perl -pe "s/>>>>>>:/>>>>>>>/g" \
  > mediawiki7
 
cat mediawiki7 |
  perl -pe "s/<pre>/<code>/g" |
  perl -pe "s/<\/pre>/<\/code>/g" \
  > mediawiki8
 
cat mediawiki8 > dokuwiki

Howto use (for shell newbies)

  1. Make sure you are under Linux/Unix ;-)
  2. Save the code above in a file named mediawiki2dokuwiki.sh.
  3. Save the MediaWiki page you want to transform to a file called mediawiki in the same directory.
  4. In the shell go to the directory (using cd) and execute:
    chmod +x mediawiki2dokuwiki.sh #we want to be able to execute it
    ./mediawiki2dokuwiki.sh
  5. Now you got some files called mediawiki+a number. These are debugging steps (ignore).
  6. In the file 'dokuwiki' you'll find your DokuWiki-Syntax.

Remember, all the fame goes to me, cause I started this ;-)! — Johannes Buchner 2006-01-26 19:27

Changelog

ToDo

Would be great if someone wants to improve this! What is needed:

Feedback & discussion

Code markup

Graham Macleod 30/1/08 1:50pm GMT - Hi there. Code mark up which displays such as

<?php echo 'THIS IS CODE'; ?>

in DokuWiki uses double spaces but your converter seems to keep the single space that MediaWiki uses. I'd also just like to take the time to give you massive props on this. It's been a life saver.

sed Version

There is also one issue, when bold and italic texts are combined. I tested with the German UNIX Wikipedia article and there were 2 tags that made whole parts of the generated DokuWiki in bold. The following code fixes this behaviour:

$ diff mediawiki2dokuwiki.sh mediawiki2dokuwiki.sh.080925-1
7d6
< # changes by Reiner Rottmann: - fixed erroneous interpretation of combined bold and italic text.
165,169c164
<
< cat mediawiki9 \
<    | sed -r "s/\*\*\/\//\/\/\*\*/g"> mediawiki10
<
< cat mediawiki10 > dokuwiki
---
> cat mediawiki9 > dokuwiki

Automatic script

This script get the contents of your mediawiki (by database connection), and convert it to dokuwiki syntax. All, out of the box, you only need to configure user/password of database.

Example:

cd mediawiki2dokuwiki
./getContent.sh
mv old /var/www/dokuwiki/data/pages
chmod a+r /var/www/dokuwiki/data/pages/old -R

Here you can find the package: http://dabax.net/files/mediawiki2dokuwiki.tar.gz (this link is broken as of 9/3/2012)

This is the main script.

#!/bin/bash

#About your mediawiki
WIKIDB="DATABSE_NAME"
WIKIPASS="DATABASE_PASSWORD"

#The destination folder
DEST="old"

#Dont touch this
TITLES="titles"
PHARSER="./m2d.sh"

mysql --password=$WIKIPASS $WIKIDB -e 'select cur_title from cur;' | \
while read title; do

	newtitle="$(echo $title | tr "[:upper:]" "[:lower:]").txt" 
	echo "$newtitle"
	mysql --password=$WIKIPASS $WIKIDB -e "select cur_text from cur where cur_title='$title';" \
	| sed s/'\\n'/\\n/g | grep -v cur_text |  $PHARSER $DEST/$newtitle 

done

for f in $DEST/*; do 
	[ $(cat $f | wc -w) -lt 25 ] && \
	{ echo "Deleting $f, too short"; rm -f $f;}
done



echo ""
echo "Done. Put the contents of $DEST to Path_Of_dokuwiki/data/pages/"

m2d.sh is the “sed version” published in this page, with some modifications. Enjoy it!

ERROR 1146 (42S02) at line 1: Table 'dbname.cur' doesn't exist

Hi,

Using the above attached code, I'm having some problems:

ERROR 1146 (42S02) at line 1: Table 'pswiki.cur' doesn't exist
cat: old/*: No such file or directory
Deleting old/*, too short

Done. Put the contents of old to Path_Of_dokuwiki/data/pages/

(where pswiki is the database containing my MW data, which came from a Windows-based MySQL server via mysqldump.) The directory ./old is empty.

If I turn on MySQL statement logging:

SET GLOBAL general_log_file='/var/log/mysql/sql.log';
SET GLOBAL general_log='ON';

then I get the following only in that log file when I run ./getContent.sh:

110216  2:11:17    75 Connect   root@localhost on pswiki
                   75 Query     select @@version_comment limit 1
                   75 Query     select cur_title from cur
                   75 Quit

It looks to me as if what's intended to be a cursor is being interpreted as a literal table name, but I'm getting out of my depth there. I have tried back-ticking `cur` in getContent.sh to no avail.

Can anyone shed light?

Thanks!

tomgreen 2011/02/16 03:06

EDIT: I've used the web-based service linked above, and apart from some table funnies it worked very well - a lifesaver. Thanks!

tomgreen 2011/02/18 03:26

I have solved the “cur problem”. It is assumed, for my opinion, as a VIEW. Create this view, named “cur”, with the following SQL request :

CREATE VIEW cur AS SELECT mw_page.page_title AS cur_title, mw_text.old_text AS cur_text
FROM mw_page,mw_text WHERE mw_page.page_id=mw_text.old_id;

and re-run the shell.

Hope this helps!

gtournat 2011/11/06 13:16

Mediawiki 2 Dokuwiki Converter

#! /bin/sh
# Mediawiki2Dokuwiki Converter
# originally by Johannes Buchner <buchner.johannes [at] gmx.at>
# License: GPL (http://www.gnu.org/licenses/gpl.txt)
 
# Headings
cat mediawiki | \
   perl -pe 's/^[ ]*=([^=])/<h1> ${1}/g' | \
   perl -pe 's/([^=])=[ ]*$/${1} <\/h1>/g' | \
   perl -pe 's/^[ ]*==([^=])/<h2> ${1}/g' | \
   perl -pe 's/([^=])==[ ]*$/${1} <\/h2>/g' | \
   perl -pe 's/^[ ]*===([^=])/<h3> ${1}/g' | \
   perl -pe 's/([^=])===[ ]*$/${1} <\/h3>/g' | \
   perl -pe 's/^[ ]*====([^=])/<h4> ${1}/g' | \
   perl -pe 's/([^=])====[ ]*$/${1} <\/h4>/g' | \
   perl -pe 's/^[ ]*=====([^=])/<h5> ${1}/g' | \
   perl -pe 's/([^=])=====[ ]*$/${1} <\/h5>/g' | \
   perl -pe 's/^[ ]*======([^=])/<h6> ${1}/g' | \
   perl -pe 's/([^=])======[ ]*$/${1} <\/h6>/g' \
    > mediawiki1
 
cat mediawiki1 | \
   perl -pe 's/<\/?h1>/======/g' | \
   perl -pe 's/<\/?h2>/=====/g' | \
   perl -pe 's/<\/?h3>/====/g' | \
   perl -pe 's/<\/?h4>/===/g' | \
   perl -pe 's/<\/?h5>/==/g' | \
   perl -pe 's/<\/?h6>/=/g' | \
   cat > mediawiki2
 
# lists
cat mediawiki2 |
  perl -pe 's/^[\*#]{4}\*/          * /g'  | \
  perl -pe 's/^[\*#]{3}\*/        * /g'    | \
  perl -pe 's/^[\*#]{2}\*/      * /g'      | \
  perl -pe 's/^[\*#]{1}\*/    * /g'        | \
  perl -pe 's/^\*/  * /g'                  | \
  perl -pe 's/^[\*#]{4}#/          \- /g'  | \
  perl -pe 's/^[\*\#]{3}\#/      \- /g'    | \
  perl -pe 's/^[\*\#]{2}\#/    \- /g'      | \
  perl -pe 's/^[\*\#]{1}\#/  \- /g'        | \
  perl -pe 's/^\#/  - /g'                  | \
  cat > mediawiki3
 
#[link] => [[link]]
cat mediawiki3 |
  perl -pe 's/([^\[])\[([^\[])/${1}[[${2}/g' |
  perl -pe 's/^\[([^\[])/[[${1}/g' |
  perl -pe 's/([^\]])\]([^\]])/${1}]]${2}/g' |
  perl -pe 's/([^\]])\]$/${1}]]/g' \
  > mediawiki4
 
#[[url text]] => [[url|text]]
cat mediawiki4 |
  perl -pe 's/(\[\[[^| \]]*) ([^|\]]*\]\])/${1}|${2}/g' \
  > mediawiki5
 
# bold, italic
cat mediawiki5 |
  perl -pe "s/'''/**/g" |
  perl -pe "s/''/\/\//g" \
  > mediawiki6
 
# talks
cat mediawiki6 |
  perl -pe "s/^[ ]*:/>/g" |
  perl -pe "s/>:/>>/g" |
  perl -pe "s/>>:/>>>/g" |
  perl -pe "s/>>>:/>>>>/g" |
  perl -pe "s/>>>>:/>>>>>/g" |
  perl -pe "s/>>>>>:/>>>>>>/g" |
  perl -pe "s/>>>>>>:/>>>>>>>/g" \
  > mediawiki7
 
cat mediawiki7 |
  perl -pe "s/<pre>/<code>/g" |
  perl -pe "s/<\/pre>/<\/code>/g" \
  > mediawiki8
 
cat mediawiki8 > dokuwiki