Allow $wgInterwikiCache to be an associative array
authorOri Livneh <ori@wikimedia.org>
Sun, 1 Nov 2015 06:04:18 +0000 (23:04 -0700)
committerKrinkle <krinklemail@gmail.com>
Wed, 23 Dec 2015 22:23:52 +0000 (22:23 +0000)
For the same reasons wikiversions.cdb was converted to a PHP file -- viz., that
static arrays in PHP files get cached in HHVM's bytecode cache and are
therefore faster to use with HHVM than CDB files.

Bug: T122362
Change-Id: I5a979f047031ef211622f399df9b3b388797f53a

includes/DefaultSettings.php
includes/interwiki/Interwiki.php

index 54216fd..a1cda86 100644 (file)
@@ -3884,10 +3884,13 @@ $wgInterwikiExpiry = 10800;
  */
 
 /**
- *$wgInterwikiCache specifies path to constant database file.
+ * Interwiki cache, either as an associative array or a path to a constant
+ * database (.cdb) file.
+ *
+ * This data structure database is generated by the `dumpInterwiki` maintenance
+ * script (which lives in the WikimediaMaintenance repository) and has key
+ * formats such as the following:
  *
- * This cdb database is generated by dumpInterwiki from maintenance and has
- * such key formats:
  *  - dbname:key - a simple key (e.g. enwiki:meta)
  *  - _sitename:key - site-scope key (e.g. wiktionary:meta)
  *  - __global:key - global-scope key (e.g. __global:meta)
@@ -3895,6 +3898,8 @@ $wgInterwikiExpiry = 10800;
  *
  * Sites mapping just specifies site name, other keys provide "local url"
  * data layout.
+ *
+ * @var bool|array|string
  */
 $wgInterwikiCache = false;
 
index bd8291f..be16bcf 100644 (file)
@@ -137,7 +137,7 @@ class Interwiki {
                $value = self::getInterwikiCacheEntry( $prefix );
 
                $s = new Interwiki( $prefix );
-               if ( $value != '' ) {
+               if ( $value ) {
                        // Split values
                        list( $local, $url ) = explode( ' ', $value, 2 );
                        $s->mURL = $url;
@@ -155,34 +155,31 @@ class Interwiki {
         * @note More logic is explained in DefaultSettings.
         *
         * @param string $prefix Database key
-        * @return string The interwiki entry
+        * @return bool|string The interwiki entry or false if not found
         */
        protected static function getInterwikiCacheEntry( $prefix ) {
-               global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
-               static $db, $site;
+               global $wgInterwikiScopes, $wgInterwikiFallbackSite;
+               static $site;
 
                wfDebug( __METHOD__ . "( $prefix )\n" );
                $value = false;
                try {
-                       if ( !$db ) {
-                               $db = CdbReader::open( $wgInterwikiCache );
-                       }
-                       /* Resolve site name */
+                       // Resolve site name
                        if ( $wgInterwikiScopes >= 3 && !$site ) {
-                               $site = $db->get( '__sites:' . wfWikiID() );
+                               $site = self::getCacheValue( '__sites:' . wfWikiID() );
                                if ( $site == '' ) {
                                        $site = $wgInterwikiFallbackSite;
                                }
                        }
 
-                       $value = $db->get( wfMemcKey( $prefix ) );
+                       $value = self::getCacheValue( wfMemcKey( $prefix ) );
                        // Site level
                        if ( $value == '' && $wgInterwikiScopes >= 3 ) {
-                               $value = $db->get( "_{$site}:{$prefix}" );
+                               $value = self::getCacheValue( "_{$site}:{$prefix}" );
                        }
                        // Global Level
                        if ( $value == '' && $wgInterwikiScopes >= 2 ) {
-                               $value = $db->get( "__global:{$prefix}" );
+                               $value = self::getCacheValue( "__global:{$prefix}" );
                        }
                        if ( $value == 'undef' ) {
                                $value = '';
@@ -195,6 +192,19 @@ class Interwiki {
                return $value;
        }
 
+       private static function getCacheValue( $key ) {
+               global $wgInterwikiCache;
+               static $reader;
+               if ( $reader === null ) {
+                       $reader = is_array( $wgInterwikiCache ) ? false : CdbReader::open( $wgInterwikiCache );
+               }
+               if ( $reader ) {
+                       return $reader->get( $key );
+               } else {
+                       return isset( $wgInterwikiCache[$key] ) ? $wgInterwikiCache[$key] : false;
+               }
+       }
+
        /**
         * Load the interwiki, trying first memcached then the DB
         *