XZise has uploaded a new change for review.

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

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

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

This returns al interwiki prefixes redirecting to a site from the
original site. It is like the reverse of APISite.interwiki.

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, 54 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/48/172548/1

diff --git a/pywikibot/site.py b/pywikibot/site.py
index ba1b00c..b801536 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,51 @@
             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, other_site):
+        """
+        Return the interwiki prefixes going to that site.
+
+        The interwiki prefixes are ordered first by length (shortest first)
+        and then alphabetically. The first entry might be different the
+        code if both sites belong to the same family and there is such prefix.
+
+        @param other_site: The other site
+        @type other_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(other_site is not None)
+        self._cache_interwikimap()
+        prefixes = set()
+        for prefix, site in self._iw_sites.items():
+            if site[0] == other_site:
+                prefixes.add(prefix)
+        if prefixes:
+            if (self.family == other_site.family and
+                    other_site.code in prefixes):
+                special = True
+                prefixes.remove(other_site.code)
+            else:
+                special = False
+            prefixes = sorted(prefixes, key=lambda p: (len(p), p))
+            if special:
+                prefixes = [other_site.code] + prefixes
+            return prefixes
+        else:
+            raise KeyError(
+                u"There is no interwiki prefix to '{0}'".format(other_site))
 
     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: newchange
Gerrit-Change-Id: Id2b889ccaf3c3f122429d59f5cf064855b0b4fea
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>

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

Reply via email to