From 21e71e0235e8f41189e4189b4fe03daede1d3cad Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 30 Mar 2017 13:46:06 -0700 Subject: [PATCH] Use IDatabase type hints in /maintenance Relatedly, move lockTables()/unlockTables() to IMaintainableDatabase Change-Id: Ib53e9fa948deb2f9a70f0ce16c002613d0060bf9 --- maintenance/Maintenance.php | 19 ++++++++++--------- maintenance/archives/upgradeLogging.php | 4 +++- .../benchmarks/bench_delete_truncate.php | 9 ++++++--- maintenance/convertUserOptions.php | 3 ++- maintenance/deleteOrphanedRevisions.php | 4 +++- maintenance/deleteRevision.php | 1 - maintenance/dumpIterator.php | 2 +- maintenance/dumpTextPass.php | 4 ++-- maintenance/fetchText.php | 4 +++- maintenance/namespaceDupes.php | 7 ++++--- maintenance/orphans.php | 4 +++- maintenance/populateContentModel.php | 12 +++++++----- maintenance/populateRecentChangesSource.php | 4 +++- maintenance/rebuildImages.php | 4 +++- maintenance/rebuildtextindex.php | 4 +++- maintenance/refreshImageMetadata.php | 7 +++++-- maintenance/refreshLinks.php | 2 +- maintenance/sql.php | 2 +- maintenance/update.php | 4 +++- maintenance/updateCollation.php | 4 +++- 20 files changed, 66 insertions(+), 38 deletions(-) diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index a8080c50be..e5ba411a34 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -40,6 +40,7 @@ use Wikimedia\Rdbms\IDatabase; use MediaWiki\Logger\LoggerFactory; use MediaWiki\MediaWikiServices; use Wikimedia\Rdbms\LBFactory; +use Wikimedia\Rdbms\IMaintainableDatabase; /** * Abstract maintenance class for quickly writing and churning out @@ -106,7 +107,7 @@ abstract class Maintenance { /** * Used by getDB() / setDB() - * @var Database + * @var IMaintainableDatabase */ private $mDb = null; @@ -206,7 +207,7 @@ abstract class Maintenance { * @param string $description The description of the param to show on --help * @param bool $required Is the param required? * @param bool $withArg Is an argument required with this option? - * @param string $shortName Character to use as short name + * @param string|bool $shortName Character to use as short name * @param bool $multiOccurrence Can this option be passed multiple times? */ protected function addOption( $name, $description, $required = false, @@ -1243,7 +1244,7 @@ abstract class Maintenance { * @param integer $db DB index (DB_REPLICA/DB_MASTER) * @param array $groups; default: empty array * @param string|bool $wiki; default: current wiki - * @return Database + * @return IMaintainableDatabase */ protected function getDB( $db, $groups = [], $wiki = false ) { if ( is_null( $this->mDb ) ) { @@ -1256,7 +1257,7 @@ abstract class Maintenance { /** * Sets database object to be returned by getDB(). * - * @param IDatabase $db Database object to be used + * @param IDatabase $db */ public function setDB( IDatabase $db ) { $this->mDb = $db; @@ -1318,7 +1319,7 @@ abstract class Maintenance { /** * Lock the search index - * @param Database &$db + * @param IMaintainableDatabase &$db */ private function lockSearchindex( $db ) { $write = [ 'searchindex' ]; @@ -1336,7 +1337,7 @@ abstract class Maintenance { /** * Unlock the tables - * @param Database &$db + * @param IMaintainableDatabase &$db */ private function unlockSearchindex( $db ) { $db->unlockTables( __CLASS__ . '::' . __METHOD__ ); @@ -1345,7 +1346,7 @@ abstract class Maintenance { /** * Unlock and lock again * Since the lock is low-priority, queued reads will be able to complete - * @param Database &$db + * @param IMaintainableDatabase &$db */ private function relockSearchindex( $db ) { $this->unlockSearchindex( $db ); @@ -1356,7 +1357,7 @@ abstract class Maintenance { * Perform a search index update with locking * @param int $maxLockTime The maximum time to keep the search index locked. * @param string $callback The function that will update the function. - * @param Database $dbw + * @param IMaintainableDatabase $dbw * @param array $results */ public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) { @@ -1392,7 +1393,7 @@ abstract class Maintenance { /** * Update the searchindex table for a given pageid - * @param Database $dbw A database write handle + * @param IDatabase $dbw A database write handle * @param int $pageId The page ID to update. * @return null|string */ diff --git a/maintenance/archives/upgradeLogging.php b/maintenance/archives/upgradeLogging.php index 0beff7caec..13362e09ae 100644 --- a/maintenance/archives/upgradeLogging.php +++ b/maintenance/archives/upgradeLogging.php @@ -23,6 +23,8 @@ require __DIR__ . '/../commandLine.inc'; +use Wikimedia\Rdbms\IMaintainableDatabase; + /** * Maintenance script that upgrade for log_id/log_deleted fields in a * replication-safe way. @@ -32,7 +34,7 @@ require __DIR__ . '/../commandLine.inc'; class UpdateLogging { /** - * @var Database + * @var IMaintainableDatabase */ public $dbw; public $batchSize = 1000; diff --git a/maintenance/benchmarks/bench_delete_truncate.php b/maintenance/benchmarks/bench_delete_truncate.php index 2a8d79a944..2369d993ea 100644 --- a/maintenance/benchmarks/bench_delete_truncate.php +++ b/maintenance/benchmarks/bench_delete_truncate.php @@ -23,6 +23,9 @@ require_once __DIR__ . '/Benchmarker.php'; +use Wikimedia\Rdbms\IDatabase; +use Wikimedia\Rdbms\IMaintainableDatabase; + /** * Maintenance script that benchmarks SQL DELETE vs SQL TRUNCATE. * @@ -69,7 +72,7 @@ class BenchmarkDeleteTruncate extends Benchmarker { } /** - * @param Database $dbw + * @param IDatabase $dbw * @return void */ private function insertData( $dbw ) { @@ -82,7 +85,7 @@ class BenchmarkDeleteTruncate extends Benchmarker { } /** - * @param Database $dbw + * @param IDatabase $dbw * @return void */ private function delete( $dbw ) { @@ -90,7 +93,7 @@ class BenchmarkDeleteTruncate extends Benchmarker { } /** - * @param Database $dbw + * @param IMaintainableDatabase $dbw * @return void */ private function truncate( $dbw ) { diff --git a/maintenance/convertUserOptions.php b/maintenance/convertUserOptions.php index 70f365485d..675d0695d9 100644 --- a/maintenance/convertUserOptions.php +++ b/maintenance/convertUserOptions.php @@ -24,6 +24,7 @@ require_once __DIR__ . '/Maintenance.php'; use Wikimedia\Rdbms\ResultWrapper; +use Wikimedia\Rdbms\IDatabase; /** * Maintenance script to convert user options to the new `user_properties` table. @@ -76,7 +77,7 @@ class ConvertUserOptions extends Maintenance { /** * @param ResultWrapper $res - * @param Database $dbw + * @param IDatabase $dbw * @return null|int */ function convertOptionBatch( $res, $dbw ) { diff --git a/maintenance/deleteOrphanedRevisions.php b/maintenance/deleteOrphanedRevisions.php index df496d435b..e99f2b0d5b 100644 --- a/maintenance/deleteOrphanedRevisions.php +++ b/maintenance/deleteOrphanedRevisions.php @@ -26,6 +26,8 @@ require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IDatabase; + /** * Maintenance script that deletes revisions which refer to a nonexisting page. * @@ -83,7 +85,7 @@ class DeleteOrphanedRevisions extends Maintenance { * Do this inside a transaction * * @param array $id Array of revision id values - * @param Database $dbw Database class (needs to be a master) + * @param IDatabase $dbw Master DB handle */ private function deleteRevs( $id, &$dbw ) { if ( !is_array( $id ) ) { diff --git a/maintenance/deleteRevision.php b/maintenance/deleteRevision.php index 0111ac578a..3abbdab99b 100644 --- a/maintenance/deleteRevision.php +++ b/maintenance/deleteRevision.php @@ -86,7 +86,6 @@ class DeleteRevision extends Maintenance { ); $dbw->delete( 'revision', [ 'rev_id' => $revID ] ); if ( $pageLatest == $revID ) { - // Database integrity $newLatest = $dbw->selectField( 'revision', 'rev_id', diff --git a/maintenance/dumpIterator.php b/maintenance/dumpIterator.php index 9f983c1a04..6dbad94956 100644 --- a/maintenance/dumpIterator.php +++ b/maintenance/dumpIterator.php @@ -117,7 +117,7 @@ abstract class DumpIterator extends Maintenance { /** * Callback function for each revision, child classes should override * processRevision instead. - * @param Database $rev + * @param WikiRevision $rev */ public function handleRevision( $rev ) { $title = $rev->getTitle(); diff --git a/maintenance/dumpTextPass.php b/maintenance/dumpTextPass.php index 5d92eec2ae..581f0d7b4d 100644 --- a/maintenance/dumpTextPass.php +++ b/maintenance/dumpTextPass.php @@ -27,7 +27,7 @@ require_once __DIR__ . '/backup.inc'; require_once __DIR__ . '/../includes/export/WikiExporter.php'; -use Wikimedia\Rdbms\LoadBalancer; +use Wikimedia\Rdbms\IMaintainableDatabase; /** * @ingroup Maintenance @@ -88,7 +88,7 @@ class TextPassDumper extends BackupDumper { protected $checkpointFiles = []; /** - * @var Database + * @var IMaintainableDatabase */ protected $db; diff --git a/maintenance/fetchText.php b/maintenance/fetchText.php index 9dee6e530d..9c5a375154 100644 --- a/maintenance/fetchText.php +++ b/maintenance/fetchText.php @@ -24,6 +24,8 @@ require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IDatabase; + /** * Maintenance script used to fetch page text in a subprocess. * @@ -71,7 +73,7 @@ class FetchText extends Maintenance { /** * May throw a database error if, say, the server dies during query. - * @param Database $db + * @param IDatabase $db * @param int $id The old_id * @return string */ diff --git a/maintenance/namespaceDupes.php b/maintenance/namespaceDupes.php index 80e8011f40..0bd627ff1a 100644 --- a/maintenance/namespaceDupes.php +++ b/maintenance/namespaceDupes.php @@ -24,11 +24,12 @@ * @ingroup Maintenance */ +require_once __DIR__ . '/Maintenance.php'; + use MediaWiki\Linker\LinkTarget; use MediaWiki\MediaWikiServices; use Wikimedia\Rdbms\ResultWrapper; - -require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IMaintainableDatabase; /** * Maintenance script that checks for articles to fix after @@ -39,7 +40,7 @@ require_once __DIR__ . '/Maintenance.php'; class NamespaceConflictChecker extends Maintenance { /** - * @var Database + * @var IMaintainableDatabase */ protected $db; diff --git a/maintenance/orphans.php b/maintenance/orphans.php index 2da8830643..e36c5b67dd 100644 --- a/maintenance/orphans.php +++ b/maintenance/orphans.php @@ -30,6 +30,8 @@ require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IMaintainableDatabase; + /** * Maintenance script that looks for 'orphan' revisions hooked to pages which * don't exist and 'childless' pages with no revisions. @@ -56,7 +58,7 @@ class Orphans extends Maintenance { /** * Lock the appropriate tables for the script - * @param Database $db + * @param IMaintainableDatabase $db * @param string[] $extraTable The name of any extra tables to lock (eg: text) */ private function lockTables( $db, $extraTable = [] ) { diff --git a/maintenance/populateContentModel.php b/maintenance/populateContentModel.php index b41e0e0d6e..7d83180eed 100644 --- a/maintenance/populateContentModel.php +++ b/maintenance/populateContentModel.php @@ -23,13 +23,15 @@ require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IDatabase; + /** * Usage: * populateContentModel.php --ns=1 --table=page */ class PopulateContentModel extends Maintenance { protected $wikiId; - + /** @var WANObjectCache */ protected $wanCache; public function __construct() { @@ -78,7 +80,7 @@ class PopulateContentModel extends Maintenance { $this->wanCache->delete( $revisionKey ); } - private function updatePageRows( Database $dbw, $pageIds, $model ) { + private function updatePageRows( IDatabase $dbw, $pageIds, $model ) { $count = count( $pageIds ); $this->output( "Setting $count rows to $model..." ); $dbw->update( @@ -91,7 +93,7 @@ class PopulateContentModel extends Maintenance { $this->output( "done.\n" ); } - protected function populatePage( Database $dbw, $ns ) { + protected function populatePage( IDatabase $dbw, $ns ) { $toSave = []; $lastId = 0; $nsCondition = $ns === 'all' ? [] : [ 'page_namespace' => $ns ]; @@ -123,7 +125,7 @@ class PopulateContentModel extends Maintenance { } } - private function updateRevisionOrArchiveRows( Database $dbw, $ids, $model, $table ) { + private function updateRevisionOrArchiveRows( IDatabase $dbw, $ids, $model, $table ) { $prefix = $table === 'archive' ? 'ar' : 'rev'; $model_column = "{$prefix}_content_model"; $format_column = "{$prefix}_content_format"; @@ -142,7 +144,7 @@ class PopulateContentModel extends Maintenance { $this->output( "done.\n" ); } - protected function populateRevisionOrArchive( Database $dbw, $table, $ns ) { + protected function populateRevisionOrArchive( IDatabase $dbw, $table, $ns ) { $prefix = $table === 'archive' ? 'ar' : 'rev'; $model_column = "{$prefix}_content_model"; $format_column = "{$prefix}_content_format"; diff --git a/maintenance/populateRecentChangesSource.php b/maintenance/populateRecentChangesSource.php index ac87cf3ecb..5d5da89a8c 100644 --- a/maintenance/populateRecentChangesSource.php +++ b/maintenance/populateRecentChangesSource.php @@ -23,6 +23,8 @@ require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IDatabase; + /** * Maintenance script to populate the rc_source field. * @@ -83,7 +85,7 @@ class PopulateRecentChangesSource extends LoggedUpdateMaintenance { return __CLASS__; } - protected function buildUpdateCondition( Database $dbw ) { + protected function buildUpdateCondition( IDatabase $dbw ) { $rcNew = $dbw->addQuotes( RC_NEW ); $rcSrcNew = $dbw->addQuotes( RecentChange::SRC_NEW ); $rcEdit = $dbw->addQuotes( RC_EDIT ); diff --git a/maintenance/rebuildImages.php b/maintenance/rebuildImages.php index 6aa1f37a8c..966864e62d 100644 --- a/maintenance/rebuildImages.php +++ b/maintenance/rebuildImages.php @@ -32,6 +32,8 @@ require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IMaintainableDatabase; + /** * Maintenance script to update image metadata records. * @@ -40,7 +42,7 @@ require_once __DIR__ . '/Maintenance.php'; class ImageBuilder extends Maintenance { /** - * @var Database + * @var IMaintainableDatabase */ protected $dbw; diff --git a/maintenance/rebuildtextindex.php b/maintenance/rebuildtextindex.php index b7f07629ec..e4bb60e932 100644 --- a/maintenance/rebuildtextindex.php +++ b/maintenance/rebuildtextindex.php @@ -27,6 +27,8 @@ require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IMaintainableDatabase; + /** * Maintenance script that rebuilds search index table from scratch. * @@ -36,7 +38,7 @@ class RebuildTextIndex extends Maintenance { const RTI_CHUNK_SIZE = 500; /** - * @var Database + * @var IMaintainableDatabase */ private $db; diff --git a/maintenance/refreshImageMetadata.php b/maintenance/refreshImageMetadata.php index 372c352846..b557f3d3df 100644 --- a/maintenance/refreshImageMetadata.php +++ b/maintenance/refreshImageMetadata.php @@ -29,6 +29,9 @@ require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IDatabase; +use Wikimedia\Rdbms\IMaintainableDatabase; + /** * Maintenance script to refresh image metadata fields. * @@ -37,7 +40,7 @@ require_once __DIR__ . '/Maintenance.php'; class RefreshImageMetadata extends Maintenance { /** - * @var Database + * @var IMaintainableDatabase */ protected $dbw; @@ -205,7 +208,7 @@ class RefreshImageMetadata extends Maintenance { } /** - * @param Database $dbw + * @param IDatabase $dbw * @return array */ function getConditions( $dbw ) { diff --git a/maintenance/refreshLinks.php b/maintenance/refreshLinks.php index 67f778062b..a6cd54853e 100644 --- a/maintenance/refreshLinks.php +++ b/maintenance/refreshLinks.php @@ -390,7 +390,7 @@ class RefreshLinks extends Maintenance { * By specifying a null $start or $end, it is also possible to create * half-bounded or unbounded intervals using this function. * - * @param IDatabase $db Database connection + * @param IDatabase $db * @param string $var Field name * @param mixed $start First value to include or null * @param mixed $end Last value to include or null diff --git a/maintenance/sql.php b/maintenance/sql.php index b03620dd6a..70f9aaad47 100644 --- a/maintenance/sql.php +++ b/maintenance/sql.php @@ -77,7 +77,7 @@ class MwSql extends Maintenance { $index = DB_MASTER; } - /** @var Database $db DB handle for the appropriate cluster/wiki */ + /** @var IDatabase $db DB handle for the appropriate cluster/wiki */ $db = $lb->getConnection( $index, [], $wiki ); if ( $replicaDB != '' && $db->getLBInfo( 'master' ) !== null ) { $this->error( "The server selected ({$db->getServer()}) is not a replica DB.", 1 ); diff --git a/maintenance/update.php b/maintenance/update.php index d96cecd6be..5f705ba371 100755 --- a/maintenance/update.php +++ b/maintenance/update.php @@ -27,6 +27,8 @@ require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IMaintainableDatabase; + /** * Maintenance script to run database schema updates. * @@ -145,7 +147,7 @@ class UpdateMediaWiki extends Maintenance { $this->output( "Going to run database updates for " . wfWikiID() . "\n" ); if ( $db->getType() === 'sqlite' ) { - /** @var Database|DatabaseSqlite $db */ + /** @var IMaintainableDatabase|DatabaseSqlite $db */ $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" ); } $this->output( "Depending on the size of your database this may take a while!\n" ); diff --git a/maintenance/updateCollation.php b/maintenance/updateCollation.php index e70a176909..84fc2d208e 100644 --- a/maintenance/updateCollation.php +++ b/maintenance/updateCollation.php @@ -26,6 +26,8 @@ require_once __DIR__ . '/Maintenance.php'; +use Wikimedia\Rdbms\IDatabase; + /** * Maintenance script that will find all rows in the categorylinks table * whose collation is out-of-date. @@ -242,7 +244,7 @@ TEXT * Return an SQL expression selecting rows which sort above the given row, * assuming an ordering of cl_collation, cl_to, cl_type, cl_from * @param stdClass $row - * @param Database $dbw + * @param IDatabase $dbw * @return string */ function getBatchCondition( $row, $dbw ) { -- 2.20.1