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 );
63 $lb = $lbFactory->getExternalLB( $this->
getOption(
'cluster' ) );
65 $lb = $lbFactory->getMainLB( $wiki );
68 $replicaDB = $this->
getOption(
'replicadb',
'' );
69 if ( $replicaDB ===
'any' ) {
71 } elseif ( $replicaDB !==
'' ) {
73 $serverCount = $lb->getServerCount();
74 for ( $i = 0; $i < $serverCount; ++$i ) {
75 if ( $lb->getServerName( $i ) === $replicaDB ) {
80 if ( $index ===
null || $index === $lb->getWriterIndex() ) {
81 $this->
fatalError(
"No replica DB server configured with the name '$replicaDB'." );
87 $db = $lb->getMaintenanceConnectionRef( $index, [], $wiki );
88 if ( $replicaDB !=
'' && $db->getLBInfo(
'master' ) !==
null ) {
89 $this->
fatalError(
"Server {$db->getServerName()} is not a replica DB." );
93 $updater = DatabaseUpdater::newForDB( $db,
true, $this );
94 $db->setSchemaVars( $updater->getSchemaVars() );
97 if ( $this->
hasArg( 0 ) ) {
98 $file = fopen( $this->
getArg( 0 ),
'r' );
100 $this->
fatalError(
"Unable to open input file" );
103 $error = $db->sourceStream( $file,
null, [ $this,
'sqlPrintResult' ], __METHOD__ );
104 if ( $error !==
true ) {
112 $res = $this->
sqlDoQuery( $db, $query,
true );
114 if ( $this->
hasOption(
'status' ) && !$res ) {
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;
139 # User simply pressed return key
142 $done = $db->streamStatementEnd( $wholeLine, $line );
151 if ( $historyFile ) {
152 # Delimiter is eaten by streamStatementEnd, we add it
153 # up in the history (T39020)
154 readline_add_history( $wholeLine .
';' );
155 readline_write_history( $historyFile );
158 $res = $this->
sqlDoQuery( $db, $wholeLine, $doDie );
163 $prompt = $newPrompt;
167 if ( $this->
hasOption(
'status' ) && !$res ) {
180 $res = $db->
query( $line, __METHOD__ );
186 $this->
error( (
string)$e );
202 } elseif ( is_object( $res ) ) {
205 foreach ( $res as $row ) {
206 $out .= print_r( $row,
true );
210 $out = json_encode( $rows, JSON_PRETTY_PRINT );
211 } elseif ( !$rows ) {
212 $out =
'Query OK, 0 row(s) affected';
214 $this->
output( $out .
"\n" );
215 return count( $rows );
217 $affected = $db->affectedRows();
219 $this->
output( json_encode( [
'affected' => $affected ], JSON_PRETTY_PRINT ) .
"\n" );
221 $this->
output(
"Query OK, $affected row(s) affected\n" );
236require_once RUN_MAINTENANCE_IF_MAIN;
if(!defined( 'MEDIAWIKI')) if(ini_get('mbstring.func_overload')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
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?
waitForReplication()
Wait for replica DBs to catch up.
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...
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
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.