33require_once __DIR__ .
'/Maintenance.php';
41 parent::__construct();
43 "Non-option arguments will be passed through to mysql." );
44 $this->
addOption(
'write',
'Connect to the primary database',
false,
false );
45 $this->
addOption(
'group',
'Specify query group',
false,
true );
46 $this->
addOption(
'host',
'Connect to a known MySQL server',
false,
true );
48 'Connect directly to a specific MySQL server, even if not known to MediaWiki '
49 .
'via wgLBFactoryConf (e.g. parser cache or depooled host). '
50 .
'Credentails will be chosen based on --cluster and --wikidb.',
54 $this->
addOption(
'list-hosts',
'List the available DB hosts',
false,
false );
55 $this->
addOption(
'cluster',
'Use an external cluster by name',
false,
true );
57 'The database wiki ID to use if not the current one',
63 $this->
addArg(
'-- mysql_option ...',
'Options to pass to mysql',
false );
67 $dbName = $this->
getOption(
'wikidb',
false );
73 $lb = $lbf->getExternalLB( $this->
getOption(
'cluster' ) );
74 }
catch ( InvalidArgumentException $e ) {
78 $lb = $lbf->getMainLB( $dbName );
83 $serverCount = $lb->getServerCount();
84 for ( $index = 0; $index < $serverCount; ++$index ) {
85 echo $lb->getServerName( $index ) .
"\n";
91 $serverCount = $lb->getServerCount();
92 for ( $index = 0; $index < $serverCount; ++$index ) {
93 if ( $lb->getServerName( $index ) === $host ) {
97 if ( $index >= $serverCount ) {
98 $this->
fatalError(
"Error: Host not configured: \"$host\"" );
100 } elseif ( $this->
hasOption(
'write' ) ) {
101 $index = ServerInfo::WRITER_INDEX;
103 $group = $this->
getOption(
'group',
false );
104 $index = $lb->getReaderIndex( $group );
105 if ( $index ===
false && $group ) {
107 $index = $lb->getReaderIndex(
false );
109 if ( $index ===
false ) {
110 $this->
fatalError(
'Error: unable to get reader index' );
113 if ( $lb->getServerType( $index ) !==
'mysql' ) {
114 $this->
fatalError(
'Error: this script only works with MySQL/MariaDB' );
117 $serverInfo = $lb->getServerInfo( $index );
122 $serverInfo = [
'host' => $host ] + $serverInfo;
125 $this->runMysql( $serverInfo, $dbName );
134 private function runMysql( $info, $dbName ) {
137 $tmpFile = TempFSFile::factory(
'mw-mysql',
'ini' );
138 chmod( $tmpFile->getPath(), 0600 );
139 file_put_contents( $tmpFile->getPath(),
"[client]\npassword={$info['password']}\n" );
151 $realServer = $info[
'host'];
152 $hostAndPort = IPUtils::splitHostAndPort( $realServer );
155 if ( $hostAndPort ) {
156 $realServer = $hostAndPort[0];
157 if ( $hostAndPort[1] ) {
158 $port = $hostAndPort[1];
160 } elseif ( substr_count( $realServer,
':' ) == 1 ) {
163 [ $realServer, $socket ] = explode(
':', $realServer, 2 );
166 if ( $dbName ===
false ) {
167 $dbName = $info[
'dbname'];
172 "--defaults-extra-file={$tmpFile->getPath()}",
173 "--user={$info['user']}",
174 "--database={$dbName}",
176 if ( $socket !==
false ) {
177 $args[] =
"--socket={$socket}";
179 $args[] =
"--host={$realServer}";
181 if ( $port !==
false ) {
182 $args[] =
"--port={$port}";
185 $args = array_merge( $args, $this->
getArgs() );
189 if ( function_exists(
'pcntl_signal' ) ) {
190 pcntl_signal( SIGINT, SIG_IGN );
194 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
195 if ( $proc ===
false ) {
196 $this->
fatalError(
'Unable to execute mysql' );
199 $ret = proc_close( $proc );
201 $this->
fatalError(
'proc_close() returned -1' );
210require_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.