jenkins-bot has submitted this change and it was merged.
Change subject: Adding option of following redirects (bug 55310) ......................................................................
Adding option of following redirects (bug 55310)
Update from compat Ie84aa0292270f8a45c0e104a61e037b869f40850
- pep8 changes - code improvements - synchronize with compat
Change-Id: Ic862b8653c07e863b89de09c230de0dea31a50ee --- M scripts/category.py 1 file changed, 90 insertions(+), 50 deletions(-)
Approvals: Ladsgroup: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/category.py b/scripts/category.py index 73929cc..01ecd70 100755 --- a/scripts/category.py +++ b/scripts/category.py @@ -18,6 +18,7 @@ Options for "add" action: * -person - sort persons by their last name * -create - If a page doesn't exist, do not skip it, create it instead + * -redirect - Follow redirects
If action is "add", the following options are supported:
@@ -85,7 +86,7 @@ # (C) Cyde, 2006-2010 # (C) Anreas J Schwab, 2007 # (C) xqt, 2009-2012 -# (C) Pywikipedia team, 2008-2012 +# (C) Pywikipedia team, 2008-2013 # __version__ = '$Id$' # @@ -109,7 +110,8 @@ cfd_templates = { 'wikipedia': { 'en': [u'cfd', u'cfr', u'cfru', u'cfr-speedy', u'cfm', u'cfdu'], - 'fi': [u'roskaa', u'poistettava', u'korjattava/nimi', u'yhdistettäväLuokka'], + 'fi': [u'roskaa', u'poistettava', u'korjattava/nimi', + u'yhdistettäväLuokka'], 'he': [u'הצבעת מחיקה', u'למחוק'], 'nl': [u'categorieweg', u'catweg', u'wegcat', u'weg2'] }, @@ -137,7 +139,8 @@ % config.shortpath(filename)) databases = pickle.load(f) f.close() - # keys are categories, values are 2-tuples with lists as entries. + # keys are categories, values are 2-tuples with lists as + # entries. self.catContentDB = databases['catContentDB'] # like the above, but for supercategories self.superclassDB = databases['superclassDB'] @@ -168,8 +171,8 @@
def getArticles(self, cat): '''For a given category, return a list of Pages for all its articles. - Saves this list in a temporary database so that it won't be loaded from the - server next time it's required. + Saves this list in a temporary database so that it won't be loaded from + the server next time it's required.
''' # if we already know which articles exist here @@ -227,10 +230,11 @@ '''A robot to mass-add a category to a list of pages.'''
def __init__(self, generator, sort_by_last_name=False, create=False, - editSummary='', dry=False): + editSummary='', follow_redirects=False, dry=False): self.generator = generator self.sort = sort_by_last_name self.create = create + self.follow_redirects = follow_redirects self.site = pywikibot.getSite() self.always = False self.dry = dry @@ -272,7 +276,8 @@ self.newcatTitle = pywikibot.input( u'Category to add (do not give namespace):') if not self.site.nocapitalize: - self.newcatTitle = self.newcatTitle[:1].upper() + self.newcatTitle[1:] + self.newcatTitle = (self.newcatTitle[:1].upper() + + self.newcatTitle[1:]) if not self.editSummary: self.editSummary = i18n.twtranslate(self.site, 'category-adding', {'newcat': self.newcatTitle}) @@ -299,12 +304,14 @@ % page.title(asLink=True)) except pywikibot.IsRedirectPage, arg: redirTarget = pywikibot.Page(self.site, arg.args[0]) - pywikibot.output(u"WARNING: Page %s is a redirect to %s; skipping." - % (page.title(asLink=True), - redirTarget.title(asLink=True))) + if self.follow_redirects: + text = redirTarget.get() + else: + pywikibot.warning(u"Page %s is a redirect to %s; skipping." + % (page.title(asLink=True), + redirTarget.title(asLink=True))) else: return text - return None
def save(self, text, page, comment, minorEdit=True, botflag=True): # only save if something was changed @@ -342,8 +349,8 @@ % (page.title())) except pywikibot.SpamfilterError, error: pywikibot.output( - u'Cannot change %s because of spam blacklist entry %s' - % (page.title(), error.url)) + u'Cannot change %s because of spam blacklist entry ' + u'%s' % (page.title(), error.url)) else: return True return False @@ -485,7 +492,9 @@
class CategoryListifyRobot: '''Creates a list containing all of the members in a category.''' - def __init__(self, catTitle, listTitle, editSummary, overwrite=False, showImages=False, subCats=False, talkPages=False, recurse=False): + def __init__(self, catTitle, listTitle, editSummary, overwrite=False, + showImages=False, subCats=False, talkPages=False, + recurse=False): self.editSummary = editSummary self.overwrite = overwrite self.showImages = showImages @@ -501,25 +510,31 @@ if self.subCats: setOfArticles = setOfArticles.union(set(self.cat.subcategories())) if not self.editSummary: - self.editSummary = i18n.twtranslate(self.site, - 'category-listifying', - {'fromcat': self.cat.title(), - 'num': len(setOfArticles)}) + self.editSummary = i18n.twntranslate(self.site, + 'category-listifying', + {'fromcat': self.cat.title(), + 'num': len(setOfArticles)})
listString = "" for article in setOfArticles: - if (not article.isImage() or self.showImages) and not article.isCategory(): + if (not article.isImage() or + self.showImages) and not article.isCategory(): if self.talkPages and not article.isTalkPage(): - listString = listString + "*[[%s]] -- [[%s|talk]]\n" % (article.title(), article.toggleTalkPage().title()) + listString += "*[[%s]] -- [[%s|talk]]\n" \ + % (article.title(), + article.toggleTalkPage().title()) else: - listString = listString + "*[[%s]]\n" % article.title() + listString += "*[[%s]]\n" % article.title() else: if self.talkPages and not article.isTalkPage(): - listString = listString + "*[[:%s]] -- [[%s|talk]]\n" % (article.title(), article.toggleTalkPage().title()) + listString += "*[[:%s]] -- [[%s|talk]]\n" \ + % (article.title(), + article.toggleTalkPage().title()) else: - listString = listString + "*[[:%s]]\n" % article.title() + listString += "*[[:%s]]\n" % article.title() if self.list.exists() and not self.overwrite: - pywikibot.output(u'Page %s already exists, aborting.' % self.list.title()) + pywikibot.output(u'Page %s already exists, aborting.' + % self.list.title()) else: self.list.put(listString, comment=self.editSummary)
@@ -552,21 +567,28 @@ def run(self): articles = set(self.cat.articles()) if len(articles) == 0: - pywikibot.output(u'There are no articles in category %s' % self.cat.title()) + pywikibot.output(u'There are no articles in category %s' + % self.cat.title()) else: for article in articles: - if not self.titleRegex or re.search(self.titleRegex, article.title()): - catlib.change_category(article, self.cat, None, comment=self.editSummary, inPlace=self.inPlace) + if not self.titleRegex or re.search(self.titleRegex, + article.title()): + catlib.change_category(article, self.cat, None, + comment=self.editSummary, + inPlace=self.inPlace) if self.pagesonly: return
# Also removes the category tag from subcategories' pages subcategories = set(self.cat.subcategories()) if len(subcategories) == 0: - pywikibot.output(u'There are no subcategories in category %s' % self.cat.title()) + pywikibot.output(u'There are no subcategories in category %s' + % self.cat.title()) else: for subcategory in subcategories: - catlib.change_category(subcategory, self.cat, None, comment=self.editSummary, inPlace=self.inPlace) + catlib.change_category(subcategory, self.cat, None, + comment=self.editSummary, + inPlace=self.inPlace) # Deletes the category page if self.cat.exists() and self.cat.isEmptyCategory(): if self.useSummaryForDeletion and self.editSummary: @@ -577,7 +599,9 @@ try: self.cat.delete(reason, not self.batchMode) except pywikibot.NoUsername: - pywikibot.output(u'You're not setup sysop info, category will not delete.' % self.cat.site()) + pywikibot.output( + u'You're not setup sysop info, category will not delete.' + % self.cat.site()) return if (talkPage.exists()): talkPage.delete(reason=reason, prompt=not self.batchMode) @@ -593,8 +617,8 @@ in the category. It will ask you to type the number of the appropriate replacement, and perform the change robotically.
- If you don't want to move the article to a subcategory or supercategory, but to - another category, you can use the 'j' (jump) command. + If you don't want to move the article to a subcategory or supercategory, but + to another category, you can use the 'j' (jump) command.
Typing 's' will leave the complete page unchanged.
@@ -611,8 +635,9 @@ self.catTitle = catTitle self.catDB = catDB self.site = pywikibot.getSite() - self.editSummary = i18n.twtranslate(self.site, 'category-changing')\ - % {'oldcat': self.catTitle, 'newcat': u''} + self.editSummary = i18n.twtranslate(self.site, 'category-changing', + {'oldcat': self.catTitle, + 'newcat': u''})
def move_to_category(self, article, original_cat, current_cat): ''' @@ -625,7 +650,10 @@ pywikibot.output(u'') # Show the title of the page where the link was found. # Highlight the title in purple. - pywikibot.output(u'Treating page \03{lightpurple}%s\03{default}, currently in \03{lightpurple}%s\03{default}' % (article.title(), current_cat.title())) + pywikibot.output( + u'Treating page \03{lightpurple}%s\03{default}, ' + u'currently in \03{lightpurple}%s\03{default}' + % (article.title(), current_cat.title()))
# Determine a reasonable amount of context to print try: @@ -682,14 +710,16 @@ catlib.change_category(article, original_cat, current_cat, comment=self.editSummary) flag = True elif choice in ['j', 'J']: - newCatTitle = pywikibot.input(u'Please enter the category the article should be moved to:') + newCatTitle = pywikibot.input(u'Please enter the category the ' + u'article should be moved to:') newCat = catlib.Category(pywikibot.Link('Category:' + newCatTitle)) # recurse into chosen category self.move_to_category(article, original_cat, newCat) flag = True elif choice in ['r', 'R']: # remove the category tag - catlib.change_category(article, original_cat, None, comment=self.editSummary) + catlib.change_category(article, original_cat, None, + comment=self.editSummary) flag = True elif choice == '?': contextLength += 500 @@ -710,7 +740,8 @@ except ValueError: # user pressed an unknown command. Prompt him again. continue - self.move_to_category(article, original_cat, supercatlist[choice]) + self.move_to_category(article, original_cat, + supercatlist[choice]) flag = True else: try: @@ -762,8 +793,8 @@
def treeview(self, cat, currentDepth=0, parent=None): ''' - Returns a multi-line string which contains a tree view of all subcategories - of cat, up to level maxDepth. Recursively calls itself. + Returns a multi-line string which contains a tree view of all + subcategories of cat, up to level maxDepth. Recursively calls itself.
Parameters: * cat - the Category of the node we're currently opening @@ -774,8 +805,8 @@ result = u'#' * currentDepth result += '[[:%s|%s]]' % (cat.title(), cat.title().split(':', 1)[1]) result += ' (%d)' % len(self.catDB.getArticles(cat)) - # We will remove an element of this array, but will need the original array - # later, so we create a shallow copy with [:] + # We will remove an element of this array, but will need the original + # array later, so we create a shallow copy with [:] supercats = self.catDB.getSupercats(cat)[:] # Find out which other cats are supercats of the current cat try: @@ -786,10 +817,14 @@ supercat_names = [] for i in range(len(supercats)): # create a list of wiki links to the supercategories - supercat_names.append('[[:%s|%s]]' % (supercats[i].title(), supercats[i].title().split(':', 1)[1])) - # print this list, separated with commas, using translations given in also_in_cats + supercat_names.append('[[:%s|%s]]' + % (supercats[i].title(), + supercats[i].title().split(':', 1)[1])) + # print this list, separated with commas, using translations + # given in also_in_cats result += ' ' + i18n.twtranslate(self.site, 'category-also-in', - {'alsocat': ', '.join(supercat_names)}) + {'alsocat': ', '.join( + supercat_names)}) result += '\n' if currentDepth < self.maxDepth: for subcat in self.catDB.getSubcats(cat): @@ -852,6 +887,7 @@ sort_by_last_name = False restore = False create_pages = False + follow_redirects = False for arg in pywikibot.handleArgs(*args): if arg == 'add': action = 'add' @@ -901,6 +937,8 @@ pagesonly = True elif arg == '-create': create_pages = True + elif arg == '-redirect': + follow_redirects = True else: genFactory.handleArg(arg) pywikibot.Site().login() @@ -913,11 +951,12 @@ # The preloading generator is responsible for downloading multiple # pages from the wiki simultaneously. gen = pagegenerators.PreloadingGenerator(gen) - bot = AddCategory(gen, sort_by_last_name, create_pages, editSummary) + bot = AddCategory(gen, sort_by_last_name, create_pages, editSummary, follow_redirects) bot.run() elif action == 'remove': if not fromGiven: - oldCatTitle = pywikibot.input(u'Please enter the name of the category that should be removed:') + oldCatTitle = pywikibot.input(u'Please enter the name of the ' + u'category that should be removed:') bot = CategoryRemoveRobot(oldCatTitle, batchMode, editSummary, useSummaryForDeletion, inPlace=inPlace, pagesonly=pagesonly) @@ -930,7 +969,8 @@ newCatTitle = pywikibot.input( u'Please enter the new name of the category:') bot = CategoryMoveRobot(oldCatTitle, newCatTitle, batchMode, - editSummary, inPlace, titleRegex=titleRegex) + editSummary, inPlace, + titleRegex=titleRegex) bot.run() elif action == 'tidy': catTitle = pywikibot.input(u'Which category do you want to tidy up?') @@ -940,8 +980,8 @@ catTitle = pywikibot.input( u'For which category do you want to create a tree view?') filename = pywikibot.input( - u'Please enter the name of the file where the tree should be saved,\n' - u'or press enter to simply show the tree:') + u'Please enter the name of the file where the tree should be saved,' + u'\nor press enter to simply show the tree:') bot = CategoryTreeRobot(catTitle, catDB, filename) bot.run() elif action == 'listify':