Services: Convert PasswordReset's static to a const now HHVM is gone
[lhc/web/wiklou.git] / maintenance / sql.php
index dba4a22..e7988fe 100644 (file)
@@ -25,7 +25,7 @@
 require_once __DIR__ . '/Maintenance.php';
 
 use MediaWiki\MediaWikiServices;
-use Wikimedia\Rdbms\ResultWrapper;
+use Wikimedia\Rdbms\IResultWrapper;
 use Wikimedia\Rdbms\IDatabase;
 use Wikimedia\Rdbms\DBQueryError;
 
@@ -42,6 +42,8 @@ class MwSql extends Maintenance {
                $this->addOption( 'query',
                        'Run a single query instead of running interactively', false, true );
                $this->addOption( 'json', 'Output the results as JSON instead of PHP objects' );
+               $this->addOption( 'status', 'Return successful exit status only if the query succeeded '
+                       . '(selected or altered rows), otherwise 1 for errors, 2 for no rows' );
                $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
                $this->addOption( 'wikidb',
                        'The database wiki ID to use if not the current one', false, true );
@@ -65,7 +67,7 @@ class MwSql extends Maintenance {
                $replicaDB = $this->getOption( 'replicadb', $this->getOption( 'slave', '' ) );
                if ( $replicaDB === 'any' ) {
                        $index = DB_REPLICA;
-               } elseif ( $replicaDB != '' ) {
+               } elseif ( $replicaDB !== '' ) {
                        $index = null;
                        $serverCount = $lb->getServerCount();
                        for ( $i = 0; $i < $serverCount; ++$i ) {
@@ -74,15 +76,14 @@ class MwSql extends Maintenance {
                                        break;
                                }
                        }
-                       if ( $index === null ) {
+                       if ( $index === null || $index === $lb->getWriterIndex() ) {
                                $this->fatalError( "No replica DB server configured with the name '$replicaDB'." );
                        }
                } else {
                        $index = DB_MASTER;
                }
 
-               /** @var IDatabase $db DB handle for the appropriate cluster/wiki */
-               $db = $lb->getConnection( $index, [], $wiki );
+               $db = $lb->getMaintenanceConnectionRef( $index, [], $wiki );
                if ( $replicaDB != '' && $db->getLBInfo( 'master' ) !== null ) {
                        $this->fatalError( "The server selected ({$db->getServer()}) is not a replica DB." );
                }
@@ -108,8 +109,11 @@ class MwSql extends Maintenance {
 
                if ( $this->hasOption( 'query' ) ) {
                        $query = $this->getOption( 'query' );
-                       $this->sqlDoQuery( $db, $query, /* dieOnError */ true );
+                       $res = $this->sqlDoQuery( $db, $query, /* dieOnError */ true );
                        wfWaitForSlaves();
+                       if ( $this->hasOption( 'status' ) ) {
+                               exit( $res ? 0 : 2 );
+                       }
                        return;
                }
 
@@ -128,6 +132,7 @@ class MwSql extends Maintenance {
                $newPrompt = '> ';
                $prompt = $newPrompt;
                $doDie = !Maintenance::posix_isatty( 0 );
+               $res = 1;
                while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
                        if ( !$line ) {
                                # User simply pressed return key
@@ -148,35 +153,46 @@ class MwSql extends Maintenance {
                                readline_add_history( $wholeLine . ';' );
                                readline_write_history( $historyFile );
                        }
-                       $this->sqlDoQuery( $db, $wholeLine, $doDie );
+                       $res = $this->sqlDoQuery( $db, $wholeLine, $doDie );
                        $prompt = $newPrompt;
                        $wholeLine = '';
                }
                wfWaitForSlaves();
+               if ( $this->hasOption( 'status' ) ) {
+                       exit( $res ? 0 : 2 );
+               }
        }
 
+       /**
+        * @param IDatabase $db
+        * @param string $line The SQL text of the query
+        * @param bool $dieOnError
+        * @return int|null Number of rows selected or updated, or null if the query was unsuccessful.
+        */
        protected function sqlDoQuery( IDatabase $db, $line, $dieOnError ) {
                try {
                        $res = $db->query( $line );
-                       $this->sqlPrintResult( $res, $db );
+                       return $this->sqlPrintResult( $res, $db );
                } catch ( DBQueryError $e ) {
                        if ( $dieOnError ) {
-                               $this->fatalError( $e );
+                               $this->fatalError( (string)$e );
                        } else {
-                               $this->error( $e );
+                               $this->error( (string)$e );
                        }
                }
+               return null;
        }
 
        /**
         * Print the results, callback for $db->sourceStream()
-        * @param ResultWrapper|bool $res
+        * @param IResultWrapper|bool $res
         * @param IDatabase $db
+        * @return int|null Number of rows selected or updated, or null if the query was unsuccessful.
         */
        public function sqlPrintResult( $res, $db ) {
                if ( !$res ) {
                        // Do nothing
-                       return;
+                       return null;
                } elseif ( is_object( $res ) ) {
                        $out = '';
                        $rows = [];
@@ -190,6 +206,7 @@ class MwSql extends Maintenance {
                                $out = 'Query OK, 0 row(s) affected';
                        }
                        $this->output( $out . "\n" );
+                       return count( $rows );
                } else {
                        $affected = $db->affectedRows();
                        if ( $this->hasOption( 'json' ) ) {
@@ -197,6 +214,7 @@ class MwSql extends Maintenance {
                        } else {
                                $this->output( "Query OK, $affected row(s) affected\n" );
                        }
+                       return $affected;
                }
        }