Skip to content

Instantly share code, notes, and snippets.

@toniher
Last active August 29, 2015 13:56
Show Gist options
  • Save toniher/8892683 to your computer and use it in GitHub Desktop.
Save toniher/8892683 to your computer and use it in GitHub Desktop.
Python script for importing old ratings MediaWiki extension into a dump for VoteNY using LP_solve
# Python script for importing old ratings MediaWiki extension http://www.mediawiki.org/wiki/Extension:AjaxRatingScript
# into a dump for more suitable new extension http://www.mediawiki.org/wiki/Extension:VoteNY
# Using LP_solve Python wrapper
from lpsolve55 import *
import MySQLdb
def main():
host = "localhost"
user = "user"
pwd = "passwd"
database = "ratings"
db=MySQLdb.connect(host=host,user=user,passwd=pwd,db=database)
cursor = db.cursor()
# original ratings table
cursor.execute("SELECT id, total_votes, total_value from ratings")
data = cursor.fetchall()
for row in data :
pageid = row[0]
lp = lpsolve('make_lp', 0, 5)
# We force that variables must be integers
lpsolve('set_int',lp,1,1)
lpsolve('set_int',lp,2,1)
lpsolve('set_int',lp,3,1)
lpsolve('set_int',lp,4,1)
lpsolve('set_int',lp,5,1)
lpsolve('set_verbose', lp, IMPORTANT)
ret = lpsolve('add_constraint', lp, [1, 2, 3, 4, 5], EQ, row[2])
ret = lpsolve('add_constraint', lp, [1, 1, 1, 1, 1], EQ, row[1])
# We could make it more complex, but it's risky
#ret = lpsolve('add_constraint', lp, [-1, 1, 0, 0, 0], GE, 0)
#ret = lpsolve('add_constraint', lp, [0, -1, 1, 0, 0], GE, 0)
#ret = lpsolve('add_constraint', lp, [0, 0, 0, 1, -1], GE, 0)
lpsolve('solve', lp)
list_variables = lpsolve('get_variables', lp)[0]
iter = 1
for var in list_variables :
intvar = int(round(var))
if ( intvar > 0 ) :
for i in range(intvar):
# Actual table name depends on your wiki installation. here prefix is mw_
print "INSERT INTO `mw_Vote` (`vote_page_id`, `vote_value`) VALUES ("+pageid+", "+str(iter)+");"
iter = iter + 1
db.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment