12require_once __DIR__ .
'/Maintenance.php';
29 parent::__construct();
30 $this->
addDescription(
'Send SQL queries to a MediaWiki database. ' .
31 'Takes a file name containing SQL as argument or runs interactively.' );
33 'Run a single query instead of running interactively',
false,
true );
34 $this->
addOption(
'json',
'Output the results as JSON instead of PHP objects' );
35 $this->
addOption(
'status',
'Return successful exit status only if the query succeeded '
36 .
'(selected or altered rows), otherwise 1 for errors, 2 for no rows' );
37 $this->
addOption(
'cluster',
'Use an external cluster by name',
false,
true );
39 'The database wiki ID to use if not the current one',
false,
true );
41 'Replica DB server to use instead of the primary DB (can be "any")',
false,
true );
51 $lb = $lbFactory->getExternalLB( $this->
getOption(
'cluster' ) );
53 $lb = $lbFactory->getMainLB( $wiki );
56 $replicaDB = $this->
getOption(
'replicadb',
'' );
57 if ( $replicaDB ===
'any' ) {
59 } elseif ( $replicaDB !==
'' ) {
61 $serverCount = $lb->getServerCount();
62 for ( $i = 0; $i < $serverCount; ++$i ) {
63 if ( $lb->getServerName( $i ) === $replicaDB ) {
68 if ( $index ===
null || $index === ServerInfo::WRITER_INDEX ) {
69 $this->
fatalError(
"No replica DB server configured with the name '$replicaDB'." );
75 $db = $lb->getMaintenanceConnectionRef( $index, [], $wiki );
76 if ( $replicaDB !=
'' && $db->getLBInfo(
'master' ) !==
null ) {
77 $this->
fatalError(
"Server {$db->getServerName()} is not a replica DB." );
81 $updater = DatabaseUpdater::newForDB( $db,
true, $this );
82 $db->setSchemaVars( $updater->getSchemaVars() );
85 if ( $this->
hasArg( 0 ) ) {
86 $file = fopen( $this->
getArg( 0 ),
'r' );
88 $this->
fatalError(
"Unable to open input file" );
91 $error = $db->sourceStream( $file,
null, $this->sqlPrintResult( ... ), __METHOD__ );
92 if ( $error !==
true ) {
100 $res = $this->
sqlDoQuery( $db, $query,
true );
102 if ( $this->
hasOption(
'status' ) && !$res ) {
109 function_exists(
'readline_add_history' ) &&
110 Maintenance::posix_isatty( 0 )
112 $home = getenv(
'HOME' );
114 ?
"$home/.mwsql_history"
115 : MW_INSTALL_PATH .
'/maintenance/.mwsql_history';
116 readline_read_history( $historyFile );
123 $prompt = $newPrompt;
124 $doDie = !Maintenance::posix_isatty( 0 );
128 while ( ( $line = Maintenance::readconsole( $prompt ) ) !==
false ) {
130 # User simply pressed return key
133 $done = $db->streamStatementEnd( $wholeLine, $line );
142 if ( $historyFile ) {
143 # Delimiter is eaten by streamStatementEnd, we add it
144 # up in the history (T39020)
145 readline_add_history( $wholeLine .
';' );
146 readline_write_history( $historyFile );
149 $res = $this->
sqlDoQuery( $db, $wholeLine, $doDie );
154 $prompt = $newPrompt;
158 if ( $this->
hasOption(
'status' ) && !$res ) {
171 $res = $db->
query( $line, __METHOD__ );
172 return $this->sqlPrintResult( $res, $db );
177 $this->
error( (
string)$e );
189 private function sqlPrintResult( $res, $db ) {
193 } elseif ( is_object( $res ) ) {
196 foreach ( $res as $row ) {
197 $out .= print_r( $row,
true );
201 $out = json_encode( $rows, JSON_PRETTY_PRINT );
202 } elseif ( !$rows ) {
203 $out =
'Query OK, 0 row(s) affected';
205 $this->
output( $out .
"\n" );
206 return count( $rows );
208 $affected = $db->affectedRows();
210 $this->
output( json_encode( [
'affected' => $affected ], JSON_PRETTY_PRINT ) .
"\n" );
212 $this->
output(
"Query OK, $affected row(s) affected\n" );
222 return Maintenance::DB_ADMIN;
228require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getArg( $argId=0, $default=null)
Get an argument.
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
hasArg( $argId=0)
Does a given argument exist?
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Maintenance script that sends SQL queries from the specified file to the database.
__construct()
Default constructor.
sqlDoQuery(IDatabase $db, $line, $dieOnError)
execute()
Do the actual work.