From fc8c39baa50b664a9ed23f8826e17a1a62bb8414 Mon Sep 17 00:00:00 2001 From: Arlo Breault Date: Mon, 10 Jan 2022 16:22:30 -0500 Subject: [PATCH] Fix lint error updating The article id of the title is set to 0 when the page is deleted so, although the lint job from the hook runs, it doesn't remove anything. Reverts most of I06b821b65f65609ddac8ed4e7c662336082d8266 Bug: T298782 Bug: T170313 Change-Id: I2610b9b16d4032b0e18b3537cc9ed51bfdaff299 --- includes/Database.php | 53 ++++++++++++++++++++++++++++++++ includes/Hooks.php | 10 +++--- includes/RecordLintJob.php | 62 ++------------------------------------ 3 files changed, 60 insertions(+), 65 deletions(-) diff --git a/includes/Database.php b/includes/Database.php index c79d86a4..01c0ebfa 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -22,6 +22,8 @@ namespace MediaWiki\Linter; use FormatJson; use MediaWiki\Logger\LoggerFactory; +use MediaWiki\MediaWikiServices; +use WikiMap; /** * Database logic @@ -319,4 +321,55 @@ class Database { return $ret; } + /** + * Send stats to statsd and update totals cache + * + * @param array $changes + */ + public function updateStats( array $changes ) { + global $wgLinterStatsdSampleFactor; + + $mwServices = MediaWikiServices::getInstance(); + + $totalsLookup = new TotalsLookup( + new CategoryManager(), + $mwServices->getMainWANObjectCache() + ); + + if ( $wgLinterStatsdSampleFactor === false ) { + // Don't send to statsd, but update cache with $changes + $raw = $changes['added']; + foreach ( $changes['deleted'] as $cat => $count ) { + if ( isset( $raw[$cat] ) ) { + $raw[$cat] -= $count; + } else { + // Negative value + $raw[$cat] = 0 - $count; + } + } + + foreach ( $raw as $cat => $count ) { + if ( $count != 0 ) { + // There was a change in counts, invalidate the cache + $totalsLookup->touchCategoryCache( $cat ); + } + } + return; + } elseif ( mt_rand( 1, $wgLinterStatsdSampleFactor ) != 1 ) { + return; + } + + $totals = $this->getTotals(); + $wiki = WikiMap::getCurrentWikiId(); + + $stats = $mwServices->getStatsdDataFactory(); + foreach ( $totals as $name => $count ) { + $stats->gauge( "linter.category.$name.$wiki", $count ); + } + + $stats->gauge( "linter.totals.$wiki", array_sum( $totals ) ); + + $totalsLookup->touchAllCategoriesCache(); + } + } diff --git a/includes/Hooks.php b/includes/Hooks.php index 450293ea..3b196762 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -96,12 +96,10 @@ class Hooks { public static function onWikiPageDeletionUpdates( WikiPage $wikiPage, Content $content, array &$updates ) { - $title = $wikiPage->getTitle(); - $updates[] = new MWCallableUpdate( static function () use ( $title ) { - $job = new RecordLintJob( - $title, [ 'errors' => [] ] - ); - $job->run(); + $id = $wikiPage->getId(); + $updates[] = new MWCallableUpdate( static function () use ( $id ) { + $database = new Database( $id ); + $database->updateStats( $database->setForPage( [] ) ); }, __METHOD__ ); } diff --git a/includes/RecordLintJob.php b/includes/RecordLintJob.php index 65cd0119..ee8f6b7d 100644 --- a/includes/RecordLintJob.php +++ b/includes/RecordLintJob.php @@ -21,9 +21,7 @@ namespace MediaWiki\Linter; use Job; -use MediaWiki\MediaWikiServices; use Title; -use WikiMap; class RecordLintJob extends Job { /** @@ -36,9 +34,7 @@ class RecordLintJob extends Job { } public function run() { - if ( isset( $this->params['revision'] ) - && $this->title->getLatestRevID() != $this->params['revision'] - ) { + if ( $this->title->getLatestRevID() != $this->params['revision'] ) { // Outdated now, let a later job handle it return true; } @@ -56,63 +52,11 @@ class RecordLintJob extends Job { // (e.g. same category of error in same template) $errors[$error->id()] = $error; } + $lintDb = new Database( $this->title->getArticleID() ); - $changes = $lintDb->setForPage( $errors ); - $this->updateStats( $lintDb, $changes ); + $lintDb->updateStats( $lintDb->setForPage( $errors ) ); return true; } - /** - * Send stats to statsd and update totals cache - * - * @param Database $lintDb - * @param array $changes - */ - protected function updateStats( Database $lintDb, array $changes ) { - global $wgLinterStatsdSampleFactor; - - $mwServices = MediaWikiServices::getInstance(); - - $totalsLookup = new TotalsLookup( - new CategoryManager(), - $mwServices->getMainWANObjectCache() - ); - - if ( $wgLinterStatsdSampleFactor === false ) { - // Don't send to statsd, but update cache with $changes - $raw = $changes['added']; - foreach ( $changes['deleted'] as $cat => $count ) { - if ( isset( $raw[$cat] ) ) { - $raw[$cat] -= $count; - } else { - // Negative value - $raw[$cat] = 0 - $count; - } - } - - foreach ( $raw as $cat => $count ) { - if ( $count != 0 ) { - // There was a change in counts, invalidate the cache - $totalsLookup->touchCategoryCache( $cat ); - } - } - return; - } elseif ( mt_rand( 1, $wgLinterStatsdSampleFactor ) != 1 ) { - return; - } - - $totals = $lintDb->getTotals(); - $wiki = WikiMap::getCurrentWikiId(); - - $stats = $mwServices->getStatsdDataFactory(); - foreach ( $totals as $name => $count ) { - $stats->gauge( "linter.category.$name.$wiki", $count ); - } - - $stats->gauge( "linter.totals.$wiki", array_sum( $totals ) ); - - $totalsLookup->touchAllCategoriesCache(); - } - }