diff options
Diffstat (limited to 'maintenance/Maintenance.php')
-rw-r--r-- | maintenance/Maintenance.php | 275 |
1 files changed, 141 insertions, 134 deletions
diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 30e93c90..8d30df4c 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -49,7 +49,6 @@ $maintClass = false; * @ingroup Maintenance */ abstract class Maintenance { - /** * Constants for DB access type * @see Maintenance::getDbType() @@ -115,11 +114,11 @@ abstract class Maintenance { public $fileHandle; /** - * List of all the core maintenance scripts. This is added - * to scripts added by extensions in $wgMaintenanceScripts - * and returned by getMaintenanceScripts() + * Accessible via getConfig() + * + * @var Config */ - protected static $mCoreScripts = null; + private $config; /** * Default constructor. Children should call this *first* if implementing @@ -141,9 +140,16 @@ abstract class Maintenance { * as a standalone class? It checks that the call stack only includes this * function and "requires" (meaning was called from the file scope) * - * @return Boolean + * @return bool */ public static function shouldExecute() { + global $wgCommandLineMode; + + if ( !function_exists( 'debug_backtrace' ) ) { + // If someone has a better idea... + return $wgCommandLineMode; + } + $bt = debug_backtrace(); $count = count( $bt ); if ( $count < 2 ) { @@ -158,6 +164,7 @@ abstract class Maintenance { return false; // previous calls should all be "requires" } } + return true; } @@ -170,14 +177,22 @@ abstract class Maintenance { * Add a parameter to the script. Will be displayed on --help * with the associated description * - * @param $name String: the name of the param (help, version, etc) - * @param $description String: the description of the param to show on --help - * @param $required Boolean: is the param required? - * @param $withArg Boolean: is an argument required with this option? - * @param $shortName String: character to use as short name + * @param string $name The name of the param (help, version, etc) + * @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 */ - protected function addOption( $name, $description, $required = false, $withArg = false, $shortName = false ) { - $this->mParams[$name] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg, 'shortName' => $shortName ); + protected function addOption( $name, $description, $required = false, + $withArg = false, $shortName = false + ) { + $this->mParams[$name] = array( + 'desc' => $description, + 'require' => $required, + 'withArg' => $withArg, + 'shortName' => $shortName + ); + if ( $shortName !== false ) { $this->mShortParamsMap[$shortName] = $name; } @@ -185,8 +200,8 @@ abstract class Maintenance { /** * Checks to see if a particular param exists. - * @param $name String: the name of the param - * @return Boolean + * @param string $name The name of the param + * @return bool */ protected function hasOption( $name ) { return isset( $this->mOptions[$name] ); @@ -194,9 +209,9 @@ abstract class Maintenance { /** * Get an option, or return the default - * @param $name String: the name of the param - * @param $default Mixed: anything you want, default null - * @return Mixed + * @param string $name The name of the param + * @param mixed $default Anything you want, default null + * @return mixed */ protected function getOption( $name, $default = null ) { if ( $this->hasOption( $name ) ) { @@ -204,15 +219,16 @@ abstract class Maintenance { } else { // Set it so we don't have to provide the default again $this->mOptions[$name] = $default; + return $this->mOptions[$name]; } } /** * 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? + * @param string $arg Name of the arg, like 'start' + * @param string $description Short description of the arg + * @param bool $required Is this required? */ protected function addArg( $arg, $description, $required = true ) { $this->mArgList[] = array( @@ -224,7 +240,7 @@ abstract class Maintenance { /** * Remove an option. Useful for removing options that won't be used in your script. - * @param $name String: the option to remove. + * @param string $name The option to remove. */ protected function deleteOption( $name ) { unset( $this->mParams[$name] ); @@ -232,7 +248,7 @@ abstract class Maintenance { /** * Set the description text. - * @param $text String: the text of the description + * @param string $text The text of the description */ protected function addDescription( $text ) { $this->mDescription = $text; @@ -240,8 +256,8 @@ abstract class Maintenance { /** * Does a given argument exist? - * @param $argId Integer: the integer value (from zero) for the arg - * @return Boolean + * @param int $argId The integer value (from zero) for the arg + * @return bool */ protected function hasArg( $argId = 0 ) { return isset( $this->mArgs[$argId] ); @@ -249,8 +265,8 @@ abstract class Maintenance { /** * Get an argument. - * @param $argId Integer: the integer value (from zero) for the arg - * @param $default Mixed: the default if it doesn't exist + * @param int $argId The integer value (from zero) for the arg + * @param mixed $default The default if it doesn't exist * @return mixed */ protected function getArg( $argId = 0, $default = null ) { @@ -259,7 +275,7 @@ abstract class Maintenance { /** * Set the batch size. - * @param $s Integer: the number of operations to do in a batch + * @param int $s The number of operations to do in a batch */ protected function setBatchSize( $s = 0 ) { $this->mBatchSize = $s; @@ -281,7 +297,7 @@ abstract class Maintenance { /** * Get the script's name - * @return String + * @return string */ public function getName() { return $this->mSelf; @@ -289,10 +305,9 @@ abstract class Maintenance { /** * Return input from stdin. - * @param $len Integer: the number of bytes to read. If null, - * just return the handle. Maintenance::STDIN_ALL returns - * the full length - * @return Mixed + * @param int $len The number of bytes to read. If null, just return the handle. + * Maintenance::STDIN_ALL returns the full length + * @return mixed */ protected function getStdin( $len = null ) { if ( $len == Maintenance::STDIN_ALL ) { @@ -304,6 +319,7 @@ abstract class Maintenance { } $input = fgets( $f, $len ); fclose( $f ); + return rtrim( $input ); } @@ -317,9 +333,8 @@ abstract class Maintenance { /** * Throw some output to the user. Scripts can call this with no fears, * as we handle all --quiet stuff here - * @param $out String: the text to show to the user - * @param $channel Mixed: unique identifier for the channel. See - * function outputChanneled. + * @param string $out The text to show to the user + * @param mixed $channel Unique identifier for the channel. See function outputChanneled. */ protected function output( $out, $channel = null ) { if ( $this->mQuiet ) { @@ -337,8 +352,8 @@ abstract class Maintenance { /** * Throw an error to the user. Doesn't respect --quiet, so don't use * this for non-error output - * @param $err String: the error to display - * @param $die Int: if > 0, go ahead and die out using this int as the code + * @param string $err The error to display + * @param int $die If > 0, go ahead and die out using this int as the code */ protected function error( $err, $die = 0 ) { $this->outputChanneled( false ); @@ -370,13 +385,14 @@ abstract class Maintenance { * Message outputter with channeled message support. Messages on the * same channel are concatenated, but any intervening messages in another * channel start a new line. - * @param $msg String: the message without trailing newline - * @param $channel string Channel identifier or null for no + * @param string $msg The message without trailing newline + * @param string $channel Channel identifier or null for no * channel. Channel comparison uses ===. */ public function outputChanneled( $msg, $channel = null ) { if ( $msg === false ) { $this->cleanupChanneled(); + return; } @@ -404,7 +420,7 @@ abstract class Maintenance { * Maintenance::DB_NONE - For no DB access at all * Maintenance::DB_STD - For normal DB access, default * Maintenance::DB_ADMIN - For admin DB access - * @return Integer + * @return int */ public function getDbType() { return Maintenance::DB_STD; @@ -422,10 +438,14 @@ abstract class Maintenance { $this->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true ); $this->addOption( 'wiki', 'For specifying the wiki ID', false, true ); $this->addOption( 'globals', 'Output globals at the end of processing for debugging' ); - $this->addOption( 'memory-limit', 'Set a specific memory limit for the script, "max" for no limit or "default" to avoid changing it' ); + $this->addOption( + 'memory-limit', + 'Set a specific memory limit for the script, ' + . '"max" for no limit or "default" to avoid changing it' + ); $this->addOption( 'server', "The protocol and server name to use in URLs, e.g. " . - "http://en.wikipedia.org. This is sometimes necessary because " . - "server name detection may fail in command line scripts.", false, true ); + "http://en.wikipedia.org. This is sometimes necessary because " . + "server name detection may fail in command line scripts.", false, true ); $this->addOption( 'profiler', 'Set to "text" or "trace" to show profiling output', false, true ); # Save generic options to display them separately in help @@ -445,11 +465,31 @@ abstract class Maintenance { } /** + * @since 1.24 + * @return Config + */ + public function getConfig() { + if ( $this->config === null ) { + $this->config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' ); + } + + return $this->config; + } + + /** + * @since 1.24 + * @param Config $config + */ + public function setConfig( Config $config ) { + $this->config = $config; + } + + /** * Run a child maintenance script. Pass all of the current arguments * to it. - * @param $maintClass String: a name of a child maintenance class - * @param $classFile String: full path of where the child is - * @return Maintenance child + * @param string $maintClass A name of a child maintenance class + * @param string $classFile Full path of where the child is + * @return Maintenance */ public function runChild( $maintClass, $classFile = null ) { // Make sure the class is loaded first @@ -470,6 +510,7 @@ abstract class Maintenance { if ( !is_null( $this->mDb ) ) { $child->setDB( $this->mDb ); } + return $child; } @@ -570,9 +611,9 @@ abstract class Maintenance { * $mOptions becomes an array with keys set to the option names * $mArgs becomes a zero-based array containing the non-option arguments * - * @param $self String The name of the script, if any - * @param $opts Array An array of options, in form of key=>value - * @param $args Array An array of command line arguments + * @param string $self The name of the script, if any + * @param array $opts An array of options, in form of key=>value + * @param array $args An array of command line arguments */ public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) { # If we were given opts or args, set those and return early @@ -594,6 +635,7 @@ abstract class Maintenance { # it's run again and again if ( $this->mInputLoaded ) { $this->loadSpecialVars(); + return; } @@ -639,8 +681,9 @@ abstract class Maintenance { } } elseif ( substr( $arg, 0, 1 ) == '-' ) { # Short options - for ( $p = 1; $p < strlen( $arg ); $p++ ) { - $option = $arg { $p }; + $argLength = strlen( $arg ); + for ( $p = 1; $p < $argLength; $p++ ) { + $option = $arg[$p]; if ( !isset( $this->mParams[$option] ) && isset( $this->mShortParamsMap[$option] ) ) { $option = $this->mShortParamsMap[$option]; } @@ -715,14 +758,14 @@ abstract class Maintenance { /** * Maybe show the help. - * @param $force boolean Whether to force the help to show, default false + * @param bool $force Whether to force the help to show, default false */ protected function maybeHelp( $force = false ) { if ( !$force && !$this->hasOption( 'help' ) ) { return; } - $screenWidth = 80; // TODO: Caculate this! + $screenWidth = 80; // TODO: Calculate this! $tab = " "; $descWidth = $screenWidth - ( 2 * strlen( $tab ) ); @@ -766,7 +809,7 @@ abstract class Maintenance { } $this->output( wordwrap( "$tab--$par: " . $info['desc'], $descWidth, - "\n$tab$tab" ) . "\n" + "\n$tab$tab" ) . "\n" ); } $this->output( "\n" ); @@ -781,13 +824,12 @@ abstract class Maintenance { } $this->output( wordwrap( "$tab--$par: " . $info['desc'], $descWidth, - "\n$tab$tab" ) . "\n" + "\n$tab$tab" ) . "\n" ); } $this->output( "\n" ); } - // Script specific parameters not defined on construction by // Maintenance::addDefaultParams() $scriptSpecificParams = array_diff_key( @@ -806,7 +848,7 @@ abstract class Maintenance { } $this->output( wordwrap( "$tab--$par: " . $info['desc'], $descWidth, - "\n$tab$tab" ) . "\n" + "\n$tab$tab" ) . "\n" ); } $this->output( "\n" ); @@ -881,7 +923,12 @@ abstract class Maintenance { $this->afterFinalSetup(); $wgShowSQLErrors = true; + + // @codingStandardsIgnoreStart Allow error supppression. wfSuppressWarnings() + // is not avaiable. @set_time_limit( 0 ); + // @codingStandardsIgnoreStart + $this->adjustMemoryLimit(); // Per-script profiling; useful for debugging @@ -916,7 +963,7 @@ abstract class Maintenance { /** * Generic setup for most installs. Returns the location of LocalSettings - * @return String + * @return string */ public function loadSettings() { global $wgCommandLineMode, $IP; @@ -939,16 +986,17 @@ abstract class Maintenance { if ( !is_readable( $settingsFile ) ) { $this->error( "A copy of your installation's LocalSettings.php\n" . - "must exist and be readable in the source directory.\n" . - "Use --conf to specify it.", true ); + "must exist and be readable in the source directory.\n" . + "Use --conf to specify it.", true ); } $wgCommandLineMode = true; + return $settingsFile; } /** * Support function for cleaning up redundant text records - * @param $delete Boolean: whether or not to actually delete the records + * @param bool $delete Whether or not to actually delete the records * @author Rob Church <robchur@gmail.com> */ public function purgeRedundantText( $delete = true ) { @@ -1009,52 +1057,6 @@ abstract class Maintenance { } /** - * Get the list of available maintenance scripts. Note - * that if you call this _before_ calling doMaintenance - * you won't have any extensions in it yet - * @return Array - */ - public static function getMaintenanceScripts() { - global $wgMaintenanceScripts; - return $wgMaintenanceScripts + self::getCoreScripts(); - } - - /** - * Return all of the core maintenance scripts - * @return array - */ - protected static function getCoreScripts() { - if ( !self::$mCoreScripts ) { - $paths = array( - __DIR__, - __DIR__ . '/language', - __DIR__ . '/storage', - ); - self::$mCoreScripts = array(); - foreach ( $paths as $p ) { - $handle = opendir( $p ); - while ( ( $file = readdir( $handle ) ) !== false ) { - if ( $file == 'Maintenance.php' ) { - continue; - } - $file = $p . '/' . $file; - if ( is_dir( $file ) || !strpos( $file, '.php' ) || - ( strpos( file_get_contents( $file ), '$maintClass' ) === false ) ) { - continue; - } - require $file; - $vars = get_defined_vars(); - if ( array_key_exists( 'maintClass', $vars ) ) { - self::$mCoreScripts[$vars['maintClass']] = $file; - } - } - closedir( $handle ); - } - } - return self::$mCoreScripts; - } - - /** * Returns a database to be used by current maintenance script. It can be set by setDB(). * If not set, wfGetDB() will be used. * This function has the same parameters as wfGetDB() @@ -1072,7 +1074,7 @@ abstract class Maintenance { /** * Sets database object to be returned by getDB(). * - * @param $db DatabaseBase: Database object to be used + * @param DatabaseBase $db Database object to be used */ public function setDB( &$db ) { $this->mDb = $db; @@ -1080,7 +1082,7 @@ abstract class Maintenance { /** * Lock the search index - * @param &$db DatabaseBase object + * @param DatabaseBase &$db */ private function lockSearchindex( &$db ) { $write = array( 'searchindex' ); @@ -1090,7 +1092,7 @@ abstract class Maintenance { /** * Unlock the tables - * @param &$db DatabaseBase object + * @param DatabaseBase &$db */ private function unlockSearchindex( &$db ) { $db->unlockTables( __CLASS__ . '::' . __METHOD__ ); @@ -1099,7 +1101,7 @@ abstract class Maintenance { /** * Unlock and lock again * Since the lock is low-priority, queued reads will be able to complete - * @param &$db DatabaseBase object + * @param DatabaseBase &$db */ private function relockSearchindex( &$db ) { $this->unlockSearchindex( $db ); @@ -1108,10 +1110,10 @@ abstract class Maintenance { /** * Perform a search index update with locking - * @param $maxLockTime Integer: the maximum time to keep the search index locked. - * @param $callback callback String: the function that will update the function. - * @param $dbw DatabaseBase object - * @param $results + * @param int $maxLockTime The maximum time to keep the search index locked. + * @param string $callback The function that will update the function. + * @param DatabaseBase $dbw + * @param array $results */ public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) { $lockTime = time(); @@ -1142,13 +1144,12 @@ abstract class Maintenance { $this->unlockSearchindex( $dbw ); $this->output( "\n" ); } - } /** * Update the searchindex table for a given pageid - * @param $dbw DatabaseBase a database write handle - * @param $pageId Integer: the page ID to update. + * @param DatabaseBase $dbw A database write handle + * @param int $pageId The page ID to update. * @return null|string */ public function updateSearchIndexForPage( $dbw, $pageId ) { @@ -1164,6 +1165,7 @@ abstract class Maintenance { $u->doUpdate(); $this->output( "\n" ); } + return $title; } @@ -1172,7 +1174,7 @@ abstract class Maintenance { * We default as considering stdin a tty (for nice readline methods) * but treating stout as not a tty to avoid color codes * - * @param $fd int File descriptor + * @param int $fd File descriptor * @return bool */ public static function posix_isatty( $fd ) { @@ -1185,8 +1187,8 @@ abstract class Maintenance { /** * Prompt the console for input - * @param $prompt String what to begin the line with, like '> ' - * @return String response + * @param string $prompt What to begin the line with, like '> ' + * @return string Response */ public static function readconsole( $prompt = '> ' ) { static $isatty = null; @@ -1210,14 +1212,15 @@ abstract class Maintenance { return false; } $resp = trim( $st ); + return $resp; } } /** * Emulate readline() - * @param $prompt String what to begin the line with, like '> ' - * @return String + * @param string $prompt What to begin the line with, like '> ' + * @return string */ private static function readlineEmulation( $prompt ) { $bash = Installer::locateExecutableInDefaultPaths( array( 'bash' ) ); @@ -1245,6 +1248,7 @@ abstract class Maintenance { return false; } print $prompt; + return fgets( STDIN, 1024 ); } } @@ -1254,6 +1258,7 @@ abstract class Maintenance { */ class FakeMaintenance extends Maintenance { protected $mSelf = "FakeMaintenanceScript"; + public function execute() { return; } @@ -1274,10 +1279,11 @@ abstract class LoggedUpdateMaintenance extends Maintenance { $db = $this->getDB( DB_MASTER ); $key = $this->getUpdateKey(); - if ( !$this->hasOption( 'force' ) && - $db->selectRow( 'updatelog', '1', array( 'ul_key' => $key ), __METHOD__ ) ) - { + if ( !$this->hasOption( 'force' ) + && $db->selectRow( 'updatelog', '1', array( 'ul_key' => $key ), __METHOD__ ) + ) { $this->output( "..." . $this->updateSkippedMessage() . "\n" ); + return true; } @@ -1285,44 +1291,45 @@ abstract class LoggedUpdateMaintenance extends Maintenance { return false; } - if ( - $db->insert( 'updatelog', array( 'ul_key' => $key ), __METHOD__, 'IGNORE' ) ) - { + if ( $db->insert( 'updatelog', array( 'ul_key' => $key ), __METHOD__, 'IGNORE' ) ) { return true; } else { $this->output( $this->updatelogFailedMessage() . "\n" ); + return false; } } /** * Message to show that the update was done already and was just skipped - * @return String + * @return string */ protected function updateSkippedMessage() { $key = $this->getUpdateKey(); + return "Update '{$key}' already logged as completed."; } /** * Message to show the the update log was unable to log the completion of this update - * @return String + * @return string */ protected function updatelogFailedMessage() { $key = $this->getUpdateKey(); + return "Unable to log update '{$key}' as completed."; } /** * Do the actual work. All child classes will need to implement this. * Return true to log the update as done or false (usually on failure). - * @return Bool + * @return bool */ abstract protected function doDBUpdates(); /** * Get the update key name to go in the update log table - * @return String + * @return string */ abstract protected function getUpdateKey(); } |