-
Notifications
You must be signed in to change notification settings - Fork 6
/
bumble-bee.py
executable file
·1211 lines (1061 loc) · 49.4 KB
/
bumble-bee.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python
#encoding: utf-8
"""
Bumble Bee is responsible for collecting statistics and other information from
sites registered on WikiApiary. See http://wikiapiary.com/wiki/User:Bumble_Bee
for more information.
Jamie Thingelstad <jamie@thingelstad.com>
http://wikiapiary.com/wiki/User:Thingles
http://thingelstad.com/
"""
import sys
import time
import socket
import json as simplejson
import re
import gzip
import html
import html.parser
from bs4 import BeautifulSoup
import operator
import urllib.parse
import pygeoip
from simplemediawiki import MediaWiki
import validators
from xml.sax.saxutils import escape
import traceback
sys.path.append('../lib')
from PyWhoisAPI import *
from apiary import ApiaryBot, FourHundred, FiveHundred, NoJSON
class BumbleBee(ApiaryBot):
"""Bot that collects statistics for sites."""
def edit_page(self, datapage, template_block):
if datapage[:6] == 'Error/':
if self.args.verbose >= 1:
print(repr(traceback.format_stack()))
return
socket.setdefaulttimeout(30)
# We need an edit token
#c = self.apiary_wiki.call({'action': 'query', 'titles': 'Foo', 'prop': 'info', 'intoken': 'edit'})
c = self.apiary_wiki.call({'action': 'query', 'meta': 'tokens'})
self.edit_token = c['query']['tokens']['csrftoken']
if self.args.verbose >= 1:
print("Edit token: %s" % self.edit_token)
c = self.apiary_wiki.call({'action': 'edit', 'title': datapage, 'text': template_block, 'token': self.edit_token, 'bot': 'true'})
if self.args.verbose >= 4:
print(template_block)
print(datapage)
if self.args.verbose >= 3:
print("Edited page, result below")
print(c)
def parse_version(self, t, site):
ver = {}
t = str(t)
if self.args.verbose >= 3:
print("Getting version details for %s" % t)
try:
# Do we have a x.y.z
y = re.findall(r'^(?:(\d+)\.)?(?:(\d+)\.?)?(?:(\d+)\.?)?(?:(\d+)\.?)?', t)
if y:
if len(y[0][0]) > 0:
ver['major'] = y[0][0]
if len(y[0][1]) > 0:
ver['minor'] = y[0][1]
if len(y[0][2]) > 0:
ver['bugfix'] = y[0][2]
if not ver.get('major', None):
# Do we have a YYYY-MM-DD
if re.match(r'\d{4}-\d{2}-\d{2}', t):
y = re.findall(r'(\d{4})-(\d{2})-(\d{2})', t)
(ver['major'], ver['minor'], ver['bugfix']) = y[0]
if not ver.get('major', None):
# Do we have a YYYYMMDD
if re.match(r'\d{4}\d{2}\d{2}', t):
y = re.findall(r'(\d{4})(\d{2})(\d{2})', t)
(ver['major'], ver['minor'], ver['bugfix']) = y[0]
# Do we have a flag
y = re.match(r'.*(alpha|beta|wmf|CLDR|MLEB|stable).*', t)
if y:
ver['flag'] = y.group(1)
except Exception as e:
self.record_error(
site=site,
log_message="Exception %s while parsing version string %s" % (e, t),
log_type='warn',
log_bot='Bumble Bee'
)
if self.args.verbose >= 2:
print("Version details: ", ver)
return ver
def record_statistics(self, site, method):
if method == 'API':
# Go out and get the statistic information
data_url = site['Has API URL'] + '?action=query&meta=siteinfo&siprop=statistics&format=json'
if self.args.verbose >= 2:
print("Pulling statistics info from %s." % data_url)
(status, data, duration) = self.pull_json(site, data_url)
elif method == 'Statistics':
status = False
# Get stats the old fashioned way
data_url = site['Has statistics URL']
if "?" in data_url:
data_url += "&action=raw"
else:
data_url += "?action=raw"
if self.args.verbose >= 2:
print("Pulling statistics from %s." % data_url)
# This is terrible and should be put into pull_json somewhow
socket.setdefaulttimeout(15)
# Get CSV data via raw Statistics call
f, duration = self.make_request(site,data_url)
if f is not None:
# Create an object that is the same as that returned by the API
if f.info().get('Content-Encoding') == 'gzip':
gz = gzip.GzipFile(fileobj=f)
ret_string = gz.read().decode('utf-8')
else:
ret_string = f.read().decode('utf-8')
ret_string = ret_string.strip()
if re.match(r'(\w+=\d+)\;?', ret_string):
# The return value looks as we expected
status = True
data = {}
data['query'] = {}
data['query']['statistics'] = {}
items = ret_string.split(";")
for item in items:
(name, value) = item.split("=")
try:
# Convert the value to an int, if this fails we skip it
value = int(value)
if name == "total":
name = "pages"
if name == "good":
name = "articles"
if self.args.verbose >= 3:
print("Transforming %s to %s" % (name, value))
data['query']['statistics'][name] = value
except:
if self.args.verbose >= 3:
print("Illegal value '%s' for %s." % (value, name))
else:
self.record_error(
site=site,
log_message="Unexpected response to statistics call",
log_type='error',
log_severity='normal',
log_bot='Bumble Bee',
log_url=data_url
)
if self.args.verbose >= 3:
print("Result from statistics was not formatted as expected:\n%s" % ret_string)
ret_value = True
if status:
# Record the new data into the DB
if self.args.verbose >= 2:
print("JSON: %s" % data)
print("Duration: %s" % duration)
if 'query' in data:
# Record the data received to the database
sql_command = """
INSERT INTO statistics
(website_id, capture_date, response_timer, articles, jobs, users, admins, edits, activeusers, images, pages, views)
VALUES
(%s, '%s', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
data = data['query']['statistics']
if 'articles' in data:
articles = "%s" % data['articles']
else:
articles = 'null'
if 'jobs' in data:
jobs = "%s" % data['jobs']
else:
jobs = 'null'
if 'users' in data:
users = "%s" % data['users']
else:
users = 'null'
if 'admins' in data:
admins = "%s" % data['admins']
else:
admins = 'null'
if 'edits' in data:
edits = "%s" % data['edits']
else:
edits = 'null'
if 'activeusers' in data:
if data['activeusers'] < 0:
data['activeusers'] = 0
activeusers = "%s" % data['activeusers']
else:
activeusers = 'null'
if 'images' in data:
images = "%s" % data['images']
else:
images = 'null'
if 'pages' in data:
pages = "%s" % data['pages']
else:
pages = 'null'
if 'views' in data:
views = "%s" % data['views']
else:
views = 'null'
sql_command = sql_command % (
site['Has ID'],
self.sqlutcnow(),
duration,
articles,
jobs,
users,
admins,
edits,
activeusers,
images,
pages,
views)
self.runSql(sql_command)
self.stats['statistics'] += 1
else:
self.record_error(
site=site,
log_message='Statistics returned unexpected JSON.',
log_type='info',
log_severity='normal',
log_bot='Bumble Bee',
log_url=data_url
)
ret_value = False
else:
if self.args.verbose >= 3:
print("Did not receive valid data from %s" % (data_url))
ret_value = False
# Update the status table that we did our work!
self.update_status(site, 'statistics')
return ret_value
def record_smwusage(self, site):
# Get the extended SMW usage
data_url = site['Has API URL'] + '?action=parse&page=Project:SMWExtInfo&prop=text&disablepp=1&format=json'
if self.args.verbose >= 2:
print("Pulling semantic usage info from %s." % data_url)
(status, data, duration) = self.pull_json(site, data_url)
if status:
try:
data_block = data['parse']['text']['*']
data_soup = BeautifulSoup.BeautifulSoup(data_block)
json_block = data_soup.find("div", {"id": "wikiapiary-semantic-usage-data"})
json_data = simplejson.loads(json_block.text)
except Exception as e:
self.record_error(
site=site,
log_message="Semantic usage failed parsing: %s" % e,
log_type='info',
log_severity='normal',
log_bot='Bumble Bee',
log_url=data_url
)
return False
sql = """INSERT INTO smwextinfo
(website_id, capture_date, response_timer,
query_count, query_pages, query_concepts, query_pageslarge,
size1, size2, size3, size4, size5, size6, size7, size8, size9, size10plus,
format_broadtable, format_csv, format_category, format_count, format_dsv, format_debug, format_embedded,
format_feed, format_json, format_list, format_ol, format_rdf, format_table, format_template, format_ul)
VALUES
( %d, '%s', %f,
%d, %d, %d, %d,
%d, %d, %d, %d, %d, %d, %d, %d, %d, %d,
%d, %d, %d, %d, %d, %d, %d,
%d, %d, %d, %d, %d, %d, %d, %d)"""
sql_command = sql % (
site['Has ID'],
self.sqlutcnow(),
duration,
json_data['smwqueries']['count'],
json_data['smwqueries']['pages'],
json_data['smwqueries']['concepts'],
json_data['smwqueries']['pageslarge'],
json_data['smwquerysizes']['size-1'],
json_data['smwquerysizes']['size-2'],
json_data['smwquerysizes']['size-3'],
json_data['smwquerysizes']['size-4'],
json_data['smwquerysizes']['size-5'],
json_data['smwquerysizes']['size-6'],
json_data['smwquerysizes']['size-7'],
json_data['smwquerysizes']['size-8'],
json_data['smwquerysizes']['size-9'],
json_data['smwquerysizes']['size-10plus'],
json_data['smwformats']['broadtable'],
json_data['smwformats']['csv'],
json_data['smwformats']['category'],
json_data['smwformats']['count'],
json_data['smwformats']['dsv'],
json_data['smwformats']['debug'],
json_data['smwformats']['embedded'],
json_data['smwformats']['feed'],
json_data['smwformats']['json'],
json_data['smwformats']['list'],
json_data['smwformats']['ol'],
json_data['smwformats']['rdf'],
json_data['smwformats']['table'],
json_data['smwformats']['template'],
json_data['smwformats']['ul'])
self.runSql(sql_command)
self.stats['smwusage'] += 1
else:
if self.args.verbose >= 3:
print("Did not receive valid data from %s" % (data_url))
return False
def record_smwinfo(self, site):
# Go out and get the statistic information
data_url = site['Has API URL'] + ''.join([
'?action=smwinfo',
'&info=propcount%7Cusedpropcount%7Cdeclaredpropcount%7Cproppagecount%7Cquerycount%7Cquerysize%7Cconceptcount%7Csubobjectcount%7Cerrorcount',
'&format=json'])
if self.args.verbose >= 2:
print("Pulling SMW info from %s." % data_url)
(status, data, duration) = self.pull_json(site, data_url)
ret_value = True
if status:
# Record the new data into the DB
if self.args.verbose >= 2:
print("JSON: %s" % data)
print("Duration: %s" % duration)
if 'info' in data:
# Record the data received to the database
sql_command = """
INSERT INTO smwinfo (website_id, capture_date, response_timer,
propcount, proppagecount, usedpropcount,
declaredpropcount, querycount, querysize,
conceptcount, subobjectcount, errorcount) VALUES
(%d, '%s', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
if 'propcount' in data['info']:
propcount = data['info']['propcount']
else:
propcount = 'null'
if 'proppagecount' in data['info']:
proppagecount = data['info']['proppagecount']
else:
proppagecount = 'null'
if 'usedpropcount' in data['info']:
usedpropcount = data['info']['usedpropcount']
else:
usedpropcount = 'null'
if 'declaredpropcount' in data['info']:
declaredpropcount = data['info']['declaredpropcount']
else:
declaredpropcount = 'null'
if 'errorcount' in data['info']:
errorcount = data['info']['errorcount']
else:
errorcount = 'null'
# Catch additional results returned in SMW 1.9
if 'querycount' in data['info']:
querycount = data['info']['querycount']
else:
querycount = 'null'
if 'querysize' in data['info']:
querysize = data['info']['querysize']
else:
querysize = 'null'
if 'conceptcount' in data['info']:
conceptcount = data['info']['conceptcount']
else:
conceptcount = 'null'
if 'subobjectcount' in data['info']:
subobjectcount = data['info']['subobjectcount']
else:
subobjectcount = 'null'
# Before inserting insure we have good data
if propcount != 'null':
sql_command = sql_command % (
site['Has ID'],
self.sqlutcnow(),
duration,
propcount,
proppagecount,
usedpropcount,
declaredpropcount,
querycount,
querysize,
conceptcount,
subobjectcount,
errorcount)
self.runSql(sql_command)
self.stats['smwinfo'] += 1
else:
self.record_error(
site=site,
log_message='SMWInfo returned unexpected JSON.',
log_type='info',
log_severity='normal',
log_bot='Bumble Bee',
log_url=data_url
)
ret_value = False
else:
if self.args.verbose >= 3:
print("Did not receive valid data from %s" % (data_url))
ret_value = False
# Update the status table that we did our work! TODO:
# Commenting out. There is a bug that if this updates at the
# same time as the previous one there is no change to the row,
# and my check for rows_affected in update_status will not
# work as intended. Going to assume that smwinfo slaves off of
# regular statistics.
#self.update_status(site, 'statistics')
return ret_value
def ProcessMultiprops(self, site_id, key, value):
# Here we deal with properties that change frequently and we care about all of them.
# For example, dbversion in a wiki farm will often have multiple values
# and we will get different values each time, rotating between a set.
# This function will take the value and return a more complex data structure.
# First update the timestamp for seeing the current name/value
cur = self.apiary_db.cursor()
temp_sql = "UPDATE apiary_multiprops SET last_date=\'%s\', occurrences = occurrences + 1 WHERE website_id = %d AND t_name = \'%s\' AND t_value = \'%s\'" % (
self.sqlutcnow(),
site_id,
key,
value)
if self.args.verbose >= 3:
print("SQL Debug: %s" % temp_sql)
cur.execute(temp_sql)
rows_returned = cur.rowcount
# No rows returned, we need to create this value
if rows_returned == 0:
temp_sql = "INSERT apiary_multiprops (website_id, t_name, t_value, first_date, last_date, occurrences) VALUES (%d, \'%s\', \'%s\', \'%s\', \'%s\', %d)" % (
site_id,
key,
value,
self.sqlutcnow(),
self.sqlutcnow(),
1)
if self.args.verbose >= 3:
print("SQL Debug: %s" % temp_sql)
cur.execute(temp_sql)
# Now build the return value
multivalue = ""
temp_sql = "SELECT t_value, last_date, occurrences FROM apiary_multiprops WHERE website_id = %d AND last_date > \'%s\' AND t_name = \'%s\' ORDER BY occurrences DESC" % (
site_id,
'2013-04-26 18:23:01',
key)
cur.execute(temp_sql)
rows = cur.fetchall()
for row in rows:
if len(multivalue) > 0:
multivalue += ","
multivalue += "%s" % row[0]
return multivalue
def build_general_template(self, site_id, x):
# Some keys we do not want to store in WikiApiary
ignore_keys = ['time', 'fallback', 'fallback8bitEncoding']
# These items are only included if they are true
boolean_keys = ['imagewhitelistenabled', 'rtl', 'writeapi', 'misermode']
# Some keys we turn into more readable names for using inside of WikiApiary
key_names = {
'dbtype': 'Database type',
'dbversion': 'Database version',
'generator': 'MediaWiki version',
'lang': 'Language',
'timezone': 'Timezone',
'timeoffset': 'Timeoffset',
'sitename': 'Sitename',
'rights': 'Rights',
'phpversion': 'PHP Version',
'phpsapi': 'PHP Server API',
'wikiid': 'Wiki ID'
}
template_block = "<noinclude>{{General subpage}}</noinclude><includeonly>"
template_block += "{{General siteinfo\n"
# Loop through all the keys provided and create the template block
for key in x:
# Make sure we aren't ignoring this key
if key not in ignore_keys:
# If we have a name for this key use that
name = key_names.get(key, key)
value = "%s" % x[key]
# These items are only included if they are true
if key in boolean_keys:
value = True
# For some items we may need to do some preprocessing
else:
# A pipe will break the template, try HTML entity encoding it instead
value = value.replace('|', '{!}')
# Double right brackets also will break the template
value = value.replace('}}', '} }')
if key == 'lang':
# Make sure language is all lowercase, and try to standardize structure
value = value.lower().replace('_', '-').replace(' ', '-')
if key == 'sitename':
# Sometimes a : appears in sitename and messes up semantics
# Try using an HTML entity instead
value = value.replace(':', ':')
if key == 'dbversion':
value = self.ProcessMultiprops(site_id, key, value)
template_block += "|%s=%s\n" % (name, value)
template_block += "}}\n</includeonly>\n"
return template_block
def BuildMaxmindTemplate(self, hostname):
template_block = "<noinclude>{{Maxmind subpage}}</noinclude><includeonly>"
template_block += "{{Maxmind\n"
gi = pygeoip.GeoIP('../vendor/GeoLiteCity.dat')
data = gi.record_by_name(hostname)
for val in data:
template_block += "|%s=%s\n" % (val, data[val])
template_block += "}}\n</includeonly>\n"
return template_block
def record_general(self, site):
data_url = site['Has API URL'] + "?action=query&meta=siteinfo&siprop=general&format=json"
if self.args.verbose >= 2:
print("Pulling general info from %s." % data_url)
(success, data, duration) = self.pull_json(site, data_url)
ret_value = True
if success:
# Successfully pulled data
if 'query' in data:
datapage = "%s/General" % site['pagename']
template_block = self.build_general_template(site['Has ID'], data['query']['general'])
self.edit_page(datapage, template_block)
self.stats['general'] += 1
else:
self.record_error(
site=site,
log_message='Returned unexpected JSON when general info.',
log_type='info',
log_severity='normal',
log_bot='Bumble Bee',
log_url=data_url
)
ret_value = False
# Update the status table that we did our work! It doesn't matter if this was an error.
self.update_status(site, 'general')
return ret_value
def record_whois(self, site):
# Now that we successfully got the data, we can make a quick query to get the server info
hostname = urllib.parse.urlparse(site['Has API URL']).hostname
try:
addr = socket.gethostbyname(hostname)
except:
return None
datapage = "%s/Whois" % site['pagename']
template_block = "<noinclude>{{Whois subpage}}</noinclude><includeonly>"
template_block += "{{Whois\n"
template_block += "|HTTP server=%s\n" % ('')
try:
template_block += "|IP address=%s\n" % (
self.ProcessMultiprops(site['Has ID'], 'addr', addr)
)
except:
pass
try:
reverse_host = socket.gethostbyaddr(addr)[0]
template_block += "|Reverse lookup=%s\n" % (
self.ProcessMultiprops(site['Has ID'], 'reverse_host', reverse_host)
)
except:
if self.args.verbose >= 3:
traceback.print_exc()
pass
# Now lets get the netblock information
try:
whois = Whois()
related = whois.getNetworkRegistrationRelatedToIP(addr, format='json')
netblock_owner = related['net']['orgRef']['@name']
netblock_owner_handle = related['net']['orgRef']['@handle']
template_block += "|Netblock organization=%s\n" % (netblock_owner)
template_block += "|Netblock organization handle=%s\n" % netblock_owner_handle
except:
pass
template_block += "}}\n</includeonly>\n"
self.edit_page(datapage, template_block)
self.stats['whois'] += 1
def record_maxmind(self, site):
# Create the Maxmind page to put all the geographic data in
datapage = "%s/Maxmind" % site['pagename']
hostname = urllib.parse.urlparse(site['Has API URL']).hostname
template_block = self.BuildMaxmindTemplate(hostname)
self.edit_page(datapage, template_block)
self.stats['maxmind'] += 1
def build_extensions_template(self, ext_obj, site):
h = html.parser.HTMLParser()
# Some keys we do not want to store in WikiApiary
ignore_keys = ['descriptionmsg']
# Some keys we turn into more readable names for using inside of WikiApiary
key_names = {
'author': 'Extension author',
'name': 'Extension name',
'version': 'Extension version',
'type': 'Extension type',
'url': 'Extension URL'
}
template_block = "<noinclude>{{Extensions subpage}}</noinclude><includeonly>"
for x in ext_obj:
if 'name' in x:
template_block += "{{Extension in use\n"
for item in x:
if item not in ignore_keys:
name = key_names.get(item, item)
value = x[item]
if item == 'name':
# Sometimes people make the name of the
# extension a hyperlink using wikitext
# links and this makes things ugly. So,
# let's detect that if present.
if re.match(r'\[(http[^\s]+)\s+([^\]]+)\]', value):
(possible_url, value) = re.findall(r'\[(http[^\s]+)\s+([^\]]+)\]', value)[0]
# If a URL was given in the name, and
# not given as a formal part of the
# extension definition (yes, this
# happens) then add this to the
# template it is up to the template to
# decide what to do with this
template_block += "|URL Embedded in name=%s" % possible_url
value = self.filter_illegal_chars(value)
# Before unescaping 'regular' unicode characters, first deal with spaces
# because they cause problems when converted to unicode non-breaking spaces
value = value.replace(' ', ' ').replace(' ', ' ')
value = value.replace('&160;', ' ')
value = html.unescape(value)
if value.strip() == '':
template_block += '|Remote error=No name provided for extension.\n'
if item == 'version':
# Breakdown the version information for more detailed analysis
ver_details = self.parse_version(value, site)
if 'major' in ver_details:
template_block += "|Extension version major=%s\n" % ver_details['major']
if 'minor' in ver_details:
template_block += "|Extension version minor=%s\n" % ver_details['minor']
if 'bugfix' in ver_details:
template_block += "|Extension version bugfix=%s\n" % ver_details['bugfix']
if 'flag' in ver_details:
template_block += "|Extension version flag=%s\n" % ver_details['flag']
if item == 'author':
# Authors can have a lot of junk in them, wikitext and such.
# We'll try to clean that up.
# Wikilinks with names
# "[[Foobar | Foo Bar]]"
value = re.sub(r'\[\[.*\|(.*)\]\]', r'\1', value)
# Simple Wikilinks
value = re.sub(r'\[\[(.*)\]\]', r'\1', value)
# Hyperlinks as wikiext
# "[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]"
value = re.sub(r'\[\S+\s+([^\]]+)\]', r'\1', value)
# Misc text
value = re.sub(r'\sand\s', r', ', value)
value = re.sub(r'\.\.\.', r'', value)
value = re.sub(r' ', r' ', value)
# Lastly, there could be HTML encoded stuff in these
value = self.filter_illegal_chars(value)
value = html.unescape(value)
if item == 'url':
# Seems some people really really love protocol agnostic URL's
# We detect them and add a generic http: protocol to them
if value.strip() != '':
if re.match(r'^\/\/', value):
value = 'http:' + value
if validators.url(value) != True:
template_block += '|Remote error=\'%s\'' % value
template_block += 'is not a valid URL.\n'
value = ""
template_block += "|%s=%s\n" % (name, value)
template_block += "}}\n"
template_block += "</includeonly>"
return template_block
def build_libraries_template(self, ext_obj):
template_block = "<noinclude>{{Libraries subpage}}</noinclude><includeonly>"
libraries_sorted = sorted(ext_obj, key=operator.itemgetter('name'))
for x in libraries_sorted:
if 'name' in x:
# Start the template instance
template_block += "{{Library in use\n"
for item in x:
# Loop through all the items in the library data and build the instance
if item == 'name':
(vendor, package) = x[item].split('/')
template_block += "|vendor=%s\n" % vendor
template_block += "|package=%s\n" % package
else:
template_block += "|%s=%s\n" % (item, x[item])
# Now end the template instance
template_block += "}}\n"
template_block += "</includeonly>"
return template_block
def record_libraries(self, site):
data_url = site['Has API URL'] + "?action=query&meta=siteinfo&siprop=libraries&format=json"
if self.args.verbose >= 2:
print("Pulling extensions and libraries from %s." % data_url)
(success, data, duration) = self.pull_json(site, data_url)
ret_value = True
if success:
# Successfully pulled data
if 'query' in data:
if 'libraries' in data['query']:
datapage = "%s/Libraries" % site['pagename']
template_block = self.build_libraries_template(data['query']['libraries'])
self.edit_page(datapage, template_block)
self.stats['libraries'] += 1
else:
self.record_error(
site=site,
log_message='Returned unexpected JSON when requesting library data.',
log_type='warn',
log_severity='normal',
log_bot='Bumble Bee',
log_url=data_url
)
ret_value = False
return ret_value
def record_extensions(self, site):
data_url = site['Has API URL'] + "?action=query&meta=siteinfo&siprop=extensions&format=json"
if self.args.verbose >= 2:
print("Pulling extensions and libraries from %s." % data_url)
(success, data, duration) = self.pull_json(site, data_url)
ret_value = True
if success:
# Successfully pulled data
if 'query' in data:
if 'extensions' in data['query']:
datapage = "%s/Extensions" % site['pagename']
template_block = self.build_extensions_template(data['query']['extensions'], site)
self.edit_page(datapage, template_block)
self.stats['extensions'] += 1
else:
self.record_error(
site=site,
log_message='Returned unexpected JSON when requesting extension data.',
log_type='warn',
log_severity='normal',
log_bot='Bumble Bee',
log_url=data_url
)
ret_value = False
return ret_value
def build_skins_template(self, ext_obj):
# Some keys we do not want to store in WikiApiary
ignore_keys = []
# Some keys we turn into more readable names for using inside of WikiApiary
key_names = {
'*': 'Skin name',
'code': 'Skin code',
'default': 'Default skin',
'unusable': 'Skipped skin'
}
template_block = "<noinclude>{{Skins subpage}}</noinclude><includeonly>"
# Skins are returned in random order so we need to sort them before
# making the template, otherwise we generate a lot of edits
# that are just different ordering
skins_sorted = sorted(ext_obj, key=operator.itemgetter('*'))
for x in skins_sorted:
if '*' in x:
# Start the template instance
include_skin = True
skin = "{{Skin in use\n"
for item in x:
# Loop through all the items in the skin data and build the instance
if item not in ignore_keys:
name = key_names.get(item, item)
value = x[item]
if item == 'code':
if value in ['fallback', 'apioutput']:
include_skin = False
break
if item == '*':
value = self.filter_illegal_chars(value)
if item == 'default':
# This parameter won't appear unless it is true
value = True
if item == 'unusable':
# This paramter won't appear unless it is true
value = True
if item == 'name' and value == '':
skin += '|Remote error=No name provided for skin.\n'
skin += "|%s=%s\n" % (name, value)
# Now end the template instance
if include_skin:
template_block += skin + "}}\n"
template_block += "</includeonly>"
return template_block
def record_skins(self, site):
data_url = site['Has API URL'] + "?action=query&meta=siteinfo&siprop=skins"
data_url += "&siinlanguagecode=en&format=json"
if self.args.verbose >= 2:
print("Pulling skin info from %s." % data_url)
(success, data, duration) = self.pull_json(site, data_url)
ret_value = True
if success:
# Successfully pulled data
if 'query' in data:
datapage = "%s/Skins" % site['pagename']
template_block = self.build_skins_template(data['query']['skins'])
self.edit_page(datapage, template_block)
self.stats['skins'] += 1
else:
self.record_error(
site=site,
log_message='Returned unexpected JSON when requesting skin data.',
log_type='info',
log_severity='normal',
log_bot='Bumble Bee',
log_url=data_url
)
ret_value = False
return ret_value
def build_interwikimap_template(self, ext_obj):
# Some keys we do not want to store in WikiApiary
ignore_keys = []
# Some keys we turn into more readable names for using inside of WikiApiary
key_names = {}
template_block = "<noinclude>{{Interwikimap subpage}}</noinclude><includeonly>"
# Skins are returned in random order so we need to sort them before
# making the template, otherwise we generate a lot of edits
# that are just different ordering
interwiki_sorted = sorted(ext_obj, key=operator.itemgetter('prefix'))
for x in interwiki_sorted:
if 'prefix' in x:
# Start the template instance
template_block += "{{Interwiki link\n"
for item in x:
# Loop through all the items in the interwiki data and build the instance
if item not in ignore_keys:
name = key_names.get(item, item)
value = x[item]
if item in ['local', 'protorel', 'trans']:
# This parameter won't appear unless it is true
value = True
if item == 'url':
value = value.strip()
if not validators.url(value):
template_block += '|Remote error=\'%s\' is not a valid URL.\n' % value
value = ""
template_block += "|%s=%s\n" % (name, value)
# Now end the template instance
template_block += "}}\n"
template_block += "</includeonly>"
return template_block
def record_interwikimap(self, site):
data_url = site['Has API URL'] + "?action=query&meta=siteinfo&siprop=interwikimap"
data_url += "&siinlanguagecode=en&format=json"
if self.args.verbose >= 2:
print("Pulling interwikimap info from %s." % data_url)
(success, data, duration) = self.pull_json(site, data_url)
ret_value = True
if success:
# Successfully pulled data
if 'query' in data:
datapage = "%s/Interwikimap" % site['pagename']
template_block = self.build_interwikimap_template(data['query']['interwikimap'])
self.edit_page( datapage, template_block )
self.stats['interwikimap'] += 1
else:
self.record_error(
site=site,
log_message='Returned unexpected JSON when requesting interwikimap data.',
log_type='info',
log_severity='normal',
log_bot='Bumble Bee',
log_url=data_url
)
ret_value = False
return ret_value
def build_namespaces_template(self, ext_obj):
# Some keys we do not want to store in WikiApiary
ignore_keys = []
# Some keys we turn into more readable names for using inside of WikiApiary
key_names = {
'*': 'Namespace'
}
template_block = "<noinclude>{{Namespaces subpage}}</noinclude><includeonly>"