Merge "Added result properties to action=paraminfo"
[lhc/web/wiklou.git] / includes / interwiki / Interwiki.php
index b79f31b..eacf9a8 100644 (file)
@@ -1,7 +1,23 @@
 <?php
 /**
+ * Interwiki table entry.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
- * Interwiki table entry
  */
 
 /**
@@ -9,7 +25,6 @@
  * 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)
- * This class also contains the functions that allow interwiki templates transclusion.
  */
 class Interwiki {
 
@@ -43,7 +58,7 @@ class Interwiki {
         * Fetch an Interwiki object
         *
         * @param $prefix String: interwiki prefix to use
-        * @return Interwiki Object, or null if not valid
+        * @return Interwiki|null|bool
         */
        static public function fetch( $prefix ) {
                global $wgContLang;
@@ -131,13 +146,14 @@ class Interwiki {
                        $value = '';
                }
 
+
                return $value;
        }
 
        /**
         * Load the interwiki, trying first memcached then the DB
         *
-        * @param $prefix The interwiki prefix
+        * @param $prefix string The interwiki prefix
         * @return Boolean: the prefix is valid
         */
        protected static function load( $prefix ) {
@@ -151,6 +167,9 @@ class Interwiki {
                if ( !$iwData ) {
                        $key = wfMemcKey( 'interwiki', $prefix );
                        $iwData = $wgMemc->get( $key );
+                       if ( $iwData === '!NONEXISTENT' ) {
+                               return false; // negative cache hit
+                       }
                }
 
                if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
@@ -162,19 +181,20 @@ class Interwiki {
 
                $db = wfGetDB( DB_SLAVE );
 
-               $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
+               $row = $db->fetchRow( $db->select( 'interwiki', self::selectFields(), array( 'iw_prefix' => $prefix ),
                        __METHOD__ ) );
                $iw = Interwiki::loadFromArray( $row );
                if ( $iw ) {
                        $mc = array(
                                'iw_url' => $iw->mURL,
                                'iw_api' => $iw->mAPI,
-                               'iw_wikiid' => $iw->mWikiID,
                                'iw_local' => $iw->mLocal,
                                'iw_trans' => $iw->mTrans
                        );
                        $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
                        return $iw;
+               } else {
+                       $wgMemc->add( $key, '!NONEXISTENT', $wgInterwikiExpiry ); // negative cache hit
                }
 
                return false;
@@ -183,8 +203,8 @@ class Interwiki {
        /**
         * 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
+        * @param $mc array Associative array: row from the interwiki table
+        * @return Boolean|Interwiki whether everything was there
         */
        protected static function loadFromArray( $mc ) {
                if( isset( $mc['iw_url'] ) ) {
@@ -192,10 +212,9 @@ class 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->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
                        $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
-                       
+
                        return $iw;
                }
                return false;
@@ -204,7 +223,7 @@ class Interwiki {
        /**
         * Fetch all interwiki prefixes from interwiki cache
         *
-        * @param $local If not null, limits output to local/non-local interwikis
+        * @param $local null|string If not null, limits output to local/non-local interwikis
         * @return Array List of prefixes
         * @since 1.19
         */
@@ -268,7 +287,7 @@ class Interwiki {
        /**
         * Fetch all interwiki prefixes from DB
         *
-        * @param $local If not null, limits output to local/non-local interwikis
+        * @param $local string|null If not null, limits output to local/non-local interwikis
         * @return Array List of prefixes
         * @since 1.19
         */
@@ -286,7 +305,7 @@ class Interwiki {
                }
 
                $res = $db->select( 'interwiki',
-                       array( 'iw_prefix', 'iw_url', 'iw_api', 'iw_wikiid', 'iw_local', 'iw_trans' ),
+                       self::selectFields(),
                        $where, __METHOD__, array( 'ORDER BY' => 'iw_prefix' )
                );
                $retval = array();
@@ -299,7 +318,7 @@ class Interwiki {
        /**
         * Returns all interwiki prefixes
         *
-        * @param $local If set, limits output to local/non-local interwikis
+        * @param $local string|null If set, limits output to local/non-local interwikis
         * @return Array List of prefixes
         * @since 1.19
         */
@@ -318,11 +337,14 @@ class Interwiki {
         *
         * @param $title String: what text to put for the article name
         * @return String: the URL
+        * @note Prior to 1.19 The getURL with an argument was broken.
+        *       If you if you use this arg in an extension that supports MW earlier
+        *       than 1.19 please wfUrlencode and substitute $1 on your own.
         */
        public function getURL( $title = null ) {
                $url = $this->mURL;
-               if( $title != null ) {
-                       $url = str_replace( "$1", $title, $url );
+               if( $title !== null ) {
+                       $url = str_replace( "$1", wfUrlencode( $title ), $url );
                }
                return $url;
        }
@@ -384,166 +406,20 @@ class Interwiki {
                $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
                return !$msg->exists() ? '' : $msg;
        }
-       
 
        /**
-        * Transclude an interwiki link.
+        * Return the list of interwiki fields that should be selected to create
+        * a new interwiki object.
+        * @return array
         */
-       public static function interwikiTransclude( $title ) {
-                       
-               // If we have a wikiID, we will use it to get an access to the remote database
-               // if not, we will use the API URL to retrieve the data through a HTTP Get
-               
-               $wikiID = $title->getTransWikiID( );
-               $transAPI = $title->getTransAPI( );
-               
-               if ( $wikiID !== '') {
-               
-                       $finalText = self::fetchTemplateFromDB( $wikiID, $title );
-                       return $finalText;
-
-               } else if( $transAPI !== '' ) {
-                       
-                       $interwiki = $title->getInterwiki( );
-                       $fullTitle = $title->getSemiPrefixedText( );
-                       
-                       $finalText = self::fetchTemplateFromAPI( $interwiki, $transAPI, $fullTitle );
-                       
-                       return $finalText;
-                       
-               }
-               return false;
-       }
-       
-       /**
-        * Retrieve the wikitext of a distant page accessing the foreign DB
-        */
-       public static function fetchTemplateFromDB ( $wikiID, $title ) {
-               
-               $revision = Revision::loadFromTitleForeignWiki( $wikiID, $title );
-               
-               if ( $revision ) {
-                       $text = $revision->getText();
-                       return $text;
-               }
-                               
-               return false;
-       }
-       
-       /**
-        * Retrieve the wikitext of a distant page using the API of the foreign wiki
-        */
-       public static function fetchTemplateFromAPI( $interwiki, $transAPI, $fullTitle ) {
-               global $wgMemc, $wgTranscludeCacheExpiry;
-               
-               $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $fullTitle );
-               $text = $wgMemc->get( $key );
-               if( is_array ( $text ) &&
-                               isset ( $text['missing'] ) &&
-                               $text['missing'] === true ) {
-                       return false;
-               } else if ( $text ) {
-                       return $text;
-               }
-               
-               $url = wfAppendQuery(
-                       $transAPI,
-                       array(  'action' => 'query',
-                                       'titles' => $fullTitle,
-                                       'prop' => 'revisions',
-                                       'rvprop' => 'content',
-                                       'format' => 'json'
-                       )
+       public static function selectFields() {
+               return array(
+                       'iw_prefix',
+                       'iw_url',
+                       'iw_api',
+                       'iw_wikiid',
+                       'iw_local',
+                       'iw_trans'
                );
-               
-               $get = Http::get( $url );
-               $content = FormatJson::decode( $get, true );
-                       
-               if ( isset ( $content['query'] ) &&
-                               isset ( $content['query']['pages'] ) ) {
-                       $page = array_pop( $content['query']['pages'] );
-                       if ( $page && isset( $page['revisions'][0]['*'] ) ) {
-                               $text = $page['revisions'][0]['*'];
-                               $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );
-
-                               // When we cache a template, we also retrieve and cache its subtemplates
-                               $subtemplates = self::getSubtemplatesListFromAPI( $interwiki, $transAPI, $fullTitle );
-                               self::cacheTemplatesFromAPI( $interwiki, $transAPI, $subtemplates );
-                               
-                               return $text;
-                       } else {
-                               $wgMemc->set( $key, array ( 'missing' => true ), $wgTranscludeCacheExpiry );
-                       }
-               }
-               return false;
-       }       
-
-       public static function getSubtemplatesListFromAPI ( $interwiki, $transAPI, $title ) {
-               $url = wfAppendQuery( $transAPI,
-                       array( 'action' => 'query',
-                       'titles' => $title,
-                       'prop' => 'templates',
-                       'format' => 'json'
-                       )
-               );
-                       
-               $get = Http::get( $url );
-               $myArray = FormatJson::decode($get, true);
-
-               $templates = array( );
-               if ( ! empty( $myArray['query'] )) {
-                       if ( ! empty( $myArray['query']['pages'] )) {
-                               $templates = array_pop( $myArray['query']['pages'] );
-                               if ( ! empty( $templates['templates'] )) {
-                                       $templates = $templates['templates'];
-                               }
-                       }
-                       return $templates;
-               }
-       }
-
-       public static function cacheTemplatesFromAPI( $interwiki, $transAPI, $titles ){
-               global $wgMemc, $wgTranscludeCacheExpiry;
-               
-               $outdatedTitles = array( );
-               
-               foreach( $titles as $title ){
-                       if ( isset ( $title['title'] ) ) {
-                               $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $title['title'] );
-                               $text = $wgMemc->get( $key );
-                               if( !$text ){
-                                       $outdatedTitles[] = $title['title'];
-                               }
-                       }                       
-               }
-               
-               $batches = array_chunk( $outdatedTitles, 50 );
-               
-               foreach( $batches as $batch ){
-                       $url = wfAppendQuery(
-                               $transAPI,
-                               array(  'action' => 'query',
-                                               'titles' => implode( '|', $batch ),
-                                               'prop' => 'revisions',
-                                               'rvprop' => 'content',
-                                               'format' => 'json'
-                               )
-                       );
-                       $get = Http::get( $url );
-                       $content = FormatJson::decode( $get, true );
-                               
-                       if ( isset ( $content['query'] ) &&
-                                       isset ( $content['query']['pages'] ) ) {
-                               foreach( $content['query']['pages'] as $page ) {
-                                       $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $page['title'] );
-                                       if ( isset ( $page['revisions'][0]['*'] ) ) {
-                                               $text = $page['revisions'][0]['*'];
-                                       } else {
-                                               $text = array ( 'missing' => true );
-                                       }
-                                       $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );  
-                               }
-                       }
-               }
        }
 }