forked from wikimedia/pywikibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovepages.py
executable file
·283 lines (225 loc) · 9.54 KB
/
movepages.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
#!/usr/bin/env python3
"""
This script can move pages.
These command line parameters can be used to specify which pages to work on:
¶ms;
Furthermore, the following command line parameters are supported:
-from and -to The page to move from and the page to move to.
-noredirect Leave no redirect behind.
-notalkpage Do not move this page's talk page (if it exists)
-nosubpages Do not move subpages
-prefix Move pages by adding a namespace prefix to the names of the
pages. (Will remove the old namespace prefix if any)
Argument can also be given as "-prefix:namespace:".
-always Don't prompt to make changes, just do them.
-skipredirects Skip redirect pages (Warning: increases server load)
-summary Prompt for a custom summary, bypassing the predefined message
texts. Argument can also be given as "-summary:XYZ".
-pairsfile Read pairs of file names from a file. The file must be in a
format [[frompage]] [[topage]] [[frompage]] [[topage]] ...
Argument can also be given as "-pairsfile:filename"
"""
#
# (C) Pywikibot team, 2006-2022
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations
import re
from functools import partial
from itertools import zip_longest
import pywikibot
from pywikibot import i18n, pagegenerators
from pywikibot.bot import CurrentPageBot
from pywikibot.exceptions import PageRelatedError
# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {'¶ms;': pagegenerators.parameterHelp} # noqa: N816
class MovePagesBot(CurrentPageBot):
"""Page move bot.
.. versionchanged:: 7.2
`movesubpages` option was added
"""
update_options = {
'prefix': '',
'noredirect': False,
'movetalkpage': True,
'movesubpages': True,
'skipredirects': False,
'summary': '',
}
def __init__(self, **kwargs) -> None:
"""Initializer."""
super().__init__(**kwargs)
self.create_title = None
def move_one(self, page, new_page_tite) -> None:
"""Move one page to new_page_tite."""
msg = self.opt.summary
if not msg:
msg = i18n.twtranslate(page.site, 'movepages-moving')
pywikibot.info(f'Moving page {page} to [[{new_page_tite}]]')
try:
page.move(new_page_tite, reason=msg,
movetalk=self.opt.movetalkpage,
movesubpages=self.opt.movesubpages,
noredirect=self.opt.noredirect)
except PageRelatedError as e:
pywikibot.error(e)
def skip_page(self, page):
"""Treat only non-redirect pages if 'skipredirects' is set."""
if self.opt.skipredirects and page.isRedirectPage():
pywikibot.warning(
f'Page {page} on {page.site} is a redirect; skipping'
)
return True
return super().skip_page(page)
@staticmethod
def _select_action(page):
"""Manage interactive choices."""
def create_new_title_change(page, new_title=None):
"""Change helper function."""
if new_title is None:
return pywikibot.input('New page name:')
return new_title
def create_new_title_append(start, end, page, namespace=None):
"""Append helper function."""
page_title = page.title(with_ns=False)
new_page_tite = f'{start}{page_title}{end}'
if namespace is not None:
new_page_tite = f'{namespace}:{new_page_tite}'
return new_page_tite
def create_new_title_regex(regex, replacement, page, namespace=None):
"""Replace helper function."""
page_title = page.title(with_ns=False)
new_page_title = regex.sub(replacement, page_title)
if namespace is not None:
new_page_title = f'{namespace}:{new_page_title}'
return new_page_title
def manage_namespace(page):
"""Manage interactive choices for namespace prefix."""
namespace = page.site.namespace(page.namespace())
q = pywikibot.input_yn('Do you want to remove the '
f'namespace prefix "{namespace}:"?',
automatic_quit=False)
return None if q else namespace
choice = pywikibot.input_choice('What do you want to do?',
[('change page name', 'c'),
('append to page name', 'a'),
('use a regular expression', 'r'),
('next page', 'n')])
if choice == 'c':
handler = partial(create_new_title_change,
new_title=create_new_title_change(page))
choices = [('yes', 'y'), ('no', 'n')]
elif choice == 'a':
start = pywikibot.input('Append this to the start:')
end = pywikibot.input('Append this to the end:')
ns = manage_namespace(page)
handler = partial(create_new_title_append,
start, end, namespace=ns)
choices = [('yes', 'y'), ('no', 'n'), ('all', 'a')]
elif choice == 'r':
search_pattern = pywikibot.input('Enter the search pattern:')
regex = re.compile(search_pattern)
replacement = pywikibot.input('Enter the replace pattern:')
ns = manage_namespace(page)
handler = partial(create_new_title_regex,
regex, replacement, namespace=ns)
choices = [('yes', 'y'), ('no', 'n'), ('all', 'a')]
else:
handler = None
choices = []
return handler, choices
def _title_creator(self, page, prefix):
"""Create function to generate new title."""
def create_new_title_prefix(prefix, page):
"""Replace prefix helper function."""
page_title = page.title(with_ns=False)
return f'{prefix}{page_title}'
if prefix:
handler = partial(create_new_title_prefix, prefix)
choices = [('yes', 'y'), ('no', 'n'), ('all', 'a')]
else:
handler, choices = self._select_action(page)
return choices, handler
def treat_page(self) -> None:
"""Treat a single page."""
page = self.current_page
if not self.opt.always:
choices, create_title = self._title_creator(page, self.opt.prefix)
if create_title is None:
return
self.create_title = create_title
choice = pywikibot.input_choice(
f'Change the page title to {create_title(page)!r}?',
choices)
if choice == 'y':
pass
elif choice == 'a':
self.opt.always = True
else:
return
new_page_title = self.create_title(page)
self.move_one(page, new_page_title)
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
"""
old_name = None
options = {}
from_to_pairs = []
# Process global args and prepare generator args parser
local_args = pywikibot.handle_args(args)
gen_factory = pagegenerators.GeneratorFactory()
local_args = gen_factory.handle_args(local_args)
for arg in local_args:
opt, _, value = arg.partition(':')
if not opt.startswith('-'):
continue
opt = opt[1:]
if opt == 'pairsfile':
filename = value or pywikibot.input(
'Enter the name of the file containing pairs:')
page_gen = [pagegenerators.TextIOPageGenerator(filename)] * 2
for old_page, new_page in zip_longest(*page_gen, fillvalue=None):
if new_page is None:
pywikibot.warning(
f'file {filename} contains odd number of links')
else:
from_to_pairs.append([old_page.title(), new_page.title()])
elif opt in ('always', 'noredirect', 'skipredirects'):
options[opt] = True
elif opt in ('notalkpage', 'nosubpages'):
options[opt.replace('no', 'move', 1)] = False
elif opt == 'from':
if old_name:
pywikibot.warning(f'-from:{old_name} without -to:')
old_name = value
elif opt == 'to':
if old_name:
from_to_pairs.append([old_name, value])
old_name = None
else:
pywikibot.warning(f'{arg} without -from')
elif opt == 'prefix':
options[opt] = value or pywikibot.input('Enter the prefix:')
elif opt == 'summary':
options[opt] = value or pywikibot.input('Enter the summary:')
if old_name:
pywikibot.warning(f'-from:{old_name} without -to:')
site = pywikibot.Site()
if not site.logged_in():
site.login()
bot = MovePagesBot(**options)
for old_title, new_title in from_to_pairs:
bot.move_one(pywikibot.Page(site, old_title), new_title)
gen = gen_factory.getCombinedGenerator(preload=True)
if gen:
bot = MovePagesBot(generator=gen, **options)
bot.run()
elif not from_to_pairs:
pywikibot.bot.suggest_help(missing_generator=True)
if __name__ == '__main__':
main()