Trimmed code
[andrew-tools.git] / afcfix.py
blobe554c7f494ba36f2a3ca8785b82a26dda826257b
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
29 # This is required for the text that is shown when you run this script
30 # with the parameter -help.
31 docuReplacements = {
32 '&params;': pagegenerators.parameterHelp
35 class BasicBot:
36 # Edit summary message that should be used.
37 # NOTE: Put a good description here, and add translations, if possible!
38 msg = {
39 'en': u'Robot correcting misplaced [[WP:WPAFC|AFC]] submission',
42 def __init__(self, generator, dry):
43 """
44 Constructor. Parameters:
45 * generator - The page generator that determines on which pages
46 to work on.
47 * dry - If True, doesn't do any real changes, but only shows
48 what would have been changed.
49 """
50 self.generator = generator
51 self.dry = dry
52 self.perdry = dry
53 # Set the edit summary message
54 self.summary = pywikibot.translate(pywikibot.getSite(), self.msg)
56 def run(self):
57 for page in self.generator:
58 self.treat(page)
60 def treat(self, page):
61 namespace_pg = page.namespace()
62 tmp = ""
63 if namespace_pg != 5:
64 # Bingo, misplaced.
65 tmp = page.titleWithoutNamespace()
66 tmp = tmp.replace(tmp.split("/")[0]+"/","")
67 tg = page.title();
68 tmp = "Wikipedia talk:Articles for creation/" + tmp
70 if page.title() == "Wikipedia:Files for upload":
71 self.dry = True
72 self.doNotMove = True
73 elif page.title() == "Wikipedia:Articles for creation/Redirects":
74 self.dry = True
75 self.doNotMove = True
76 else:
77 self.dry = self.perdry
78 self.doNotMove = False
80 if not self.dry:
81 page.move(newPageTitle, self.summary, throttle=True)
82 else:
83 if tmp:
84 if not self.doNotMove:
85 print "I would have moved " + tg + " to: " + tmp
87 def main():
88 # This factory is responsible for processing command line arguments
89 # that are also used by other scripts and that determine on which pages
90 # to work on.
91 genFactory = pagegenerators.GeneratorFactory()
92 # The generator gives the pages that should be worked upon.
93 gen = None
94 # This temporary array is used to read the page title if one single
95 # page to work on is specified by the arguments.
96 pageTitleParts = []
97 # If dry is True, doesn't do any real changes, but only show
98 # what would have been changed.
99 dry = False
101 # Parse command line arguments
102 for arg in pywikibot.handleArgs():
103 if arg.startswith("-dry"):
104 dry = True
105 else:
106 # check if a standard argument like
107 # -start:XYZ or -ref:Asdf was given.
108 if not genFactory.handleArg(arg):
109 pageTitleParts.append(arg)
111 if pageTitleParts != []:
112 # We will only work on a single page.
113 pageTitle = ' '.join(pageTitleParts)
114 page = pywikibot.Page(pywikibot.getSite(), pageTitle)
115 gen = iter([page])
117 if not gen:
118 gen = genFactory.getCombinedGenerator()
119 if gen:
120 # The preloading generator is responsible for downloading multiple
121 # pages from the wiki simultaneously.
122 gen = pagegenerators.PreloadingGenerator(gen)
123 bot = BasicBot(gen, dry)
124 bot.run()
125 else:
126 pywikibot.showHelp()
128 if __name__ == "__main__":
129 try:
130 main()
131 finally:
132 pywikibot.stopme()