Ladsgroup has uploaded a new change for review.

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

Change subject: Introduce InterwikiLookupAdapter on top of SiteLookup
......................................................................

Introduce InterwikiLookupAdapter on top of SiteLookup

Not tested very carefully yet also not sure about caching

Bug: T135146
Change-Id: I387dc2ff3f5564fcedde835dec66781d8e9424fd
---
A includes/interwiki/InterwikiLookupAdapter.php
1 file changed, 125 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/04/312704/1

diff --git a/includes/interwiki/InterwikiLookupAdapter.php 
b/includes/interwiki/InterwikiLookupAdapter.php
new file mode 100644
index 0000000..0f7ba97
--- /dev/null
+++ b/includes/interwiki/InterwikiLookupAdapter.php
@@ -0,0 +1,125 @@
+<?php
+namespace MediaWiki\Interwiki;
+
+/**
+ * InterwikiLookupAdapter on top of SiteLookup
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ *
+ * @since 1.28
+ * @ingroup InterwikiLookup
+ *
+ * @license GNU GPL v2+
+ */
+
+use Interwiki;
+use SiteLookup;
+
+class InterwikiLookupAdapter implements InterwikiLookup {
+
+       /**
+        * @var SiteLookup
+        */
+       private $siteLookup;
+
+       /**
+        * @var array
+        */
+       protected $interwikiMap;
+
+       function __construct(
+               SiteLookup $siteLookup,
+               array $interwikiMap = null
+       ) {
+               $this->siteLookup = $siteLookup;
+               if ( $interwikiMap === null ) {
+                       $interwikiMap = self::loadInterwikiMap( $siteLookup );
+               }
+               $this->interwikiMap = $interwikiMap;
+       }
+
+       /**
+        * See InterwikiLookup::isValidInterwiki
+        *
+        * @param string $prefix Interwiki prefix to use
+        * @return bool Whether it exists
+        */
+       public function isValidInterwiki($prefix) {
+               return array_key_exists( $this->interwikiMap[$prefix] );
+       }
+
+       /**
+        * See InterwikiLookup::Fetch
+        *
+        * @param string $prefix Interwiki prefix to use
+        * @return Interwiki|null|bool
+        */
+       public function fetch( $prefix ) {
+               if ( $this->isValidInterwiki( $prefix ) === false ) {
+                       return null;
+               }
+               return $this->interwikiMap[$prefix];
+       }
+
+       /**
+        * See InterwikiLookup::getAllPrefixes
+        *
+        * @param string|null $local If set, limits output to local/non-local 
interwikis
+        * @return string[] List of prefixes
+        */
+       public function getAllPrefixes( $local = null ) {
+               if ( $local === null ) {
+                       return array_keys( $this->interwikiMap );
+               }
+               $res = [];
+               foreach ( $this->interwikiMap as $interwiki => $site ) {
+                       if ( $site->getSource() === $local ) {
+                               $res[] = $interwiki;
+                       }
+               }
+               return $res;
+       }
+
+       /**
+        * See InterwikiLookup::invalidateCache
+        *
+        * @param string $prefix
+        */
+       public function invalidateCache( $prefix ) {
+               unset( $this->interwikiMap[$prefix] );
+       }
+
+       /**
+        * Load interwiki map to use as cache
+        *
+        * @param SiteLookup $siteLookup
+        * @return Interwiki[]
+        */
+       public static function loadInterwikiMap( SiteLookup $siteLookup ) {
+               $interwikiMap = [];
+               $siteList = $siteLookup->getSites();
+               $globals = $siteList->getGlobalIdentifiers();
+               foreach ( $globals as $global ) {
+                       $site = $siteList->getSite( $global );
+                       foreach ( $site->getInterwikiIds() as $interwiki ) {
+                               $interwikiMap[$interwiki] = $site;
+                       }
+               }
+               return $interwikiMap;
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I387dc2ff3f5564fcedde835dec66781d8e9424fd
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <[email protected]>

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

Reply via email to