Commit 920d2438 authored by Evan Goldenberg's avatar Evan Goldenberg

add mindtouch to mediawiki converter

Signed-off-by: default avatarEvan Goldenberg <evang@catalyst.net.nz>
parent 8fd7459b
This diff is collapsed.
mt2mw -- package for migrating a Mindtouch wiki to MediaWiki
------------------------------------------------------------
Requirements:
* Python 2.6 or newer (not tested on older versions)
* Perl5
* Alex Z's wikitools package - available on pypi
<http://python-wikitools.googlecode.com>
* HTML::WikiConverter perl module
* Config::Simple perl module
* A target wiki running MediaWiki 1.13 or newer that has the full API
enabled (including write functions)
Usage:
* Edit config.ini
* python mt2mw.py
[config]
# trailing slashes!
mindtouch_url=http://my.mindtouch.wiki.com/
mediawiki_url=http://my.mediawiki.wiki.com/
mediawiki_user=wikiuser
mediawiki_password=password
#! /usr/bin/env perl
# mt2mw -- package for migrating a Mindtouch wiki to MediaWiki
# Copyright (C) 2010 Catalyst IT Ltd
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 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/>.
@input = <STDIN>;
use HTML::WikiConverter;
use Config::Simple;
$cfg = new Config::Simple('config.ini');
$url = $cfg->param('config.mindtouch_url');
$html = join(' ', @input);
$wc = new HTML::WikiConverter(
dialect => 'MediaWiki',
base_uri => $url,
wiki_uri => $url,
);
print $wc->html2wiki(html => $html);
# mt2mw -- package for migrating a Mindtouch wiki to MediaWiki
# Copyright (C) 2010 Catalyst IT Ltd
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 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/>.
# system library
import wikitools as wt
# our library
from page import WikiPage, html2wiki
class MWWiki:
def __init__(self, baseurl, username, password):
try:
self.site = wt.wiki.Wiki('%s/api.php' % baseurl)
self.site.login(username, password=password, remember=True)
except Exception, e:
print e
def html_write(self, page):
p = wt.page.Page(self.site, title=page.path)
try:
p.edit(text=html2wiki(page.get_content()))
except Exception, e:
print e
def write(self, page):
p = wt.page.Page(self.site, title=page.path)
try:
p.edit(text=page.get_content())
except Exception, e:
print e
def update_mainpage(self, root):
mainpage = WikiPage('MediaWiki:Mainpage')
mainpage.content = root.title.replace(' ', '_')
self.write(mainpage)
def create_from_mindtouch(self, root):
self.html_write(root)
for subpage in root.subpages:
self.create_from_mindtouch(subpage)
def done(self):
self.site.logout()
# mt2mw -- package for migrating a Mindtouch wiki to MediaWiki
# Copyright (C) 2010 Catalyst IT Ltd
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 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/>.
# system library
import urllib2
import xml.etree.ElementTree as etree
from xml.sax.saxutils import unescape
# our library
from page import HTMLPage
class MTWiki:
def __init__(self, baseurl):
self.baseurl = baseurl
def request(self, api_func):
url = '%s/@api/deki/%s' % (self.baseurl, api_func)
response = urllib2.urlopen(url)
if response.msg != 'OK':
raise Exception('ERROR: Mindtouch api request failed')
return response.read()
@staticmethod
def generate_sitemap(root, wiki):
id = root.get('id')
title = root.find('title').text
path = root.find('path').text
page = HTMLPage(id, title, wiki, path)
for subpage in root.find('subpages').findall('page'):
page.add_subpage(MTWiki.generate_sitemap(subpage, wiki))
return page
def get_sitemap(self):
root = etree.fromstring(self.request('pages')).find('page')
self.homepage = self.generate_sitemap(root, self)
self.homepage.path = self.homepage.title
return self.homepage
def get_page_content(self, page):
response = etree.fromstring(
self.request('pages/%s/contents' % page.id)
)
return unescape(response.find('body').text.strip())
# mt2mw -- package for migrating a Mindtouch wiki to MediaWiki
# Copyright (C) 2010 Catalyst IT Ltd
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 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/>.
# system library
import ConfigParser as cp
# our library
from mindtouch import MTWiki
from mediawiki import MWWiki
cfg = cp.SafeConfigParser()
cfg.read('config.ini')
# get the root element in the page structure
mtwiki = MTWiki(cfg.get('config', 'mindtouch_url'))
homepage = mtwiki.get_sitemap()
mwwiki = MWWiki(
cfg.get('config', 'mediawiki_url'),
cfg.get('config', 'mediawiki_user'),
cfg.get('config', 'mediawiki_password')
)
mwwiki.create_from_mindtouch(homepage)
# point MediaWiki:MainPage at the new homepage
mwwiki.update_mainpage(homepage)
mwwiki.done()
# mt2mw -- package for migrating a Mindtouch wiki to MediaWiki
# Copyright (C) 2010 Catalyst IT Ltd
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 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/>.
# system library
import os
import subprocess as sp
class HTMLPage:
def __init__(self, id, title, wiki, path):
self.id = id
self.title = title
self.wiki = wiki
self.path = path
self.subpages = list()
def add_subpage(self, page):
self.subpages.append(page)
def get_content(self):
return self.wiki.get_page_content(self)
class WikiPage:
def __init__(self, title):
self.title = title
self.path = title
def get_content(self):
return self.content
def html2wiki(html):
cmd = os.path.join(os.getcwd(), 'convert.pl')
converter = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
out, err = converter.communicate(input=html)
if err:
raise Exception(err)
return out
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment