36 parent::__construct();
38 "Non-option arguments will be passed through to mysql." );
39 $this->
addOption(
'write',
'Connect to the primary database',
false,
false );
40 $this->
addOption(
'group',
'Specify query group',
false,
true );
41 $this->
addOption(
'host',
'Connect to a known MySQL server',
false,
true );
43 'Connect directly to a specific MySQL server, even if not known to MediaWiki '
44 .
'via wgLBFactoryConf (e.g. parser cache or depooled host). '
45 .
'Credentails will be chosen based on --cluster and --wikidb.',
49 $this->
addOption(
'list-hosts',
'List the available DB hosts',
false,
false );
50 $this->
addOption(
'cluster',
'Use an external cluster by name',
false,
true );
52 'The database wiki ID to use if not the current one',
58 $this->
addArg(
'-- mysql_option ...',
'Options to pass to mysql',
false );
62 $dbName = $this->
getOption(
'wikidb',
false );
68 $lb = $lbf->getExternalLB( $this->
getOption(
'cluster' ) );
69 }
catch ( InvalidArgumentException $e ) {
73 $lb = $lbf->getMainLB( $dbName );
78 $serverCount = $lb->getServerCount();
79 for ( $index = 0; $index < $serverCount; ++$index ) {
80 echo $lb->getServerName( $index ) .
"\n";
86 $serverCount = $lb->getServerCount();
87 for ( $index = 0; $index < $serverCount; ++$index ) {
88 if ( $lb->getServerName( $index ) === $host ) {
92 if ( $index >= $serverCount ) {
93 $this->
fatalError(
"Error: Host not configured: \"$host\"" );
95 } elseif ( $this->
hasOption(
'write' ) ) {
96 $index = $lb->getWriterIndex();
98 $group = $this->
getOption(
'group',
false );
99 $index = $lb->getReaderIndex( $group );
100 if ( $index ===
false && $group ) {
102 $index = $lb->getReaderIndex(
false );
104 if ( $index ===
false ) {
105 $this->
fatalError(
'Error: unable to get reader index' );
108 if ( $lb->getServerType( $index ) !==
'mysql' ) {
109 $this->
fatalError(
'Error: this script only works with MySQL/MariaDB' );
112 $serverInfo = $lb->getServerInfo( $index );
117 $serverInfo = [
'host' => $host ] + $serverInfo;
120 $this->runMysql( $serverInfo, $dbName );
129 private function runMysql( $info, $dbName ) {
132 $tmpFile = TempFSFile::factory(
'mw-mysql',
'ini' );
133 chmod( $tmpFile->getPath(), 0600 );
134 file_put_contents( $tmpFile->getPath(),
"[client]\npassword={$info['password']}\n" );
146 $realServer = $info[
'host'];
147 $hostAndPort = IPUtils::splitHostAndPort( $realServer );
150 if ( $hostAndPort ) {
151 $realServer = $hostAndPort[0];
152 if ( $hostAndPort[1] ) {
153 $port = $hostAndPort[1];
155 } elseif ( substr_count( $realServer,
':' ) == 1 ) {
158 [ $realServer, $socket ] = explode(
':', $realServer, 2 );
161 if ( $dbName ===
false ) {
162 $dbName = $info[
'dbname'];
167 "--defaults-extra-file={$tmpFile->getPath()}",
168 "--user={$info['user']}",
169 "--database={$dbName}",
171 if ( $socket !==
false ) {
172 $args[] =
"--socket={$socket}";
174 $args[] =
"--host={$realServer}";
176 if ( $port !==
false ) {
177 $args[] =
"--port={$port}";
180 $args = array_merge( $args, $this->
getArgs() );
184 if ( function_exists(
'pcntl_signal' ) ) {
185 pcntl_signal( SIGINT, SIG_IGN );
189 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
190 if ( $proc ===
false ) {
191 $this->
fatalError(
'Unable to execute mysql' );
194 $ret = proc_close( $proc );
196 $this->
fatalError(
'proc_close() returned -1' );
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.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
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.