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