25require_once __DIR__ .
'/Maintenance.php';
39 parent::__construct();
40 $this->
addDescription(
'Send SQL queries to a MediaWiki database. ' .
41 'Takes a file name containing SQL as argument or runs interactively.' );
43 'Run a single query instead of running interactively',
false,
true );
44 $this->
addOption(
'json',
'Output the results as JSON instead of PHP objects' );
45 $this->
addOption(
'status',
'Return successful exit status only if the query succeeded '
46 .
'(selected or altered rows), otherwise 1 for errors, 2 for no rows' );
47 $this->
addOption(
'cluster',
'Use an external cluster by name',
false,
true );
49 'The database wiki ID to use if not the current one',
false,
true );
51 'Replica DB server to use instead of the primary DB (can be "any")',
false,
true );
60 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
62 $lb = $lbFactory->getExternalLB( $this->
getOption(
'cluster' ) );
64 $lb = $lbFactory->getMainLB( $wiki );
67 $replicaDB = $this->
getOption(
'replicadb',
'' );
68 if ( $replicaDB ===
'any' ) {
70 } elseif ( $replicaDB !==
'' ) {
72 $serverCount = $lb->getServerCount();
73 for ( $i = 0; $i < $serverCount; ++$i ) {
74 if ( $lb->getServerName( $i ) === $replicaDB ) {
79 if ( $index ===
null || $index === $lb->getWriterIndex() ) {
80 $this->
fatalError(
"No replica DB server configured with the name '$replicaDB'." );
86 $db = $lb->getMaintenanceConnectionRef( $index, [], $wiki );
87 if ( $replicaDB !=
'' && $db->getLBInfo(
'master' ) !==
null ) {
88 $this->
fatalError(
"Server {$db->getServerName()} is not a replica DB." );
92 $updater = DatabaseUpdater::newForDB( $db,
true, $this );
93 $db->setSchemaVars( $updater->getSchemaVars() );
96 if ( $this->
hasArg( 0 ) ) {
99 $this->
fatalError(
"Unable to open input file" );
102 $error = $db->sourceStream(
$file,
null, [ $this,
'sqlPrintResult' ], __METHOD__ );
103 if ( $error !==
true ) {
112 $lbFactory->waitForReplication();
120 function_exists(
'readline_add_history' ) &&
123 $historyFile = isset( $_ENV[
'HOME'] ) ?
124 "{$_ENV['HOME']}/.mwsql_history" :
"$IP/maintenance/.mwsql_history";
125 readline_read_history( $historyFile );
132 $prompt = $newPrompt;
137 # User simply pressed return key
140 $done = $db->streamStatementEnd( $wholeLine,
$line );
149 if ( $historyFile ) {
150 # Delimiter is eated by streamStatementEnd, we add it
151 # up in the history (T39020)
152 readline_add_history( $wholeLine .
';' );
153 readline_write_history( $historyFile );
157 $prompt = $newPrompt;
160 $lbFactory->waitForReplication();
180 $this->
error( (
string)$e );
196 } elseif ( is_object(
$res ) ) {
199 foreach (
$res as $row ) {
200 $out .= print_r( $row,
true );
204 $out = json_encode( $rows, JSON_PRETTY_PRINT );
205 } elseif ( !$rows ) {
206 $out =
'Query OK, 0 row(s) affected';
208 $this->
output( $out .
"\n" );
209 return count( $rows );
211 $affected = $db->affectedRows();
213 $this->
output( json_encode( [
'affected' => $affected ], JSON_PRETTY_PRINT ) .
"\n" );
215 $this->
output(
"Query OK, $affected row(s) affected\n" );
230require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasArg( $argId=0)
Does a given argument exist?
hasOption( $name)
Checks to see if a particular option was set.
static readconsole( $prompt='> ')
Prompt the console for input.
static posix_isatty( $fd)
Wrapper for posix_isatty() We default as considering stdin a tty (for nice readline methods) but trea...
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that sends SQL queries from the specified file to the database.
sqlPrintResult( $res, $db)
Print the results, callback for $db->sourceStream()
__construct()
Default constructor.
sqlDoQuery(IDatabase $db, $line, $dieOnError)
execute()
Do the actual work.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.