#! /usr/bin/python import wikipedia, pagegenerators import datetime, MySQLdb import cgitb; cgitb.enable(logdir='/home/dispenser/public_html/cgi-bin/logs/', context=20) def colorValue(subset, total, revert = False): v = min(255 * subset / total, 255) if (revert): v = 255 - v; blue = 00 if (v < 128): # Red to Yellow red = 255 green = 2 * v else: # Yellow to Green red = 2 * (255 - v) green = 255 return "#%02X%02X%02X" % (red, green, blue) class UserActivity: def __init__(self): self.page = None self.days = 240 def main(self): site = wikipedia.getSite() action = 0 for arg in wikipedia.handleArgs(): if arg.startswith('-page:'): self.page = wikipedia.Page(site, arg[6:]) site = self.page.site() elif arg.startswith('-inactivetime:'): pass elif arg.startswith('-action:edit'): action = 2 elif arg.startswith('-days:'): self.days = int(arg[6:]) # print '' # print '' print '
' print '
Member activity check' print '

:
' % (self.page and self.page.title() or '') print ' days
'%self.days print '
' print '
' #print 'Example: WikiPorject Trains/Members

' print '
' if not self.page: return page_title = self.page.title(underscore=True) if page_title.lower().startswith('wikipedia:'): page_title=page_title[10:] elif page_title.lower().startswith('project:'): page_title=page_title[8:] db = MySQLdb.connect(db=site.dbName()+'_p', host=site.dbName().replace('_', '-') + '-p.db.toolserver.org', read_default_file="/home/dispenser/.my.cnf") self.conn = db.cursor() self.conn.execute("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED") self.conn.execute(""" SELECT user_id, user_name, user_editcount, TIMESTAMP(user_registration) FROM page RIGHT JOIN pagelinks ON page_id=pl_from RIGHT JOIN user ON user_name=replace(pl_title,"_"," ") WHERE pl_namespace=2 AND page_namespace=4 AND page_title=%s;""", (page_title,) ) if action == 0: self.infoTable() else: self.updatePage() self.conn.close() def updatePage(self): import re text = self.page.get() msg_commented = 0 msg_removed = 0 msg_renamed = 0 for (user_id, user_name, user_editcount, user_registration) in self.conn.fetchall(): self.conn.execute("SELECT rd_title FROM page JOIN redirect ON page_id=rd_from WHERE rd_namespace=2 AND page_namespace=2 AND page_title=replace(%s,' ','_')", (user_name)) user_rd = self.conn.fetchone() self.conn.execute("SELECT TIMESTAMP(rev_timestamp), TIMESTAMPDIFF(DAY,rev_timestamp,now()) FROM revision WHERE rev_user=%s ORDER BY rev_timestamp DESC LIMIT 1", (user_id,)) lasteditdate = self.conn.fetchone() p = re.compile(r'(?mi)(^[#*].*(?:\[\[|\{\{)(?:user)[:|] *)(%s)( *(?=[]{|}[]).*)' % re.escape(user_name).replace('\ ', '[_ ]')) # List update logic if user_editcount <= 20 and user_rd: wikipedia.output('Renaming [[User:%s]] to [[User:%s]]'%(user_name, user_rd[0])) msg_renamed += 1 text = p.sub(r'\g<1>%s\g<3>'%user_rd[0], text) elif user_editcount == 1 and lasteditdate and lasteditdate[1] > 180: wikipedia.output('Commenting [[User:%s]] single edit account (%d months inactive)'%(user_name, lasteditdate[1]/30)) msg_commented += 1 text = p.sub(r'', text) elif user_editcount == 0: if not lasteditdate: wikipedia.output('Removing [[User:%s]] - [[wp:vanish]]ed'%user_name) msg_removed += 1 text = p.sub(r'', text) else: wikipedia.output('Bad edit info for [[User:%s]]'%user_name) elif lasteditdate and lasteditdate[1] > self.days and lasteditdate[1]/user_editcount >= 7: wikipedia.output('Commenting [[User:%s]] (%d edits, %d months inactive)' % (user_name, user_editcount, lasteditdate and lasteditdate[1]/30, )) msg_commented += 1 text = p.sub(lasteditdate[0].strftime(r""), text) else: pass wikipedia.showDiff(self.page.get(), text) wikipedia.setAction(self.summary(parts={'removed':msg_removed, 'renamed':msg_renamed, 'commented out':msg_commented})) self.page.put(text) def summary(self, parts): msg = [] wordform = ('user ', 'users ') for (key, value) in parts.iteritems(): if key and value: if value == 1: msg += ["%s%s"%(wordform[0], key)] else: msg += ["%d %s%s"%(value, wordform[1], key)] if len(msg) <= 2: return ' and '.join(msg) else: return ', '.join(msg[:-1]) + ', and '+msg[-1] def infoTable(self): print '

It is suggested that you used the sort table bookmarklet until I get sorting implemented

' print '''

sort tables [HACK]

''' print '' print '' print '' print '' for (user_id, user_name, user_editcount, user_registration) in self.conn.fetchall(): self.conn.execute(""" SELECT TIMESTAMP(rev_timestamp), TIMESTAMPDIFF(DAY,rev_timestamp,now()) FROM revision WHERE rev_user=%s ORDER BY rev_timestamp DESC LIMIT 1""", (user_id,)) d = self.conn.fetchone() print '' css = d and d[1] > self.days and d[1]/user_editcount >= 7 and ' style="background:lightblue;"' or '' print '%(user_editcount)s' % locals() if d: # bt: Blue Trigger value print '' % (d[0].strftime("%d %b %Y"), colorValue(d[1], self.days, revert=True), '%4.2f'%(d[1]/float(user_editcount)), d[1]) print '' print '
UserEdits *RegisteredLast editDays since
%(user_name)s (talkcontribs)%(user_registration)s%s% 5d
' wikipedia.output('* this includes deleted edits') if __name__ == "__main__" and wikipedia.handleUrlAndHeader(): import time startTime = time.time() try: wikipedia.startContent(form=False) ua = UserActivity() ua.main() finally: wikipedia.endContent() wikipedia.stopme()