5 * @defgroup Maintenance Maintenance
8 // Define this so scripts can easily find doMaintenance.php
9 define( 'DO_MAINTENANCE', dirname( __FILE__
) . '/doMaintenance.php' );
12 function wfRunMaintenance( $class ) {
14 require_once( DO_MAINTENANCE
);
17 // Make sure we're on PHP5 or better
18 if ( version_compare( PHP_VERSION
, '5.1.0' ) < 0 ) {
19 die ( "Sorry! This version of MediaWiki requires PHP 5.1.x; you are running " .
20 PHP_VERSION
. ".\n\n" .
21 "If you are sure you already have PHP 5.1.x or higher installed, it may be\n" .
22 "installed in a different path from PHP " . PHP_VERSION
. ". Check with your system\n" .
27 * Abstract maintenance class for quickly writing and churning out
28 * maintenance scripts with minimal effort. All that _must_ be defined
29 * is the execute() method. See docs/maintenance.txt for more info
30 * and a quick demo of how to use it.
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License along
43 * with this program; if not, write to the Free Software Foundation, Inc.,
44 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
45 * http://www.gnu.org/copyleft/gpl.html
47 * @author Chad Horohoe <chad@anyonecanedit.org>
49 * @ingroup Maintenance
51 abstract class Maintenance
{
54 * Constants for DB access type
55 * @see Maintenance::getDbType()
61 // Const for getStdin()
62 const STDIN_ALL
= 'all';
64 // This is the desired params
65 protected $mParams = array();
67 // Array of desired args
68 protected $mArgList = array();
70 // This is the list of options that were actually passed
71 protected $mOptions = array();
73 // This is the list of arguments that were actually passed
74 protected $mArgs = array();
76 // Name of the script currently running
79 // Special vars for params that are always used
80 protected $mQuiet = false;
81 protected $mDbUser, $mDbPass;
83 // A description of the script, children should change this
84 protected $mDescription = '';
86 // Have we already loaded our user input?
87 protected $mInputLoaded = false;
89 // Batch size. If a script supports this, they should set
90 // a default with setBatchSize()
91 protected $mBatchSize = null;
94 * List of all the core maintenance scripts. This is added
95 * to scripts added by extensions in $wgMaintenanceScripts
96 * and returned by getMaintenanceScripts()
98 protected static $mCoreScripts = null;
101 * Default constructor. Children should call this if implementing
102 * their own constructors
104 public function __construct() {
105 $this->addDefaultParams();
106 register_shutdown_function( array( $this, 'outputChanneled' ), false );
110 * Do the actual work. All child classes will need to implement this
112 abstract public function execute();
115 * Add a parameter to the script. Will be displayed on --help
116 * with the associated description
118 * @param $name String: the name of the param (help, version, etc)
119 * @param $description String: the description of the param to show on --help
120 * @param $required Boolean: is the param required?
121 * @param $withArg Boolean: is an argument required with this option?
123 protected function addOption( $name, $description, $required = false, $withArg = false ) {
124 $this->mParams
[$name] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg );
128 * Checks to see if a particular param exists.
129 * @param $name String: the name of the param
132 protected function hasOption( $name ) {
133 return isset( $this->mOptions
[$name] );
137 * Get an option, or return the default
138 * @param $name String: the name of the param
139 * @param $default Mixed: anything you want, default null
142 protected function getOption( $name, $default = null ) {
143 if ( $this->hasOption( $name ) ) {
144 return $this->mOptions
[$name];
146 // Set it so we don't have to provide the default again
147 $this->mOptions
[$name] = $default;
148 return $this->mOptions
[$name];
153 * Add some args that are needed
154 * @param $arg String: name of the arg, like 'start'
155 * @param $description String: short description of the arg
156 * @param $required Boolean: is this required?
158 protected function addArg( $arg, $description, $required = true ) {
159 $this->mArgList
[] = array(
161 'desc' => $description,
162 'require' => $required
167 * Remove an option. Useful for removing options that won't be used in your script.
168 * @param $name String: the option to remove.
170 protected function deleteOption( $name ) {
171 unset( $this->mParams
[$name] );
175 * Set the description text.
176 * @param $text String: the text of the description
178 protected function addDescription( $text ) {
179 $this->mDescription
= $text;
183 * Does a given argument exist?
184 * @param $argId Integer: the integer value (from zero) for the arg
187 protected function hasArg( $argId = 0 ) {
188 return isset( $this->mArgs
[$argId] );
193 * @param $argId Integer: the integer value (from zero) for the arg
194 * @param $default Mixed: the default if it doesn't exist
197 protected function getArg( $argId = 0, $default = null ) {
198 return $this->hasArg( $argId ) ?
$this->mArgs
[$argId] : $default;
202 * Set the batch size.
203 * @param $s Integer: the number of operations to do in a batch
205 protected function setBatchSize( $s = 0 ) {
206 $this->mBatchSize
= $s;
210 * Get the script's name
213 public function getName() {
218 * Return input from stdin.
219 * @param $len Integer: the number of bytes to read. If null,
220 * just return the handle. Maintenance::STDIN_ALL returns
224 protected function getStdin( $len = null ) {
225 if ( $len == Maintenance
::STDIN_ALL
) {
226 return file_get_contents( 'php://stdin' );
228 $f = fopen( 'php://stdin', 'rt' );
232 $input = fgets( $f, $len );
234 return rtrim( $input );
238 * Throw some output to the user. Scripts can call this with no fears,
239 * as we handle all --quiet stuff here
240 * @param $out String: the text to show to the user
241 * @param $channel Mixed: unique identifier for the channel. See
242 * function outputChanneled.
244 protected function output( $out, $channel = null ) {
245 if ( $this->mQuiet
) {
248 if ( $channel === null ) {
249 $this->cleanupChanneled();
251 $f = fopen( 'php://stdout', 'w' );
256 $out = preg_replace( '/\n\z/', '', $out );
257 $this->outputChanneled( $out, $channel );
262 * Throw an error to the user. Doesn't respect --quiet, so don't use
263 * this for non-error output
264 * @param $err String: the error to display
265 * @param $die Boolean: If true, go ahead and die out.
267 protected function error( $err, $die = false ) {
268 $this->outputChanneled( false );
269 if ( php_sapi_name() == 'cli' ) {
270 fwrite( STDERR
, $err . "\n" );
272 $f = fopen( 'php://stderr', 'w' );
273 fwrite( $f, $err . "\n" );
281 private $atLineStart = true;
282 private $lastChannel = null;
285 * Clean up channeled output. Output a newline if necessary.
287 public function cleanupChanneled() {
288 if ( !$this->atLineStart
) {
289 $handle = fopen( 'php://stdout', 'w' );
290 fwrite( $handle, "\n" );
292 $this->atLineStart
= true;
297 * Message outputter with channeled message support. Messages on the
298 * same channel are concatenated, but any intervening messages in another
299 * channel start a new line.
300 * @param $msg String: the message without trailing newline
301 * @param $channel Channel identifier or null for no
302 * channel. Channel comparison uses ===.
304 public function outputChanneled( $msg, $channel = null ) {
305 if ( $msg === false ) {
306 $this->cleanupChanneled();
310 $handle = fopen( 'php://stdout', 'w' );
312 // End the current line if necessary
313 if ( !$this->atLineStart
&& $channel !== $this->lastChannel
) {
314 fwrite( $handle, "\n" );
317 fwrite( $handle, $msg );
319 $this->atLineStart
= false;
320 if ( $channel === null ) {
321 // For unchanneled messages, output trailing newline immediately
322 fwrite( $handle, "\n" );
323 $this->atLineStart
= true;
325 $this->lastChannel
= $channel;
332 * Does the script need different DB access? By default, we give Maintenance
333 * scripts normal rights to the DB. Sometimes, a script needs admin rights
334 * access for a reason and sometimes they want no access. Subclasses should
335 * override and return one of the following values, as needed:
336 * Maintenance::DB_NONE - For no DB access at all
337 * Maintenance::DB_STD - For normal DB access, default
338 * Maintenance::DB_ADMIN - For admin DB access
341 public function getDbType() {
342 return Maintenance
::DB_STD
;
346 * Add the default parameters to the scripts
348 protected function addDefaultParams() {
349 $this->addOption( 'help', 'Display this help message' );
350 $this->addOption( 'quiet', 'Whether to supress non-error output' );
351 $this->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true );
352 $this->addOption( 'wiki', 'For specifying the wiki ID', false, true );
353 $this->addOption( 'globals', 'Output globals at the end of processing for debugging' );
354 // If we support a DB, show the options
355 if ( $this->getDbType() > 0 ) {
356 $this->addOption( 'dbuser', 'The DB user to use for this script', false, true );
357 $this->addOption( 'dbpass', 'The password to use for this script', false, true );
359 // If we support $mBatchSize, show the option
360 if ( $this->mBatchSize
) {
361 $this->addOption( 'batch-size', 'Run this many operations ' .
362 'per batch, default: ' . $this->mBatchSize
, false, true );
367 * Run a child maintenance script. Pass all of the current arguments
369 * @param $maintClass String: a name of a child maintenance class
370 * @param $classFile String: full path of where the child is
371 * @return Maintenance child
373 protected function runChild( $maintClass, $classFile = null ) {
374 // If we haven't already specified, kill setup procedures
375 // for child scripts, we've already got a sane environment
376 self
::disableSetup();
378 // Make sure the class is loaded first
379 if ( !class_exists( $maintClass ) ) {
381 require_once( $classFile );
383 if ( !class_exists( $maintClass ) ) {
384 $this->error( "Cannot spawn child: $maintClass" );
388 $child = new $maintClass();
389 $child->loadParamsAndArgs( $this->mSelf
, $this->mOptions
, $this->mArgs
);
394 * Disable Setup.php mostly
396 protected static function disableSetup() {
397 if ( !defined( 'MW_NO_SETUP' ) ) {
398 define( 'MW_NO_SETUP', true );
403 * Do some sanity checking and basic setup
405 public function setup() {
406 global $IP, $wgCommandLineMode, $wgRequestTime;
408 # Abort if called from a web server
409 if ( isset( $_SERVER ) && isset( $_SERVER['REQUEST_METHOD'] ) ) {
410 $this->error( 'This script must be run from the command line', true );
413 # Make sure we can handle script parameters
414 if ( !ini_get( 'register_argc_argv' ) ) {
415 $this->error( 'Cannot get command line arguments, register_argc_argv is set to false', true );
418 if ( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
419 // Send PHP warnings and errors to stderr instead of stdout.
420 // This aids in diagnosing problems, while keeping messages
421 // out of redirected output.
422 if ( ini_get( 'display_errors' ) ) {
423 ini_set( 'display_errors', 'stderr' );
426 // Don't touch the setting on earlier versions of PHP,
427 // as setting it would disable output if you'd wanted it.
429 // Note that exceptions are also sent to stderr when
430 // command-line mode is on, regardless of PHP version.
433 # Set the memory limit
434 # Note we need to set it again later in cache LocalSettings changed it
435 ini_set( 'memory_limit', $this->memoryLimit() );
437 # Set max execution time to 0 (no limit). PHP.net says that
438 # "When running PHP from the command line the default setting is 0."
439 # But sometimes this doesn't seem to be the case.
440 ini_set( 'max_execution_time', 0 );
442 $wgRequestTime = microtime( true );
444 # Define us as being in MediaWiki
445 define( 'MEDIAWIKI', true );
447 # Setup $IP, using MW_INSTALL_PATH if it exists
448 $IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== ''
449 ?
getenv( 'MW_INSTALL_PATH' )
450 : realpath( dirname( __FILE__
) . '/..' );
452 $wgCommandLineMode = true;
453 # Turn off output buffering if it's on
456 $this->loadParamsAndArgs();
458 $this->validateParamsAndArgs();
462 * Normally we disable the memory_limit when running admin scripts.
463 * Some scripts may wish to actually set a limit, however, to avoid
464 * blowing up unexpectedly.
466 public function memoryLimit() {
471 * Clear all params and arguments.
473 public function clearParamsAndArgs() {
474 $this->mOptions
= array();
475 $this->mArgs
= array();
476 $this->mInputLoaded
= false;
480 * Process command line arguments
481 * $mOptions becomes an array with keys set to the option names
482 * $mArgs becomes a zero-based array containing the non-option arguments
484 * @param $self String The name of the script, if any
485 * @param $opts Array An array of options, in form of key=>value
486 * @param $args Array An array of command line arguments
488 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) {
489 # If we were given opts or args, set those and return early
491 $this->mSelf
= $self;
492 $this->mInputLoaded
= true;
495 $this->mOptions
= $opts;
496 $this->mInputLoaded
= true;
499 $this->mArgs
= $args;
500 $this->mInputLoaded
= true;
503 # If we've already loaded input (either by user values or from $argv)
504 # skip on loading it again. The array_shift() will corrupt values if
505 # it's run again and again
506 if ( $this->mInputLoaded
) {
507 $this->loadSpecialVars();
512 $this->mSelf
= array_shift( $argv );
518 for ( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
519 if ( $arg == '--' ) {
520 # End of options, remainder should be considered arguments
521 $arg = next( $argv );
522 while ( $arg !== false ) {
524 $arg = next( $argv );
527 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
529 $option = substr( $arg, 2 );
530 if ( isset( $this->mParams
[$option] ) && $this->mParams
[$option]['withArg'] ) {
531 $param = next( $argv );
532 if ( $param === false ) {
533 $this->error( "\nERROR: $option needs a value after it\n" );
534 $this->maybeHelp( true );
536 $options[$option] = $param;
538 $bits = explode( '=', $option, 2 );
539 if ( count( $bits ) > 1 ) {
545 $options[$option] = $param;
547 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
549 for ( $p = 1; $p < strlen( $arg ); $p++
) {
550 $option = $arg { $p } ;
551 if ( isset( $this->mParams
[$option]['withArg'] ) && $this->mParams
[$option]['withArg'] ) {
552 $param = next( $argv );
553 if ( $param === false ) {
554 $this->error( "\nERROR: $option needs a value after it\n" );
555 $this->maybeHelp( true );
557 $options[$option] = $param;
559 $options[$option] = 1;
567 $this->mOptions
= $options;
568 $this->mArgs
= $args;
569 $this->loadSpecialVars();
570 $this->mInputLoaded
= true;
574 * Run some validation checks on the params, etc
576 protected function validateParamsAndArgs() {
578 # Check to make sure we've got all the required options
579 foreach ( $this->mParams
as $opt => $info ) {
580 if ( $info['require'] && !$this->hasOption( $opt ) ) {
581 $this->error( "Param $opt required!" );
586 foreach ( $this->mArgList
as $k => $info ) {
587 if ( $info['require'] && !$this->hasArg( $k ) ) {
588 $this->error( 'Argument <' . $info['name'] . '> required!' );
594 $this->maybeHelp( true );
599 * Handle the special variables that are global to all scripts
601 protected function loadSpecialVars() {
602 if ( $this->hasOption( 'dbuser' ) ) {
603 $this->mDbUser
= $this->getOption( 'dbuser' );
605 if ( $this->hasOption( 'dbpass' ) ) {
606 $this->mDbPass
= $this->getOption( 'dbpass' );
608 if ( $this->hasOption( 'quiet' ) ) {
609 $this->mQuiet
= true;
611 if ( $this->hasOption( 'batch-size' ) ) {
612 $this->mBatchSize
= $this->getOption( 'batch-size' );
617 * Maybe show the help.
618 * @param $force boolean Whether to force the help to show, default false
620 protected function maybeHelp( $force = false ) {
621 $screenWidth = 80; // TODO: Caculate this!
623 $descWidth = $screenWidth - ( 2 * strlen( $tab ) );
625 ksort( $this->mParams
);
626 if ( $this->hasOption( 'help' ) ||
$force ) {
627 $this->mQuiet
= false;
629 if ( $this->mDescription
) {
630 $this->output( "\n" . $this->mDescription
. "\n" );
632 $output = "\nUsage: php " . basename( $this->mSelf
);
633 if ( $this->mParams
) {
634 $output .= " [--" . implode( array_keys( $this->mParams
), "|--" ) . "]";
636 if ( $this->mArgList
) {
638 foreach ( $this->mArgList
as $k => $arg ) {
639 $output .= $arg['name'] . ">";
640 if ( $k < count( $this->mArgList
) - 1 )
644 $this->output( "$output\n" );
645 foreach ( $this->mParams
as $par => $info ) {
647 wordwrap( "$tab$par : " . $info['desc'], $descWidth,
648 "\n$tab$tab" ) . "\n"
651 foreach ( $this->mArgList
as $info ) {
653 wordwrap( "$tab<" . $info['name'] . "> : " .
654 $info['desc'], $descWidth, "\n$tab$tab" ) . "\n"
662 * Handle some last-minute setup here.
664 public function finalSetup() {
665 global $wgCommandLineMode, $wgShowSQLErrors;
666 global $wgProfiling, $wgDBadminuser, $wgDBadminpassword;
667 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
669 # Turn off output buffering again, it might have been turned on in the settings files
670 if ( ob_get_level() ) {
674 $wgCommandLineMode = true;
676 # If these were passed, use them
677 if ( $this->mDbUser
) {
678 $wgDBadminuser = $this->mDbUser
;
680 if ( $this->mDbPass
) {
681 $wgDBadminpassword = $this->mDbPass
;
684 if ( $this->getDbType() == self
::DB_ADMIN
&& isset( $wgDBadminuser ) ) {
685 $wgDBuser = $wgDBadminuser;
686 $wgDBpassword = $wgDBadminpassword;
688 if ( $wgDBservers ) {
689 foreach ( $wgDBservers as $i => $server ) {
690 $wgDBservers[$i]['user'] = $wgDBuser;
691 $wgDBservers[$i]['password'] = $wgDBpassword;
694 if ( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
695 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
696 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
698 LBFactory
::destroyInstance();
701 $this->afterFinalSetup();
703 $wgShowSQLErrors = true;
704 @set_time_limit
( 0 );
705 ini_set( 'memory_limit', $this->memoryLimit() );
707 $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
711 * Execute a callback function at the end of initialisation
713 protected function afterFinalSetup() {
714 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
715 call_user_func( MW_CMDLINE_CALLBACK
);
720 * Potentially debug globals. Originally a feature only
723 public function globals() {
724 if ( $this->hasOption( 'globals' ) ) {
730 * Do setup specific to WMF
732 public function loadWikimediaSettings() {
733 global $IP, $wgNoDBParam, $wgUseNormalUser, $wgConf, $site, $lang;
735 if ( empty( $wgNoDBParam ) ) {
736 # Check if we were passed a db name
737 if ( isset( $this->mOptions
['wiki'] ) ) {
738 $db = $this->mOptions
['wiki'];
740 $db = array_shift( $this->mArgs
);
742 list( $site, $lang ) = $wgConf->siteFromDB( $db );
744 # If not, work out the language and site the old way
745 if ( is_null( $site ) ||
is_null( $lang ) ) {
751 if ( isset( $this->mArgs
[0] ) ) {
752 $site = array_shift( $this->mArgs
);
762 # This is for the IRC scripts, which now run as the apache user
763 # The apache user doesn't have access to the wikiadmin_pass command
764 if ( $_ENV['USER'] == 'apache' ) {
765 # if ( posix_geteuid() == 48 ) {
766 $wgUseNormalUser = true;
769 putenv( 'wikilang=' . $lang );
771 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
773 if ( $lang == 'test' && $site == 'wikipedia' ) {
774 define( 'TESTWIKI', 1 );
779 * Generic setup for most installs. Returns the location of LocalSettings
782 public function loadSettings() {
783 global $wgWikiFarm, $wgCommandLineMode, $IP;
786 if ( isset( $this->mOptions
['conf'] ) ) {
787 $settingsFile = $this->mOptions
['conf'];
789 $settingsFile = "$IP/LocalSettings.php";
791 if ( isset( $this->mOptions
['wiki'] ) ) {
792 $bits = explode( '-', $this->mOptions
['wiki'] );
793 if ( count( $bits ) == 1 ) {
796 define( 'MW_DB', $bits[0] );
797 define( 'MW_PREFIX', $bits[1] );
800 if ( !is_readable( $settingsFile ) ) {
801 $this->error( "A copy of your installation's LocalSettings.php\n" .
802 "must exist and be readable in the source directory.", true );
804 $wgCommandLineMode = true;
805 return $settingsFile;
809 * Support function for cleaning up redundant text records
810 * @param $delete Boolean: whether or not to actually delete the records
811 * @author Rob Church <robchur@gmail.com>
813 public function purgeRedundantText( $delete = true ) {
814 # Data should come off the master, wrapped in a transaction
815 $dbw = wfGetDB( DB_MASTER
);
818 $tbl_arc = $dbw->tableName( 'archive' );
819 $tbl_rev = $dbw->tableName( 'revision' );
820 $tbl_txt = $dbw->tableName( 'text' );
822 # Get "active" text records from the revisions table
823 $this->output( 'Searching for active text records in revisions table...' );
824 $res = $dbw->query( "SELECT DISTINCT rev_text_id FROM $tbl_rev" );
825 foreach ( $res as $row ) {
826 $cur[] = $row->rev_text_id
;
828 $this->output( "done.\n" );
830 # Get "active" text records from the archive table
831 $this->output( 'Searching for active text records in archive table...' );
832 $res = $dbw->query( "SELECT DISTINCT ar_text_id FROM $tbl_arc" );
833 foreach ( $res as $row ) {
834 $cur[] = $row->ar_text_id
;
836 $this->output( "done.\n" );
838 # Get the IDs of all text records not in these sets
839 $this->output( 'Searching for inactive text records...' );
840 $set = implode( ', ', $cur );
841 $res = $dbw->query( "SELECT old_id FROM $tbl_txt WHERE old_id NOT IN ( $set )" );
843 foreach ( $res as $row ) {
844 $old[] = $row->old_id
;
846 $this->output( "done.\n" );
848 # Inform the user of what we're going to do
849 $count = count( $old );
850 $this->output( "$count inactive items found.\n" );
852 # Delete as appropriate
853 if ( $delete && $count ) {
854 $this->output( 'Deleting...' );
855 $set = implode( ', ', $old );
856 $dbw->query( "DELETE FROM $tbl_txt WHERE old_id IN ( $set )" );
857 $this->output( "done.\n" );
865 * Get the maintenance directory.
867 protected function getDir() {
868 return dirname( __FILE__
);
872 * Get the list of available maintenance scripts. Note
873 * that if you call this _before_ calling doMaintenance
874 * you won't have any extensions in it yet
877 public static function getMaintenanceScripts() {
878 global $wgMaintenanceScripts;
879 return $wgMaintenanceScripts + self
::getCoreScripts();
883 * Return all of the core maintenance scripts
886 protected static function getCoreScripts() {
887 if ( !self
::$mCoreScripts ) {
888 self
::disableSetup();
891 dirname( __FILE__
) . '/gearman',
892 dirname( __FILE__
) . '/language',
893 dirname( __FILE__
) . '/storage',
895 self
::$mCoreScripts = array();
896 foreach ( $paths as $p ) {
897 $handle = opendir( $p );
898 while ( ( $file = readdir( $handle ) ) !== false ) {
899 if ( $file == 'Maintenance.php' ) {
902 $file = $p . '/' . $file;
903 if ( is_dir( $file ) ||
!strpos( $file, '.php' ) ||
904 ( strpos( file_get_contents( $file ), '$maintClass' ) === false ) ) {
908 $vars = get_defined_vars();
909 if ( array_key_exists( 'maintClass', $vars ) ) {
910 self
::$mCoreScripts[$vars['maintClass']] = $file;
916 return self
::$mCoreScripts;
920 * Lock the search index
921 * @param &$db Database object
923 private function lockSearchindex( &$db ) {
924 $write = array( 'searchindex' );
925 $read = array( 'page', 'revision', 'text', 'interwiki', 'l10n_cache' );
926 $db->lockTables( $read, $write, __CLASS__
. '::' . __METHOD__
);
931 * @param &$db Database object
933 private function unlockSearchindex( &$db ) {
934 $db->unlockTables( __CLASS__
. '::' . __METHOD__
);
938 * Unlock and lock again
939 * Since the lock is low-priority, queued reads will be able to complete
940 * @param &$db Database object
942 private function relockSearchindex( &$db ) {
943 $this->unlockSearchindex( $db );
944 $this->lockSearchindex( $db );
948 * Perform a search index update with locking
949 * @param $maxLockTime Integer: the maximum time to keep the search index locked.
950 * @param $callback callback String: the function that will update the function.
951 * @param $dbw Database object
954 public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) {
958 if ( $maxLockTime ) {
959 $this->output( " --- Waiting for lock ---" );
960 $this->lockSearchindex( $dbw );
962 $this->output( "\n" );
965 # Loop through the results and do a search update
966 foreach ( $results as $row ) {
967 # Allow reads to be processed
968 if ( $maxLockTime && time() > $lockTime +
$maxLockTime ) {
969 $this->output( " --- Relocking ---" );
970 $this->relockSearchindex( $dbw );
972 $this->output( "\n" );
974 call_user_func( $callback, $dbw, $row );
978 if ( $maxLockTime ) {
979 $this->output( " --- Unlocking --" );
980 $this->unlockSearchindex( $dbw );
981 $this->output( "\n" );
987 * Update the searchindex table for a given pageid
988 * @param $dbw Database: a database write handle
989 * @param $pageId Integer: the page ID to update.
991 public function updateSearchIndexForPage( $dbw, $pageId ) {
992 // Get current revision
993 $rev = Revision
::loadFromPageId( $dbw, $pageId );
996 $titleObj = $rev->getTitle();
997 $title = $titleObj->getPrefixedDBkey();
998 $this->output( "$title..." );
1000 $u = new SearchUpdate( $pageId, $titleObj->getText(), $rev->getText() );
1002 $this->output( "\n" );