62 $wiki = $this->hasOption(
'wikidb' ) ? $this->getOption(
'wikidb' ) :
false;
64 $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
65 if ( $this->hasOption(
'cluster' ) ) {
66 $lb = $lbFactory->getExternalLB( $this->getOption(
'cluster' ) );
68 $lb = $lbFactory->getMainLB( $wiki );
71 $replicaDB = $this->getOption(
'replicadb',
'' );
72 if ( $replicaDB ===
'any' ) {
74 } elseif ( $replicaDB !==
'' ) {
76 $serverCount = $lb->getServerCount();
77 for ( $i = 0; $i < $serverCount; ++$i ) {
78 if ( $lb->getServerName( $i ) === $replicaDB ) {
84 if ( $index ===
null || $index === ServerInfo::WRITER_INDEX ) {
85 $this->fatalError(
"No replica DB server configured with the name '$replicaDB'." );
91 $db = $lb->getMaintenanceConnectionRef( $index, [], $wiki );
92 if ( $replicaDB !=
'' && $db->getLBInfo(
'master' ) !==
null ) {
93 $this->fatalError(
"Server {$db->getServerName()} is not a replica DB." );
97 $updater = DatabaseUpdater::newForDB( $db,
true, $this );
98 $db->setSchemaVars( $updater->getSchemaVars() );
101 if ( $this->hasArg( 0 ) ) {
102 $file = fopen( $this->getArg( 0 ),
'r' );
104 $this->fatalError(
"Unable to open input file" );
107 $error = $db->sourceStream( $file,
null, [ $this,
'sqlPrintResult' ], __METHOD__ );
108 if ( $error !==
true ) {
109 $this->fatalError( $error );
114 if ( $this->hasOption(
'query' ) ) {
115 $query = $this->getOption(
'query' );
116 $res = $this->
sqlDoQuery( $db, $query,
true );
117 $this->waitForReplication();
118 if ( $this->hasOption(
'status' ) && !$res ) {
119 $this->fatalError(
'Failed.', 2 );
125 function_exists(
'readline_add_history' ) &&
126 Maintenance::posix_isatty( 0 )
128 $home = getenv(
'HOME' );
129 $historyFile = $home ?
130 "$home/.mwsql_history" :
"$IP/maintenance/.mwsql_history";
131 readline_read_history( $historyFile );
138 $prompt = $newPrompt;
139 $doDie = !Maintenance::posix_isatty( 0 );
143 while ( ( $line = Maintenance::readconsole( $prompt ) ) !==
false ) {
145 # User simply pressed return key
148 $done = $db->streamStatementEnd( $wholeLine, $line );
157 if ( $historyFile ) {
158 # Delimiter is eaten by streamStatementEnd, we add it
159 # up in the history (T39020)
160 readline_add_history( $wholeLine .
';' );
161 readline_write_history( $historyFile );
164 $res = $this->
sqlDoQuery( $db, $wholeLine, $doDie );
165 if ( $this->getBatchSize() && ++$batchCount >= $this->getBatchSize() ) {
167 $this->waitForReplication();
169 $prompt = $newPrompt;
172 $this->waitForReplication();
173 if ( $this->hasOption(
'status' ) && !$res ) {
174 $this->fatalError(
'Failed.', 2 );
208 } elseif ( is_object( $res ) ) {
211 foreach ( $res as $row ) {
212 $out .= print_r( $row,
true );
215 if ( $this->hasOption(
'json' ) ) {
216 $out = json_encode( $rows, JSON_PRETTY_PRINT );
217 } elseif ( !$rows ) {
218 $out =
'Query OK, 0 row(s) affected';
220 $this->output( $out .
"\n" );
221 return count( $rows );
223 $affected = $db->affectedRows();
224 if ( $this->hasOption(
'json' ) ) {
225 $this->output( json_encode( [
'affected' => $affected ], JSON_PRETTY_PRINT ) .
"\n" );
227 $this->output(
"Query OK, $affected row(s) affected\n" );