-
Notifications
You must be signed in to change notification settings - Fork 0
/
movepages.py
294 lines (268 loc) · 11.2 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
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
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.
-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".
-pairs 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 "-pairs:filename"
"""
#
# (C) Leonardo Gregianin, 2006
# (C) Andreas J. Schwab, 2007
# (C) Pywikipedia bot team, 2006-2013
#
# Distributed under the terms of the MIT license.
#
__version__='$Id$'
import sys, re
import wikipedia as pywikibot
from pywikibot import i18n
import pagegenerators
# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {
'¶ms;': pagegenerators.parameterHelp,
}
class MovePagesBot:
def __init__(self, generator, addprefix, noredirect, always, skipredirects,
summary):
self.generator = generator
self.addprefix = addprefix
self.leaveRedirect = not noredirect
self.always = always
self.skipredirects = skipredirects
self.summary = summary
def moveOne(self, page, newPageTitle):
try:
msg = self.summary
if not msg:
msg = i18n.twtranslate(pywikibot.getSite(), 'movepages-moving')
pywikibot.output(u'Moving page %s to [[%s]]'
% (page.title(asLink=True),
newPageTitle))
page.move(newPageTitle, msg, throttle=True,
leaveRedirect=self.leaveRedirect)
except pywikibot.NoPage:
pywikibot.output(u'Page %s does not exist!' % page.title())
except pywikibot.IsRedirectPage:
pywikibot.output(u'Page %s is a redirect; skipping.' % page.title())
except pywikibot.LockedPage:
pywikibot.output(u'Page %s is locked!' % page.title())
except pywikibot.PageNotSaved, e:
#target newPageTitle already exists
pywikibot.output(e.message)
def treat(self, page):
# Show the title of the page we're working on.
# Highlight the title in purple.
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
% page.title())
if self.skipredirects and page.isRedirectPage():
pywikibot.output(u'Page %s is a redirect; skipping.' % page.title())
return
pagetitle = page.title(withNamespace=False)
namesp = page.site().namespace(page.namespace())
if self.appendAll:
newPageTitle = (u'%s%s%s'
% (self.pagestart, pagetitle, self.pageend))
if not self.noNamespace and namesp:
newPageTitle = (u'%s:%s' % (namesp, newPageTitle))
elif self.regexAll:
newPageTitle = self.regex.sub(self.replacePattern, pagetitle)
if not self.noNamespace and namesp:
newPageTitle = (u'%s:%s' % (namesp, newPageTitle))
if self.addprefix:
newPageTitle = (u'%s%s' % (self.addprefix, pagetitle))
if self.addprefix or self.appendAll or self.regexAll:
if not self.always:
choice2 = pywikibot.inputChoice(
u'Change the page title to "%s"?' % newPageTitle,
['yes', 'no', 'all', 'quit'], ['y', 'n', 'a', 'q'])
if choice2 == 'y':
self.moveOne(page, newPageTitle)
elif choice2 == 'a':
self.always = True
self.moveOne(page, newPageTitle)
elif choice2 == 'q':
sys.exit()
elif choice2 == 'n':
pass
else:
self.treat(page)
else:
self.moveOne(page, newPageTitle)
else:
choice = pywikibot.inputChoice(u'What do you want to do?',
['change page name',
'append to page name',
'use a regular expression',
'next page', 'quit'],
['c', 'a', 'r', 'n', 'q'])
if choice == 'c':
newPageTitle = pywikibot.input(u'New page name:')
self.moveOne(page, newPageTitle)
elif choice == 'a':
self.pagestart = pywikibot.input(u'Append this to the start:')
self.pageend = pywikibot.input(u'Append this to the end:')
newPageTitle = (u'%s%s%s'
% (self.pagestart, pagetitle, self.pageend))
if namesp:
choice2 = pywikibot.inputChoice(
u'Do you want to remove the namespace prefix "%s:"?'
% namesp, ['yes', 'no'], ['y', 'n'])
if choice2 == 'y':
noNamespace = True
else:
newPageTitle = (u'%s:%s' % (namesp, newPageTitle))
choice2 = pywikibot.inputChoice(
u'Change the page title to "%s"?'
% newPageTitle, ['yes', 'no', 'all', 'quit'],
['y', 'n', 'a', 'q'])
if choice2 == 'y':
self.moveOne(page, newPageTitle)
elif choice2 == 'a':
self.appendAll = True
self.moveOne(page, newPageTitle)
elif choice2 == 'q':
sys.exit()
elif choice2 == 'n':
pass
else:
self.treat(page)
elif choice == 'r':
searchPattern = pywikibot.input(u'Enter the search pattern:')
self.replacePattern = pywikibot.input(
u'Enter the replace pattern:')
self.regex=re.compile(searchPattern)
if page.title() == page.title(withNamespace=False):
newPageTitle = self.regex.sub(self.replacePattern,
page.title())
else:
choice2 = pywikibot.inputChoice(
u'Do you want to remove the namespace prefix "%s:"?'
% namesp, ['yes', 'no'], ['y', 'n'])
if choice2 == 'y':
newPageTitle = self.regex.sub(
self.replacePattern, page.title(withNamespace=False))
noNamespace = True
else:
newPageTitle = self.regex.sub(self.replacePattern,
page.title())
choice2 = pywikibot.inputChoice(
u'Change the page title to "%s"?'
% newPageTitle, ['yes', 'no', 'all', 'quit'],
['y', 'n', 'a', 'q'])
if choice2 == 'y':
self.moveOne(page, newPageTitle)
elif choice2 == 'a':
self.regexAll = True
self.moveOne(page, newPageTitle)
elif choice2 == 'q':
sys.exit()
elif choice2 == 'n':
pass
else:
self.treat(page)
elif choice == 'n':
pass
elif choice == 'q':
sys.exit()
else:
self.treat(page)
def run(self):
self.appendAll = False
self.regexAll = False
self.noNamespace = False
for page in self.generator:
self.treat(page)
def main():
gen = None
prefix = None
oldName = None
newName = None
noredirect = False
always = False
skipredirects = False
summary = None
fromToPairs = []
# This factory is responsible for processing command line arguments
# that are also used by other scripts and that determine on which pages
# to work on.
genFactory = pagegenerators.GeneratorFactory()
for arg in pywikibot.handleArgs():
if arg.startswith('-pairs'):
if len(arg) == len('-pairs'):
filename = pywikibot.input(
u'Enter the name of the file containing pairs:')
else:
filename = arg[len('-pairs:'):]
oldName1 = None
for page in pagegenerators.TextfilePageGenerator(filename):
if oldName1:
fromToPairs.append([oldName1, page.title()])
oldName1 = None
else:
oldName1 = page.title()
if oldName1:
pywikibot.warning(
u'file %s contains odd number of links' % filename)
elif arg == '-noredirect':
noredirect = True
elif arg == '-always':
always = True
elif arg == '-skipredirects':
skipredirects = True
elif arg.startswith('-from:'):
if oldName:
pywikibot.warning(u'-from:%s without -to:' % oldName)
oldName = arg[len('-from:'):]
elif arg.startswith('-to:'):
if oldName:
fromToPairs.append([oldName, arg[len('-to:'):]])
oldName = None
else:
pywikibot.warning(u'%s without -from' % arg)
elif arg.startswith('-prefix'):
if len(arg) == len('-prefix'):
prefix = pywikibot.input(u'Enter the prefix:')
else:
prefix = arg[8:]
elif arg.startswith('-summary'):
if len(arg) == len('-summary'):
summary = pywikibot.input(u'Enter the summary:')
else:
summary = arg[9:]
else:
genFactory.handleArg(arg)
if oldName:
pywikibot.warning(u'-from:%s without -to:' % oldName)
for pair in fromToPairs:
page = pywikibot.Page(pywikibot.getSite(), pair[0])
bot = MovePagesBot(None, prefix, noredirect, always, skipredirects,
summary)
bot.moveOne(page, pair[1])
if not gen:
gen = genFactory.getCombinedGenerator()
if gen:
preloadingGen = pagegenerators.PreloadingGenerator(gen)
bot = MovePagesBot(preloadingGen, prefix, noredirect, always,
skipredirects, summary)
bot.run()
elif not fromToPairs:
pywikibot.showHelp()
if __name__ == '__main__':
try:
main()
finally:
pywikibot.stopme()