From 261339c2a3f9e043f4199ded6109cbad4f6c8e08 Mon Sep 17 00:00:00 2001 From: Arlo Breault Date: Thu, 11 Apr 2024 12:24:42 -0400 Subject: [PATCH] Inject Database into TotalsLookup Change-Id: I01e6b89b4ce9b1cea241bba9cad7ef6673803166 --- extension.json | 6 ++---- includes/ApiQueryLinterStats.php | 8 ++------ includes/Hooks.php | 4 ++-- includes/RecordLintJob.php | 1 - includes/ServiceWiring.php | 3 ++- includes/SpecialLintErrors.php | 8 ++------ includes/TotalsLookup.php | 21 +++++++++++---------- tests/phpunit/SpecialLintErrorsTest.php | 3 +-- 8 files changed, 22 insertions(+), 32 deletions(-) diff --git a/extension.json b/extension.json index bcc97510..1eb6ef23 100644 --- a/extension.json +++ b/extension.json @@ -60,8 +60,7 @@ "linterstats": { "class": "MediaWiki\\Linter\\ApiQueryLinterStats", "services": [ - "Linter.TotalsLookup", - "Linter.Database" + "Linter.TotalsLookup" ] } }, @@ -74,8 +73,7 @@ "LinkCache", "PermissionManager", "Linter.CategoryManager", - "Linter.TotalsLookup", - "Linter.Database" + "Linter.TotalsLookup" ] } }, diff --git a/includes/ApiQueryLinterStats.php b/includes/ApiQueryLinterStats.php index 1a5cba17..011fdb59 100644 --- a/includes/ApiQueryLinterStats.php +++ b/includes/ApiQueryLinterStats.php @@ -26,30 +26,26 @@ use ApiResult; class ApiQueryLinterStats extends ApiQueryBase { private TotalsLookup $totalsLookup; - private Database $database; /** * @param ApiQuery $queryModule * @param string $moduleName * @param TotalsLookup $totalsLookup - * @param Database $database */ public function __construct( ApiQuery $queryModule, string $moduleName, - TotalsLookup $totalsLookup, - Database $database + TotalsLookup $totalsLookup ) { parent::__construct( $queryModule, $moduleName, 'lntrst' ); $this->totalsLookup = $totalsLookup; - $this->database = $database; } /** * Add totals to output */ public function execute() { - $totals = $this->totalsLookup->getTotals( $this->database ); + $totals = $this->totalsLookup->getTotals(); ApiResult::setArrayType( $totals, 'assoc' ); $this->getResult()->addValue( [ 'query', 'linterstats' ], 'totals', $totals ); } diff --git a/includes/Hooks.php b/includes/Hooks.php index 272e5ca5..42060643 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -121,7 +121,7 @@ class Hooks implements public function onWikiPageDeletionUpdates( $wikiPage, $content, &$updates ) { $updates[] = new MWCallableUpdate( function () use ( $wikiPage ) { $this->totalsLookup->updateStats( - $this->database, $this->database->setForPage( + $this->database->setForPage( $wikiPage->getId(), $wikiPage->getNamespace(), [] ) ); @@ -159,7 +159,7 @@ class Hooks implements !in_array( $wikiPage->getContentModel(), self::LINTABLE_CONTENT_MODELS ) ) ) { $this->totalsLookup->updateStats( - $this->database, $this->database->setForPage( + $this->database->setForPage( $wikiPage->getId(), $wikiPage->getNamespace(), [] ) ); diff --git a/includes/RecordLintJob.php b/includes/RecordLintJob.php index 1d24ba18..952c451e 100644 --- a/includes/RecordLintJob.php +++ b/includes/RecordLintJob.php @@ -70,7 +70,6 @@ class RecordLintJob extends Job { } $this->totalsLookup->updateStats( - $this->database, $this->database->setForPage( $this->title->getArticleID(), $this->title->getNamespace(), diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index 291d80e3..3985ed9f 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -53,7 +53,8 @@ return [ ), $services->getMainWANObjectCache(), $services->getStatsdDataFactory(), - $services->get( 'Linter.CategoryManager' ) + $services->get( 'Linter.CategoryManager' ), + $services->get( 'Linter.Database' ) ); }, ]; diff --git a/includes/SpecialLintErrors.php b/includes/SpecialLintErrors.php index a3e0ba50..9c650363 100644 --- a/includes/SpecialLintErrors.php +++ b/includes/SpecialLintErrors.php @@ -39,7 +39,6 @@ class SpecialLintErrors extends SpecialPage { private PermissionManager $permissionManager; private CategoryManager $categoryManager; private TotalsLookup $totalsLookup; - private Database $database; /** * @var string|null @@ -53,7 +52,6 @@ class SpecialLintErrors extends SpecialPage { * @param PermissionManager $permissionManager * @param CategoryManager $categoryManager * @param TotalsLookup $totalsLookup - * @param Database $database */ public function __construct( NamespaceInfo $namespaceInfo, @@ -61,8 +59,7 @@ class SpecialLintErrors extends SpecialPage { LinkCache $linkCache, PermissionManager $permissionManager, CategoryManager $categoryManager, - TotalsLookup $totalsLookup, - Database $database + TotalsLookup $totalsLookup ) { parent::__construct( 'LintErrors' ); $this->namespaceInfo = $namespaceInfo; @@ -71,7 +68,6 @@ class SpecialLintErrors extends SpecialPage { $this->permissionManager = $permissionManager; $this->categoryManager = $categoryManager; $this->totalsLookup = $totalsLookup; - $this->database = $database; } /** @@ -345,7 +341,7 @@ class SpecialLintErrors extends SpecialPage { } private function showCategoryListings() { - $totals = $this->totalsLookup->getTotals( $this->database ); + $totals = $this->totalsLookup->getTotals(); // Display lint issues by priority $this->displayList( 'high', $totals, $this->categoryManager->getHighPriority() ); diff --git a/includes/TotalsLookup.php b/includes/TotalsLookup.php index 3db6e803..fc21513c 100644 --- a/includes/TotalsLookup.php +++ b/includes/TotalsLookup.php @@ -39,24 +39,28 @@ class TotalsLookup { private WANObjectCache $cache; private IBufferingStatsdDataFactory $statsdDataFactory; private CategoryManager $categoryManager; + private Database $database; /** * @param ServiceOptions $options * @param WANObjectCache $cache * @param IBufferingStatsdDataFactory $statsdDataFactory * @param CategoryManager $categoryManager + * @param Database $database */ public function __construct( ServiceOptions $options, WANObjectCache $cache, IBufferingStatsdDataFactory $statsdDataFactory, - CategoryManager $categoryManager + CategoryManager $categoryManager, + Database $database ) { $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); $this->options = $options; $this->cache = $cache; $this->statsdDataFactory = $statsdDataFactory; $this->categoryManager = $categoryManager; + $this->database = $database; } /** @@ -70,10 +74,9 @@ class TotalsLookup { /** * Get the totals for every category in the database * - * @param Database $db * @return array */ - public function getTotals( Database $db ): array { + public function getTotals(): array { $cats = $this->categoryManager->getVisibleCategories(); $fetchedTotals = false; $totals = []; @@ -81,12 +84,12 @@ class TotalsLookup { $totals[$cat] = $this->cache->getWithSetCallback( $this->makeKey( $cat ), WANObjectCache::TTL_INDEFINITE, - static function ( $oldValue, &$ttl, &$setOpts, $oldAsOf ) use ( $cat, $db, &$fetchedTotals ) { + function ( $oldValue, &$ttl, &$setOpts, $oldAsOf ) use ( $cat, &$fetchedTotals ) { $setOpts += MWDatabase::getCacheSetOptions( - $db->getDBConnectionRef( DB_REPLICA ) + $this->database->getDBConnectionRef( DB_REPLICA ) ); if ( $fetchedTotals === false ) { - $fetchedTotals = $db->getTotals(); + $fetchedTotals = $this->database->getTotals(); } return $fetchedTotals[$cat]; }, @@ -99,17 +102,15 @@ class TotalsLookup { ] ); } - return $totals; } /** * Send stats to statsd and update totals cache * - * @param Database $db * @param array $changes */ - public function updateStats( Database $db, array $changes ) { + public function updateStats( array $changes ) { $linterStatsdSampleFactor = $this->options->get( 'LinterStatsdSampleFactor' ); if ( $linterStatsdSampleFactor === false ) { @@ -135,7 +136,7 @@ class TotalsLookup { return; } - $totals = $db->getTotals(); + $totals = $this->database->getTotals(); $wiki = WikiMap::getCurrentWikiId(); $stats = $this->statsdDataFactory; foreach ( $totals as $name => $count ) { diff --git a/tests/phpunit/SpecialLintErrorsTest.php b/tests/phpunit/SpecialLintErrorsTest.php index b3ee6c6e..44e0d5a0 100644 --- a/tests/phpunit/SpecialLintErrorsTest.php +++ b/tests/phpunit/SpecialLintErrorsTest.php @@ -58,8 +58,7 @@ class SpecialLintErrorsTest extends SpecialPageTestBase { $services->getLinkCache(), $services->getPermissionManager(), $services->get( 'Linter.CategoryManager' ), - $services->get( 'Linter.TotalsLookup' ), - $this->getDatabase() + $services->get( 'Linter.TotalsLookup' ) ); }