Mpaa has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/165109

Change subject: textlib.py: categories with no indexing in categoryFormat
......................................................................

textlib.py: categories with no indexing in categoryFormat

Avoid the need for 'categories' to support indexing.
In this way, any iterable can be provided as parameter.

In Page.change_category(), use sets.

Change-Id: I1d1b83fd09f4c1f87bd2756b640aa4df34dc34d5
---
M pywikibot/page.py
M pywikibot/textlib.py
M tests/textlib_tests.py
3 files changed, 18 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/09/165109/1

diff --git a/pywikibot/page.py b/pywikibot/page.py
index 7c1d9d3..ba20bee 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -1688,11 +1688,8 @@
         """
         # get list of Category objects the article is in and remove possible
         # duplicates
-        cats = []
-        for cat in textlib.getCategoryLinks(self.text, site=self.site):
-            if cat not in cats:
-                cats.append(cat)
-
+        cats = set(textlib.getCategoryLinks(self.text, site=self.site,
+                                            include=include))
         site = self.site
 
         if not sortKey:
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 4b3934b..450fd28 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -658,7 +658,6 @@
 # -------------------------------------
 # Functions dealing with category links
 # -------------------------------------
-
 def getCategoryLinks(text, site=None):
     """Return a list of category links found in text.
 
@@ -856,14 +855,22 @@
     if insite is None:
         insite = pywikibot.Site()
 
-    if isinstance(categories[0], basestring):
-        if categories[0][0] == '[':
-            catLinks = categories
+    catLinks = []
+    for category in categories:
+        if isinstance(category, basestring):
+            if '|' in category:
+                title, sortKey = category.strip('[]').split('|', 1)
+            else:
+                title, sortKey = category.strip('[]'), None
         else:
-            catLinks = ['[[Category:%s]]' % category for category in 
categories]
-    else:
-        catLinks = [pywikibot.Category(category).aslink()
-                    for category in categories]
+            title = category.title()
+            try:
+                sortKey = category.sortKey
+            except AttributeError:
+                sortKey = None
+        category = pywikibot.Category(insite, title, sortKey=sortKey)
+        link = category.aslink()
+        catLinks.append(link)
 
     if insite.category_on_one_line():
         sep = ' '
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index cafa378..bc28612 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -172,7 +172,7 @@
         sep = config.LS
         config.line_separator = ''  # use an empty separator temporarily
         new = textlib.replaceCategoryLinks(old, cats, site=self.site)
-        # restore the default separator
+        # Restore the default separator.
         config.line_separator = sep
         self.assertEqual(old, new)
 

-- 
To view, visit https://gerrit.wikimedia.org/r/165109
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1d1b83fd09f4c1f87bd2756b640aa4df34dc34d5
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to