5 * @defgroup Maintenance Maintenance
8 // Define this so scripts can easily find doMaintenance.php
9 define( 'DO_MAINTENANCE', dirname( __FILE__
) . '/doMaintenance.php' );
12 // Make sure we're on PHP5 or better
13 if ( version_compare( PHP_VERSION
, '5.0.0' ) < 0 ) {
14 die ( "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" );
21 * Abstract maintenance class for quickly writing and churning out
22 * maintenance scripts with minimal effort. All that _must_ be defined
23 * is the execute() method. See docs/maintenance.txt for more info
24 * and a quick demo of how to use it.
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License along
37 * with this program; if not, write to the Free Software Foundation, Inc.,
38 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
39 * http://www.gnu.org/copyleft/gpl.html
41 * @author Chad Horohoe <chad@anyonecanedit.org>
43 * @ingroup Maintenance
45 abstract class Maintenance
{
48 * Constants for DB access type
49 * @see Maintenance::getDbType()
55 // Const for getStdin()
56 const STDIN_ALL
= 'all';
58 // This is the desired params
59 protected $mParams = array();
61 // Array of desired args
62 protected $mArgList = array();
64 // This is the list of options that were actually passed
65 protected $mOptions = array();
67 // This is the list of arguments that were actually passed
68 protected $mArgs = array();
70 // Name of the script currently running
73 // Special vars for params that are always used
74 protected $mQuiet = false;
75 protected $mDbUser, $mDbPass;
77 // A description of the script, children should change this
78 protected $mDescription = '';
80 // Have we already loaded our user input?
81 protected $mInputLoaded = false;
83 // Batch size. If a script supports this, they should set
84 // a default with setBatchSize()
85 protected $mBatchSize = null;
88 * List of all the core maintenance scripts. This is added
89 * to scripts added by extensions in $wgMaintenanceScripts
90 * and returned by getMaintenanceScripts()
92 protected static $mCoreScripts = null;
95 * Default constructor. Children should call this if implementing
96 * their own constructors
98 public function __construct() {
99 $this->addDefaultParams();
100 register_shutdown_function( array( $this, 'outputChanneled' ), false );
104 * Do the actual work. All child classes will need to implement this
106 abstract public function execute();
109 * Add a parameter to the script. Will be displayed on --help
110 * with the associated description
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?
117 protected function addOption( $name, $description, $required = false, $withArg = false ) {
118 $this->mParams
[$name] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg );
122 * Checks to see if a particular param exists.
123 * @param $name String: the name of the param
126 protected function hasOption( $name ) {
127 return isset( $this->mOptions
[$name] );
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
136 protected function getOption( $name, $default = null ) {
137 if ( $this->hasOption( $name ) ) {
138 return $this->mOptions
[$name];
140 // Set it so we don't have to provide the default again
141 $this->mOptions
[$name] = $default;
142 return $this->mOptions
[$name];
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?
152 protected function addArg( $arg, $description, $required = true ) {
153 $this->mArgList
[] = array(
155 'desc' => $description,
156 'require' => $required
161 * Does a given argument exist?
162 * @param $argId Integer: the integer value (from zero) for the arg
165 protected function hasArg( $argId = 0 ) {
166 return isset( $this->mArgs
[$argId] );
171 * @param $argId Integer: the integer value (from zero) for the arg
172 * @param $default Mixed: the default if it doesn't exist
175 protected function getArg( $argId = 0, $default = null ) {
176 return $this->hasArg( $argId ) ?
$this->mArgs
[$argId] : $default;
180 * Set the batch size.
181 * @param $s Integer: the number of operations to do in a batch
183 protected function setBatchSize( $s = 0 ) {
184 $this->mBatchSize
= $s;
188 * Get the script's name
191 public function getName() {
196 * Return input from stdin.
197 * @param $len Integer: the number of bytes to read. If null,
198 * just return the handle. Maintenance::STDIN_ALL returns
202 protected function getStdin( $len = null ) {
203 if ( $len == Maintenance
::STDIN_ALL
) {
204 return file_get_contents( 'php://stdin' );
206 $f = fopen( 'php://stdin', 'rt' );
210 $input = fgets( $f, $len );
212 return rtrim( $input );
216 * Throw some output to the user. Scripts can call this with no fears,
217 * as we handle all --quiet stuff here
218 * @param $out String: the text to show to the user
219 * @param $channel Mixed: unique identifier for the channel. See
220 * function outputChanneled.
222 protected function output( $out, $channel = null ) {
223 if ( $this->mQuiet
) {
226 if ( $channel === null ) {
227 $this->cleanupChanneled();
229 $f = fopen( 'php://stdout', 'w' );
234 $out = preg_replace( '/\n\z/', '', $out );
235 $this->outputChanneled( $out, $channel );
240 * Throw an error to the user. Doesn't respect --quiet, so don't use
241 * this for non-error output
242 * @param $err String: the error to display
243 * @param $die Boolean: If true, go ahead and die out.
245 protected function error( $err, $die = false ) {
246 $this->outputChanneled( false );
247 if ( php_sapi_name() == 'cli' ) {
248 fwrite( STDERR
, $err . "\n" );
250 $f = fopen( 'php://stderr', 'w' );
251 fwrite( $f, $err . "\n" );
259 private $atLineStart = true;
260 private $lastChannel = null;
263 * Clean up channeled output. Output a newline if necessary.
265 public function cleanupChanneled() {
266 if ( !$this->atLineStart
) {
267 $handle = fopen( 'php://stdout', 'w' );
268 fwrite( $handle, "\n" );
270 $this->atLineStart
= true;
275 * Message outputter with channeled message support. Messages on the
276 * same channel are concatenated, but any intervening messages in another
277 * channel start a new line.
278 * @param $msg String: the message without trailing newline
279 * @param $channel Channel identifier or null for no
280 * channel. Channel comparison uses ===.
282 public function outputChanneled( $msg, $channel = null ) {
283 if ( $msg === false ) {
284 $this->cleanupChanneled();
288 $handle = fopen( 'php://stdout', 'w' );
290 // End the current line if necessary
291 if ( !$this->atLineStart
&& $channel !== $this->lastChannel
) {
292 fwrite( $handle, "\n" );
295 fwrite( $handle, $msg );
297 $this->atLineStart
= false;
298 if ( $channel === null ) {
299 // For unchanneled messages, output trailing newline immediately
300 fwrite( $handle, "\n" );
301 $this->atLineStart
= true;
303 $this->lastChannel
= $channel;
310 * Does the script need different DB access? By default, we give Maintenance
311 * scripts normal rights to the DB. Sometimes, a script needs admin rights
312 * access for a reason and sometimes they want no access. Subclasses should
313 * override and return one of the following values, as needed:
314 * Maintenance::DB_NONE - For no DB access at all
315 * Maintenance::DB_STD - For normal DB access, default
316 * Maintenance::DB_ADMIN - For admin DB access
319 public function getDbType() {
320 return Maintenance
::DB_STD
;
324 * Add the default parameters to the scripts
326 protected function addDefaultParams() {
327 $this->addOption( 'help', 'Display this help message' );
328 $this->addOption( 'quiet', 'Whether to supress non-error output' );
329 $this->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true );
330 $this->addOption( 'wiki', 'For specifying the wiki ID', false, true );
331 $this->addOption( 'globals', 'Output globals at the end of processing for debugging' );
332 // If we support a DB, show the options
333 if ( $this->getDbType() > 0 ) {
334 $this->addOption( 'dbuser', 'The DB user to use for this script', false, true );
335 $this->addOption( 'dbpass', 'The password to use for this script', false, true );
337 // If we support $mBatchSize, show the option
338 if ( $this->mBatchSize
) {
339 $this->addOption( 'batch-size', 'Run this many operations ' .
340 'per batch, default: ' . $this->mBatchSize
, false, true );
345 * Run a child maintenance script. Pass all of the current arguments
347 * @param $maintClass String: a name of a child maintenance class
348 * @param $classFile String: full path of where the child is
349 * @return Maintenance child
351 protected function runChild( $maintClass, $classFile = null ) {
352 // If we haven't already specified, kill setup procedures
353 // for child scripts, we've already got a sane environment
354 self
::disableSetup();
356 // Make sure the class is loaded first
357 if ( !class_exists( $maintClass ) ) {
359 require_once( $classFile );
361 if ( !class_exists( $maintClass ) ) {
362 $this->error( "Cannot spawn child: $maintClass" );
366 $child = new $maintClass();
367 $child->loadParamsAndArgs( $this->mSelf
, $this->mOptions
, $this->mArgs
);
372 * Disable Setup.php mostly
374 protected static function disableSetup() {
375 if ( !defined( 'MW_NO_SETUP' ) ) {
376 define( 'MW_NO_SETUP', true );
381 * Do some sanity checking and basic setup
383 public function setup() {
384 global $IP, $wgCommandLineMode, $wgRequestTime;
386 # Abort if called from a web server
387 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
388 $this->error( 'This script must be run from the command line', true );
391 # Make sure we can handle script parameters
392 if ( !ini_get( 'register_argc_argv' ) ) {
393 $this->error( 'Cannot get command line arguments, register_argc_argv is set to false', true );
396 if ( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
397 // Send PHP warnings and errors to stderr instead of stdout.
398 // This aids in diagnosing problems, while keeping messages
399 // out of redirected output.
400 if ( ini_get( 'display_errors' ) ) {
401 ini_set( 'display_errors', 'stderr' );
404 // Don't touch the setting on earlier versions of PHP,
405 // as setting it would disable output if you'd wanted it.
407 // Note that exceptions are also sent to stderr when
408 // command-line mode is on, regardless of PHP version.
411 # Set the memory limit
412 # Note we need to set it again later in cache LocalSettings changed it
413 ini_set( 'memory_limit', $this->memoryLimit() );
415 # Set max execution time to 0 (no limit). PHP.net says that
416 # "When running PHP from the command line the default setting is 0."
417 # But sometimes this doesn't seem to be the case.
418 ini_set( 'max_execution_time', 0 );
420 $wgRequestTime = microtime( true );
422 # Define us as being in MediaWiki
423 define( 'MEDIAWIKI', true );
425 # Setup $IP, using MW_INSTALL_PATH if it exists
426 $IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== ''
427 ?
getenv( 'MW_INSTALL_PATH' )
428 : realpath( dirname( __FILE__
) . '/..' );
430 $wgCommandLineMode = true;
431 # Turn off output buffering if it's on
434 $this->loadParamsAndArgs();
436 $this->validateParamsAndArgs();
440 * Normally we disable the memory_limit when running admin scripts.
441 * Some scripts may wish to actually set a limit, however, to avoid
442 * blowing up unexpectedly.
444 public function memoryLimit() {
449 * Clear all params and arguments.
451 public function clearParamsAndArgs() {
452 $this->mOptions
= array();
453 $this->mArgs
= array();
454 $this->mInputLoaded
= false;
458 * Process command line arguments
459 * $mOptions becomes an array with keys set to the option names
460 * $mArgs becomes a zero-based array containing the non-option arguments
462 * @param $self String The name of the script, if any
463 * @param $opts Array An array of options, in form of key=>value
464 * @param $args Array An array of command line arguments
466 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) {
467 # If we were given opts or args, set those and return early
469 $this->mSelf
= $self;
470 $this->mInputLoaded
= true;
473 $this->mOptions
= $opts;
474 $this->mInputLoaded
= true;
477 $this->mArgs
= $args;
478 $this->mInputLoaded
= true;
481 # If we've already loaded input (either by user values or from $argv)
482 # skip on loading it again. The array_shift() will corrupt values if
483 # it's run again and again
484 if ( $this->mInputLoaded
) {
485 $this->loadSpecialVars();
490 $this->mSelf
= array_shift( $argv );
496 for ( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
497 if ( $arg == '--' ) {
498 # End of options, remainder should be considered arguments
499 $arg = next( $argv );
500 while ( $arg !== false ) {
502 $arg = next( $argv );
505 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
507 $option = substr( $arg, 2 );
508 if ( isset( $this->mParams
[$option] ) && $this->mParams
[$option]['withArg'] ) {
509 $param = next( $argv );
510 if ( $param === false ) {
511 $this->error( "\nERROR: $option needs a value after it\n" );
512 $this->maybeHelp( true );
514 $options[$option] = $param;
516 $bits = explode( '=', $option, 2 );
517 if ( count( $bits ) > 1 ) {
523 $options[$option] = $param;
525 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
527 for ( $p = 1; $p < strlen( $arg ); $p++
) {
528 $option = $arg { $p } ;
529 if ( isset( $this->mParams
[$option]['withArg'] ) && $this->mParams
[$option]['withArg'] ) {
530 $param = next( $argv );
531 if ( $param === false ) {
532 $this->error( "\nERROR: $option needs a value after it\n" );
533 $this->maybeHelp( true );
535 $options[$option] = $param;
537 $options[$option] = 1;
545 $this->mOptions
= $options;
546 $this->mArgs
= $args;
547 $this->loadSpecialVars();
548 $this->mInputLoaded
= true;
552 * Run some validation checks on the params, etc
554 protected function validateParamsAndArgs() {
556 # Check to make sure we've got all the required options
557 foreach ( $this->mParams
as $opt => $info ) {
558 if ( $info['require'] && !$this->hasOption( $opt ) ) {
559 $this->error( "Param $opt required!" );
564 foreach ( $this->mArgList
as $k => $info ) {
565 if ( $info['require'] && !$this->hasArg( $k ) ) {
566 $this->error( 'Argument <' . $info['name'] . '> required!' );
572 $this->maybeHelp( true );
577 * Handle the special variables that are global to all scripts
579 protected function loadSpecialVars() {
580 if ( $this->hasOption( 'dbuser' ) ) {
581 $this->mDbUser
= $this->getOption( 'dbuser' );
583 if ( $this->hasOption( 'dbpass' ) ) {
584 $this->mDbPass
= $this->getOption( 'dbpass' );
586 if ( $this->hasOption( 'quiet' ) ) {
587 $this->mQuiet
= true;
589 if ( $this->hasOption( 'batch-size' ) ) {
590 $this->mBatchSize
= $this->getOption( 'batch-size' );
595 * Maybe show the help.
596 * @param $force boolean Whether to force the help to show, default false
598 protected function maybeHelp( $force = false ) {
599 $screenWidth = 80; // TODO: Caculate this!
601 $descWidth = $screenWidth - ( 2 * strlen( $tab ) );
603 ksort( $this->mParams
);
604 if ( $this->hasOption( 'help' ) ||
$force ) {
605 $this->mQuiet
= false;
607 if ( $this->mDescription
) {
608 $this->output( "\n" . $this->mDescription
. "\n" );
610 $output = "\nUsage: php " . basename( $this->mSelf
);
611 if ( $this->mParams
) {
612 $output .= " [--" . implode( array_keys( $this->mParams
), "|--" ) . "]";
614 if ( $this->mArgList
) {
616 foreach ( $this->mArgList
as $k => $arg ) {
617 $output .= $arg['name'] . ">";
618 if ( $k < count( $this->mArgList
) - 1 )
622 $this->output( "$output\n" );
623 foreach ( $this->mParams
as $par => $info ) {
625 wordwrap( "$tab$par : " . $info['desc'], $descWidth,
626 "\n$tab$tab" ) . "\n"
629 foreach ( $this->mArgList
as $info ) {
631 wordwrap( "$tab<" . $info['name'] . "> : " .
632 $info['desc'], $descWidth, "\n$tab$tab" ) . "\n"
640 * Handle some last-minute setup here.
642 public function finalSetup() {
643 global $wgCommandLineMode, $wgShowSQLErrors;
644 global $wgProfiling, $wgDBadminuser, $wgDBadminpassword;
645 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
647 # Turn off output buffering again, it might have been turned on in the settings files
648 if ( ob_get_level() ) {
652 $wgCommandLineMode = true;
654 # If these were passed, use them
655 if ( $this->mDbUser
) {
656 $wgDBadminuser = $this->mDbUser
;
658 if ( $this->mDbPass
) {
659 $wgDBadminpassword = $this->mDbPass
;
662 if ( $this->getDbType() == self
::DB_ADMIN
&& isset( $wgDBadminuser ) ) {
663 $wgDBuser = $wgDBadminuser;
664 $wgDBpassword = $wgDBadminpassword;
666 if ( $wgDBservers ) {
667 foreach ( $wgDBservers as $i => $server ) {
668 $wgDBservers[$i]['user'] = $wgDBuser;
669 $wgDBservers[$i]['password'] = $wgDBpassword;
672 if ( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
673 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
674 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
678 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
679 $fn = MW_CMDLINE_CALLBACK
;
683 $wgShowSQLErrors = true;
684 @set_time_limit
( 0 );
685 ini_set( 'memory_limit', $this->memoryLimit() );
687 $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
691 * Potentially debug globals. Originally a feature only
694 public function globals() {
695 if ( $this->hasOption( 'globals' ) ) {
701 * Do setup specific to WMF
703 public function loadWikimediaSettings() {
704 global $IP, $wgNoDBParam, $wgUseNormalUser, $wgConf, $site, $lang;
706 if ( empty( $wgNoDBParam ) ) {
707 # Check if we were passed a db name
708 if ( isset( $this->mOptions
['wiki'] ) ) {
709 $db = $this->mOptions
['wiki'];
711 $db = array_shift( $this->mArgs
);
713 list( $site, $lang ) = $wgConf->siteFromDB( $db );
715 # If not, work out the language and site the old way
716 if ( is_null( $site ) ||
is_null( $lang ) ) {
722 if ( isset( $this->mArgs
[0] ) ) {
723 $site = array_shift( $this->mArgs
);
733 # This is for the IRC scripts, which now run as the apache user
734 # The apache user doesn't have access to the wikiadmin_pass command
735 if ( $_ENV['USER'] == 'apache' ) {
736 # if ( posix_geteuid() == 48 ) {
737 $wgUseNormalUser = true;
740 putenv( 'wikilang=' . $lang );
742 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
744 if ( $lang == 'test' && $site == 'wikipedia' ) {
745 define( 'TESTWIKI', 1 );
750 * Generic setup for most installs. Returns the location of LocalSettings
753 public function loadSettings() {
754 global $wgWikiFarm, $wgCommandLineMode, $IP;
757 if ( isset( $this->mOptions
['conf'] ) ) {
758 $settingsFile = $this->mOptions
['conf'];
760 $settingsFile = "$IP/LocalSettings.php";
762 if ( isset( $this->mOptions
['wiki'] ) ) {
763 $bits = explode( '-', $this->mOptions
['wiki'] );
764 if ( count( $bits ) == 1 ) {
767 define( 'MW_DB', $bits[0] );
768 define( 'MW_PREFIX', $bits[1] );
771 if ( !is_readable( $settingsFile ) ) {
772 $this->error( "A copy of your installation's LocalSettings.php\n" .
773 "must exist and be readable in the source directory.", true );
775 $wgCommandLineMode = true;
776 return $settingsFile;
780 * Support function for cleaning up redundant text records
781 * @param $delete Boolean: whether or not to actually delete the records
782 * @author Rob Church <robchur@gmail.com>
784 public function purgeRedundantText( $delete = true ) {
785 # Data should come off the master, wrapped in a transaction
786 $dbw = wfGetDB( DB_MASTER
);
789 $tbl_arc = $dbw->tableName( 'archive' );
790 $tbl_rev = $dbw->tableName( 'revision' );
791 $tbl_txt = $dbw->tableName( 'text' );
793 # Get "active" text records from the revisions table
794 $this->output( 'Searching for active text records in revisions table...' );
795 $res = $dbw->query( "SELECT DISTINCT rev_text_id FROM $tbl_rev" );
796 foreach ( $res as $row ) {
797 $cur[] = $row->rev_text_id
;
799 $this->output( "done.\n" );
801 # Get "active" text records from the archive table
802 $this->output( 'Searching for active text records in archive table...' );
803 $res = $dbw->query( "SELECT DISTINCT ar_text_id FROM $tbl_arc" );
804 foreach ( $res as $row ) {
805 $cur[] = $row->ar_text_id
;
807 $this->output( "done.\n" );
809 # Get the IDs of all text records not in these sets
810 $this->output( 'Searching for inactive text records...' );
811 $set = implode( ', ', $cur );
812 $res = $dbw->query( "SELECT old_id FROM $tbl_txt WHERE old_id NOT IN ( $set )" );
814 foreach ( $res as $row ) {
815 $old[] = $row->old_id
;
817 $this->output( "done.\n" );
819 # Inform the user of what we're going to do
820 $count = count( $old );
821 $this->output( "$count inactive items found.\n" );
823 # Delete as appropriate
824 if ( $delete && $count ) {
825 $this->output( 'Deleting...' );
826 $set = implode( ', ', $old );
827 $dbw->query( "DELETE FROM $tbl_txt WHERE old_id IN ( $set )" );
828 $this->output( "done.\n" );
836 * Get the maintenance directory.
838 protected function getDir() {
839 return dirname( __FILE__
);
843 * Get the list of available maintenance scripts. Note
844 * that if you call this _before_ calling doMaintenance
845 * you won't have any extensions in it yet
848 public static function getMaintenanceScripts() {
849 global $wgMaintenanceScripts;
850 return $wgMaintenanceScripts + self
::getCoreScripts();
854 * Return all of the core maintenance scripts
857 protected static function getCoreScripts() {
858 if ( !self
::$mCoreScripts ) {
859 self
::disableSetup();
862 dirname( __FILE__
) . '/gearman',
863 dirname( __FILE__
) . '/language',
864 dirname( __FILE__
) . '/storage',
866 self
::$mCoreScripts = array();
867 foreach ( $paths as $p ) {
868 $handle = opendir( $p );
869 while ( ( $file = readdir( $handle ) ) !== false ) {
870 if ( $file == 'Maintenance.php' ) {
873 $file = $p . '/' . $file;
874 if ( is_dir( $file ) ||
!strpos( $file, '.php' ) ||
875 ( strpos( file_get_contents( $file ), '$maintClass' ) === false ) ) {
879 $vars = get_defined_vars();
880 if ( array_key_exists( 'maintClass', $vars ) ) {
881 self
::$mCoreScripts[$vars['maintClass']] = $file;
887 return self
::$mCoreScripts;
891 * Lock the search index
892 * @param &$db Database object
894 private function lockSearchindex( &$db ) {
895 $write = array( 'searchindex' );
896 $read = array( 'page', 'revision', 'text', 'interwiki', 'l10n_cache' );
897 $db->lockTables( $read, $write, __CLASS__
. '::' . __METHOD__
);
902 * @param &$db Database object
904 private function unlockSearchindex( &$db ) {
905 $db->unlockTables( __CLASS__
. '::' . __METHOD__
);
909 * Unlock and lock again
910 * Since the lock is low-priority, queued reads will be able to complete
911 * @param &$db Database object
913 private function relockSearchindex( &$db ) {
914 $this->unlockSearchindex( $db );
915 $this->lockSearchindex( $db );
919 * Perform a search index update with locking
920 * @param $maxLockTime Integer: the maximum time to keep the search index locked.
921 * @param $callback callback String: the function that will update the function.
922 * @param $dbw Database object
925 public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) {
929 if ( $maxLockTime ) {
930 $this->output( " --- Waiting for lock ---" );
931 $this->lockSearchindex( $dbw );
933 $this->output( "\n" );
936 # Loop through the results and do a search update
937 foreach ( $results as $row ) {
938 # Allow reads to be processed
939 if ( $maxLockTime && time() > $lockTime +
$maxLockTime ) {
940 $this->output( " --- Relocking ---" );
941 $this->relockSearchindex( $dbw );
943 $this->output( "\n" );
945 call_user_func( $callback, $dbw, $row );
949 if ( $maxLockTime ) {
950 $this->output( " --- Unlocking --" );
951 $this->unlockSearchindex( $dbw );
952 $this->output( "\n" );
958 * Update the searchindex table for a given pageid
959 * @param $dbw Database: a database write handle
960 * @param $pageId Integer: the page ID to update.
962 public function updateSearchIndexForPage( $dbw, $pageId ) {
963 // Get current revision
964 $rev = Revision
::loadFromPageId( $dbw, $pageId );
967 $titleObj = $rev->getTitle();
968 $title = $titleObj->getPrefixedDBkey();
969 $this->output( "$title..." );
971 $u = new SearchUpdate( $pageId, $titleObj->getText(), $rev->getText() );
973 $this->output( "\n" );