Inject Database into TotalsLookup

Change-Id: I01e6b89b4ce9b1cea241bba9cad7ef6673803166
This commit is contained in:
Arlo Breault 2024-04-11 12:24:42 -04:00
parent ffc266eae6
commit 261339c2a3
8 changed files with 22 additions and 32 deletions

View file

@ -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"
]
}
},

View file

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

View file

@ -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(), []
)
);

View file

@ -70,7 +70,6 @@ class RecordLintJob extends Job {
}
$this->totalsLookup->updateStats(
$this->database,
$this->database->setForPage(
$this->title->getArticleID(),
$this->title->getNamespace(),

View file

@ -53,7 +53,8 @@ return [
),
$services->getMainWANObjectCache(),
$services->getStatsdDataFactory(),
$services->get( 'Linter.CategoryManager' )
$services->get( 'Linter.CategoryManager' ),
$services->get( 'Linter.Database' )
);
},
];

View file

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

View file

@ -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 ) {

View file

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