profiling fix
[lhc/web/wiklou.git] / includes / Interwiki.php
index 5d27ff8..f42f7e3 100644 (file)
@@ -17,8 +17,7 @@ class Interwiki {
 
        protected $mPrefix, $mURL, $mLocal, $mTrans;
 
-       function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 )
-       {
+       function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 ) {
                $this->mPrefix = $prefix;
                $this->mURL = $url;
                $this->mLocal = $local;
@@ -31,7 +30,7 @@ class Interwiki {
         * @return bool Whether it exists
         * @param $prefix string Interwiki prefix to use
         */
-       static public function isValidInterwiki( $prefix ){
+       static public function isValidInterwiki( $prefix ) {
                $result = self::fetch( $prefix );
                return (bool)$result;
        }
@@ -48,88 +47,85 @@ class Interwiki {
                        return null;
                }
                $prefix = $wgContLang->lc( $prefix );
-               if( isset( self::$smCache[$prefix] ) ){
+               if( isset( self::$smCache[$prefix] ) ) {
                        return self::$smCache[$prefix];
                }
                global $wgInterwikiCache;
-               if ($wgInterwikiCache) {
-                       return Interwiki::getInterwikiCached( $key );
-               }
-               $iw = Interwiki::load( $prefix );
-               if( !$iw ){
-                       $iw = false;
+               if( $wgInterwikiCache ) {
+                       $iw = Interwiki::getInterwikiCached( $prefix );
+               } else {
+                       $iw = Interwiki::load( $prefix );
+                       if( !$iw ) {
+                               $iw = false;
+                       }
                }
-               if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ){
+               if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
                        reset( self::$smCache );
                        unset( self::$smCache[ key( self::$smCache ) ] );
                }
                self::$smCache[$prefix] = $iw;
                return $iw;
        }
-       
+
        /**
         * Fetch interwiki prefix data from local cache in constant database.
         *
         * @note More logic is explained in DefaultSettings.
         *
-        * @param $key \type{\string} Database key
+        * @param $prefix \type{\string} Interwiki prefix
         * @return \type{\Interwiki} An interwiki object
         */
-       protected static function getInterwikiCached( $key ) {
-               $value = getInterwikiCacheEntry( $key );
-               
-               $s = new Interwiki( $key );
+       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{
+               } else {
                        $s = false;
                }
-               if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ){
-                       reset( self::$smCache );
-                       unset( self::$smCache[ key( self::$smCache ) ] );
-               }
-               self::$smCache[$prefix] = $s;
                return $s;
        }
-       
+
        /**
         * Get entry from interwiki cache
         *
         * @note More logic is explained in DefaultSettings.
         *
-        * @param $key \type{\string} Database key
+        * @param $prefix \type{\string} Database key
         * @return \type{\string) The entry
         */
-       protected static function getInterwikiCacheEntry( $key ){
+       protected static function getInterwikiCacheEntry( $prefix ) {
                global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
                static $db, $site;
 
-               if( !$db ){
+               wfDebug( __METHOD__ . "( $prefix )\n" );
+               if( !$db ) {
                        $db = dba_open( $wgInterwikiCache, 'r', 'cdb' );
                }
                /* Resolve site name */
                if( $wgInterwikiScopes>=3 && !$site ) {
                        $site = dba_fetch( '__sites:' . wfWikiID(), $db );
-                       if ( $site == "" ){
+                       if ( $site == '' ) {
                                $site = $wgInterwikiFallbackSite;
                        }
                }
-               
-               $value = dba_fetch( wfMemcKey( $key ), $db );
+
+               $value = dba_fetch( wfMemcKey( $prefix ), $db );
                // Site level
                if ( $value == '' && $wgInterwikiScopes >= 3 ) {
-                       $value = dba_fetch( "_{$site}:{$key}", $db );
+                       $value = dba_fetch( "_{$site}:{$prefix}", $db );
                }
                // Global Level
                if ( $value == '' && $wgInterwikiScopes >= 2 ) {
-                       $value = dba_fetch( "__global:{$key}", $db );
+                       $value = dba_fetch( "__global:{$prefix}", $db );
                }
                if ( $value == 'undef' )
                        $value = '';
-                       
+
                return $value;
        }
 
@@ -139,31 +135,30 @@ class Interwiki {
         * @param $prefix The interwiki prefix
         * @return bool The prefix is valid
         * @static
-        *
         */
        protected static function load( $prefix ) {
-               global $wgMemc;
+               global $wgMemc, $wgInterwikiExpiry;
                $key = wfMemcKey( 'interwiki', $prefix );
                $mc = $wgMemc->get( $key );
                $iw = false;
-               if( $mc && is_array( $mc ) ){ // is_array is hack for old keys
+               if( $mc && is_array( $mc ) ) { // is_array is hack for old keys
                        $iw = Interwiki::loadFromArray( $mc );
-                       if( $iw ){
+                       if( $iw ) {
                                return $iw;
                        }
                }
-               
+
                $db = wfGetDB( DB_SLAVE );
-                       
+
                $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
                        __METHOD__ ) );
                $iw = Interwiki::loadFromArray( $row );
                if ( $iw ) {
                        $mc = array( 'iw_url' => $iw->mURL, 'iw_local' => $iw->mLocal, 'iw_trans' => $iw->mTrans );
-                       $wgMemc->add( $key, $mc );
+                       $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
                        return $iw;
                }
-               
+
                return false;
        }
 
@@ -175,7 +170,7 @@ class Interwiki {
         * @static
         */
        protected static function loadFromArray( $mc ) {
-               if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ){
+               if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
                        $iw = new Interwiki();
                        $iw->mURL = $mc['iw_url'];
                        $iw->mLocal = $mc['iw_local'];
@@ -184,26 +179,26 @@ class Interwiki {
                }
                return false;
        }
-       
-       /** 
+
+       /**
         * Get the URL for a particular title (or with $1 if no title given)
         * 
         * @param $title string What text to put for the article name
         * @return string The URL
         */
-       function getURL( $title = null ){
+       function getURL( $title = null ) {
                $url = $this->mURL;
-               if( $title != null ){
+               if( $title != null ) {
                        $url = str_replace( "$1", $title, $url );
                }
                return $url;
        }
-       
-       function isLocal(){
+
+       function isLocal() {
                return $this->mLocal;
        }
-       
-       function isTranscludable(){
+
+       function isTranscludable() {
                return $this->mTrans;
        }