<?php
+/**
+ * @file
+ * @ingroup Maintenance
+ * @defgroup Maintenance Maintenance
+ */
+
// Define this so scripts can easily find doMaintenance.php
define( 'DO_MAINTENANCE', dirname( __FILE__ ) . '/doMaintenance.php' );
$maintClass = false;
*/
public function __construct() {
$this->addDefaultParams();
+ register_shutdown_function( array( $this, 'outputChanneled' ), false );
}
/**
* Throw some output to the user. Scripts can call this with no fears,
* as we handle all --quiet stuff here
* @param $out String The text to show to the user
+ * @param $channel Mixed Unique identifier for the channel. See function outputChanneled.
*/
- protected function output( $out ) {
+ protected function output( $out, $channel = null ) {
if( $this->mQuiet ) {
return;
}
- $f = fopen( 'php://stdout', 'w' );
- fwrite( $f, $out );
- fclose( $f );
+ $out = preg_replace( '/\n\z/', '', $out );
+ $this->outputChanneled( $out, $channel );
}
/**
* @param $die boolean If true, go ahead and die out.
*/
protected function error( $err, $die = false ) {
+ $this->outputChanneled( false );
if ( php_sapi_name() == 'cli' ) {
fwrite( STDERR, $err . "\n" );
} else {
if( $die ) die();
}
+ private $atLineStart = true;
+ private $lastChannel = null;
+
+ /**
+ * Message outputter with channeled message support. Messages on the
+ * same channel are concatenated, but any intervening messages in another
+ * channel start a new line.
+ * @param $msg String The message without trailing newline
+ * @param $channel Channel identifier or null for no channel. Channel comparison uses ===.
+ */
+ public function outputChanneled( $msg, $channel = null ) {
+ $handle = fopen( 'php://stdout', 'w' );
+
+ if ( $msg === false ) {
+ // For cleanup
+ if ( !$this->atLineStart ) fwrite( $handle, "\n" );
+ fclose( $handle );
+ return;
+ }
+
+ // End the current line if necessary
+ if ( !$this->atLineStart && $channel !== $this->lastChannel ) {
+ fwrite( $handle, "\n" );
+ }
+
+ fwrite( $handle, $msg );
+
+ $this->atLineStart = false;
+ if ( $channel === null ) {
+ // For unchanneled messages, output trailing newline immediately
+ fwrite( $handle, "\n" );
+ $this->atLineStart = true;
+ }
+ $this->lastChannel = $channel;
+
+ // Cleanup handle
+ fclose( $handle );
+ }
+
/**
* Does the script need different DB access? By default, we give Maintenance
* scripts normal rights to the DB. Sometimes, a script needs admin rights
* Maintenance::DB_ADMIN - For admin DB access
* @return int
*/
- protected function getDbType() {
+ public function getDbType() {
return Maintenance::DB_STD;
}
* Do some sanity checking and basic setup
*/
public function setup() {
- global $IP, $wgCommandLineMode, $wgUseNormalUser, $wgRequestTime;
+ global $IP, $wgCommandLineMode, $wgRequestTime;
# Abort if called from a web server
if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
# Turn off output buffering if it's on
@ob_end_flush();
- if ( !isset( $wgUseNormalUser ) ) {
- $wgUseNormalUser = false;
- }
-
$this->loadParamsAndArgs();
$this->maybeHelp();
$this->validateParamsAndArgs();
}
-
+
/**
* Normally we disable the memory_limit when running admin scripts.
* Some scripts may wish to actually set a limit, however, to avoid
* @param $force boolean Whether to force the help to show, default false
*/
protected function maybeHelp( $force = false ) {
+ $screenWidth = 80; // TODO: Caculate this!
+ $tab = " ";
+ $descWidth = $screenWidth - ( 2 * strlen( $tab ) );
+
ksort( $this->mParams );
if( $this->hasOption( 'help' ) || $force ) {
$this->mQuiet = false;
+
if( $this->mDescription ) {
$this->output( "\n" . $this->mDescription . "\n" );
}
- $this->output( "\nUsage: php " . $this->mSelf );
+ $output = "\nUsage: php " . basename( $this->mSelf );
if( $this->mParams ) {
- $this->output( " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]" );
+ $output .= " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]";
}
if( $this->mArgList ) {
- $this->output( " <" );
+ $output .= " <";
foreach( $this->mArgList as $k => $arg ) {
- $this->output( $arg['name'] . ">" );
+ $output .= $arg['name'] . ">";
if( $k < count( $this->mArgList ) - 1 )
- $this->output( " <" );
+ $output .= " <";
}
}
- $this->output( "\n" );
+ $this->output( "$output\n" );
foreach( $this->mParams as $par => $info ) {
- $this->output( "\t$par : " . $info['desc'] . "\n" );
+ $this->output( wordwrap( "$tab$par : " . $info['desc'], $descWidth,
+ "\n$tab$tab" ) . "\n" );
}
foreach( $this->mArgList as $info ) {
- $this->output( "\t<" . $info['name'] . "> : " . $info['desc'] . "\n" );
+ $this->output( wordwrap( "$tab<" . $info['name'] . "> : " .
+ $info['desc'], $descWidth, "\n$tab$tab" ) . "\n" );
}
die( 1 );
}
* Handle some last-minute setup here.
*/
public function finalSetup() {
- global $wgCommandLineMode, $wgUseNormalUser, $wgShowSQLErrors;
- global $wgTitle, $wgProfiling, $IP, $wgDBadminuser, $wgDBadminpassword;
+ global $wgCommandLineMode, $wgShowSQLErrors;
+ global $wgProfiling, $IP, $wgDBadminuser, $wgDBadminpassword;
global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
# Turn off output buffering again, it might have been turned on in the settings files
if( $this->mDbPass )
$wgDBadminpassword = $this->mDbPass;
- if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
+ if ( $this->getDbType() == self::DB_ADMIN && isset( $wgDBadminuser ) ) {
$wgDBuser = $wgDBadminuser;
$wgDBpassword = $wgDBadminpassword;
}
return self::$mCoreScripts;
}
+
+ /**
+ * Lock the search index
+ * @param &$db Database object
+ */
+ private function lockSearchindex( &$db ) {
+ $write = array( 'searchindex' );
+ $read = array( 'page', 'revision', 'text', 'interwiki', 'l10n_cache' );
+ $db->lockTables( $read, $write, __CLASS__ . '::' . __METHOD__ );
+ }
+
+ /**
+ * Unlock the tables
+ * @param &$db Database object
+ */
+ private function unlockSearchindex( &$db ) {
+ $db->unlockTables( __CLASS__ . '::' . __METHOD__ );
+ }
+
+ /**
+ * Unlock and lock again
+ * Since the lock is low-priority, queued reads will be able to complete
+ * @param &$db Database object
+ */
+ private function relockSearchindex( &$db ) {
+ $this->unlockSearchindex( $db );
+ $this->lockSearchindex( $db );
+ }
+
+ /**
+ * Perform a search index update with locking
+ * @param $maxLockTime integer the maximum time to keep the search index locked.
+ * @param $updateFunction callback the function that will update the function.
+ */
+ public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) {
+ $lockTime = time();
+
+ # Lock searchindex
+ if ( $maxLockTime ) {
+ $this->output( " --- Waiting for lock ---" );
+ $this->lockSearchindex( $dbw );
+ $lockTime = time();
+ $this->output( "\n" );
+ }
+
+ # Loop through the results and do a search update
+ foreach ( $results as $row ) {
+ # Allow reads to be processed
+ if ( $maxLockTime && time() > $lockTime + $maxLockTime ) {
+ $this->output( " --- Relocking ---" );
+ $this->relockSearchindex( $dbw );
+ $lockTime = time();
+ $this->output( "\n" );
+ }
+ call_user_func( $callback, $dbw, $row );
+ }
+
+ # Unlock searchindex
+ if ( $maxLockTime ) {
+ $this->output( " --- Unlocking --" );
+ $this->unlockSearchindex( $dbw );
+ $this->output( "\n" );
+ }
+
+ }
+
+ /**
+ * Update the searchindex table for a given pageid
+ * @param $dbw Database a database write handle
+ * @param $pageId the page ID to update.
+ */
+ public function updateSearchIndexForPage( $dbw, $pageId ) {
+ // Get current revision
+ $rev = Revision::loadFromPageId( $dbw, $pageId );
+ $title = null;
+ if( $rev ) {
+ $titleObj = $rev->getTitle();
+ $title = $titleObj->getPrefixedDBkey();
+ $this->output( "$title..." );
+ # Update searchindex
+ $u = new SearchUpdate( $pageId, $titleObj->getText(), $rev->getText() );
+ $u->doUpdate();
+ $this->output( "\n" );
+ }
+ return $title;
+ }
+
}