Russell Blau has uploaded a new change for review.
https://gerrit.wikimedia.org/r/85203
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(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/03/85203/1
diff --git a/pywikibot/families/wikipedia_family.py
b/pywikibot/families/wikipedia_family.py
index 9e57685..d35d4e9 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 88eb3d3..291f523 100644
--- a/pywikibot/family.py
+++ b/pywikibot/family.py
@@ -545,7 +545,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': []
}
@@ -750,15 +749,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: newchange
Gerrit-Change-Id: I7af283b3b7a18bd713627cb5f1d8667773e3f3c6
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Russell Blau <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits