Tags: , ,

Now, once you've exported pages from Confluence and converted to MediaWiki format, you probably want to upload those pages automatically. pywikipediabot popped up in my search for Python programs to interact with MediaWiki.

pywikipediabot can do rather a lot of things, and my needs were pretty tiny in comparison. It was relatively easy to figure out how to use it from the source, but it seems there is usage documentation, and even a page on how to use it with your own MediaWiki install.

Since it does so much for you, my code was pretty trivial, based on one of the existing scripts:

#!/usr/bin/env python

import wikipedia, config
import re, sys, codecs
import os.path

msg = {
    'en': u'Automated import of articles',
}

def uploadpage(fn):
    f = codecs.open(fn, 'r', encoding = 'utf-8')
    contents = f.read()
    title = os.path.basename(fn)

    print title

    page = wikipedia.Page(mysite, title)
    wikipedia.output(page.title())
    page.put(contents, comment = commenttext, minorEdit = False)

filenames = []
def main():
    for fn in filenames:
        uploadpage(fn)

for filename in sys.argv[1:]:
    filenames.append(filename)

mysite = wikipedia.getSite()
commenttext = wikipedia.translate(mysite,msg)

try:
    main()
except:
    wikipedia.stopme()
    raise
else:
    wikipedia.stopme()

1 old-style comments

  1. JerryAugust 02, 2006 at 05:56 AM.

    Thanks for the pointer to pywikipediabot. I had been looking for code to work with mediawiki programatically, but did not find any.
blog comments powered by Disqus