From 8f666cabcdcda75fa9cfe2409444861cccb36877 Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Mon, 19 Aug 2019 17:44:14 +0200 Subject: [PATCH] Use Maintenance::getConfig in some maintenance scripts This avoid global state Change-Id: Id67d07597159a0bd2721a381775230c3cd1d5509 --- maintenance/categoryChangesAsRdf.php | 11 +++++------ maintenance/checkDependencies.php | 7 ++++--- maintenance/cleanupPreferences.php | 10 +++++----- maintenance/getReplicaServer.php | 3 +-- maintenance/includes/MigrateActors.php | 4 ++-- maintenance/migrateArchiveText.php | 5 ++--- maintenance/namespaceDupes.php | 7 +++---- maintenance/populateContentTables.php | 5 +++-- maintenance/populateImageSha1.php | 8 ++++---- maintenance/reassignEdits.php | 12 ++++++------ maintenance/removeUnusedAccounts.php | 10 +++++----- maintenance/updateCollation.php | 8 +++----- 12 files changed, 43 insertions(+), 47 deletions(-) diff --git a/maintenance/categoryChangesAsRdf.php b/maintenance/categoryChangesAsRdf.php index f794abb0b0..95a59d23c1 100644 --- a/maintenance/categoryChangesAsRdf.php +++ b/maintenance/categoryChangesAsRdf.php @@ -115,19 +115,18 @@ SPARQLDI; } public function execute() { - global $wgRCMaxAge; - $this->initialize(); $startTS = new MWTimestamp( $this->getOption( "start" ) ); $endTS = new MWTimestamp( $this->getOption( "end" ) ); $now = new MWTimestamp(); + $rcMaxAge = $this->getConfig()->get( 'RCMaxAge' ); - if ( $now->getTimestamp() - $startTS->getTimestamp() > $wgRCMaxAge ) { - $this->error( "Start timestamp too old, maximum RC age is $wgRCMaxAge!" ); + if ( $now->getTimestamp() - $startTS->getTimestamp() > $rcMaxAge ) { + $this->error( "Start timestamp too old, maximum RC age is $rcMaxAge!" ); } - if ( $now->getTimestamp() - $endTS->getTimestamp() > $wgRCMaxAge ) { - $this->error( "End timestamp too old, maximum RC age is $wgRCMaxAge!" ); + if ( $now->getTimestamp() - $endTS->getTimestamp() > $rcMaxAge ) { + $this->error( "End timestamp too old, maximum RC age is $rcMaxAge!" ); } $this->startTS = $startTS->getTimestamp(); diff --git a/maintenance/checkDependencies.php b/maintenance/checkDependencies.php index 3e8b754567..1d588ec218 100644 --- a/maintenance/checkDependencies.php +++ b/maintenance/checkDependencies.php @@ -79,11 +79,12 @@ class CheckDependencies extends Maintenance { } private function loadThing( &$dependencies, $name, $extensions, $skins ) { - global $wgExtensionDirectory, $wgStyleDirectory; + $extDir = $this->getConfig()->get( 'ExtensionDirectory' ); + $styleDir = $this->getConfig()->get( 'StyleDirectory' ); $queue = []; $missing = false; foreach ( $extensions as $extension ) { - $path = "$wgExtensionDirectory/$extension/extension.json"; + $path = "$extDir/$extension/extension.json"; if ( file_exists( $path ) ) { // 1 is ignored $queue[$path] = 1; @@ -95,7 +96,7 @@ class CheckDependencies extends Maintenance { } foreach ( $skins as $skin ) { - $path = "$wgStyleDirectory/$skin/skin.json"; + $path = "$styleDir/$skin/skin.json"; if ( file_exists( $path ) ) { $queue[$path] = 1; $this->addToDependencies( $dependencies, [], [ $skin ], $name ); diff --git a/maintenance/cleanupPreferences.php b/maintenance/cleanupPreferences.php index bed3956a88..720e3fd8df 100644 --- a/maintenance/cleanupPreferences.php +++ b/maintenance/cleanupPreferences.php @@ -59,8 +59,6 @@ class CleanupPreferences extends Maintenance { * all values are in that range. Drop ones that aren't. */ public function execute() { - global $wgHiddenPrefs, $wgDefaultUserOptions; - $dbw = $this->getDB( DB_MASTER ); $hidden = $this->hasOption( 'hidden' ); $unknown = $this->hasOption( 'unknown' ); @@ -73,10 +71,11 @@ class CleanupPreferences extends Maintenance { // Remove hidden prefs. Iterate over them to avoid the IN on a large table if ( $hidden ) { - if ( !$wgHiddenPrefs ) { + $hiddenPrefs = $this->getConfig()->get( 'HiddenPrefs' ); + if ( !$hiddenPrefs ) { $this->output( "No hidden preferences, skipping\n" ); } - foreach ( $wgHiddenPrefs as $hiddenPref ) { + foreach ( $hiddenPrefs as $hiddenPref ) { $this->deleteByWhere( $dbw, 'Dropping hidden preferences', @@ -87,9 +86,10 @@ class CleanupPreferences extends Maintenance { // Remove unknown preferences. Special-case 'userjs-' as we can't control those names. if ( $unknown ) { + $defaultUserOptions = $this->getConfig()->get( 'DefaultUserOptions' ); $where = [ 'up_property NOT' . $dbw->buildLike( 'userjs-', $dbw->anyString() ), - 'up_property NOT IN (' . $dbw->makeList( array_keys( $wgDefaultUserOptions ) ) . ')', + 'up_property NOT IN (' . $dbw->makeList( array_keys( $defaultUserOptions ) ) . ')', ]; // Allow extensions to add to the where clause to prevent deletion of their own prefs. Hooks::run( 'DeleteUnknownPreferences', [ &$where, $dbw ] ); diff --git a/maintenance/getReplicaServer.php b/maintenance/getReplicaServer.php index 554e373e8b..d861348dbd 100644 --- a/maintenance/getReplicaServer.php +++ b/maintenance/getReplicaServer.php @@ -38,8 +38,7 @@ class GetReplicaServer extends Maintenance { } public function execute() { - global $wgAllDBsAreLocalhost; - if ( $wgAllDBsAreLocalhost ) { + if ( $this->getConfig()->get( 'AllDBsAreLocalhost' ) ) { $host = 'localhost'; } elseif ( $this->hasOption( 'group' ) ) { $db = $this->getDB( DB_REPLICA, $this->getOption( 'group' ) ); diff --git a/maintenance/includes/MigrateActors.php b/maintenance/includes/MigrateActors.php index aff67587b4..1b35a20217 100644 --- a/maintenance/includes/MigrateActors.php +++ b/maintenance/includes/MigrateActors.php @@ -51,9 +51,9 @@ class MigrateActors extends LoggedUpdateMaintenance { } protected function doDBUpdates() { - global $wgActorTableSchemaMigrationStage; + $actorTableSchemaMigrationStage = $this->getConfig()->get( 'ActorTableSchemaMigrationStage' ); - if ( !( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) ) { + if ( !( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) ) { $this->output( "...cannot update while \$wgActorTableSchemaMigrationStage lacks SCHEMA_COMPAT_WRITE_NEW\n" ); diff --git a/maintenance/migrateArchiveText.php b/maintenance/migrateArchiveText.php index 71fff56f75..2271c393a8 100644 --- a/maintenance/migrateArchiveText.php +++ b/maintenance/migrateArchiveText.php @@ -55,9 +55,8 @@ class MigrateArchiveText extends LoggedUpdateMaintenance { } protected function doDBUpdates() { - global $wgDefaultExternalStore; - $replaceMissing = $this->hasOption( 'replace-missing' ); + $defaultExternalStore = $this->getConfig()->get( 'DefaultExternalStore' ); $batchSize = $this->getBatchSize(); $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] ); @@ -96,7 +95,7 @@ class MigrateArchiveText extends LoggedUpdateMaintenance { if ( $data !== false ) { $flags = Revision::compressRevisionText( $data ); - if ( $wgDefaultExternalStore ) { + if ( $defaultExternalStore ) { $data = ExternalStore::insertToDefault( $data ); if ( $flags ) { $flags .= ','; diff --git a/maintenance/namespaceDupes.php b/maintenance/namespaceDupes.php index ea12e42956..f9868a197d 100644 --- a/maintenance/namespaceDupes.php +++ b/maintenance/namespaceDupes.php @@ -102,8 +102,6 @@ class NamespaceDupes extends Maintenance { * @return bool */ private function checkAll( $options ) { - global $wgNamespaceAliases, $wgCapitalLinks; - $contLang = MediaWikiServices::getInstance()->getContentLanguage(); $spaces = []; @@ -129,7 +127,7 @@ class NamespaceDupes extends Maintenance { $spaces[$name] = $ns; } } - foreach ( $wgNamespaceAliases as $name => $ns ) { + foreach ( $this->getConfig()->get( 'NamespaceAliases' ) as $name => $ns ) { $spaces[$name] = $ns; } foreach ( $contLang->getNamespaceAliases() as $name => $ns ) { @@ -138,6 +136,7 @@ class NamespaceDupes extends Maintenance { // We'll need to check for lowercase keys as well, // since we're doing case-sensitive searches in the db. + $capitalLinks = $this->getConfig()->get( 'CapitalLinks' ); foreach ( $spaces as $name => $ns ) { $moreNames = []; $moreNames[] = $contLang->uc( $name ); @@ -146,7 +145,7 @@ class NamespaceDupes extends Maintenance { $moreNames[] = $contLang->ucwords( $contLang->lc( $name ) ); $moreNames[] = $contLang->ucwordbreaks( $name ); $moreNames[] = $contLang->ucwordbreaks( $contLang->lc( $name ) ); - if ( !$wgCapitalLinks ) { + if ( !$capitalLinks ) { foreach ( $moreNames as $altName ) { $moreNames[] = $contLang->lcfirst( $altName ); } diff --git a/maintenance/populateContentTables.php b/maintenance/populateContentTables.php index c84f3de54e..3325b05456 100644 --- a/maintenance/populateContentTables.php +++ b/maintenance/populateContentTables.php @@ -77,11 +77,12 @@ class PopulateContentTables extends Maintenance { } public function execute() { - global $wgMultiContentRevisionSchemaMigrationStage; + $multiContentRevisionSchemaMigrationStage = + $this->getConfig()->get( 'MultiContentRevisionSchemaMigrationStage' ); $t0 = microtime( true ); - if ( ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) === 0 ) { + if ( ( $multiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) === 0 ) { $this->writeln( '...cannot update while \$wgMultiContentRevisionSchemaMigrationStage ' . 'does not have the SCHEMA_COMPAT_WRITE_NEW bit set.' diff --git a/maintenance/populateImageSha1.php b/maintenance/populateImageSha1.php index 0de9d6737e..9f77fda55c 100644 --- a/maintenance/populateImageSha1.php +++ b/maintenance/populateImageSha1.php @@ -109,10 +109,10 @@ class PopulateImageSha1 extends LoggedUpdateMaintenance { // with the database write operation, because the writes are queued // in the pipe buffer. This can improve performance by up to a // factor of 2. - global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname; - $cmd = 'mysql -u' . Shell::escape( $wgDBuser ) . - ' -h' . Shell::escape( $wgDBserver ) . - ' -p' . Shell::escape( $wgDBpassword, $wgDBname ); + $config = $this->getConfig(); + $cmd = 'mysql -u' . Shell::escape( $config->get( 'DBuser' ) ) . + ' -h' . Shell::escape( $config->get( 'DBserver' ) ) . + ' -p' . Shell::escape( $config->get( 'DBpassword' ), $config->get( 'DBname' ) ); $this->output( "Using pipe method\n" ); $pipe = popen( $cmd, 'w' ); } diff --git a/maintenance/reassignEdits.php b/maintenance/reassignEdits.php index 98025d1d90..54f1862e10 100644 --- a/maintenance/reassignEdits.php +++ b/maintenance/reassignEdits.php @@ -76,7 +76,7 @@ class ReassignEdits extends Maintenance { * @return int Number of entries changed, or that would be changed */ private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) { - global $wgActorTableSchemaMigrationStage; + $actorTableSchemaMigrationStage = $this->getConfig()->get( 'ActorTableSchemaMigrationStage' ); $dbw = $this->getDB( DB_MASTER ); $this->beginTransaction( $dbw, __METHOD__ ); @@ -136,7 +136,7 @@ class ReassignEdits extends Maintenance { if ( $total ) { # Reassign edits $this->output( "\nReassigning current edits..." ); - if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) { + if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) { $dbw->update( 'revision', [ @@ -148,7 +148,7 @@ class ReassignEdits extends Maintenance { __METHOD__ ); } - if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) { + if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) { $dbw->update( 'revision_actor_temp', [ 'revactor_actor' => $to->getActorId( $dbw ) ], @@ -189,16 +189,16 @@ class ReassignEdits extends Maintenance { * @return array */ private function userSpecification( IDatabase $dbw, &$user, $idfield, $utfield, $acfield ) { - global $wgActorTableSchemaMigrationStage; + $actorTableSchemaMigrationStage = $this->getConfig()->get( 'ActorTableSchemaMigrationStage' ); $ret = []; - if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) { + if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) { $ret += [ $idfield => $user->getId(), $utfield => $user->getName(), ]; } - if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) { + if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) { $ret += [ $acfield => $user->getActorId( $dbw ) ]; } return $ret; diff --git a/maintenance/removeUnusedAccounts.php b/maintenance/removeUnusedAccounts.php index 6b2f4886c6..16a7346a6a 100644 --- a/maintenance/removeUnusedAccounts.php +++ b/maintenance/removeUnusedAccounts.php @@ -39,7 +39,7 @@ class RemoveUnusedAccounts extends Maintenance { } public function execute() { - global $wgActorTableSchemaMigrationStage; + $actorTableSchemaMigrationStage = $this->getConfig()->get( 'ActorTableSchemaMigrationStage' ); $this->output( "Remove unused accounts\n\n" ); @@ -48,7 +48,7 @@ class RemoveUnusedAccounts extends Maintenance { $delUser = []; $delActor = []; $dbr = $this->getDB( DB_REPLICA ); - if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) { + if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) { $res = $dbr->select( [ 'user', 'actor' ], [ 'user_id', 'user_name', 'user_touched', 'actor_id' ], @@ -94,7 +94,7 @@ class RemoveUnusedAccounts extends Maintenance { $this->output( "\nDeleting unused accounts..." ); $dbw = $this->getDB( DB_MASTER ); $dbw->delete( 'user', [ 'user_id' => $delUser ], __METHOD__ ); - if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) { + if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) { # Keep actor rows referenced from ipblocks $keep = $dbw->selectFieldValues( 'ipblocks', 'ipb_by_actor', [ 'ipb_by_actor' => $delActor ], __METHOD__ @@ -110,11 +110,11 @@ class RemoveUnusedAccounts extends Maintenance { $dbw->delete( 'user_groups', [ 'ug_user' => $delUser ], __METHOD__ ); $dbw->delete( 'user_former_groups', [ 'ufg_user' => $delUser ], __METHOD__ ); $dbw->delete( 'user_properties', [ 'up_user' => $delUser ], __METHOD__ ); - if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) { + if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) { $dbw->delete( 'logging', [ 'log_actor' => $delActor ], __METHOD__ ); $dbw->delete( 'recentchanges', [ 'rc_actor' => $delActor ], __METHOD__ ); } - if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) { + if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) { $dbw->delete( 'logging', [ 'log_user' => $delUser ], __METHOD__ ); $dbw->delete( 'recentchanges', [ 'rc_user' => $delUser ], __METHOD__ ); } diff --git a/maintenance/updateCollation.php b/maintenance/updateCollation.php index 19fc54a4d7..9bcba7e344 100644 --- a/maintenance/updateCollation.php +++ b/maintenance/updateCollation.php @@ -44,10 +44,10 @@ class UpdateCollation extends Maintenance { public function __construct() { parent::__construct(); - global $wgCategoryCollation; + $categoryCollation = $this->getConfig()->get( 'CategoryCollation' ); $this->addDescription( <<getDB( DB_MASTER ); $dbr = $this->getDB( DB_REPLICA ); $force = $this->getOption( 'force' ); @@ -81,7 +79,7 @@ TEXT $collationName = $this->getOption( 'target-collation' ); $collation = Collation::factory( $collationName ); } else { - $collationName = $wgCategoryCollation; + $collationName = $this->getConfig()->get( 'CategoryCollation' ); $collation = Collation::singleton(); } -- 2.20.1