Merge "Fix documentation of Maintenance::updateSearchIndex"
[lhc/web/wiklou.git] / maintenance / Maintenance.php
index b3e958f..6beda4e 100644 (file)
@@ -26,6 +26,7 @@ require_once __DIR__ . '/../includes/PHPVersionCheck.php';
 wfEntryPointCheck( 'text' );
 
 use MediaWiki\Shell\Shell;
+use Wikimedia\Rdbms\IResultWrapper;
 
 /**
  * @defgroup MaintenanceArchive Maintenance archives
@@ -42,6 +43,13 @@ define( 'DO_MAINTENANCE', RUN_MAINTENANCE_IF_MAIN ); // original name, harmless
 
 $maintClass = false;
 
+// Some extensions rely on MW_INSTALL_PATH to find core files to include. Setting it here helps them
+// if they're included by a core script (like DatabaseUpdater) after Maintenance.php has already
+// been run.
+if ( strval( getenv( 'MW_INSTALL_PATH' ) ) === '' ) {
+       putenv( 'MW_INSTALL_PATH=' . realpath( __DIR__ . '/..' ) );
+}
+
 use Wikimedia\Rdbms\IDatabase;
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\MediaWikiServices;
@@ -171,11 +179,8 @@ abstract class Maintenance {
         * their own constructors
         */
        public function __construct() {
-               // Setup $IP, using MW_INSTALL_PATH if it exists
                global $IP;
-               $IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== ''
-                       ? getenv( 'MW_INSTALL_PATH' )
-                       : realpath( __DIR__ . '/..' );
+               $IP = getenv( 'MW_INSTALL_PATH' );
 
                $this->addDefaultParams();
                register_shutdown_function( [ $this, 'outputChanneled' ], false );
@@ -336,6 +341,10 @@ abstract class Maintenance {
         * @return bool
         */
        protected function hasArg( $argId = 0 ) {
+               if ( func_num_args() === 0 ) {
+                       wfDeprecated( __METHOD__ . ' without an $argId', '1.33' );
+               }
+
                return isset( $this->mArgs[$argId] );
        }
 
@@ -346,7 +355,11 @@ abstract class Maintenance {
         * @return mixed
         */
        protected function getArg( $argId = 0, $default = null ) {
-               return $this->hasArg( $argId ) ? $this->mArgs[$argId] : $default;
+               if ( func_num_args() === 0 ) {
+                       wfDeprecated( __METHOD__ . ' without an $argId', '1.33' );
+               }
+
+               return $this->mArgs[$argId] ?? $default;
        }
 
        /**
@@ -709,7 +722,7 @@ abstract class Maintenance {
                }
 
                /**
-                * @var $child Maintenance
+                * @var Maintenance $child
                 */
                $child = new $maintClass();
                $child->loadParamsAndArgs( $this->mSelf, $this->mOptions, $this->mArgs );
@@ -1183,7 +1196,7 @@ abstract class Maintenance {
 
                        if ( $wgDBservers ) {
                                /**
-                                * @var $wgDBservers array
+                                * @var array $wgDBservers
                                 */
                                foreach ( $wgDBservers as $i => $server ) {
                                        $wgDBservers[$i]['user'] = $wgDBuser;
@@ -1466,9 +1479,9 @@ abstract class Maintenance {
        /**
         * Perform a search index update with locking
         * @param int $maxLockTime The maximum time to keep the search index locked.
-        * @param string $callback The function that will update the function.
+        * @param callable $callback The function that will update the function.
         * @param IMaintainableDatabase $dbw
-        * @param array $results
+        * @param array|IResultWrapper $results
         */
        public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) {
                $lockTime = time();
@@ -1607,10 +1620,10 @@ abstract class Maintenance {
                $bash = ExecutableFinder::findInDefaultPaths( 'bash' );
                if ( !wfIsWindows() && $bash ) {
                        $retval = false;
-                       $encPrompt = wfEscapeShellArg( $prompt );
+                       $encPrompt = Shell::escape( $prompt );
                        $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
-                       $encCommand = wfEscapeShellArg( $command );
-                       $line = wfShellExec( "$bash -c $encCommand", $retval, [], [ 'walltime' => 0 ] );
+                       $encCommand = Shell::escape( $command );
+                       $line = Shell::escape( "$bash -c $encCommand", $retval, [], [ 'walltime' => 0 ] );
 
                        if ( $retval == 0 ) {
                                return $line;
@@ -1712,7 +1725,7 @@ abstract class LoggedUpdateMaintenance extends Maintenance {
                        return false;
                }
 
-               $db->insert( 'updatelog', [ 'ul_key' => $key ], __METHOD__, 'IGNORE' );
+               $db->insert( 'updatelog', [ 'ul_key' => $key ], __METHOD__, [ 'IGNORE' ] );
 
                return true;
        }