-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathtemplate.py
executable file
·314 lines (249 loc) · 11.7 KB
/
template.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
#!/usr/bin/env python3
r"""Very simple script to replace a template with another one.
It also converts the old MediaWiki boilerplate format to the new format.
Syntax:
python pwb.py template [-remove] [xml[:filename]] oldTemplate \
[newTemplate]
Specify the template on the command line. The program will pick up the
template page, and look for all pages using it. It will then
automatically loop over them, and replace the template.
Command line options:
-remove Remove every occurrence of the template from every article
-subst Resolves the template by putting its text directly into the
article. This is done by changing {{...}} or {{msg:...}}
into {{subst:...}}. If you want to use safesubst, you can
do -subst:safe. Substitution is not available inside
<ref>...</ref>, <gallery>...</gallery>, <poem>...</poem>
and <pagelist ... /> tags.
-assubst Replaces the first argument as old template with the second
argument as new template but substitutes it like ``-subst``
does. Using both options -remove and -subst in the same
command line has the same effect.
-xml retrieve information from a local dump
(https://dumps.wikimedia.org). If this argument isn't given,
info will be loaded from the maintenance page of the live
wiki. argument can also be given as "-xml:filename.xml".
-onlyuser: Only process pages edited by a given user
-skipuser: Only process pages not edited by a given user
-timestamp: (With -onlyuser or -skipuser). Only check for a user where
his edit is not older than the given timestamp. Timestamp
must be written in MediaWiki timestamp format which is
"%Y%m%d%H%M%S". If this parameter is missed, all edits are
checked but this is restricted to the last 100 edits.
-summary: [str] Lets you pick a custom edit summary. Use quotes if
edit summary contains spaces.
-always Don't bother asking to confirm any of the changes, Just Do It.
-addcat: Appends the given category to every page that is edited.
This is useful when a category is being broken out from a
template parameter or when templates are being upmerged but
more information must be preserved.
other: First argument is the old template name, second one is the
new name. If you want to address a template which has
spaces, put quotation marks around it, or use underscores.
Examples
--------
If you have a template called [[Template:Cities in Washington]] and want
to change it to [[Template:Cities in Washington state]], start:
python pwb.py template "Cities in Washington" "Cities in Washington state"
Move the page [[Template:Cities in Washington]] manually afterwards.
If you have a template called [[Template:test]] and want to substitute
it only on pages in the User: and User talk: namespaces, do:
python pwb.py template test -subst -namespace:2 -namespace:3
.. note:: -namespace: is a global Pywikibot parameter
This next example substitutes the template lived with a supplied edit
summary. It only performs substitutions in main article namespace and
doesn't prompt to start replacing. Note that -putthrottle: is a global
Pywikibot parameter:
python pwb.py template -putthrottle:30 -namespace:0 lived -subst -always \
-summary:"BOT: Substituting {{lived}}, see [[WP:SUBST]]."
This next example removes the templates {{cfr}}, {{cfru}}, and
{{cfr-speedy}} from five category pages as given:
python pwb.py template cfr cfru cfr-speedy -remove -always \
-page:"Category:Mountain monuments and memorials" \
-page:"Category:Indian family names" \
-page:"Category:Tennis tournaments in Belgium" \
-page:"Category:Tennis tournaments in Germany" \
-page:"Category:Episcopal cathedrals in the United States" \
-summary:"Removing Cfd templates from category pages that survived."
This next example substitutes templates test1, test2, and space test on
all user talk pages (namespace #3):
python pwb.py template test1 test2 "space test" -subst -ns:3 -always
"""
#
# (C) Pywikibot team, 2003-2024
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations
import re
import pywikibot
from pywikibot import i18n, pagegenerators, textlib
from pywikibot.backports import batched
from pywikibot.bot import SingleSiteBot
from pywikibot.pagegenerators import XMLDumpPageGenerator
from pywikibot.tools.itertools import filter_unique, roundrobin_generators
try:
from scripts.replace import ReplaceRobot as ReplaceBot
except ModuleNotFoundError:
from pywikibot_scripts.replace import ReplaceRobot as ReplaceBot
class TemplateRobot(ReplaceBot):
"""This bot will replace, remove or subst all occurrences of a template."""
update_options = {
'addcat': None,
'remove': False,
'subst': False,
'summary': '',
}
def __init__(self, generator, templates: dict, **kwargs) -> None:
"""Initializer.
:param generator: the pages to work on
:type generator: iterable
:param templates: a dictionary which maps old template names to
their replacements. If remove or subst is True, it maps the
names of the templates that should be removed/resolved to None.
"""
SingleSiteBot.__init__(self, **kwargs)
self.templates = templates
# get edit summary message if it's empty
if not self.opt.summary:
comma = self.site.mediawiki_message('comma-separator')
params = {'list': comma.join(self.templates.keys()),
'num': len(self.templates)}
if self.opt.remove:
tw_key = 'template-removing'
elif self.opt.subst:
tw_key = 'template-substituting'
else:
tw_key = 'template-changing'
self.opt.summary = i18n.twtranslate(self.site, tw_key, params)
replacements = []
exceptions = {}
builder = textlib.MultiTemplateMatchBuilder(self.site)
for old, new in self.templates.items():
template_regex = builder.pattern(old)
if self.opt.subst and self.opt.remove:
replacements.append((template_regex,
r'{{subst:%s\g<parameters>}}' % new))
exceptions['inside-tags'] = ['ref', 'gallery', 'poem',
'pagelist', ]
elif self.opt.subst:
replacements.append(
(template_regex, r'{{%s:%s\g<parameters>}}' %
(self.opt.subst, old)))
exceptions['inside-tags'] = ['ref', 'gallery', 'poem',
'pagelist', ]
elif self.opt.remove:
separate_line_regex = re.compile(
fr'^[*#:]* *{template_regex.pattern} *\n',
re.DOTALL | re.MULTILINE)
replacements.append((separate_line_regex, ''))
spaced_regex = re.compile(
fr' +{template_regex.pattern} +',
re.DOTALL)
replacements.append((spaced_regex, ' '))
replacements.append((template_regex, ''))
else:
template = pywikibot.Page(self.site, new, ns=10)
if not template.exists():
pywikibot.warning(f'Template "{new}" does not exist.')
if not pywikibot.input_yn('Do you want to proceed anyway?',
default=False,
automatic_quit=False):
continue
replacements.append((template_regex,
r'{{%s\g<parameters>}}' % new))
super().__init__(
generator, replacements, exceptions,
always=self.opt.always,
addcat=self.opt.addcat,
summary=self.opt.summary)
def main(*args: str) -> None:
"""Process command line arguments and invoke bot.
If args is an empty list, sys.argv is used.
:param args: command line arguments
"""
template_names = []
options = {}
# If xmlfilename is None, references will be loaded from the live wiki.
xmlfilename = None
user = None
skip = False
timestamp = None
# read command line parameters
local_args = pywikibot.handle_args(args)
site = pywikibot.Site()
gen_factory = pagegenerators.GeneratorFactory()
for arg in local_args:
if arg == '-remove':
options['remove'] = True
elif arg.startswith('-subst'):
options['subst'] = arg[len('-subst:'):] + 'subst'
assert options['subst'] in ('subst', 'safesubst')
elif arg == '-assubst':
options['subst'] = 'subst'
options['remove'] = True
elif arg == '-always':
options['always'] = True
elif arg.startswith('-xml'):
if len(arg) == 4:
xmlfilename = pywikibot.input(
"Please enter the XML dump's filename: ")
else:
xmlfilename = arg[5:]
elif arg.startswith('-addcat:'):
options['addcat'] = arg[len('-addcat:'):]
elif arg.startswith('-summary:'):
options['summary'] = arg[len('-summary:'):]
elif arg.startswith('-onlyuser:'):
user = arg[len('-onlyuser:'):]
elif arg.startswith('-skipuser:'):
user = arg[len('-skipuser:'):]
skip = True
elif arg.startswith('-timestamp:'):
timestamp = arg[len('-timestamp:'):]
elif not gen_factory.handle_arg(arg):
template_name = pywikibot.Page(site, arg, ns=10)
template_names.append(template_name.title(with_ns=False))
if not template_names:
pywikibot.bot.suggest_help(missing_parameters=['templates'])
return
if bool(options.get('subst', False)) ^ options.get('remove', False):
templates = dict.fromkeys(template_names)
else:
try:
templates = dict(batched(template_names, 2))
except ValueError:
pywikibot.info('Unless using solely -subst or -remove, you must '
'give an even number of template names.')
return
old_templates = [pywikibot.Page(site, template_name, ns=10)
for template_name in templates]
if xmlfilename:
builder = textlib.MultiTemplateMatchBuilder(site)
predicate = builder.search_any_predicate(old_templates)
gen = XMLDumpPageGenerator(
xmlfilename, site=site, text_predicate=predicate)
else:
gen = gen_factory.getCombinedGenerator()
if not gen:
gens = (
t.getReferences(only_template_inclusion=True,
follow_redirects=False)
for t in old_templates
)
gen = roundrobin_generators(*gens)
gen = filter_unique(gen, key=lambda p: '{}:{}:{}'.format(*p._cmpkey()))
if user:
gen = pagegenerators.UserEditFilterGenerator(gen, user, timestamp,
skip,
max_revision_depth=100,
show_filtered=True)
if not gen_factory.gens:
# make sure that proper namespace filtering etc. is handled
gen = gen_factory.getCombinedGenerator(gen)
if not gen_factory.nopreload:
gen = pagegenerators.PreloadingGenerator(gen)
bot = TemplateRobot(gen, templates, site=site, **options)
bot.run()
if __name__ == '__main__':
main()