-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathg13_db_maintenance.py
136 lines (130 loc) · 4.03 KB
/
g13_db_maintenance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Syntax: python g13_db_maintenance_bot.py
"""
#
# (C) Rob W.W. Hooft, 2004
# (C) Daniel Herding, 2004
# (C) Wikipedian, 2004-2008
# (C) leogregianin, 2004-2008
# (C) Cyde, 2006-2010
# (C) Anreas J Schwab, 2007
# (C) xqt, 2009-2012
# (C) Pywikipedia team, 2008-2012
# (C) Hasteur, 2013
#
__version__ = '$Id$'
#
# Distributed under the terms of the MIT license.
#
import os, re, pickle, bz2, time, datetime, logging
import sys
import wikipedia as pywikibot
from pywikibot import i18n
#DB CONFIG
from db_handle import *
def g13_db_maintenance():
"""
Go through the local DB and clean up any records that are no longer
appropriate
"""
global logger
logger.debug('Opened DB conn')
cur = conn.cursor()
moder = int(sys.argv[1])
cur.execute( \
"SELECT article, editor" + \
" from g13_records " + \
" where " + \
" nominated > '0000-00-00 00:00:00' and" + \
" MOD(id,5) = %s" + \
" ORDER BY id " +\
"", (moder)
)
results = cur.fetchall()
cur = None
for article_item in results:
try:
article = pywikibot.Page(
pywikibot.getSite(),
article_item[0]
)
except:
print "Problem with %s" % article_item[0]
continue
print article_item[0]
if False == article.exists():
#Submission doesn't exisist any more, Remove it from the DB
logger.info('Article %s doesn\'t exisist any more' % \
article_item[0])
curs = conn.cursor()
sql_string = "DELETE from g13_records" + \
" WHERE article = %s and editor = %s;"
curs.execute(sql_string,article_item)
conn.commit()
curs = None
logger.debug('Article %s removed from DB' % \
article_item[0])
continue
if True == article.isRedirectPage():
#Submission is now a redirect. Happy Day, it got promoted to
# article space!
logger.info('Article %s was promoted to mainspace' % \
article_item[0])
curs = conn.cursor()
sql_string = "DELETE from g13_records" + \
" WHERE article = %s and editor = %s;"
curs.execute(sql_string,article_item)
conn.commit()
curs = None
logger.debug('Article %s was removed from DB' % \
article_item[0])
continue
article_text = article.get()
if '{{db-g13}}' not in article_text:
#Check to see if we've ever nominated this article
double_check_sql = "SELECT article, editor " + \
" from g13_records " + \
" where article = %s " + \
" and editor = %s " + \
" and nominated >= '1000-01-01 00:00:00';"
curs = conn.cursor()
curs.execute(double_check_sql,article_item)
results = curs.fetchall()
curs = None
if 0 < len(results):
#Hrm... Article no longer has my CSD template on it
# and I've nominated it. Remove it from the DB
logger.info('Article %s no longer has my csd template on it' % \
article.title())
curs = conn.cursor()
sql_string = "DELETE from g13_records" + \
" WHERE article = %s and editor = %s;"
curs.execute(sql_string,article_item)
conn.commit()
curs = None
logger.debug('Article %s was removed from DB' % \
article_item[0])
continue
conn.close()
logger.info('DB Maintenance completed')
def main():
g13_db_maintenance()
if __name__ == "__main__":
logger = logging.getLogger('g13_maintenance_bot')
logger.setLevel(logging.DEBUG)
trfh = logging.handlers.TimedRotatingFileHandler('logs/g13_db_maintenance', \
when='D', \
interval = 14, \
backupCount = 90, \
)
trfh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
trfh.setFormatter(formatter)
logger.addHandler(trfh)
trfh.doRollover()
try:
main()
finally:
pywikibot.stopme()