Updated
[andrew-tools.git] / afcfix.py
bloba2f2ad4725f6ba78f8a2e3629d57530277d5c26f
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; tagging."
69 % page.aslink())
70 page.put(u'{{db-g6}}', comment = "Robot tagging for G6")
71 else:
72 return text
73 return None
75 def run(self):
76 for page in self.generator:
77 self.treat(page)
79 def treat(self, page):
81 # Checking for speedies
82 pgtext = self.load(page)
84 wiki_e = re.search("{{db-", pgtext)
86 try:
87 wiki_e.group(0)
88 pywikibot.output(u'Page %s not saved, has tag.' % page.aslink())
89 except AttributeError:
90 tmp = ""
91 if page.namespace() == 0:
92 # Bingo, misplaced.
93 #tmp = page.titleWithoutNamespace()
94 #tmp = tmp.replace(tmp.split("/")[0]+"/","")
95 tg = page.title()
96 tmp = "Wikipedia talk:Articles for creation/" + tg
97 if page.title() == "Wikipedia:Files for upload":
98 self.dry = True
99 self.doNotMove = True
101 if page.title() == "Wikipedia:Articles for creation/Redirects":
102 self.dry = True
103 self.doNotMove = True
104 else:
105 self.dry = self.perdry
106 self.doNotMove = False
107 else:
108 return
110 if self.dry:
111 if tmp:
112 if not self.doNotMove:
113 print "I would have moved " + tg + " to: " + tmp
114 else:
115 if page.title() == "Wikipedia:Files for upload":
116 return
117 if page.title() == "Wikipedia:Articles for creation/Redirects":
118 return
119 print "Moving " + page.aslink() + " to " + tmp
120 pg2 = pywikibot.Page(pywikibot.getSite(), page.title())
121 page.move(tmp, self.summary, throttle=True)
122 pg2.put(u'{{db-r2}}', comment = "Robot tagging for R2")
124 def main():
125 # This factory is responsible for processing command line arguments
126 # that are also used by other scripts and that determine on which pages
127 # to work on.
128 genFactory = pagegenerators.GeneratorFactory()
129 # The generator gives the pages that should be worked upon.
130 gen = None
131 # This temporary array is used to read the page title if one single
132 # page to work on is specified by the arguments.
133 pageTitleParts = []
134 # If dry is True, doesn't do any real changes, but only show
135 # what would have been changed.
136 dry = False
138 # Parse command line arguments
139 for arg in pywikibot.handleArgs():
140 if arg.startswith("-dry"):
141 dry = True
142 else:
143 # check if a standard argument like
144 # -start:XYZ or -ref:Asdf was given.
145 if not genFactory.handleArg(arg):
146 pageTitleParts.append(arg)
148 if pageTitleParts != []:
149 # We will only work on a single page.
150 pageTitle = ' '.join(pageTitleParts)
151 page = pywikibot.Page(pywikibot.getSite(), pageTitle)
152 gen = iter([page])
154 if not gen:
155 gen = genFactory.getCombinedGenerator()
156 if gen:
157 # The preloading generator is responsible for downloading multiple
158 # pages from the wiki simultaneously.
159 gen = pagegenerators.PreloadingGenerator(gen)
160 bot = BasicBot(gen, dry)
161 bot.run()
162 else:
163 pywikibot.showHelp()
165 if __name__ == "__main__":
166 try:
167 main()
168 finally:
169 pywikibot.stopme()