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

Change subject: [FEAT] Site: Search for interwiki prefix for a site
......................................................................


[FEAT] Site: Search for interwiki prefix for a site

This returns all interwiki prefixes redirecting to a site from the
original site. It is like the reverse of APISite.interwiki and could be
useful to create link texts to other sites, without relying on the fact
that the language could be used for a interwiki link within the same
family.

This also changes the behaviour that now all sites are cached. So when
requesting the site for an interwiki prefix it previously had only
cached that site to that prefix.

Change-Id: Id2b889ccaf3c3f122429d59f5cf064855b0b4fea
---
M pywikibot/site.py
1 file changed, 42 insertions(+), 20 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/site.py b/pywikibot/site.py
index ba1b00c..7aa8218 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -597,6 +597,19 @@
         return [lang for lang in self.languages()
                 if lang[:1].upper() + lang[1:] not in nsnames]
 
+    def _cache_interwikimap(self, refresh=False):
+        """Cache the interwikimap with usable site instances."""
+        # _iw_sites is a local cache to return a APISite instance depending
+        # on the interwiki prefix of that site
+        if refresh or not hasattr(self, '_iw_sites'):
+            self._iw_sites = {}
+            for iw in self.siteinfo['interwikimap']:
+                try:
+                    site = (pywikibot.Site(url=iw['url']), 'local' in iw)
+                except Error:
+                    site = (None, False)
+                self._iw_sites[iw['prefix']] = site
+
     def interwiki(self, prefix):
         """
         Return the site for a corresponding interwiki prefix.
@@ -605,30 +618,39 @@
             doesn't match any of the existing families.
         @raise KeyError: if the prefix is not an interwiki prefix.
         """
-        # _iw_sites is a local cache to return a APISite instance depending
-        # on the interwiki prefix of that site
-        if not hasattr(self, '_iw_sites'):
-            self._iw_sites = {}
+        self._cache_interwikimap()
         if prefix in self._iw_sites:
             site = self._iw_sites[prefix]
-        else:
-            for interwiki in self.siteinfo['interwikimap']:
-                if interwiki['prefix'] == prefix:
-                    break
+            if site[0]:
+                return site[0]
             else:
-                raise KeyError(
-                    u"'{0}' is not an interwiki prefix.".format(prefix))
-            try:
-                site = (pywikibot.Site(url=interwiki['url']),
-                        'local' in interwiki)
-            except Error:
-                site = (None, False)
-            self._iw_sites[prefix] = site
-        if site[0]:
-            return site[0]
+                raise SiteDefinitionError(
+                    u"No family/site found for prefix '{0}'".format(prefix))
         else:
-            raise SiteDefinitionError(
-                u"No family/site found for prefix '{0}'".format(prefix))
+            raise KeyError(u"'{0}' is not an interwiki prefix.".format(prefix))
+
+    def interwiki_prefix(self, site):
+        """
+        Return the interwiki prefixes going to that site.
+
+        The interwiki prefixes are ordered first by length (shortest first)
+        and then alphabetically.
+
+        @param site: The targeted site, which might be it's own.
+        @type site: L{BaseSite}
+        @return: The interwiki prefixes
+        @rtype: list (guaranteed to be not empty)
+        @raise KeyError: if there is no interwiki prefix for that site.
+        """
+        assert(site is not None)
+        self._cache_interwikimap()
+        prefixes = set([prefix
+                        for prefix, cache_entry in self._iw_sites.items()
+                        if cache_entry[0] == site])
+        if not prefixes:
+            raise KeyError(
+                u"There is no interwiki prefix to '{0}'".format(site))
+        return sorted(prefixes, key=lambda p: (len(p), p))
 
     def local_interwiki(self, prefix):
         """

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Id2b889ccaf3c3f122429d59f5cf064855b0b4fea
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: XZise <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to