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
This commit is contained in:
Arlo Breault 2022-01-10 16:22:30 -05:00
parent 2218e87c05
commit fc8c39baa5
3 changed files with 60 additions and 65 deletions

View file

@ -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();
}
}

View file

@ -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__ );
}

View file

@ -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();
}
}