jenkins-bot has submitted this change and it was merged.

Change subject: Use the API to retrieve redirects to the category redirect 
template (this time with caching)
......................................................................


Use the API to retrieve redirects to the category redirect template
(this time with caching)

Change-Id: I7af283b3b7a18bd713627cb5f1d8667773e3f3c6
---
M pywikibot/families/wikipedia_family.py
M pywikibot/family.py
2 files changed, 24 insertions(+), 10 deletions(-)

Approvals:
  Merlijn van Deen: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/families/wikipedia_family.py 
b/pywikibot/families/wikipedia_family.py
index 0149a8e..67f3fd4 100644
--- a/pywikibot/families/wikipedia_family.py
+++ b/pywikibot/families/wikipedia_family.py
@@ -54,11 +54,7 @@
             'arz': (u'تحويل تصنيف',),
             'cs': (u'Zastaralá kategorie',),
             'da': (u'Kategoriomdirigering',),
-            'en': (u'Category redirect',
-                   u'Category Redirect',
-                   u"Categoryredirect",
-                   u'Catredirect',
-                   u'Cat redirect',),
+            'en': (u'Category redirect',),
             'es': (u'Categoría redirigida',),
             'eu': (u'Kategoria redirect',),
             'fa': (u'رده بهتر',
diff --git a/pywikibot/family.py b/pywikibot/family.py
index 59484c9..a7c58d4 100644
--- a/pywikibot/family.py
+++ b/pywikibot/family.py
@@ -653,7 +653,6 @@
         }
 
         # A list of category redirect template names in different languages
-        # Note: It *is* necessary to list template redirects here
         self.category_redirect_templates = {
             '_default': []
         }
@@ -858,15 +857,34 @@
                 % {'language_code': code})
 
     def category_redirects(self, code, fallback="_default"):
-        if code in self.category_redirect_templates:
-            return self.category_redirect_templates[code]
-        elif fallback:
-            return self.category_redirect_templates[fallback]
+        if not hasattr(self, "_catredirtemplates") or code not in 
self._catredirtemplates:
+            self.get_cr_templates(code, fallback)
+        if code in self._catredirtemplates:
+            return self._catredirtemplates[code]
         else:
             raise KeyError(
 "ERROR: title for category redirect template in language '%s' unknown"
                 % code)
 
+    def get_cr_templates(self, code, fallback):
+        if not hasattr(self, "_catredirtemplates"):
+            self._catredirtemplates = {}
+        if code in self.category_redirect_templates:
+            cr_template = self.category_redirect_templates[code][0]
+        else:
+            cr_template = self.category_redirect_templates[fallback][0]
+        # start with list of category redirect templates from family file
+        cr_page = pywikibot.Page(pywikibot.Site(code, self),
+                                 "Template:" + cr_template)
+        cr_list = list(self.category_redirect_templates[code])
+        # retrieve all redirects to primary template from API,
+        # add any that are not already on the list
+        for t in cr_page.backlinks(filterRedirects=True, namespaces=10):
+            newtitle = t.title(withNamespace=False)
+            if newtitle not in cr_list:
+                cr_list.append(newtitle)
+        self._catredirtemplates[code] = cr_list
+
     def disambig(self, code, fallback='_default'):
         if code in self.disambiguationTemplates:
             return self.disambiguationTemplates[code]

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I7af283b3b7a18bd713627cb5f1d8667773e3f3c6
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Russell Blau <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Russell Blau <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to