Merge "Turn ApiPrefixUniquenessTest into a structure test"
[lhc/web/wiklou.git] / maintenance / mysql.php
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 MediaWiki\MediaWikiServices;
28
29 require_once __DIR__ . '/Maintenance.php';
30
31 /**
32 * @ingroup Maintenance
33 */
34 class MysqlMaintenance extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( "Execute the MySQL client binary. " .
38 "Non-option arguments will be passed through to mysql." );
39 $this->addOption( 'write', 'Connect to the master database', false, false );
40 $this->addOption( 'group', 'Specify query group', false, false );
41 $this->addOption( 'host', 'Connect to a specific MySQL server', false, true );
42 $this->addOption( 'list-hosts', 'List the available DB hosts', false, false );
43 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
44 $this->addOption( 'wikidb',
45 'The database wiki ID to use if not the current one', false, true );
46
47 // Fake argument for help message
48 $this->addArg( '-- mysql_option ...', 'Options to pass to mysql', false );
49 }
50
51 public function execute() {
52 $dbName = $this->getOption( 'wikidb', false );
53 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
54 if ( $this->hasOption( 'cluster' ) ) {
55 try {
56 $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) );
57 } catch ( InvalidArgumentException $e ) {
58 $this->error( "Error: invalid cluster" );
59 exit( 1 );
60 }
61 } else {
62 $lb = $lbf->getMainLB( $dbName );
63 }
64 if ( $this->hasOption( 'list-hosts' ) ) {
65 $serverCount = $lb->getServerCount();
66 for ( $index = 0; $index < $serverCount; ++$index ) {
67 echo $lb->getServerName( $index ) . "\n";
68 }
69 exit( 0 );
70 }
71 if ( $this->hasOption( 'host' ) ) {
72 $host = $this->getOption( 'host' );
73 $serverCount = $lb->getServerCount();
74 for ( $index = 0; $index < $serverCount; ++$index ) {
75 $serverInfo = $lb->getServerInfo( $index );
76
77 if ( $lb->getServerName( $index ) === $host ) {
78 break;
79 }
80 }
81 if ( $index >= $serverCount ) {
82 $this->error( "Error: Host not configured: \"$host\"" );
83 exit( 1 );
84 }
85 } elseif ( $this->hasOption( 'write' ) ) {
86 $index = $lb->getWriterIndex();
87 } else {
88 $group = $this->getOption( 'group', false );
89 $index = $lb->getReaderIndex( $group, $dbName );
90 if ( $index === false ) {
91 $this->error( "Error: unable to get reader index" );
92 exit( 1 );
93 }
94 }
95
96 if ( $lb->getServerType( $index ) !== 'mysql' ) {
97 $this->error( "Error: this script only works with MySQL/MariaDB" );
98 exit( 1 );
99 }
100
101 $status = $this->runMysql( $lb->getServerInfo( $index ), $dbName );
102 exit( $status );
103 }
104
105 /**
106 * Run the mysql client for the given server info
107 *
108 * @param array $info
109 * @param string|false $dbName The DB name, or false to use the main wiki DB
110 *
111 * @return int The desired exit status
112 */
113 private function runMysql( $info, $dbName ) {
114 // Write the password to an option file to avoid disclosing it to other
115 // processes running on the system
116 $tmpFile = TempFSFile::factory( 'mw-mysql', 'ini' );
117 chmod( $tmpFile->getPath(), 0600 );
118 file_put_contents( $tmpFile->getPath(), "[client]\npassword={$info['password']}\n" );
119
120 // stdin/stdout need to be the actual file descriptors rather than
121 // PHP's pipe wrappers so that mysql can use them as an interactive
122 // terminal.
123 $desc = [
124 0 => STDIN,
125 1 => STDOUT,
126 2 => STDERR,
127 ];
128
129 // Split host and port as in DatabaseMysqli::mysqlConnect()
130 $realServer = $info['host'];
131 $hostAndPort = IP::splitHostAndPort( $realServer );
132 $socket = false;
133 $port = false;
134 if ( $hostAndPort ) {
135 $realServer = $hostAndPort[0];
136 if ( $hostAndPort[1] ) {
137 $port = $hostAndPort[1];
138 }
139 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
140 // If we have a colon and something that's not a port number
141 // inside the hostname, assume it's the socket location
142 $hostAndSocket = explode( ':', $realServer, 2 );
143 $realServer = $hostAndSocket[0];
144 $socket = $hostAndSocket[1];
145 }
146
147 if ( $dbName === false ) {
148 $dbName = $info['dbname'];
149 }
150
151 $args = [
152 'mysql',
153 "--defaults-extra-file={$tmpFile->getPath()}",
154 "--user={$info['user']}",
155 "--database={$dbName}",
156 ];
157 if ( $socket !== false ) {
158 $args[] = "--socket={$socket}";
159 } else {
160 $args[] = "--host={$realServer}";
161 }
162 if ( $port !== false ) {
163 $args[] = "--port={$port}";
164 }
165
166 $args = array_merge( $args, $this->mArgs );
167
168 // Ignore SIGINT if possible, otherwise the wrapper terminates when the user presses
169 // ctrl-C to kill a query.
170 if ( function_exists( 'pcntl_signal' ) ) {
171 pcntl_signal( SIGINT, SIG_IGN );
172 }
173
174 $pipes = [];
175 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
176 if ( $proc === false ) {
177 $this->error( "Unable to execute mysql" );
178 return 1;
179 }
180 $ret = proc_close( $proc );
181 if ( $ret === -1 ) {
182 $this->error( "proc_close() returned -1" );
183 return 1;
184 }
185 return $ret;
186 }
187 }
188
189 $maintClass = MysqlMaintenance::class;
190 require_once RUN_MAINTENANCE_IF_MAIN;