http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10268
Revision: 10268 Author: xqt Date: 2012-06-02 20:07:26 +0000 (Sat, 02 Jun 2012) Log Message: ----------- -pagesonly option for not to remove subcategories from its main, update from rewrite 10267
Modified Paths: -------------- trunk/pywikipedia/category.py
Modified: trunk/pywikipedia/category.py =================================================================== --- trunk/pywikipedia/category.py 2012-06-02 19:47:54 UTC (rev 10267) +++ trunk/pywikipedia/category.py 2012-06-02 20:07:26 UTC (rev 10268) @@ -57,6 +57,8 @@ * -inplace - Use this flag to change categories in place rather than rearranging them. * -recurse - Recurse through all subcategories of categories. + * -pagesonly - While removing pages from a category, keep the subpage links + and do not remove them * -match - Only work on pages whose titles match the given regex (for move and remove actions).
@@ -249,7 +251,7 @@
''' page_name = pagelink.title() - site = pagelink.site() + site = pagelink.site # regular expression that matches a name followed by a space and # disambiguation brackets. Group 1 is the name without the rest. bracketsR = re.compile('(.*) (.+?)') @@ -536,12 +538,16 @@
class CategoryRemoveRobot: '''Removes the category tag from all pages in a given category - and from the category pages of all subcategories, without prompting. - Does not remove category tags pointing at subcategories. + and if pagesonly parameter is False also from the category pages of all + subcategories, without prompting. If the category is empty, it will be + tagged for deleting. Does not remove category tags pointing at + subcategories.
'''
- def __init__(self, catTitle, batchMode = False, editSummary = '', useSummaryForDeletion = True, titleRegex = None, inPlace = False): + def __init__(self, catTitle, batchMode=False, editSummary='', + useSummaryForDeletion=True, titleRegex=None, inPlace=False, + pagesonly=False): self.editSummary = editSummary self.site = pywikibot.getSite() self.cat = catlib.Category(self.site, 'Category:'+ catTitle) @@ -550,6 +556,7 @@ self.batchMode = batchMode self.titleRegex = titleRegex self.inPlace = inPlace + self.pagesonly = pagesonly if not self.editSummary: self.editSummary = i18n.twtranslate(self.site, 'category-removing', {'oldcat': self.cat.title()}) @@ -562,6 +569,9 @@ 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 self.pagesonly: + return + # Also removes the category tag from subcategories' pages subcategories = self.cat.subcategoriesList(recurse = 0) if len(subcategories) == 0: @@ -857,6 +867,7 @@ recurse = False withHistory = False titleRegex = None + pagesonly = False
# This factory is responsible for processing command line arguments # that are also used by other scripts and that determine on which pages @@ -918,6 +929,8 @@ talkPages = True elif arg == '-recurse': recurse = True + elif arg == '-pagesonly': + pagesonly = True elif arg == '-create': create_pages = True elif arg == '-hist': @@ -946,7 +959,8 @@ oldCatTitle = pywikibot.input( u'Please enter the name of the category that should be removed:') bot = CategoryRemoveRobot(oldCatTitle, batchMode, editSummary, - useSummaryForDeletion, inPlace=inPlace) + useSummaryForDeletion, inPlace=inPlace, + pagesonly=pagesonly) bot.run() elif action == 'move': if (fromGiven == False):
pywikipedia-svn@lists.wikimedia.org