- $cache = ObjectCache::getMainWANInstance();
- $key = wfMemcKey( 'interwiki', $prefix );
- $cache->delete( $key );
- }
-
- /**
- * Fetch interwiki prefix data from local cache in constant database.
- *
- * @note More logic is explained in DefaultSettings.
- *
- * @param string $prefix Interwiki prefix
- * @return Interwiki
- */
- protected static function getInterwikiCached( $prefix ) {
- $value = self::getInterwikiCacheEntry( $prefix );
-
- $s = new Interwiki( $prefix );
- if ( $value ) {
- // Split values
- list( $local, $url ) = explode( ' ', $value, 2 );
- $s->mURL = $url;
- $s->mLocal = (int)$local;
- } else {
- $s = false;
- }
-
- return $s;
- }
-
- /**
- * Get entry from interwiki cache
- *
- * @note More logic is explained in DefaultSettings.
- *
- * @param string $prefix Database key
- * @return bool|string The interwiki entry or false if not found
- */
- protected static function getInterwikiCacheEntry( $prefix ) {
- global $wgInterwikiScopes, $wgInterwikiFallbackSite;
- static $site;
-
- wfDebug( __METHOD__ . "( $prefix )\n" );
- $value = false;
- try {
- // Resolve site name
- if ( $wgInterwikiScopes >= 3 && !$site ) {
- $site = self::getCacheValue( '__sites:' . wfWikiID() );
- if ( $site == '' ) {
- $site = $wgInterwikiFallbackSite;
- }
- }
-
- $value = self::getCacheValue( wfMemcKey( $prefix ) );
- // Site level
- if ( $value == '' && $wgInterwikiScopes >= 3 ) {
- $value = self::getCacheValue( "_{$site}:{$prefix}" );
- }
- // Global Level
- if ( $value == '' && $wgInterwikiScopes >= 2 ) {
- $value = self::getCacheValue( "__global:{$prefix}" );
- }
- if ( $value == 'undef' ) {
- $value = '';
- }
- } catch ( CdbException $e ) {
- wfDebug( __METHOD__ . ": CdbException caught, error message was "
- . $e->getMessage() );
- }
-
- 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
- *
- * @param string $prefix The interwiki prefix
- * @return Interwiki|bool Interwiki if $prefix is valid, otherwise false
- */
- protected static function load( $prefix ) {
- global $wgInterwikiExpiry;
-
- $iwData = array();
- if ( !Hooks::run( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
- return Interwiki::loadFromArray( $iwData );
- }
-
- if ( is_array( $iwData ) ) {
- $iw = Interwiki::loadFromArray( $iwData );
- if ( $iw ) {
- return $iw; // handled by hook
- }
- }
-
- $iwData = ObjectCache::getMainWANInstance()->getWithSetCallback(
- wfMemcKey( 'interwiki', $prefix ),
- $wgInterwikiExpiry,
- function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix ) {
- $dbr = wfGetDB( DB_SLAVE );
-
- $setOpts += Database::getCacheSetOptions( $dbr );
-
- $row = $dbr->selectRow(
- 'interwiki',
- Interwiki::selectFields(),
- array( 'iw_prefix' => $prefix ),
- __METHOD__
- );
-
- return $row ? (array)$row : '!NONEXISTENT';
- }
- );
-
- if ( is_array( $iwData ) ) {
- return Interwiki::loadFromArray( $iwData ) ?: false;
- }
-
- return false;
- }
-
- /**
- * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
- *
- * @param array $mc Associative array: row from the interwiki table
- * @return Interwiki|bool Interwiki object or false if $mc['iw_url'] is not set
- */
- protected static function loadFromArray( $mc ) {
- if ( isset( $mc['iw_url'] ) ) {
- $iw = new Interwiki();
- $iw->mURL = $mc['iw_url'];
- $iw->mLocal = isset( $mc['iw_local'] ) ? $mc['iw_local'] : 0;
- $iw->mTrans = isset( $mc['iw_trans'] ) ? $mc['iw_trans'] : 0;
- $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
- $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
-
- return $iw;
- }
-
- return false;
- }
-
- /**
- * Fetch all interwiki prefixes from interwiki cache
- *
- * @param null|string $local If not null, limits output to local/non-local interwikis
- * @return array List of prefixes
- * @since 1.19
- */
- protected static function getAllPrefixesCached( $local ) {
- global $wgInterwikiScopes, $wgInterwikiFallbackSite;
- static $site;
-
- wfDebug( __METHOD__ . "()\n" );
- $data = array();
- try {
- /* Resolve site name */
- if ( $wgInterwikiScopes >= 3 && !$site ) {
- $site = self::getCacheValue( '__sites:' . wfWikiID() );
-
- if ( $site == '' ) {
- $site = $wgInterwikiFallbackSite;
- }
- }
-
- // List of interwiki sources
- $sources = array();
- // Global Level
- if ( $wgInterwikiScopes >= 2 ) {
- $sources[] = '__global';
- }
- // Site level
- if ( $wgInterwikiScopes >= 3 ) {
- $sources[] = '_' . $site;
- }
- $sources[] = wfWikiID();
-
- foreach ( $sources as $source ) {
- $list = self::getCacheValue( '__list:' . $source );
- foreach ( explode( ' ', $list ) as $iw_prefix ) {
- $row = self::getCacheValue( "{$source}:{$iw_prefix}" );
- if ( !$row ) {
- continue;
- }
-
- list( $iw_local, $iw_url ) = explode( ' ', $row );
-
- if ( $local !== null && $local != $iw_local ) {
- continue;
- }
-
- $data[$iw_prefix] = array(
- 'iw_prefix' => $iw_prefix,
- 'iw_url' => $iw_url,
- 'iw_local' => $iw_local,
- );
- }
- }
- } catch ( CdbException $e ) {
- wfDebug( __METHOD__ . ": CdbException caught, error message was "
- . $e->getMessage() );
- }
-
- ksort( $data );
-
- return array_values( $data );
- }
-
- /**
- * Fetch all interwiki prefixes from DB
- *
- * @param string|null $local If not null, limits output to local/non-local interwikis
- * @return array List of prefixes
- * @since 1.19
- */
- protected static function getAllPrefixesDB( $local ) {
- $db = wfGetDB( DB_SLAVE );
-
- $where = array();
-
- if ( $local !== null ) {
- if ( $local == 1 ) {
- $where['iw_local'] = 1;
- } elseif ( $local == 0 ) {
- $where['iw_local'] = 0;
- }
- }
-
- $res = $db->select( 'interwiki',
- self::selectFields(),
- $where, __METHOD__, array( 'ORDER BY' => 'iw_prefix' )
- );
-
- $retval = array();
- foreach ( $res as $row ) {
- $retval[] = (array)$row;
- }
-
- return $retval;