diff --git a/extension.json b/extension.json index d0ca75b9..bcc97510 100644 --- a/extension.json +++ b/extension.json @@ -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" ] } }, diff --git a/includes/ApiQueryLinterStats.php b/includes/ApiQueryLinterStats.php index b0218e43..1a5cba17 100644 --- a/includes/ApiQueryLinterStats.php +++ b/includes/ApiQueryLinterStats.php @@ -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 ); } diff --git a/includes/DatabaseFactory.php b/includes/DatabaseFactory.php deleted file mode 100644 index fe51ec92..00000000 --- a/includes/DatabaseFactory.php +++ /dev/null @@ -1,61 +0,0 @@ -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 - ); - } -} diff --git a/includes/Hooks.php b/includes/Hooks.php index bbe163ec..272e5ca5 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -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; } diff --git a/includes/RecordLintJob.php b/includes/RecordLintJob.php index 44302fce..1d24ba18 100644 --- a/includes/RecordLintJob.php +++ b/includes/RecordLintJob.php @@ -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 diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index 85d2667e..291d80e3 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -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() diff --git a/includes/SpecialLintErrors.php b/includes/SpecialLintErrors.php index 01a3318e..a3e0ba50 100644 --- a/includes/SpecialLintErrors.php +++ b/includes/SpecialLintErrors.php @@ -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() ); diff --git a/maintenance/migrateNamespace.php b/maintenance/migrateNamespace.php index 369dcfcc..8b31ffd1 100644 --- a/maintenance/migrateNamespace.php +++ b/maintenance/migrateNamespace.php @@ -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" ); diff --git a/maintenance/migrateTagTemplate.php b/maintenance/migrateTagTemplate.php index 2e50d2d7..2027de75 100644 --- a/maintenance/migrateTagTemplate.php +++ b/maintenance/migrateTagTemplate.php @@ -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( diff --git a/tests/phpunit/DatabaseTest.php b/tests/phpunit/DatabaseTest.php index 54ef584b..762d8382 100644 --- a/tests/phpunit/DatabaseTest.php +++ b/tests/phpunit/DatabaseTest.php @@ -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 ); diff --git a/tests/phpunit/RecordLintJobTest.php b/tests/phpunit/RecordLintJobTest.php index 264bc287..5e9d52d8 100644 --- a/tests/phpunit/RecordLintJobTest.php +++ b/tests/phpunit/RecordLintJobTest.php @@ -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 ); } } diff --git a/tests/phpunit/SpecialLintErrorsTest.php b/tests/phpunit/SpecialLintErrorsTest.php index 4a2f555c..b3ee6c6e 100644 --- a/tests/phpunit/SpecialLintErrorsTest.php +++ b/tests/phpunit/SpecialLintErrorsTest.php @@ -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 );