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(
'cluster',
'Use an external cluster by name',
false,
true );
47 'The database wiki ID to use if not the current one',
false,
true );
49 'Replica DB server to use instead of the master DB (can be "any")',
false,
true );
58 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
60 $lb = $lbFactory->getExternalLB( $this->
getOption(
'cluster' ) );
62 $lb = $lbFactory->getMainLB( $wiki );
66 if ( $replicaDB ===
'any' ) {
68 } elseif ( $replicaDB !=
'' ) {
70 $serverCount = $lb->getServerCount();
71 for ( $i = 0; $i < $serverCount; ++$i ) {
72 if ( $lb->getServerName( $i ) === $replicaDB ) {
77 if ( $index ===
null ) {
78 $this->
fatalError(
"No replica DB server configured with the name '$replicaDB'." );
85 $db = $lb->getConnection( $index, [], $wiki );
86 if ( $replicaDB !=
'' && $db->getLBInfo(
'master' ) !==
null ) {
87 $this->
fatalError(
"The server selected ({$db->getServer()}) is not a replica DB." );
92 $db->setSchemaVars( $updater->getSchemaVars() );
95 if ( $this->
hasArg( 0 ) ) {
96 $file = fopen( $this->
getArg( 0 ),
'r' );
98 $this->
fatalError(
"Unable to open input file" );
101 $error = $db->sourceStream( $file,
null, [ $this,
'sqlPrintResult' ] );
102 if ( $error !==
true ) {
117 function_exists(
'readline_add_history' ) &&
120 $historyFile = isset( $_ENV[
'HOME'] ) ?
121 "{$_ENV['HOME']}/.mwsql_history" :
"$IP/maintenance/.mwsql_history";
122 readline_read_history( $historyFile );
129 $prompt = $newPrompt;
133 # User simply pressed return key
136 $done = $db->streamStatementEnd( $wholeLine,
$line );
145 if ( $historyFile ) {
146 # Delimiter is eated by streamStatementEnd, we add it
147 # up in the history (T39020)
148 readline_add_history( $wholeLine .
';' );
149 readline_write_history( $historyFile );
152 $prompt = $newPrompt;
180 } elseif ( is_object(
$res ) &&
$res->numRows() ) {
182 foreach (
$res as $row ) {
183 $out .= print_r( $row,
true );
187 $out = json_encode(
$rows, JSON_PRETTY_PRINT );
191 $affected = $db->affectedRows();
192 $this->
output(
"Query OK, $affected row(s) affected\n" );
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
static newForDB(Database $db, $shared=false, Maintenance $maintenance=null)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
hasArg( $argId=0)
Does a given argument exist?
hasOption( $name)
Checks to see if a particular param exists.
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.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
returning false will NOT prevent logging $e
require_once RUN_MAINTENANCE_IF_MAIN