#!/usr/bin/python """ simple script to deal w/ bulk modifications of mediawiki user options, which are blobs This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ import sys import re import MySQLdb DB_USER="xxxxxxxxx" DB_PASSWD="xxxxxxxxx" def getConnection(): conn = MySQLdb.connect(host="localhost", user=DB_USER, passwd=DB_PASSWD, db="susdev_wiki") return conn class UserOptions: def __init__(self, conn, name): self.name = name self.optionsDict = {} self.loadOptions(conn) def loadOptions(self, conn): c = conn.cursor() c.execute("SELECT user_options FROM user WHERE user_name = '%s'" % self.name) optionsBlob = c.fetchone()[0] # print optionsBlob self._decodeOptions(optionsBlob) c.close() # return self.optionsDict def setOption(self, name, value): print "Changing %s's %s->%s\n" % (self.name, name, value) self.optionsDict[name] = value def saveOptions(self, conn): optionsBlob = self._encodeOptions() c = conn.cursor() c.execute("update user SET user_options = '%s' WHERE user_name = '%s'" % (optionsBlob, self.name)) c.close() def _encodeOptions(self): blobList = ["%s=%s" % (i,k) for i,k in self.optionsDict.items()] return "\n".join(blobList) def _decodeOptions(self, optionsBlob): """ ripped from the User.php functions that do this in teh mediwiki """ pattern = re.compile("^(.[^=]*)=(.*)$") optionPairs = optionsBlob.split("\n") # import pdb; pdb.set_trace() for pair in optionPairs: m = pattern.match(pair) if m: name, value = (m.group(1), m.group(2)) self.optionsDict[name] = value def main (argv=None): conn = getConnection() cur = conn.cursor() cur.execute("SELECT user_name from user") usernames = [row[0] for row in cur.fetchall()] cur.close() for user in usernames: print "Processing %s" % user u = UserOptions(conn, user) u.setOption("skin", "") u.saveOptions(conn) conn.close() print "Done\n" if __name__ == "__main__": sys.exit(main())