maintenance: Deprecate Maintenance::hasArg/getArg with no param
authorThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Thu, 28 Feb 2019 11:13:49 +0000 (12:13 +0100)
committerThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Fri, 29 Mar 2019 13:37:46 +0000 (14:37 +0100)
Benefit of keeping the parameter optional:
- In maintenance scripts that really only have one parameter, it's a
  little more convenient to be able to ask for *the* parameter via an
  empty getArg().

Disadvantages:
- It's unclear what getArg() means when there is no indication *which*
  argument the code asks for. This might as well return the last
  argument, or an array of all arguments.
- In scripts with two or more arguments, it's confusing to see
  getArg( 1 ) next to an empty getArg().
- The methods are more complex and a bit more complicated to use with
  the extra feature of this parameter being optional. Users need to
  look up what the default is to be able to use it safely.

Change-Id: I22a43bfdfc0f0c9ffdb468c13aba73b888d1f15e

16 files changed:
RELEASE-NOTES-1.33
maintenance/Maintenance.php
maintenance/benchmarks/benchmarkParse.php
maintenance/cleanupSpam.php
maintenance/deleteBatch.php
maintenance/edit.php
maintenance/importDump.php
maintenance/jsparse.php
maintenance/mctest.php
maintenance/moveBatch.php
maintenance/nukePage.php
maintenance/pageExists.php
maintenance/protect.php
maintenance/storage/dumpRev.php
maintenance/undelete.php
maintenance/view.php

index ddd6da9..ed8b5f8 100644 (file)
@@ -409,6 +409,8 @@ because of Phabricator reports.
   instead. The setTags() method was overriding the tags, addTags() doesn't
   override, only adds new tags.
 * Block::isValid is deprecated, since it is no longer needed in core.
+* Calling Maintenance::hasArg() as well as Maintenance::getArg() with no
+  parameter has been deprecated. Please pass the argument number 0.
 
 === Other changes in 1.33 ===
 * (T201747) Html::openElement() warns if given an element name with a space
index 3476a32..7430e1e 100644 (file)
@@ -324,6 +324,10 @@ abstract class Maintenance {
         * @return bool
         */
        protected function hasArg( $argId = 0 ) {
+               if ( func_num_args() === 0 ) {
+                       wfDeprecated( __METHOD__ . ' without an $argId', '1.33' );
+               }
+
                return isset( $this->mArgs[$argId] );
        }
 
@@ -334,6 +338,10 @@ abstract class Maintenance {
         * @return mixed
         */
        protected function getArg( $argId = 0, $default = null ) {
+               if ( func_num_args() === 0 ) {
+                       wfDeprecated( __METHOD__ . ' without an $argId', '1.33' );
+               }
+
                return $this->hasArg( $argId ) ? $this->mArgs[$argId] : $default;
        }
 
index c42fcd9..a3a4974 100644 (file)
@@ -75,7 +75,7 @@ class BenchmarkParse extends Maintenance {
                // Set as a member variable to avoid function calls when we're timing the parse
                $this->linkCache = MediaWikiServices::getInstance()->getLinkCache();
 
-               $title = Title::newFromText( $this->getArg() );
+               $title = Title::newFromText( $this->getArg( 0 ) );
                if ( !$title ) {
                        $this->error( "Invalid title" );
                        exit( 1 );
index 17d2e18..04f278f 100644 (file)
@@ -52,7 +52,7 @@ class CleanupSpam extends Maintenance {
                // Hack: Grant bot rights so we don't flood RecentChanges
                $wgUser->addGroup( 'bot' );
 
-               $spec = $this->getArg();
+               $spec = $this->getArg( 0 );
 
                $protConds = [];
                foreach ( [ 'http://', 'https://' ] as $prot ) {
index fe3bea0..4f9e488 100644 (file)
@@ -69,8 +69,8 @@ class DeleteBatch extends Maintenance {
                }
                $wgUser = $user;
 
-               if ( $this->hasArg() ) {
-                       $file = fopen( $this->getArg(), 'r' );
+               if ( $this->hasArg( 0 ) ) {
+                       $file = fopen( $this->getArg( 0 ), 'r' );
                } else {
                        $file = $this->getStdin();
                }
index d4a7bef..3609cf2 100644 (file)
@@ -71,7 +71,7 @@ class EditCLI extends Maintenance {
                        $wgUser->addToDatabase();
                }
 
-               $title = Title::newFromText( $this->getArg() );
+               $title = Title::newFromText( $this->getArg( 0 ) );
                if ( !$title ) {
                        $this->fatalError( "Invalid title" );
                }
index 7c20748..c2c5ccf 100644 (file)
@@ -112,8 +112,8 @@ TEXT
                        $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
                }
 
-               if ( $this->hasArg() ) {
-                       $this->importFromFile( $this->getArg() );
+               if ( $this->hasArg( 0 ) ) {
+                       $this->importFromFile( $this->getArg( 0 ) );
                } else {
                        $this->importFromStdin();
                }
index 661ec98..aa5077d 100644 (file)
@@ -38,7 +38,7 @@ class JSParseHelper extends Maintenance {
        }
 
        public function execute() {
-               if ( $this->hasArg() ) {
+               if ( $this->hasArg( 0 ) ) {
                        $files = $this->mArgs;
                } else {
                        $this->maybeHelp( true ); // @todo fixme this is a lame API :)
index c976bd7..98f0eb9 100644 (file)
@@ -50,8 +50,8 @@ class McTest extends Maintenance {
                                $this->fatalError( "MediaWiki isn't configured with a cache named '$cache'" );
                        }
                        $servers = $wgObjectCaches[$cache]['servers'];
-               } elseif ( $this->hasArg() ) {
-                       $servers = [ $this->getArg() ];
+               } elseif ( $this->hasArg( 0 ) ) {
+                       $servers = [ $this->getArg( 0 ) ];
                } elseif ( $wgMainCacheType === CACHE_MEMCACHED ) {
                        global $wgMemCachedServers;
                        $servers = $wgMemCachedServers;
index 9c20c67..47828e6 100644 (file)
@@ -65,8 +65,8 @@ class MoveBatch extends Maintenance {
                $reason = $this->getOption( 'r', '' );
                $interval = $this->getOption( 'i', 0 );
                $noredirects = $this->hasOption( 'noredirects' );
-               if ( $this->hasArg() ) {
-                       $file = fopen( $this->getArg(), 'r' );
+               if ( $this->hasArg( 0 ) ) {
+                       $file = fopen( $this->getArg( 0 ), 'r' );
                } else {
                        $file = $this->getStdin();
                }
index baead94..ba0fe5d 100644 (file)
@@ -39,7 +39,7 @@ class NukePage extends Maintenance {
        }
 
        public function execute() {
-               $name = $this->getArg();
+               $name = $this->getArg( 0 );
                $delete = $this->hasOption( 'delete' );
 
                $dbw = $this->getDB( DB_MASTER );
index 10d37de..6a5654f 100644 (file)
@@ -32,7 +32,7 @@ class PageExists extends Maintenance {
        }
 
        public function execute() {
-               $titleArg = $this->getArg();
+               $titleArg = $this->getArg( 0 );
                $title = Title::newFromText( $titleArg );
                $pageExists = $title && $title->exists();
 
index e2c1a8b..1603ab0 100644 (file)
@@ -62,7 +62,7 @@ class Protect extends Maintenance {
                        $this->fatalError( "Invalid username" );
                }
 
-               $t = Title::newFromText( $this->getArg() );
+               $t = Title::newFromText( $this->getArg( 0 ) );
                if ( !$t ) {
                        $this->fatalError( "Invalid title" );
                }
index b00db20..7d9883e 100644 (file)
@@ -39,7 +39,7 @@ class DumpRev extends Maintenance {
        }
 
        public function execute() {
-               $id = (int)$this->getArg();
+               $id = (int)$this->getArg( 0 );
 
                $lookup = MediaWikiServices::getInstance()->getRevisionLookup();
                $rev = $lookup->getRevisionById( $id );
index e9b2abd..144518e 100644 (file)
@@ -37,7 +37,7 @@ class Undelete extends Maintenance {
 
                $user = $this->getOption( 'user', false );
                $reason = $this->getOption( 'reason', '' );
-               $pageName = $this->getArg();
+               $pageName = $this->getArg( 0 );
 
                $title = Title::newFromText( $pageName );
                if ( !$title ) {
index 24b5007..671369a 100644 (file)
@@ -36,7 +36,7 @@ class ViewCLI extends Maintenance {
        }
 
        public function execute() {
-               $title = Title::newFromText( $this->getArg() );
+               $title = Title::newFromText( $this->getArg( 0 ) );
                if ( !$title ) {
                        $this->fatalError( "Invalid title" );
                }