Revert r88008 (add size difference to Special:Contributions) and its large group...
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index e819f7c..09d0866 100644 (file)
@@ -2,6 +2,21 @@
 /**
  * See docs/deferred.txt
  *
+ * 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
+ *
  * @todo document (e.g. one-sentence top-level class description).
  */
 class LinksUpdate {
@@ -14,6 +29,7 @@ class LinksUpdate {
                $mLinks,         //!< Map of title strings to IDs for the links in the document
                $mImages,        //!< DB keys of the images used, in the array key only
                $mTemplates,     //!< Map of title strings to IDs for the template references, including broken ones
+               $mDistantTemplates,//!< Map of title strings to IDs for the distant template references, including broken ones
                $mExternals,     //!< URLs of external links, array key only
                $mCategories,    //!< Map of category names to sort keys
                $mInterlangs,    //!< Map of language codes to titles
@@ -51,6 +67,7 @@ class LinksUpdate {
                $this->mLinks = $parserOutput->getLinks();
                $this->mImages = $parserOutput->getImages();
                $this->mTemplates = $parserOutput->getTemplates();
+               $this->mDistantTemplates = $parserOutput->getDistantTemplates();
                $this->mExternals = $parserOutput->getExternalLinks();
                $this->mCategories = $parserOutput->getCategories();
                $this->mProperties = $parserOutput->getProperties();
@@ -120,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();
@@ -137,6 +154,15 @@ class LinksUpdate {
                $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
                        $this->getTemplateInsertions( $existing ) );
 
+               # Distant template links
+               global $wgGlobalDatabase;
+               if ( $wgGlobalDatabase ) {
+                       $existing = $this->getDistantExistingTemplates();
+                       $this->incrSharedTableUpdate( 'globaltemplatelinks', 'gtl',
+                               $this->getDistantTemplateDeletions( $existing ),
+                               $this->getDistantTemplateInsertions( $existing ) );
+               }
+
                # Category links
                $existing = $this->getExistingCategories();
 
@@ -226,6 +252,7 @@ class LinksUpdate {
                foreach ( $batches as $batch ) {
                        list( $start, $end ) = $batch;
                        $params = array(
+                               'table' => 'templatelinks',
                                'start' => $start,
                                'end' => $end,
                        );
@@ -351,11 +378,53 @@ 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 ) ) {
                        $this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
                }
        }
 
+       /**
+        * Update a shared table by doing a delete query then an insert query
+        * @private
+        */
+       function incrSharedTableUpdate( $table, $prefix, $deletions, $insertions ) {
+               global $wgGlobalDatabase;
+
+               if ( $wgGlobalDatabase ) {
+                       $dbw = wfGetDB( DB_MASTER, array(), $wgGlobalDatabase );
+                       $where = array( "{$prefix}_from_wiki" => wfWikiID(),
+                                                       "{$prefix}_from_page" => $this->mId
+                                       );
+                       $baseKey = "{$prefix}_to_wiki";
+                       $middleKey = "{$prefix}_to_namespace";
+
+                       $clause = $dbw->makeWhereFrom3d( $deletions, $baseKey, $middleKey, "{$prefix}_to_title" );
+                       if ( $clause ) {
+                               $where[] = $clause;
+                       } else {
+                               $where = false;
+                       }
+
+                       if ( $where ) {
+                               $dbw->delete( $table, $where, __METHOD__ );
+                       }
+                       if ( count( $insertions ) ) {
+                               $dbw->insert( $table, $insertions, __METHOD__, 'IGNORE' );
+                       }
+               }
+       }
 
        /**
         * Get an array of pagelinks insertions for passing to the DB
@@ -398,6 +467,44 @@ class LinksUpdate {
                return $arr;
        }
 
+       /**
+        * Get an array of distant template insertions. Like getLinkInsertions()
+        * @private
+        */
+       function getDistantTemplateInsertions( $existing = array() ) {
+               $arr = array();
+               foreach( $this->mDistantTemplates as $prefix => $templatesToNS ) {
+                       foreach( $templatesToNS as $ns => $dbkeys ) {
+                               $diffs = isset( $existing[$prefix] ) && isset( $existing[$prefix][$ns] )
+                                               ? array_diff_key( $dbkeys, $existing[$prefix][$ns] )
+                                               : $dbkeys;
+                               $interwiki = Interwiki::fetch( $prefix );
+                               $wikiid = $interwiki->getWikiID();
+                               foreach ( $diffs as $dbk => $id ) {
+                                       $arr['globaltemplatelinks'][] = array(
+                                               'gtl_from_wiki'      => wfWikiID(),
+                                               'gtl_from_page'      => $this->mId,
+                                               'gtl_from_namespace' => $this->mTitle->getNamespace(),
+                                               'gtl_from_title'     => $this->mTitle->getText(),
+                                               'gtl_to_wiki'        => $wikiid,
+                                               'gtl_to_namespace'   => $ns,
+                                               'gtl_to_title'       => $dbk
+                                       );
+                                       $arr['globalinterwiki'][] = array(
+                                               'giw_wikiid'             => $wikiid,
+                                               'giw_prefix'             => $prefix,
+                                       );
+                                       $arr['globalnamespaces'][] = array(
+                                               'gn_wiki'                        => wfWikiID( ),
+                                               'gn_namespace'           => $this->mTitle->getNamespace(),
+                                               'gn_namespacetext'       => $this->mTitle->getNsText(),
+                                       );
+                               }
+                       }
+               }
+               return $arr;
+       }
+
        /**
         * Get an array of image insertions
         * Skips the names specified in $existing
@@ -482,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;
        }
 
        /**
@@ -564,6 +671,30 @@ class LinksUpdate {
                return $del;
        }
 
+       /**
+        * Given an array of existing templates, returns those templates which are not in $this
+        * and thus should be deleted.
+        * @private
+        */
+       function getDistantTemplateDeletions( $existing ) {
+               $del = array();
+               foreach ( $existing as $wikiid => $templatesForNS ) {
+                       if ( isset( $this->mDistantTemplates[$wikiid] ) ) {
+                               $del[$wikiid] = array_diff_key( $existing[$wikiid], $this->mDistantTemplates[$wikiid] );
+                       } else {
+                               $del[$wikiid] = $existing[$wikiid];
+                       }
+                       foreach ( $templatesForNS as $ns => $dbkeys ) {
+                               if ( isset( $this->mDistantTemplates[$wikiid][$ns] ) ) {
+                                       $del[$wikiid][$ns] = array_diff_key( $existing[$wikiid][$ns], $this->mDistantTemplates[$wikiid][$ns] );
+                               } else {
+                                       $del[$wikiid][$ns] = $existing[$wikiid][$ns];
+                               }
+                       }
+               }
+               return $del;
+       }
+
        /**
         * Given an array of existing images, returns those images which are not in $this
         * and thus should be deleted.
@@ -597,7 +728,7 @@ class LinksUpdate {
         * @private
         */
        function getInterlangDeletions( $existing ) {
-           return array_diff_assoc( $existing, $this->mInterlangs );
+               return array_diff_assoc( $existing, $this->mInterlangs );
        }
 
        /**
@@ -659,6 +790,36 @@ class LinksUpdate {
                return $arr;
        }
 
+       /**
+        * Get an array of existing distant templates, as a 3-D array
+        * @private
+        */
+       function getDistantExistingTemplates() {
+               global $wgGlobalDatabase;
+
+               $arr = array();
+               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();
+                               }
+                               if ( !isset( $arr[$row->gtl_to_wiki][$row->gtl_to_namespace] ) ) {
+                                       $arr[$row->gtl_to_wiki][$row->gtl_to_namespace] = array();
+                               }
+                               $arr[$row->gtl_to_wiki][$row->gtl_to_namespace][$row->gtl_to_title] = 1;
+                       }
+               }
+               return $arr;
+       }
+
        /**
         * Get an array of existing images, image names in the keys
         * @private
@@ -754,7 +915,7 @@ class LinksUpdate {
        function getTitle() {
                return $this->mTitle;
        }
-       
+
        /**
         * Return the list of images used as generated by the parser
         */