Drop DatabaseFactory, just have Database as the service

Change-Id: Id25271c82bc7ba833d32dff3fb11d3dfe15a3f02
This commit is contained in:
Arlo Breault 2024-04-10 21:20:43 -04:00
parent c04b075858
commit ffc266eae6
12 changed files with 52 additions and 124 deletions

View file

@ -32,7 +32,7 @@
"JobQueueGroup",
"Linter.CategoryManager",
"Linter.TotalsLookup",
"Linter.DatabaseFactory"
"Linter.Database"
]
},
"schema": {
@ -61,7 +61,7 @@
"class": "MediaWiki\\Linter\\ApiQueryLinterStats",
"services": [
"Linter.TotalsLookup",
"Linter.DatabaseFactory"
"Linter.Database"
]
}
},
@ -75,7 +75,7 @@
"PermissionManager",
"Linter.CategoryManager",
"Linter.TotalsLookup",
"Linter.DatabaseFactory"
"Linter.Database"
]
}
},
@ -84,7 +84,7 @@
"class": "MediaWiki\\Linter\\RecordLintJob",
"services": [
"Linter.TotalsLookup",
"Linter.DatabaseFactory"
"Linter.Database"
]
}
},

View file

@ -26,30 +26,30 @@ use ApiResult;
class ApiQueryLinterStats extends ApiQueryBase {
private TotalsLookup $totalsLookup;
private DatabaseFactory $databaseFactory;
private Database $database;
/**
* @param ApiQuery $queryModule
* @param string $moduleName
* @param TotalsLookup $totalsLookup
* @param DatabaseFactory $databaseFactory
* @param Database $database
*/
public function __construct(
ApiQuery $queryModule,
string $moduleName,
TotalsLookup $totalsLookup,
DatabaseFactory $databaseFactory
Database $database
) {
parent::__construct( $queryModule, $moduleName, 'lntrst' );
$this->totalsLookup = $totalsLookup;
$this->databaseFactory = $databaseFactory;
$this->database = $database;
}
/**
* Add totals to output
*/
public function execute() {
$totals = $this->totalsLookup->getTotals( $this->databaseFactory->newDatabase() );
$totals = $this->totalsLookup->getTotals( $this->database );
ApiResult::setArrayType( $totals, 'assoc' );
$this->getResult()->addValue( [ 'query', 'linterstats' ], 'totals', $totals );
}

View file

@ -1,61 +0,0 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Linter;
use MediaWiki\Config\ServiceOptions;
use Wikimedia\Rdbms\LBFactory;
/**
* Create a Database helper.
*/
class DatabaseFactory {
private ServiceOptions $options;
private CategoryManager $categoryManager;
private LBFactory $dbLoadBalancerFactory;
/**
* @param ServiceOptions $options
* @param CategoryManager $categoryManager
* @param LBFactory $dbLoadBalancerFactory
*/
public function __construct(
ServiceOptions $options,
CategoryManager $categoryManager,
LBFactory $dbLoadBalancerFactory
) {
$this->options = $options;
$this->categoryManager = $categoryManager;
$this->dbLoadBalancerFactory = $dbLoadBalancerFactory;
}
/**
* Create a new Database helper.
*
* @return Database
*/
public function newDatabase(): Database {
return new Database(
$this->options,
$this->categoryManager,
$this->dbLoadBalancerFactory
);
}
}

View file

@ -53,27 +53,27 @@ class Hooks implements
private JobQueueGroup $jobQueueGroup;
private CategoryManager $categoryManager;
private TotalsLookup $totalsLookup;
private DatabaseFactory $databaseFactory;
private Database $database;
/**
* @param LinkRenderer $linkRenderer
* @param JobQueueGroup $jobQueueGroup
* @param CategoryManager $categoryManager
* @param TotalsLookup $totalsLookup
* @param DatabaseFactory $databaseFactory
* @param Database $database
*/
public function __construct(
LinkRenderer $linkRenderer,
JobQueueGroup $jobQueueGroup,
CategoryManager $categoryManager,
TotalsLookup $totalsLookup,
DatabaseFactory $databaseFactory
Database $database
) {
$this->linkRenderer = $linkRenderer;
$this->jobQueueGroup = $jobQueueGroup;
$this->categoryManager = $categoryManager;
$this->totalsLookup = $totalsLookup;
$this->databaseFactory = $databaseFactory;
$this->database = $database;
}
/**
@ -96,8 +96,7 @@ class Hooks implements
return;
}
$database = $this->databaseFactory->newDatabase();
$lintError = $database->getFromId( $lintId );
$lintError = $this->database->getFromId( $lintId );
if ( !$lintError ) {
// Already fixed or bogus URL parameter?
return;
@ -121,9 +120,8 @@ class Hooks implements
*/
public function onWikiPageDeletionUpdates( $wikiPage, $content, &$updates ) {
$updates[] = new MWCallableUpdate( function () use ( $wikiPage ) {
$database = $this->databaseFactory->newDatabase();
$this->totalsLookup->updateStats(
$database, $database->setForPage(
$this->database, $this->database->setForPage(
$wikiPage->getId(), $wikiPage->getNamespace(), []
)
);
@ -160,9 +158,8 @@ class Hooks implements
( in_array( "mw-contentmodelchange", $tags ) &&
!in_array( $wikiPage->getContentModel(), self::LINTABLE_CONTENT_MODELS ) )
) {
$database = $this->databaseFactory->newDatabase();
$this->totalsLookup->updateStats(
$database, $database->setForPage(
$this->database, $this->database->setForPage(
$wikiPage->getId(), $wikiPage->getNamespace(), []
)
);
@ -199,8 +196,7 @@ class Hooks implements
if ( !$pageId ) {
return;
}
$database = $this->databaseFactory->newDatabase();
$totals = array_filter( $database->getTotalsForPage( $pageId ) );
$totals = array_filter( $this->database->getTotalsForPage( $pageId ) );
if ( !$totals ) {
// No errors, yay!
return;
@ -282,7 +278,7 @@ class Hooks implements
$job = new RecordLintJob( $title, [
'errors' => $errors,
'revision' => $revision,
], $this->totalsLookup, $this->databaseFactory );
], $this->totalsLookup, $this->database );
$this->jobQueueGroup->push( $job );
return true;
}

View file

@ -25,24 +25,24 @@ use MediaWiki\Page\PageReference;
class RecordLintJob extends Job {
private TotalsLookup $totalsLookup;
private DatabaseFactory $databaseFactory;
private Database $database;
/**
* RecordLintJob constructor.
* @param PageReference $page
* @param array $params
* @param TotalsLookup $totalsLookup
* @param DatabaseFactory $databaseFactory
* @param Database $database
*/
public function __construct(
PageReference $page,
array $params,
TotalsLookup $totalsLookup,
DatabaseFactory $databaseFactory
Database $database
) {
parent::__construct( 'RecordLintJob', $page, $params );
$this->totalsLookup = $totalsLookup;
$this->databaseFactory = $databaseFactory;
$this->database = $database;
}
public function run() {
@ -69,10 +69,9 @@ class RecordLintJob extends Job {
$errors[$error->id()] = $error;
}
$lintDb = $this->databaseFactory->newDatabase();
$this->totalsLookup->updateStats(
$lintDb,
$lintDb->setForPage(
$this->database,
$this->database->setForPage(
$this->title->getArticleID(),
$this->title->getNamespace(),
$errors

View file

@ -35,8 +35,8 @@ return [
$services->getMainConfig()->get( 'LinterCategories' )
);
},
'Linter.DatabaseFactory' => static function ( MediaWikiServices $services ): DatabaseFactory {
return new DatabaseFactory(
'Linter.Database' => static function ( MediaWikiServices $services ): Database {
return new Database(
new ServiceOptions(
Database::CONSTRUCTOR_OPTIONS,
$services->getMainConfig()

View file

@ -39,7 +39,7 @@ class SpecialLintErrors extends SpecialPage {
private PermissionManager $permissionManager;
private CategoryManager $categoryManager;
private TotalsLookup $totalsLookup;
private DatabaseFactory $databaseFactory;
private Database $database;
/**
* @var string|null
@ -53,7 +53,7 @@ class SpecialLintErrors extends SpecialPage {
* @param PermissionManager $permissionManager
* @param CategoryManager $categoryManager
* @param TotalsLookup $totalsLookup
* @param DatabaseFactory $databaseFactory
* @param Database $database
*/
public function __construct(
NamespaceInfo $namespaceInfo,
@ -62,7 +62,7 @@ class SpecialLintErrors extends SpecialPage {
PermissionManager $permissionManager,
CategoryManager $categoryManager,
TotalsLookup $totalsLookup,
DatabaseFactory $databaseFactory
Database $database
) {
parent::__construct( 'LintErrors' );
$this->namespaceInfo = $namespaceInfo;
@ -71,7 +71,7 @@ class SpecialLintErrors extends SpecialPage {
$this->permissionManager = $permissionManager;
$this->categoryManager = $categoryManager;
$this->totalsLookup = $totalsLookup;
$this->databaseFactory = $databaseFactory;
$this->database = $database;
}
/**
@ -345,7 +345,7 @@ class SpecialLintErrors extends SpecialPage {
}
private function showCategoryListings() {
$totals = $this->totalsLookup->getTotals( $this->databaseFactory->newDatabase() );
$totals = $this->totalsLookup->getTotals( $this->database );
// Display lint issues by priority
$this->displayList( 'high', $totals, $this->categoryManager->getHighPriority() );

View file

@ -59,7 +59,7 @@ class MigrateNamespace extends LoggedUpdateMaintenance {
$this->output( "Migrating the page table page_namespace field to the linter table...\n" );
$database = $this->getServiceContainer()->get( 'Linter.DatabaseFactory' )->newDatabase();
$database = $this->getServiceContainer()->get( 'Linter.Database' );
$updated = $database->migrateNamespace( $batchSize, $batchSize, $sleep, false );
$this->output( "Completed migration of page_namespace data to the linter table, $updated rows updated.\n" );

View file

@ -57,7 +57,7 @@ class MigrateTagTemplate extends LoggedUpdateMaintenance {
$this->output( "Migrating the linter_params field to the linter_tag and linter_template fields...\n" );
$database = $this->getServiceContainer()->get( 'Linter.DatabaseFactory' )->newDatabase();
$database = $this->getServiceContainer()->get( 'Linter.Database' );
$updated = $database->migrateTemplateAndTagInfo( $batchSize, $sleep, false );
$this->output(

View file

@ -29,14 +29,12 @@ use MediaWikiIntegrationTestCase;
* @covers \MediaWiki\Linter\Database
*/
class DatabaseTest extends MediaWikiIntegrationTestCase {
private function newDatabase() {
$services = $this->getServiceContainer();
$databaseFactory = $services->get( 'Linter.DatabaseFactory' );
return $databaseFactory->newDatabase();
private function getDatabase() {
return $this->getServiceContainer()->get( 'Linter.Database' );
}
public function testConstructor() {
$this->assertInstanceOf( Database::class, $this->newDatabase() );
$this->assertInstanceOf( Database::class, $this->getDatabase() );
}
private function getDummyLintErrors() {
@ -84,7 +82,7 @@ class DatabaseTest extends MediaWikiIntegrationTestCase {
public function testSetForPage() {
$pageId = 5;
$namespaceId = 0;
$lintDb = $this->newDatabase();
$lintDb = $this->getDatabase();
$dummyErrors = $this->getDummyLintErrors();
$result = $lintDb->setForPage( $pageId, $namespaceId, $dummyErrors );

View file

@ -33,10 +33,8 @@ use Wikimedia\Rdbms\SelectQueryBuilder;
*/
class RecordLintJobTest extends MediaWikiIntegrationTestCase {
private function newDatabase() {
$services = $this->getServiceContainer();
$databaseFactory = $services->get( 'Linter.DatabaseFactory' );
return $databaseFactory->newDatabase();
private function getDatabase() {
return $this->getServiceContainer()->get( 'Linter.Database' );
}
private function newRecordLintJob( PageReference $page, array $params ) {
@ -45,7 +43,7 @@ class RecordLintJobTest extends MediaWikiIntegrationTestCase {
$page,
$params,
$services->get( 'Linter.TotalsLookup' ),
$services->get( 'Linter.DatabaseFactory' )
$this->getDatabase()
);
}
@ -126,7 +124,7 @@ class RecordLintJobTest extends MediaWikiIntegrationTestCase {
'revision' => $titleAndPage[ 'revID' ]
] );
$this->assertTrue( $job->run() );
$db = $this->newDatabase();
$db = $this->getDatabase();
$errorsFromDb = array_values( $db->getForPage( $titleAndPage[ 'pageID' ] ) );
$this->assertCount( 1, $errorsFromDb );
$this->assertInstanceOf( LintError::class, $errorsFromDb[ 0 ] );
@ -153,7 +151,7 @@ class RecordLintJobTest extends MediaWikiIntegrationTestCase {
] );
$this->assertTrue( $job->run() );
$pageId = $titleAndPage[ 'pageID' ];
$db = $this->newDatabase();
$db = $this->getDatabase();
$errorsFromDb = array_values( $db->getForPage( $pageId ) );
$this->assertCount( 1, $errorsFromDb );
$this->assertInstanceOf( LintError::class, $errorsFromDb[0] );
@ -193,7 +191,7 @@ class RecordLintJobTest extends MediaWikiIntegrationTestCase {
] );
$this->assertTrue( $job->run() );
$pageId = $titleAndPage[ 'pageID' ];
$db = $this->newDatabase();
$db = $this->getDatabase();
$errorsFromDb = array_values( $db->getForPage( $pageId ) );
$this->assertCount( 1, $errorsFromDb );
$this->assertInstanceOf( LintError::class, $errorsFromDb[0] );
@ -271,7 +269,7 @@ class RecordLintJobTest extends MediaWikiIntegrationTestCase {
$this->assertNull( $namespace );
// migrate unpopulated namespace_id(s) from the page table to linter table
$database = $this->newDatabase();
$database = $this->getDatabase();
$database->migrateNamespace( 2, 3, 0, true );
// Verify all linter records now have proper namespace IDs in the linter_namespace field
@ -380,7 +378,7 @@ class RecordLintJobTest extends MediaWikiIntegrationTestCase {
$this->assertSame( "", $template );
// Migrate unpopulated tag and template info from the params field
$database = $this->newDatabase();
$database = $this->getDatabase();
$database->migrateTemplateAndTagInfo( 3, 0, true );
// Verify all linter records have the proper tag and template field info migrated from the params field
@ -423,7 +421,7 @@ class RecordLintJobTest extends MediaWikiIntegrationTestCase {
'revision' => $titleAndPage[ 'revID' ]
] );
$this->assertTrue( $job->run() );
$errorsFromDb = array_values( $this->newDatabase()->getForPage( $titleAndPage['pageID'] ) );
$errorsFromDb = array_values( $this->getDatabase()->getForPage( $titleAndPage['pageID'] ) );
$this->assertCount( 0, $errorsFromDb );
}
}

View file

@ -36,10 +36,8 @@ use SpecialPageTestBase;
*/
class SpecialLintErrorsTest extends SpecialPageTestBase {
private function newDatabase() {
$services = $this->getServiceContainer();
$databaseFactory = $services->get( 'Linter.DatabaseFactory' );
return $databaseFactory->newDatabase();
private function getDatabase() {
return $this->getServiceContainer()->get( 'Linter.Database' );
}
private function newRecordLintJob( PageReference $page, array $params ) {
@ -48,7 +46,7 @@ class SpecialLintErrorsTest extends SpecialPageTestBase {
$page,
$params,
$services->get( 'Linter.TotalsLookup' ),
$services->get( 'Linter.DatabaseFactory' )
$this->getDatabase()
);
}
@ -61,7 +59,7 @@ class SpecialLintErrorsTest extends SpecialPageTestBase {
$services->getPermissionManager(),
$services->get( 'Linter.CategoryManager' ),
$services->get( 'Linter.TotalsLookup' ),
$services->get( 'Linter.DatabaseFactory' )
$this->getDatabase()
);
}
@ -123,7 +121,7 @@ class SpecialLintErrorsTest extends SpecialPageTestBase {
$this->assertTrue( $job->run() );
$pageId = $titleAndPage['pageID'];
$db = $this->newDatabase();
$db = $this->getDatabase();
$errorsFromDb = array_values( $db->getForPage( $pageId ) );
$this->assertCount( 1, $errorsFromDb );
@ -160,7 +158,7 @@ class SpecialLintErrorsTest extends SpecialPageTestBase {
$this->assertTrue( $job->run() );
$pageId = $titleAndPage['pageID'];
$db = $this->newDatabase();
$db = $this->getDatabase();
$errorsFromDb = array_values( $db->getForPage( $pageId ) );
$this->assertCount( 1, $errorsFromDb );