| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
use MediaWiki\Shell\Shell; |
| 27 |
use Wikimedia\FileBackend\FSFile\TempFSFile; |
| 28 |
use Wikimedia\IPUtils; |
| 29 |
use Wikimedia\Rdbms\ServerInfo; |
| 30 |
|
| 31 |
|
| 32 |
require_once __DIR__ . '/Maintenance.php'; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class MysqlMaintenance extends Maintenance { |
| 39 |
public function __construct() { |
| 40 |
parent::__construct(); |
| 41 |
$this->addDescription( "Execute the MySQL client binary. " . |
| 42 |
"Non-option arguments will be passed through to mysql." ); |
| 43 |
$this->addOption( 'write', 'Connect to the primary database', false, false ); |
| 44 |
$this->addOption( 'group', 'Specify query group', false, true ); |
| 45 |
$this->addOption( 'host', 'Connect to a known MySQL server', false, true ); |
| 46 |
$this->addOption( 'raw-host', |
| 47 |
'Connect directly to a specific MySQL server, even if not known to MediaWiki ' |
| 48 |
. 'via wgLBFactoryConf (e.g. parser cache or depooled host). ' |
| 49 |
. 'Credentails will be chosen based on --cluster and --wikidb.', |
| 50 |
false, |
| 51 |
true |
| 52 |
); |
| 53 |
$this->addOption( 'list-hosts', 'List the available DB hosts', false, false ); |
| 54 |
$this->addOption( 'cluster', 'Use an external cluster by name', false, true ); |
| 55 |
$this->addOption( 'wikidb', |
| 56 |
'The database wiki ID to use if not the current one', |
| 57 |
false, |
| 58 |
true |
| 59 |
); |
| 60 |
|
| 61 |
|
| 62 |
$this->addArg( '-- mysql_option ...', 'Options to pass to mysql', false ); |
| 63 |
} |
| 64 |
|
| 65 |
public function execute() { |
| 66 |
$dbName = $this->getOption( 'wikidb', false ); |
| 67 |
$lbf = $this->getServiceContainer()->getDBLoadBalancerFactory(); |
| 68 |
|
| 69 |
|
| 70 |
if ( $this->hasOption( 'cluster' ) ) { |
| 71 |
try { |
| 72 |
$lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) ); |
| 73 |
} catch ( InvalidArgumentException $e ) { |
| 74 |
$this->fatalError( 'Error: invalid cluster' ); |
| 75 |
} |
| 76 |
} else { |
| 77 |
$lb = $lbf->getMainLB( $dbName ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
if ( $this->hasOption( 'list-hosts' ) ) { |
| 82 |
$serverCount = $lb->getServerCount(); |
| 83 |
for ( $index = 0; $index < $serverCount; ++$index ) { |
| 84 |
echo $lb->getServerName( $index ) . "\n"; |
| 85 |
} |
| 86 |
return; |
| 87 |
} |
| 88 |
if ( $this->hasOption( 'host' ) ) { |
| 89 |
$host = $this->getOption( 'host' ); |
| 90 |
$serverCount = $lb->getServerCount(); |
| 91 |
for ( $index = 0; $index < $serverCount; ++$index ) { |
| 92 |
if ( $lb->getServerName( $index ) === $host ) { |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
if ( $index >= $serverCount ) { |
| 97 |
$this->fatalError( "Error: Host not configured: \"$host\"" ); |
| 98 |
} |
| 99 |
} elseif ( $this->hasOption( 'write' ) ) { |
| 100 |
$index = ServerInfo::WRITER_INDEX; |
| 101 |
} else { |
| 102 |
$group = $this->getOption( 'group', false ); |
| 103 |
$index = $lb->getReaderIndex( $group ); |
| 104 |
if ( $index === false && $group ) { |
| 105 |
|
| 106 |
$index = $lb->getReaderIndex( false ); |
| 107 |
} |
| 108 |
if ( $index === false ) { |
| 109 |
$this->fatalError( 'Error: unable to get reader index' ); |
| 110 |
} |
| 111 |
} |
| 112 |
if ( $lb->getServerType( $index ) !== 'mysql' ) { |
| 113 |
$this->fatalError( 'Error: this script only works with MySQL/MariaDB' ); |
| 114 |
} |
| 115 |
|
| 116 |
$serverInfo = $lb->getServerInfo( $index ); |
| 117 |
|
| 118 |
|
| 119 |
if ( $this->hasOption( 'raw-host' ) ) { |
| 120 |
$host = $this->getOption( 'raw-host' ); |
| 121 |
$serverInfo = [ 'host' => $host ] + $serverInfo; |
| 122 |
} |
| 123 |
|
| 124 |
$this->runMysql( $serverInfo, $dbName ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
private function runMysql( $info, $dbName ) { |
| 134 |
|
| 135 |
|
| 136 |
$tmpFile = TempFSFile::factory( 'mw-mysql', 'ini' ); |
| 137 |
chmod( $tmpFile->getPath(), 0600 ); |
| 138 |
file_put_contents( $tmpFile->getPath(), "[client]\npassword={$info['password']}\n" ); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
$desc = [ |
| 144 |
0 => STDIN, |
| 145 |
1 => STDOUT, |
| 146 |
2 => STDERR, |
| 147 |
]; |
| 148 |
|
| 149 |
|
| 150 |
$realServer = $info['host']; |
| 151 |
$hostAndPort = IPUtils::splitHostAndPort( $realServer ); |
| 152 |
$socket = false; |
| 153 |
$port = false; |
| 154 |
if ( $hostAndPort ) { |
| 155 |
$realServer = $hostAndPort[0]; |
| 156 |
if ( $hostAndPort[1] ) { |
| 157 |
$port = $hostAndPort[1]; |
| 158 |
} |
| 159 |
} elseif ( substr_count( $realServer, ':' ) == 1 ) { |
| 160 |
|
| 161 |
|
| 162 |
[ $realServer, $socket ] = explode( ':', $realServer, 2 ); |
| 163 |
} |
| 164 |
|
| 165 |
if ( $dbName === false ) { |
| 166 |
$dbName = $info['dbname']; |
| 167 |
} |
| 168 |
|
| 169 |
$args = [ |
| 170 |
'mysql', |
| 171 |
"--defaults-extra-file={$tmpFile->getPath()}", |
| 172 |
"--user={$info['user']}", |
| 173 |
"--database={$dbName}", |
| 174 |
]; |
| 175 |
if ( $socket !== false ) { |
| 176 |
$args[] = "--socket={$socket}"; |
| 177 |
} else { |
| 178 |
$args[] = "--host={$realServer}"; |
| 179 |
} |
| 180 |
if ( $port !== false ) { |
| 181 |
$args[] = "--port={$port}"; |
| 182 |
} |
| 183 |
|
| 184 |
$args = array_merge( $args, $this->getArgs() ); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
if ( function_exists( 'pcntl_signal' ) ) { |
| 189 |
pcntl_signal( SIGINT, SIG_IGN ); |
| 190 |
} |
| 191 |
|
| 192 |
$pipes = []; |
| 193 |
$proc = proc_open( Shell::escape( $args ), $desc, $pipes ); |
| 194 |
if ( $proc === false ) { |
| 195 |
$this->fatalError( 'Unable to execute mysql' ); |
| 196 |
} |
| 197 |
|
| 198 |
$ret = proc_close( $proc ); |
| 199 |
if ( $ret === -1 ) { |
| 200 |
$this->fatalError( 'proc_close() returned -1' ); |
| 201 |
} elseif ( $ret ) { |
| 202 |
$this->fatalError( 'Failed.', $ret ); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
$maintClass = MysqlMaintenance::class; |
| 209 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 210 |
|