3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @ingroup Maintenance
20 * @defgroup Maintenance Maintenance
23 if( PHP_SAPI
!= 'cli' ) {
24 die( "Run me from the command line please.\n" );
27 // Make sure we're on PHP5.3.2 or better
28 if ( !function_exists( 'version_compare' ) ||
version_compare( PHP_VERSION
, '5.3.2' ) < 0 ) {
29 // We need to use dirname( __FILE__ ) here cause __DIR__ is PHP5.3+
30 require_once( dirname( __FILE__
) . '/../includes/PHPVersionError.php' );
31 wfPHPVersionError( 'cli' );
35 * @defgroup MaintenanceArchive Maintenance archives
36 * @ingroup Maintenance
39 // Define this so scripts can easily find doMaintenance.php
40 define( 'RUN_MAINTENANCE_IF_MAIN', __DIR__
. '/doMaintenance.php' );
41 define( 'DO_MAINTENANCE', RUN_MAINTENANCE_IF_MAIN
); // original name, harmless
46 * Abstract maintenance class for quickly writing and churning out
47 * maintenance scripts with minimal effort. All that _must_ be defined
48 * is the execute() method. See docs/maintenance.txt for more info
49 * and a quick demo of how to use it.
51 * @author Chad Horohoe <chad@anyonecanedit.org>
53 * @ingroup Maintenance
55 abstract class Maintenance
{
58 * Constants for DB access type
59 * @see Maintenance::getDbType()
65 // Const for getStdin()
66 const STDIN_ALL
= 'all';
68 // This is the desired params
69 protected $mParams = array();
71 // Array of mapping short parameters to long ones
72 protected $mShortParamsMap = array();
74 // Array of desired args
75 protected $mArgList = array();
77 // This is the list of options that were actually passed
78 protected $mOptions = array();
80 // This is the list of arguments that were actually passed
81 protected $mArgs = array();
83 // Name of the script currently running
86 // Special vars for params that are always used
87 protected $mQuiet = false;
88 protected $mDbUser, $mDbPass;
90 // A description of the script, children should change this
91 protected $mDescription = '';
93 // Have we already loaded our user input?
94 protected $mInputLoaded = false;
97 * Batch size. If a script supports this, they should set
98 * a default with setBatchSize()
102 protected $mBatchSize = null;
104 // Generic options added by addDefaultParams()
105 private $mGenericParameters = array();
106 // Generic options which might or not be supported by the script
107 private $mDependantParameters = array();
110 * Used by getDD() / setDB()
116 * Used when creating separate schema files.
122 * List of all the core maintenance scripts. This is added
123 * to scripts added by extensions in $wgMaintenanceScripts
124 * and returned by getMaintenanceScripts()
126 protected static $mCoreScripts = null;
129 * Default constructor. Children should call this *first* if implementing
130 * their own constructors
132 public function __construct() {
133 // Setup $IP, using MW_INSTALL_PATH if it exists
135 $IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== ''
136 ?
getenv( 'MW_INSTALL_PATH' )
137 : realpath( __DIR__
. '/..' );
139 $this->addDefaultParams();
140 register_shutdown_function( array( $this, 'outputChanneled' ), false );
144 * Should we execute the maintenance script, or just allow it to be included
145 * as a standalone class? It checks that the call stack only includes this
146 * function and "requires" (meaning was called from the file scope)
150 public static function shouldExecute() {
151 $bt = debug_backtrace();
152 $count = count( $bt );
154 return false; // sanity
156 if ( $bt[0]['class'] !== 'Maintenance' ||
$bt[0]['function'] !== 'shouldExecute' ) {
157 return false; // last call should be to this function
159 $includeFuncs = array( 'require_once', 'require', 'include', 'include_once' );
160 for( $i=1; $i < $count; $i++
) {
161 if ( !in_array( $bt[$i]['function'], $includeFuncs ) ) {
162 return false; // previous calls should all be "requires"
169 * Do the actual work. All child classes will need to implement this
171 abstract public function execute();
174 * Add a parameter to the script. Will be displayed on --help
175 * with the associated description
177 * @param $name String: the name of the param (help, version, etc)
178 * @param $description String: the description of the param to show on --help
179 * @param $required Boolean: is the param required?
180 * @param $withArg Boolean: is an argument required with this option?
181 * @param $shortName String: character to use as short name
183 protected function addOption( $name, $description, $required = false, $withArg = false, $shortName = false ) {
184 $this->mParams
[$name] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg, 'shortName' => $shortName );
185 if ( $shortName !== false ) {
186 $this->mShortParamsMap
[$shortName] = $name;
191 * Checks to see if a particular param exists.
192 * @param $name String: the name of the param
195 protected function hasOption( $name ) {
196 return isset( $this->mOptions
[$name] );
200 * Get an option, or return the default
201 * @param $name String: the name of the param
202 * @param $default Mixed: anything you want, default null
205 protected function getOption( $name, $default = null ) {
206 if ( $this->hasOption( $name ) ) {
207 return $this->mOptions
[$name];
209 // Set it so we don't have to provide the default again
210 $this->mOptions
[$name] = $default;
211 return $this->mOptions
[$name];
216 * Add some args that are needed
217 * @param $arg String: name of the arg, like 'start'
218 * @param $description String: short description of the arg
219 * @param $required Boolean: is this required?
221 protected function addArg( $arg, $description, $required = true ) {
222 $this->mArgList
[] = array(
224 'desc' => $description,
225 'require' => $required
230 * Remove an option. Useful for removing options that won't be used in your script.
231 * @param $name String: the option to remove.
233 protected function deleteOption( $name ) {
234 unset( $this->mParams
[$name] );
238 * Set the description text.
239 * @param $text String: the text of the description
241 protected function addDescription( $text ) {
242 $this->mDescription
= $text;
246 * Does a given argument exist?
247 * @param $argId Integer: the integer value (from zero) for the arg
250 protected function hasArg( $argId = 0 ) {
251 return isset( $this->mArgs
[$argId] );
256 * @param $argId Integer: the integer value (from zero) for the arg
257 * @param $default Mixed: the default if it doesn't exist
260 protected function getArg( $argId = 0, $default = null ) {
261 return $this->hasArg( $argId ) ?
$this->mArgs
[$argId] : $default;
265 * Set the batch size.
266 * @param $s Integer: the number of operations to do in a batch
268 protected function setBatchSize( $s = 0 ) {
269 $this->mBatchSize
= $s;
271 // If we support $mBatchSize, show the option.
272 // Used to be in addDefaultParams, but in order for that to
273 // work, subclasses would have to call this function in the constructor
274 // before they called parent::__construct which is just weird
275 // (and really wasn't done).
276 if ( $this->mBatchSize
) {
277 $this->addOption( 'batch-size', 'Run this many operations ' .
278 'per batch, default: ' . $this->mBatchSize
, false, true );
279 if ( isset( $this->mParams
['batch-size'] ) ) {
280 // This seems a little ugly...
281 $this->mDependantParameters
['batch-size'] = $this->mParams
['batch-size'];
287 * Get the script's name
290 public function getName() {
295 * Return input from stdin.
296 * @param $len Integer: the number of bytes to read. If null,
297 * just return the handle. Maintenance::STDIN_ALL returns
301 protected function getStdin( $len = null ) {
302 if ( $len == Maintenance
::STDIN_ALL
) {
303 return file_get_contents( 'php://stdin' );
305 $f = fopen( 'php://stdin', 'rt' );
309 $input = fgets( $f, $len );
311 return rtrim( $input );
317 public function isQuiet() {
318 return $this->mQuiet
;
322 * Throw some output to the user. Scripts can call this with no fears,
323 * as we handle all --quiet stuff here
324 * @param $out String: the text to show to the user
325 * @param $channel Mixed: unique identifier for the channel. See
326 * function outputChanneled.
328 protected function output( $out, $channel = null ) {
329 if ( $this->mQuiet
) {
332 if ( $channel === null ) {
333 $this->cleanupChanneled();
336 $out = preg_replace( '/\n\z/', '', $out );
337 $this->outputChanneled( $out, $channel );
342 * Throw an error to the user. Doesn't respect --quiet, so don't use
343 * this for non-error output
344 * @param $err String: the error to display
345 * @param $die Int: if > 0, go ahead and die out using this int as the code
347 protected function error( $err, $die = 0 ) {
348 $this->outputChanneled( false );
349 if ( PHP_SAPI
== 'cli' ) {
350 fwrite( STDERR
, $err . "\n" );
354 $die = intval( $die );
360 private $atLineStart = true;
361 private $lastChannel = null;
364 * Clean up channeled output. Output a newline if necessary.
366 public function cleanupChanneled() {
367 if ( !$this->atLineStart
) {
369 $this->atLineStart
= true;
374 * Message outputter with channeled message support. Messages on the
375 * same channel are concatenated, but any intervening messages in another
376 * channel start a new line.
377 * @param $msg String: the message without trailing newline
378 * @param $channel string Channel identifier or null for no
379 * channel. Channel comparison uses ===.
381 public function outputChanneled( $msg, $channel = null ) {
382 if ( $msg === false ) {
383 $this->cleanupChanneled();
387 // End the current line if necessary
388 if ( !$this->atLineStart
&& $channel !== $this->lastChannel
) {
394 $this->atLineStart
= false;
395 if ( $channel === null ) {
396 // For unchanneled messages, output trailing newline immediately
398 $this->atLineStart
= true;
400 $this->lastChannel
= $channel;
404 * Does the script need different DB access? By default, we give Maintenance
405 * scripts normal rights to the DB. Sometimes, a script needs admin rights
406 * access for a reason and sometimes they want no access. Subclasses should
407 * override and return one of the following values, as needed:
408 * Maintenance::DB_NONE - For no DB access at all
409 * Maintenance::DB_STD - For normal DB access, default
410 * Maintenance::DB_ADMIN - For admin DB access
413 public function getDbType() {
414 return Maintenance
::DB_STD
;
418 * Add the default parameters to the scripts
420 protected function addDefaultParams() {
422 # Generic (non script dependant) options:
424 $this->addOption( 'help', 'Display this help message', false, false, 'h' );
425 $this->addOption( 'quiet', 'Whether to supress non-error output', false, false, 'q' );
426 $this->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true );
427 $this->addOption( 'wiki', 'For specifying the wiki ID', false, true );
428 $this->addOption( 'globals', 'Output globals at the end of processing for debugging' );
429 $this->addOption( 'memory-limit', 'Set a specific memory limit for the script, "max" for no limit or "default" to avoid changing it' );
430 $this->addOption( 'server', "The protocol and server name to use in URLs, e.g. " .
431 "http://en.wikipedia.org. This is sometimes necessary because " .
432 "server name detection may fail in command line scripts.", false, true );
434 # Save generic options to display them separately in help
435 $this->mGenericParameters
= $this->mParams
;
437 # Script dependant options:
439 // If we support a DB, show the options
440 if ( $this->getDbType() > 0 ) {
441 $this->addOption( 'dbuser', 'The DB user to use for this script', false, true );
442 $this->addOption( 'dbpass', 'The password to use for this script', false, true );
445 # Save additional script dependant options to display
446 #Â them separately in help
447 $this->mDependantParameters
= array_diff_key( $this->mParams
, $this->mGenericParameters
);
451 * Run a child maintenance script. Pass all of the current arguments
453 * @param $maintClass String: a name of a child maintenance class
454 * @param $classFile String: full path of where the child is
455 * @return Maintenance child
457 public function runChild( $maintClass, $classFile = null ) {
458 // Make sure the class is loaded first
459 if ( !MWInit
::classExists( $maintClass ) ) {
461 require_once( $classFile );
463 if ( !MWInit
::classExists( $maintClass ) ) {
464 $this->error( "Cannot spawn child: $maintClass" );
469 * @var $child Maintenance
471 $child = new $maintClass();
472 $child->loadParamsAndArgs( $this->mSelf
, $this->mOptions
, $this->mArgs
);
473 if ( !is_null( $this->mDb
) ) {
474 $child->setDB( $this->mDb
);
480 * Do some sanity checking and basic setup
482 public function setup() {
483 global $wgCommandLineMode, $wgRequestTime;
485 # Abort if called from a web server
486 if ( isset( $_SERVER ) && isset( $_SERVER['REQUEST_METHOD'] ) ) {
487 $this->error( 'This script must be run from the command line', true );
490 # Make sure we can handle script parameters
491 if ( !function_exists( 'hphp_thread_set_warmup_enabled' ) && !ini_get( 'register_argc_argv' ) ) {
492 $this->error( 'Cannot get command line arguments, register_argc_argv is set to false', true );
495 // Send PHP warnings and errors to stderr instead of stdout.
496 // This aids in diagnosing problems, while keeping messages
497 // out of redirected output.
498 if ( ini_get( 'display_errors' ) ) {
499 ini_set( 'display_errors', 'stderr' );
502 $this->loadParamsAndArgs();
505 # Set the memory limit
506 # Note we need to set it again later in cache LocalSettings changed it
507 $this->adjustMemoryLimit();
509 # Set max execution time to 0 (no limit). PHP.net says that
510 # "When running PHP from the command line the default setting is 0."
511 # But sometimes this doesn't seem to be the case.
512 ini_set( 'max_execution_time', 0 );
514 $wgRequestTime = microtime( true );
516 # Define us as being in MediaWiki
517 define( 'MEDIAWIKI', true );
519 $wgCommandLineMode = true;
521 # Turn off output buffering if it's on
522 while( ob_get_level() > 0 ) {
526 $this->validateParamsAndArgs();
530 * Normally we disable the memory_limit when running admin scripts.
531 * Some scripts may wish to actually set a limit, however, to avoid
532 * blowing up unexpectedly. We also support a --memory-limit option,
533 * to allow sysadmins to explicitly set one if they'd prefer to override
534 * defaults (or for people using Suhosin which yells at you for trying
535 * to disable the limits)
538 public function memoryLimit() {
539 $limit = $this->getOption( 'memory-limit', 'max' );
540 $limit = trim( $limit, "\" '" ); // trim quotes in case someone misunderstood
545 * Adjusts PHP's memory limit to better suit our needs, if needed.
547 protected function adjustMemoryLimit() {
548 $limit = $this->memoryLimit();
549 if ( $limit == 'max' ) {
550 $limit = -1; // no memory limit
552 if ( $limit != 'default' ) {
553 ini_set( 'memory_limit', $limit );
558 * Clear all params and arguments.
560 public function clearParamsAndArgs() {
561 $this->mOptions
= array();
562 $this->mArgs
= array();
563 $this->mInputLoaded
= false;
567 * Process command line arguments
568 * $mOptions becomes an array with keys set to the option names
569 * $mArgs becomes a zero-based array containing the non-option arguments
571 * @param $self String The name of the script, if any
572 * @param $opts Array An array of options, in form of key=>value
573 * @param $args Array An array of command line arguments
575 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) {
576 # If we were given opts or args, set those and return early
578 $this->mSelf
= $self;
579 $this->mInputLoaded
= true;
582 $this->mOptions
= $opts;
583 $this->mInputLoaded
= true;
586 $this->mArgs
= $args;
587 $this->mInputLoaded
= true;
590 # If we've already loaded input (either by user values or from $argv)
591 # skip on loading it again. The array_shift() will corrupt values if
592 # it's run again and again
593 if ( $this->mInputLoaded
) {
594 $this->loadSpecialVars();
599 $this->mSelf
= array_shift( $argv );
605 for ( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
606 if ( $arg == '--' ) {
607 # End of options, remainder should be considered arguments
608 $arg = next( $argv );
609 while ( $arg !== false ) {
611 $arg = next( $argv );
614 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
616 $option = substr( $arg, 2 );
617 if ( array_key_exists( $option, $options ) ) {
618 $this->error( "\nERROR: $option parameter given twice\n" );
619 $this->maybeHelp( true );
621 if ( isset( $this->mParams
[$option] ) && $this->mParams
[$option]['withArg'] ) {
622 $param = next( $argv );
623 if ( $param === false ) {
624 $this->error( "\nERROR: $option parameter needs a value after it\n" );
625 $this->maybeHelp( true );
627 $options[$option] = $param;
629 $bits = explode( '=', $option, 2 );
630 if ( count( $bits ) > 1 ) {
636 $options[$option] = $param;
638 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
640 for ( $p = 1; $p < strlen( $arg ); $p++
) {
641 $option = $arg { $p } ;
642 if ( !isset( $this->mParams
[$option] ) && isset( $this->mShortParamsMap
[$option] ) ) {
643 $option = $this->mShortParamsMap
[$option];
645 if ( array_key_exists( $option, $options ) ) {
646 $this->error( "\nERROR: $option parameter given twice\n" );
647 $this->maybeHelp( true );
649 if ( isset( $this->mParams
[$option]['withArg'] ) && $this->mParams
[$option]['withArg'] ) {
650 $param = next( $argv );
651 if ( $param === false ) {
652 $this->error( "\nERROR: $option parameter needs a value after it\n" );
653 $this->maybeHelp( true );
655 $options[$option] = $param;
657 $options[$option] = 1;
665 $this->mOptions
= $options;
666 $this->mArgs
= $args;
667 $this->loadSpecialVars();
668 $this->mInputLoaded
= true;
672 * Run some validation checks on the params, etc
674 protected function validateParamsAndArgs() {
676 # Check to make sure we've got all the required options
677 foreach ( $this->mParams
as $opt => $info ) {
678 if ( $info['require'] && !$this->hasOption( $opt ) ) {
679 $this->error( "Param $opt required!" );
684 foreach ( $this->mArgList
as $k => $info ) {
685 if ( $info['require'] && !$this->hasArg( $k ) ) {
686 $this->error( 'Argument <' . $info['name'] . '> required!' );
692 $this->maybeHelp( true );
697 * Handle the special variables that are global to all scripts
699 protected function loadSpecialVars() {
700 if ( $this->hasOption( 'dbuser' ) ) {
701 $this->mDbUser
= $this->getOption( 'dbuser' );
703 if ( $this->hasOption( 'dbpass' ) ) {
704 $this->mDbPass
= $this->getOption( 'dbpass' );
706 if ( $this->hasOption( 'quiet' ) ) {
707 $this->mQuiet
= true;
709 if ( $this->hasOption( 'batch-size' ) ) {
710 $this->mBatchSize
= intval( $this->getOption( 'batch-size' ) );
715 * Maybe show the help.
716 * @param $force boolean Whether to force the help to show, default false
718 protected function maybeHelp( $force = false ) {
719 if( !$force && !$this->hasOption( 'help' ) ) {
723 $screenWidth = 80; // TODO: Caculate this!
725 $descWidth = $screenWidth - ( 2 * strlen( $tab ) );
727 ksort( $this->mParams
);
728 $this->mQuiet
= false;
731 if ( $this->mDescription
) {
732 $this->output( "\n" . $this->mDescription
. "\n" );
734 $output = "\nUsage: php " . basename( $this->mSelf
);
736 // ... append parameters ...
737 if ( $this->mParams
) {
738 $output .= " [--" . implode( array_keys( $this->mParams
), "|--" ) . "]";
741 // ... and append arguments.
742 if ( $this->mArgList
) {
744 foreach ( $this->mArgList
as $k => $arg ) {
745 if ( $arg['require'] ) {
746 $output .= '<' . $arg['name'] . '>';
748 $output .= '[' . $arg['name'] . ']';
750 if ( $k < count( $this->mArgList
) - 1 )
754 $this->output( "$output\n\n" );
756 # TODO abstract some repetitive code below
758 // Generic parameters
759 $this->output( "Generic maintenance parameters:\n" );
760 foreach ( $this->mGenericParameters
as $par => $info ) {
761 if ( $info['shortName'] !== false ) {
762 $par .= " (-{$info['shortName']})";
765 wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
766 "\n$tab$tab" ) . "\n"
769 $this->output( "\n" );
771 $scriptDependantParams = $this->mDependantParameters
;
772 if( count($scriptDependantParams) > 0 ) {
773 $this->output( "Script dependant parameters:\n" );
774 // Parameters description
775 foreach ( $scriptDependantParams as $par => $info ) {
776 if ( $info['shortName'] !== false ) {
777 $par .= " (-{$info['shortName']})";
780 wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
781 "\n$tab$tab" ) . "\n"
784 $this->output( "\n" );
788 // Script specific parameters not defined on construction by
789 // Maintenance::addDefaultParams()
790 $scriptSpecificParams = array_diff_key(
791 # all script parameters:
793 # remove the Maintenance default parameters:
794 $this->mGenericParameters
,
795 $this->mDependantParameters
797 if( count($scriptSpecificParams) > 0 ) {
798 $this->output( "Script specific parameters:\n" );
799 // Parameters description
800 foreach ( $scriptSpecificParams as $par => $info ) {
801 if ( $info['shortName'] !== false ) {
802 $par .= " (-{$info['shortName']})";
805 wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
806 "\n$tab$tab" ) . "\n"
809 $this->output( "\n" );
813 if( count( $this->mArgList
) > 0 ) {
814 $this->output( "Arguments:\n" );
815 // Arguments description
816 foreach ( $this->mArgList
as $info ) {
817 $openChar = $info['require'] ?
'<' : '[';
818 $closeChar = $info['require'] ?
'>' : ']';
820 wordwrap( "$tab$openChar" . $info['name'] . "$closeChar: " .
821 $info['desc'], $descWidth, "\n$tab$tab" ) . "\n"
824 $this->output( "\n" );
831 * Handle some last-minute setup here.
833 public function finalSetup() {
834 global $wgCommandLineMode, $wgShowSQLErrors, $wgServer;
835 global $wgDBadminuser, $wgDBadminpassword;
836 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
838 # Turn off output buffering again, it might have been turned on in the settings files
839 if ( ob_get_level() ) {
843 $wgCommandLineMode = true;
846 if( $this->hasOption( 'server') ) {
847 $wgServer = $this->getOption( 'server', $wgServer );
850 # If these were passed, use them
851 if ( $this->mDbUser
) {
852 $wgDBadminuser = $this->mDbUser
;
854 if ( $this->mDbPass
) {
855 $wgDBadminpassword = $this->mDbPass
;
858 if ( $this->getDbType() == self
::DB_ADMIN
&& isset( $wgDBadminuser ) ) {
859 $wgDBuser = $wgDBadminuser;
860 $wgDBpassword = $wgDBadminpassword;
862 if ( $wgDBservers ) {
864 * @var $wgDBservers array
866 foreach ( $wgDBservers as $i => $server ) {
867 $wgDBservers[$i]['user'] = $wgDBuser;
868 $wgDBservers[$i]['password'] = $wgDBpassword;
871 if ( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
872 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
873 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
875 LBFactory
::destroyInstance();
878 $this->afterFinalSetup();
880 $wgShowSQLErrors = true;
881 @set_time_limit
( 0 );
882 $this->adjustMemoryLimit();
886 * Execute a callback function at the end of initialisation
888 protected function afterFinalSetup() {
889 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
890 call_user_func( MW_CMDLINE_CALLBACK
);
895 * Potentially debug globals. Originally a feature only
898 public function globals() {
899 if ( $this->hasOption( 'globals' ) ) {
905 * Generic setup for most installs. Returns the location of LocalSettings
908 public function loadSettings() {
909 global $wgCommandLineMode, $IP;
911 if ( isset( $this->mOptions
['conf'] ) ) {
912 $settingsFile = $this->mOptions
['conf'];
913 } elseif ( defined("MW_CONFIG_FILE") ) {
914 $settingsFile = MW_CONFIG_FILE
;
916 $settingsFile = "$IP/LocalSettings.php";
918 if ( isset( $this->mOptions
['wiki'] ) ) {
919 $bits = explode( '-', $this->mOptions
['wiki'] );
920 if ( count( $bits ) == 1 ) {
923 define( 'MW_DB', $bits[0] );
924 define( 'MW_PREFIX', $bits[1] );
927 if ( !is_readable( $settingsFile ) ) {
928 $this->error( "A copy of your installation's LocalSettings.php\n" .
929 "must exist and be readable in the source directory.\n" .
930 "Use --conf to specify it.", true );
932 $wgCommandLineMode = true;
933 return $settingsFile;
937 * Support function for cleaning up redundant text records
938 * @param $delete Boolean: whether or not to actually delete the records
939 * @author Rob Church <robchur@gmail.com>
941 public function purgeRedundantText( $delete = true ) {
942 # Data should come off the master, wrapped in a transaction
943 $dbw = $this->getDB( DB_MASTER
);
944 $dbw->begin( __METHOD__
);
946 # Get "active" text records from the revisions table
947 $this->output( 'Searching for active text records in revisions table...' );
948 $res = $dbw->select( 'revision', 'rev_text_id', array(), __METHOD__
, array( 'DISTINCT' ) );
949 foreach ( $res as $row ) {
950 $cur[] = $row->rev_text_id
;
952 $this->output( "done.\n" );
954 # Get "active" text records from the archive table
955 $this->output( 'Searching for active text records in archive table...' );
956 $res = $dbw->select( 'archive', 'ar_text_id', array(), __METHOD__
, array( 'DISTINCT' ) );
957 foreach ( $res as $row ) {
958 # old pre-MW 1.5 records can have null ar_text_id's.
959 if ( $row->ar_text_id
!== null ) {
960 $cur[] = $row->ar_text_id
;
963 $this->output( "done.\n" );
965 # Get the IDs of all text records not in these sets
966 $this->output( 'Searching for inactive text records...' );
967 $cond = 'old_id NOT IN ( ' . $dbw->makeList( $cur ) . ' )';
968 $res = $dbw->select( 'text', 'old_id', array( $cond ), __METHOD__
, array( 'DISTINCT' ) );
970 foreach ( $res as $row ) {
971 $old[] = $row->old_id
;
973 $this->output( "done.\n" );
975 # Inform the user of what we're going to do
976 $count = count( $old );
977 $this->output( "$count inactive items found.\n" );
979 # Delete as appropriate
980 if ( $delete && $count ) {
981 $this->output( 'Deleting...' );
982 $dbw->delete( 'text', array( 'old_id' => $old ), __METHOD__
);
983 $this->output( "done.\n" );
987 $dbw->commit( __METHOD__
);
991 * Get the maintenance directory.
994 protected function getDir() {
999 * Get the list of available maintenance scripts. Note
1000 * that if you call this _before_ calling doMaintenance
1001 * you won't have any extensions in it yet
1004 public static function getMaintenanceScripts() {
1005 global $wgMaintenanceScripts;
1006 return $wgMaintenanceScripts + self
::getCoreScripts();
1010 * Return all of the core maintenance scripts
1013 protected static function getCoreScripts() {
1014 if ( !self
::$mCoreScripts ) {
1017 __DIR__
. '/language',
1018 __DIR__
. '/storage',
1020 self
::$mCoreScripts = array();
1021 foreach ( $paths as $p ) {
1022 $handle = opendir( $p );
1023 while ( ( $file = readdir( $handle ) ) !== false ) {
1024 if ( $file == 'Maintenance.php' ) {
1027 $file = $p . '/' . $file;
1028 if ( is_dir( $file ) ||
!strpos( $file, '.php' ) ||
1029 ( strpos( file_get_contents( $file ), '$maintClass' ) === false ) ) {
1033 $vars = get_defined_vars();
1034 if ( array_key_exists( 'maintClass', $vars ) ) {
1035 self
::$mCoreScripts[$vars['maintClass']] = $file;
1038 closedir( $handle );
1041 return self
::$mCoreScripts;
1045 * Returns a database to be used by current maintenance script. It can be set by setDB().
1046 * If not set, wfGetDB() will be used.
1047 * This function has the same parameters as wfGetDB()
1049 * @return DatabaseBase
1051 protected function &getDB( $db, $groups = array(), $wiki = false ) {
1052 if ( is_null( $this->mDb
) ) {
1053 return wfGetDB( $db, $groups, $wiki );
1060 * Sets database object to be returned by getDB().
1062 * @param $db DatabaseBase: Database object to be used
1064 public function setDB( &$db ) {
1069 * Lock the search index
1070 * @param &$db DatabaseBase object
1072 private function lockSearchindex( &$db ) {
1073 $write = array( 'searchindex' );
1074 $read = array( 'page', 'revision', 'text', 'interwiki', 'l10n_cache', 'user' );
1075 $db->lockTables( $read, $write, __CLASS__
. '::' . __METHOD__
);
1080 * @param &$db DatabaseBase object
1082 private function unlockSearchindex( &$db ) {
1083 $db->unlockTables( __CLASS__
. '::' . __METHOD__
);
1087 * Unlock and lock again
1088 * Since the lock is low-priority, queued reads will be able to complete
1089 * @param &$db DatabaseBase object
1091 private function relockSearchindex( &$db ) {
1092 $this->unlockSearchindex( $db );
1093 $this->lockSearchindex( $db );
1097 * Perform a search index update with locking
1098 * @param $maxLockTime Integer: the maximum time to keep the search index locked.
1099 * @param $callback callback String: the function that will update the function.
1100 * @param $dbw DatabaseBase object
1103 public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) {
1107 if ( $maxLockTime ) {
1108 $this->output( " --- Waiting for lock ---" );
1109 $this->lockSearchindex( $dbw );
1111 $this->output( "\n" );
1114 # Loop through the results and do a search update
1115 foreach ( $results as $row ) {
1116 # Allow reads to be processed
1117 if ( $maxLockTime && time() > $lockTime +
$maxLockTime ) {
1118 $this->output( " --- Relocking ---" );
1119 $this->relockSearchindex( $dbw );
1121 $this->output( "\n" );
1123 call_user_func( $callback, $dbw, $row );
1126 # Unlock searchindex
1127 if ( $maxLockTime ) {
1128 $this->output( " --- Unlocking --" );
1129 $this->unlockSearchindex( $dbw );
1130 $this->output( "\n" );
1136 * Update the searchindex table for a given pageid
1137 * @param $dbw DatabaseBase a database write handle
1138 * @param $pageId Integer: the page ID to update.
1139 * @return null|string
1141 public function updateSearchIndexForPage( $dbw, $pageId ) {
1142 // Get current revision
1143 $rev = Revision
::loadFromPageId( $dbw, $pageId );
1146 $titleObj = $rev->getTitle();
1147 $title = $titleObj->getPrefixedDBkey();
1148 $this->output( "$title..." );
1149 # Update searchindex
1150 # TODO: pass the Content object to SearchUpdate, let the search engine decide how to deal with it.
1151 $u = new SearchUpdate( $pageId, $titleObj->getText(), $rev->getContent()->getTextForSearchIndex() );
1153 $this->output( "\n" );
1159 * Wrapper for posix_isatty()
1160 * We default as considering stdin a tty (for nice readline methods)
1161 * but treating stout as not a tty to avoid color codes
1163 * @param $fd int File descriptor
1166 public static function posix_isatty( $fd ) {
1167 if ( !MWInit
::functionExists( 'posix_isatty' ) ) {
1170 return posix_isatty( $fd );
1175 * Prompt the console for input
1176 * @param $prompt String what to begin the line with, like '> '
1177 * @return String response
1179 public static function readconsole( $prompt = '> ' ) {
1180 static $isatty = null;
1181 if ( is_null( $isatty ) ) {
1182 $isatty = self
::posix_isatty( 0 /*STDIN*/ );
1185 if ( $isatty && function_exists( 'readline' ) ) {
1186 return readline( $prompt );
1189 $st = self
::readlineEmulation( $prompt );
1191 if ( feof( STDIN
) ) {
1194 $st = fgets( STDIN
, 1024 );
1197 if ( $st === false ) return false;
1198 $resp = trim( $st );
1204 * Emulate readline()
1205 * @param $prompt String what to begin the line with, like '> '
1208 private static function readlineEmulation( $prompt ) {
1209 $bash = Installer
::locateExecutableInDefaultPaths( array( 'bash' ) );
1210 if ( !wfIsWindows() && $bash ) {
1212 $encPrompt = wfEscapeShellArg( $prompt );
1213 $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
1214 $encCommand = wfEscapeShellArg( $command );
1215 $line = wfShellExec( "$bash -c $encCommand", $retval, array(), array( 'walltime' => 0 ) );
1217 if ( $retval == 0 ) {
1219 } elseif ( $retval == 127 ) {
1220 // Couldn't execute bash even though we thought we saw it.
1221 // Shell probably spit out an error message, sorry :(
1222 // Fall through to fgets()...
1229 // Fallback... we'll have no editing controls, EWWW
1230 if ( feof( STDIN
) ) {
1234 return fgets( STDIN
, 1024 );
1239 * Fake maintenance wrapper, mostly used for the web installer/updater
1241 class FakeMaintenance
extends Maintenance
{
1242 protected $mSelf = "FakeMaintenanceScript";
1243 public function execute() {
1249 * Class for scripts that perform database maintenance and want to log the
1250 * update in `updatelog` so we can later skip it
1252 abstract class LoggedUpdateMaintenance
extends Maintenance
{
1253 public function __construct() {
1254 parent
::__construct();
1255 $this->addOption( 'force', 'Run the update even if it was completed already' );
1256 $this->setBatchSize( 200 );
1259 public function execute() {
1260 $db = $this->getDB( DB_MASTER
);
1261 $key = $this->getUpdateKey();
1263 if ( !$this->hasOption( 'force' ) &&
1264 $db->selectRow( 'updatelog', '1', array( 'ul_key' => $key ), __METHOD__
) )
1266 $this->output( "..." . $this->updateSkippedMessage() . "\n" );
1270 if ( !$this->doDBUpdates() ) {
1275 $db->insert( 'updatelog', array( 'ul_key' => $key ), __METHOD__
, 'IGNORE' ) )
1279 $this->output( $this->updatelogFailedMessage() . "\n" );
1285 * Message to show that the update was done already and was just skipped
1288 protected function updateSkippedMessage() {
1289 $key = $this->getUpdateKey();
1290 return "Update '{$key}' already logged as completed.";
1294 * Message to show the the update log was unable to log the completion of this update
1297 protected function updatelogFailedMessage() {
1298 $key = $this->getUpdateKey();
1299 return "Unable to log update '{$key}' as completed.";
1303 * Do the actual work. All child classes will need to implement this.
1304 * Return true to log the update as done or false (usually on failure).
1307 abstract protected function doDBUpdates();
1310 * Get the update key name to go in the update log table
1313 abstract protected function getUpdateKey();