DokuWiki

It's better when it's simple

User Tools

Site Tools


tips:mediawiki_to_dokuwiki_converter

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tips:mediawiki_to_dokuwiki_converter [2012-07-30 16:51] – add font converter 92.103.27.162tips:mediawiki_to_dokuwiki_converter [2024-05-05 09:49] (current) – [Web Based Version] Wirbel78
Line 1: Line 1:
 ====== MediaWiki to DokuWiki Converter ====== ====== 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**: 
 +  * It uses the MediaWiki API so can create a local DokuWiki from a remote MediaWiki install. Automatically imports full revision history for each page, imports media and (optionally) imports users & passwords if it has database access. 
 +  * Uses [[http://mwlib.readthedocs.org/|mwlib]] to parse MediaWiki syntax so complex pages import more cleanly.
 +
 +**Cons**: 
 +  * Is Python based so it's more involved to set up than most of the other tools listed here (install steps for Linux are provided.)
 +  * Slow for large amounts of Mediawiki content
 +  * Categories are not converted (the content within the category pages)
 +===== Older scripts: =====
 ===== Updated Scripts for Converting Mediawiki 1.15.1 to Anteater. ===== ===== Updated Scripts for Converting Mediawiki 1.15.1 to Anteater. =====
  
Line 19: Line 42:
  
 Hey, I was playing with AWK and Perl a little bit. I created a MediaWiki to DokuWiki Converter. 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]].+A online converter is now hosted at [[http://johbuc6.coconia.net/mediawiki2dokuwiki.php]].FIXME
 ==== Requirements ==== ==== Requirements ====
   * bash   * bash
Line 44: Line 67:
 ==== Source ===== ==== Source =====
 File mediawiki2dokuwiki.sh: File mediawiki2dokuwiki.sh:
-<code bash>+<code bash mediawiki2dokuwiki.sh>
 #! /bin/sh #! /bin/sh
 # Mediawiki2Dokuwiki Converter # Mediawiki2Dokuwiki Converter
Line 375: Line 398:
  
  
-Here you can find the package: http://dabax.net/files/mediawiki2dokuwiki.tar.gz+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. This is the main script.
Line 469: Line 492:
  --- [[user>gtournat|gtournat]] //2011/11/06 13:16//  --- [[user>gtournat|gtournat]] //2011/11/06 13:16//
  
-===== Another 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 above shell script 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.+====== Mediawiki 2 Dokuwiki Converter ======
  
-The mw2dw-conv_sed.sh script has been converted into native PHP so shell access is not requiredAlso runs from the web server if desired.+<code perl> 
 +#! /bin/sh 
 +# Mediawiki2Dokuwiki Converter 
 +# originally by Johannes Buchner <buchner.johannes [at] gmx.at> 
 +# License: GPL (http://www.gnu.org/licenses/gpl.txt)
  
-Find on GitHub: https://github.com/tetsuo13/MediaWiki-to-DokuWiki-Importer+# 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
  
-===== A PHP page which combines all of the above =====+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
  
-Having a webspace with no shell access I combined all of the above to have a single page which imports a mediawiki page via a DB connection.+# 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
  
-<file php mw2dw.php>+# bold, italic 
 +cat mediawiki5 | 
 +  perl -pe "s/'''/**/g"
 +  perl -pe "s/''/\/\//g"
 +  mediawiki6
  
-<?php +# talks 
-/** +cat mediawiki6 | 
-* MediaWiki2DokuWiki. Imports a MediaWiki install into DokuWiki. +  perl -pe "s/^[ ]*:/>/g" | 
-+  perl -pe "s/>:/>>/g" | 
-* Copyright (C) 2011 Andrei Nicholson +  perl -pe "s/>>:/>>>/g" | 
-+  perl -pe "s/>>>:/>>>>/g" | 
-* This program is free softwareyou can redistribute it and/or modify +  perl -pe "s/>>>>:/>>>>>/g" | 
-* it under the terms of the GNU General Public License as published by +  perl -pe "s/>>>>>:/>>>>>>/g" | 
-* the Free Software Foundation, either version 3 of the License, or +  perl -pe "s/>>>>>>:/>>>>>>>/g"
-* (at your option) any later version. +  > mediawiki7
-+
-* This program is distributed in the hope that it will be useful, +
-* but WITHOUT ANY WARRANTY; without even the implied warranty of +
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
-* GNU General Public License for more details. +
-+
-* You should have received a copy of the GNU General Public License +
-* along with this program. If not, see <http://www.gnu.org/licenses/>+
-+
-* @author Andrei Nicholson +
-* @since 2011-11-18 +
-*/+
  
-ini_set('display_errors', '1'); +cat mediawiki7 
-error_reporting(E_ALL E_STRICT); +  perl -pe "s/<pre>/<code>/g| 
- +  perl -pe "s/<\/pre>/<\/code>/g" \ 
-// Path to root DokuWiki install. Required by include files. +  mediawiki8
-define('DOKU_INC', dirname(__FILE__) . '/'); +
- +
-require_once DOKU_INC . 'inc/init.php'; +
-require_once DOKU_INC . 'inc/common.php'; +
- +
-if (urldecode($_REQUEST['action']) == 'import') { +
-    print "<pre>\n"; +
-    $mwikiSettingsPath = realpath($_SERVER['argv'][1]); +
-    $mwikiSettingsPath = urldecode($_REQUEST['localsetting']); +
- +
-    if (!file_exists($mwikiSettingsPath)) { +
-        exit("Path to LocalSettings.php, $mwikiSettingsPath, is invalid"); +
-    } +
- +
-    $mwikiSettings = file($mwikiSettingsPath, +
-                          FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); +
- +
-    $mwikiDb = dbConnectionSettings($mwikiSettings); +
-    print_r($mwikiDb); +
- +
-    if ( (int)count($mwikiDb) <= 8 ) { +
-        exit('Something went wrong with scraping LocalSettings.php for DB info'); +
-    } +
- +
-    $db = dbConnect($mwikiDb); +
-    convert($db, $mwikiDb); +
-    print "<pre>\n"; +
-} else { +
-    print "<h1>path to LocalSettings.php</h1>\n"; +
-    print "<form>\n"; +
-    print "<input name='localsetting' value='../wiki/LocalSettings.php'><br>\n"; +
-    print "<input type='submit' name='action' value='import'>\n"; +
-    print "</form>\n"; +
-+
- +
-/******************************************************************************* +
-* Convert pages from MediaWiki. +
-+
-* @param PDO $db DB handle. +
-* @param array $mwikiDb DB attributes. +
-*/ +
-function convert(PDO $db, array $mwikiDb) { +
-    $prefix = $mwikiDb['wgDBprefix']; +
- +
-    $sql = "SELECT p.page_title, p.page_namespace, t.old_text +
-FROM {$prefix}page p +
-INNER JOIN {$prefix}revision r ON +
-p.page_latest = r.rev_id +
-INNER JOIN {$prefix}text t ON +
-r.rev_text_id = t.old_id +
-ORDER BY p.page_title"; +
- +
-    try { +
-        $statement = $db->prepare($sql); +
- +
-        if (!$statement->execute()) { +
-            $error = $statement->errorInfo(); +
-            exit('Could not fetch MediaWiki: ' . $error[2]); +
-        } +
- +
-        while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { +
-            echo 'Processing ' . $row['page_title'] . '... '; +
-            //if ($i++ > 10) {break;} +
-            switch ($row['page_namespace']) { +
-                case 0: +
-                    processPage($row); +
-                    break; +
- +
-                case 6: +
-                    processImage($row); +
-                    break; +
- +
-                default: +
-                    echo 'Unknown type. Skipping.'; +
-            } +
-            echo PHP_EOL; +
-        } +
-    } catch (PDOException $e) { +
-        exit('Could not select all pages: ' . $e->getMessage()); +
-    } +
-+
- +
-/** +
-* Inject new page into DokuWiki. +
-+
-* @param array $record Info on page. +
-*/ +
-function processPage(array $record) { +
- +
-    $page = $record['old_text']; +
-    //print $page; +
-    $file = explode(PHP_EOL, $page); +
- +
-    foreach ($file as $i=>$line) { +
-        //print "$i:$line\n"; +
- +
-        //Headings +
-        $line = preg_replace("/^[ ]*=([^=])/", "<h1$1", $line); +
-        $line = preg_replace("/([^=])=[ ]*$/", "$1 </h1>", $line); +
-        $line = preg_replace("/^[ ]*==([^=])/", "<h2> $1", $line); +
-        $line = preg_replace("/([^=])==[ ]*$/", "$1 </h2>", $line); +
-        $line = preg_replace("/^[ ]*===([^=])/", "<h3> $1", $line); +
-        $line = preg_replace("/([^=])===[ ]*$/", "$1 </h3>", $line); +
-        $line = preg_replace("/^[ ]*====([^=])/", "<h4> $1", $line); +
-        $line = preg_replace("/([^=])====[ ]*$/", "$1 </h4>", $line); +
-        $line = preg_replace("/^[ ]*=====([^=])/", "<h5> $1", $line); +
-        $line = preg_replace("/([^=])=====[ ]*$/", "$1 </h5>", $line); +
-        $line = preg_replace("/^[ ]*======([^=])/", "<h6> $1", $line); +
-        $line = preg_replace("/([^=])======[ ]*$/", "$1 </h6>", $line); +
- +
-        $line = preg_replace("/<\/?h1>/", "======", $line); +
-        $line = preg_replace("/<\/?h2>/", "=====", $line); +
-        $line = preg_replace("/<\/?h3>/", "====", $line); +
-        $line = preg_replace("/<\/?h4>/", "===", $line); +
-        $line = preg_replace("/<\/?h5>/", "==", $line); +
-        $line = preg_replace("/<\/?h6>/", "=", $line); +
- +
-        //lists +
-        $line = preg_replace("/^[\*#]{4}\*/",         * ", $line); +
-        $line = preg_replace("/^[\*#]{3}\*/",       * ", $line); +
-        $line = preg_replace("/^[\*#]{2}\*/",     * ", $line); +
-        $line = preg_replace("/^[\*#]{1}\*/",   * ", $line); +
-        $line = preg_replace("/^\* /", "  * ", $line); // test +
- +
-        $line = preg_replace("/^[\*#]{4}#/",         \- ", $line); +
-        $line = preg_replace("/^[\*\#]{3}\#/", "      \- ", $line); +
-        $line = preg_replace("/^[\*\#]{2}\#/",   \- ", $line); +
-        $line = preg_replace("/^[\*\#]{1}\#/", \- ", $line); +
-        $line = preg_replace("/^\#/", - ", $line); +
- +
-        //[link] => [[link]] +
-        //$line = preg_replace("/(\[)([^\[\]]*)(\])/", "--$2--", $line); +
- +
-        $line = preg_replace("/([^\[])\[([^\[])/", "$1[[$2", $line); +
-        $line = preg_replace("/^\[([^\[])/", "[[$1", $line); +
-        $line = preg_replace("/([^\]])\]([^\]])/", "$1]]$2", $line); +
-        $line = preg_replace("/([^\]])\]$/", "$1]]", $line); +
- +
-        // [[url text]] => [[url|text]] +
-        $line = preg_replace("/(\[\[[http|ftp|file][^| \]]*) ([^|\]]*\]\])/", "$1|$2", $line); +
- +
-        // bold, italic +
-        $line = preg_replace("/'''/","**", $line); +
-        $line = preg_replace("/''/","\/\/", $line); +
- +
-        //talks +
-        $line = preg_replace("/^[ ]*:/", ">", $line); +
-        $line = preg_replace("/>:/", ">>", $line); +
-        $line = preg_replace("/>>:/", ">>>", $line); +
-        $line = preg_replace("/>>>:/", ">>>>", $line); +
-        $line = preg_replace("/>>>>:/", ">>>>>", $line); +
-        $line = preg_replace("/>>>>>:/", ">>>>>>", $line); +
-        $line = preg_replace("/>>>>>>:/", ">>>>>>>", $line); +
- +
-        //code +
-        $line = preg_replace("/<pre>/", "<code>", $line); +
-        $line = preg_replace("/<\/pre>/", "<\/code>", $line); +
- +
-        $out[$i] = $line; +
-    } +
- +
-    $page = implode(PHP_EOL, $out); +
- +
-    saveWikiText($record['page_title'], +
-        con('', $page, ''), +
-        'created'); +
-+
- +
-/** +
-* Inject image. +
-+
-* @param array $record Info on page. +
-*/ +
-function processImage(array $record) { +
-    echo 'Skipping.'; +
-+
- +
-/** +
-* Connect to the DB and return handle. +
-+
-* @param array $mwikiDb DB attributes. +
-+
-* @return PDO DB handle. +
-*/ +
-function dbConnect(array $mwikiDb) { +
-    $dsn = $mwikiDb['wgDBtype'] . ':dbname=' . $mwikiDb['wgDBname'] . ';' +
-         . 'host=' . $mwikiDb['wgDBserver']; +
- +
-    try { +
-        $db = new PDO($dsn, $mwikiDb['wgDBuser'], $mwikiDb['wgDBpassword']); +
-    } catch (PDOException $e) { +
-        exit('DB connection failed: ' . $e->getMessage()); +
-    } +
-    return $db; +
-+
- +
-/** +
-* Strip DB connection settings from LocalSettings.php. +
-+
-* @param array $mwikiSettings Content of LocalSettings.php with each line as +
-* an element in the array. +
-+
-* @return array DB attributes. +
-*/ +
-function dbConnectionSettings(array $mwikiSettings) { +
-    foreach ($mwikiSettings as $line) { +
-        if (substr($line, 0, 5) != '$wgDB') { +
-            continue; +
-        } +
- +
-        $x = explode('=', $line, 2); +
- +
-        if (!is_array($x) || count($x) != 2) { +
-            continue; +
-        } +
- +
-        $val = trim($x[1]); +
- +
-        // Strip leading dollar sign from key. Strip leading quote, trailing +
-        // quote and semicolon from value. +
-        $db[substr(trim($x[0]), 1)] = substr($val, 1, -2); +
-    } +
- +
-    return $db; +
-+
-?>+
  
 +cat mediawiki8 > dokuwiki
 +</code>
  
-</file> 
tips/mediawiki_to_dokuwiki_converter.1343659881.txt.gz · Last modified: 2012-07-30 16:51 by 92.103.27.162

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki