19require_once __DIR__ .
'/Maintenance.php';
27 parent::__construct();
29 "Non-option arguments will be passed through to mysql." );
30 $this->
addOption(
'write',
'Connect to the primary database',
false,
false );
31 $this->
addOption(
'group',
'Specify query group',
false,
true );
32 $this->
addOption(
'host',
'Connect to a known MySQL server',
false,
true );
34 'Connect directly to a specific MySQL server, even if not known to MediaWiki '
35 .
'via wgLBFactoryConf (e.g. parser cache or depooled host). '
36 .
'Credentials will be chosen based on --cluster and --wikidb.',
40 $this->
addOption(
'list-hosts',
'List the available DB hosts',
false,
false );
41 $this->
addOption(
'cluster',
'Use an external cluster by name',
false,
true );
43 'The database wiki ID to use if not the current one',
49 $this->
addArg(
'-- mysql_option ...',
'Options to pass to mysql',
false );
53 $dbName = $this->
getOption(
'wikidb',
false );
59 $lb = $lbf->getExternalLB( $this->
getOption(
'cluster' ) );
60 }
catch ( InvalidArgumentException ) {
64 $lb = $lbf->getMainLB( $dbName );
69 $serverCount = $lb->getServerCount();
70 for ( $index = 0; $index < $serverCount; ++$index ) {
71 echo $lb->getServerName( $index ) .
"\n";
77 $serverCount = $lb->getServerCount();
78 for ( $index = 0; $index < $serverCount; ++$index ) {
79 if ( $lb->getServerName( $index ) === $host ) {
83 if ( $index >= $serverCount ) {
84 $this->
fatalError(
"Error: Host not configured: \"$host\"" );
86 } elseif ( $this->
hasOption(
'write' ) ) {
87 $index = ServerInfo::WRITER_INDEX;
89 $group = $this->
getOption(
'group',
false );
90 $index = $lb->getReaderIndex( $group );
91 if ( $index ===
false && $group ) {
93 $index = $lb->getReaderIndex(
false );
95 if ( $index ===
false ) {
96 $this->
fatalError(
'Error: unable to get reader index' );
99 if ( $lb->getServerType( $index ) !==
'mysql' ) {
100 $this->
fatalError(
'Error: this script only works with MySQL/MariaDB' );
103 $serverInfo = $lb->getServerInfo( $index );
108 $serverInfo = [
'host' => $host ] + $serverInfo;
111 $this->runMysql( $serverInfo, $dbName );
120 private function runMysql( $info, $dbName ) {
123 $tmpFile = TempFSFile::factory(
'mw-mysql',
'ini' );
124 chmod( $tmpFile->getPath(), 0600 );
125 file_put_contents( $tmpFile->getPath(),
"[client]\npassword={$info['password']}\n" );
137 $realServer = $info[
'host'];
138 $hostAndPort = IPUtils::splitHostAndPort( $realServer );
141 if ( $hostAndPort ) {
142 $realServer = $hostAndPort[0];
143 if ( $hostAndPort[1] ) {
144 $port = $hostAndPort[1];
146 } elseif ( substr_count( $realServer,
':' ) == 1 ) {
149 [ $realServer, $socket ] = explode(
':', $realServer, 2 );
152 if ( $dbName ===
false ) {
153 $dbName = $info[
'dbname'];
158 "--defaults-extra-file={$tmpFile->getPath()}",
159 "--user={$info['user']}",
160 "--database={$dbName}",
162 if ( $socket !==
false ) {
163 $args[] =
"--socket={$socket}";
165 $args[] =
"--host={$realServer}";
167 if ( $port !==
false ) {
168 $args[] =
"--port={$port}";
175 if ( function_exists(
'pcntl_signal' ) ) {
176 pcntl_signal( SIGINT, SIG_IGN );
180 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
181 if ( $proc ===
false ) {
182 $this->
fatalError(
'Unable to execute mysql' );
185 $ret = proc_close( $proc );
187 $this->
fatalError(
'proc_close() returned -1' );
196require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArgs( $offset=0)
Get arguments.
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.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
__construct()
Default constructor.
execute()
Do the actual work.