#!/usr/bin/env python
"""
Export all revisions of all pages, plus all images/meda, from a
Mediawiki install to a Dokuwiki install. Mediawiki install can be
remote (uses API, but check terms of service.) Dokuwiki install is
local.

Requirements:
Python 2.7, mwlib, simplemediawiki, requests

Copyright (C) 2014 Angus Gratton
Licensed under New BSD License as described in the file LICENSE.
"""
from __future__ import print_function, unicode_literals, absolute_import, division
import argparse, sys
from pprint import pprint
import mediawiki, dokuwiki


def main():
    args = arguments.parse_args()
    importer = mediawiki.Importer(args.mediawiki)
    exporter = dokuwiki.Exporter(args.dokuwiki)

    # Convert all pages and page revisions
    pages = importer.get_all_pages()
    print("Found %d pages to export..." % len(pages))
    exporter.write_pages(pages)

    # Bring over images
    images = importer.get_all_images()
    print("Found %d images to export..." % len(images))
    exporter.write_images(images)

    # fix permissions on data directory if possible
    exporter.fixup_permissions()
    print("Done.")

# Parser for command line arguments
arguments = argparse.ArgumentParser(description='Convert a Mediawiki installation to a Dokuwiki installation.')
#arguments.add_argument('-y', '--yes',help="Don't pause for confirmation before exporting", action="store_true")
arguments.add_argument('mediawiki', metavar='MEDIAWIKI_API_URL', help="URL of mediawiki's api.php file (something like http://mysite/wiki/api.php)")
arguments.add_argument('dokuwiki', metavar='DOKUWIKI_ROOT', help="Root path to an existing dokuwiki installation to add the Mediawiki pages to (can be a brand new install.)")

if __name__ == "__main__":
    main()

@@ -544,17 +544,16 @@
 kuwiki%0A%0A
-%0A
 def main
@@ -555,16 +555,152 @@
  main():
+%0A    # the wikicontent code (that uses visitor module) tends to recurse quite deeply for complex pages%0A    sys.setrecursionlimit(20000)%0A
 %0A    arg

Increase maximum recursion depth (wikicontent parsing can lead to deep recursion in complex pages), closes #1.