Use Maintenance::getConfig in some maintenance scripts
authorUmherirrender <umherirrender_de.wp@web.de>
Mon, 19 Aug 2019 15:44:14 +0000 (17:44 +0200)
committerUmherirrender <umherirrender_de.wp@web.de>
Sun, 1 Sep 2019 18:55:08 +0000 (20:55 +0200)
This avoid global state

Change-Id: Id67d07597159a0bd2721a381775230c3cd1d5509

12 files changed:
maintenance/categoryChangesAsRdf.php
maintenance/checkDependencies.php
maintenance/cleanupPreferences.php
maintenance/getReplicaServer.php
maintenance/includes/MigrateActors.php
maintenance/migrateArchiveText.php
maintenance/namespaceDupes.php
maintenance/populateContentTables.php
maintenance/populateImageSha1.php
maintenance/reassignEdits.php
maintenance/removeUnusedAccounts.php
maintenance/updateCollation.php

index f794abb..95a59d2 100644 (file)
@@ -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();
index 3e8b754..1d588ec 100644 (file)
@@ -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 );
index bed3956..720e3fd 100644 (file)
@@ -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 ] );
index 554e373..d861348 100644 (file)
@@ -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' ) );
index aff6758..1b35a20 100644 (file)
@@ -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"
                        );
index 71fff56..2271c39 100644 (file)
@@ -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 .= ',';
index ea12e42..f9868a1 100644 (file)
@@ -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 );
                                }
index c84f3de..3325b05 100644 (file)
@@ -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.'
index 0de9d67..9f77fda 100644 (file)
@@ -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' );
                }
index 98025d1..54f1862 100644 (file)
@@ -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;
index 6b2f488..16a7346 100644 (file)
@@ -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__ );
                        }
index 19fc54a..9bcba7e 100644 (file)
@@ -44,10 +44,10 @@ class UpdateCollation extends Maintenance {
        public function __construct() {
                parent::__construct();
 
-               global $wgCategoryCollation;
+               $categoryCollation = $this->getConfig()->get( 'CategoryCollation' );
                $this->addDescription( <<<TEXT
 This script will find all rows in the categorylinks table whose collation is
-out-of-date (cl_collation != '$wgCategoryCollation') and repopulate cl_sortkey
+out-of-date (cl_collation != '$categoryCollation') and repopulate cl_sortkey
 using the page title and cl_sortkey_prefix.  If all collations are
 up-to-date, it will do nothing.
 TEXT
@@ -70,8 +70,6 @@ TEXT
        }
 
        public function execute() {
-               global $wgCategoryCollation;
-
                $dbw = $this->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();
                }