Index: trunk/pywikipedia/capitalize_redirects.py |
— | — | @@ -7,48 +7,33 @@ |
8 | 8 | |
9 | 9 | Command-line arguments: |
10 | 10 | |
11 | | - -file Work on all pages listed in a text file. |
12 | | - Argument can also be given as "-file:filename". |
| 11 | +¶ms; |
13 | 12 | |
14 | | - -cat Work on all pages which are in a specific category. |
15 | | - Argument can also be given as "-cat:categoryname". |
| 13 | +-always Don't prompt to make changes, just do them. |
16 | 14 | |
17 | | - -ref Work on all pages that link to a certain page. |
18 | | - Argument can also be given as "-ref:referredpagetitle". |
| 15 | +-titlecase creates a titlecased redirect version of a given page |
| 16 | + where all words of the title start with an uppercase |
| 17 | + character and the remaining characters are lowercase. |
19 | 18 | |
20 | | - -links Work on all pages that are linked from a certain page. |
21 | | - Argument can also be given as "-link:linkingpagetitle". |
22 | | - |
23 | | - -start Work on all pages on the home wiki, starting at the named |
24 | | - page. |
25 | | - |
26 | | - -page Work on a single page. |
27 | | - |
28 | | - -namespace Run over especific namespace. |
29 | | - Argument can also be given as "-namespace:100" or |
30 | | - "-namespace:Image". |
31 | | - |
32 | | - -always Don't prompt to make changes, just do them. |
33 | | - |
34 | | - -titlecase creates a titlecased redirect version of a given page |
35 | | - where all words of the title start with an uppercase |
36 | | - character and the remaining characters are lowercase |
37 | | - |
38 | 19 | Example: "python capitalize_redirects.py -start:B -always" |
39 | 20 | ''' |
40 | 21 | # |
41 | 22 | # (C) Yrithinnd |
42 | | -# (C) Pywikipedia bot team, 2007-2009 |
| 23 | +# (C) Pywikipedia bot team, 2007-2010 |
43 | 24 | # |
44 | 25 | # Class licensed under terms of the MIT license |
45 | 26 | # |
46 | 27 | __version__ = '$Id$' |
47 | 28 | # |
48 | 29 | |
49 | | -import time, sys |
| 30 | +import time, sys, re |
50 | 31 | import wikipedia as pywikibot |
51 | 32 | import pagegenerators, catlib |
52 | 33 | |
| 34 | +docuReplacements = { |
| 35 | + '¶ms;': pagegenerators.parameterHelp |
| 36 | +} |
| 37 | + |
53 | 38 | msg = { |
54 | 39 | 'ar': u'روبوت: إنشاء تحويلة إلى [[%s]]', |
55 | 40 | 'cs': u'Robot vytvořil přesměrování na [[%s]]', |
— | — | @@ -70,124 +55,65 @@ |
71 | 56 | self.acceptall = acceptall |
72 | 57 | self.titlecase = titlecase |
73 | 58 | self.site = pywikibot.getSite() |
| 59 | + self.done = False |
74 | 60 | |
75 | 61 | def run(self): |
76 | 62 | for page in self.generator: |
77 | | - if page.isRedirectPage(): |
78 | | - page = page.getRedirectTarget() |
79 | | - page_t = page.title() |
80 | | - # Show the title of the page we're working on. |
81 | | - # Highlight the title in purple. |
82 | | - pywikibot.output(u"\n>>> \03{lightpurple}%s\03{default} <<<" |
83 | | - % page_t) |
84 | | - if self.titlecase: |
85 | | - page_cap = pywikibot.Page(self.site, page_t.title()) |
86 | | - else: |
87 | | - page_cap = pywikibot.Page(self.site, page_t.capitalize()) |
88 | | - if not page_cap.exists(): |
89 | | - pywikibot.output(u'[[%s]] doesn\'t exist' % page_cap.title()) |
90 | | - if not self.acceptall: |
91 | | - choice = pywikibot.inputChoice( |
92 | | - u'Do you want to create a redirect?', |
93 | | - ['Yes', 'No', 'All', 'Quit'], ['y', 'N', 'a', 'q'], 'N') |
94 | | - if choice == 'a': |
95 | | - self.acceptall = True |
96 | | - elif choice == 'q': |
97 | | - break |
98 | | - if self.acceptall or choice == 'y': |
99 | | - comment = pywikibot.translate(self.site, msg) % page_t |
100 | | - try: |
101 | | - page_cap.put(u"#REDIRECT [[%s]]" % page_t, comment) |
102 | | - print |
103 | | - except: |
104 | | - pywikibot.output(u"An error occurred, skipping...") |
105 | | - continue |
106 | | - else: |
107 | | - pywikibot.output(u'%s already exists, skipping...\n' |
108 | | - % page_t.title()) |
| 63 | + if self.done: break |
| 64 | + if page.exists(): |
| 65 | + self.treat(page) |
109 | 66 | |
| 67 | + def treat(self, page): |
| 68 | + if page.isRedirectPage(): |
| 69 | + page = page.getRedirectTarget() |
| 70 | + page_t = page.title() |
| 71 | + # Show the title of the page we're working on. |
| 72 | + # Highlight the title in purple. |
| 73 | + pywikibot.output(u"\n>>> \03{lightpurple}%s\03{default} <<<" |
| 74 | + % page_t) |
| 75 | + if self.titlecase: |
| 76 | + page_cap = pywikibot.Page(self.site, page_t.title()) |
| 77 | + else: |
| 78 | + page_cap = pywikibot.Page(self.site, page_t.capitalize()) |
| 79 | + if page_cap.exists(): |
| 80 | + pywikibot.output(u'%s already exists, skipping...\n' |
| 81 | + % page_cap.aslink()) |
| 82 | + else: |
| 83 | + pywikibot.output(u'[[%s]] doesn\'t exist' % page_cap.title()) |
| 84 | + if not self.acceptall: |
| 85 | + choice = pywikibot.inputChoice( |
| 86 | + u'Do you want to create a redirect?', |
| 87 | + ['Yes', 'No', 'All', 'Quit'], ['y', 'N', 'a', 'q'], 'N') |
| 88 | + if choice == 'a': |
| 89 | + self.acceptall = True |
| 90 | + elif choice == 'q': |
| 91 | + self.done = True |
| 92 | + if self.acceptall or choice == 'y': |
| 93 | + comment = pywikibot.translate(self.site, msg) % page_t |
| 94 | + try: |
| 95 | + page_cap.put(u"#%s [[%s]]" % (self.site.redirect(True), page_t), comment) |
| 96 | + except: |
| 97 | + pywikibot.output(u"An error occurred, skipping...") |
| 98 | + |
110 | 99 | def main(): |
111 | | - gen = None |
112 | | - source = None |
113 | | - textfilename = None |
114 | | - categoryname = None |
115 | | - pageNames = [] |
116 | | - referredPageName = None |
| 100 | + genFactory = pagegenerators.GeneratorFactory() |
117 | 101 | acceptall = False |
118 | | - namespaces = [] |
119 | | - startpage = None |
120 | 102 | titlecase = False |
121 | 103 | |
122 | 104 | for arg in pywikibot.handleArgs(): |
123 | | - if arg.startswith('-file'): |
124 | | - if len(arg) == 5: |
125 | | - textfilename = pywikibot.input(u'Please enter the filename:') |
126 | | - else: |
127 | | - textfilename = arg[6:] |
128 | | - source = 'textfile' |
129 | | - elif arg.startswith('-cat'): |
130 | | - if len(arg) == 4: |
131 | | - categoryname = pywikibot.input( |
132 | | - u'Please enter the category name:') |
133 | | - else: |
134 | | - categoryname = arg[5:] |
135 | | - source = 'category' |
136 | | - elif arg.startswith('-page'): |
137 | | - if len(arg) == 5: |
138 | | - pageNames.append(pywikibot.input( |
139 | | - u'Which page do you want to change?')) |
140 | | - else: |
141 | | - pageNames.append(arg[6:]) |
142 | | - source = 'singlepage' |
143 | | - elif arg.startswith('-ref'): |
144 | | - if len(arg) == 4: |
145 | | - referredPageName = pywikibot.input( |
146 | | - u'Links to which page should be processed?') |
147 | | - else: |
148 | | - referredPageName = arg[5:] |
149 | | - source = 'ref' |
150 | | - elif arg.startswith('-start'): |
151 | | - if len(arg) == 6: |
152 | | - firstPageTitle = pywikibot.input( |
153 | | - u'Which page do you want to change?') |
154 | | - else: |
155 | | - firstPageTitle = arg[7:] |
156 | | - source = 'allpages' |
157 | | - elif arg == '-always': |
| 105 | + if arg == '-always': |
158 | 106 | acceptall = True |
159 | 107 | elif arg == '-titlecase': |
160 | 108 | titlecase = True |
161 | | - elif arg.startswith('-namespace:'): |
162 | | - try: |
163 | | - namespaces.append(int(arg[11:])) |
164 | | - except ValueError: |
165 | | - namespaces.append(arg[11:]) |
| 109 | + elif genFactory.handleArg(arg): |
| 110 | + pass |
166 | 111 | else: |
167 | | - commandline_replacements.append(arg) |
| 112 | + pywikibot.showHelp(u'capitalize_redirects') |
| 113 | + return |
168 | 114 | |
169 | | - if source == 'textfile': |
170 | | - gen = pagegenerators.TextfilePageGenerator(textfilename) |
171 | | - elif source == 'category': |
172 | | - cat = catlib.Category(pywikibot.getSite(), categoryname) |
173 | | - gen = pagegenerators.CategorizedPageGenerator(cat) |
174 | | - elif source == 'singlepage': |
175 | | - pages = [pywikibot.Page(pywikibot.getSite(), pageName) |
176 | | - for pageName in pageNames] |
177 | | - gen = iter(pages) |
178 | | - elif source == 'allpages': |
179 | | - namespace = pywikibot.Page(pywikibot.getSite(), |
180 | | - firstPageTitle).namespace() |
181 | | - gen = pagegenerators.AllpagesPageGenerator(firstPageTitle, namespace) |
182 | | - elif source == 'ref': |
183 | | - referredPage = pywikibot.Page(pywikibot.getSite(), referredPageName) |
184 | | - gen = pagegenerators.ReferringPageGenerator(referredPage) |
185 | | - elif source == None or len(commandline_replacements) not in [0, 2]: |
186 | | - pywikibot.showHelp(u'capitalize_redirects') |
187 | | - return |
188 | | - if namespaces != []: |
189 | | - gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces) |
190 | | - preloadingGen = pagegenerators.PreloadingGenerator(gen, pageNumber = 20) |
191 | | - bot = CapitalizeBot(preloadingGen, acceptall, titlecase) |
| 115 | + gen = genFactory.getCombinedGenerator() |
| 116 | + preloadingGen = pagegenerators.PreloadingGenerator(gen) |
| 117 | + bot = CapitalizeBot(preloadingGen, acceptall, titlecase, standard) |
192 | 118 | bot.run() |
193 | 119 | |
194 | 120 | if __name__ == "__main__": |