Updated
[andrew-tools.git] / afcfix.py
blob6161826641ded47b9d5ac34870adf257de3db8df
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 """
4 This is not a complete bot; rather, it is a template from which simple
5 bots can be made. You can rename it to mybot.py, then edit it in
6 whatever way you want.
8 The following parameters are supported:
10 &params;
12 -dry If given, doesn't do any real changes, but only shows
13 what would have been changed.
15 All other parameters will be regarded as part of the title of a single page,
16 and the bot will only work on that single page.
17 """
19 # (C) Pywikipedia bot team, 2006-2010
21 # Distributed under the terms of the MIT license.
23 __version__ = '$Id: basic.py 8278 2010-06-11 17:01:24Z xqt $'
26 import wikipedia as pywikibot
27 import pagegenerators
28 import re
30 # This is required for the text that is shown when you run this script
31 # with the parameter -help.
32 docuReplacements = {
33 '&params;': pagegenerators.parameterHelp
36 class BasicBot:
37 # Edit summary message that should be used.
38 # NOTE: Put a good description here, and add translations, if possible!
39 msg = {
40 'en': u'Robot correcting misplaced [[WP:WPAFC|AFC]] submission',
43 def __init__(self, generator, dry):
44 """
45 Constructor. Parameters:
46 * generator - The page generator that determines on which pages
47 to work on.
48 * dry - If True, doesn't do any real changes, but only shows
49 what would have been changed.
50 """
51 self.generator = generator
52 self.dry = dry
53 self.perdry = dry
54 # Set the edit summary message
55 self.summary = pywikibot.translate(pywikibot.getSite(), self.msg)
57 def load(self, page):
58 """
59 Loads the given page, does some changes, and saves it.
60 """
61 try:
62 # Load the page
63 text = page.get()
64 except pywikibot.NoPage:
65 pywikibot.output(u"Page %s does not exist; skipping."
66 % page.aslink())
67 except pywikibot.IsRedirectPage:
68 pywikibot.output(u"Page %s is a redirect; skipping."
69 % page.aslink())
70 else:
71 return text
72 return None
74 def run(self):
75 for page in self.generator:
76 self.treat(page)
78 def treat(self, page):
80 # Checking for speedies
81 pgtext = self.load(page)
83 wiki_e = re.search("{{db-", pgtext)
85 try:
86 wiki_e.group(0)
87 pywikibot.output(u'Page %s not saved, has tag.' % page.aslink())
88 except AttributeError:
89 tmp = ""
90 if page.namespace() == 0:
91 # Bingo, misplaced.
92 #tmp = page.titleWithoutNamespace()
93 #tmp = tmp.replace(tmp.split("/")[0]+"/","")
94 tg = page.title()
95 tmp = "Wikipedia talk:Articles for creation/" + tg
96 if page.title() == "Wikipedia:Files for upload":
97 self.dry = True
98 self.doNotMove = True
100 if page.title() == "Wikipedia:Articles for creation/Redirects":
101 self.dry = True
102 self.doNotMove = True
103 else:
104 self.dry = self.perdry
105 self.doNotMove = False
106 else:
107 return
109 if self.dry:
110 if tmp:
111 if not self.doNotMove:
112 print "I would have moved " + tg + " to: " + tmp
113 else:
114 if page.title() == "Wikipedia:Files for upload":
115 return
116 if page.title() == "Wikipedia:Articles for creation/Redirects":
117 return
118 print "Moving " + page.aslink() + " to " + tmp
119 page.move(tmp, self.summary, throttle=True)
121 def main():
122 # This factory is responsible for processing command line arguments
123 # that are also used by other scripts and that determine on which pages
124 # to work on.
125 genFactory = pagegenerators.GeneratorFactory()
126 # The generator gives the pages that should be worked upon.
127 gen = None
128 # This temporary array is used to read the page title if one single
129 # page to work on is specified by the arguments.
130 pageTitleParts = []
131 # If dry is True, doesn't do any real changes, but only show
132 # what would have been changed.
133 dry = False
135 # Parse command line arguments
136 for arg in pywikibot.handleArgs():
137 if arg.startswith("-dry"):
138 dry = True
139 else:
140 # check if a standard argument like
141 # -start:XYZ or -ref:Asdf was given.
142 if not genFactory.handleArg(arg):
143 pageTitleParts.append(arg)
145 if pageTitleParts != []:
146 # We will only work on a single page.
147 pageTitle = ' '.join(pageTitleParts)
148 page = pywikibot.Page(pywikibot.getSite(), pageTitle)
149 gen = iter([page])
151 if not gen:
152 gen = genFactory.getCombinedGenerator()
153 if gen:
154 # The preloading generator is responsible for downloading multiple
155 # pages from the wiki simultaneously.
156 gen = pagegenerators.PreloadingGenerator(gen)
157 bot = BasicBot(gen, dry)
158 bot.run()
159 else:
160 pywikibot.showHelp()
162 if __name__ == "__main__":
163 try:
164 main()
165 finally:
166 pywikibot.stopme()