Debian Codegraph @

maintenance/mysql.php

raw symbols SCIP
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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Maintenance
24 */
25
26 use MediaWiki\Shell\Shell;
27 use Wikimedia\FileBackend\FSFile\TempFSFile;
28 use Wikimedia\IPUtils;
29 use Wikimedia\Rdbms\ServerInfo;
30
31 // @codeCoverageIgnoreStart
32 require_once __DIR__ . '/Maintenance.php';
33 // @codeCoverageIgnoreEnd
34
35 /**
36 * @ingroup Maintenance
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 // Fake argument for help message
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 // Pick LB
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 // List hosts, or pick host
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 // retry without the group; it may not exist
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 // Override host. This uses the server info of the host determined
118 // by the other options for the purposes of user/password.
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 * Run the mysql client for the given server info
129 *
130 * @param array $info
131 * @param string|false $dbName The DB name, or false to use the main wiki DB
132 */
133 private function runMysql( $info, $dbName ) {
134 // Write the password to an option file to avoid disclosing it to other
135 // processes running on the system
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 // stdin/stdout need to be the actual file descriptors rather than
141 // PHP's pipe wrappers so that mysql can use them as an interactive
142 // terminal.
143 $desc = [
144 0 => STDIN,
145 1 => STDOUT,
146 2 => STDERR,
147 ];
148
149 // Split host and port as in DatabaseMySQL::mysqlConnect()
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 // If we have a colon and something that's not a port number
161 // inside the hostname, assume it's the socket location
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 // Ignore SIGINT if possible, otherwise the wrapper terminates when the user presses
187 // ctrl-C to kill a query.
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 // @codeCoverageIgnoreStart
208 $maintClass = MysqlMaintenance::class;
209 require_once RUN_MAINTENANCE_IF_MAIN;
210 // @codeCoverageIgnoreEnd