Fixing bug 20524: Hideuser: Nicer error when trying to block hidden user without...
[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 if ( php_sapi_name() == 'cli' ) {
229 fwrite( STDERR, $err . "\n" );
230 } else {
231 $f = fopen( 'php://stderr', 'w' );
232 fwrite( $f, $err . "\n" );
233 fclose( $f );
234 }
235 if( $die ) die();
236 }
237
238 /**
239 * Does the script need different DB access? By default, we give Maintenance
240 * scripts normal rights to the DB. Sometimes, a script needs admin rights
241 * access for a reason and sometimes they want no access. Subclasses should
242 * override and return one of the following values, as needed:
243 * Maintenance::DB_NONE - For no DB access at all
244 * Maintenance::DB_STD - For normal DB access, default
245 * Maintenance::DB_ADMIN - For admin DB access
246 * @return int
247 */
248 protected function getDbType() {
249 return Maintenance::DB_STD;
250 }
251
252 /**
253 * Add the default parameters to the scripts
254 */
255 protected function addDefaultParams() {
256 $this->addOption( 'help', "Display this help message" );
257 $this->addOption( 'quiet', "Whether to supress non-error output" );
258 $this->addOption( 'conf', "Location of LocalSettings.php, if not default", false, true );
259 $this->addOption( 'wiki', "For specifying the wiki ID", false, true );
260 $this->addOption( 'globals', "Output globals at the end of processing for debugging" );
261 // If we support a DB, show the options
262 if( $this->getDbType() > 0 ) {
263 $this->addOption( 'dbuser', "The DB user to use for this script", false, true );
264 $this->addOption( 'dbpass', "The password to use for this script", false, true );
265 }
266 // If we support $mBatchSize, show the option
267 if( $this->mBatchSize ) {
268 $this->addOption( 'batch-size', 'Run this many operations ' .
269 'per batch, default: ' . $this->mBatchSize , false, true );
270 }
271 }
272
273 /**
274 * Run a child maintenance script. Pass all of the current arguments
275 * to it.
276 * @param $maintClass String A name of a child maintenance class
277 * @param $classFile String Full path of where the child is
278 * @return Maintenance child
279 */
280 protected function runChild( $maintClass, $classFile = null ) {
281 // If we haven't already specified, kill setup procedures
282 // for child scripts, we've already got a sane environment
283 self::disableSetup();
284
285 // Make sure the class is loaded first
286 if( !class_exists( $maintClass ) ) {
287 if( $classFile ) {
288 require_once( $classFile );
289 }
290 if( !class_exists( $maintClass ) ) {
291 $this->error( "Cannot spawn child: $maintClass" );
292 }
293 }
294
295 $child = new $maintClass();
296 $child->loadParamsAndArgs( $this->mSelf, $this->mOptions, $this->mArgs );
297 return $child;
298 }
299
300 /**
301 * Disable Setup.php mostly
302 */
303 protected static function disableSetup() {
304 if( !defined( 'MW_NO_SETUP' ) )
305 define( 'MW_NO_SETUP', true );
306 }
307
308 /**
309 * Do some sanity checking and basic setup
310 */
311 public function setup() {
312 global $IP, $wgCommandLineMode, $wgUseNormalUser, $wgRequestTime;
313
314 # Abort if called from a web server
315 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
316 $this->error( "This script must be run from the command line", true );
317 }
318
319 # Make sure we can handle script parameters
320 if( !ini_get( 'register_argc_argv' ) ) {
321 $this->error( "Cannot get command line arguments, register_argc_argv is set to false", true );
322 }
323
324 if( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
325 // Send PHP warnings and errors to stderr instead of stdout.
326 // This aids in diagnosing problems, while keeping messages
327 // out of redirected output.
328 if( ini_get( 'display_errors' ) ) {
329 ini_set( 'display_errors', 'stderr' );
330 }
331
332 // Don't touch the setting on earlier versions of PHP,
333 // as setting it would disable output if you'd wanted it.
334
335 // Note that exceptions are also sent to stderr when
336 // command-line mode is on, regardless of PHP version.
337 }
338
339 # Set the memory limit
340 # Note we need to set it again later in cache LocalSettings changed it
341 ini_set( 'memory_limit', $this->memoryLimit() );
342
343 # Set max execution time to 0 (no limit). PHP.net says that
344 # "When running PHP from the command line the default setting is 0."
345 # But sometimes this doesn't seem to be the case.
346 ini_set( 'max_execution_time', 0 );
347
348 $wgRequestTime = microtime( true );
349
350 # Define us as being in MediaWiki
351 define( 'MEDIAWIKI', true );
352
353 # Setup $IP, using MW_INSTALL_PATH if it exists
354 $IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== ''
355 ? getenv( 'MW_INSTALL_PATH' )
356 : realpath( dirname( __FILE__ ) . '/..' );
357
358 $wgCommandLineMode = true;
359 # Turn off output buffering if it's on
360 @ob_end_flush();
361
362 if ( !isset( $wgUseNormalUser ) ) {
363 $wgUseNormalUser = false;
364 }
365
366 $this->loadParamsAndArgs();
367 $this->maybeHelp();
368 $this->validateParamsAndArgs();
369 }
370
371 /**
372 * Normally we disable the memory_limit when running admin scripts.
373 * Some scripts may wish to actually set a limit, however, to avoid
374 * blowing up unexpectedly.
375 */
376 public function memoryLimit() {
377 return -1;
378 }
379
380 /**
381 * Clear all params and arguments.
382 */
383 public function clearParamsAndArgs() {
384 $this->mOptions = array();
385 $this->mArgs = array();
386 $this->mInputLoaded = false;
387 }
388
389 /**
390 * Process command line arguments
391 * $mOptions becomes an array with keys set to the option names
392 * $mArgs becomes a zero-based array containing the non-option arguments
393 *
394 * @param $self String The name of the script, if any
395 * @param $opts Array An array of options, in form of key=>value
396 * @param $args Array An array of command line arguments
397 */
398 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) {
399 # If we were given opts or args, set those and return early
400 if( $self ) {
401 $this->mSelf = $self;
402 $this->mInputLoaded = true;
403 }
404 if( $opts ) {
405 $this->mOptions = $opts;
406 $this->mInputLoaded = true;
407 }
408 if( $args ) {
409 $this->mArgs = $args;
410 $this->mInputLoaded = true;
411 }
412
413 # If we've already loaded input (either by user values or from $argv)
414 # skip on loading it again. The array_shift() will corrupt values if
415 # it's run again and again
416 if( $this->mInputLoaded ) {
417 $this->loadSpecialVars();
418 return;
419 }
420
421 global $argv;
422 $this->mSelf = array_shift( $argv );
423
424 $options = array();
425 $args = array();
426
427 # Parse arguments
428 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
429 if ( $arg == '--' ) {
430 # End of options, remainder should be considered arguments
431 $arg = next( $argv );
432 while( $arg !== false ) {
433 $args[] = $arg;
434 $arg = next( $argv );
435 }
436 break;
437 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
438 # Long options
439 $option = substr( $arg, 2 );
440 if ( isset( $this->mParams[$option] ) && $this->mParams[$option]['withArg'] ) {
441 $param = next( $argv );
442 if ( $param === false ) {
443 $this->error( "\nERROR: $option needs a value after it\n" );
444 $this->maybeHelp( true );
445 }
446 $options[$option] = $param;
447 } else {
448 $bits = explode( '=', $option, 2 );
449 if( count( $bits ) > 1 ) {
450 $option = $bits[0];
451 $param = $bits[1];
452 } else {
453 $param = 1;
454 }
455 $options[$option] = $param;
456 }
457 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
458 # Short options
459 for ( $p=1; $p<strlen( $arg ); $p++ ) {
460 $option = $arg{$p};
461 if ( isset( $this->mParams[$option]['withArg'] ) && $this->mParams[$option]['withArg'] ) {
462 $param = next( $argv );
463 if ( $param === false ) {
464 $this->error( "\nERROR: $option needs a value after it\n" );
465 $this->maybeHelp( true );
466 }
467 $options[$option] = $param;
468 } else {
469 $options[$option] = 1;
470 }
471 }
472 } else {
473 $args[] = $arg;
474 }
475 }
476
477 $this->mOptions = $options;
478 $this->mArgs = $args;
479 $this->loadSpecialVars();
480 $this->mInputLoaded = true;
481 }
482
483 /**
484 * Run some validation checks on the params, etc
485 */
486 protected function validateParamsAndArgs() {
487 $die = false;
488 # Check to make sure we've got all the required options
489 foreach( $this->mParams as $opt => $info ) {
490 if( $info['require'] && !$this->hasOption( $opt ) ) {
491 $this->error( "Param $opt required!" );
492 $die = true;
493 }
494 }
495 # Check arg list too
496 foreach( $this->mArgList as $k => $info ) {
497 if( $info['require'] && !$this->hasArg($k) ) {
498 $this->error( "Argument <" . $info['name'] . "> required!" );
499 $die = true;
500 }
501 }
502
503 if( $die ) $this->maybeHelp( true );
504 }
505
506 /**
507 * Handle the special variables that are global to all scripts
508 */
509 protected function loadSpecialVars() {
510 if( $this->hasOption( 'dbuser' ) )
511 $this->mDbUser = $this->getOption( 'dbuser' );
512 if( $this->hasOption( 'dbpass' ) )
513 $this->mDbPass = $this->getOption( 'dbpass' );
514 if( $this->hasOption( 'quiet' ) )
515 $this->mQuiet = true;
516 if( $this->hasOption( 'batch-size' ) )
517 $this->mBatchSize = $this->getOption( 'batch-size' );
518 }
519
520 /**
521 * Maybe show the help.
522 * @param $force boolean Whether to force the help to show, default false
523 */
524 protected function maybeHelp( $force = false ) {
525 ksort( $this->mParams );
526 if( $this->hasOption( 'help' ) || $force ) {
527 $this->mQuiet = false;
528 if( $this->mDescription ) {
529 $this->output( "\n" . $this->mDescription . "\n" );
530 }
531 $this->output( "\nUsage: php " . $this->mSelf );
532 if( $this->mParams ) {
533 $this->output( " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]" );
534 }
535 if( $this->mArgList ) {
536 $this->output( " <" );
537 foreach( $this->mArgList as $k => $arg ) {
538 $this->output( $arg['name'] . ">" );
539 if( $k < count( $this->mArgList ) - 1 )
540 $this->output( " <" );
541 }
542 }
543 $this->output( "\n" );
544 foreach( $this->mParams as $par => $info ) {
545 $this->output( "\t$par : " . $info['desc'] . "\n" );
546 }
547 foreach( $this->mArgList as $info ) {
548 $this->output( "\t<" . $info['name'] . "> : " . $info['desc'] . "\n" );
549 }
550 die( 1 );
551 }
552 }
553
554 /**
555 * Handle some last-minute setup here.
556 */
557 public function finalSetup() {
558 global $wgCommandLineMode, $wgUseNormalUser, $wgShowSQLErrors;
559 global $wgTitle, $wgProfiling, $IP, $wgDBadminuser, $wgDBadminpassword;
560 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
561
562 # Turn off output buffering again, it might have been turned on in the settings files
563 if( ob_get_level() ) {
564 ob_end_flush();
565 }
566 # Same with these
567 $wgCommandLineMode = true;
568
569 # If these were passed, use them
570 if( $this->mDbUser )
571 $wgDBadminuser = $this->mDbUser;
572 if( $this->mDbPass )
573 $wgDBadminpassword = $this->mDbPass;
574
575 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
576 $wgDBuser = $wgDBadminuser;
577 $wgDBpassword = $wgDBadminpassword;
578
579 if( $wgDBservers ) {
580 foreach ( $wgDBservers as $i => $server ) {
581 $wgDBservers[$i]['user'] = $wgDBuser;
582 $wgDBservers[$i]['password'] = $wgDBpassword;
583 }
584 }
585 if( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
586 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
587 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
588 }
589 }
590
591 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
592 $fn = MW_CMDLINE_CALLBACK;
593 $fn();
594 }
595
596 $wgShowSQLErrors = true;
597 @set_time_limit( 0 );
598 ini_set( 'memory_limit', $this->memoryLimit() );
599
600 $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
601 }
602
603 /**
604 * Potentially debug globals. Originally a feature only
605 * for refreshLinks
606 */
607 public function globals() {
608 if( $this->hasOption( 'globals' ) ) {
609 print_r( $GLOBALS );
610 }
611 }
612
613 /**
614 * Do setup specific to WMF
615 */
616 public function loadWikimediaSettings() {
617 global $IP, $wgNoDBParam, $wgUseNormalUser, $wgConf, $site, $lang;
618
619 if ( empty( $wgNoDBParam ) ) {
620 # Check if we were passed a db name
621 if ( isset( $this->mOptions['wiki'] ) ) {
622 $db = $this->mOptions['wiki'];
623 } else {
624 $db = array_shift( $this->mArgs );
625 }
626 list( $site, $lang ) = $wgConf->siteFromDB( $db );
627
628 # If not, work out the language and site the old way
629 if ( is_null( $site ) || is_null( $lang ) ) {
630 if ( !$db ) {
631 $lang = 'aa';
632 } else {
633 $lang = $db;
634 }
635 if ( isset( $this->mArgs[0] ) ) {
636 $site = array_shift( $this->mArgs );
637 } else {
638 $site = 'wikipedia';
639 }
640 }
641 } else {
642 $lang = 'aa';
643 $site = 'wikipedia';
644 }
645
646 # This is for the IRC scripts, which now run as the apache user
647 # The apache user doesn't have access to the wikiadmin_pass command
648 if ( $_ENV['USER'] == 'apache' ) {
649 #if ( posix_geteuid() == 48 ) {
650 $wgUseNormalUser = true;
651 }
652
653 putenv( 'wikilang=' . $lang );
654
655 $DP = $IP;
656 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
657
658 if ( $lang == 'test' && $site == 'wikipedia' ) {
659 define( 'TESTWIKI', 1 );
660 }
661 }
662
663 /**
664 * Generic setup for most installs. Returns the location of LocalSettings
665 * @return String
666 */
667 public function loadSettings() {
668 global $wgWikiFarm, $wgCommandLineMode, $IP, $DP;
669
670 $wgWikiFarm = false;
671 if ( isset( $this->mOptions['conf'] ) ) {
672 $settingsFile = $this->mOptions['conf'];
673 } else {
674 $settingsFile = "$IP/LocalSettings.php";
675 }
676 if ( isset( $this->mOptions['wiki'] ) ) {
677 $bits = explode( '-', $this->mOptions['wiki'] );
678 if ( count( $bits ) == 1 ) {
679 $bits[] = '';
680 }
681 define( 'MW_DB', $bits[0] );
682 define( 'MW_PREFIX', $bits[1] );
683 }
684
685 if ( !is_readable( $settingsFile ) ) {
686 $this->error( "A copy of your installation's LocalSettings.php\n" .
687 "must exist and be readable in the source directory.", true );
688 }
689 $wgCommandLineMode = true;
690 $DP = $IP;
691 return $settingsFile;
692 }
693
694 /**
695 * Support function for cleaning up redundant text records
696 * @param $delete boolean Whether or not to actually delete the records
697 * @author Rob Church <robchur@gmail.com>
698 */
699 protected function purgeRedundantText( $delete = true ) {
700 # Data should come off the master, wrapped in a transaction
701 $dbw = wfGetDB( DB_MASTER );
702 $dbw->begin();
703
704 $tbl_arc = $dbw->tableName( 'archive' );
705 $tbl_rev = $dbw->tableName( 'revision' );
706 $tbl_txt = $dbw->tableName( 'text' );
707
708 # Get "active" text records from the revisions table
709 $this->output( "Searching for active text records in revisions table..." );
710 $res = $dbw->query( "SELECT DISTINCT rev_text_id FROM $tbl_rev" );
711 foreach( $res as $row ) {
712 $cur[] = $row->rev_text_id;
713 }
714 $this->output( "done.\n" );
715
716 # Get "active" text records from the archive table
717 $this->output( "Searching for active text records in archive table..." );
718 $res = $dbw->query( "SELECT DISTINCT ar_text_id FROM $tbl_arc" );
719 foreach( $res as $row ) {
720 $cur[] = $row->ar_text_id;
721 }
722 $this->output( "done.\n" );
723
724 # Get the IDs of all text records not in these sets
725 $this->output( "Searching for inactive text records..." );
726 $set = implode( ', ', $cur );
727 $res = $dbw->query( "SELECT old_id FROM $tbl_txt WHERE old_id NOT IN ( $set )" );
728 $old = array();
729 foreach( $res as $row ) {
730 $old[] = $row->old_id;
731 }
732 $this->output( "done.\n" );
733
734 # Inform the user of what we're going to do
735 $count = count( $old );
736 $this->output( "$count inactive items found.\n" );
737
738 # Delete as appropriate
739 if( $delete && $count ) {
740 $this->output( "Deleting..." );
741 $set = implode( ', ', $old );
742 $dbw->query( "DELETE FROM $tbl_txt WHERE old_id IN ( $set )" );
743 $this->output( "done.\n" );
744 }
745
746 # Done
747 $dbw->commit();
748 }
749
750 /**
751 * Get the maintenance directory.
752 */
753 protected function getDir() {
754 return dirname( __FILE__ );
755 }
756
757 /**
758 * Get the list of available maintenance scripts. Note
759 * that if you call this _before_ calling doMaintenance
760 * you won't have any extensions in it yet
761 * @return array
762 */
763 public static function getMaintenanceScripts() {
764 global $wgMaintenanceScripts;
765 return $wgMaintenanceScripts + self::getCoreScripts();
766 }
767
768 /**
769 * Return all of the core maintenance scripts
770 * @return array
771 */
772 protected static function getCoreScripts() {
773 if( !self::$mCoreScripts ) {
774 self::disableSetup();
775 $paths = array(
776 dirname( __FILE__ ),
777 dirname( __FILE__ ) . '/gearman',
778 dirname( __FILE__ ) . '/language',
779 dirname( __FILE__ ) . '/storage',
780 );
781 self::$mCoreScripts = array();
782 foreach( $paths as $p ) {
783 $handle = opendir( $p );
784 while( ( $file = readdir( $handle ) ) !== false ) {
785 if( $file == 'Maintenance.php' )
786 continue;
787 $file = $p . '/' . $file;
788 if( is_dir( $file ) || !strpos( $file, '.php' ) ||
789 ( strpos( file_get_contents( $file ), '$maintClass' ) === false ) ) {
790 continue;
791 }
792 require( $file );
793 $vars = get_defined_vars();
794 if( array_key_exists( 'maintClass', $vars ) ) {
795 self::$mCoreScripts[$vars['maintClass']] = $file;
796 }
797 }
798 closedir( $handle );
799 }
800 }
801 return self::$mCoreScripts;
802 }
803 }