Revert r88008 (add size difference to Special:Contributions) and its large group...
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index 9cb11b9..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
@@ -30,7 +46,7 @@ class LinksUpdate {
         * @param $parserOutput ParserOutput: output from a full parse of this page
         * @param $recursive Boolean: queue jobs for recursive updates?
         */
-       function LinksUpdate( $title, $parserOutput, $recursive = true ) {
+       function __construct( $title, $parserOutput, $recursive = true ) {
                global $wgAntiLockFlags;
 
                if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) {
@@ -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();
@@ -67,6 +84,17 @@ class LinksUpdate {
                        $this->mInterlangs[$key] = $title;
                }
 
+               foreach ( $this->mCategories as &$sortkey ) {
+                       # If the sortkey is longer then 255 bytes,
+                       # it truncated by DB, and then doesn't get
+                       # matched when comparing existing vs current
+                       # categories, causing bug 25254.
+                       # Also. substr behaves weird when given "".
+                       if ( $sortkey !== '' ) {
+                               $sortkey = substr( $sortkey, 0, 255 );
+                       }
+               }
+
                $this->mRecursive = $recursive;
 
                wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
@@ -99,7 +127,8 @@ class LinksUpdate {
                $existing = $this->getExistingImages();
 
                $imageDeletes = $this->getImageDeletions( $existing );
-               $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes, $this->getImageInsertions( $existing ) );
+               $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
+                       $this->getImageInsertions( $existing ) );
 
                # Invalidate all image description pages which had links added or removed
                $imageUpdates = $imageDeletes + array_diff_key( $this->mImages, $existing );
@@ -108,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();
@@ -125,12 +154,22 @@ 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();
 
                $categoryDeletes = $this->getCategoryDeletions( $existing );
 
-               $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes, $this->getCategoryInsertions( $existing ) );
+               $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
+                       $this->getCategoryInsertions( $existing ) );
 
                # Invalidate all categories which were added, deleted or changed (set symmetric difference)
                $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
@@ -143,7 +182,8 @@ class LinksUpdate {
 
                $propertiesDeletes = $this->getPropertyDeletions( $existing );
 
-               $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes, $this->getPropertyInsertions( $existing ) );
+               $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
+                       $this->getPropertyInsertions( $existing ) );
 
                # Invalidate the necessary pages
                $changed = $propertiesDeletes + array_diff_assoc( $this->mProperties, $existing );
@@ -212,6 +252,7 @@ class LinksUpdate {
                foreach ( $batches as $batch ) {
                        list( $start, $end ) = $batch;
                        $params = array(
+                               'table' => 'templatelinks',
                                'start' => $start,
                                'end' => $end,
                        );
@@ -247,7 +288,7 @@ class LinksUpdate {
                                'page_touched < ' . $this->mDb->addQuotes( $now )
                        ), __METHOD__
                );
-               while ( $row = $this->mDb->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $ids[] = $row->page_id;
                }
                if ( !count( $ids ) ) {
@@ -337,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
@@ -351,9 +434,9 @@ class LinksUpdate {
        function getLinkInsertions( $existing = array() ) {
                $arr = array();
                foreach( $this->mLinks as $ns => $dbkeys ) {
-                       # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
-                       # in GlobalFunctions.php
-                       $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
+                       $diffs = isset( $existing[$ns] )
+                               ? array_diff_key( $dbkeys, $existing[$ns] )
+                               : $dbkeys;
                        foreach ( $diffs as $dbk => $id ) {
                                $arr[] = array(
                                        'pl_from'      => $this->mId,
@@ -384,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
@@ -426,40 +547,37 @@ class LinksUpdate {
         * @private
         */
        function getCategoryInsertions( $existing = array() ) {
-               global $wgContLang, $wgExperimentalCategorySort, $wgCollationVersion;
+               global $wgContLang, $wgCategoryCollation;
                $diffs = array_diff_assoc( $this->mCategories, $existing );
                $arr = array();
-               foreach ( $diffs as $name => $sortkey ) {
+               foreach ( $diffs as $name => $prefix ) {
                        $nt = Title::makeTitleSafe( NS_CATEGORY, $name );
                        $wgContLang->findVariantLink( $name, $nt, true );
 
-                       if ( $wgExperimentalCategorySort ) {
-                               if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
-                                       $type = 'subcat';
-                               } elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
-                                       $type = 'file';
-                               } else {
-                                       $type = 'page';
-                               }
-                               $convertedSortkey = $wgContLang->convertToSortkey( $sortkey );
-                               # TODO: Set $sortkey to null if it's redundant
-                               $arr[] = array(
-                                       'cl_from'    => $this->mId,
-                                       'cl_to'      => $name,
-                                       'cl_sortkey' => $convertedSortkey,
-                                       'cl_timestamp' => $this->mDb->timestamp(),
-                                       'cl_raw_sortkey' => $sortkey,
-                                       'cl_collation' => $wgCollationVersion,
-                                       'cl_type' => $type,
-                               );
+                       if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
+                               $type = 'subcat';
+                       } elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
+                               $type = 'file';
                        } else {
-                               $arr[] = array(
-                                       'cl_from'    => $this->mId,
-                                       'cl_to'      => $name,
-                                       'cl_sortkey' => $sortkey,
-                                       'cl_timestamp' => $this->mDb->timestamp()
-                               );
+                               $type = 'page';
                        }
+
+                       # Treat custom sortkeys as a prefix, so that if multiple
+                       # things are forced to sort as '*' or something, they'll
+                       # sort properly in the category rather than in page_id
+                       # order or such.
+                       $sortkey = Collation::singleton()->getSortKey(
+                               $this->mTitle->getCategorySortkey( $prefix ) );
+
+                       $arr[] = array(
+                               'cl_from'    => $this->mId,
+                               'cl_to'      => $name,
+                               'cl_sortkey' => $sortkey,
+                               'cl_timestamp' => $this->mDb->timestamp(),
+                               'cl_sortkey_prefix' => $prefix,
+                               'cl_collation' => $wgCategoryCollation,
+                               'cl_type' => $type,
+                       );
                }
                return $arr;
        }
@@ -471,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;
        }
 
        /**
@@ -507,8 +625,6 @@ class LinksUpdate {
        function getInterwikiInsertions( $existing = array() ) {
                $arr = array();
                foreach( $this->mInterwikis as $prefix => $dbkeys ) {
-                       # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
-                       # in GlobalFunctions.php
                        $diffs = isset( $existing[$prefix] ) ? array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
                        foreach ( $diffs as $dbk => $id ) {
                                $arr[] = array(
@@ -521,8 +637,6 @@ class LinksUpdate {
                return $arr;
        }
 
-
-
        /**
         * Given an array of existing links, returns those links which are not in $this
         * and thus should be deleted.
@@ -557,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.
@@ -590,7 +728,7 @@ class LinksUpdate {
         * @private
         */
        function getInterlangDeletions( $existing ) {
-           return array_diff_assoc( $existing, $this->mInterlangs );
+               return array_diff_assoc( $existing, $this->mInterlangs );
        }
 
        /**
@@ -626,13 +764,12 @@ class LinksUpdate {
                $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
                        array( 'pl_from' => $this->mId ), __METHOD__, $this->mOptions );
                $arr = array();
-               while ( $row = $this->mDb->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        if ( !isset( $arr[$row->pl_namespace] ) ) {
                                $arr[$row->pl_namespace] = array();
                        }
                        $arr[$row->pl_namespace][$row->pl_title] = 1;
                }
-               $this->mDb->freeResult( $res );
                return $arr;
        }
 
@@ -644,13 +781,42 @@ class LinksUpdate {
                $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
                        array( 'tl_from' => $this->mId ), __METHOD__, $this->mOptions );
                $arr = array();
-               while ( $row = $this->mDb->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        if ( !isset( $arr[$row->tl_namespace] ) ) {
                                $arr[$row->tl_namespace] = array();
                        }
                        $arr[$row->tl_namespace][$row->tl_title] = 1;
                }
-               $this->mDb->freeResult( $res );
+               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;
        }
 
@@ -662,10 +828,9 @@ class LinksUpdate {
                $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
                        array( 'il_from' => $this->mId ), __METHOD__, $this->mOptions );
                $arr = array();
-               while ( $row = $this->mDb->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $arr[$row->il_to] = 1;
                }
-               $this->mDb->freeResult( $res );
                return $arr;
        }
 
@@ -677,10 +842,9 @@ class LinksUpdate {
                $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
                        array( 'el_from' => $this->mId ), __METHOD__, $this->mOptions );
                $arr = array();
-               while ( $row = $this->mDb->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $arr[$row->el_to] = 1;
                }
-               $this->mDb->freeResult( $res );
                return $arr;
        }
 
@@ -689,13 +853,12 @@ class LinksUpdate {
         * @private
         */
        function getExistingCategories() {
-               $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
+               $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
                        array( 'cl_from' => $this->mId ), __METHOD__, $this->mOptions );
                $arr = array();
-               while ( $row = $this->mDb->fetchObject( $res ) ) {
-                       $arr[$row->cl_to] = $row->cl_sortkey;
+               foreach ( $res as $row ) {
+                       $arr[$row->cl_to] = $row->cl_sortkey_prefix;
                }
-               $this->mDb->freeResult( $res );
                return $arr;
        }
 
@@ -708,7 +871,7 @@ class LinksUpdate {
                $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
                        array( 'll_from' => $this->mId ), __METHOD__, $this->mOptions );
                $arr = array();
-               while ( $row = $this->mDb->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $arr[$row->ll_lang] = $row->ll_title;
                }
                return $arr;
@@ -722,13 +885,12 @@ class LinksUpdate {
                $res = $this->mDb->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
                        array( 'iwl_from' => $this->mId ), __METHOD__, $this->mOptions );
                $arr = array();
-               while ( $row = $this->mDb->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        if ( !isset( $arr[$row->iwl_prefix] ) ) {
                                $arr[$row->iwl_prefix] = array();
                        }
                        $arr[$row->iwl_prefix][$row->iwl_title] = 1;
                }
-               $this->mDb->freeResult( $res );
                return $arr;
        }
 
@@ -740,10 +902,9 @@ class LinksUpdate {
                $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
                        array( 'pp_page' => $this->mId ), __METHOD__, $this->mOptions );
                $arr = array();
-               while ( $row = $this->mDb->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $arr[$row->pp_propname] = $row->pp_value;
                }
-               $this->mDb->freeResult( $res );
                return $arr;
        }
 
@@ -754,7 +915,7 @@ class LinksUpdate {
        function getTitle() {
                return $this->mTitle;
        }
-       
+
        /**
         * Return the list of images used as generated by the parser
         */