Merge "Set api-request log http.request_headers properly"
[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 $hostAndSocket = explode( ':', $realServer, 2 );
82 $realServer = $hostAndSocket[0];
83 $socket = $hostAndSocket[1];
84 }
85
86 $mysqli = mysqli_init();
87
88 $connFlags = 0;
89 if ( $this->flags & self::DBO_SSL ) {
90 $connFlags |= MYSQLI_CLIENT_SSL;
91 $mysqli->ssl_set(
92 $this->sslKeyPath,
93 $this->sslCertPath,
94 $this->sslCAFile,
95 $this->sslCAPath,
96 $this->sslCiphers
97 );
98 }
99 if ( $this->flags & self::DBO_COMPRESS ) {
100 $connFlags |= MYSQLI_CLIENT_COMPRESS;
101 }
102 if ( $this->flags & self::DBO_PERSISTENT ) {
103 $realServer = 'p:' . $realServer;
104 }
105
106 if ( $this->utf8Mode ) {
107 // Tell the server we're communicating with it in UTF-8.
108 // This may engage various charset conversions.
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
110 } else {
111 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
112 }
113 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
114
115 if ( $mysqli->real_connect(
116 $realServer,
117 $this->user,
118 $this->password,
119 $dbName,
120 $port,
121 $socket,
122 $connFlags
123 ) ) {
124 return $mysqli;
125 }
126
127 return false;
128 }
129
130 protected function connectInitCharset() {
131 // already done in mysqlConnect()
132 return true;
133 }
134
135 /**
136 * @param string $charset
137 * @return bool
138 */
139 protected function mysqlSetCharset( $charset ) {
140 $conn = $this->getBindingHandle();
141
142 if ( method_exists( $conn, 'set_charset' ) ) {
143 return $conn->set_charset( $charset );
144 } else {
145 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
146 }
147 }
148
149 /**
150 * @return bool
151 */
152 protected function closeConnection() {
153 $conn = $this->getBindingHandle();
154
155 return $conn->close();
156 }
157
158 /**
159 * @return int
160 */
161 function insertId() {
162 $conn = $this->getBindingHandle();
163
164 return (int)$conn->insert_id;
165 }
166
167 /**
168 * @return int
169 */
170 function lastErrno() {
171 if ( $this->conn instanceof mysqli ) {
172 return $this->conn->errno;
173 } else {
174 return mysqli_connect_errno();
175 }
176 }
177
178 /**
179 * @return int
180 */
181 protected function fetchAffectedRowCount() {
182 $conn = $this->getBindingHandle();
183
184 return $conn->affected_rows;
185 }
186
187 function doSelectDomain( DatabaseDomain $domain ) {
188 $conn = $this->getBindingHandle();
189
190 if ( $domain->getSchema() !== null ) {
191 throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
192 }
193
194 $database = $domain->getDatabase();
195 if ( !$conn->select_db( $database ) ) {
196 throw new DBExpectedError( $this, "Could not select database '$database'." );
197 }
198
199 // Update that domain fields on success (no exception thrown)
200 $this->currentDomain = $domain;
201
202 return true;
203 }
204
205 /**
206 * @param mysqli_result $res
207 * @return bool
208 */
209 protected function mysqlFreeResult( $res ) {
210 $res->free_result();
211
212 return true;
213 }
214
215 /**
216 * @param mysqli_result $res
217 * @return stdClass|bool
218 */
219 protected function mysqlFetchObject( $res ) {
220 $object = $res->fetch_object();
221 if ( $object === null ) {
222 return false;
223 }
224
225 return $object;
226 }
227
228 /**
229 * @param mysqli_result $res
230 * @return bool
231 */
232 protected function mysqlFetchArray( $res ) {
233 $array = $res->fetch_array();
234 if ( $array === null ) {
235 return false;
236 }
237
238 return $array;
239 }
240
241 /**
242 * @param mysqli_result $res
243 * @return mixed
244 */
245 protected function mysqlNumRows( $res ) {
246 return $res->num_rows;
247 }
248
249 /**
250 * @param mysqli_result $res
251 * @return mixed
252 */
253 protected function mysqlNumFields( $res ) {
254 return $res->field_count;
255 }
256
257 /**
258 * @param mysqli_result $res
259 * @param int $n
260 * @return mixed
261 */
262 protected function mysqlFetchField( $res, $n ) {
263 $field = $res->fetch_field_direct( $n );
264
265 // Add missing properties to result (using flags property)
266 // which will be part of function mysql-fetch-field for backward compatibility
267 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
268 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
269 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
270 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
271 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
272 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
273 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
274 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
275 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
276
277 return $field;
278 }
279
280 /**
281 * @param mysqli_result $res
282 * @param int $n
283 * @return mixed
284 */
285 protected function mysqlFieldName( $res, $n ) {
286 $field = $res->fetch_field_direct( $n );
287
288 return $field->name;
289 }
290
291 /**
292 * @param mysqli_result $res
293 * @param int $n
294 * @return mixed
295 */
296 protected function mysqlFieldType( $res, $n ) {
297 $field = $res->fetch_field_direct( $n );
298
299 return $field->type;
300 }
301
302 /**
303 * @param mysqli_result $res
304 * @param int $row
305 * @return mixed
306 */
307 protected function mysqlDataSeek( $res, $row ) {
308 return $res->data_seek( $row );
309 }
310
311 /**
312 * @param mysqli|null $conn Optional connection object
313 * @return string
314 */
315 protected function mysqlError( $conn = null ) {
316 if ( $conn === null ) {
317 return mysqli_connect_error();
318 } else {
319 return $conn->error;
320 }
321 }
322
323 /**
324 * Escapes special characters in a string for use in an SQL statement
325 * @param string $s
326 * @return string
327 */
328 protected function mysqlRealEscapeString( $s ) {
329 $conn = $this->getBindingHandle();
330
331 return $conn->real_escape_string( (string)$s );
332 }
333
334 /**
335 * Give an id for the connection
336 *
337 * mysql driver used resource id, but mysqli objects cannot be cast to string.
338 * @return string
339 */
340 public function __toString() {
341 if ( $this->conn instanceof mysqli ) {
342 return (string)$this->conn->thread_id;
343 } else {
344 // mConn might be false or something.
345 return (string)$this->conn;
346 }
347 }
348
349 /**
350 * @return mysqli
351 */
352 protected function getBindingHandle() {
353 return parent::getBindingHandle();
354 }
355 }
356
357 /**
358 * @deprecated since 1.29
359 */
360 class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );