Xqt has uploaded a new change for review.
https://gerrit.wikimedia.org/r/92026
Change subject: Adding option of not deleting the old cat
......................................................................
Adding option of not deleting the old cat
(update from compat),
- pep8 changes
- replace pywikibot.output("warning") with pywikibot.warning("")
- synchronize with compat
Change-Id: Ia555c20e6360c0c4bcb603af6f76b39fc08d3e88
---
M scripts/category.py
1 file changed, 101 insertions(+), 59 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/26/92026/1
diff --git a/scripts/category.py b/scripts/category.py
index 73929cc..efc675b 100755
--- a/scripts/category.py
+++ b/scripts/category.py
@@ -36,6 +36,9 @@
for the language, which is "Category was disbanded" in
English.
+Options for "move" action:
+ * -nodelete - Don't delete the old category after move
+
Options for several actions:
* -rebuild - reset the database
* -from: - The category to move from (for the move option)
@@ -84,8 +87,8 @@
# (C) leogregianin, 2004-2008
# (C) Cyde, 2006-2010
# (C) Anreas J Schwab, 2007
-# (C) xqt, 2009-2012
-# (C) Pywikipedia team, 2008-2012
+# (C) xqt, 2009-2013
+# (C) Pywikipedia team, 2008-2013
#
__version__ = '$Id$'
#
@@ -109,7 +112,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 +141,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 +173,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
@@ -272,7 +277,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 +305,11 @@
% 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)))
+ 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 +347,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
@@ -385,7 +390,8 @@
useSummaryForDeletion=True):
site = pywikibot.getSite()
self.editSummary = editSummary
- self.oldCat = catlib.Category(pywikibot.Link('Category:' +
oldCatTitle))
+ self.oldCat = pywikibot.Category(
+ pywikibot.Link('Category:' + oldCatTitle))
self.newCatTitle = newCatTitle
self.inPlace = inPlace
self.moveCatPage = moveCatPage
@@ -396,15 +402,14 @@
def run(self):
site = pywikibot.getSite()
- newCat = catlib.Category(pywikibot.Link('Category:' +
self.newCatTitle))
+ newCat = pywikibot.Category(
+ pywikibot.Link('Category:' + self.newCatTitle))
newcat_contents = set(newCat.members())
# set edit summary message
if not self.editSummary:
- self.editSummary = i18n.twtranslate(
- site, 'category-replacing',
- {'oldcat': self.oldCat.title(),
- 'newcat': newCat.title()}
- )
+ self.editSummary = i18n.twtranslate(site, 'category-replacing',
+ {'oldcat': self.oldCat.title(),
+ 'newcat': newCat.title()})
# Copy the category contents to the new category page
copied = False
@@ -485,7 +490,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 +508,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 +565,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 +597,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 +615,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 +633,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 +648,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:
@@ -658,10 +684,12 @@
# show subcategories as possible choices (with numbers)
for i in range(len(supercatlist)):
# layout: we don't expect a cat to have more than 10 supercats
- pywikibot.output(u'u%d - Move up to %s' % (i,
supercatlist[i].title()))
+ pywikibot.output(u'u%d - Move up to %s'
+ % (i, supercatlist[i].title()))
for i in range(len(subcatlist)):
# layout: we don't expect a cat to have more than 100 subcats
- pywikibot.output(u'%2d - Move down to %s' % (i,
subcatlist[i].title()))
+ pywikibot.output(u'%2d - Move down to %s'
+ % (i, subcatlist[i].title()))
print ' j - Jump to another category'
print ' s - Skip this article'
print ' r - Remove this category tag'
@@ -679,17 +707,20 @@
if current_cat == original_cat:
print 'No changes necessary.'
else:
- catlib.change_category(article, original_cat, current_cat,
comment=self.editSummary)
+ 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 +741,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 +794,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 +806,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 +818,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 +888,7 @@
sort_by_last_name = False
restore = False
create_pages = False
+ deleteEmptySourceCat = True
for arg in pywikibot.handleArgs(*args):
if arg == 'add':
action = 'add'
@@ -865,6 +902,8 @@
action = 'tree'
elif arg == 'listify':
action = 'listify'
+ elif arg == '-nodelete':
+ deleteEmptySourceCat = False
elif arg == '-person':
sort_by_last_name = True
elif arg == '-rebuild':
@@ -917,7 +956,8 @@
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 +970,9 @@
newCatTitle = pywikibot.input(
u'Please enter the new name of the category:')
bot = CategoryMoveRobot(oldCatTitle, newCatTitle, batchMode,
- editSummary, inPlace, titleRegex=titleRegex)
+ editSummary, inPlace,
+ deleteEmptySourceCat=deleteEmptySourceCat,
+ titleRegex=titleRegex)
bot.run()
elif action == 'tidy':
catTitle = pywikibot.input(u'Which category do you want to tidy up?')
@@ -940,8 +982,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':
--
To view, visit https://gerrit.wikimedia.org/r/92026
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia555c20e6360c0c4bcb603af6f76b39fc08d3e88
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits