+ /** @var IDatabase $db DB handle for the appropriate cluster/wiki */
+ $db = $lb->getConnection( $index, [], $wiki );
+ if ( $replicaDB != '' && $db->getLBInfo( 'master' ) !== null ) {
+ $this->error( "The server selected ({$db->getServer()}) is not a replica DB.", 1 );
+ }
+
+ if ( $index === DB_MASTER ) {
+ $updater = DatabaseUpdater::newForDB( $db, true, $this );
+ $db->setSchemaVars( $updater->getSchemaVars() );
+ }
+
+ if ( $this->hasArg( 0 ) ) {
+ $file = fopen( $this->getArg( 0 ), 'r' );
+ if ( !$file ) {
+ $this->error( "Unable to open input file", true );
+ }
+
+ $error = $db->sourceStream( $file, null, [ $this, 'sqlPrintResult' ] );
+ if ( $error !== true ) {
+ $this->error( $error, true );
+ } else {
+ exit( 0 );
+ }
+ }
+
+ if ( $this->hasOption( 'query' ) ) {
+ $query = $this->getOption( 'query' );
+ $this->sqlDoQuery( $db, $query, /* dieOnError */ true );
+ wfWaitForSlaves();
+ return;
+ }