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/
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__":
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.