Reduce frequency of refreshCounts() calls in LinksDeletionUpdate
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 12 Jun 2018 00:23:49 +0000 (17:23 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Tue, 12 Jun 2018 17:50:52 +0000 (17:50 +0000)
Bug: T195397
Change-Id: I0a39c735ec516b70c43c7a40583c43289550b687

includes/Category.php
includes/deferred/LinksDeletionUpdate.php

index 46b86d8..fb5916b 100644 (file)
@@ -48,7 +48,7 @@ class Category {
 
        /**
         * Set up all member variables using a database query.
-        * @param int $mode
+        * @param int $mode One of (Category::LOAD_ONLY, Category::LAZY_INIT_ROW)
         * @throws MWException
         * @return bool True on success, false on failure.
         */
index 3c86d11..9f6257c 100644 (file)
@@ -90,6 +90,7 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate {
                foreach ( $catBatches as $catBatch ) {
                        $this->page->updateCategoryCounts( [], $catBatch, $id );
                        if ( count( $catBatches ) > 1 ) {
+                               // Only sacrifice atomicity if necessary due to size
                                $lbFactory->commitAndWaitForReplication(
                                        __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
                                );
@@ -98,19 +99,10 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate {
 
                // Refresh counts on categories that should be empty now
                if ( $title->getNamespace() === NS_CATEGORY ) {
-                       $row = $dbw->selectRow(
-                               'category',
-                               [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
-                               [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 100' ],
-                               __METHOD__
-                       );
-                       if ( $row ) {
-                               $cat = Category::newFromRow( $row, $title );
-                               // T166757: do the update after the main job DB commit
-                               DeferredUpdates::addCallableUpdate( function () use ( $cat ) {
-                                       $cat->refreshCounts();
-                               } );
-                       }
+                       // T166757: do the update after the main job DB commit
+                       DeferredUpdates::addCallableUpdate( function () use ( $title ) {
+                               $this->refreshCategoryIfEmpty( $title );
+                       } );
                }
 
                $this->batchDeleteByPK(
@@ -195,6 +187,35 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate {
                ScopedCallback::consume( $scopedLock );
        }
 
+       /**
+        * @param Title $title
+        */
+       private function refreshCategoryIfEmpty( Title $title ) {
+               $dbw = $this->getDB();
+
+               $row = $dbw->selectRow(
+                       'category',
+                       [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
+                       [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 100' ],
+                       __METHOD__
+               );
+
+               if ( !$row ) {
+                       return; // nothing to delete
+               }
+
+               $cat = Category::newFromRow( $row, $title );
+               $hasLink = $dbw->selectField(
+                       'categorylinks',
+                       '1',
+                       [ 'cl_to' => $title->getDBkey() ],
+                       __METHOD__
+               );
+               if ( !$hasLink ) {
+                       $cat->refreshCounts(); // delete the category table entry
+               }
+       }
+
        private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
                $services = MediaWikiServices::getInstance();
                $lbFactory = $services->getDBLoadBalancerFactory();