2 // Define this so scripts can easily find doMaintenance.php
3 define( 'DO_MAINTENANCE', dirname(__FILE__
) . '/doMaintenance.php' );
6 * Abstract maintenance class for quickly writing and churning out
7 * maintenance scripts with minimal effort. All that _must_ be defined
8 * is the execute() method. See docs/maintenance.txt for more info
9 * and a quick demo of how to use it.
11 * @author Chad Horohoe <chad@anyonecanedit.org>
13 * @ingroup Maintenance
15 abstract class Maintenance
{
18 * Constants for DB access type
19 * @see Maintenance::getDbType()
25 // This is the desired params
26 private $mParams = array();
28 // Array of desired args
29 private $mArgList = array();
31 // This is the list of options that were actually passed
32 private $mOptions = array();
34 // This is the list of arguments that were actually passed
35 protected $mArgs = array();
37 // Name of the script currently running
40 // Special vars for params that are always used
41 private $mQuiet = false;
42 private $mDbUser, $mDbPass;
44 // A description of the script, children should change this
45 protected $mDescription = '';
47 // Have we already loaded our user input?
48 private $inputLoaded = false;
51 protected $mBatchSize = 100;
54 * Default constructor. Children should call this if implementing
55 * their own constructors
57 public function __construct() {
58 $this->addDefaultParams();
62 * Do the actual work. All child classes will need to implement this
64 abstract public function execute();
67 * Add a parameter to the script. Will be displayed on --help
68 * with the associated description
70 * @param $name String The name of the param (help, version, etc)
71 * @param $description String The description of the param to show on --help
72 * @param $required boolean Is the param required?
73 * @param $withArg Boolean Is an argument required with this option?
75 protected function addParam( $name, $description, $required = false, $withArg = false ) {
76 $this->mParams
[ $name ] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg );
80 * Checks to see if a particular param exists.
81 * @param $name String The name of the param
84 protected function hasOption( $name ) {
85 return isset( $this->mOptions
[ $name ] );
89 * Get an option, or return the default
90 * @param $name String The name of the param
91 * @param $default mixed Anything you want, default null
94 protected function getOption( $name, $default = null ) {
95 if( $this->hasOption($name) ) {
96 return $this->mOptions
[$name];
98 // Set it so we don't have to provide the default again
99 $this->mOptions
[$name] = $default;
100 return $this->mOptions
[$name];
105 * Add some args that are needed. Used in formatting help
107 protected function addArgs( $args ) {
108 $this->mArgList
= array_merge( $this->mArgList
, $args );
112 * Does a given argument exist?
113 * @param $argId int The integer value (from zero) for the arg
116 protected function hasArg( $argId = 0 ) {
117 return isset( $this->mArgs
[ $argId ] ) ;
122 * @param $argId int The integer value (from zero) for the arg
123 * @param $default mixed The default if it doesn't exist
126 protected function getArg( $argId = 0, $default = null ) {
127 return $this->hasArg($name) ?
$this->mArgs
[$name] : $default;
131 * Set the batch size.
132 * @param $s int The number of operations to do in a batch
134 protected function setBatchSize( $s = 0 ) {
135 $this->mBatchSize
= $s;
139 * Return input from stdin.
140 * @param $length int The number of bytes to read. If null,
141 * just return the handle
144 protected function getStdin( $len = null ) {
145 $f = fopen( 'php://stdin', 'rt' );
149 $input = fgets( $f, $len );
151 return rtrim( $input );
155 * Throw some output to the user. Scripts can call this with no fears,
156 * as we handle all --quiet stuff here
157 * @param $out String The text to show to the user
159 protected function output( $out ) {
160 if( $this->mQuiet
) {
163 $f = fopen( 'php://stdout', 'w' );
169 * Throw an error to the user. Doesn't respect --quiet, so don't use
170 * this for non-error output
171 * @param $err String The error to display
172 * @param $die boolean If true, go ahead and die out.
174 protected function error( $err, $die = false ) {
175 $f = fopen( 'php://stderr', 'w' );
182 * Does the script need normal DB access? By default, we give Maintenance
183 * scripts admin rights to the DB (when available). Sometimes, a script needs
184 * normal access for a reason and sometimes they want no access. Subclasses
185 * should override and return one of the following values, as needed:
186 * Maintenance::NO_DB - For no DB access at all
187 * Maintenance::NORMAL_DB - For normal DB access
188 * Maintenance::ADMIN_DB - For admin DB access, default
191 protected function getDbType() {
192 return Maintenance
:: ADMIN_DB
;
196 * Add the default parameters to the scripts
198 private function addDefaultParams() {
199 $this->addParam( 'help', "Display this help message" );
200 $this->addParam( 'quiet', "Whether to supress non-error output" );
201 $this->addParam( 'conf', "Location of LocalSettings.php, if not default", false, true );
202 $this->addParam( 'wiki', "For specifying the wiki ID", false, true );
203 if( $this->getDbType() > 0 ) {
204 $this->addParam( 'dbuser', "The DB user to use for this script", false, true );
205 $this->addParam( 'dbpass', "The password to use for this script", false, true );
210 * Spawn a child maintenance script. Pass all of the current arguments
212 * @param $maintClass String A name of a child maintenance class
213 * @param $classFile String Full path of where the child is
214 * @return Maintenance child
216 protected function spawnChild( $maintClass, $classFile = null ) {
217 // If we haven't already specified, kill setup procedures
218 // for child scripts, we've already got a sane environment
219 if( !defined( 'MW_NO_SETUP' ) ) {
220 define( 'MW_NO_SETUP', true );
223 // Make sure the class is loaded first
224 if( !class_exists( $maintClass ) ) {
226 require_once( $classFile );
228 if( !class_exists( $maintClass ) ) {
229 $this->error( "Cannot spawn child: $maintClass\n" );
233 $child = new $maintClass();
234 $child->loadParamsAndArgs( $this->mSelf
, $this->mOptions
, $this->mArgs
);
239 * Do some sanity checking and basic setup
241 public function setup() {
242 global $IP, $wgCommandLineMode, $wgUseNormalUser, $wgRequestTime;
244 # Abort if called from a web server
245 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
246 $this->error( "This script must be run from the command line\n", true );
249 # Make sure we can handle script parameters
250 if( !ini_get( 'register_argc_argv' ) ) {
251 $this->error( "Cannot get command line arguments, register_argc_argv is set to false", true );
254 # Make sure we're on PHP5 or better
255 if( version_compare( PHP_VERSION
, '5.0.0' ) < 0 ) {
256 $this->error( "Sorry! This version of MediaWiki requires PHP 5; you are running " .
257 PHP_VERSION
. ".\n\n" .
258 "If you are sure you already have PHP 5 installed, it may be installed\n" .
259 "in a different path from PHP 4. Check with your system administrator.\n", true );
262 if( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
263 // Send PHP warnings and errors to stderr instead of stdout.
264 // This aids in diagnosing problems, while keeping messages
265 // out of redirected output.
266 if( ini_get( 'display_errors' ) ) {
267 ini_set( 'display_errors', 'stderr' );
270 // Don't touch the setting on earlier versions of PHP,
271 // as setting it would disable output if you'd wanted it.
273 // Note that exceptions are also sent to stderr when
274 // command-line mode is on, regardless of PHP version.
277 # Set the memory limit
278 ini_set( 'memory_limit', -1 );
280 $wgRequestTime = microtime(true);
282 # Define us as being in Mediawiki
283 define( 'MEDIAWIKI', true );
285 # Setup $IP, using MW_INSTALL_PATH if it exists
286 $IP = strval( getenv('MW_INSTALL_PATH') ) !== ''
287 ?
getenv('MW_INSTALL_PATH')
288 : realpath( dirname( __FILE__
) . '/..' );
290 $wgCommandLineMode = true;
291 # Turn off output buffering if it's on
294 if (!isset( $wgUseNormalUser ) ) {
295 $wgUseNormalUser = false;
298 $this->loadParamsAndArgs();
303 * Clear all params and arguments.
305 public function clearParamsAndArgs() {
306 $this->mOptions
= array();
307 $this->mArgs
= array();
308 $this->inputLoaded
= false;
312 * Process command line arguments
313 * $mOptions becomes an array with keys set to the option names
314 * $mArgs becomes a zero-based array containing the non-option arguments
316 * @param $self String The name of the script, if any
317 * @param $opts Array An array of options, in form of key=>value
318 * @param $args Array An array of command line arguments
320 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) {
321 # If we were given opts or args, set those and return early
323 $this->mSelf
= $self;
324 $this->inputLoaded
= true;
327 $this->mOptions
= $opts;
328 $this->inputLoaded
= true;
331 $this->mArgs
= $args;
332 $this->inputLoaded
= true;
335 # If we've already loaded input (either by user values or from $argv)
336 # skip on loading it again. The array_shift() will corrupt values if
337 # it's run again and again
338 if( $this->inputLoaded
) {
339 $this->loadSpecialVars();
344 $this->mSelf
= array_shift( $argv );
350 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
351 if ( $arg == '--' ) {
352 # End of options, remainder should be considered arguments
353 $arg = next( $argv );
354 while( $arg !== false ) {
356 $arg = next( $argv );
359 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
361 $option = substr( $arg, 2 );
362 if ( isset( $this->mParams
[$option] ) && $this->mParams
[$option]['withArg'] ) {
363 $param = next( $argv );
364 if ( $param === false ) {
365 $this->error( "$arg needs a value after it\n", true );
367 $options[$option] = $param;
369 $bits = explode( '=', $option, 2 );
370 if( count( $bits ) > 1 ) {
376 $options[$option] = $param;
378 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
380 for ( $p=1; $p<strlen( $arg ); $p++
) {
382 if ( isset( $this->mParams
[$option]['withArg'] ) ) {
383 $param = next( $argv );
384 if ( $param === false ) {
385 $this->error( "$arg needs a value after it\n", true );
387 $options[$option] = $param;
389 $options[$option] = 1;
397 # Check to make sure we've got all the required ones
398 foreach( $this->mParams
as $opt => $info ) {
399 if( $info['require'] && !$this->hasOption($opt) ) {
400 $this->error( "Param $opt required.\n", true );
404 # Also make sure we've got enough arguments
405 if ( count( $args ) < count( $this->mArgList
) ) {
406 $this->error( "Not enough arguments passed", true );
409 $this->mOptions
= $options;
410 $this->mArgs
= $args;
411 $this->loadSpecialVars();
412 $this->inputLoaded
= true;
416 * Handle the special variables that are global to all scripts
418 private function loadSpecialVars() {
419 if( $this->hasOption( 'dbuser' ) )
420 $this->mDbUser
= $this->getOption( 'dbuser' );
421 if( $this->hasOption( 'dbpass' ) )
422 $this->mDbPass
= $this->getOption( 'dbpass' );
423 if( $this->hasOption( 'quiet' ) )
424 $this->mQuiet
= true;
428 * Maybe show the help.
429 * @param $force boolean Whether to force the help to show, default false
431 private function maybeHelp( $force = false ) {
432 if( $this->hasOption('help') ||
in_array( 'help', $this->mArgs
) ||
$force ) {
433 $this->mQuiet
= false;
434 if( $this->mDescription
) {
435 $this->output( $this->mDescription
. "\n" );
437 $this->output( "\nUsage: php " . $this->mSelf
);
438 if( $this->mParams
) {
439 $this->output( " [--" . implode( array_keys( $this->mParams
), "|--" ) . "]" );
441 if( $this->mArgList
) {
442 $this->output( " <" . implode( $this->mArgList
, "> <" ) . ">" );
444 $this->output( "\n" );
445 foreach( $this->mParams
as $par => $info ) {
446 $this->output( "\t$par : " . $info['desc'] . "\n" );
453 * Handle some last-minute setup here.
455 private function finalSetup() {
456 global $wgCommandLineMode, $wgUseNormalUser, $wgShowSQLErrors;
457 global $wgTitle, $wgProfiling, $IP, $wgDBadminuser, $wgDBadminpassword;
458 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
460 # Turn off output buffering again, it might have been turned on in the settings files
461 if( ob_get_level() ) {
465 $wgCommandLineMode = true;
467 # If these were passed, use them
469 $wgDBadminuser = $this->mDbUser
;
471 $wgDBadminpass = $this->mDbPass
;
473 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
474 $wgDBuser = $wgDBadminuser;
475 $wgDBpassword = $wgDBadminpassword;
478 foreach ( $wgDBservers as $i => $server ) {
479 $wgDBservers[$i]['user'] = $wgDBuser;
480 $wgDBservers[$i]['password'] = $wgDBpassword;
483 if( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
484 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
485 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
489 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
490 $fn = MW_CMDLINE_CALLBACK
;
494 $wgShowSQLErrors = true;
495 @set_time_limit
( 0 );
497 $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
501 * Do setup specific to WMF
503 public function loadWikimediaSettings() {
504 global $IP, $wgNoDBParam, $wgUseNormalUser, $wgConf;
506 if ( empty( $wgNoDBParam ) ) {
507 # Check if we were passed a db name
508 if ( isset( $this->mOptions
['wiki'] ) ) {
509 $db = $this->mOptions
['wiki'];
511 $db = array_shift( $this->mArgs
);
513 list( $site, $lang ) = $wgConf->siteFromDB( $db );
515 # If not, work out the language and site the old way
516 if ( is_null( $site ) ||
is_null( $lang ) ) {
522 if ( isset( $this->mArgs
[0] ) ) {
523 $site = array_shift( $this->mArgs
);
533 # This is for the IRC scripts, which now run as the apache user
534 # The apache user doesn't have access to the wikiadmin_pass command
535 if ( $_ENV['USER'] == 'apache' ) {
536 #if ( posix_geteuid() == 48 ) {
537 $wgUseNormalUser = true;
540 putenv( 'wikilang=' . $lang );
543 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
545 if ( $lang == 'test' && $site == 'wikipedia' ) {
546 define( 'TESTWIKI', 1 );
551 * Generic setup for most installs. Returns the location of LocalSettings
554 public function loadSettings() {
555 global $wgWikiFarm, $wgCommandLineMode, $IP, $DP;
558 if ( isset( $this->mOptions
['conf'] ) ) {
559 $settingsFile = $this->mOptions
['conf'];
561 $settingsFile = "$IP/LocalSettings.php";
563 if ( isset( $this->mOptions
['wiki'] ) ) {
564 $bits = explode( '-', $this->mOptions
['wiki'] );
565 if ( count( $bits ) == 1 ) {
568 define( 'MW_DB', $bits[0] );
569 define( 'MW_PREFIX', $bits[1] );
572 if ( ! is_readable( $settingsFile ) ) {
573 $this->error( "A copy of your installation's LocalSettings.php\n" .
574 "must exist and be readable in the source directory.\n", true );
576 $wgCommandLineMode = true;
579 return $settingsFile;
583 * Support function for cleaning up redundant text records
584 * @param $delete boolean Whether or not to actually delete the records
585 * @author Rob Church <robchur@gmail.com>
587 protected function purgeRedundantText( $delete = true ) {
588 # Data should come off the master, wrapped in a transaction
589 $dbw = wfGetDB( DB_MASTER
);
592 $tbl_arc = $dbw->tableName( 'archive' );
593 $tbl_rev = $dbw->tableName( 'revision' );
594 $tbl_txt = $dbw->tableName( 'text' );
596 # Get "active" text records from the revisions table
597 $this->output( "Searching for active text records in revisions table..." );
598 $res = $dbw->query( "SELECT DISTINCT rev_text_id FROM $tbl_rev" );
599 while( $row = $dbw->fetchObject( $res ) ) {
600 $cur[] = $row->rev_text_id
;
602 $this->output( "done.\n" );
604 # Get "active" text records from the archive table
605 $this->output( "Searching for active text records in archive table..." );
606 $res = $dbw->query( "SELECT DISTINCT ar_text_id FROM $tbl_arc" );
607 while( $row = $dbw->fetchObject( $res ) ) {
608 $cur[] = $row->ar_text_id
;
610 $this->output( "done.\n" );
612 # Get the IDs of all text records not in these sets
613 $this->output( "Searching for inactive text records..." );
614 $set = implode( ', ', $cur );
615 $res = $dbw->query( "SELECT old_id FROM $tbl_txt WHERE old_id NOT IN ( $set )" );
617 while( $row = $dbw->fetchObject( $res ) ) {
618 $old[] = $row->old_id
;
620 $this->output( "done.\n" );
622 # Inform the user of what we're going to do
623 $count = count( $old );
624 $this->output( "$count inactive items found.\n" );
626 # Delete as appropriate
627 if( $delete && $count ) {
628 $this->output( "Deleting..." );
629 $set = implode( ', ', $old );
630 $dbw->query( "DELETE FROM $tbl_txt WHERE old_id IN ( $set )" );
631 $this->output( "done.\n" );