Fix SQLite patch-(page|template)links-fix-pk.sql column order
[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 if ( $lb->getServerName( $index ) === $host ) {
76 break;
77 }
78 }
79 if ( $index >= $serverCount ) {
80 $this->error( "Error: Host not configured: \"$host\"" );
81 exit( 1 );
82 }
83 } elseif ( $this->hasOption( 'write' ) ) {
84 $index = $lb->getWriterIndex();
85 } else {
86 $group = $this->getOption( 'group', false );
87 $index = $lb->getReaderIndex( $group, $dbName );
88 if ( $index === false ) {
89 $this->error( "Error: unable to get reader index" );
90 exit( 1 );
91 }
92 }
93
94 if ( $lb->getServerType( $index ) !== 'mysql' ) {
95 $this->error( "Error: this script only works with MySQL/MariaDB" );
96 exit( 1 );
97 }
98
99 $status = $this->runMysql( $lb->getServerInfo( $index ), $dbName );
100 exit( $status );
101 }
102
103 /**
104 * Run the mysql client for the given server info
105 *
106 * @param array $info
107 * @param string|false $dbName The DB name, or false to use the main wiki DB
108 *
109 * @return int The desired exit status
110 */
111 private function runMysql( $info, $dbName ) {
112 // Write the password to an option file to avoid disclosing it to other
113 // processes running on the system
114 $tmpFile = TempFSFile::factory( 'mw-mysql', 'ini' );
115 chmod( $tmpFile->getPath(), 0600 );
116 file_put_contents( $tmpFile->getPath(), "[client]\npassword={$info['password']}\n" );
117
118 // stdin/stdout need to be the actual file descriptors rather than
119 // PHP's pipe wrappers so that mysql can use them as an interactive
120 // terminal.
121 $desc = [
122 0 => STDIN,
123 1 => STDOUT,
124 2 => STDERR,
125 ];
126
127 // Split host and port as in DatabaseMysqli::mysqlConnect()
128 $realServer = $info['host'];
129 $hostAndPort = IP::splitHostAndPort( $realServer );
130 $socket = false;
131 $port = false;
132 if ( $hostAndPort ) {
133 $realServer = $hostAndPort[0];
134 if ( $hostAndPort[1] ) {
135 $port = $hostAndPort[1];
136 }
137 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
138 // If we have a colon and something that's not a port number
139 // inside the hostname, assume it's the socket location
140 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
141 }
142
143 if ( $dbName === false ) {
144 $dbName = $info['dbname'];
145 }
146
147 $args = [
148 'mysql',
149 "--defaults-extra-file={$tmpFile->getPath()}",
150 "--user={$info['user']}",
151 "--database={$dbName}",
152 ];
153 if ( $socket !== false ) {
154 $args[] = "--socket={$socket}";
155 } else {
156 $args[] = "--host={$realServer}";
157 }
158 if ( $port !== false ) {
159 $args[] = "--port={$port}";
160 }
161
162 $args = array_merge( $args, $this->mArgs );
163
164 // Ignore SIGINT if possible, otherwise the wrapper terminates when the user presses
165 // ctrl-C to kill a query.
166 if ( function_exists( 'pcntl_signal' ) ) {
167 pcntl_signal( SIGINT, SIG_IGN );
168 }
169
170 $pipes = [];
171 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
172 if ( $proc === false ) {
173 $this->error( "Unable to execute mysql" );
174 return 1;
175 }
176 $ret = proc_close( $proc );
177 if ( $ret === -1 ) {
178 $this->error( "proc_close() returned -1" );
179 return 1;
180 }
181 return $ret;
182 }
183 }
184
185 $maintClass = MysqlMaintenance::class;
186 require_once RUN_MAINTENANCE_IF_MAIN;