CategoryDocumentation Removals

karderio karderio at gmail.com
Mon Jul 3 23:24:21 UTC 2006


I have written a little Python script that can replace string A with
string B in a wiki page. This would be like running sed on the pages...

However, if you simply remove the "CategoryDocumentation" string, there
may be some instances which are imperfect. One example would be if
"CategoryDocumentation" is the only category a page is in, running this
script will remove the "CategoryDocumentation" string, but not the
horizontal rule ("----")...

I've provided the script anyhow, perhaps it could inspire someone to
modify it to behave better, or perhaps it could be satisfactory as is...

WARNING : no guarantee of any type is provided, I have tested this on
just one page, and haven't spent much time writing or testing it. It
uses your personnel wiki account to do the damage - er work - so
beware ;)

To use it, copy the script (end of mail) to a file and give it execute
permission. On the command line feed it the names (not address) of the
pages you want to modify separated by spaces.

It fills out the edit comments by it's self.

Probably best, if doing many edits, to do a run with the "toReplace"
string terminated by a space, then without the space. This should then
work around spaces hanging about and indenting things that shouldn't be.

If it don't work, fix it ;)

btw, I have another script to transfer pages from the wiki to the
community documentation site, if it can be of use prod me :)

Love, Karderio.


----------------------------

#!/usr/bin/env python

# This script transfers a page from one wiki to another. It was designed
to aid
# with the migration of documentation from the Ubuntu wiki to
help.ubuntu.com.
#
# !! MoinMoin wiki only !!
#
# Original author : Karderio (: I release this script into the Public
Domain :)

# Usage : fill your username and password in the apropriate variables
and save.
# In a terminal, type apthe to script followed bu names of pages to
transfer.

# Bugs : your username and password must be the same on both wikis


import urllib
import sys


# *** Constants you can edit to modify script behaviour ***
userName = "*********"
userPassword = "*********"
# Remember trailing slash and protocol for URLs ;)
wiki = "https://help.ubuntu.com/community/"
toReplace = "CategoryDocumentation "
replaceWith = ""


# *************************** #
# Subclassing stuff to get a cookie to be sent
class AppURLopener(urllib.FancyURLopener):
    def __init__(self, cookie=None, *args):
        apply(urllib.FancyURLopener.__init__, (self,) + args)
        if cookie:
            self.addheader( "Cookie", cookie)
# *************************** #


# Log in to wiki

print("Logging in to wiki : %s" % wiki);

params = urllib.urlencode({'action':'userform','username':userName,
'password':userPassword, 'login':'Login'})
loginPage = urllib.urlopen("%sUserPreferences" % wiki, params)

cookie = loginPage.info().get("Set-Cookie")


# For each parameter, ignoring the first (script location) thanks to
slice
for pageName in sys.argv[1:] :

	pageAddress = "%s%s" % (wiki, pageName)


	# Get page content

	print("Downloading page : %s" % pageAddress);

	pageContent = urllib.urlopen("%s?action=raw" % pageAddress).read()

	print("Replacing '%s' with '%s'" % (toReplace, replaceWith))


	# Save page to wiki

	print("Saving page to wiki : %s" % pageAddress);

	comment = "Replaced '%s' with '%s'" % (toReplace, replaceWith)

	urllib._urlopener = AppURLopener(cookie)
	params = urllib.urlencode({'action':'savepage','rev':"0",
'savetext':pageContent.replace(toReplace, replaceWith),
'comment':comment})
	aPage = urllib.urlopen("%s?action=edit" % pageAddress, params)


	print
	print


print "Operation terminated"

# End of script





More information about the ubuntu-doc mailing list