ApiParse: don't reparse language link titles
[mediawiki.git] / maintenance / mysql.php
blob0776d871c50c8b93ac370cc657ee46a62fb28f0c
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.
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.
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.
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
22 * @file
23 * @ingroup Maintenance
26 use MediaWiki\Shell\Shell;
27 use Wikimedia\IPUtils;
28 use Wikimedia\Rdbms\ServerInfo;
30 // @codeCoverageIgnoreStart
31 require_once __DIR__ . '/Maintenance.php';
32 // @codeCoverageIgnoreEnd
34 /**
35 * @ingroup Maintenance
37 class MysqlMaintenance extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( "Execute the MySQL client binary. " .
41 "Non-option arguments will be passed through to mysql." );
42 $this->addOption( 'write', 'Connect to the primary database', false, false );
43 $this->addOption( 'group', 'Specify query group', false, true );
44 $this->addOption( 'host', 'Connect to a known MySQL server', false, true );
45 $this->addOption( 'raw-host',
46 'Connect directly to a specific MySQL server, even if not known to MediaWiki '
47 . 'via wgLBFactoryConf (e.g. parser cache or depooled host). '
48 . 'Credentails will be chosen based on --cluster and --wikidb.',
49 false,
50 true
52 $this->addOption( 'list-hosts', 'List the available DB hosts', false, false );
53 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
54 $this->addOption( 'wikidb',
55 'The database wiki ID to use if not the current one',
56 false,
57 true
60 // Fake argument for help message
61 $this->addArg( '-- mysql_option ...', 'Options to pass to mysql', false );
64 public function execute() {
65 $dbName = $this->getOption( 'wikidb', false );
66 $lbf = $this->getServiceContainer()->getDBLoadBalancerFactory();
68 // Pick LB
69 if ( $this->hasOption( 'cluster' ) ) {
70 try {
71 $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) );
72 } catch ( InvalidArgumentException $e ) {
73 $this->fatalError( 'Error: invalid cluster' );
75 } else {
76 $lb = $lbf->getMainLB( $dbName );
79 // List hosts, or pick host
80 if ( $this->hasOption( 'list-hosts' ) ) {
81 $serverCount = $lb->getServerCount();
82 for ( $index = 0; $index < $serverCount; ++$index ) {
83 echo $lb->getServerName( $index ) . "\n";
85 return;
87 if ( $this->hasOption( 'host' ) ) {
88 $host = $this->getOption( 'host' );
89 $serverCount = $lb->getServerCount();
90 for ( $index = 0; $index < $serverCount; ++$index ) {
91 if ( $lb->getServerName( $index ) === $host ) {
92 break;
95 if ( $index >= $serverCount ) {
96 $this->fatalError( "Error: Host not configured: \"$host\"" );
98 } elseif ( $this->hasOption( 'write' ) ) {
99 $index = ServerInfo::WRITER_INDEX;
100 } else {
101 $group = $this->getOption( 'group', false );
102 $index = $lb->getReaderIndex( $group );
103 if ( $index === false && $group ) {
104 // retry without the group; it may not exist
105 $index = $lb->getReaderIndex( false );
107 if ( $index === false ) {
108 $this->fatalError( 'Error: unable to get reader index' );
111 if ( $lb->getServerType( $index ) !== 'mysql' ) {
112 $this->fatalError( 'Error: this script only works with MySQL/MariaDB' );
115 $serverInfo = $lb->getServerInfo( $index );
116 // Override host. This uses the server info of the host determined
117 // by the other options for the purposes of user/password.
118 if ( $this->hasOption( 'raw-host' ) ) {
119 $host = $this->getOption( 'raw-host' );
120 $serverInfo = [ 'host' => $host ] + $serverInfo;
123 $this->runMysql( $serverInfo, $dbName );
127 * Run the mysql client for the given server info
129 * @param array $info
130 * @param string|false $dbName The DB name, or false to use the main wiki DB
132 private function runMysql( $info, $dbName ) {
133 // Write the password to an option file to avoid disclosing it to other
134 // processes running on the system
135 $tmpFile = TempFSFile::factory( 'mw-mysql', 'ini' );
136 chmod( $tmpFile->getPath(), 0600 );
137 file_put_contents( $tmpFile->getPath(), "[client]\npassword={$info['password']}\n" );
139 // stdin/stdout need to be the actual file descriptors rather than
140 // PHP's pipe wrappers so that mysql can use them as an interactive
141 // terminal.
142 $desc = [
143 0 => STDIN,
144 1 => STDOUT,
145 2 => STDERR,
148 // Split host and port as in DatabaseMySQL::mysqlConnect()
149 $realServer = $info['host'];
150 $hostAndPort = IPUtils::splitHostAndPort( $realServer );
151 $socket = false;
152 $port = false;
153 if ( $hostAndPort ) {
154 $realServer = $hostAndPort[0];
155 if ( $hostAndPort[1] ) {
156 $port = $hostAndPort[1];
158 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
159 // If we have a colon and something that's not a port number
160 // inside the hostname, assume it's the socket location
161 [ $realServer, $socket ] = explode( ':', $realServer, 2 );
164 if ( $dbName === false ) {
165 $dbName = $info['dbname'];
168 $args = [
169 'mysql',
170 "--defaults-extra-file={$tmpFile->getPath()}",
171 "--user={$info['user']}",
172 "--database={$dbName}",
174 if ( $socket !== false ) {
175 $args[] = "--socket={$socket}";
176 } else {
177 $args[] = "--host={$realServer}";
179 if ( $port !== false ) {
180 $args[] = "--port={$port}";
183 $args = array_merge( $args, $this->getArgs() );
185 // Ignore SIGINT if possible, otherwise the wrapper terminates when the user presses
186 // ctrl-C to kill a query.
187 if ( function_exists( 'pcntl_signal' ) ) {
188 pcntl_signal( SIGINT, SIG_IGN );
191 $pipes = [];
192 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
193 if ( $proc === false ) {
194 $this->fatalError( 'Unable to execute mysql' );
197 $ret = proc_close( $proc );
198 if ( $ret === -1 ) {
199 $this->fatalError( 'proc_close() returned -1' );
200 } elseif ( $ret ) {
201 $this->fatalError( 'Failed.', $ret );
206 // @codeCoverageIgnoreStart
207 $maintClass = MysqlMaintenance::class;
208 require_once RUN_MAINTENANCE_IF_MAIN;
209 // @codeCoverageIgnoreEnd