#! /usr/bin/env python# Public domain; MZMcBride; 2012from__future__importgeneratorsimportdatetimeimportMySQLdbimportwikitoolsimportsettingsreport_title=settings.rootpage+'Dubious stub categories'report_template=u'''\Dubious stub categories; data as of <onlyinclude>%s</onlyinclude>.{| class="wikitable sortable" style="width:100%%; margin:auto;"|- style="white-space:nowrap;"! No.! Category%s|}'''wiki=wikitools.Wiki(settings.apiurl)wiki.login(settings.username,settings.password)target_cat='Stub_categories'master_dict={}defget_subcats(cursor,cat):globalmaster_dictresults=[]cursor.execute(''' /* dubiousstubcats.py */ SELECT page_title FROM page JOIN categorylinks ON cl_from = page_id WHERE cl_to = %s AND page_namespace = 14; ''',cat)rows=cursor.fetchall()forrowinrows:results.append(row[0])try:master_dict[cat]=resultsexceptKeyError:returnFalsereturnresultsdefwalk_tree(cursor,target_cat):subcats=get_subcats(cursor,target_cat)forsubcatinsubcats:ifsubcatnotinmaster_dict.keys():forsubsubcatinwalk_tree(cursor,subcat):yieldsubsubcat,subcatselse:yieldsubcat,subcatsconn=MySQLdb.connect(host=settings.host,db=settings.dbname,read_default_file='~/.my.cnf')cursor=conn.cursor()forhelloinwalk_tree(cursor,target_cat):# Surely there's a better way to do thishere='silliness'all_cats_from_target_cat=set()forkinmaster_dict.keys():all_cats_from_target_cat.add(k)i=1output=[]formemberinall_cats_from_target_cat:ifnotmember.endswith('_stubs'):ifnotmember.endswith('_stub_templates'):cat_title=u'[[:Category:%s|%s]]'%(unicode(member,'utf-8'),unicode(member,'utf-8'))table_row=u'''|-| %d| %s'''%(i,cat_title)output.append(table_row)i+=1cursor.execute(''' SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(rc_timestamp) FROM recentchanges ORDER BY rc_timestamp DESC LIMIT 1; ''')rep_lag=cursor.fetchone()[0]time_diff=datetime.datetime.utcnow()-datetime.timedelta(seconds=rep_lag)current_of=time_diff.strftime('%H:%M, %d %B %Y (UTC)')report=wikitools.Page(wiki,report_title)report_text=report_template%(current_of,'\n'.join(output))report_text=report_text.encode('utf-8')report.edit(report_text,summary=settings.editsumm,bot=1)cursor.close()conn.close()