Revert r35848 per Brion's WONTFIX of bug 14536: "This would just mean that there...
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index 152058a..202ef6e 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /**
  * See docs/deferred.txt
- * 
+ *
  * @todo document (e.g. one-sentence top-level class description).
  */
 class LinksUpdate {
@@ -47,6 +47,7 @@ class LinksUpdate {
                $this->mTitle = $title;
                $this->mId = $title->getArticleID();
 
+               $this->mParserOutput = $parserOutput;
                $this->mLinks = $parserOutput->getLinks();
                $this->mImages = $parserOutput->getImages();
                $this->mTemplates = $parserOutput->getTemplates();
@@ -55,8 +56,8 @@ class LinksUpdate {
                $this->mProperties = $parserOutput->getProperties();
 
                # Convert the format of the interlanguage links
-               # I didn't want to change it in the ParserOutput, because that array is passed all 
-               # the way back to the skin, so either a skin API break would be required, or an 
+               # I didn't want to change it in the ParserOutput, because that array is passed all
+               # the way back to the skin, so either a skin API break would be required, or an
                # inefficient back-conversion.
                $ill = $parserOutput->getLanguageLinks();
                $this->mInterlangs = array();
@@ -66,7 +67,7 @@ class LinksUpdate {
                }
 
                $this->mRecursive = $recursive;
-               
+
                wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
        }
 
@@ -75,7 +76,7 @@ class LinksUpdate {
         */
        function doUpdate() {
                global $wgUseDumbLinkUpdate;
-               
+
                wfRunHooks( 'LinksUpdate', array( &$this ) );
                if ( $wgUseDumbLinkUpdate ) {
                        $this->doDumbUpdate();
@@ -88,7 +89,7 @@ class LinksUpdate {
 
        function doIncrementalUpdate() {
                wfProfileIn( __METHOD__ );
-               
+
                # Page links
                $existing = $this->getExistingLinks();
                $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
@@ -124,8 +125,11 @@ class LinksUpdate {
                        $this->getCategoryInsertions( $existing ) );
 
                # Invalidate all categories which were added, deleted or changed (set symmetric difference)
-               $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
+               $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
+               $categoryDeletes = array_diff_assoc( $existing, $this->mCategories );
+               $categoryUpdates = $categoryInserts + $categoryDeletes;
                $this->invalidateCategories( $categoryUpdates );
+               $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
 
                # Page properties
                $existing = $this->getExistingProperties();
@@ -141,7 +145,7 @@ class LinksUpdate {
                if ( $this->mRecursive ) {
                        $this->queueRecursiveJobs();
                }
-               
+
                wfProfileOut( __METHOD__ );
        }
 
@@ -155,7 +159,9 @@ class LinksUpdate {
 
                # Refresh category pages and image description pages
                $existing = $this->getExistingCategories();
-               $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
+               $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
+               $categoryDeletes = array_diff_assoc( $existing, $this->mCategoties );
+               $categoryUpdates = $categoryInserts + $categoryDeletes;
                $existing = $this->getExistingImages();
                $imageUpdates = array_diff_key( $existing, $this->mImages ) + array_diff_key( $this->mImages, $existing );
 
@@ -164,11 +170,13 @@ class LinksUpdate {
                $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
                $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
                $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
-               $this->dumbTableUpdate( 'langlinks',     $this->getInterlangInsertions(), 'll_from' );
+               $this->dumbTableUpdate( 'langlinks',     $this->getInterlangInsertions(),'ll_from' );
                $this->dumbTableUpdate( 'page_props',    $this->getPropertyInsertions(), 'pp_page' );
 
-               # Update the cache of all the category pages and image description pages which were changed
+               # Update the cache of all the category pages and image description
+               # pages which were changed, and fix the category table count
                $this->invalidateCategories( $categoryUpdates );
+               $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
                $this->invalidateImageDescriptions( $imageUpdates );
 
                # Refresh links of all pages including this page
@@ -182,13 +190,13 @@ class LinksUpdate {
 
        function queueRecursiveJobs() {
                wfProfileIn( __METHOD__ );
-               
+
                $batchSize = 100;
                $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( array( 'templatelinks', 'page' ), 
+               $res = $dbr->select( array( 'templatelinks', 'page' ),
                        array( 'page_namespace', 'page_title' ),
-                       array( 
-                               'page_id=tl_from', 
+                       array(
+                               'page_id=tl_from',
                                'tl_namespace' => $this->mTitle->getNamespace(),
                                'tl_title' => $this->mTitle->getDBkey()
                        ), __METHOD__
@@ -211,7 +219,7 @@ class LinksUpdate {
                $dbr->freeResult( $res );
                wfProfileOut( __METHOD__ );
        }
-       
+
        /**
         * Invalidate the cache of a list of pages from a single namespace
         *
@@ -222,7 +230,7 @@ class LinksUpdate {
                if ( !count( $dbkeys ) ) {
                        return;
                }
-               
+
                /**
                 * Determine which pages need to be updated
                 * This is necessary to prevent the job queue from smashing the DB with
@@ -230,8 +238,8 @@ class LinksUpdate {
                 */
                $now = $this->mDb->timestamp();
                $ids = array();
-               $res = $this->mDb->select( 'page', array( 'page_id' ), 
-                       array( 
+               $res = $this->mDb->select( 'page', array( 'page_id' ),
+                       array(
                                'page_namespace' => $namespace,
                                'page_title IN (' . $this->mDb->makeList( $dbkeys ) . ')',
                                'page_touched < ' . $this->mDb->addQuotes( $now )
@@ -243,14 +251,14 @@ class LinksUpdate {
                if ( !count( $ids ) ) {
                        return;
                }
-               
+
                /**
                 * Do the update
-                * We still need the page_touched condition, in case the row has changed since 
+                * We still need the page_touched condition, in case the row has changed since
                 * the non-locking select above.
                 */
-               $this->mDb->update( 'page', array( 'page_touched' => $now ), 
-                       array( 
+               $this->mDb->update( 'page', array( 'page_touched' => $now ),
+                       array(
                                'page_id IN (' . $this->mDb->makeList( $ids ) . ')',
                                'page_touched < ' . $this->mDb->addQuotes( $now )
                        ), __METHOD__
@@ -261,6 +269,18 @@ class LinksUpdate {
                $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
        }
 
+       /**
+        * Update all the appropriate counts in the category table.
+        * @param $added associative array of category name => sort key
+        * @param $deleted associative array of category name => sort key
+        */
+       function updateCategoryCounts( $added, $deleted ) {
+               $a = new Article($this->mTitle);
+               $a->updateCategoryCounts(
+                       array_keys( $added ), array_keys( $deleted )
+               );
+       }
+
        function invalidateImageDescriptions( $images ) {
                $this->invalidatePages( NS_IMAGE, array_keys( $images ) );
        }
@@ -268,9 +288,9 @@ class LinksUpdate {
        function dumbTableUpdate( $table, $insertions, $fromField ) {
                $this->mDb->delete( $table, array( $fromField => $this->mId ), __METHOD__ );
                if ( count( $insertions ) ) {
-                       # The link array was constructed without FOR UPDATE, so there may be collisions
-                       # This may cause minor link table inconsistencies, which is better than
-                       # crippling the site with lock contention.
+                       # The link array was constructed without FOR UPDATE, so there may
+                       # be collisions.  This may cause minor link table inconsistencies,
+                       # which is better than crippling the site with lock contention.
                        $this->mDb->insert( $table, $insertions, __METHOD__, array( 'IGNORE' ) );
                }
        }
@@ -425,7 +445,7 @@ class LinksUpdate {
 
        /**
         * Get an array of interlanguage link insertions
-        * @param array $existing Array mapping existing language codes to titles        
+        * @param array $existing Array mapping existing language codes to titles
         * @private
         */
        function getInterlangInsertions( $existing = array() ) {
@@ -501,7 +521,7 @@ class LinksUpdate {
                return array_diff_key( $existing, $this->mImages );
        }
 
-       /** 
+       /**
         * Given an array of existing external links, returns those links which are not
         * in $this and thus should be deleted.
         * @private
@@ -519,7 +539,7 @@ class LinksUpdate {
                return array_diff_assoc( $existing, $this->mCategories );
        }
 
-       /** 
+       /**
         * Given an array of existing interlanguage links, returns those links which are not
         * in $this and thus should be deleted.
         * @private
@@ -618,12 +638,12 @@ class LinksUpdate {
        }
 
        /**
-        * Get an array of existing interlanguage links, with the language code in the key and the 
+        * Get an array of existing interlanguage links, with the language code in the key and the
         * title in the value.
         * @private
         */
        function getExistingInterlangs() {
-               $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ), 
+               $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 ) ) {
@@ -647,10 +667,10 @@ class LinksUpdate {
                return $arr;
        }
 
-       
+
        /**
         * Return the title object of the page being updated
-        */     
+        */
        function getTitle() {
                return $this->mTitle;
        }
@@ -675,4 +695,3 @@ class LinksUpdate {
                }
        }
 }
-