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 master 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(
"The server selected ({$db->getServer()}) 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 ) {
113 $lbFactory->waitForReplication();
115 exit(
$res ? 0 : 2 );
121 function_exists(
'readline_add_history' ) &&
124 $historyFile = isset( $_ENV[
'HOME'] ) ?
125 "{$_ENV['HOME']}/.mwsql_history" :
"$IP/maintenance/.mwsql_history";
126 readline_read_history( $historyFile );
133 $prompt = $newPrompt;
138 # User simply pressed return key
141 $done = $db->streamStatementEnd( $wholeLine,
$line );
150 if ( $historyFile ) {
151 # Delimiter is eated by streamStatementEnd, we add it
152 # up in the history (T39020)
153 readline_add_history( $wholeLine .
';' );
154 readline_write_history( $historyFile );
157 $prompt = $newPrompt;
160 $lbFactory->waitForReplication();
162 exit(
$res ? 0 : 2 );
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" );
const 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.