Merge "Remove old workaround for HHVM"
[lhc/web/wiklou.git] / maintenance / Maintenance.php
index a8080c5..7de0ae4 100644 (file)
@@ -25,6 +25,8 @@
 require_once __DIR__ . '/../includes/PHPVersionCheck.php';
 wfEntryPointCheck( 'cli' );
 
+use Wikimedia\Rdbms\DBReplicationWaitError;
+
 /**
  * @defgroup MaintenanceArchive Maintenance archives
  * @ingroup Maintenance
@@ -40,6 +42,7 @@ use Wikimedia\Rdbms\IDatabase;
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\LBFactory;
+use Wikimedia\Rdbms\IMaintainableDatabase;
 
 /**
  * Abstract maintenance class for quickly writing and churning out
@@ -106,7 +109,7 @@ abstract class Maintenance {
 
        /**
         * Used by getDB() / setDB()
-        * @var Database
+        * @var IMaintainableDatabase
         */
        private $mDb = null;
 
@@ -206,7 +209,7 @@ abstract class Maintenance {
         * @param string $description The description of the param to show on --help
         * @param bool $required Is the param required?
         * @param bool $withArg Is an argument required with this option?
-        * @param string $shortName Character to use as short name
+        * @param string|bool $shortName Character to use as short name
         * @param bool $multiOccurrence Can this option be passed multiple times?
         */
        protected function addOption( $name, $description, $required = false,
@@ -341,7 +344,7 @@ abstract class Maintenance {
         * @return mixed
         */
        protected function getStdin( $len = null ) {
-               if ( $len == Maintenance::STDIN_ALL ) {
+               if ( $len == self::STDIN_ALL ) {
                        return file_get_contents( 'php://stdin' );
                }
                $f = fopen( 'php://stdin', 'rt' );
@@ -454,7 +457,7 @@ abstract class Maintenance {
         * @return int
         */
        public function getDbType() {
-               return Maintenance::DB_STD;
+               return self::DB_STD;
        }
 
        /**
@@ -1240,10 +1243,10 @@ abstract class Maintenance {
         * If not set, wfGetDB() will be used.
         * This function has the same parameters as wfGetDB()
         *
-        * @param integer $db DB index (DB_REPLICA/DB_MASTER)
-        * @param array $groups; default: empty array
-        * @param string|bool $wiki; default: current wiki
-        * @return Database
+        * @param int $db DB index (DB_REPLICA/DB_MASTER)
+        * @param array $groups default: empty array
+        * @param string|bool $wiki default: current wiki
+        * @return IMaintainableDatabase
         */
        protected function getDB( $db, $groups = [], $wiki = false ) {
                if ( is_null( $this->mDb ) ) {
@@ -1256,7 +1259,7 @@ abstract class Maintenance {
        /**
         * Sets database object to be returned by getDB().
         *
-        * @param IDatabase $db Database object to be used
+        * @param IDatabase $db
         */
        public function setDB( IDatabase $db ) {
                $this->mDb = $db;
@@ -1318,7 +1321,7 @@ abstract class Maintenance {
 
        /**
         * Lock the search index
-        * @param Database &$db
+        * @param IMaintainableDatabase &$db
         */
        private function lockSearchindex( $db ) {
                $write = [ 'searchindex' ];
@@ -1336,7 +1339,7 @@ abstract class Maintenance {
 
        /**
         * Unlock the tables
-        * @param Database &$db
+        * @param IMaintainableDatabase &$db
         */
        private function unlockSearchindex( $db ) {
                $db->unlockTables( __CLASS__ . '::' . __METHOD__ );
@@ -1345,7 +1348,7 @@ abstract class Maintenance {
        /**
         * Unlock and lock again
         * Since the lock is low-priority, queued reads will be able to complete
-        * @param Database &$db
+        * @param IMaintainableDatabase &$db
         */
        private function relockSearchindex( $db ) {
                $this->unlockSearchindex( $db );
@@ -1356,7 +1359,7 @@ 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 Database $dbw
+        * @param IMaintainableDatabase $dbw
         * @param array $results
         */
        public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) {
@@ -1392,7 +1395,7 @@ abstract class Maintenance {
 
        /**
         * Update the searchindex table for a given pageid
-        * @param Database $dbw A database write handle
+        * @param IDatabase $dbw A database write handle
         * @param int $pageId The page ID to update.
         * @return null|string
         */
@@ -1413,6 +1416,32 @@ abstract class Maintenance {
                return $title;
        }
 
+       /**
+        * Count down from $seconds to zero on the terminal, with a one-second pause
+        * between showing each number. If the maintenance script is in quiet mode,
+        * this function does nothing.
+        *
+        * @since 1.31
+        *
+        * @codeCoverageIgnore
+        * @param int $seconds
+        */
+       protected function countDown( $seconds ) {
+               if ( $this->isQuiet() ) {
+                       return;
+               }
+               for ( $i = $seconds; $i >= 0; $i-- ) {
+                       if ( $i != $seconds ) {
+                               $this->output( str_repeat( "\x08", strlen( $i + 1 ) ) );
+                       }
+                       $this->output( $i );
+                       if ( $i ) {
+                               sleep( 1 );
+                       }
+               }
+               $this->output( "\n" );
+       }
+
        /**
         * Wrapper for posix_isatty()
         * We default as considering stdin a tty (for nice readline methods)
@@ -1441,13 +1470,7 @@ abstract class Maintenance {
                }
 
                if ( $isatty && function_exists( 'readline' ) ) {
-                       $resp = readline( $prompt );
-                       if ( $resp === null ) {
-                               // Workaround for https://github.com/facebook/hhvm/issues/4776
-                               return false;
-                       } else {
-                               return $resp;
-                       }
+                       return readline( $prompt );
                } else {
                        if ( $isatty ) {
                                $st = self::readlineEmulation( $prompt );