In Special:Protectedpages and Special:Protectedtitles, show protection expiry times...
[lhc/web/wiklou.git] / maintenance / Maintenance.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Maintenance
20 * @defgroup Maintenance Maintenance
21 */
22
23 // Define this so scripts can easily find doMaintenance.php
24 define( 'RUN_MAINTENANCE_IF_MAIN', dirname( __FILE__ ) . '/doMaintenance.php' );
25 define( 'DO_MAINTENANCE', RUN_MAINTENANCE_IF_MAIN ); // original name, harmless
26
27 $maintClass = false;
28
29 // Make sure we're on PHP5 or better
30 if ( !function_exists( 'version_compare' ) || version_compare( PHP_VERSION, '5.2.3' ) < 0 ) {
31 require_once( dirname( __FILE__ ) . '/../includes/PHPVersionError.php' );
32 wfPHPVersionError( 'cli' );
33 }
34
35 // Wrapper for posix_isatty()
36 if ( !function_exists( 'posix_isatty' ) ) {
37 # We default as considering stdin a tty (for nice readline methods)
38 # but treating stout as not a tty to avoid color codes
39 function posix_isatty( $fd ) {
40 return !$fd;
41 }
42 }
43
44 /**
45 * Abstract maintenance class for quickly writing and churning out
46 * maintenance scripts with minimal effort. All that _must_ be defined
47 * is the execute() method. See docs/maintenance.txt for more info
48 * and a quick demo of how to use it.
49 *
50 * @author Chad Horohoe <chad@anyonecanedit.org>
51 * @since 1.16
52 * @ingroup Maintenance
53 */
54 abstract class Maintenance {
55
56 /**
57 * Constants for DB access type
58 * @see Maintenance::getDbType()
59 */
60 const DB_NONE = 0;
61 const DB_STD = 1;
62 const DB_ADMIN = 2;
63
64 // Const for getStdin()
65 const STDIN_ALL = 'all';
66
67 // This is the desired params
68 protected $mParams = array();
69
70 // Array of mapping short parameters to long ones
71 protected $mShortParamsMap = array();
72
73 // Array of desired args
74 protected $mArgList = array();
75
76 // This is the list of options that were actually passed
77 protected $mOptions = array();
78
79 // This is the list of arguments that were actually passed
80 protected $mArgs = array();
81
82 // Name of the script currently running
83 protected $mSelf;
84
85 // Special vars for params that are always used
86 protected $mQuiet = false;
87 protected $mDbUser, $mDbPass;
88
89 // A description of the script, children should change this
90 protected $mDescription = '';
91
92 // Have we already loaded our user input?
93 protected $mInputLoaded = false;
94
95 /**
96 * Batch size. If a script supports this, they should set
97 * a default with setBatchSize()
98 *
99 * @var int
100 */
101 protected $mBatchSize = null;
102
103 // Generic options added by addDefaultParams()
104 private $mGenericParameters = array();
105 // Generic options which might or not be supported by the script
106 private $mDependantParameters = array();
107
108 // Used by getDD() / setDB()
109 private $mDb = null;
110
111 /**
112 * List of all the core maintenance scripts. This is added
113 * to scripts added by extensions in $wgMaintenanceScripts
114 * and returned by getMaintenanceScripts()
115 */
116 protected static $mCoreScripts = null;
117
118 /**
119 * Default constructor. Children should call this *first* if implementing
120 * their own constructors
121 */
122 public function __construct() {
123 // Setup $IP, using MW_INSTALL_PATH if it exists
124 global $IP;
125 $IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== ''
126 ? getenv( 'MW_INSTALL_PATH' )
127 : realpath( dirname( __FILE__ ) . '/..' );
128
129 $this->addDefaultParams();
130 register_shutdown_function( array( $this, 'outputChanneled' ), false );
131 }
132
133 /**
134 * Should we execute the maintenance script, or just allow it to be included
135 * as a standalone class? It checks that the call stack only includes this
136 * function and "requires" (meaning was called from the file scope)
137 *
138 * @return Boolean
139 */
140 public static function shouldExecute() {
141 $bt = debug_backtrace();
142 $count = count( $bt );
143 if ( $count < 2 ) {
144 return false; // sanity
145 }
146 if ( $bt[0]['class'] !== 'Maintenance' || $bt[0]['function'] !== 'shouldExecute' ) {
147 return false; // last call should be to this function
148 }
149 $includeFuncs = array( 'require_once', 'require', 'include', 'include_once' );
150 for( $i=1; $i < $count; $i++ ) {
151 if ( !in_array( $bt[$i]['function'], $includeFuncs ) ) {
152 return false; // previous calls should all be "requires"
153 }
154 }
155 return true;
156 }
157
158 /**
159 * Do the actual work. All child classes will need to implement this
160 */
161 abstract public function execute();
162
163 /**
164 * Add a parameter to the script. Will be displayed on --help
165 * with the associated description
166 *
167 * @param $name String: the name of the param (help, version, etc)
168 * @param $description String: the description of the param to show on --help
169 * @param $required Boolean: is the param required?
170 * @param $withArg Boolean: is an argument required with this option?
171 * @param $shortName String: character to use as short name
172 */
173 protected function addOption( $name, $description, $required = false, $withArg = false, $shortName = false ) {
174 $this->mParams[$name] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg, 'shortName' => $shortName );
175 if ( $shortName !== false ) {
176 $this->mShortParamsMap[$shortName] = $name;
177 }
178 }
179
180 /**
181 * Checks to see if a particular param exists.
182 * @param $name String: the name of the param
183 * @return Boolean
184 */
185 protected function hasOption( $name ) {
186 return isset( $this->mOptions[$name] );
187 }
188
189 /**
190 * Get an option, or return the default
191 * @param $name String: the name of the param
192 * @param $default Mixed: anything you want, default null
193 * @return Mixed
194 */
195 protected function getOption( $name, $default = null ) {
196 if ( $this->hasOption( $name ) ) {
197 return $this->mOptions[$name];
198 } else {
199 // Set it so we don't have to provide the default again
200 $this->mOptions[$name] = $default;
201 return $this->mOptions[$name];
202 }
203 }
204
205 /**
206 * Add some args that are needed
207 * @param $arg String: name of the arg, like 'start'
208 * @param $description String: short description of the arg
209 * @param $required Boolean: is this required?
210 */
211 protected function addArg( $arg, $description, $required = true ) {
212 $this->mArgList[] = array(
213 'name' => $arg,
214 'desc' => $description,
215 'require' => $required
216 );
217 }
218
219 /**
220 * Remove an option. Useful for removing options that won't be used in your script.
221 * @param $name String: the option to remove.
222 */
223 protected function deleteOption( $name ) {
224 unset( $this->mParams[$name] );
225 }
226
227 /**
228 * Set the description text.
229 * @param $text String: the text of the description
230 */
231 protected function addDescription( $text ) {
232 $this->mDescription = $text;
233 }
234
235 /**
236 * Does a given argument exist?
237 * @param $argId Integer: the integer value (from zero) for the arg
238 * @return Boolean
239 */
240 protected function hasArg( $argId = 0 ) {
241 return isset( $this->mArgs[$argId] );
242 }
243
244 /**
245 * Get an argument.
246 * @param $argId Integer: the integer value (from zero) for the arg
247 * @param $default Mixed: the default if it doesn't exist
248 * @return mixed
249 */
250 protected function getArg( $argId = 0, $default = null ) {
251 return $this->hasArg( $argId ) ? $this->mArgs[$argId] : $default;
252 }
253
254 /**
255 * Set the batch size.
256 * @param $s Integer: the number of operations to do in a batch
257 */
258 protected function setBatchSize( $s = 0 ) {
259 $this->mBatchSize = $s;
260 }
261
262 /**
263 * Get the script's name
264 * @return String
265 */
266 public function getName() {
267 return $this->mSelf;
268 }
269
270 /**
271 * Return input from stdin.
272 * @param $len Integer: the number of bytes to read. If null,
273 * just return the handle. Maintenance::STDIN_ALL returns
274 * the full length
275 * @return Mixed
276 */
277 protected function getStdin( $len = null ) {
278 if ( $len == Maintenance::STDIN_ALL ) {
279 return file_get_contents( 'php://stdin' );
280 }
281 $f = fopen( 'php://stdin', 'rt' );
282 if ( !$len ) {
283 return $f;
284 }
285 $input = fgets( $f, $len );
286 fclose( $f );
287 return rtrim( $input );
288 }
289
290 public function isQuiet() {
291 return $this->mQuiet;
292 }
293
294 /**
295 * Throw some output to the user. Scripts can call this with no fears,
296 * as we handle all --quiet stuff here
297 * @param $out String: the text to show to the user
298 * @param $channel Mixed: unique identifier for the channel. See
299 * function outputChanneled.
300 */
301 protected function output( $out, $channel = null ) {
302 if ( $this->mQuiet ) {
303 return;
304 }
305 if ( $channel === null ) {
306 $this->cleanupChanneled();
307
308 $f = fopen( 'php://stdout', 'w' );
309 fwrite( $f, $out );
310 fclose( $f );
311 }
312 else {
313 $out = preg_replace( '/\n\z/', '', $out );
314 $this->outputChanneled( $out, $channel );
315 }
316 }
317
318 /**
319 * Throw an error to the user. Doesn't respect --quiet, so don't use
320 * this for non-error output
321 * @param $err String: the error to display
322 * @param $die Int: if > 0, go ahead and die out using this int as the code
323 */
324 protected function error( $err, $die = 0 ) {
325 $this->outputChanneled( false );
326 if ( php_sapi_name() == 'cli' ) {
327 fwrite( STDERR, $err . "\n" );
328 } else {
329 $f = fopen( 'php://stderr', 'w' );
330 fwrite( $f, $err . "\n" );
331 fclose( $f );
332 }
333 $die = intval( $die );
334 if ( $die > 0 ) {
335 die( $die );
336 }
337 }
338
339 private $atLineStart = true;
340 private $lastChannel = null;
341
342 /**
343 * Clean up channeled output. Output a newline if necessary.
344 */
345 public function cleanupChanneled() {
346 if ( !$this->atLineStart ) {
347 $handle = fopen( 'php://stdout', 'w' );
348 fwrite( $handle, "\n" );
349 fclose( $handle );
350 $this->atLineStart = true;
351 }
352 }
353
354 /**
355 * Message outputter with channeled message support. Messages on the
356 * same channel are concatenated, but any intervening messages in another
357 * channel start a new line.
358 * @param $msg String: the message without trailing newline
359 * @param $channel Channel identifier or null for no
360 * channel. Channel comparison uses ===.
361 */
362 public function outputChanneled( $msg, $channel = null ) {
363 if ( $msg === false ) {
364 $this->cleanupChanneled();
365 return;
366 }
367
368 $handle = fopen( 'php://stdout', 'w' );
369
370 // End the current line if necessary
371 if ( !$this->atLineStart && $channel !== $this->lastChannel ) {
372 fwrite( $handle, "\n" );
373 }
374
375 fwrite( $handle, $msg );
376
377 $this->atLineStart = false;
378 if ( $channel === null ) {
379 // For unchanneled messages, output trailing newline immediately
380 fwrite( $handle, "\n" );
381 $this->atLineStart = true;
382 }
383 $this->lastChannel = $channel;
384
385 // Cleanup handle
386 fclose( $handle );
387 }
388
389 /**
390 * Does the script need different DB access? By default, we give Maintenance
391 * scripts normal rights to the DB. Sometimes, a script needs admin rights
392 * access for a reason and sometimes they want no access. Subclasses should
393 * override and return one of the following values, as needed:
394 * Maintenance::DB_NONE - For no DB access at all
395 * Maintenance::DB_STD - For normal DB access, default
396 * Maintenance::DB_ADMIN - For admin DB access
397 * @return Integer
398 */
399 public function getDbType() {
400 return Maintenance::DB_STD;
401 }
402
403 /**
404 * Add the default parameters to the scripts
405 */
406 protected function addDefaultParams() {
407
408 # Generic (non script dependant) options:
409
410 $this->addOption( 'help', 'Display this help message', false, false, 'h' );
411 $this->addOption( 'quiet', 'Whether to supress non-error output', false, false, 'q' );
412 $this->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true );
413 $this->addOption( 'wiki', 'For specifying the wiki ID', false, true );
414 $this->addOption( 'globals', 'Output globals at the end of processing for debugging' );
415 $this->addOption( 'memory-limit', 'Set a specific memory limit for the script, "max" for no limit or "default" to avoid changing it' );
416 $this->addOption( 'server', "The protocol and server name to use in URLs, e.g. " .
417 "http://en.wikipedia.org. This is sometimes necessary because " .
418 "server name detection may fail in command line scripts.", false, true );
419
420 # Save generic options to display them separately in help
421 $this->mGenericParameters = $this->mParams ;
422
423 # Script dependant options:
424
425 // If we support a DB, show the options
426 if ( $this->getDbType() > 0 ) {
427 $this->addOption( 'dbuser', 'The DB user to use for this script', false, true );
428 $this->addOption( 'dbpass', 'The password to use for this script', false, true );
429 }
430 // If we support $mBatchSize, show the option
431 if ( $this->mBatchSize ) {
432 $this->addOption( 'batch-size', 'Run this many operations ' .
433 'per batch, default: ' . $this->mBatchSize, false, true );
434 }
435 # Save additional script dependant options to display
436 # them separately in help
437 $this->mDependantParameters = array_diff_key( $this->mParams, $this->mGenericParameters );
438 }
439
440 /**
441 * Run a child maintenance script. Pass all of the current arguments
442 * to it.
443 * @param $maintClass String: a name of a child maintenance class
444 * @param $classFile String: full path of where the child is
445 * @return Maintenance child
446 */
447 public function runChild( $maintClass, $classFile = null ) {
448 // Make sure the class is loaded first
449 if ( !MWInit::classExists( $maintClass ) ) {
450 if ( $classFile ) {
451 require_once( $classFile );
452 }
453 if ( !MWInit::classExists( $maintClass ) ) {
454 $this->error( "Cannot spawn child: $maintClass" );
455 }
456 }
457
458 $child = new $maintClass();
459 $child->loadParamsAndArgs( $this->mSelf, $this->mOptions, $this->mArgs );
460 if ( !is_null( $this->mDb ) ) {
461 $child->setDB( $this->mDb );
462 }
463 return $child;
464 }
465
466 /**
467 * Do some sanity checking and basic setup
468 */
469 public function setup() {
470 global $wgCommandLineMode, $wgRequestTime;
471
472 # Abort if called from a web server
473 if ( isset( $_SERVER ) && isset( $_SERVER['REQUEST_METHOD'] ) ) {
474 $this->error( 'This script must be run from the command line', true );
475 }
476
477 # Make sure we can handle script parameters
478 if ( !function_exists( 'hphp_thread_set_warmup_enabled' ) && !ini_get( 'register_argc_argv' ) ) {
479 $this->error( 'Cannot get command line arguments, register_argc_argv is set to false', true );
480 }
481
482 if ( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
483 // Send PHP warnings and errors to stderr instead of stdout.
484 // This aids in diagnosing problems, while keeping messages
485 // out of redirected output.
486 if ( ini_get( 'display_errors' ) ) {
487 ini_set( 'display_errors', 'stderr' );
488 }
489
490 // Don't touch the setting on earlier versions of PHP,
491 // as setting it would disable output if you'd wanted it.
492
493 // Note that exceptions are also sent to stderr when
494 // command-line mode is on, regardless of PHP version.
495 }
496
497 $this->loadParamsAndArgs();
498 $this->maybeHelp();
499
500 # Set the memory limit
501 # Note we need to set it again later in cache LocalSettings changed it
502 $this->adjustMemoryLimit();
503
504 # Set max execution time to 0 (no limit). PHP.net says that
505 # "When running PHP from the command line the default setting is 0."
506 # But sometimes this doesn't seem to be the case.
507 ini_set( 'max_execution_time', 0 );
508
509 $wgRequestTime = microtime( true );
510
511 # Define us as being in MediaWiki
512 define( 'MEDIAWIKI', true );
513
514 $wgCommandLineMode = true;
515 # Turn off output buffering if it's on
516 @ob_end_flush();
517
518 $this->validateParamsAndArgs();
519 }
520
521 /**
522 * Normally we disable the memory_limit when running admin scripts.
523 * Some scripts may wish to actually set a limit, however, to avoid
524 * blowing up unexpectedly. We also support a --memory-limit option,
525 * to allow sysadmins to explicitly set one if they'd prefer to override
526 * defaults (or for people using Suhosin which yells at you for trying
527 * to disable the limits)
528 */
529 public function memoryLimit() {
530 $limit = $this->getOption( 'memory-limit', 'max' );
531 $limit = trim( $limit, "\" '" ); // trim quotes in case someone misunderstood
532 return $limit;
533 }
534
535 /**
536 * Adjusts PHP's memory limit to better suit our needs, if needed.
537 */
538 protected function adjustMemoryLimit() {
539 $limit = $this->memoryLimit();
540 if ( $limit == 'max' ) {
541 $limit = -1; // no memory limit
542 }
543 if ( $limit != 'default' ) {
544 ini_set( 'memory_limit', $limit );
545 }
546 }
547
548 /**
549 * Clear all params and arguments.
550 */
551 public function clearParamsAndArgs() {
552 $this->mOptions = array();
553 $this->mArgs = array();
554 $this->mInputLoaded = false;
555 }
556
557 /**
558 * Process command line arguments
559 * $mOptions becomes an array with keys set to the option names
560 * $mArgs becomes a zero-based array containing the non-option arguments
561 *
562 * @param $self String The name of the script, if any
563 * @param $opts Array An array of options, in form of key=>value
564 * @param $args Array An array of command line arguments
565 */
566 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) {
567 # If we were given opts or args, set those and return early
568 if ( $self ) {
569 $this->mSelf = $self;
570 $this->mInputLoaded = true;
571 }
572 if ( $opts ) {
573 $this->mOptions = $opts;
574 $this->mInputLoaded = true;
575 }
576 if ( $args ) {
577 $this->mArgs = $args;
578 $this->mInputLoaded = true;
579 }
580
581 # If we've already loaded input (either by user values or from $argv)
582 # skip on loading it again. The array_shift() will corrupt values if
583 # it's run again and again
584 if ( $this->mInputLoaded ) {
585 $this->loadSpecialVars();
586 return;
587 }
588
589 global $argv;
590 $this->mSelf = array_shift( $argv );
591
592 $options = array();
593 $args = array();
594
595 # Parse arguments
596 for ( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
597 if ( $arg == '--' ) {
598 # End of options, remainder should be considered arguments
599 $arg = next( $argv );
600 while ( $arg !== false ) {
601 $args[] = $arg;
602 $arg = next( $argv );
603 }
604 break;
605 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
606 # Long options
607 $option = substr( $arg, 2 );
608 if ( array_key_exists( $option, $options ) ) {
609 $this->error( "\nERROR: $option parameter given twice\n" );
610 $this->maybeHelp( true );
611 }
612 if ( isset( $this->mParams[$option] ) && $this->mParams[$option]['withArg'] ) {
613 $param = next( $argv );
614 if ( $param === false ) {
615 $this->error( "\nERROR: $option parameter needs a value after it\n" );
616 $this->maybeHelp( true );
617 }
618 $options[$option] = $param;
619 } else {
620 $bits = explode( '=', $option, 2 );
621 if ( count( $bits ) > 1 ) {
622 $option = $bits[0];
623 $param = $bits[1];
624 } else {
625 $param = 1;
626 }
627 $options[$option] = $param;
628 }
629 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
630 # Short options
631 for ( $p = 1; $p < strlen( $arg ); $p++ ) {
632 $option = $arg { $p } ;
633 if ( !isset( $this->mParams[$option] ) && isset( $this->mShortParamsMap[$option] ) ) {
634 $option = $this->mShortParamsMap[$option];
635 }
636 if ( array_key_exists( $option, $options ) ) {
637 $this->error( "\nERROR: $option parameter given twice\n" );
638 $this->maybeHelp( true );
639 }
640 if ( isset( $this->mParams[$option]['withArg'] ) && $this->mParams[$option]['withArg'] ) {
641 $param = next( $argv );
642 if ( $param === false ) {
643 $this->error( "\nERROR: $option parameter needs a value after it\n" );
644 $this->maybeHelp( true );
645 }
646 $options[$option] = $param;
647 } else {
648 $options[$option] = 1;
649 }
650 }
651 } else {
652 $args[] = $arg;
653 }
654 }
655
656 $this->mOptions = $options;
657 $this->mArgs = $args;
658 $this->loadSpecialVars();
659 $this->mInputLoaded = true;
660 }
661
662 /**
663 * Run some validation checks on the params, etc
664 */
665 protected function validateParamsAndArgs() {
666 $die = false;
667 # Check to make sure we've got all the required options
668 foreach ( $this->mParams as $opt => $info ) {
669 if ( $info['require'] && !$this->hasOption( $opt ) ) {
670 $this->error( "Param $opt required!" );
671 $die = true;
672 }
673 }
674 # Check arg list too
675 foreach ( $this->mArgList as $k => $info ) {
676 if ( $info['require'] && !$this->hasArg( $k ) ) {
677 $this->error( 'Argument <' . $info['name'] . '> required!' );
678 $die = true;
679 }
680 }
681
682 if ( $die ) {
683 $this->maybeHelp( true );
684 }
685 }
686
687 /**
688 * Handle the special variables that are global to all scripts
689 */
690 protected function loadSpecialVars() {
691 if ( $this->hasOption( 'dbuser' ) ) {
692 $this->mDbUser = $this->getOption( 'dbuser' );
693 }
694 if ( $this->hasOption( 'dbpass' ) ) {
695 $this->mDbPass = $this->getOption( 'dbpass' );
696 }
697 if ( $this->hasOption( 'quiet' ) ) {
698 $this->mQuiet = true;
699 }
700 if ( $this->hasOption( 'batch-size' ) ) {
701 $this->mBatchSize = $this->getOption( 'batch-size' );
702 }
703 }
704
705 /**
706 * Maybe show the help.
707 * @param $force boolean Whether to force the help to show, default false
708 */
709 protected function maybeHelp( $force = false ) {
710 if( !$force && !$this->hasOption( 'help' ) ) {
711 return;
712 }
713
714 $screenWidth = 80; // TODO: Caculate this!
715 $tab = " ";
716 $descWidth = $screenWidth - ( 2 * strlen( $tab ) );
717
718 ksort( $this->mParams );
719 $this->mQuiet = false;
720
721 // Description ...
722 if ( $this->mDescription ) {
723 $this->output( "\n" . $this->mDescription . "\n" );
724 }
725 $output = "\nUsage: php " . basename( $this->mSelf );
726
727 // ... append parameters ...
728 if ( $this->mParams ) {
729 $output .= " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]";
730 }
731
732 // ... and append arguments.
733 if ( $this->mArgList ) {
734 $output .= ' ';
735 foreach ( $this->mArgList as $k => $arg ) {
736 if ( $arg['require'] ) {
737 $output .= '<' . $arg['name'] . '>';
738 } else {
739 $output .= '[' . $arg['name'] . ']';
740 }
741 if ( $k < count( $this->mArgList ) - 1 )
742 $output .= ' ';
743 }
744 }
745 $this->output( "$output\n\n" );
746
747 # TODO abstract some repetitive code below
748
749 // Generic parameters
750 $this->output( "Generic maintenance parameters:\n" );
751 foreach ( $this->mGenericParameters as $par => $info ) {
752 if ( $info['shortName'] !== false ) {
753 $par .= " (-{$info['shortName']})";
754 }
755 $this->output(
756 wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
757 "\n$tab$tab" ) . "\n"
758 );
759 }
760 $this->output( "\n" );
761
762 $scriptDependantParams = $this->mDependantParameters;
763 if( count($scriptDependantParams) > 0 ) {
764 $this->output( "Script dependant parameters:\n" );
765 // Parameters description
766 foreach ( $scriptDependantParams as $par => $info ) {
767 if ( $info['shortName'] !== false ) {
768 $par .= " (-{$info['shortName']})";
769 }
770 $this->output(
771 wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
772 "\n$tab$tab" ) . "\n"
773 );
774 }
775 $this->output( "\n" );
776 }
777
778
779 // Script specific parameters not defined on construction by
780 // Maintenance::addDefaultParams()
781 $scriptSpecificParams = array_diff_key(
782 # all script parameters:
783 $this->mParams,
784 # remove the Maintenance default parameters:
785 $this->mGenericParameters,
786 $this->mDependantParameters
787 );
788 if( count($scriptSpecificParams) > 0 ) {
789 $this->output( "Script specific parameters:\n" );
790 // Parameters description
791 foreach ( $scriptSpecificParams as $par => $info ) {
792 if ( $info['shortName'] !== false ) {
793 $par .= " (-{$info['shortName']})";
794 }
795 $this->output(
796 wordwrap( "$tab--$par: " . $info['desc'], $descWidth,
797 "\n$tab$tab" ) . "\n"
798 );
799 }
800 $this->output( "\n" );
801 }
802
803 // Print arguments
804 if( count( $this->mArgList ) > 0 ) {
805 $this->output( "Arguments:\n" );
806 // Arguments description
807 foreach ( $this->mArgList as $info ) {
808 $openChar = $info['require'] ? '<' : '[';
809 $closeChar = $info['require'] ? '>' : ']';
810 $this->output(
811 wordwrap( "$tab$openChar" . $info['name'] . "$closeChar: " .
812 $info['desc'], $descWidth, "\n$tab$tab" ) . "\n"
813 );
814 }
815 $this->output( "\n" );
816 }
817
818 die( 1 );
819 }
820
821 /**
822 * Handle some last-minute setup here.
823 */
824 public function finalSetup() {
825 global $wgCommandLineMode, $wgShowSQLErrors, $wgServer;
826 global $wgDBadminuser, $wgDBadminpassword;
827 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
828
829 # Turn off output buffering again, it might have been turned on in the settings files
830 if ( ob_get_level() ) {
831 ob_end_flush();
832 }
833 # Same with these
834 $wgCommandLineMode = true;
835
836 # Override $wgServer
837 if( $this->hasOption( 'server') ) {
838 $wgServer = $this->getOption( 'server', $wgServer );
839 }
840
841 # If these were passed, use them
842 if ( $this->mDbUser ) {
843 $wgDBadminuser = $this->mDbUser;
844 }
845 if ( $this->mDbPass ) {
846 $wgDBadminpassword = $this->mDbPass;
847 }
848
849 if ( $this->getDbType() == self::DB_ADMIN && isset( $wgDBadminuser ) ) {
850 $wgDBuser = $wgDBadminuser;
851 $wgDBpassword = $wgDBadminpassword;
852
853 if ( $wgDBservers ) {
854 foreach ( $wgDBservers as $i => $server ) {
855 $wgDBservers[$i]['user'] = $wgDBuser;
856 $wgDBservers[$i]['password'] = $wgDBpassword;
857 }
858 }
859 if ( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
860 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
861 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
862 }
863 LBFactory::destroyInstance();
864 }
865
866 $this->afterFinalSetup();
867
868 $wgShowSQLErrors = true;
869 @set_time_limit( 0 );
870 $this->adjustMemoryLimit();
871 }
872
873 /**
874 * Execute a callback function at the end of initialisation
875 */
876 protected function afterFinalSetup() {
877 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
878 call_user_func( MW_CMDLINE_CALLBACK );
879 }
880 }
881
882 /**
883 * Potentially debug globals. Originally a feature only
884 * for refreshLinks
885 */
886 public function globals() {
887 if ( $this->hasOption( 'globals' ) ) {
888 print_r( $GLOBALS );
889 }
890 }
891
892 /**
893 * Do setup specific to WMF
894 */
895 public function loadWikimediaSettings() {
896 global $IP, $wgNoDBParam, $wgUseNormalUser, $wgConf, $site, $lang;
897
898 if ( empty( $wgNoDBParam ) ) {
899 # Check if we were passed a db name
900 if ( isset( $this->mOptions['wiki'] ) ) {
901 $db = $this->mOptions['wiki'];
902 } else {
903 $db = array_shift( $this->mArgs );
904 }
905 list( $site, $lang ) = $wgConf->siteFromDB( $db );
906
907 # If not, work out the language and site the old way
908 if ( is_null( $site ) || is_null( $lang ) ) {
909 if ( !$db ) {
910 $lang = 'aa';
911 } else {
912 $lang = $db;
913 }
914 if ( isset( $this->mArgs[0] ) ) {
915 $site = array_shift( $this->mArgs );
916 } else {
917 $site = 'wikipedia';
918 }
919 }
920 } else {
921 $lang = 'aa';
922 $site = 'wikipedia';
923 }
924
925 # This is for the IRC scripts, which now run as the apache user
926 # The apache user doesn't have access to the wikiadmin_pass command
927 if ( $_ENV['USER'] == 'apache' ) {
928 # if ( posix_geteuid() == 48 ) {
929 $wgUseNormalUser = true;
930 }
931
932 putenv( 'wikilang=' . $lang );
933
934 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
935
936 if ( $lang == 'test' && $site == 'wikipedia' ) {
937 if ( !defined( 'TESTWIKI' ) ) {
938 define( 'TESTWIKI', 1 );
939 }
940 }
941 }
942
943 /**
944 * Generic setup for most installs. Returns the location of LocalSettings
945 * @return String
946 */
947 public function loadSettings() {
948 global $wgCommandLineMode, $IP;
949
950 if ( isset( $this->mOptions['conf'] ) ) {
951 $settingsFile = $this->mOptions['conf'];
952 } elseif ( defined("MW_CONFIG_FILE") ) {
953 $settingsFile = MW_CONFIG_FILE;
954 } else {
955 $settingsFile = "$IP/LocalSettings.php";
956 }
957 if ( isset( $this->mOptions['wiki'] ) ) {
958 $bits = explode( '-', $this->mOptions['wiki'] );
959 if ( count( $bits ) == 1 ) {
960 $bits[] = '';
961 }
962 define( 'MW_DB', $bits[0] );
963 define( 'MW_PREFIX', $bits[1] );
964 }
965
966 if ( !is_readable( $settingsFile ) ) {
967 $this->error( "A copy of your installation's LocalSettings.php\n" .
968 "must exist and be readable in the source directory.\n" .
969 "Use --conf to specify it." , true );
970 }
971 $wgCommandLineMode = true;
972 return $settingsFile;
973 }
974
975 /**
976 * Support function for cleaning up redundant text records
977 * @param $delete Boolean: whether or not to actually delete the records
978 * @author Rob Church <robchur@gmail.com>
979 */
980 public function purgeRedundantText( $delete = true ) {
981 # Data should come off the master, wrapped in a transaction
982 $dbw = $this->getDB( DB_MASTER );
983 $dbw->begin();
984
985 $tbl_arc = $dbw->tableName( 'archive' );
986 $tbl_rev = $dbw->tableName( 'revision' );
987 $tbl_txt = $dbw->tableName( 'text' );
988
989 # Get "active" text records from the revisions table
990 $this->output( 'Searching for active text records in revisions table...' );
991 $res = $dbw->query( "SELECT DISTINCT rev_text_id FROM $tbl_rev" );
992 foreach ( $res as $row ) {
993 $cur[] = $row->rev_text_id;
994 }
995 $this->output( "done.\n" );
996
997 # Get "active" text records from the archive table
998 $this->output( 'Searching for active text records in archive table...' );
999 $res = $dbw->query( "SELECT DISTINCT ar_text_id FROM $tbl_arc" );
1000 foreach ( $res as $row ) {
1001 $cur[] = $row->ar_text_id;
1002 }
1003 $this->output( "done.\n" );
1004
1005 # Get the IDs of all text records not in these sets
1006 $this->output( 'Searching for inactive text records...' );
1007 $set = implode( ', ', $cur );
1008 $res = $dbw->query( "SELECT old_id FROM $tbl_txt WHERE old_id NOT IN ( $set )" );
1009 $old = array();
1010 foreach ( $res as $row ) {
1011 $old[] = $row->old_id;
1012 }
1013 $this->output( "done.\n" );
1014
1015 # Inform the user of what we're going to do
1016 $count = count( $old );
1017 $this->output( "$count inactive items found.\n" );
1018
1019 # Delete as appropriate
1020 if ( $delete && $count ) {
1021 $this->output( 'Deleting...' );
1022 $set = implode( ', ', $old );
1023 $dbw->query( "DELETE FROM $tbl_txt WHERE old_id IN ( $set )" );
1024 $this->output( "done.\n" );
1025 }
1026
1027 # Done
1028 $dbw->commit();
1029 }
1030
1031 /**
1032 * Get the maintenance directory.
1033 */
1034 protected function getDir() {
1035 return dirname( __FILE__ );
1036 }
1037
1038 /**
1039 * Get the list of available maintenance scripts. Note
1040 * that if you call this _before_ calling doMaintenance
1041 * you won't have any extensions in it yet
1042 * @return Array
1043 */
1044 public static function getMaintenanceScripts() {
1045 global $wgMaintenanceScripts;
1046 return $wgMaintenanceScripts + self::getCoreScripts();
1047 }
1048
1049 /**
1050 * Return all of the core maintenance scripts
1051 * @return array
1052 */
1053 protected static function getCoreScripts() {
1054 if ( !self::$mCoreScripts ) {
1055 $paths = array(
1056 dirname( __FILE__ ),
1057 dirname( __FILE__ ) . '/gearman',
1058 dirname( __FILE__ ) . '/language',
1059 dirname( __FILE__ ) . '/storage',
1060 );
1061 self::$mCoreScripts = array();
1062 foreach ( $paths as $p ) {
1063 $handle = opendir( $p );
1064 while ( ( $file = readdir( $handle ) ) !== false ) {
1065 if ( $file == 'Maintenance.php' ) {
1066 continue;
1067 }
1068 $file = $p . '/' . $file;
1069 if ( is_dir( $file ) || !strpos( $file, '.php' ) ||
1070 ( strpos( file_get_contents( $file ), '$maintClass' ) === false ) ) {
1071 continue;
1072 }
1073 require( $file );
1074 $vars = get_defined_vars();
1075 if ( array_key_exists( 'maintClass', $vars ) ) {
1076 self::$mCoreScripts[$vars['maintClass']] = $file;
1077 }
1078 }
1079 closedir( $handle );
1080 }
1081 }
1082 return self::$mCoreScripts;
1083 }
1084
1085 /**
1086 * Returns a database to be used by current maintenance script. It can be set by setDB().
1087 * If not set, wfGetDB() will be used.
1088 * This function has the same parameters as wfGetDB()
1089 *
1090 * @return DatabaseBase
1091 */
1092 protected function &getDB( $db, $groups = array(), $wiki = false ) {
1093 if ( is_null( $this->mDb ) ) {
1094 return wfGetDB( $db, $groups, $wiki );
1095 } else {
1096 return $this->mDb;
1097 }
1098 }
1099
1100 /**
1101 * Sets database object to be returned by getDB().
1102 *
1103 * @param $db DatabaseBase: Database object to be used
1104 */
1105 public function setDB( &$db ) {
1106 $this->mDb = $db;
1107 }
1108
1109 /**
1110 * Lock the search index
1111 * @param &$db Database object
1112 */
1113 private function lockSearchindex( &$db ) {
1114 $write = array( 'searchindex' );
1115 $read = array( 'page', 'revision', 'text', 'interwiki', 'l10n_cache' );
1116 $db->lockTables( $read, $write, __CLASS__ . '::' . __METHOD__ );
1117 }
1118
1119 /**
1120 * Unlock the tables
1121 * @param &$db Database object
1122 */
1123 private function unlockSearchindex( &$db ) {
1124 $db->unlockTables( __CLASS__ . '::' . __METHOD__ );
1125 }
1126
1127 /**
1128 * Unlock and lock again
1129 * Since the lock is low-priority, queued reads will be able to complete
1130 * @param &$db Database object
1131 */
1132 private function relockSearchindex( &$db ) {
1133 $this->unlockSearchindex( $db );
1134 $this->lockSearchindex( $db );
1135 }
1136
1137 /**
1138 * Perform a search index update with locking
1139 * @param $maxLockTime Integer: the maximum time to keep the search index locked.
1140 * @param $callback callback String: the function that will update the function.
1141 * @param $dbw DatabaseBase object
1142 * @param $results
1143 */
1144 public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) {
1145 $lockTime = time();
1146
1147 # Lock searchindex
1148 if ( $maxLockTime ) {
1149 $this->output( " --- Waiting for lock ---" );
1150 $this->lockSearchindex( $dbw );
1151 $lockTime = time();
1152 $this->output( "\n" );
1153 }
1154
1155 # Loop through the results and do a search update
1156 foreach ( $results as $row ) {
1157 # Allow reads to be processed
1158 if ( $maxLockTime && time() > $lockTime + $maxLockTime ) {
1159 $this->output( " --- Relocking ---" );
1160 $this->relockSearchindex( $dbw );
1161 $lockTime = time();
1162 $this->output( "\n" );
1163 }
1164 call_user_func( $callback, $dbw, $row );
1165 }
1166
1167 # Unlock searchindex
1168 if ( $maxLockTime ) {
1169 $this->output( " --- Unlocking --" );
1170 $this->unlockSearchindex( $dbw );
1171 $this->output( "\n" );
1172 }
1173
1174 }
1175
1176 /**
1177 * Update the searchindex table for a given pageid
1178 * @param $dbw Database: a database write handle
1179 * @param $pageId Integer: the page ID to update.
1180 */
1181 public function updateSearchIndexForPage( $dbw, $pageId ) {
1182 // Get current revision
1183 $rev = Revision::loadFromPageId( $dbw, $pageId );
1184 $title = null;
1185 if ( $rev ) {
1186 $titleObj = $rev->getTitle();
1187 $title = $titleObj->getPrefixedDBkey();
1188 $this->output( "$title..." );
1189 # Update searchindex
1190 $u = new SearchUpdate( $pageId, $titleObj->getText(), $rev->getText() );
1191 $u->doUpdate();
1192 $this->output( "\n" );
1193 }
1194 return $title;
1195 }
1196
1197 /**
1198 * Prompt the console for input
1199 * @param $prompt String what to begin the line with, like '> '
1200 * @return String response
1201 */
1202 public static function readconsole( $prompt = '> ' ) {
1203 static $isatty = null;
1204 if ( is_null( $isatty ) ) {
1205 $isatty = posix_isatty( 0 /*STDIN*/ );
1206 }
1207
1208 if ( $isatty && function_exists( 'readline' ) ) {
1209 return readline( $prompt );
1210 } else {
1211 if ( $isatty ) {
1212 $st = self::readlineEmulation( $prompt );
1213 } else {
1214 if ( feof( STDIN ) ) {
1215 $st = false;
1216 } else {
1217 $st = fgets( STDIN, 1024 );
1218 }
1219 }
1220 if ( $st === false ) return false;
1221 $resp = trim( $st );
1222 return $resp;
1223 }
1224 }
1225
1226 /**
1227 * Emulate readline()
1228 * @param $prompt String what to begin the line with, like '> '
1229 * @return String
1230 */
1231 private static function readlineEmulation( $prompt ) {
1232 $bash = Installer::locateExecutableInDefaultPaths( array( 'bash' ) );
1233 if ( !wfIsWindows() && $bash ) {
1234 $retval = false;
1235 $encPrompt = wfEscapeShellArg( $prompt );
1236 $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
1237 $encCommand = wfEscapeShellArg( $command );
1238 $line = wfShellExec( "$bash -c $encCommand", $retval );
1239
1240 if ( $retval == 0 ) {
1241 return $line;
1242 } elseif ( $retval == 127 ) {
1243 // Couldn't execute bash even though we thought we saw it.
1244 // Shell probably spit out an error message, sorry :(
1245 // Fall through to fgets()...
1246 } else {
1247 // EOF/ctrl+D
1248 return false;
1249 }
1250 }
1251
1252 // Fallback... we'll have no editing controls, EWWW
1253 if ( feof( STDIN ) ) {
1254 return false;
1255 }
1256 print $prompt;
1257 return fgets( STDIN, 1024 );
1258 }
1259 }
1260
1261 class FakeMaintenance extends Maintenance {
1262 protected $mSelf = "FakeMaintenanceScript";
1263 public function execute() {
1264 return;
1265 }
1266 }