<?php
/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
* @file
* @ingroup Maintenance
* @defgroup Maintenance Maintenance
*/
// Define this so scripts can easily find doMaintenance.php
-define( 'DO_MAINTENANCE', dirname( __FILE__ ) . '/doMaintenance.php' );
+define( 'RUN_MAINTENANCE_IF_MAIN', dirname( __FILE__ ) . '/doMaintenance.php' );
+define( 'DO_MAINTENANCE', RUN_MAINTENANCE_IF_MAIN ); // original name, harmless
+
$maintClass = false;
// Make sure we're on PHP5 or better
-if ( version_compare( PHP_VERSION, '5.1.0' ) < 0 ) {
- die ( "Sorry! This version of MediaWiki requires PHP 5.1.x; you are running " .
+if ( version_compare( PHP_VERSION, '5.2.3' ) < 0 ) {
+ die ( "Sorry! This version of MediaWiki requires PHP 5.2.3; you are running " .
PHP_VERSION . ".\n\n" .
- "If you are sure you already have PHP 5.1.x or higher installed, it may be\n" .
+ "If you are sure you already have PHP 5.2.3 or higher installed, it may be\n" .
"installed in a different path from PHP " . PHP_VERSION . ". Check with your system\n" .
"administrator.\n" );
}
* is the execute() method. See docs/maintenance.txt for more info
* and a quick demo of how to use it.
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
* @author Chad Horohoe <chad@anyonecanedit.org>
* @since 1.16
* @ingroup Maintenance
// a default with setBatchSize()
protected $mBatchSize = null;
+ // Generic options added by addDefaultParams()
+ private $mGenericParameters = array();
+ // Generic options which might or not be supported by the script
+ private $mDependantParameters = array();
+
/**
* List of all the core maintenance scripts. This is added
* to scripts added by extensions in $wgMaintenanceScripts
register_shutdown_function( array( $this, 'outputChanneled' ), false );
}
+ /**
+ * Should we execute the maintenance script, or just allow it to be included
+ * as a standalone class? It checks that the call stack only includes this
+ * function and a require (meaning was called from the file scope)
+ *
+ * @return Boolean
+ */
+ public static function shouldExecute() {
+ $bt = debug_backtrace();
+ if( count( $bt ) !== 2 ) {
+ return false;
+ }
+ return ( $bt[1]['function'] == 'require_once' || $bt[1]['function'] == 'require' ) &&
+ $bt[0]['class'] == 'Maintenance' &&
+ $bt[0]['function'] == 'shouldExecute';
+ }
+
/**
* Do the actual work. All child classes will need to implement this
*/
* Add the default parameters to the scripts
*/
protected function addDefaultParams() {
+
+ # Generic (non script dependant) options:
+
$this->addOption( 'help', 'Display this help message' );
$this->addOption( 'quiet', 'Whether to supress non-error output' );
$this->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true );
$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 );
+
+ # Save generic options to display them separately in help
+ $this->mGenericParameters = $this->mParams ;
+
+ # Script dependant options:
+
// If we support a DB, show the options
if ( $this->getDbType() > 0 ) {
$this->addOption( 'dbuser', 'The DB user to use for this script', false, true );
$this->addOption( 'batch-size', 'Run this many operations ' .
'per batch, default: ' . $this->mBatchSize, false, true );
}
+ # Save additional script dependant options to display
+ # them separately in help
+ $this->mDependantParameters = array_diff_key( $this->mParams, $this->mGenericParameters );
}
/**
* @return Maintenance child
*/
public function runChild( $maintClass, $classFile = null ) {
- // If we haven't already specified, kill setup procedures
- // for child scripts, we've already got a sane environment
- self::disableSetup();
-
// Make sure the class is loaded first
if ( !class_exists( $maintClass ) ) {
if ( $classFile ) {
return $child;
}
- /**
- * Disable Setup.php mostly
- */
- protected static function disableSetup() {
- if ( !defined( 'MW_NO_SETUP' ) ) {
- define( 'MW_NO_SETUP', true );
- }
- }
-
/**
* Do some sanity checking and basic setup
*/
}
$this->output( "$output\n\n" );
- // Parameters description
- foreach ( $this->mParams as $par => $info ) {
+ # TODO abstract some repetitive code below
+
+ // Generic parameters
+ $this->output( "Generic maintenance parameters:\n" );
+ foreach ( $this->mGenericParameters as $par => $info ) {
$this->output(
wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
"\n$tab$tab" ) . "\n"
);
}
+ $this->output( "\n" );
+
+ $scriptDependantParams = $this->mDependantParameters;
+ if( count($scriptDependantParams) > 0 ) {
+ $this->output( "Script dependant parameters:\n" );
+ // Parameters description
+ foreach ( $scriptDependantParams as $par => $info ) {
+ $this->output(
+ wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
+ "\n$tab$tab" ) . "\n"
+ );
+ }
+ $this->output( "\n" );
+ }
- // Arguments description
- foreach ( $this->mArgList as $info ) {
- $openChar = $info['require'] ? '<' : '[';
- $closeChar = $info['require'] ? '>' : ']';
- $this->output(
- wordwrap( "$tab$openChar" . $info['name'] . "$closeChar: " .
- $info['desc'], $descWidth, "\n$tab$tab" ) . "\n"
- );
+
+ // Script specific parameters not defined on construction by
+ // Maintenance::addDefaultParams()
+ $scriptSpecificParams = array_diff_key(
+ # all script parameters:
+ $this->mParams,
+ # remove the Maintenance default parameters:
+ $this->mGenericParameters,
+ $this->mDependantParameters
+ );
+ if( count($scriptSpecificParams) > 0 ) {
+ $this->output( "Script specific parameters:\n" );
+ // Parameters description
+ foreach ( $scriptSpecificParams as $par => $info ) {
+ $this->output(
+ wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
+ "\n$tab$tab" ) . "\n"
+ );
+ }
+ $this->output( "\n" );
+ }
+
+ // Print arguments
+ if( count( $this->mArgList ) > 0 ) {
+ $this->output( "Arguments:\n" );
+ // Arguments description
+ foreach ( $this->mArgList as $info ) {
+ $openChar = $info['require'] ? '<' : '[';
+ $closeChar = $info['require'] ? '>' : ']';
+ $this->output(
+ wordwrap( "$tab$openChar" . $info['name'] . "$closeChar: " .
+ $info['desc'], $descWidth, "\n$tab$tab" ) . "\n"
+ );
+ }
+ $this->output( "\n" );
}
die( 1 );
*/
protected static function getCoreScripts() {
if ( !self::$mCoreScripts ) {
- self::disableSetup();
$paths = array(
dirname( __FILE__ ),
dirname( __FILE__ ) . '/gearman',