-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwpmed_society_scan.py
253 lines (226 loc) · 8.51 KB
/
wpmed_society_scan.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This script will display the list of pages transcluding a given list of
templates. It can also be used to simply count the number of pages (rather than
listing each individually).
Syntax: python templatecount.py command [arguments]
Command line options:
-count Counts the number of times each template (passed in as an
argument) is transcluded.
-list Gives the list of all of the pages transcluding the templates
(rather than just counting them).
-namespace: Filters the search to a given namespace. If this is specified
multiple times it will search all given namespaces
Examples:
Counts how many times {{ref}} and {{note}} are transcluded in articles.
python templatecount.py -count -namespace:0 ref note
Lists all the category pages that transclude {{cfd}} and {{cfdu}}.
python templatecount.py -list -namespace:14 cfd cfdu
"""
#
# (c) Pywikipedia bot team, 2006-2012
# (c) xqt, 2009-2013
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: fea8063ba1d66346408353cce5235ac936d5cbfa $'
import datetime
import wikipedia as pywikibot
import pagegenerators as pg
import re
templates = ['WPMED',
"WikiProject Medicine",
'CMedWikiProject',
'WPMEDICINE',
'WP Medicine',
'Wikiproject Medicines',
'Wikiproject Medicine',
'WPMedicine',
'WPMed',
'WikiProject Med'
]
templates = ['WPMED']
class TemplateCountRobot:
@staticmethod
def countTemplates(templates, namespaces):
templateDict = TemplateCountRobot.template_dict(templates, namespaces)
pywikibot.output(u'\nNumber of transclusions per template',
toStdout=True)
pywikibot.output(u'-' * 36, toStdout=True)
total = 0
for key in templateDict:
count = len(templateDict[key])
pywikibot.output(u'%-10s: %5d' % (key, count),
toStdout=True)
total += count
pywikibot.output(u'TOTAL : %5d' % total, toStdout=True)
pywikibot.output(u'Report generated on %s'
% datetime.datetime.utcnow().isoformat(),
toStdout=True)
@staticmethod
def listTemplates(templates, namespaces):
templateDict = TemplateCountRobot.template_dict(templates, namespaces)
pywikibot.output(u'\nList of pages transcluding templates:',
toStdout=True)
for key in templates:
pywikibot.output(u'* %s' % key)
pywikibot.output(u'-' * 36, toStdout=True)
total = 0
affected_pages = []
for key in templateDict:
for page in templateDict[key]:
return_eval = TemplateCountRobot.evaluate_qualified(page)
if return_eval != '':
affected_pages.append(return_eval)
#pywikibot.output(page.title(), toStdout=True)
total += 1
pywikibot.output(affected_pages)
pywikibot.output(u'Total page count: %d' % total)
pywikibot.output(u'Report generated on %s'
% datetime.datetime.utcnow().isoformat(),
toStdout=True)
@staticmethod
def evaluate_qualified(page):
page_text = page.get()
match = False
#Check Matching Cases
match_list = [
'{{WikiProject Biography',
'{{WPBIO|',
'{{WP Biography',
'{{WPbiography',
'{{Wikiproject Biography',
'{{WP Bio|',
'{{Bio|',
'{{WPBiography',
'{{WikiProject Biographies',
'{{WikiProject biography',
'{{Wpbio|',
'{{Companies',
'{{WPCO',
'{{WP Companies',
'{{WPCOMPANIES',
'{{WikiProject Corporations',
'{{WP Company',
'{{WPCOMPANY',
'{{WPCORP',
'{{WikiProject Corp',
'{{Wpco',
'{{WikiProject Companies',
'{{WikiProject Organizations',
'{{WikiProject Organizations CoopBanner',
'{{Organizations',
'{{WikiProject Organization',
'{{WikiProject Organizations',
'{{WPORG',
'{{WPOrganizations',
'{{WPOrganisations',
'{{WP Organizations',
'{{WP Organisation',
'{{WPORGANIZATIONS',
'{{BLP}}',
'{{Blpinfo}}',
'{{Blp}}',
]
for key in match_list:
if key in page_text:
match = True
if match == False:
if match == False and \
"{{WikiProjectBannerShell" in page_text and \
"living=yes" in page_text:
match = True
if match == False and \
"{{WikiProjectBannerShell" in page_text and \
"blp=yes" in page_text:
match = True
#Handle the case for "Charity" titled pages
if match == False and 'CHARITY' in page.title().upper():
match = True
#Check Exclusions
if match == False:
return ''
else:
#Found a match
print page.title()
matched_template = ''
for template in templates:
matched_template = re.findall('{{'+template+'\|?.*?}}',page_text)
if 1 == len(matched_template):
#Found the instigating template
break
if 'society=' not in matched_template[0]:
#Doesn't have the society parm, let's add it
strip_template = matched_template[0]
replace_template = strip_template[0:-2]+'|society=yes|society-imp=???}}'
replacement_text = page_text.replace(strip_template,replace_template)
comment = "[[Wikipedia:Bots/Requests for approval/HasteurBot 7|HasteurBot 7]] Adding Society Task Force Parm"
page.put(replacement_text,comment = comment,maxTries=5)
return page.title()
else:
return ''
@staticmethod
def template_dict(templates, namespaces):
gen = TemplateCountRobot.template_dict_generator(templates, namespaces)
templateDict = {}
for template, transcludingArray in gen:
templateDict[template] = transcludingArray
return templateDict
@staticmethod
def template_dict_generator(templates, namespaces):
mysite = pywikibot.getSite()
# The names of the templates are the keys, and lists of pages
# transcluding templates are the values.
mytpl = mysite.getNamespaceIndex(mysite.template_namespace())
for template in templates:
transcludingArray = []
gen = pg.ReferringPageGenerator(
pywikibot.Page(mysite, template, defaultNamespace=mytpl),
onlyTemplateInclusion=True)
if namespaces:
gen = pg.NamespaceFilterPageGenerator(gen, namespaces)
for page in gen:
transcludingArray.append(page)
yield template, transcludingArray
def main():
operation = None
argsList = []
namespaces = []
for arg in pywikibot.handleArgs():
if arg == '-count':
operation = "Count"
elif arg == '-list':
operation = "List"
elif arg.startswith('-namespace:'):
try:
namespaces.append(int(arg[len('-namespace:'):]))
except ValueError:
namespaces.append(arg[len('-namespace:'):])
else:
argsList.append(arg)
if not operation:
pywikibot.showHelp('templatecount')
else:
robot = TemplateCountRobot()
if not argsList:
argsList = templates
choice = ''
if 'reflist' in argsList:
pywikibot.output(
u'NOTE: it will take a long time to count "reflist".')
choice = pywikibot.inputChoice(
u'Proceed anyway?', ['yes', 'no', 'skip'], ['y', 'n', 's'], 'y')
if choice == 's':
argsList.remove('reflist')
if choice == 'n':
return
elif operation == "Count":
robot.countTemplates(argsList, namespaces)
elif operation == "List":
robot.listTemplates(argsList, namespaces)
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()