From 25e604f1c02db46f4cfc8d3429c42d4beb206a16 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Tue, 18 Aug 2009 23:06:24 +0000 Subject: [PATCH] Redo addArgs() as addArg() so we can actually do useful things with arguments like descriptions and proper requirement checks, similar to addOption() --- maintenance/Maintenance.php | 43 ++++++++++++++++++++++++-------- maintenance/addwiki.php | 4 ++- maintenance/cleanupSpam.php | 2 +- maintenance/createAndPromote.php | 3 ++- maintenance/deleteBatch.php | 2 +- maintenance/edit.php | 2 +- maintenance/fixTimestamps.php | 4 ++- maintenance/mctest.php | 2 +- maintenance/moveBatch.php | 2 +- maintenance/nukePage.php | 2 +- maintenance/patchSql.php | 2 +- maintenance/reassignEdits.php | 3 ++- maintenance/rebuildFileCache.php | 4 +-- maintenance/refreshLinks.php | 2 +- maintenance/renamewiki.php | 3 ++- maintenance/undelete.php | 2 +- maintenance/waitForSlave.php | 2 +- 17 files changed, 56 insertions(+), 28 deletions(-) diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 9ef2ef74c2..394b0a41bb 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -137,10 +137,17 @@ abstract class Maintenance { } /** - * Add some args that are needed. Used in formatting help - */ - protected function addArgs( $args ) { - $this->mArgList = array_merge( $this->mArgList, $args ); + * Add some args that are needed + * @param $arg String Name of the arg, like 'start' + * @param $description String Short description of the arg + * @param $required Boolean Is this required? + */ + protected function addArg( $arg, $description, $required = true ) { + $this->mArgList[] = array( + 'name' => $arg, + 'desc' => $description, + 'require' => $required + ); } /** @@ -462,17 +469,23 @@ abstract class Maintenance { * Run some validation checks on the params, etc */ private function validateParamsAndArgs() { - # Check to make sure we've got all the required ones + $die = false; + # Check to make sure we've got all the required options foreach( $this->mParams as $opt => $info ) { if( $info['require'] && !$this->hasOption( $opt ) ) { - $this->error( "Param $opt required.", true ); + $this->error( "Param $opt required!" ); + $die = true; } } - - # Also make sure we've got enough arguments - if ( count( $this->mArgs ) < count( $this->mArgList ) ) { - $this->error( "Not enough arguments passed", true ); + # Check arg list too + foreach( $this->mArgList as $k => $info ) { + if( $info['require'] && !$this->hasArg($k) ) { + $this->error( "Argument <" . $info['name'] . "> required!" ); + $die = true; + } } + + if( $die ) $this->maybeHelp( true ); } /** @@ -505,12 +518,20 @@ abstract class Maintenance { $this->output( " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]" ); } if( $this->mArgList ) { - $this->output( " <" . implode( $this->mArgList, "> <" ) . ">" ); + $this->output( " <" ); + foreach( $this->mArgList as $k => $arg ) { + $this->output( $arg['name'] . ">" ); + if( $k < count( $this->mArgList ) - 1 ) + $this->output( " <" ); + } } $this->output( "\n" ); foreach( $this->mParams as $par => $info ) { $this->output( "\t$par : " . $info['desc'] . "\n" ); } + foreach( $this->mArgList as $info ) { + $this->output( "\t<" . $info['name'] . "> : " . $info['desc'] . "\n" ); + } die( 1 ); } } diff --git a/maintenance/addwiki.php b/maintenance/addwiki.php index 46818e6e07..9603d2be46 100644 --- a/maintenance/addwiki.php +++ b/maintenance/addwiki.php @@ -29,7 +29,9 @@ class AddWiki extends Maintenance { public function __construct() { parent::__construct(); $this->mDescription = "Add a new wiki to the family. Wikimedia specific!"; - $this->addArgs( 'language', 'site', 'dbname' ); + $this->addArg( 'language', 'Language code of new site' ); + $this->addArg( 'site', 'Type of site' ); + $this->addArg( 'dbname', 'Name of database to create' ); } protected function getDbType() { diff --git a/maintenance/cleanupSpam.php b/maintenance/cleanupSpam.php index f9e897eadf..fb76e31758 100644 --- a/maintenance/cleanupSpam.php +++ b/maintenance/cleanupSpam.php @@ -27,7 +27,7 @@ class CleanupSpam extends Maintenance { parent::__construct(); $this->mDescription = "Cleanup all spam from a given hostname"; $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' ); - $this->addArgs( array( 'hostname' ) ); + $this->addArg( 'hostname', 'Hostname that was spamming' ); } public function execute() { diff --git a/maintenance/createAndPromote.php b/maintenance/createAndPromote.php index 694f9992c8..391d122670 100644 --- a/maintenance/createAndPromote.php +++ b/maintenance/createAndPromote.php @@ -30,7 +30,8 @@ class CreateAndPromote extends Maintenance { parent::__construct(); $this->mDescription = "Create a new user account with administrator rights"; $this->addOption( "bureaucrat", "Grant the account bureaucrat rights" ); - $this->addArgs( array( "username", "password" ) ); + $this->addArg( "username", "Username of new user" ); + $this->addArg( "password", "Password to set" ); } public function execute() { diff --git a/maintenance/deleteBatch.php b/maintenance/deleteBatch.php index 6aecda7d7d..6946b0c3d0 100644 --- a/maintenance/deleteBatch.php +++ b/maintenance/deleteBatch.php @@ -37,7 +37,7 @@ class DeleteBatch extends Maintenance { $this->addOption( 'u', "User to perform deletion", false, true ); $this->addOption( 'r', "Reason to delete page", false, true ); $this->addOption( 'i', "Interval to sleep between deletions" ); - $this->addArgs( array( 'listfile' ) ); + $this->addArg( 'listfile', 'File with titles to delete, separated by newlines', false ); } public function execute() { diff --git a/maintenance/edit.php b/maintenance/edit.php index d7360907e9..8d0068c34d 100644 --- a/maintenance/edit.php +++ b/maintenance/edit.php @@ -32,7 +32,7 @@ class EditCLI extends Maintenance { $this->addOption( 'b', 'Bot edit' ); $this->addOption( 'a', 'Enable autosummary' ); $this->addOption( 'no-rc', 'Do not show the change in recent changes' ); - $this->addArgs( array( 'title' ) ); + $this->addArg( 'title', 'Title of article to edit' ); } public function execute() { diff --git a/maintenance/fixTimestamps.php b/maintenance/fixTimestamps.php index 2cbec4466b..ea102fb808 100644 --- a/maintenance/fixTimestamps.php +++ b/maintenance/fixTimestamps.php @@ -30,7 +30,9 @@ class FixTimestamps extends Maintenance { public function __construct() { parent::__construct(); $this->mDescription = ""; - $this->addArgs( array( 'offset', 'start', 'end' ) ); + $this->addArg( 'offset', '' ); + $this->addArg( 'start', 'Starting timestamp' ); + $this->addArg( 'end', 'Ending timestamp' ); } public function execute() { diff --git a/maintenance/mctest.php b/maintenance/mctest.php index b77ffcbc77..4e424b1158 100644 --- a/maintenance/mctest.php +++ b/maintenance/mctest.php @@ -29,7 +29,7 @@ class mcTest extends Maintenance { $this->mDescription = "Makes several 'set', 'incr' and 'get' requests on every" . " memcached server and shows a report"; $this->addOption( 'i', 'Number of iterations', false, true ); - $this->addArgs( array( 'server' ) ); + $this->addArg( 'server', 'Memcached server to test' ); } public function execute() { diff --git a/maintenance/moveBatch.php b/maintenance/moveBatch.php index 878b6492ef..9f236773e7 100644 --- a/maintenance/moveBatch.php +++ b/maintenance/moveBatch.php @@ -42,7 +42,7 @@ class MoveBatch extends Maintenance { $this->addOption( 'u', "User to perform move", false, true ); $this->addOption( 'r', "Reason to move page", false, true ); $this->addOption( 'i', "Interval to sleep between moves" ); - $this->addArgs( array( 'listfile' ) ); + $this->addArg( 'listfile', 'List of pages to move, newline delimited', false ); } public function execute() { diff --git a/maintenance/nukePage.php b/maintenance/nukePage.php index 1cf3c79ff6..16ff2e4010 100644 --- a/maintenance/nukePage.php +++ b/maintenance/nukePage.php @@ -29,7 +29,7 @@ class NukePage extends Maintenance { parent::__construct(); $this->mDescription = "Remove a page record from the database"; $this->addOption( 'delete', "Actually delete the page" ); - $this->addArgs( array( 'title' ) ); + $this->addArg( 'title', 'Title to delete' ); } public function execute() { diff --git a/maintenance/patchSql.php b/maintenance/patchSql.php index 122f23a7e4..6071323837 100644 --- a/maintenance/patchSql.php +++ b/maintenance/patchSql.php @@ -27,7 +27,7 @@ class PatchSql extends Maintenance { public function __construct() { parent::__construct(); $this->mDescription = "Run an SQL file into the DB, replacing prefix and charset vars"; - $this->addArgs( array( 'patch-name' ) ); + $this->addArg( 'patch-name', 'Name of the patch file, either full path or in maintenance/archives' ); } protected function getDbType() { diff --git a/maintenance/reassignEdits.php b/maintenance/reassignEdits.php index 926ee5cc80..0ce08cc35e 100644 --- a/maintenance/reassignEdits.php +++ b/maintenance/reassignEdits.php @@ -31,7 +31,8 @@ class ReassignEdits extends Maintenance { $this->addOption( "force", "Reassign even if the target user doesn't exist" ); $this->addOption( "norc", "Don't update the recent changes table" ); $this->addOption( "report", "Print out details of what would be changed, but don't update it" ); - $this->addArgs( array( 'from', 'to' ) ); + $this->addArg( 'from', 'Old user to take edits from' ); + $this->addArg( 'to', 'New user to give edits to' ); } public function execute() { diff --git a/maintenance/rebuildFileCache.php b/maintenance/rebuildFileCache.php index 0173726915..2a4e488471 100644 --- a/maintenance/rebuildFileCache.php +++ b/maintenance/rebuildFileCache.php @@ -26,8 +26,8 @@ class RebuildFileCache extends Maintenance { public function __construct() { parent::__construct(); $this->mDescription = "Build file cache for content pages"; - //$this->addArgs( array( 'start' ) ); - $this->addOption( 'overwrite', 'Refresh page cache', false ); + $this->addArg( 'start', 'Page_id to start from', true ); + $this->addArg( 'overwrite', 'Refresh page cache', false ); $this->setBatchSize( 100 ); } diff --git a/maintenance/refreshLinks.php b/maintenance/refreshLinks.php index c43d8c6c59..58c789c8a3 100644 --- a/maintenance/refreshLinks.php +++ b/maintenance/refreshLinks.php @@ -30,7 +30,7 @@ class RefreshLinks extends Maintenance { $this->addOption( 'old-redirects-only', 'Only fix redirects with no redirect table entry' ); $this->addOption( 'm', 'Maximum replication lag', false, true ); $this->addOption( 'e', 'Last page id to refresh', false, true ); - $this->addArgs( array( 'start' => true ) ); + $this->addArg( 'start', 'Page_id to start from, default 1' ); $this->setBatchSize( 100 ); } diff --git a/maintenance/renamewiki.php b/maintenance/renamewiki.php index da8742e683..a022477980 100644 --- a/maintenance/renamewiki.php +++ b/maintenance/renamewiki.php @@ -28,7 +28,8 @@ class RenameWiki extends Maintenance { public function __construct() { parent::__construct(); $this->mDescription = "Rename external storage dbs and leave a new one"; - $this->addArgs( array( 'olddb', 'newdb' ) ); + $this->addArg( 'olddb' 'Old DB name' ); + $this->addArg( 'newdb' 'New DB name' ); } protected function getDbType() { diff --git a/maintenance/undelete.php b/maintenance/undelete.php index b392ac95c4..099ea88d67 100644 --- a/maintenance/undelete.php +++ b/maintenance/undelete.php @@ -14,7 +14,7 @@ class Undelete extends Maintenance { $this->mDescription = "Undelete a page"; $this->addOption( 'u', 'The user to perform the undeletion', false, true ); $this->addOption( 'r', 'The reason to undelete', false, true ); - $this->addArgs( array( 'pagename' ) ); + $this->addArg( 'pagename', 'Page to undelete' ); } public function execute() { diff --git a/maintenance/waitForSlave.php b/maintenance/waitForSlave.php index cf9bd001d0..f2a532c9f4 100644 --- a/maintenance/waitForSlave.php +++ b/maintenance/waitForSlave.php @@ -23,7 +23,7 @@ require_once( dirname(__FILE__) . '/Maintenance.php' ); class WaitForSlave extends Maintenance { public function __construct() { - $this->addArgs( array( 'maxlag' ) ); + $this->addArg( 'maxlag', 'How long to wait for the slaves, default 10 seconds', false ); } public function execute() { wfWaitForSlaves( $this->getArg( 0, 10 ) ); -- 2.20.1