From: Ori Livneh Date: Sun, 1 Nov 2015 06:04:18 +0000 (-0700) Subject: Allow $wgInterwikiCache to be an associative array X-Git-Tag: 1.31.0-rc.0~8560^2 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=eb7cc60aa736dc2700b69f8d62c8acd6adca3684 Allow $wgInterwikiCache to be an associative array 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 --- diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 54216fdf52..a1cda86ce4 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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; diff --git a/includes/interwiki/Interwiki.php b/includes/interwiki/Interwiki.php index bd8291ff4b..be16bcf0b5 100644 --- a/includes/interwiki/Interwiki.php +++ b/includes/interwiki/Interwiki.php @@ -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 *