26require_once __DIR__ .
'/Maintenance.php';
43 parent::__construct();
44 $this->
addDescription(
'Send SQL queries to a MediaWiki database. ' .
45 'Takes a file name containing SQL as argument or runs interactively.' );
47 'Run a single query instead of running interactively',
false,
true );
48 $this->
addOption(
'json',
'Output the results as JSON instead of PHP objects' );
49 $this->
addOption(
'status',
'Return successful exit status only if the query succeeded '
50 .
'(selected or altered rows), otherwise 1 for errors, 2 for no rows' );
51 $this->
addOption(
'cluster',
'Use an external cluster by name',
false,
true );
53 'The database wiki ID to use if not the current one',
false,
true );
55 'Replica DB server to use instead of the primary DB (can be "any")',
false,
true );
67 $lb = $lbFactory->getExternalLB( $this->
getOption(
'cluster' ) );
69 $lb = $lbFactory->getMainLB( $wiki );
72 $replicaDB = $this->
getOption(
'replicadb',
'' );
73 if ( $replicaDB ===
'any' ) {
75 } elseif ( $replicaDB !==
'' ) {
77 $serverCount = $lb->getServerCount();
78 for ( $i = 0; $i < $serverCount; ++$i ) {
79 if ( $lb->getServerName( $i ) === $replicaDB ) {
85 if ( $index ===
null || $index === ServerInfo::WRITER_INDEX ) {
86 $this->
fatalError(
"No replica DB server configured with the name '$replicaDB'." );
92 $db = $lb->getMaintenanceConnectionRef( $index, [], $wiki );
93 if ( $replicaDB !=
'' && $db->getLBInfo(
'master' ) !==
null ) {
94 $this->
fatalError(
"Server {$db->getServerName()} is not a replica DB." );
98 $updater = DatabaseUpdater::newForDB( $db,
true, $this );
99 $db->setSchemaVars( $updater->getSchemaVars() );
102 if ( $this->
hasArg( 0 ) ) {
103 $file = fopen( $this->
getArg( 0 ),
'r' );
105 $this->
fatalError(
"Unable to open input file" );
108 $error = $db->sourceStream( $file,
null, [ $this,
'sqlPrintResult' ], __METHOD__ );
109 if ( $error !==
true ) {
117 $res = $this->
sqlDoQuery( $db, $query,
true );
119 if ( $this->
hasOption(
'status' ) && !$res ) {
126 function_exists(
'readline_add_history' ) &&
127 Maintenance::posix_isatty( 0 )
129 $home = getenv(
'HOME' );
130 $historyFile = $home ?
131 "$home/.mwsql_history" :
"$IP/maintenance/.mwsql_history";
132 readline_read_history( $historyFile );
139 $prompt = $newPrompt;
140 $doDie = !Maintenance::posix_isatty( 0 );
144 while ( ( $line = Maintenance::readconsole( $prompt ) ) !==
false ) {
146 # User simply pressed return key
149 $done = $db->streamStatementEnd( $wholeLine, $line );
158 if ( $historyFile ) {
159 # Delimiter is eaten by streamStatementEnd, we add it
160 # up in the history (T39020)
161 readline_add_history( $wholeLine .
';' );
162 readline_write_history( $historyFile );
165 $res = $this->
sqlDoQuery( $db, $wholeLine, $doDie );
170 $prompt = $newPrompt;
174 if ( $this->
hasOption(
'status' ) && !$res ) {
187 $res = $db->
query( $line, __METHOD__ );
193 $this->
error( (
string)$e );
209 } elseif ( is_object( $res ) ) {
212 foreach ( $res as $row ) {
213 $out .= print_r( $row,
true );
217 $out = json_encode( $rows, JSON_PRETTY_PRINT );
218 } elseif ( !$rows ) {
219 $out =
'Query OK, 0 row(s) affected';
221 $this->
output( $out .
"\n" );
222 return count( $rows );
224 $affected = $db->affectedRows();
226 $this->
output( json_encode( [
'affected' => $affected ], JSON_PRETTY_PRINT ) .
"\n" );
228 $this->
output(
"Query OK, $affected row(s) affected\n" );
238 return Maintenance::DB_ADMIN;
244require_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...
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.
sqlPrintResult( $res, $db)
Print the results, callback for $db->sourceStream()
__construct()
Default constructor.
sqlDoQuery(IDatabase $db, $line, $dieOnError)
execute()
Do the actual work.