Revision: 6129
Author: russblau
Date: 2008-12-04 20:39:59 +0000 (Thu, 04 Dec 2008)
Log Message:
-----------
Backwards-compatibility for catlib.py contents. Note that I changed the default on
"inPlace" to True, on the principle of least astonishment. If the user wants to
rearrange the category links, they should have to say so explicitly.
Modified Paths:
--------------
branches/rewrite/pywikibot/page.py
Added Paths:
-----------
branches/rewrite/pywikibot/catlib.py
Added: branches/rewrite/pywikibot/catlib.py
===================================================================
--- branches/rewrite/pywikibot/catlib.py (rev 0)
+++ branches/rewrite/pywikibot/catlib.py 2008-12-04 20:39:59 UTC (rev 6129)
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+"""
+WARNING: THIS MODULE EXISTS SOLELY TO PROVIDE BACKWARDS-COMPATIBILITY.
+
+Do not use in new scripts; use the source to find the appropriate
+function/method instead.
+
+"""
+#
+# (C) Pywikipedia bot team, 2008
+#
+# Distributed under the terms of the MIT license.
+#
+__version__ = '$Id: $'
+
+import pywikibot
+import pywikibot.textlib
+
+from pywikibot import Category
+
+def change_category(article, oldCat, newCat, comment=None, sortKey=None,
+ inPlace=True):
+ return article.change_category(oldCat, newCat, comment, sortKey, inPlace)
Modified: branches/rewrite/pywikibot/page.py
===================================================================
--- branches/rewrite/pywikibot/page.py 2008-12-03 21:29:38 UTC (rev 6128)
+++ branches/rewrite/pywikibot/page.py 2008-12-04 20:39:59 UTC (rev 6129)
@@ -11,6 +11,7 @@
import pywikibot
from pywikibot import deprecate_arg
+import pywikibot.textlib
import htmlentitydefs
import logging
@@ -1037,6 +1038,101 @@
if answer in ['y', 'Y']:
return self.site().protect(self, edit, move, reason)
+ def change_category(article, oldCat, newCat, comment=None, sortKey=None,
+ inPlace=True):
+ """Remove page from oldCat and add it to newCat.
+
+ oldCat and newCat should be Category objects.
+ If newCat is None, the category will be removed.
+
+ """ # TODO: document remaining arguments
+ cats = self.categories(get_redirect=True)
+ site = self.site()
+ changesMade = False
+
+ if not self.canBeEdited():
+ pywikibot.output(u"Can't edit %s, skipping it..."
+ % self.title(asLink=True))
+ return False
+ if inPlace == True:
+ newtext = pywikibot.textlib.replaceCategoryInPlace(
+ self.text, oldCat, newCat)
+ if newtext == self.text:
+ pywikibot.output(
+ u'No changes in made in page %s.'
+ % self.title(asLink=True))
+ return False
+ try:
+ self.put(newtext, comment)
+ return True
+ except pywikibot.EditConflict:
+ pywikibot.output(
+ u'Skipping %s because of edit conflict'
+ % self.title(asLink=True))
+ except pywikibot.LockedPage:
+ pywikibot.output(u'Skipping locked page %s'
+ % self.title(asLink=True))
+ except pywikibot.SpamfilterError, error:
+ pywikibot.output(
+ u'Changing page %s blocked by spam filter (URL=%s)'
+ % (self.title(asLink=True), error.url))
+ except pywikibot.NoUsername:
+ pywikibot.output(
+ u"Page %s not saved; sysop privileges required."
+ % self.title(asLink=True))
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u"Saving page %s failed: %s"
+ % (self.title(asLink=True), error.message))
+ return False
+
+ # This loop will replace all occurrences of the category to be changed,
+ # and remove duplicates.
+ newCatList = []
+ newCatSet = set()
+ for i in range(len(cats)):
+ cat = cats[i]
+ if cat == oldCat:
+ changesMade = True
+ if not sortKey:
+ sortKey = cat.sortKey
+ if newCat:
+ if newCat.title() not in newCatSet:
+ newCategory = Category(site, newCat.title(),
+ sortKey=sortKey)
+ newCatSet.add(newCat.title())
+ newCatList.append(newCategory)
+ elif cat.title() not in newCatSet:
+ newCatSet.add(cat.title())
+ newCatList.append(cat)
+
+ if not changesMade:
+ pywikibot.output(u'ERROR: %s is not in category %s!'
+ % (self.title(asLink=True), oldCat.title()))
+ else:
+ try:
+ text = pywikibot.textlib.replaceCategoryLinks(self.text,
+ newCatList)
+ except ValueError:
+ # Make sure that the only way replaceCategoryLinks() can return
+ # a ValueError is in the case of interwiki links to self.
+ pywikibot.output(
+ u'Skipping %s because of interwiki link to self' % self)
+ try:
+ self.put(text, comment)
+ except pywikibot.EditConflict:
+ pywikibot.output(
+ u'Skipping %s because of edit conflict' % self.title())
+ except pywikibot.SpamfilterError, e:
+ pywikibot.output(
+ u'Skipping %s because of blacklist entry %s'
+ % (self.title(), e.url))
+ except pywikibot.LockedPage:
+ pywikibot.output(
+ u'Skipping %s because page is locked' % self.title())
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u"Saving page %s failed: %s"
+ % (self.title(asLink=True), error.message))
+
######## DEPRECATED METHODS ########
def encoding(self):