-
Notifications
You must be signed in to change notification settings - Fork 2
/
rfc_stats.py
executable file
·549 lines (456 loc) · 15.4 KB
/
rfc_stats.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Calculates participant statistics for MediaWiki RFC-s/votes/etc.
Assumptions:
* RFC is on a single page, with each position having its own section
* every vote starts with # (numbered list item) - other lines (including those starting with ##, #: etc) are ignored
* first userpage/talkpage link in the line is that of the voter (this will fail sometimes, but hopefully not often
enough to throw off the results)
* first date string in the line is the time of the vote
Usage:
* install dependencies with pip install -r requirements.txt
* copy config.dist.py to config.py, edit settings
* run rfc_stats.py
"""
from collections import OrderedDict
import re
import locale
from datetime import datetime
import codecs
import wikitools
import csv
import HTMLParser
import config
locale.setlocale(locale.LC_TIME, config.date_locale)
html_parser = HTMLParser.HTMLParser()
class Vote:
"""Data about a single vote"""
def __init__(self):
self.section_label = None
"""
@type: str
One of the section labels
"""
self.text = None
"""
@type: str
Full text of the vote
"""
self.user = None
"""
@type: User
"""
self.datetime = None
"""
time of the vote
@type : datetime.datetime
"""
self.local_gap = None
"""time spent inactive on the local wiki before the RFC (months, rounded down)"""
self.global_gap = None
"""time spent inactive everywhere before the RFC (months, rounded down)"""
def __str__(self):
return str(self.to_dict())
def to_dict(self):
self_dict = self.__dict__
if self_dict['user']:
self_dict['user'] = self_dict['user'].to_dict()
return self_dict
@classmethod
def from_line(cls, page, line, section_label):
"""
Creates a Vote from a line of text (which should contain a signature). There is no sanity check done
to see if it is indeed a vote.
@type page: VotePage
@type line: str
@type section_label: str
@rtype: Vote
"""
vote = cls()
vote.section_label = section_label
vote.text = line
vote.datetime = cls.parse_datetime(line)
username = cls.parse_username(line)
if username:
vote.user = User(page.api, username)
try:
vote.user.load_data()
except NoSuchUserException:
vote.user = None
except:
print(vote.text)
raise
return vote
@staticmethod
def parse_username(line):
"""
@type line: str
@rtype: str
"""
# some people use @[[User:Foo]] to refer to others, so we skip sigs starting with those
m = re.search(r'[^@]\[\[User(?:_talk)?:([^|\]]+)', line)
if m:
username = m.group(1)
# some people write their usernames in weird ways in their signatures
return html_parser.unescape(username)
@staticmethod
def parse_datetime(line):
"""
@type line: str
@rtype: str
"""
m = re.search(config.date_regexp, line)
if m:
return datetime.strptime(m.group(0), config.date_format)
@staticmethod
def filter_vote_lines(lines):
"""
Iterates through a set of lines and only returns those which seem to be votes (top-level ordered lists).
@type lines: collections.Iterable[string]
"""
for line in lines:
if re.match(r'#[^#*:]', line):
yield line
def get_plaintext(self):
return html_parser.unescape(re.sub(r'<.*?>', '', self.text))
class Api:
endpoint = None
"""@type wikitools.wiki.Wiki"""
def __init__(self, endpoint):
"""
@type endpoint: wikitools.wiki.Wiki
"""
assert isinstance(endpoint, wikitools.wiki.Wiki)
self.endpoint = endpoint
@classmethod
def from_domain(cls, domain):
"""
@type domain: string
@rtype: Api
"""
return cls(wikitools.wiki.Wiki("http://%s/w/api.php" % domain))
@classmethod
def from_globaluserinfo_url(cls, url):
"""
@type url: string
@rtype: Api
"""
return cls(wikitools.wiki.Wiki("%s/w/api.php" % url))
@staticmethod
def timestamp_to_datetime(timestamp):
"""
@type timestamp: string
@rtype: datetime.datetime
"""
return datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
@staticmethod
def chunks(seq, size):
"""
Chops a list or iterable into segments.
@type seq: list
@type size: int
"""
chunk = []
seq = iter(seq)
try:
for i in range(size):
chunk.append(next(seq))
except StopIteration:
if len(chunk) > 0:
yield chunk
return
yield chunk
def call(self, **params):
"""
Makes a call to the API. Pass API parameters as function parameters: api.call(action='query', list='users', ...
@rtype: dict
"""
return wikitools.api.APIRequest(self.endpoint, params).query(False)
def __call__(self, **params):
return self.call(**params)
def get_section_text(self, page=None, revision=None, section=None):
"""
Returns the text of the specified section. section is required; one of page and revision is required.
@type page: str
@type revision: int
@type section: int
@rtype: str
"""
if not section or not page and not revision:
raise ValueError('section and either page or revision is required')
params = {
'action': 'query',
'prop': 'revisions',
'rvsection': section,
'rvprop': 'content'
}
if revision:
params['revids'] = revision
else:
params['titles'] = page
for p in self(**params)['query']['pages'].values():
for r in p['revisions']:
return r['*']
class VotePage:
def __init__(self, api, page=None, revision=None, sections=None):
"""
@type api: Api
@type page: str
@type revision: int
@type sections: dict[str, int]
"""
if not sections or not page and not revision:
raise ValueError('sections and either page or revision is required')
self.api = api
"""@type: Api"""
self.page = page
"""
Page name (ignored if there is a revision id)
@type: str
"""
self.revision = revision
"""
Revision id to analyze, None for current
@type: int
"""
self.sections = self.create_ordered_dict(sections)
"""
Section ids and internal labels (will be used in the output)
@type: OrderedDict[str, int]
"""
@staticmethod
def create_ordered_dict(sections):
"""
Orders sections by
@type sections: dict[str, int]
@rtype: OrderedDict[str, int]
"""
return OrderedDict(sorted(sections.items(), key=lambda t: t[1]))
def get_page_arg(self):
arg = {}
if self.revision:
arg['revision'] = self.revision
else:
arg['page'] = self.page
return arg
def get_vote_lines(self, section):
"""
@type section: int
"""
args = self.get_page_arg()
args['section'] = section
all_lines = self.api.get_section_text(page=self.page, revision=self.revision, section=section).splitlines()
for line in Vote.filter_vote_lines(all_lines):
yield line
def get_votes(self, section=None, limit=None):
"""
@type section: int|str
@param section: limit votes to a single section
@type limit: int
@param limit: only return a limited number of votes
"""
i = 0
for section_label, section_id in self.sections.items():
if section and section_label != section and section_id != section:
continue
for line in self.get_vote_lines(section_id):
i += 1
yield Vote.from_line(self, line, section_label)
if i == limit:
return
class GlobalUser:
def __init__(self, username):
"""
@type username: str
"""
self.username = username
"""
@type str
"""
self.home_wiki = None
"""
Home wiki db name (enwiki, frsource etc)
@type: str
"""
self.wikis = None
"""
List of wiki db names where the user has an account
@type: list[str]
"""
self.wiki_urls = None
"""
List of wiki URLs (schema + domain, no trailing slash) where the user has an account
@type: list[str]
"""
self.editcount = None
"""
@type: int
"""
self.groups = None
"""
A union of all the group roles held at some wiki
@type: list[str]
"""
self.first_edit = None
"""
@type: datetime.datetime
"""
def __str__(self):
return str(self.to_dict())
def to_dict(self):
self_dict = self.__dict__
del self_dict['wikis']
del self_dict['wiki_urls']
return self_dict
@classmethod
def from_globaluserinfo(cls, username, global_data):
"""
@type username: str
@type global_data: dict
"""
user = cls(username)
user.home_wiki = global_data['home']
user.editcount = global_data['editcount']
user.wikis = []
user.wiki_urls = []
for account in global_data['merged']:
user.wikis.append(account['wiki'])
user.wiki_urls.append(account['url'])
return user
def load_data(self):
self.groups = []
for url in self.wiki_urls:
api = Api.from_globaluserinfo_url(url)
data = api(action='query', list='users|usercontribs',
ususers=self.username, usprop='groups',
ucuser=self.username, ucdir='newer', uclimit=1, ucprop='timestamp')['query']
self.groups.extend(data['users'][0]['groups'])
if len(data['usercontribs']) > 0:
first_edit_on_this_wiki = Api.timestamp_to_datetime(data['usercontribs'][0]['timestamp'])
if not self.first_edit or first_edit_on_this_wiki < self.first_edit:
self.first_edit = first_edit_on_this_wiki
class NoSuchUserException(BaseException):
pass
class User:
def __init__(self, api, username):
"""
@type api: Api
@param api: Api object to the user's wiki
"""
self.api = api
"""@type: Api"""
self.username = username
"""Username without the User: prefix"""
self.global_user = None
"""
None means uninitialized, False means not attached.
@type: GlobalUser
"""
self.editcount = None
"""
@type: int
"""
self.groups = None
"""
@type: list[str]
"""
self.first_edit = None
"""
@type: datetime.datetime
"""
def __str__(self):
return str(self.to_dict())
def to_dict(self):
self_dict = self.__dict__
if self_dict['global_user']:
self_dict['global_user'] = self_dict['global_user'].to_dict()
return self_dict
def is_admin(self):
return 'sysop' in self.groups
def load_data(self, data=None):
if not data:
data = self.api(action='query',
list='users|usercontribs',
ususers=self.username, usprop='editcount|groups|registration',
ucuser=self.username, ucdir='newer', uclimit=1, ucprop='timestamp',
meta='globaluserinfo', guiuser=self.username, guiprop='editcount|groups|merged')['query']
try:
local_data = data['users'][0]
global_data = data['globaluserinfo']
if 'missing' in local_data or 'invalid' in local_data:
raise NoSuchUserException(self.username)
self.groups = local_data['groups']
self.editcount = local_data['editcount']
first_local_edit = data['usercontribs'][0]['timestamp']
self.first_edit = Api.timestamp_to_datetime(first_local_edit)
if self.data_is_global(global_data):
self.global_user = GlobalUser.from_globaluserinfo(self.username, global_data)
#self.global_user.load_data()
else:
self.global_user = False
except:
print(self.username, data)
raise
def data_is_global(self, global_data):
merged = False
if 'merged' in global_data:
for account in global_data['merged']:
if account['wiki'] == 'commonswiki':
merged = True
break
return merged
def get_local_gap(self, user):
data = self.api(action='query',
list='usercontribs', ucuser=self.username, ucdir='older', uclimit=500, ucprop='title|timestamp')
def get_global_editcount(self):
if self.global_user:
return self.global_user.editcount
else:
return None
def get_home_wiki(self):
if self.global_user:
return self.global_user.home_wiki
else:
return None
class CsvVoteWriter:
def __init__(self, filename):
self.filename = filename
self.file = None
self.writer = None
self.open()
def open(self):
self.file = open(self.filename, 'wt')
self.file.write(codecs.BOM_UTF8)
self.writer = csv.writer(self.file)
self.writerow(['User', '!vote section', '!vote date', 'Commons edit count', 'First Commons edit',
'Global edit count', 'Home wiki', 'Full text'])
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.file.close()
def writerow(self, row):
"""
@type row: list
"""
row = [s.encode('utf-8') if type(s) is str or type(s) is unicode else str(s) for s in row]
self.writer.writerow(row)
def write(self, vote):
"""
@type vote: Vote
"""
self.writerow([
vote.user.username if vote.user else '-',
vote.section_label,
vote.datetime.isoformat(' ') if vote.datetime else '-',
vote.user.editcount if vote.user else '-',
vote.user.first_edit.isoformat(' ') if vote.user else '-',
vote.user.get_global_editcount() or '-' if vote.user else '-',
vote.user.get_home_wiki() or 'commonswiki' if vote.user else '-',
vote.get_plaintext(),
])
vote_page = VotePage(Api.from_domain(config.wiki), page=config.page, revision=config.revision, sections=config.sections)
with CsvVoteWriter('votes.csv') as writer:
for i, vote in enumerate(vote_page.get_votes()):
writer.write(vote)
print('%d: %s' % (i, vote.user.username if vote.user else '-'))