Revision: 7908 Author: xqt Date: 2010-02-04 07:12:46 +0000 (Thu, 04 Feb 2010)
Log Message: ----------- * Use pagegenerators for getting pages to processed * check whether source page exists * use localized translation for #REDIRECT
Modified Paths: -------------- trunk/pywikipedia/capitalize_redirects.py
Modified: trunk/pywikipedia/capitalize_redirects.py =================================================================== --- trunk/pywikipedia/capitalize_redirects.py 2010-02-03 13:13:43 UTC (rev 7907) +++ trunk/pywikipedia/capitalize_redirects.py 2010-02-04 07:12:46 UTC (rev 7908) @@ -7,48 +7,33 @@
Command-line arguments:
- -file Work on all pages listed in a text file. - Argument can also be given as "-file:filename". +¶ms;
- -cat Work on all pages which are in a specific category. - Argument can also be given as "-cat:categoryname". +-always Don't prompt to make changes, just do them.
- -ref Work on all pages that link to a certain page. - Argument can also be given as "-ref:referredpagetitle". +-titlecase creates a titlecased redirect version of a given page + where all words of the title start with an uppercase + character and the remaining characters are lowercase.
- -links Work on all pages that are linked from a certain page. - Argument can also be given as "-link:linkingpagetitle". - - -start Work on all pages on the home wiki, starting at the named - page. - - -page Work on a single page. - - -namespace Run over especific namespace. - Argument can also be given as "-namespace:100" or - "-namespace:Image". - - -always Don't prompt to make changes, just do them. - - -titlecase creates a titlecased redirect version of a given page - where all words of the title start with an uppercase - character and the remaining characters are lowercase - Example: "python capitalize_redirects.py -start:B -always" ''' # # (C) Yrithinnd -# (C) Pywikipedia bot team, 2007-2009 +# (C) Pywikipedia bot team, 2007-2010 # # Class licensed under terms of the MIT license # __version__ = '$Id$' #
-import time, sys +import time, sys, re import wikipedia as pywikibot import pagegenerators, catlib
+docuReplacements = { + '¶ms;': pagegenerators.parameterHelp +} + msg = { 'ar': u'روبوت: إنشاء تحويلة إلى [[%s]]', 'cs': u'Robot vytvořil přesměrování na [[%s]]', @@ -70,124 +55,65 @@ self.acceptall = acceptall self.titlecase = titlecase self.site = pywikibot.getSite() + self.done = False
def run(self): for page in self.generator: - if page.isRedirectPage(): - page = page.getRedirectTarget() - page_t = page.title() - # Show the title of the page we're working on. - # Highlight the title in purple. - pywikibot.output(u"\n>>> \03{lightpurple}%s\03{default} <<<" - % page_t) - if self.titlecase: - page_cap = pywikibot.Page(self.site, page_t.title()) - else: - page_cap = pywikibot.Page(self.site, page_t.capitalize()) - if not page_cap.exists(): - pywikibot.output(u'[[%s]] doesn't exist' % page_cap.title()) - if not self.acceptall: - choice = pywikibot.inputChoice( - u'Do you want to create a redirect?', - ['Yes', 'No', 'All', 'Quit'], ['y', 'N', 'a', 'q'], 'N') - if choice == 'a': - self.acceptall = True - elif choice == 'q': - break - if self.acceptall or choice == 'y': - comment = pywikibot.translate(self.site, msg) % page_t - try: - page_cap.put(u"#REDIRECT [[%s]]" % page_t, comment) - print - except: - pywikibot.output(u"An error occurred, skipping...") - continue - else: - pywikibot.output(u'%s already exists, skipping...\n' - % page_t.title()) + if self.done: break + if page.exists(): + self.treat(page)
+ def treat(self, page): + if page.isRedirectPage(): + page = page.getRedirectTarget() + page_t = page.title() + # Show the title of the page we're working on. + # Highlight the title in purple. + pywikibot.output(u"\n>>> \03{lightpurple}%s\03{default} <<<" + % page_t) + if self.titlecase: + page_cap = pywikibot.Page(self.site, page_t.title()) + else: + page_cap = pywikibot.Page(self.site, page_t.capitalize()) + if page_cap.exists(): + pywikibot.output(u'%s already exists, skipping...\n' + % page_cap.aslink()) + else: + pywikibot.output(u'[[%s]] doesn't exist' % page_cap.title()) + if not self.acceptall: + choice = pywikibot.inputChoice( + u'Do you want to create a redirect?', + ['Yes', 'No', 'All', 'Quit'], ['y', 'N', 'a', 'q'], 'N') + if choice == 'a': + self.acceptall = True + elif choice == 'q': + self.done = True + if self.acceptall or choice == 'y': + comment = pywikibot.translate(self.site, msg) % page_t + try: + page_cap.put(u"#%s [[%s]]" % (self.site.redirect(True), page_t), comment) + except: + pywikibot.output(u"An error occurred, skipping...") + def main(): - gen = None - source = None - textfilename = None - categoryname = None - pageNames = [] - referredPageName = None + genFactory = pagegenerators.GeneratorFactory() acceptall = False - namespaces = [] - startpage = None titlecase = False
for arg in pywikibot.handleArgs(): - if arg.startswith('-file'): - if len(arg) == 5: - textfilename = pywikibot.input(u'Please enter the filename:') - else: - textfilename = arg[6:] - source = 'textfile' - elif arg.startswith('-cat'): - if len(arg) == 4: - categoryname = pywikibot.input( - u'Please enter the category name:') - else: - categoryname = arg[5:] - source = 'category' - elif arg.startswith('-page'): - if len(arg) == 5: - pageNames.append(pywikibot.input( - u'Which page do you want to change?')) - else: - pageNames.append(arg[6:]) - source = 'singlepage' - elif arg.startswith('-ref'): - if len(arg) == 4: - referredPageName = pywikibot.input( - u'Links to which page should be processed?') - else: - referredPageName = arg[5:] - source = 'ref' - elif arg.startswith('-start'): - if len(arg) == 6: - firstPageTitle = pywikibot.input( - u'Which page do you want to change?') - else: - firstPageTitle = arg[7:] - source = 'allpages' - elif arg == '-always': + if arg == '-always': acceptall = True elif arg == '-titlecase': titlecase = True - elif arg.startswith('-namespace:'): - try: - namespaces.append(int(arg[11:])) - except ValueError: - namespaces.append(arg[11:]) + elif genFactory.handleArg(arg): + pass else: - commandline_replacements.append(arg) + pywikibot.showHelp(u'capitalize_redirects') + return
- if source == 'textfile': - gen = pagegenerators.TextfilePageGenerator(textfilename) - elif source == 'category': - cat = catlib.Category(pywikibot.getSite(), categoryname) - gen = pagegenerators.CategorizedPageGenerator(cat) - elif source == 'singlepage': - pages = [pywikibot.Page(pywikibot.getSite(), pageName) - for pageName in pageNames] - gen = iter(pages) - elif source == 'allpages': - namespace = pywikibot.Page(pywikibot.getSite(), - firstPageTitle).namespace() - gen = pagegenerators.AllpagesPageGenerator(firstPageTitle, namespace) - elif source == 'ref': - referredPage = pywikibot.Page(pywikibot.getSite(), referredPageName) - gen = pagegenerators.ReferringPageGenerator(referredPage) - elif source == None or len(commandline_replacements) not in [0, 2]: - pywikibot.showHelp(u'capitalize_redirects') - return - if namespaces != []: - gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces) - preloadingGen = pagegenerators.PreloadingGenerator(gen, pageNumber = 20) - bot = CapitalizeBot(preloadingGen, acceptall, titlecase) + gen = genFactory.getCombinedGenerator() + preloadingGen = pagegenerators.PreloadingGenerator(gen) + bot = CapitalizeBot(preloadingGen, acceptall, titlecase, standard) bot.run()
if __name__ == "__main__":