[Pywikipedia-l] SVN: [4715] trunk/pywikipedia

rotem at svn.wikimedia.org rotem at svn.wikimedia.org
Sat Dec 15 18:22:23 UTC 2007


Revision: 4715
Author:   rotem
Date:     2007-12-15 18:22:18 +0000 (Sat, 15 Dec 2007)

Log Message:
-----------
Creating a subcategories generator, using it in category.py, and rewriting the move operation in category.py: first copying the category, and moving its talk page if necessary; then moving the articles and subcategories (to an existing category); and then deleting the old category and its redirect talk page, if requested and necessary.

Modified Paths:
--------------
    trunk/pywikipedia/category.py
    trunk/pywikipedia/pagegenerators.py

Modified: trunk/pywikipedia/category.py
===================================================================
--- trunk/pywikipedia/category.py	2007-12-15 15:22:46 UTC (rev 4714)
+++ trunk/pywikipedia/category.py	2007-12-15 18:22:18 UTC (rev 4715)
@@ -374,32 +374,47 @@
 
     def run(self):
         newCat = catlib.Category(wikipedia.getSite(), 'Category:' + self.newCatTitle)
+
+        # Copy the category contents to the new category page
+        copied = False
+        oldMovedTalk = None
+        if self.oldCat.exists() and self.moveCatPage:
+            copied = self.oldCat.copyAndKeep(self.newCatTitle, wikipedia.translate(wikipedia.getSite(), cfd_templates))
+            copied = True # ALREADY COPIED!
+            # Also move the talk page
+            if copied:
+                reason = wikipedia.translate(wikipedia.getSite(), deletion_reason_move) % (self.newCatTitle, self.newCatTitle)
+                oldTalk = self.oldCat.toggleTalkPage()
+                if oldTalk.exists():
+                    newTalkTitle = newCat.toggleTalkPage().title()
+                    if oldTalk.move(newTalkTitle, reason):
+                        oldMovedTalk = oldTalk
+
+        # Move articles
         gen = pagegenerators.CategorizedPageGenerator(self.oldCat, recurse = False)
         preloadingGen = pagegenerators.PreloadingGenerator(gen)
         for article in preloadingGen:
             if not self.titleRegex or re.search(self.titleRegex,article.title()):
                 catlib.change_category(article, self.oldCat, newCat, inPlace=self.inPlace)
 
-        # TODO: create subcategory generator
-        subcategories = self.oldCat.subcategoriesList(recurse = False)
-        if len(subcategories) == 0:
-            wikipedia.output(u'There are no subcategories in category ' + self.oldCat.title())
-        else:
-            for subcategory in subcategories:
-                if not self.titleRegex or re.search(self.titleRegex,subcategory.title()):
-                    catlib.change_category(subcategory, self.oldCat, newCat, comment = self.editSummary, inPlace=self.inPlace)
-        if self.oldCat.exists() and self.moveCatPage:
-            # try to copy page contents to new cat page
-            if self.oldCat.copyAndKeep(self.newCatTitle, wikipedia.translate(wikipedia.getSite(), cfd_templates)):
-                if self.oldCat.isEmpty() and self.deleteEmptySourceCat == True:
-                    reason = wikipedia.translate(wikipedia.getSite(), deletion_reason_move) % (self.newCatTitle, self.newCatTitle)
-                    if self.batchMode:
-                        self.oldCat.delete(reason, False)
-                    else:
-                        self.oldCat.delete(reason, True)
-                else:
-                    wikipedia.output('Couldn\'t copy contents of %s because %s already exists.' % (self.oldCat.title(), self.newCatTitle))
+        # Move subcategories
+        gen = pagegenerators.SubCategoriesPageGenerator(self.oldCat, recurse = False)
+        preloadingGen = pagegenerators.PreloadingGenerator(gen)
+        for subcategory in preloadingGen:
+            if not self.titleRegex or re.search(self.titleRegex,subcategory.title()):
+                catlib.change_category(article, self.oldCat, newCat, inPlace=self.inPlace)
 
+        # Delete the old category and its moved talk page
+        if copied and self.deleteEmptySourceCat == True:
+            if self.oldCat.isEmpty():
+                reason = wikipedia.translate(wikipedia.getSite(), deletion_reason_move) % (self.newCatTitle, self.newCatTitle)
+                confirm = not self.batchMode
+                self.oldCat.delete(reason, confirm)
+                if oldMovedTalk is not None:
+                    oldMovedTalk.delete(reason, confirm)
+            else:
+                wikipedia.output('Couldn\'t delete %s - not empty.' % (self.oldCat.title(), self.newCatTitle))
+
 class CategoryListifyRobot:
     '''
     Creates a list containing all of the members in a category.

Modified: trunk/pywikipedia/pagegenerators.py
===================================================================
--- trunk/pywikipedia/pagegenerators.py	2007-12-15 15:22:46 UTC (rev 4714)
+++ trunk/pywikipedia/pagegenerators.py	2007-12-15 18:22:18 UTC (rev 4715)
@@ -183,6 +183,18 @@
         if page.title() >= start:
             yield page
 
+def SubCategoriesPageGenerator(category, recurse=False):
+    '''
+    Yields all subcategories in a specific category.
+
+    If recurse is True, pages in subcategories are included as well; if
+    recurse is an int, only subcategories to that depth will be included
+    (e.g., recurse=2 will get pages in subcats and sub-subcats, but will
+    not go any further).
+    '''
+    for page in category.subcategories(recurse = recurse):
+        yield page
+
 def UnCategorizedCategoryGenerator(number = 100, repeat = False, site = None):
     if site is None:
         site = wikipedia.getSite()





More information about the Pywikipedia-l mailing list