-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher.py
More file actions
executable file
·364 lines (343 loc) · 11.9 KB
/
watcher.py
File metadata and controls
executable file
·364 lines (343 loc) · 11.9 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# version 1.4
import cgi
import os
import urllib
import re
import MySQLdb
import Cookie
import hashlib
import settings
if os.environ.has_key('HTTP_COOKIE'):
dough = os.environ['HTTP_COOKIE']
cookies = dough.split(';')
for i in cookies:
cookie_session = i.split('=')[1]
else:
cookie_session = 'invalid'
def trusted_users():
trusted_users = []
conn = MySQLdb.connect(host='sql-s7',
db='metawiki_p',
read_default_file='/home/mzmcbride/.my.cnf')
cursor = conn.cursor()
cursor.execute('''
/* watcher.py SLOW_OK */
SELECT DISTINCT
pl_title
FROM pagelinks
JOIN page
ON pl_from = page_id
WHERE page_namespace = 0
AND page_title = 'Toolserver/watcher'
AND pl_namespace IN (2,3);
''')
for row in cursor.fetchall():
trusted_users.append(re.sub('_', ' ', row[0]))
cursor.close()
conn.close()
return trusted_users
def database_list():
conn = MySQLdb.connect(host='sql-s3',
db='toolserver',
read_default_file='/home/mzmcbride/.my.cnf')
cursor = conn.cursor()
cursor.execute('''
/* watcher.py database_list */
SELECT
dbname
FROM wiki
WHERE is_closed = 0;
''')
databases = cursor.fetchall()
cursor.close()
conn.close()
return [database[0] for database in databases]
def choose_host_and_domain(db):
conn = MySQLdb.connect(host='sql-s3',
db='toolserver',
read_default_file='/home/mzmcbride/.my.cnf')
cursor = conn.cursor()
cursor.execute('''
/* watcher.py choose_host_and_domain */
SELECT
server,
domain
FROM wiki
WHERE dbname = '%s';
''' % db)
for row in cursor.fetchall():
if str(row[0]) == '1':
host = 'sql-s1-fast'
else:
host = 'sql-s%s' % str(row[0])
domain = '%s' % row[1]
cursor.close()
conn.close()
return {'host': host, 'domain': domain}
def page_info(db, namespace, page_title):
conn = MySQLdb.connect(host=host,
db=db,
read_default_file='/home/mzmcbride/.my.cnf')
cursor = conn.cursor()
cursor.execute('''
/* watcher.py page_info */
SELECT
page_is_redirect,
COUNT(*)
FROM watchlist
JOIN toolserver.namespacename
ON dbname = %s
AND wl_namespace = ns_id
LEFT JOIN page
ON page_namespace = wl_namespace
AND page_title = wl_title
WHERE ns_name = %s
AND wl_title = %s;
''' , (db, namespace, page_title))
for row in cursor.fetchall():
page_status = row[0]
count = row[1]
cursor.close()
conn.close()
return { 'page_status': page_status, 'count': count}
secret_key = settings.secret_key
trusted_keys = []
for name in trusted_users():
n = hashlib.md5()
n.update(name)
n.update('watcher')
n.update(secret_key)
trusted_keys.append(n.hexdigest())
if cookie_session in trusted_keys:
logged_in = True
else:
logged_in = False
form = cgi.FieldStorage()
# Pick a db; make enwiki_p the default
if form.getvalue('db') is not None:
db = form.getvalue('db')
else:
db = 'enwiki_p'
# All right, now let's pick a host and domain
try:
connection_props = choose_host_and_domain(db)
host = connection_props['host']
domain = connection_props['domain']
except:
host = None
domain = None
if 'titles' in form:
input = form["titles"].value
else:
input = ''
cj_info = '' # In case it doesn't get set later.
redirect_count = 0
exclude_count = 0
output = []
if host is not None:
for title in input.split('|'):
# Eliminate LTR and RTL marks and strip extra whitespace.
title = re.sub(r'(\xe2\x80\x8e|\xe2\x80\x8f)', '', title).strip(' ')
if title == '':
continue
else:
try:
ns_name = re.sub('_', ' ', title.split(':', 1)[0][0].upper() + title.split(':', 1)[0][1:])
except:
ns_name = ''
if title.split(':', 1)[0] == title:
ns_name = ''
try:
pre_title = title.split(':', 1)[1]
except:
pre_title = title.split(':', 1)[0]
if re.search('wikt', db, re.I):
page_title = re.sub(r'(%20| )', '_', pre_title[0] + pre_title[1:])
else:
try:
page_title = re.sub(r'(%20| )', '_', pre_title[0].upper() + pre_title[1:])
except:
page_title = ''
combined_title = re.sub(r'(%20| )', '_', '%s:%s' % (ns_name, page_title))
title_info = page_info(db, ns_name, page_title)
page_status = title_info['page_status']
if page_status is None:
css_class = 'red'
elif page_status == 1:
redirect_count += 1
css_class = 'redirect'
else:
css_class = 'normal'
count = title_info['count']
if count == 0 and re.search(':', title):
ns_name = ''
if page_info(db, ns_name, combined_title) > 0:
title_info = page_info(db, ns_name, combined_title)
count = title_info['count']
pretty_title = '%s:%s' % (re.sub('_', ' ', ns_name), re.sub('_', ' ', combined_title))
else:
ns_name = re.sub('_', ' ', title.split(':', 1)[0][0].upper() + title.split(':', 1)[0][1:])
title_info = page_info(db, ns_name, page_title)
count = title_info['count']
pretty_title = '%s:%s' % (re.sub('_', ' ', ns_name), re.sub('_', ' ', page_title))
elif not re.search(':', title):
ns_name = ''
if re.search('wikt', db, re.I):
pre_title = re.sub(r'(%20| )', '_', pre_title[0] + pre_title[1:])
else:
pre_title = re.sub(r'(%20| )', '_', pre_title[0].upper() + pre_title[1:])
title_info = page_info(db, ns_name, pre_title) # Bad hack like what.
count = title_info['count']
pretty_title = '%s' % (re.sub('_', ' ', pre_title))
else:
pretty_title = '%s:%s' % (re.sub('_', ' ', ns_name), re.sub('_', ' ', page_title))
try:
pretty_title = pretty_title.lstrip(':').decode('utf-8')
except UnicodeDecodeError:
pretty_title = pretty_title.lstrip(':').decode('latin-1')
# Just for fun :-)
try:
if re.match('centi(jimboe?s?|jimbeaux?)$', form["measure"].value.lower(), re.I):
cj_count = page_info(db, 'User', 'Jimbo_Wales')['count']
cj_info = '<div id="subheadline">1 centijimbo is %.2f watchers</div>' % (float(cj_count)/100)
cj_header = '<th class="header">Centijimbos</th>'
if logged_in or not count < 30:
cj_data = '<td>%.1f</td>' % ((float(count)/cj_count) * 100)
elif count < 30:
cj_data = '<td>—</td>'
else:
cj_info = ''
cj_header = ''
cj_data = ''
except:
cj_info = ''
cj_header = ''
cj_data = ''
if logged_in:
count = count
elif count < 30:
count = '—'
exclude_count += 1
else:
count = count
if combined_title != 'User_talk:Durova' and combined_title != 'User:Durova':
table_row = '<tr><td><a href="//%s/wiki/%s" title="%s" class="%s">%s</a></td><td>%s</td>%s</tr>' % (domain,
urllib.quote(pretty_title.encode('utf8')),
cgi.escape(pretty_title.encode('utf8'),quote=True),
css_class,
cgi.escape(pretty_title.encode('utf8'),quote=True),
count,
cj_data)
else:
table_row = ''
output.append(table_row)
if logged_in:
if os.environ['QUERY_STRING']:
login_footer = '<a href="/~mzmcbride/cgi-bin/login.py?logout=1&tool=watcher&%s" title="log out">log out</a>' % os.environ['QUERY_STRING']
else:
login_footer = '<a href="/~mzmcbride/cgi-bin/login.py?logout=1&tool=watcher" title="log out">log out</a>'
else:
if os.environ['QUERY_STRING']:
login_footer = '<a href="/~mzmcbride/cgi-bin/login.py?tool=watcher&%s" title="log in">log in</a>' % os.environ['QUERY_STRING']
else:
login_footer = '<a href="/~mzmcbride/cgi-bin/login.py?tool=watcher" title="log in">log in</a>'
if exclude_count > 0:
exclude_footer = '— indicates the page has fewer than 30 watchers<br />'
else:
exclude_footer = ''
if redirect_count > 0:
redirect_footer = 'redirects are in <i>italics</i>'
else:
redirect_footer = ''
print """\
Cache-Control: no-cache
Content-Type: text/html;charset=utf-8\n
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="/~mzmcbride/style.css?2" type="text/css" />
<script type="text/javascript" src="/~mzmcbride/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/~mzmcbride/jquery.tablesorter.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('input.focus:first').focus();
$('#results').tablesorter({widgets: ['zebra']});
}
);
</script>
<title>watcher</title>
</head>
<body>
<div class="header" id="main-title"><a href="/~mzmcbride/watcher/" title="watcher">watcher</a></div>
%s""" % (cj_info)
if input:
if db and host is not None and input:
print """\
<table id="results" class="inner-table">
<thead>
<tr>
<th class="header">Page</th>
<th class="header">Watchers</th>
%s</tr>
</thead>
<tbody>
%s
</tbody>
</table>""" % (cj_header, '\n'.join(output))
else:
print """\
<pre>
There was some sort of error. Sorry. :-(
</pre>"""
elif host is None:
print """\
<pre>
You didn't specify an appropriate database name.
</pre>"""
else:
print """\
<form action="/~mzmcbride/watcher/" method="get">
<table id="input" class="inner-table">
<tr>
<th colspan="2" class="header">Input page titles. Separate multiple titles with |.</th>
</tr>
<tr>
<th>Database</th>
<th>
<select id="database" name="db">"""
for i in database_list():
if i == '%s' % db:
print """\
<option value="%s" selected="selected">%s</option>""" % (i, i)
else:
print """\
<option value="%s">%s</option>""" % (i, i)
print """\
</select>
</th>
</tr>
<tr>
<td colspan="2" id="input-cell">
<input class="focus" id="input" name="titles" size="50" /><input id="go-button" type="submit" value="Go" />
</td>
</tr>
</table>
</form>"""
print """\
<div id="footer">
<div id="redirect-info">
%s%s
</div>
<div id="meta-info">
%s<!--
--> <b>·</b> <!--
-->public domain<!--
--> <b>·</b> <!--
--><a href="//en.wikipedia.org/w/index.php?title=User_talk:MZMcBride/watcher&action=edit§ion=new" title="Report a bug">bugs</a>
</div>
</div>
</body>
</html>""" % (exclude_footer, redirect_footer, login_footer)