Automatically updating/replacing/editing a mediawiki page from a python script with mwclient – an example

Agile, Media wiki, Project management

Many workplaces have mediawiki wikis – and these are often the information hub for a team or, indeed, the entire organisation.

Rather than generating  a new web page on a different web server to display a semi-regularly updated computer generated report, I thought (perhaps) it could be convenient to have a tool which permitted the easy replacement of a particular page.

I recently undertook to develop such a tool – and, in researching, discovered the excellent mwclient library   – although it _is_ somewhat lacking in examples/documentation.

As luck had it, the reporting tool I was using already generated output in mediawiki markup  on stdout – so all I needed to do was write a tool that could read a page contents from stdin and then upload it to a predefined page on wiki.

Here’s what I came up with:

#! /usr/bin/env python

import mwclient
import sys

def replace_page(wikiname,
                 wikibasepath,
                 pagename,
                 username,
                 password,
                 contents):
    site = mwclient.Site(wikiname,
                         wikibasepath,
                         force_login=True)
    site.login(username, password)
    page = site.Pages[pagename]
    some = page.edit()
    page.save(contents, summary='Automatic update')

def main():
    wikiname='wiki.mydomain.com'
    wikibasepath="/wiki/"  # This assumes that your wiki pages all reside under http://wiki.mydomain.com/wiki/
    pagename='User:PurpleScarab/reportpage'
    username="PurpleScarab"
    password="asecret"
    contents=sys.stdin.read()
    replace_page(wikiname,
                 wikibasepath,
                 pagename,
                 username,
                 password,
                 contents)
# Usage cat somefile.txt | reportpagepaste.py
if __name__ == "__main__":
    main()

Beware: it’s not best practice to hard-wire passwords in plain text in your own code!

Obviously, it would be childsplay to write a more general purpose tool that read the the various parameters in from a config file or as command line parameters (perhaps using getpass to securely capture the password) – but this was not needed in my particular environment and so is is left as an exercise for the reader.

Advertisements

8 thoughts on “Automatically updating/replacing/editing a mediawiki page from a python script with mwclient – an example

  1. Seems nice, but I don’t understand how to use it ? Put the script in a Python file tot.py and then how to execute it ? > toto.py mediwikicontent.txt ? By the way, how is the Main Page name pagename=’User:Main Page’ ?

  2. Yves: In the example “pagename” simply matched the wasy mediawiki had been deployed at my former employer. The setup of your mediawiki may be quite different.

    How to use the script:

    Assuming that:

    1) you’ve created a file “tot.py” containing the above code along with changes to wikiname, wikibasepath, pagename, username and password to match those required by your wiki. “pagename” is simply the mediawiki page you want your file to appear on – and, in our wiki, it just so happened that each user had their own page
    2) you have some text in a file named “somefile.txt”

    you simply need to pipe the file (or some other program that produces text output) into the script:

    On *nix systems enter the following at the command prompt:

    cat somefile.txt | tot.py

    On windows systems enter the following at the command prompt:

    type somefile.txt | tot.py

    Hope this helps!

  3. Hi…

    First Thanks for this code. Its very crisp and helpful.

    I tried the same code with a simple wiki page (https://en.wikipedia.org/wiki/User:).
    The error I am getting is
    “ValueError: No JSON object could be decoded”

    When I tried to print the object which is trying to be decoded, the content in a html format.
    I do not how to get along with this.
    Kindly help me out.

    Thanks,
    Sowmya.

  4. Sowmya: I’d be cautious about running this against a public resource like like wikipedia.org – really it’s best used for private wikis!

    It sounds like you may be trying to run it against a page you don’t have permission to edit. Try running it again against a page on your private wiki and see if there are any error messages embedded in the html returned which might shed some light on what went wrong – you can share the output on pastebin.com and I’ll take a look if you like?

  5. Thanks for the code. I got the following error:
    mwclient.errors.MediaWikiVersionError: Requires version 1.16, current version is 1.15

    Can you please help me how can I resolve this.

    Thanks in Advance.

    1. Hi Mohan,

      I’ve not encountered this issue before myself (and no longer use this code in my current role) so it’s hard to investigate.

      However, it looks like the 3rd party open source library used by the example code (mwclient) which originates from here: https://github.com/mwclient/mwclient/ is not compatible with your wiki version as the version you are using is too old.

      If you can upgrade your MediaWiki server to a newer version (1.16 or later – current version seems to be 1.25, available here https://www.mediawiki.org/wiki/Download ), that may well fix the issue -although it’s hard to be completely sure without trying.

      Hope this helps; best of luck!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s