48 $wiki = $this->hasOption(
'wikidb' ) ? $this->getOption(
'wikidb' ) :
false;
50 $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
51 if ( $this->hasOption(
'cluster' ) ) {
52 $lb = $lbFactory->getExternalLB( $this->getOption(
'cluster' ) );
54 $lb = $lbFactory->getMainLB( $wiki );
57 $replicaDB = $this->getOption(
'replicadb',
'' );
58 if ( $replicaDB ===
'any' ) {
60 } elseif ( $replicaDB !==
'' ) {
62 $serverCount = $lb->getServerCount();
63 for ( $i = 0; $i < $serverCount; ++$i ) {
64 if ( $lb->getServerName( $i ) === $replicaDB ) {
70 $this->fatalError(
"No replica DB server configured with the name '$replicaDB'." );
76 $db = $lb->getMaintenanceConnectionRef( $index, [], $wiki );
77 if ( $replicaDB !=
'' && $db->getLBInfo(
'master' ) !==
null ) {
78 $this->fatalError(
"Server {$db->getServerName()} is not a replica DB." );
83 $db->setSchemaVars( $updater->getSchemaVars() );
86 if ( $this->hasArg( 0 ) ) {
87 $fileName = $this->getArg( 0 );
88 if ( !is_readable( $fileName ) ) {
89 $this->fatalError(
"Unable to open input file: $fileName" );
91 $file = fopen( $fileName,
'r' );
93 $this->fatalError(
"Unable to open input file: $fileName" );
96 $error = $db->sourceStream( $file,
null, $this->sqlPrintResult( ... ), __METHOD__ );
97 if ( $error !==
true ) {
98 $this->fatalError( $error );
103 if ( $this->hasOption(
'query' ) ) {
104 $query = $this->getOption(
'query' );
105 $res = $this->
sqlDoQuery( $db, $query,
true );
106 $this->waitForReplication();
107 if ( $this->hasOption(
'status' ) && !$res ) {
108 $this->fatalError(
'Failed.', 2 );
114 function_exists(
'readline_add_history' ) &&
115 Maintenance::posix_isatty( 0 )
117 $home = getenv(
'HOME' );
119 ?
"$home/.mwsql_history"
120 : MW_INSTALL_PATH .
'/maintenance/.mwsql_history';
121 readline_read_history( $historyFile );
128 $prompt = $newPrompt;
129 $doDie = !Maintenance::posix_isatty( 0 );
133 while ( ( $line = Maintenance::readconsole( $prompt ) ) !==
false ) {
135 # User simply pressed return key
138 $done = $db->streamStatementEnd( $wholeLine, $line );
147 if ( $historyFile ) {
148 # Delimiter is eaten by streamStatementEnd, we add it
149 # up in the history (T39020)
150 readline_add_history( $wholeLine .
';' );
151 readline_write_history( $historyFile );
154 $res = $this->
sqlDoQuery( $db, $wholeLine, $doDie );
155 if ( $this->getBatchSize() && ++$batchCount >= $this->getBatchSize() ) {
157 $this->waitForReplication();
159 $prompt = $newPrompt;
162 $this->waitForReplication();
163 if ( $this->hasOption(
'status' ) && !$res ) {
164 $this->fatalError(
'Failed.', 2 );
215 $this->output( json_encode( [
'affected' => $affected ], JSON_PRETTY_PRINT ) .
"\n" );