coding style tweaks, doc updates, marked some public functions as such
[lhc/web/wiklou.git] / includes / Interwiki.php
index 3d0f216..036cbe3 100644 (file)
-<?php\r
-/**\r
- * @file\r
- * Interwiki table entry\r
- */\r
-\r
-/**\r
- * The interwiki class\r
- * All information is loaded on creation when called by Interwiki::fetch( $prefix ).\r
- * All work is done on slave, because this should *never* change (except during schema updates etc, which arent wiki-related)\r
- */\r
-class Interwiki {\r
-\r
-       // Cache - removed in LRU order when it hits limit\r
-       protected static $smCache = array();\r
-       const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.\r
-\r
-       protected $mPrefix, $mURL, $mLocal, $mTrans;\r
-\r
-       function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 )\r
-       {\r
-               $this->mPrefix = $prefix;\r
-               $this->mURL = $url;\r
-               $this->mLocal = $local;\r
-               $this->mTrans = $trans;\r
-       }\r
-\r
-       /**\r
-        * Fetch an Interwiki object\r
-        * \r
-        * @return Interwiki Object, or null if not valid\r
-        * @param $prefix string Interwiki prefix to use\r
-        */\r
-       static public function fetch( $prefix ) {\r
-               if( isset( self::$smCache[$prefix] ) ){\r
-                       return self::$smCache[$prefix];\r
-               }\r
-               global $wgInterwikiCache;\r
-               if ($wgInterwikiCache) {\r
-                       return Interwiki::getInterwikiCached( $key );\r
-               }\r
-               $iw = new Interwiki;\r
-               $iw->load( $prefix );\r
-               if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ){\r
-                       array_shift( self::$smCache );\r
-               }\r
-               self::$smCache[$prefix] = &$iw;\r
-               return $iw;\r
-       }\r
-       \r
-       /**\r
-        * Fetch interwiki prefix data from local cache in constant database.\r
-        *\r
-        * @note More logic is explained in DefaultSettings.\r
-        *\r
-        * @param $key \type{\string} Database key\r
-        * @return \type{\string} URL of interwiki site\r
-        */\r
-       protected static function getInterwikiCached( $key ) {\r
-               global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;\r
-               static $db, $site;\r
-\r
-               if (!$db)\r
-                       $db=dba_open($wgInterwikiCache,'r','cdb');\r
-               /* Resolve site name */\r
-               if ($wgInterwikiScopes>=3 and !$site) {\r
-                       $site = dba_fetch('__sites:' . wfWikiID(), $db);\r
-                       if ($site=="")\r
-                               $site = $wgInterwikiFallbackSite;\r
-               }\r
-               $value = dba_fetch( wfMemcKey( $key ), $db);\r
-               if ($value=='' and $wgInterwikiScopes>=3) {\r
-                       /* try site-level */\r
-                       $value = dba_fetch("_{$site}:{$key}", $db);\r
-               }\r
-               if ($value=='' and $wgInterwikiScopes>=2) {\r
-                       /* try globals */\r
-                       $value = dba_fetch("__global:{$key}", $db);\r
-               }\r
-               if ($value=='undef')\r
-                       $value='';\r
-               $s = new Interwiki( $key );\r
-               if ( $value != '' ) {\r
-                       list( $local, $url ) = explode( ' ', $value, 2 );\r
-                       $s->mURL = $url;\r
-                       $s->mLocal = (int)$local;\r
-               }\r
-               if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ){\r
-                       array_shift( self::$smCache );\r
-               }\r
-               self::$smCache[$prefix] = &$s;\r
-               return $s;\r
-       }\r
-\r
-       /**\r
-        * Clear all member variables in the current object. Does not clear\r
-        * the block from the DB.\r
-        */\r
-       function clear() {\r
-               $this->mURL = '';\r
-               $this->mLocal = $this->mTrans = 0;\r
-               $this->mPrefix = null;\r
-       }\r
-\r
-       /**\r
-        * Get the DB object\r
-        *\r
-        * @return Database\r
-        */\r
-       function &getDB(){\r
-               $db = wfGetDB( DB_SLAVE );\r
-               return $db;\r
-       }\r
-\r
-       /**\r
-        * Load interwiki from the DB\r
-        *\r
-        * @param $prefix The interwiki prefix\r
-        * @return bool The prefix is valid\r
-        *\r
-        */\r
-       function load( $prefix ) {\r
-               global $wgMemc;\r
-               $key = wfMemcKey( 'interwiki', $prefix );\r
-               $mc = $wgMemc->get( $key );\r
-               if( $mc && is_array( $mc ) ){ // is_array is hack for old keys\r
-                       if( $this->loadFromArray( $mc ) ){\r
-                               wfDebug("Succeeded\n");\r
-                               return true;\r
-                       }\r
-               }else{\r
-                       $db =& $this->getDB();\r
-                       \r
-                       $res = $db->resultObject( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),\r
-                               __METHOD__ ) );\r
-                       if ( $this->loadFromResult( $res ) ) {\r
-                               $mc = array( 'url' => $this->mURL, 'local' => $this->mLocal, 'trans' => $this->mTrans );\r
-                               $wgMemc->add( $key, $mc );\r
-                               return true;\r
-                       }\r
-               }\r
-               \r
-               # Give up\r
-               $this->clear();\r
-               return false;\r
-       }\r
-\r
-       /**\r
-        * Fill in member variables from an array (e.g. memcached result)\r
-        *\r
-        * @return bool Whether everything was there\r
-        * @param $res ResultWrapper Row from the interwiki table\r
-        */\r
-       function loadFromArray( $mc ) {\r
-               if( isset( $mc['url'] ) && isset( $mc['local'] ) && isset( $mc['trans'] ) ){\r
-                       $this->mURL = $mc['url'];\r
-                       $this->mLocal = $mc['local'];\r
-                       $this->mTrans = $mc['trans'];\r
-                       return true;\r
-               }\r
-               return false;\r
-       }\r
-       \r
-       /**\r
-        * Fill in member variables from a result wrapper\r
-        *\r
-        * @return bool Whether there was a row there\r
-        * @param $res ResultWrapper Row from the interwiki table\r
-        */\r
-       function loadFromResult( ResultWrapper $res ) {\r
-               $ret = false;\r
-               if ( 0 != $res->numRows() ) {\r
-                       # Get first entry\r
-                       $row = $res->fetchObject();\r
-                       $this->initFromRow( $row );\r
-                       $ret = true;\r
-               }\r
-               $res->free();\r
-               return $ret;\r
-       }\r
-\r
-       /**\r
-        * Given a database row from the interwiki table, initialize\r
-        * member variables\r
-        *\r
-        * @param $row ResultWrapper A row from the interwiki table\r
-        */\r
-       function initFromRow( $row ) {\r
-               $this->mPrefix = $row->iw_prefix;\r
-               $this->mURL = $row->iw_url;\r
-               $this->mLocal = $row->iw_local;\r
-               $this->mTrans = $row->iw_trans;\r
-       }\r
-       \r
-       /** \r
-        * Get the URL for a particular title (or with $1 if no title given)\r
-        * \r
-        * @param $title string What text to put for the article name\r
-        * @return string The URL\r
-        */\r
-       function getURL( $title = null ){\r
-               $url = $this->mURL;\r
-               if( $title != null ){\r
-                       $url = str_replace( "$1", $title, $url );\r
-               }\r
-               return $url;\r
-       }\r
-       \r
-       function isLocal(){\r
-               return $this->mLocal;\r
-       }\r
-       \r
-       function isTranscludable(){\r
-               return $this->mTrans;\r
-       }\r
-\r
-}\r
+<?php
+/**
+ * @file
+ * Interwiki table entry
+ */
+
+/**
+ * The interwiki class
+ * All information is loaded on creation when called by Interwiki::fetch( $prefix ).
+ * All work is done on slave, because this should *never* change (except during
+ * schema updates etc, which aren't wiki-related)
+ */
+class Interwiki {
+
+       // Cache - removes oldest entry when it hits limit
+       protected static $smCache = array();
+       const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.
+
+       protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
+
+       public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0, $trans = 0 ) {
+               $this->mPrefix = $prefix;
+               $this->mURL = $url;
+               $this->mAPI = $api;
+               $this->mWikiID = $wikiId;
+               $this->mLocal = $local;
+               $this->mTrans = $trans;
+       }
+
+       /**
+        * Check whether an interwiki prefix exists
+        *
+        * @param $prefix String: interwiki prefix to use
+        * @return Boolean: whether it exists
+        */
+       static public function isValidInterwiki( $prefix ) {
+               $result = self::fetch( $prefix );
+               return (bool)$result;
+       }
+
+       /**
+        * Fetch an Interwiki object
+        *
+        * @param $prefix String: interwiki prefix to use
+        * @return Interwiki Object, or null if not valid
+        */
+       static public function fetch( $prefix ) {
+               global $wgContLang;
+               if( $prefix == '' ) {
+                       return null;
+               }
+               $prefix = $wgContLang->lc( $prefix );
+               if( isset( self::$smCache[$prefix] ) ) {
+                       return self::$smCache[$prefix];
+               }
+               global $wgInterwikiCache;
+               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 ) {
+                       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 $prefix String: interwiki prefix
+        * @return Interwiki object
+        */
+       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 $prefix String: database key
+        * @return String: the entry
+        */
+       protected static function getInterwikiCacheEntry( $prefix ) {
+               global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
+               static $db, $site;
+
+               wfDebug( __METHOD__ . "( $prefix )\n" );
+               if( !$db ) {
+                       $db = CdbReader::open( $wgInterwikiCache );
+               }
+               /* Resolve site name */
+               if( $wgInterwikiScopes >= 3 && !$site ) {
+                       $site = $db->get( '__sites:' . wfWikiID() );
+                       if ( $site == '' ) {
+                               $site = $wgInterwikiFallbackSite;
+                       }
+               }
+
+               $value = $db->get( wfMemcKey( $prefix ) );
+               // Site level
+               if ( $value == '' && $wgInterwikiScopes >= 3 ) {
+                       $value = $db->get( "_{$site}:{$prefix}" );
+               }
+               // Global Level
+               if ( $value == '' && $wgInterwikiScopes >= 2 ) {
+                       $value = $db->get( "__global:{$prefix}" );
+               }
+               if ( $value == 'undef' ) {
+                       $value = '';
+               }
+
+               return $value;
+       }
+
+       /**
+        * Load the interwiki, trying first memcached then the DB
+        *
+        * @param $prefix The interwiki prefix
+        * @return Boolean: the prefix is valid
+        */
+       protected static function load( $prefix ) {
+               global $wgMemc, $wgInterwikiExpiry;
+
+               $iwData = false;
+               if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
+                       return Interwiki::loadFromArray( $iwData );
+               }
+
+               if ( !$iwData ) {
+                       $key = wfMemcKey( 'interwiki', $prefix );
+                       $iwData = $wgMemc->get( $key );
+               }
+
+               if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
+                       $iw = Interwiki::loadFromArray( $iwData );
+                       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_api' => $iw->mAPI,
+                               'iw_local' => $iw->mLocal,
+                               'iw_trans' => $iw->mTrans
+                       );
+                       $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
+                       return $iw;
+               }
+
+               return false;
+       }
+
+       /**
+        * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
+        *
+        * @param $mc Associative array: row from the interwiki table
+        * @return Boolean: whether everything was there
+        */
+       protected static function loadFromArray( $mc ) {
+               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'];
+                       $iw->mTrans = $mc['iw_trans'];
+                       $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
+                       $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
+
+                       return $iw;
+               }
+               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
+        */
+       public function getURL( $title = null ) {
+               $url = $this->mURL;
+               if( $title != null ) {
+                       $url = str_replace( "$1", $title, $url );
+               }
+               return $url;
+       }
+
+       /**
+        * Get the API URL for this wiki
+        *
+        * @return String: the URL
+        */
+       public function getAPI() {
+               return $this->mAPI;
+       }
+
+       /**
+        * Get the DB name for this wiki
+        *
+        * @return String: the DB name
+        */
+       public function getWikiID() {
+               return $this->mWikiID;
+       }
+
+       /**
+        * Is this a local link from a sister project, or is
+        * it something outside, like Google
+        *
+        * @return Boolean
+        */
+       public function isLocal() {
+               return $this->mLocal;
+       }
+
+       /**
+        * Can pages from this wiki be transcluded?
+        * Still requires $wgEnableScaryTransclusion
+        *
+        * @return Boolean
+        */
+       public function isTranscludable() {
+               return $this->mTrans;
+       }
+
+       /**
+        * Get the name for the interwiki site
+        *
+        * @return String
+        */
+       public function getName() {
+               $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
+               return !$msg->exists() ? '' : $msg;
+       }
+
+       /**
+        * Get a description for this interwiki
+        *
+        * @return String
+        */
+       public function getDescription() {
+               $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
+               return !$msg->exists() ? '' : $msg;
+       }
+}