Remove unneeded $classes from SpecialTrackingCategories.php
[lhc/web/wiklou.git] / maintenance / Maintenance.php
index d9ef350..e316d9e 100644 (file)
@@ -49,7 +49,6 @@ $maintClass = false;
  * @ingroup Maintenance
  */
 abstract class Maintenance {
-
        /**
         * Constants for DB access type
         * @see Maintenance::getDbType()
@@ -134,7 +133,7 @@ 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() {
                $bt = debug_backtrace();
@@ -151,6 +150,7 @@ abstract class Maintenance {
                                return false; // previous calls should all be "requires"
                        }
                }
+
                return true;
        }
 
@@ -163,14 +163,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;
                }
@@ -178,8 +186,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] );
@@ -187,9 +195,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 ) ) {
@@ -197,15 +205,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(
@@ -217,7 +226,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] );
@@ -225,7 +234,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;
@@ -233,8 +242,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] );
@@ -242,8 +251,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 ) {
@@ -252,7 +261,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;
@@ -274,7 +283,7 @@ abstract class Maintenance {
 
        /**
         * Get the script's name
-        * @return String
+        * @return string
         */
        public function getName() {
                return $this->mSelf;
@@ -282,10 +291,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 ) {
@@ -297,6 +305,7 @@ abstract class Maintenance {
                }
                $input = fgets( $f, $len );
                fclose( $f );
+
                return rtrim( $input );
        }
 
@@ -310,9 +319,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 ) {
@@ -330,8 +338,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 );
@@ -363,13 +371,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;
                }
 
@@ -397,7 +406,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;
@@ -415,10 +424,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
@@ -440,9 +453,9 @@ abstract class Maintenance {
        /**
         * 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
@@ -463,6 +476,7 @@ abstract class Maintenance {
                if ( !is_null( $this->mDb ) ) {
                        $child->setDB( $this->mDb );
                }
+
                return $child;
        }
 
@@ -563,9 +577,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
@@ -587,6 +601,7 @@ abstract class Maintenance {
                # it's run again and again
                if ( $this->mInputLoaded ) {
                        $this->loadSpecialVars();
+
                        return;
                }
 
@@ -632,8 +647,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];
                                        }
@@ -708,14 +724,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 ) );
 
@@ -759,7 +775,7 @@ abstract class Maintenance {
                        }
                        $this->output(
                                wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
-                                               "\n$tab$tab" ) . "\n"
+                                       "\n$tab$tab" ) . "\n"
                        );
                }
                $this->output( "\n" );
@@ -774,7 +790,7 @@ abstract class Maintenance {
                                }
                                $this->output(
                                        wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
-                                                       "\n$tab$tab" ) . "\n"
+                                               "\n$tab$tab" ) . "\n"
                                );
                        }
                        $this->output( "\n" );
@@ -798,7 +814,7 @@ abstract class Maintenance {
                                }
                                $this->output(
                                        wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
-                                                       "\n$tab$tab" ) . "\n"
+                                               "\n$tab$tab" ) . "\n"
                                );
                        }
                        $this->output( "\n" );
@@ -873,7 +889,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
@@ -908,7 +929,7 @@ abstract class Maintenance {
 
        /**
         * Generic setup for most installs. Returns the location of LocalSettings
-        * @return String
+        * @return string
         */
        public function loadSettings() {
                global $wgCommandLineMode, $IP;
@@ -931,16 +952,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 ) {
@@ -1018,7 +1040,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;
@@ -1026,7 +1048,7 @@ abstract class Maintenance {
 
        /**
         * Lock the search index
-        * @param &$db DatabaseBase object
+        * @param DatabaseBase &$db
         */
        private function lockSearchindex( &$db ) {
                $write = array( 'searchindex' );
@@ -1036,7 +1058,7 @@ abstract class Maintenance {
 
        /**
         * Unlock the tables
-        * @param &$db DatabaseBase object
+        * @param DatabaseBase &$db
         */
        private function unlockSearchindex( &$db ) {
                $db->unlockTables( __CLASS__ . '::' . __METHOD__ );
@@ -1045,7 +1067,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 );
@@ -1054,10 +1076,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();
@@ -1088,13 +1110,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 ) {
@@ -1110,6 +1131,7 @@ abstract class Maintenance {
                        $u->doUpdate();
                        $this->output( "\n" );
                }
+
                return $title;
        }
 
@@ -1118,7 +1140,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 ) {
@@ -1131,8 +1153,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;
@@ -1156,14 +1178,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' ) );
@@ -1191,6 +1214,7 @@ abstract class Maintenance {
                        return false;
                }
                print $prompt;
+
                return fgets( STDIN, 1024 );
        }
 }
@@ -1200,6 +1224,7 @@ abstract class Maintenance {
  */
 class FakeMaintenance extends Maintenance {
        protected $mSelf = "FakeMaintenanceScript";
+
        public function execute() {
                return;
        }
@@ -1224,6 +1249,7 @@ abstract class LoggedUpdateMaintenance extends Maintenance {
                        && $db->selectRow( 'updatelog', '1', array( 'ul_key' => $key ), __METHOD__ )
                ) {
                        $this->output( "..." . $this->updateSkippedMessage() . "\n" );
+
                        return true;
                }
 
@@ -1235,38 +1261,41 @@ abstract class LoggedUpdateMaintenance extends Maintenance {
                        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();
 }