forked from mzmcbride/database-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordoverlap.py
executable file
·104 lines (89 loc) · 2.55 KB
/
coordoverlap.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
#!/usr/bin/python
# Public domain; MZMcBride; 2012
import ConfigParser
import datetime
import MySQLdb
import os
import wikitools
config = ConfigParser.ConfigParser()
config.read([os.path.expanduser('~/.dbreps.ini')])
report_title = config.get('dbreps', 'rootpage') + 'Articles containing overlapping coordinates'
report_template = u'''
Articles that contain {{tl|Coord/display/inline,title}} and \
{{tl|Coord/display/title}}; data as of <onlyinclude>%s</onlyinclude>.
{| class="wikitable sortable plainlinks" style="width:100%%; margin:auto;"
|- style="white-space:nowrap;"
! No.
! Article
|-
%s
|}
'''
wiki = wikitools.Wiki(config.get('dbreps', 'apiurl'))
wiki.login(config.get('dbreps', 'username'), config.get('dbreps', 'password'))
conn = MySQLdb.connect(host=config.get('dbreps', 'host'),
db=config.get('dbreps', 'dbname'),
read_default_file='~/.my.cnf')
cursor = conn.cursor()
inline_title_pages = set()
cursor.execute('''
/* coordoverlap.py SLOW_OK */
SELECT
page_id
FROM page
JOIN templatelinks
ON tl_from = page_id
WHERE page_namespace = 0
AND tl_namespace = 10
AND tl_title = 'Coord/display/inline,title';
''')
for row in cursor.fetchall():
inline_title_pages.add(row[0])
title_pages = set()
cursor.execute('''
/* coordoverlap.py SLOW_OK */
SELECT
page_id
FROM page
JOIN templatelinks
ON tl_from = page_id
WHERE page_namespace = 0
AND tl_namespace = 10
AND tl_title = 'Coord/display/title';
''')
for row in cursor.fetchall():
title_pages.add(row[0])
i = 1
output = []
for page_id in inline_title_pages:
if page_id in title_pages:
cursor.execute('''
/* coordoverlap.py SLOW_OK */
SELECT
page_title
FROM page
WHERE page_id = %s;
''' , int(page_id))
page_title = u'[[%s]]' % unicode(cursor.fetchone()[0], 'utf-8')
table_row = u'''\
| %d
| %s
|-''' % (i, page_title)
output.append(table_row)
i += 1
cursor.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=config.get('dbreps', 'editsumm'), bot=1)
cursor.close()
conn.close()