FU 97704: I was thinking the space was already added in this case
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index 293c2fc..09d0866 100644 (file)
@@ -137,7 +137,7 @@ class LinksUpdate {
                # External links
                $existing = $this->getExistingExternals();
                $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
-               $this->getExternalInsertions( $existing ) );
+                       $this->getExternalInsertions( $existing ) );
 
                # Language links
                $existing = $this->getExistingInterlangs();
@@ -155,8 +155,8 @@ class LinksUpdate {
                        $this->getTemplateInsertions( $existing ) );
 
                # Distant template links
-               global $wgGlobalDB;
-               if ( $wgGlobalDB ) {
+               global $wgGlobalDatabase;
+               if ( $wgGlobalDatabase ) {
                        $existing = $this->getDistantExistingTemplates();
                        $this->incrSharedTableUpdate( 'globaltemplatelinks', 'gtl',
                                $this->getDistantTemplateDeletions( $existing ),
@@ -252,6 +252,7 @@ class LinksUpdate {
                foreach ( $batches as $batch ) {
                        list( $start, $end ) = $batch;
                        $params = array(
+                               'table' => 'templatelinks',
                                'start' => $start,
                                'end' => $end,
                        );
@@ -377,16 +378,20 @@ class LinksUpdate {
                if ( $where ) {
                        $this->mDb->delete( $table, $where, __METHOD__ );
                }
+               if ( isset( $insertions['globaltemplatelinks'] ) ) {
+                       $this->mDb->insert( 'globaltemplatelinks', $insertions['globaltemplatelinks'], __METHOD__, 'IGNORE' );
+                       unset( $insertions['globaltemplatelinks'] );
+               }
+               if ( isset( $insertions['globalnamespaces'] ) ) {
+                       $this->mDb->insert( 'globalnamespaces', $insertions['globalnamespaces'], __METHOD__, 'IGNORE' );
+                       unset( $insertions['globalnamespaces'] );
+               }
+               if ( isset( $insertions['globalinterwiki'] ) ) {
+                       $this->mDb->insert( 'globalinterwiki', $insertions['globalinterwiki'], __METHOD__, 'IGNORE' );
+                       unset( $insertions['globalinterwiki'] );
+               }
                if ( count( $insertions ) ) {
-                       if ( isset( $insertions['globaltemplatelinks'] ) ) {
-                               $this->mDb->insert( 'globaltemplatelinks', $insertions['globaltemplatelinks'], __METHOD__, 'IGNORE' );
-                       }
-                       if ( isset( $insertions['globalnamespaces'] ) ) {
-                               $this->mDb->insert( 'globalnamespaces', $insertions['globalnamespaces'], __METHOD__, 'IGNORE' );
-                       }
-                       if ( isset( $insertions['globalinterwiki'] ) ) {
-                               $this->mDb->insert( 'globalinterwiki', $insertions['globalinterwiki'], __METHOD__, 'IGNORE' );
-                       }
+                       $this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
                }
        }
 
@@ -395,13 +400,11 @@ class LinksUpdate {
         * @private
         */
        function incrSharedTableUpdate( $table, $prefix, $deletions, $insertions ) {
+               global $wgGlobalDatabase;
 
-               global $wgWikiID;
-               global $wgGlobalDB;
-
-               if ( $wgGlobalDB ) {
-                       $dbw = wfGetDB( DB_MASTER, array(), $wgGlobalDB );
-                       $where = array( "{$prefix}_from_wiki" => $wgWikiID,
+               if ( $wgGlobalDatabase ) {
+                       $dbw = wfGetDB( DB_MASTER, array(), $wgGlobalDatabase );
+                       $where = array( "{$prefix}_from_wiki" => wfWikiID(),
                                                        "{$prefix}_from_page" => $this->mId
                                        );
                        $baseKey = "{$prefix}_to_wiki";
@@ -469,18 +472,17 @@ class LinksUpdate {
         * @private
         */
        function getDistantTemplateInsertions( $existing = array() ) {
-               global $wgWikiID;
                $arr = array();
-               foreach( $this->mDistantTemplates as $wikiid => $templatesToNS ) {
+               foreach( $this->mDistantTemplates as $prefix => $templatesToNS ) {
                        foreach( $templatesToNS as $ns => $dbkeys ) {
-                               $diffs = isset( $existing[$wikiid] ) && isset( $existing[$wikiid][$ns] )
-                                               ? array_diff_key( $dbkeys, $existing[$wikiid][$ns] )
+                               $diffs = isset( $existing[$prefix] ) && isset( $existing[$prefix][$ns] )
+                                               ? array_diff_key( $dbkeys, $existing[$prefix][$ns] )
                                                : $dbkeys;
-                               $interwiki = Interwiki::fetch( $wikiid );
+                               $interwiki = Interwiki::fetch( $prefix );
                                $wikiid = $interwiki->getWikiID();
                                foreach ( $diffs as $dbk => $id ) {
                                        $arr['globaltemplatelinks'][] = array(
-                                               'gtl_from_wiki'      => $wgWikiID,
+                                               'gtl_from_wiki'      => wfWikiID(),
                                                'gtl_from_page'      => $this->mId,
                                                'gtl_from_namespace' => $this->mTitle->getNamespace(),
                                                'gtl_from_title'     => $this->mTitle->getText(),
@@ -490,7 +492,7 @@ class LinksUpdate {
                                        );
                                        $arr['globalinterwiki'][] = array(
                                                'giw_wikiid'             => $wikiid,
-                                               'giw_prefix'             => $prefix, // FIXME: $prefix ix undefined
+                                               'giw_prefix'             => $prefix,
                                        );
                                        $arr['globalnamespaces'][] = array(
                                                'gn_wiki'                        => wfWikiID( ),
@@ -587,16 +589,16 @@ class LinksUpdate {
         * @private
         */
        function getInterlangInsertions( $existing = array() ) {
-           $diffs = array_diff_assoc( $this->mInterlangs, $existing );
-           $arr = array();
-           foreach( $diffs as $lang => $title ) {
-               $arr[] = array(
-                   'll_from'  => $this->mId,
-                   'll_lang'  => $lang,
-                   'll_title' => $title
-               );
-           }
-           return $arr;
+               $diffs = array_diff_assoc( $this->mInterlangs, $existing );
+               $arr = array();
+               foreach( $diffs as $lang => $title ) {
+                       $arr[] = array(
+                               'll_from'  => $this->mId,
+                               'll_lang'  => $lang,
+                               'll_title' => $title
+                       );
+               }
+               return $arr;
        }
 
        /**
@@ -726,7 +728,7 @@ class LinksUpdate {
         * @private
         */
        function getInterlangDeletions( $existing ) {
-           return array_diff_assoc( $existing, $this->mInterlangs );
+               return array_diff_assoc( $existing, $this->mInterlangs );
        }
 
        /**
@@ -793,15 +795,19 @@ class LinksUpdate {
         * @private
         */
        function getDistantExistingTemplates() {
-               global $wgWikiID;
-               global $wgGlobalDB;
+               global $wgGlobalDatabase;
 
                $arr = array();
-               if ( $wgGlobalDB ) {
-                       $dbr = wfGetDB( DB_SLAVE, array(), $wgGlobalDB );
-                       $res = $dbr->select( 'globaltemplatelinks', array( 'gtl_to_wiki', 'gtl_to_namespace', 'gtl_to_title' ),
-                               array( 'gtl_from_wiki' => $wgWikiID, 'gtl_from_page' => $this->mId ), __METHOD__, $this->mOptions );
-                       while ( $row = $dbr->fetchObject( $res ) ) {
+               if ( $wgGlobalDatabase ) {
+                       $dbr = wfGetDB( DB_SLAVE, array(), $wgGlobalDatabase );
+                       $res = $dbr->select(
+                               'globaltemplatelinks',
+                               array( 'gtl_to_wiki', 'gtl_to_namespace', 'gtl_to_title' ),
+                               array( 'gtl_from_wiki' => wfWikiID(), 'gtl_from_page' => $this->mId ),
+                               __METHOD__,
+                               $this->mOptions
+                       );
+                       foreach ( $res as $row ) {
                                if ( !isset( $arr[$row->gtl_to_wiki] ) ) {
                                        $arr[$row->gtl_to_wiki] = array();
                                }
@@ -810,7 +816,6 @@ class LinksUpdate {
                                }
                                $arr[$row->gtl_to_wiki][$row->gtl_to_namespace][$row->gtl_to_title] = 1;
                        }
-                       $dbr->freeResult( $res );
                }
                return $arr;
        }