* Fix r74790 by actually calling setup (using parent::setup)
[lhc/web/wiklou.git] / maintenance / Maintenance.php
index 3614adf..84c1535 100644 (file)
@@ -9,13 +9,18 @@
 define( 'DO_MAINTENANCE', dirname( __FILE__ ) . '/doMaintenance.php' );
 $maintClass = false;
 
+function wfRunMaintenance( $class ) {
+       $maintClass = $class;
+       require_once( DO_MAINTENANCE );
+}
+
 // Make sure we're on PHP5 or better
-if ( version_compare( PHP_VERSION, '5.0.0' ) < 0 ) {
-       echo( "Sorry! This version of MediaWiki requires PHP 5; you are running " .
+if ( version_compare( PHP_VERSION, '5.1.0' ) < 0 ) {
+       die ( "Sorry! This version of MediaWiki requires PHP 5.1.x; you are running " .
                PHP_VERSION . ".\n\n" .
-               "If you are sure you already have PHP 5 installed, it may be installed\n" .
-               "in a different path from PHP 4. Check with your system administrator.\n" );
-       die();
+               "If you are sure you already have PHP 5.1.x or higher installed, it may be\n" .
+               "installed in a different path from PHP " . PHP_VERSION . ". Check with your system\n" .
+               "administrator.\n" );
 }
 
 /**
@@ -158,6 +163,22 @@ 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.
+        */
+       protected function deleteOption( $name ) {
+               unset( $this->mParams[$name] );
+       }
+
+       /**
+        * Set the description text.
+        * @param $text String: the text of the description
+        */
+       protected function addDescription( $text ) {
+               $this->mDescription = $text;
+       }
+
        /**
         * Does a given argument exist?
         * @param $argId Integer: the integer value (from zero) for the arg
@@ -195,7 +216,7 @@ abstract class Maintenance {
 
        /**
         * Return input from stdin.
-        * @param $length Integer: the number of bytes to read. If null,
+        * @param $len Integer: the number of bytes to read. If null,
         *        just return the handle. Maintenance::STDIN_ALL returns
         *        the full length
         * @return Mixed
@@ -217,13 +238,16 @@ 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 $channel Mixed: unique identifier for the channel. See
+        *     function outputChanneled.
         */
        protected function output( $out, $channel = null ) {
                if ( $this->mQuiet ) {
                        return;
                }
                if ( $channel === null ) {
+                       $this->cleanupChanneled();
+
                        $f = fopen( 'php://stdout', 'w' );
                        fwrite( $f, $out );
                        fclose( $f );
@@ -256,26 +280,35 @@ abstract class Maintenance {
 
        private $atLineStart = true;
        private $lastChannel = null;
-       
+
+       /**
+        * Clean up channeled output.  Output a newline if necessary.
+        */
+       public function cleanupChanneled() {
+               if ( !$this->atLineStart ) {
+                       $handle = fopen( 'php://stdout', 'w' );
+                       fwrite( $handle, "\n" );
+                       fclose( $handle );
+                       $this->atLineStart = true;
+               }
+       }
+
        /**
         * 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 Channel identifier or null for no channel. Channel comparison uses ===.
+        * @param $channel Channel identifier or null for no
+        *     channel. Channel comparison uses ===.
         */
        public function outputChanneled( $msg, $channel = null ) {
-               $handle = fopen( 'php://stdout', 'w' );
-
                if ( $msg === false ) {
-                       // For cleanup
-                       if ( !$this->atLineStart ) {
-                               fwrite( $handle, "\n" );
-                       }
-                       fclose( $handle );
+                       $this->cleanupChanneled();
                        return;
                }
 
+               $handle = fopen( 'php://stdout', 'w' );
+
                // End the current line if necessary
                if ( !$this->atLineStart && $channel !== $this->lastChannel ) {
                        fwrite( $handle, "\n" );
@@ -373,7 +406,7 @@ abstract class Maintenance {
                global $IP, $wgCommandLineMode, $wgRequestTime;
 
                # Abort if called from a web server
-               if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
+               if ( isset( $_SERVER ) && isset( $_SERVER['REQUEST_METHOD'] ) ) {
                        $this->error( 'This script must be run from the command line', true );
                }
 
@@ -556,7 +589,7 @@ abstract class Maintenance {
                                $die = true;
                        }
                }
-               
+
                if ( $die ) {
                        $this->maybeHelp( true );
                }
@@ -588,7 +621,7 @@ abstract class Maintenance {
                $screenWidth = 80; // TODO: Caculate this!
                $tab = "    ";
                $descWidth = $screenWidth - ( 2 * strlen( $tab ) );
-               
+
                ksort( $this->mParams );
                if ( $this->hasOption( 'help' ) || $force ) {
                        $this->mQuiet = false;
@@ -630,7 +663,7 @@ abstract class Maintenance {
         */
        public function finalSetup() {
                global $wgCommandLineMode, $wgShowSQLErrors;
-               global $wgProfiling, $IP, $wgDBadminuser, $wgDBadminpassword;
+               global $wgProfiling, $wgDBadminuser, $wgDBadminpassword;
                global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
 
                # Turn off output buffering again, it might have been turned on in the settings files
@@ -662,12 +695,10 @@ abstract class Maintenance {
                                $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
                                $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
                        }
+                       LBFactory::destroyInstance();
                }
 
-               if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
-                       $fn = MW_CMDLINE_CALLBACK;
-                       $fn();
-               }
+               $this->afterFinalSetup();
 
                $wgShowSQLErrors = true;
                @set_time_limit( 0 );
@@ -676,6 +707,15 @@ abstract class Maintenance {
                $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
        }
 
+       /**
+        * Execute a callback function at the end of initialisation
+        */
+       protected function afterFinalSetup() {
+               if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
+                       call_user_func( MW_CMDLINE_CALLBACK );
+               }
+       }
+
        /**
         * Potentially debug globals. Originally a feature only
         * for refreshLinks
@@ -728,7 +768,6 @@ abstract class Maintenance {
 
                putenv( 'wikilang=' . $lang );
 
-               $DP = $IP;
                ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
 
                if ( $lang == 'test' && $site == 'wikipedia' ) {
@@ -741,7 +780,7 @@ abstract class Maintenance {
         * @return String
         */
        public function loadSettings() {
-               global $wgWikiFarm, $wgCommandLineMode, $IP, $DP;
+               global $wgWikiFarm, $wgCommandLineMode, $IP;
 
                $wgWikiFarm = false;
                if ( isset( $this->mOptions['conf'] ) ) {
@@ -760,10 +799,9 @@ 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.", true );
+                                               "must exist and be readable in the source directory.", true );
                }
                $wgCommandLineMode = true;
-               $DP = $IP;
                return $settingsFile;
        }
 
@@ -772,7 +810,7 @@ abstract class Maintenance {
         * @param $delete Boolean: whether or not to actually delete the records
         * @author Rob Church <robchur@gmail.com>
         */
-       protected function purgeRedundantText( $delete = true ) {
+       public function purgeRedundantText( $delete = true ) {
                # Data should come off the master, wrapped in a transaction
                $dbw = wfGetDB( DB_MASTER );
                $dbw->begin();