Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 99 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| MysqlMaintenance | |
0.00% |
0 / 99 |
|
0.00% |
0 / 3 |
756 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
240 | |||
| runMysql | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
132 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Execute the MySQL client binary, connecting to the wiki's DB. |
| 4 | * Note that this will not do table prefixing or variable substitution. |
| 5 | * To safely run schema patches, use sql.php. |
| 6 | * |
| 7 | * @license GPL-2.0-or-later |
| 8 | * @file |
| 9 | * @ingroup Maintenance |
| 10 | */ |
| 11 | |
| 12 | use MediaWiki\Maintenance\Maintenance; |
| 13 | use MediaWiki\Shell\Shell; |
| 14 | use Wikimedia\FileBackend\FSFile\TempFSFile; |
| 15 | use Wikimedia\IPUtils; |
| 16 | use Wikimedia\Rdbms\ServerInfo; |
| 17 | |
| 18 | // @codeCoverageIgnoreStart |
| 19 | require_once __DIR__ . '/Maintenance.php'; |
| 20 | // @codeCoverageIgnoreEnd |
| 21 | |
| 22 | /** |
| 23 | * @ingroup Maintenance |
| 24 | */ |
| 25 | class MysqlMaintenance extends Maintenance { |
| 26 | public function __construct() { |
| 27 | parent::__construct(); |
| 28 | $this->addDescription( "Execute the MySQL client binary. " . |
| 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 ); |
| 33 | $this->addOption( 'raw-host', |
| 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.', |
| 37 | false, |
| 38 | true |
| 39 | ); |
| 40 | $this->addOption( 'list-hosts', 'List the available DB hosts', false, false ); |
| 41 | $this->addOption( 'cluster', 'Use an external cluster by name', false, true ); |
| 42 | $this->addOption( 'wikidb', |
| 43 | 'The database wiki ID to use if not the current one', |
| 44 | false, |
| 45 | true |
| 46 | ); |
| 47 | |
| 48 | // Fake argument for help message |
| 49 | $this->addArg( '-- mysql_option ...', 'Options to pass to mysql', false ); |
| 50 | } |
| 51 | |
| 52 | public function execute() { |
| 53 | $dbName = $this->getOption( 'wikidb', false ); |
| 54 | $lbf = $this->getServiceContainer()->getDBLoadBalancerFactory(); |
| 55 | |
| 56 | // Pick LB |
| 57 | if ( $this->hasOption( 'cluster' ) ) { |
| 58 | try { |
| 59 | $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) ); |
| 60 | } catch ( InvalidArgumentException ) { |
| 61 | $this->fatalError( 'Error: invalid cluster' ); |
| 62 | } |
| 63 | } else { |
| 64 | $lb = $lbf->getMainLB( $dbName ); |
| 65 | } |
| 66 | |
| 67 | // List hosts, or pick host |
| 68 | if ( $this->hasOption( 'list-hosts' ) ) { |
| 69 | $serverCount = $lb->getServerCount(); |
| 70 | for ( $index = 0; $index < $serverCount; ++$index ) { |
| 71 | echo $lb->getServerName( $index ) . "\n"; |
| 72 | } |
| 73 | return; |
| 74 | } |
| 75 | if ( $this->hasOption( 'host' ) ) { |
| 76 | $host = $this->getOption( 'host' ); |
| 77 | $serverCount = $lb->getServerCount(); |
| 78 | for ( $index = 0; $index < $serverCount; ++$index ) { |
| 79 | if ( $lb->getServerName( $index ) === $host ) { |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | if ( $index >= $serverCount ) { |
| 84 | $this->fatalError( "Error: Host not configured: \"$host\"" ); |
| 85 | } |
| 86 | } elseif ( $this->hasOption( 'write' ) ) { |
| 87 | $index = ServerInfo::WRITER_INDEX; |
| 88 | } else { |
| 89 | $group = $this->getOption( 'group', false ); |
| 90 | $index = $lb->getReaderIndex( $group ); |
| 91 | if ( $index === false && $group ) { |
| 92 | // retry without the group; it may not exist |
| 93 | $index = $lb->getReaderIndex( false ); |
| 94 | } |
| 95 | if ( $index === false ) { |
| 96 | $this->fatalError( 'Error: unable to get reader index' ); |
| 97 | } |
| 98 | } |
| 99 | if ( $lb->getServerType( $index ) !== 'mysql' ) { |
| 100 | $this->fatalError( 'Error: this script only works with MySQL/MariaDB' ); |
| 101 | } |
| 102 | |
| 103 | $serverInfo = $lb->getServerInfo( $index ); |
| 104 | // Override host. This uses the server info of the host determined |
| 105 | // by the other options for the purposes of user/password. |
| 106 | if ( $this->hasOption( 'raw-host' ) ) { |
| 107 | $host = $this->getOption( 'raw-host' ); |
| 108 | $serverInfo = [ 'host' => $host ] + $serverInfo; |
| 109 | } |
| 110 | |
| 111 | $this->runMysql( $serverInfo, $dbName ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Run the mysql client for the given server info |
| 116 | * |
| 117 | * @param array $info |
| 118 | * @param string|false $dbName The DB name, or false to use the main wiki DB |
| 119 | */ |
| 120 | private function runMysql( $info, $dbName ) { |
| 121 | // Write the password to an option file to avoid disclosing it to other |
| 122 | // processes running on the system |
| 123 | $tmpFile = TempFSFile::factory( 'mw-mysql', 'ini' ); |
| 124 | chmod( $tmpFile->getPath(), 0600 ); |
| 125 | file_put_contents( $tmpFile->getPath(), "[client]\npassword={$info['password']}\n" ); |
| 126 | |
| 127 | // stdin/stdout need to be the actual file descriptors rather than |
| 128 | // PHP's pipe wrappers so that mysql can use them as an interactive |
| 129 | // terminal. |
| 130 | $desc = [ |
| 131 | 0 => STDIN, |
| 132 | 1 => STDOUT, |
| 133 | 2 => STDERR, |
| 134 | ]; |
| 135 | |
| 136 | // Split host and port as in DatabaseMySQL::mysqlConnect() |
| 137 | $realServer = $info['host']; |
| 138 | $hostAndPort = IPUtils::splitHostAndPort( $realServer ); |
| 139 | $socket = false; |
| 140 | $port = false; |
| 141 | if ( $hostAndPort ) { |
| 142 | $realServer = $hostAndPort[0]; |
| 143 | if ( $hostAndPort[1] ) { |
| 144 | $port = $hostAndPort[1]; |
| 145 | } |
| 146 | } elseif ( substr_count( $realServer, ':' ) == 1 ) { |
| 147 | // If we have a colon and something that's not a port number |
| 148 | // inside the hostname, assume it's the socket location |
| 149 | [ $realServer, $socket ] = explode( ':', $realServer, 2 ); |
| 150 | } |
| 151 | |
| 152 | if ( $dbName === false ) { |
| 153 | $dbName = $info['dbname']; |
| 154 | } |
| 155 | |
| 156 | $args = [ |
| 157 | 'mysql', |
| 158 | "--defaults-extra-file={$tmpFile->getPath()}", |
| 159 | "--user={$info['user']}", |
| 160 | "--database={$dbName}", |
| 161 | ]; |
| 162 | if ( $socket !== false ) { |
| 163 | $args[] = "--socket={$socket}"; |
| 164 | } else { |
| 165 | $args[] = "--host={$realServer}"; |
| 166 | } |
| 167 | if ( $port !== false ) { |
| 168 | $args[] = "--port={$port}"; |
| 169 | } |
| 170 | |
| 171 | $args = array_merge( $args, $this->getArgs() ); |
| 172 | |
| 173 | // Ignore SIGINT if possible, otherwise the wrapper terminates when the user presses |
| 174 | // ctrl-C to kill a query. |
| 175 | if ( function_exists( 'pcntl_signal' ) ) { |
| 176 | pcntl_signal( SIGINT, SIG_IGN ); |
| 177 | } |
| 178 | |
| 179 | $pipes = []; |
| 180 | $proc = proc_open( Shell::escape( $args ), $desc, $pipes ); |
| 181 | if ( $proc === false ) { |
| 182 | $this->fatalError( 'Unable to execute mysql' ); |
| 183 | } |
| 184 | |
| 185 | $ret = proc_close( $proc ); |
| 186 | if ( $ret === -1 ) { |
| 187 | $this->fatalError( 'proc_close() returned -1' ); |
| 188 | } elseif ( $ret ) { |
| 189 | $this->fatalError( 'Failed.', $ret ); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // @codeCoverageIgnoreStart |
| 195 | $maintClass = MysqlMaintenance::class; |
| 196 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 197 | // @codeCoverageIgnoreEnd |