(bug 23375) Added ogv, oga, spx as extensions for ogg files. Patch by Derk-Jan Hartman.
[lhc/web/wiklou.git] / maintenance / Maintenance.php
index 107e66a..ba395ef 100644 (file)
@@ -1,4 +1,10 @@
 <?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;
@@ -92,6 +98,7 @@ abstract class Maintenance {
         */
        public function __construct() {
                $this->addDefaultParams();
+               register_shutdown_function( array( $this, 'outputChanneled' ), false );
        }
 
        /**
@@ -208,14 +215,14 @@ abstract class Maintenance {
         * 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 );
        }
 
        /**
@@ -225,12 +232,56 @@ abstract class Maintenance {
         * @param $die boolean If true, go ahead and die out.
         */
        protected function error( $err, $die = false ) {
-               $f = fopen( 'php://stderr', 'w' ); 
-               fwrite( $f, $err . "\n" );
-               fclose( $f );
+               $this->outputChanneled( false );
+               if ( php_sapi_name() == 'cli' ) {
+                       fwrite( STDERR, $err . "\n" );
+               } else {
+                       $f = fopen( 'php://stderr', 'w' ); 
+                       fwrite( $f, $err . "\n" );
+                       fclose( $f );
+               }
                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
@@ -241,7 +292,7 @@ abstract class Maintenance {
         *    Maintenance::DB_ADMIN -  For admin DB access
         * @return int
         */
-       protected function getDbType() {
+       public function getDbType() {
                return Maintenance::DB_STD;
        }
 
@@ -305,7 +356,7 @@ abstract class Maintenance {
         * 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 ) ) {
@@ -333,7 +384,8 @@ abstract class Maintenance {
                }
 
                # Set the memory limit
-               ini_set( 'memory_limit', -1 );
+               # Note we need to set it again later in cache LocalSettings changed it
+               ini_set( 'memory_limit', $this->memoryLimit() );
 
                # Set max execution time to 0 (no limit). PHP.net says that
                # "When running PHP from the command line the default setting is 0."
@@ -354,15 +406,20 @@ abstract class Maintenance {
                # 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
+        * blowing up unexpectedly.
+        */
+       public function memoryLimit() {
+               return -1;
+       }
+
        /**
         * Clear all params and arguments.
         */
@@ -444,7 +501,7 @@ abstract class Maintenance {
                                # Short options
                                for ( $p=1; $p<strlen( $arg ); $p++ ) {
                                        $option = $arg{$p};
-                                       if ( $this->mParams[$option]['withArg'] ) {
+                                       if ( isset( $this->mParams[$option]['withArg'] ) && $this->mParams[$option]['withArg'] ) {
                                                $param = next( $argv );
                                                if ( $param === false ) {
                                                        $this->error( "\nERROR: $option needs a value after it\n" );
@@ -508,30 +565,37 @@ abstract class Maintenance {
         * @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 );
                }
@@ -541,8 +605,8 @@ abstract class Maintenance {
         * 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
@@ -558,7 +622,7 @@ abstract class Maintenance {
                if( $this->mDbPass )
                        $wgDBadminpassword = $this->mDbPass;
 
-               if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
+               if ( $this->getDbType() == self::DB_ADMIN && isset( $wgDBadminuser ) ) {
                        $wgDBuser = $wgDBadminuser;
                        $wgDBpassword = $wgDBadminpassword;
 
@@ -581,6 +645,7 @@ abstract class Maintenance {
 
                $wgShowSQLErrors = true;
                @set_time_limit( 0 );
+               ini_set( 'memory_limit', $this->memoryLimit() );
 
                $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
        }
@@ -785,4 +850,91 @@ abstract class Maintenance {
                }
                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;
+       }
+
 }