-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.py
180 lines (165 loc) · 6.29 KB
/
delete.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
# -*- coding: utf-8 -*-
"""
This script can be used to delete and undelete pages en masse.
Of course, you will need an admin account on the relevant wiki.
Syntax: python delete.py [-category categoryName]
Command line options:
-page: Delete specified page
-cat: Delete all pages in the given category.
-nosubcats: Don't delete pages in the subcategories.
-links: Delete all pages linked from a given page.
-file: Delete all pages listed in a text file.
-ref: Delete all pages referring from a given page.
-images: Delete all images used on a given page.
-always: Don't prompt to delete pages, just do it.
-summary: Supply a custom edit summary.
-undelete: Actually undelete pages instead of deleting.
Obviously makes sense only with -page and -file.
Examples:
Delete everything in the category "To delete" without prompting.
python delete.py -cat:"To delete" -always
"""
__version__ = '$Id$'
#
# Distributed under the terms of the MIT license.
#
import wikipedia as pywikibot
from pywikibot import i18n
import config, catlib
import pagegenerators
class DeletionRobot:
""" This robot allows deletion of pages en masse. """
def __init__(self, generator, summary, always = False, undelete=True):
""" Arguments:
* generator - A page generator.
* always - Delete without prompting?
"""
self.generator = generator
self.summary = summary
self.always = always
self.undelete = undelete
def run(self):
""" Starts the robot's action. """
#Loop through everything in the page generator and delete it.
for page in self.generator:
pywikibot.output(u'Processing page %s' % page.title())
if self.undelete:
page.undelete(self.summary, throttle = True)
else:
page.delete(self.summary, not self.always, throttle = True)
def main():
pageName = ''
singlePage = ''
summary = ''
always = False
doSinglePage = False
doCategory = False
deleteSubcategories = True
doRef = False
doLinks = False
doImages = False
undelete = False
fileName = ''
gen = None
# read command line parameters
for arg in pywikibot.handleArgs():
if arg == '-always':
always = True
elif arg.startswith('-file'):
if len(arg) == len('-file'):
fileName = pywikibot.input(
u'Enter name of file to delete pages from:')
else:
fileName = arg[len('-file:'):]
elif arg.startswith('-summary'):
if len(arg) == len('-summary'):
summary = pywikibot.input(u'Enter a reason for the deletion:')
else:
summary = arg[len('-summary:'):]
elif arg.startswith('-cat'):
doCategory = True
if len(arg) == len('-cat'):
pageName = pywikibot.input(
u'Enter the category to delete from:')
else:
pageName = arg[len('-cat:'):]
elif arg.startswith('-nosubcats'):
deleteSubcategories = False
elif arg.startswith('-links'):
doLinks = True
if len(arg) == len('-links'):
pageName = pywikibot.input(u'Enter the page to delete from:')
else:
pageName = arg[len('-links:'):]
elif arg.startswith('-ref'):
doRef = True
if len(arg) == len('-ref'):
pageName = pywikibot.input(u'Enter the page to delete from:')
else:
pageName = arg[len('-ref:'):]
elif arg.startswith('-page'):
doSinglePage = True
if len(arg) == len('-page'):
pageName = pywikibot.input(u'Enter the page to delete:')
else:
pageName = arg[len('-page:'):]
elif arg.startswith('-images'):
doImages = True
if len(arg) == len('-images'):
pageName = pywikibot.input(
u'Enter the page with the images to delete:')
else:
pageName = arg[len('-images'):]
elif arg.startswith('-undelete'):
undelete = True
mysite = pywikibot.getSite()
if doSinglePage:
if not summary:
summary = pywikibot.input(u'Enter a reason for the %sdeletion:'
% ['', 'un'][undelete])
page = pywikibot.Page(mysite, pageName)
gen = iter([page])
elif doCategory:
if not summary:
summary = i18n.twtranslate(mysite, 'delete-from-category',
{'page': pageName})
ns = mysite.category_namespace()
categoryPage = catlib.Category(mysite, ns + ':' + pageName)
gen = pagegenerators.CategorizedPageGenerator(
categoryPage, recurse=deleteSubcategories)
elif doLinks:
if not summary:
summary = i18n.twtranslate(mysite, 'delete-linked-pages',
{'page': pageName})
pywikibot.setAction(summary)
linksPage = pywikibot.Page(mysite, pageName)
gen = pagegenerators.LinkedPageGenerator(linksPage)
elif doRef:
if not summary:
summary = i18n.twtranslate(mysite, 'delete-referring-pages',
{'page': pageName})
refPage = pywikibot.Page(mysite, pageName)
gen = pagegenerators.ReferringPageGenerator(refPage)
elif fileName:
if not summary:
summary = i18n.twtranslate(mysite, 'delete-from-file')
gen = pagegenerators.TextfilePageGenerator(fileName)
elif doImages:
if not summary:
summary = i18n.twtranslate(mysite, 'delete-images',
{'page': pageName})
page = pywikibot.Page(mysite, pageName)
gen = pagegenerators.ImagesPageGenerator(page)
if gen:
pywikibot.setAction(summary)
# We are just deleting pages, so we have no need of using a preloading
# page generator to actually get the text of those pages.
bot = DeletionRobot(gen, summary, always, undelete)
bot.run()
else:
pywikibot.showHelp(u'delete')
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()