Updating a Confluence Wiki with a script

In the process of creating a wikified version of the LKSF, i had the problem of importing all those files into the Wiki. As wikis.sun.com is based on the Atlassian Confluence i couldn´t use the already available scripts for Mediawiki for example . I wasn´t really fond of the idea to do this manually, thus i´ve asked via Twitter for a solution and i´ve got a hint for the xmlrpc interface. At the end, i´ve created a script with stolen … sorry … copied components of many other scripts (like this one or this one). My script compares the current version with the new version, uploads the new version and creates new pages if nescessary.It takes three command line parameters: the name of the Wikipage, the name of the space (something like a subwiki or a administrative domain in Confluence) and the file you want to upload. Sorry for the programming style … python isn´t really my native programming language, but the xmlrpc is solved really good in python and it was a good reason to refresh my really rusty python skills. Okay … let´s get to the usage. For example:

./export.py sat.lksf_sat_crashcore.tex lksf sat/lksf_sat_crashcore.tex.wikified

This uploads the file sat/lksf_sat_crashcore.tex.wikified into the space lksf with the page name sat.lksf_sat_crashcore.tex>. This leads to the wiki page http://wikis.sun.com/display/lksf/sat.lksf_sat_crashcore.texNow to the source of the script export.py:


import sys
import xmlrpclib
import hashlib

title = sys.argv[1]
space = sys.argv[2]
file  = sys.argv[3]
content = ''

print "================="
print file

chash = hashlib.sha256()

 datei = open(file, 'r')
 for line in datei:
    content = content + line
    if line == "BREAKBREAKBREAK\n":
except IOError:
 print  file , "not readable"

contenthash = chash.hexdigest();
print "new content hash:", contenthash

s = xmlrpclib.Server("http://wikis.sun.com/rpc/xmlrpc")
token = s.confluence1.login("username", "password")

newpage= "false"

 	page2 = s.confluence1.getPage(token, space, title)
 	oldcontent = page2["content"]
	ochash = hashlib.sha256()
	oldcontenthash = ochash.hexdigest();
	print "old content hash:", oldcontenthash
	if oldcontenthash == contenthash:
		print "no update needed"
except xmlrpclib.Fault, err:
    print "A fault occurred"
    print "Fault code: %d" % err.faultCode
    newpage = "true"

if newpage == "true":
	page["title"] = title
	page["content"] = content
	page["space"] = space
    page["content"] = content

	s.confluence1.storePage(token, page)
except xmlrpclib.Fault, err:
    print "A fault occurred"
    print "Fault code: %d" % err.faultCode
    print "Fault string: %s" % err.faultString

print title, "updated"

Just in case you wonder because of this “BREAKBREAKBREAK” line. It´s just for debugging purposes. As i wrote yesterday, the xmlrpc is a little bit bitchy about charsets and the error message wasn´t helpful. It was easier to find the problem by bisecting the file again and again. And this was done by the “BREAKBREAKBREAK” line ;)