452acf47725ac0722ba290fe47878f2ff836c6a0
[lhc/web/wiklou.git] / maintenance / Maintenance.php
1 <?php
2 // Define this so scripts can easily find doMaintenance.php
3 define( 'DO_MAINTENANCE', dirname( __FILE__ ) . '/doMaintenance.php' );
4 $maintClass = false;
5
6 // Make sure we're on PHP5 or better
7 if( version_compare( PHP_VERSION, '5.0.0' ) < 0 ) {
8 echo( "Sorry! This version of MediaWiki requires PHP 5; you are running " .
9 PHP_VERSION . ".\n\n" .
10 "If you are sure you already have PHP 5 installed, it may be installed\n" .
11 "in a different path from PHP 4. Check with your system administrator.\n" );
12 die();
13 }
14
15 /**
16 * Abstract maintenance class for quickly writing and churning out
17 * maintenance scripts with minimal effort. All that _must_ be defined
18 * is the execute() method. See docs/maintenance.txt for more info
19 * and a quick demo of how to use it.
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 * http://www.gnu.org/copyleft/gpl.html
35 *
36 * @author Chad Horohoe <chad@anyonecanedit.org>
37 * @since 1.16
38 * @ingroup Maintenance
39 */
40 abstract class Maintenance {
41
42 /**
43 * Constants for DB access type
44 * @see Maintenance::getDbType()
45 */
46 const DB_NONE = 0;
47 const DB_STD = 1;
48 const DB_ADMIN = 2;
49
50 // Const for getStdin()
51 const STDIN_ALL = 'all';
52
53 // This is the desired params
54 protected $mParams = array();
55
56 // Array of desired args
57 protected $mArgList = array();
58
59 // This is the list of options that were actually passed
60 protected $mOptions = array();
61
62 // This is the list of arguments that were actually passed
63 protected $mArgs = array();
64
65 // Name of the script currently running
66 protected $mSelf;
67
68 // Special vars for params that are always used
69 protected $mQuiet = false;
70 protected $mDbUser, $mDbPass;
71
72 // A description of the script, children should change this
73 protected $mDescription = '';
74
75 // Have we already loaded our user input?
76 protected $mInputLoaded = false;
77
78 // Batch size. If a script supports this, they should set
79 // a default with setBatchSize()
80 protected $mBatchSize = null;
81
82 /**
83 * List of all the core maintenance scripts. This is added
84 * to scripts added by extensions in $wgMaintenanceScripts
85 * and returned by getMaintenanceScripts()
86 */
87 protected static $mCoreScripts = null;
88
89 /**
90 * Default constructor. Children should call this if implementing
91 * their own constructors
92 */
93 public function __construct() {
94 $this->addDefaultParams();
95 }
96
97 /**
98 * Do the actual work. All child classes will need to implement this
99 */
100 abstract public function execute();
101
102 /**
103 * Add a parameter to the script. Will be displayed on --help
104 * with the associated description
105 *
106 * @param $name String The name of the param (help, version, etc)
107 * @param $description String The description of the param to show on --help
108 * @param $required boolean Is the param required?
109 * @param $withArg Boolean Is an argument required with this option?
110 */
111 protected function addOption( $name, $description, $required = false, $withArg = false ) {
112 $this->mParams[$name] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg );
113 }
114
115 /**
116 * Checks to see if a particular param exists.
117 * @param $name String The name of the param
118 * @return boolean
119 */
120 protected function hasOption( $name ) {
121 return isset( $this->mOptions[$name] );
122 }
123
124 /**
125 * Get an option, or return the default
126 * @param $name String The name of the param
127 * @param $default mixed Anything you want, default null
128 * @return mixed
129 */
130 protected function getOption( $name, $default = null ) {
131 if( $this->hasOption( $name ) ) {
132 return $this->mOptions[$name];
133 } else {
134 // Set it so we don't have to provide the default again
135 $this->mOptions[$name] = $default;
136 return $this->mOptions[$name];
137 }
138 }
139
140 /**
141 * Add some args that are needed
142 * @param $arg String Name of the arg, like 'start'
143 * @param $description String Short description of the arg
144 * @param $required Boolean Is this required?
145 */
146 protected function addArg( $arg, $description, $required = true ) {
147 $this->mArgList[] = array(
148 'name' => $arg,
149 'desc' => $description,
150 'require' => $required
151 );
152 }
153
154 /**
155 * Does a given argument exist?
156 * @param $argId int The integer value (from zero) for the arg
157 * @return boolean
158 */
159 protected function hasArg( $argId = 0 ) {
160 return isset( $this->mArgs[$argId] );
161 }
162
163 /**
164 * Get an argument.
165 * @param $argId int The integer value (from zero) for the arg
166 * @param $default mixed The default if it doesn't exist
167 * @return mixed
168 */
169 protected function getArg( $argId = 0, $default = null ) {
170 return $this->hasArg( $argId ) ? $this->mArgs[$argId] : $default;
171 }
172
173 /**
174 * Set the batch size.
175 * @param $s int The number of operations to do in a batch
176 */
177 protected function setBatchSize( $s = 0 ) {
178 $this->mBatchSize = $s;
179 }
180
181 /**
182 * Get the script's name
183 * @return String
184 */
185 public function getName() {
186 return $this->mSelf;
187 }
188
189 /**
190 * Return input from stdin.
191 * @param $length int The number of bytes to read. If null,
192 * just return the handle. Maintenance::STDIN_ALL returns
193 * the full length
194 * @return mixed
195 */
196 protected function getStdin( $len = null ) {
197 if ( $len == Maintenance::STDIN_ALL )
198 return file_get_contents( 'php://stdin' );
199 $f = fopen( 'php://stdin', 'rt' );
200 if( !$len )
201 return $f;
202 $input = fgets( $f, $len );
203 fclose( $f );
204 return rtrim( $input );
205 }
206
207 /**
208 * Throw some output to the user. Scripts can call this with no fears,
209 * as we handle all --quiet stuff here
210 * @param $out String The text to show to the user
211 */
212 protected function output( $out ) {
213 if( $this->mQuiet ) {
214 return;
215 }
216 $f = fopen( 'php://stdout', 'w' );
217 fwrite( $f, $out );
218 fclose( $f );
219 }
220
221 /**
222 * Throw an error to the user. Doesn't respect --quiet, so don't use
223 * this for non-error output
224 * @param $err String The error to display
225 * @param $die boolean If true, go ahead and die out.
226 */
227 protected function error( $err, $die = false ) {
228 $f = fopen( 'php://stderr', 'w' );
229 fwrite( $f, $err . "\n" );
230 fclose( $f );
231 if( $die ) die();
232 }
233
234 /**
235 * Does the script need different DB access? By default, we give Maintenance
236 * scripts normal rights to the DB. Sometimes, a script needs admin rights
237 * access for a reason and sometimes they want no access. Subclasses should
238 * override and return one of the following values, as needed:
239 * Maintenance::DB_NONE - For no DB access at all
240 * Maintenance::DB_STD - For normal DB access, default
241 * Maintenance::DB_ADMIN - For admin DB access
242 * @return int
243 */
244 protected function getDbType() {
245 return Maintenance::DB_STD;
246 }
247
248 /**
249 * Add the default parameters to the scripts
250 */
251 protected function addDefaultParams() {
252 $this->addOption( 'help', "Display this help message" );
253 $this->addOption( 'quiet', "Whether to supress non-error output" );
254 $this->addOption( 'conf', "Location of LocalSettings.php, if not default", false, true );
255 $this->addOption( 'wiki', "For specifying the wiki ID", false, true );
256 $this->addOption( 'globals', "Output globals at the end of processing for debugging" );
257 // If we support a DB, show the options
258 if( $this->getDbType() > 0 ) {
259 $this->addOption( 'dbuser', "The DB user to use for this script", false, true );
260 $this->addOption( 'dbpass', "The password to use for this script", false, true );
261 }
262 // If we support $mBatchSize, show the option
263 if( $this->mBatchSize ) {
264 $this->addOption( 'batch-size', 'Run this many operations ' .
265 'per batch, default: ' . $this->mBatchSize , false, true );
266 }
267 }
268
269 /**
270 * Run a child maintenance script. Pass all of the current arguments
271 * to it.
272 * @param $maintClass String A name of a child maintenance class
273 * @param $classFile String Full path of where the child is
274 * @return Maintenance child
275 */
276 protected function runChild( $maintClass, $classFile = null ) {
277 // If we haven't already specified, kill setup procedures
278 // for child scripts, we've already got a sane environment
279 self::disableSetup();
280
281 // Make sure the class is loaded first
282 if( !class_exists( $maintClass ) ) {
283 if( $classFile ) {
284 require_once( $classFile );
285 }
286 if( !class_exists( $maintClass ) ) {
287 $this->error( "Cannot spawn child: $maintClass" );
288 }
289 }
290
291 $child = new $maintClass();
292 $child->loadParamsAndArgs( $this->mSelf, $this->mOptions, $this->mArgs );
293 return $child;
294 }
295
296 /**
297 * Disable Setup.php mostly
298 */
299 protected static function disableSetup() {
300 if( !defined( 'MW_NO_SETUP' ) )
301 define( 'MW_NO_SETUP', true );
302 }
303
304 /**
305 * Do some sanity checking and basic setup
306 */
307 public function setup() {
308 global $IP, $wgCommandLineMode, $wgUseNormalUser, $wgRequestTime;
309
310 # Abort if called from a web server
311 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
312 $this->error( "This script must be run from the command line", true );
313 }
314
315 # Make sure we can handle script parameters
316 if( !ini_get( 'register_argc_argv' ) ) {
317 $this->error( "Cannot get command line arguments, register_argc_argv is set to false", true );
318 }
319
320 if( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
321 // Send PHP warnings and errors to stderr instead of stdout.
322 // This aids in diagnosing problems, while keeping messages
323 // out of redirected output.
324 if( ini_get( 'display_errors' ) ) {
325 ini_set( 'display_errors', 'stderr' );
326 }
327
328 // Don't touch the setting on earlier versions of PHP,
329 // as setting it would disable output if you'd wanted it.
330
331 // Note that exceptions are also sent to stderr when
332 // command-line mode is on, regardless of PHP version.
333 }
334
335 # Set the memory limit
336 ini_set( 'memory_limit', $this->memoryLimit() );
337
338 # Set max execution time to 0 (no limit). PHP.net says that
339 # "When running PHP from the command line the default setting is 0."
340 # But sometimes this doesn't seem to be the case.
341 ini_set( 'max_execution_time', 0 );
342
343 $wgRequestTime = microtime( true );
344
345 # Define us as being in MediaWiki
346 define( 'MEDIAWIKI', true );
347
348 # Setup $IP, using MW_INSTALL_PATH if it exists
349 $IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== ''
350 ? getenv( 'MW_INSTALL_PATH' )
351 : realpath( dirname( __FILE__ ) . '/..' );
352
353 $wgCommandLineMode = true;
354 # Turn off output buffering if it's on
355 @ob_end_flush();
356
357 if ( !isset( $wgUseNormalUser ) ) {
358 $wgUseNormalUser = false;
359 }
360
361 $this->loadParamsAndArgs();
362 $this->maybeHelp();
363 $this->validateParamsAndArgs();
364 }
365
366 /**
367 * Normally we disable the memory_limit when running admin scripts.
368 * Some scripts may wish to actually set a limit, however, to avoid
369 * blowing up unexpectedly.
370 */
371 public function memoryLimit() {
372 return -1;
373 }
374
375 /**
376 * Clear all params and arguments.
377 */
378 public function clearParamsAndArgs() {
379 $this->mOptions = array();
380 $this->mArgs = array();
381 $this->mInputLoaded = false;
382 }
383
384 /**
385 * Process command line arguments
386 * $mOptions becomes an array with keys set to the option names
387 * $mArgs becomes a zero-based array containing the non-option arguments
388 *
389 * @param $self String The name of the script, if any
390 * @param $opts Array An array of options, in form of key=>value
391 * @param $args Array An array of command line arguments
392 */
393 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) {
394 # If we were given opts or args, set those and return early
395 if( $self ) {
396 $this->mSelf = $self;
397 $this->mInputLoaded = true;
398 }
399 if( $opts ) {
400 $this->mOptions = $opts;
401 $this->mInputLoaded = true;
402 }
403 if( $args ) {
404 $this->mArgs = $args;
405 $this->mInputLoaded = true;
406 }
407
408 # If we've already loaded input (either by user values or from $argv)
409 # skip on loading it again. The array_shift() will corrupt values if
410 # it's run again and again
411 if( $this->mInputLoaded ) {
412 $this->loadSpecialVars();
413 return;
414 }
415
416 global $argv;
417 $this->mSelf = array_shift( $argv );
418
419 $options = array();
420 $args = array();
421
422 # Parse arguments
423 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
424 if ( $arg == '--' ) {
425 # End of options, remainder should be considered arguments
426 $arg = next( $argv );
427 while( $arg !== false ) {
428 $args[] = $arg;
429 $arg = next( $argv );
430 }
431 break;
432 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
433 # Long options
434 $option = substr( $arg, 2 );
435 if ( isset( $this->mParams[$option] ) && $this->mParams[$option]['withArg'] ) {
436 $param = next( $argv );
437 if ( $param === false ) {
438 $this->error( "\nERROR: $option needs a value after it\n" );
439 $this->maybeHelp( true );
440 }
441 $options[$option] = $param;
442 } else {
443 $bits = explode( '=', $option, 2 );
444 if( count( $bits ) > 1 ) {
445 $option = $bits[0];
446 $param = $bits[1];
447 } else {
448 $param = 1;
449 }
450 $options[$option] = $param;
451 }
452 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
453 # Short options
454 for ( $p=1; $p<strlen( $arg ); $p++ ) {
455 $option = $arg{$p};
456 if ( isset( $this->mParams[$option]['withArg'] ) && $this->mParams[$option]['withArg'] ) {
457 $param = next( $argv );
458 if ( $param === false ) {
459 $this->error( "\nERROR: $option needs a value after it\n" );
460 $this->maybeHelp( true );
461 }
462 $options[$option] = $param;
463 } else {
464 $options[$option] = 1;
465 }
466 }
467 } else {
468 $args[] = $arg;
469 }
470 }
471
472 $this->mOptions = $options;
473 $this->mArgs = $args;
474 $this->loadSpecialVars();
475 $this->mInputLoaded = true;
476 }
477
478 /**
479 * Run some validation checks on the params, etc
480 */
481 protected function validateParamsAndArgs() {
482 $die = false;
483 # Check to make sure we've got all the required options
484 foreach( $this->mParams as $opt => $info ) {
485 if( $info['require'] && !$this->hasOption( $opt ) ) {
486 $this->error( "Param $opt required!" );
487 $die = true;
488 }
489 }
490 # Check arg list too
491 foreach( $this->mArgList as $k => $info ) {
492 if( $info['require'] && !$this->hasArg($k) ) {
493 $this->error( "Argument <" . $info['name'] . "> required!" );
494 $die = true;
495 }
496 }
497
498 if( $die ) $this->maybeHelp( true );
499 }
500
501 /**
502 * Handle the special variables that are global to all scripts
503 */
504 protected function loadSpecialVars() {
505 if( $this->hasOption( 'dbuser' ) )
506 $this->mDbUser = $this->getOption( 'dbuser' );
507 if( $this->hasOption( 'dbpass' ) )
508 $this->mDbPass = $this->getOption( 'dbpass' );
509 if( $this->hasOption( 'quiet' ) )
510 $this->mQuiet = true;
511 if( $this->hasOption( 'batch-size' ) )
512 $this->mBatchSize = $this->getOption( 'batch-size' );
513 }
514
515 /**
516 * Maybe show the help.
517 * @param $force boolean Whether to force the help to show, default false
518 */
519 protected function maybeHelp( $force = false ) {
520 ksort( $this->mParams );
521 if( $this->hasOption( 'help' ) || $force ) {
522 $this->mQuiet = false;
523 if( $this->mDescription ) {
524 $this->output( "\n" . $this->mDescription . "\n" );
525 }
526 $this->output( "\nUsage: php " . $this->mSelf );
527 if( $this->mParams ) {
528 $this->output( " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]" );
529 }
530 if( $this->mArgList ) {
531 $this->output( " <" );
532 foreach( $this->mArgList as $k => $arg ) {
533 $this->output( $arg['name'] . ">" );
534 if( $k < count( $this->mArgList ) - 1 )
535 $this->output( " <" );
536 }
537 }
538 $this->output( "\n" );
539 foreach( $this->mParams as $par => $info ) {
540 $this->output( "\t$par : " . $info['desc'] . "\n" );
541 }
542 foreach( $this->mArgList as $info ) {
543 $this->output( "\t<" . $info['name'] . "> : " . $info['desc'] . "\n" );
544 }
545 die( 1 );
546 }
547 }
548
549 /**
550 * Handle some last-minute setup here.
551 */
552 public function finalSetup() {
553 global $wgCommandLineMode, $wgUseNormalUser, $wgShowSQLErrors;
554 global $wgTitle, $wgProfiling, $IP, $wgDBadminuser, $wgDBadminpassword;
555 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
556
557 # Turn off output buffering again, it might have been turned on in the settings files
558 if( ob_get_level() ) {
559 ob_end_flush();
560 }
561 # Same with these
562 $wgCommandLineMode = true;
563
564 # If these were passed, use them
565 if( $this->mDbUser )
566 $wgDBadminuser = $this->mDbUser;
567 if( $this->mDbPass )
568 $wgDBadminpassword = $this->mDbPass;
569
570 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
571 $wgDBuser = $wgDBadminuser;
572 $wgDBpassword = $wgDBadminpassword;
573
574 if( $wgDBservers ) {
575 foreach ( $wgDBservers as $i => $server ) {
576 $wgDBservers[$i]['user'] = $wgDBuser;
577 $wgDBservers[$i]['password'] = $wgDBpassword;
578 }
579 }
580 if( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
581 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
582 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
583 }
584 }
585
586 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
587 $fn = MW_CMDLINE_CALLBACK;
588 $fn();
589 }
590
591 $wgShowSQLErrors = true;
592 @set_time_limit( 0 );
593
594 $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
595 }
596
597 /**
598 * Potentially debug globals. Originally a feature only
599 * for refreshLinks
600 */
601 public function globals() {
602 if( $this->hasOption( 'globals' ) ) {
603 print_r( $GLOBALS );
604 }
605 }
606
607 /**
608 * Do setup specific to WMF
609 */
610 public function loadWikimediaSettings() {
611 global $IP, $wgNoDBParam, $wgUseNormalUser, $wgConf, $site, $lang;
612
613 if ( empty( $wgNoDBParam ) ) {
614 # Check if we were passed a db name
615 if ( isset( $this->mOptions['wiki'] ) ) {
616 $db = $this->mOptions['wiki'];
617 } else {
618 $db = array_shift( $this->mArgs );
619 }
620 list( $site, $lang ) = $wgConf->siteFromDB( $db );
621
622 # If not, work out the language and site the old way
623 if ( is_null( $site ) || is_null( $lang ) ) {
624 if ( !$db ) {
625 $lang = 'aa';
626 } else {
627 $lang = $db;
628 }
629 if ( isset( $this->mArgs[0] ) ) {
630 $site = array_shift( $this->mArgs );
631 } else {
632 $site = 'wikipedia';
633 }
634 }
635 } else {
636 $lang = 'aa';
637 $site = 'wikipedia';
638 }
639
640 # This is for the IRC scripts, which now run as the apache user
641 # The apache user doesn't have access to the wikiadmin_pass command
642 if ( $_ENV['USER'] == 'apache' ) {
643 #if ( posix_geteuid() == 48 ) {
644 $wgUseNormalUser = true;
645 }
646
647 putenv( 'wikilang=' . $lang );
648
649 $DP = $IP;
650 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
651
652 if ( $lang == 'test' && $site == 'wikipedia' ) {
653 define( 'TESTWIKI', 1 );
654 }
655 }
656
657 /**
658 * Generic setup for most installs. Returns the location of LocalSettings
659 * @return String
660 */
661 public function loadSettings() {
662 global $wgWikiFarm, $wgCommandLineMode, $IP, $DP;
663
664 $wgWikiFarm = false;
665 if ( isset( $this->mOptions['conf'] ) ) {
666 $settingsFile = $this->mOptions['conf'];
667 } else {
668 $settingsFile = "$IP/LocalSettings.php";
669 }
670 if ( isset( $this->mOptions['wiki'] ) ) {
671 $bits = explode( '-', $this->mOptions['wiki'] );
672 if ( count( $bits ) == 1 ) {
673 $bits[] = '';
674 }
675 define( 'MW_DB', $bits[0] );
676 define( 'MW_PREFIX', $bits[1] );
677 }
678
679 if ( !is_readable( $settingsFile ) ) {
680 $this->error( "A copy of your installation's LocalSettings.php\n" .
681 "must exist and be readable in the source directory.", true );
682 }
683 $wgCommandLineMode = true;
684 $DP = $IP;
685 return $settingsFile;
686 }
687
688 /**
689 * Support function for cleaning up redundant text records
690 * @param $delete boolean Whether or not to actually delete the records
691 * @author Rob Church <robchur@gmail.com>
692 */
693 protected function purgeRedundantText( $delete = true ) {
694 # Data should come off the master, wrapped in a transaction
695 $dbw = wfGetDB( DB_MASTER );
696 $dbw->begin();
697
698 $tbl_arc = $dbw->tableName( 'archive' );
699 $tbl_rev = $dbw->tableName( 'revision' );
700 $tbl_txt = $dbw->tableName( 'text' );
701
702 # Get "active" text records from the revisions table
703 $this->output( "Searching for active text records in revisions table..." );
704 $res = $dbw->query( "SELECT DISTINCT rev_text_id FROM $tbl_rev" );
705 foreach( $res as $row ) {
706 $cur[] = $row->rev_text_id;
707 }
708 $this->output( "done.\n" );
709
710 # Get "active" text records from the archive table
711 $this->output( "Searching for active text records in archive table..." );
712 $res = $dbw->query( "SELECT DISTINCT ar_text_id FROM $tbl_arc" );
713 foreach( $res as $row ) {
714 $cur[] = $row->ar_text_id;
715 }
716 $this->output( "done.\n" );
717
718 # Get the IDs of all text records not in these sets
719 $this->output( "Searching for inactive text records..." );
720 $set = implode( ', ', $cur );
721 $res = $dbw->query( "SELECT old_id FROM $tbl_txt WHERE old_id NOT IN ( $set )" );
722 $old = array();
723 foreach( $res as $row ) {
724 $old[] = $row->old_id;
725 }
726 $this->output( "done.\n" );
727
728 # Inform the user of what we're going to do
729 $count = count( $old );
730 $this->output( "$count inactive items found.\n" );
731
732 # Delete as appropriate
733 if( $delete && $count ) {
734 $this->output( "Deleting..." );
735 $set = implode( ', ', $old );
736 $dbw->query( "DELETE FROM $tbl_txt WHERE old_id IN ( $set )" );
737 $this->output( "done.\n" );
738 }
739
740 # Done
741 $dbw->commit();
742 }
743
744 /**
745 * Get the maintenance directory.
746 */
747 protected function getDir() {
748 return dirname( __FILE__ );
749 }
750
751 /**
752 * Get the list of available maintenance scripts. Note
753 * that if you call this _before_ calling doMaintenance
754 * you won't have any extensions in it yet
755 * @return array
756 */
757 public static function getMaintenanceScripts() {
758 global $wgMaintenanceScripts;
759 return $wgMaintenanceScripts + self::getCoreScripts();
760 }
761
762 /**
763 * Return all of the core maintenance scripts
764 * @return array
765 */
766 protected static function getCoreScripts() {
767 if( !self::$mCoreScripts ) {
768 self::disableSetup();
769 $paths = array(
770 dirname( __FILE__ ),
771 dirname( __FILE__ ) . '/gearman',
772 dirname( __FILE__ ) . '/language',
773 dirname( __FILE__ ) . '/storage',
774 );
775 self::$mCoreScripts = array();
776 foreach( $paths as $p ) {
777 $handle = opendir( $p );
778 while( ( $file = readdir( $handle ) ) !== false ) {
779 if( $file == 'Maintenance.php' )
780 continue;
781 $file = $p . '/' . $file;
782 if( is_dir( $file ) || !strpos( $file, '.php' ) ||
783 ( strpos( file_get_contents( $file ), '$maintClass' ) === false ) ) {
784 continue;
785 }
786 require( $file );
787 $vars = get_defined_vars();
788 if( array_key_exists( 'maintClass', $vars ) ) {
789 self::$mCoreScripts[$vars['maintClass']] = $file;
790 }
791 }
792 closedir( $handle );
793 }
794 }
795 return self::$mCoreScripts;
796 }
797 }