Merge "rdbms: Use correct value for 'sslmode' in DatabasePostgres"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabaseMysqli.php
1 <?php
2 /**
3 * This is the MySQLi database abstraction layer.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23 namespace Wikimedia\Rdbms;
24
25 use mysqli;
26 use mysqli_result;
27 use IP;
28 use stdClass;
29
30 /**
31 * Database abstraction object for PHP extension mysqli.
32 *
33 * @ingroup Database
34 * @since 1.22
35 * @see Database
36 */
37 class DatabaseMysqli extends DatabaseMysqlBase {
38 /**
39 * @param string $sql
40 * @return mysqli_result|bool
41 */
42 protected function doQuery( $sql ) {
43 $conn = $this->getBindingHandle();
44
45 if ( $this->bufferResults() ) {
46 $ret = $conn->query( $sql );
47 } else {
48 $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
49 }
50
51 return $ret;
52 }
53
54 /**
55 * @param string $realServer
56 * @param string|null $dbName
57 * @return bool|mysqli
58 * @throws DBConnectionError
59 */
60 protected function mysqlConnect( $realServer, $dbName ) {
61 # Avoid suppressed fatal error, which is very hard to track down
62 if ( !function_exists( 'mysqli_init' ) ) {
63 throw new DBConnectionError( $this, "MySQLi functions missing,"
64 . " have you compiled PHP with the --with-mysqli option?\n" );
65 }
66
67 // Other than mysql_connect, mysqli_real_connect expects an explicit port
68 // and socket parameters. So we need to parse the port and socket out of
69 // $realServer
70 $port = null;
71 $socket = null;
72 $hostAndPort = IP::splitHostAndPort( $realServer );
73 if ( $hostAndPort ) {
74 $realServer = $hostAndPort[0];
75 if ( $hostAndPort[1] ) {
76 $port = $hostAndPort[1];
77 }
78 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
79 // If we have a colon and something that's not a port number
80 // inside the hostname, assume it's the socket location
81 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
82 }
83
84 $mysqli = mysqli_init();
85
86 $connFlags = 0;
87 if ( $this->flags & self::DBO_SSL ) {
88 $connFlags |= MYSQLI_CLIENT_SSL;
89 $mysqli->ssl_set(
90 $this->sslKeyPath,
91 $this->sslCertPath,
92 $this->sslCAFile,
93 $this->sslCAPath,
94 $this->sslCiphers
95 );
96 }
97 if ( $this->flags & self::DBO_COMPRESS ) {
98 $connFlags |= MYSQLI_CLIENT_COMPRESS;
99 }
100 if ( $this->flags & self::DBO_PERSISTENT ) {
101 $realServer = 'p:' . $realServer;
102 }
103
104 if ( $this->utf8Mode ) {
105 // Tell the server we're communicating with it in UTF-8.
106 // This may engage various charset conversions.
107 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
108 } else {
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
110 }
111 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
112
113 if ( $mysqli->real_connect(
114 $realServer,
115 $this->user,
116 $this->password,
117 $dbName,
118 $port,
119 $socket,
120 $connFlags
121 ) ) {
122 return $mysqli;
123 }
124
125 return false;
126 }
127
128 protected function connectInitCharset() {
129 // already done in mysqlConnect()
130 return true;
131 }
132
133 /**
134 * @param string $charset
135 * @return bool
136 */
137 protected function mysqlSetCharset( $charset ) {
138 $conn = $this->getBindingHandle();
139
140 return $conn->set_charset( $charset );
141 }
142
143 /**
144 * @return bool
145 */
146 protected function closeConnection() {
147 $conn = $this->getBindingHandle();
148
149 return $conn->close();
150 }
151
152 /**
153 * @return int
154 */
155 function insertId() {
156 $conn = $this->getBindingHandle();
157
158 return (int)$conn->insert_id;
159 }
160
161 /**
162 * @return int
163 */
164 function lastErrno() {
165 if ( $this->conn instanceof mysqli ) {
166 return $this->conn->errno;
167 } else {
168 return mysqli_connect_errno();
169 }
170 }
171
172 /**
173 * @return int
174 */
175 protected function fetchAffectedRowCount() {
176 $conn = $this->getBindingHandle();
177
178 return $conn->affected_rows;
179 }
180
181 function doSelectDomain( DatabaseDomain $domain ) {
182 if ( $domain->getSchema() !== null ) {
183 throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
184 }
185
186 $database = $domain->getDatabase();
187 if ( $database !== $this->getDBname() ) {
188 $conn = $this->getBindingHandle();
189 if ( !$conn->select_db( $database ) ) {
190 throw new DBExpectedError( $this, "Could not select database '$database'." );
191 }
192 }
193
194 // Update that domain fields on success (no exception thrown)
195 $this->currentDomain = $domain;
196
197 return true;
198 }
199
200 /**
201 * @param mysqli_result $res
202 * @return bool
203 */
204 protected function mysqlFreeResult( $res ) {
205 $res->free_result();
206
207 return true;
208 }
209
210 /**
211 * @param mysqli_result $res
212 * @return stdClass|bool
213 */
214 protected function mysqlFetchObject( $res ) {
215 $object = $res->fetch_object();
216 if ( $object === null ) {
217 return false;
218 }
219
220 return $object;
221 }
222
223 /**
224 * @param mysqli_result $res
225 * @return bool
226 */
227 protected function mysqlFetchArray( $res ) {
228 $array = $res->fetch_array();
229 if ( $array === null ) {
230 return false;
231 }
232
233 return $array;
234 }
235
236 /**
237 * @param mysqli_result $res
238 * @return mixed
239 */
240 protected function mysqlNumRows( $res ) {
241 return $res->num_rows;
242 }
243
244 /**
245 * @param mysqli_result $res
246 * @return mixed
247 */
248 protected function mysqlNumFields( $res ) {
249 return $res->field_count;
250 }
251
252 /**
253 * @param mysqli_result $res
254 * @param int $n
255 * @return mixed
256 */
257 protected function mysqlFetchField( $res, $n ) {
258 $field = $res->fetch_field_direct( $n );
259
260 // Add missing properties to result (using flags property)
261 // which will be part of function mysql-fetch-field for backward compatibility
262 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
263 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
264 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
265 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
266 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
267 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
268 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
269 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
270 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
271
272 return $field;
273 }
274
275 /**
276 * @param mysqli_result $res
277 * @param int $n
278 * @return mixed
279 */
280 protected function mysqlFieldName( $res, $n ) {
281 $field = $res->fetch_field_direct( $n );
282
283 return $field->name;
284 }
285
286 /**
287 * @param mysqli_result $res
288 * @param int $n
289 * @return mixed
290 */
291 protected function mysqlFieldType( $res, $n ) {
292 $field = $res->fetch_field_direct( $n );
293
294 return $field->type;
295 }
296
297 /**
298 * @param mysqli_result $res
299 * @param int $row
300 * @return mixed
301 */
302 protected function mysqlDataSeek( $res, $row ) {
303 return $res->data_seek( $row );
304 }
305
306 /**
307 * @param mysqli|null $conn Optional connection object
308 * @return string
309 */
310 protected function mysqlError( $conn = null ) {
311 if ( $conn === null ) {
312 return mysqli_connect_error();
313 } else {
314 return $conn->error;
315 }
316 }
317
318 /**
319 * Escapes special characters in a string for use in an SQL statement
320 * @param string $s
321 * @return string
322 */
323 protected function mysqlRealEscapeString( $s ) {
324 $conn = $this->getBindingHandle();
325
326 return $conn->real_escape_string( (string)$s );
327 }
328
329 /**
330 * Give an id for the connection
331 *
332 * mysql driver used resource id, but mysqli objects cannot be cast to string.
333 * @return string
334 */
335 public function __toString() {
336 if ( $this->conn instanceof mysqli ) {
337 return (string)$this->conn->thread_id;
338 } else {
339 // mConn might be false or something.
340 return (string)$this->conn;
341 }
342 }
343
344 /**
345 * @return mysqli
346 */
347 protected function getBindingHandle() {
348 return parent::getBindingHandle();
349 }
350 }
351
352 /**
353 * @deprecated since 1.29
354 */
355 class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );