Merge "NamespaceMultiselectWidget: Allow filtering by namespace ID or label"
[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 if ( $domain->getSchema() !== null ) {
189 throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
190 }
191
192 $database = $domain->getDatabase();
193 if ( $database !== $this->getDBname() ) {
194 $conn = $this->getBindingHandle();
195 if ( !$conn->select_db( $database ) ) {
196 throw new DBExpectedError( $this, "Could not select database '$database'." );
197 }
198 }
199
200 // Update that domain fields on success (no exception thrown)
201 $this->currentDomain = $domain;
202
203 return true;
204 }
205
206 /**
207 * @param mysqli_result $res
208 * @return bool
209 */
210 protected function mysqlFreeResult( $res ) {
211 $res->free_result();
212
213 return true;
214 }
215
216 /**
217 * @param mysqli_result $res
218 * @return stdClass|bool
219 */
220 protected function mysqlFetchObject( $res ) {
221 $object = $res->fetch_object();
222 if ( $object === null ) {
223 return false;
224 }
225
226 return $object;
227 }
228
229 /**
230 * @param mysqli_result $res
231 * @return bool
232 */
233 protected function mysqlFetchArray( $res ) {
234 $array = $res->fetch_array();
235 if ( $array === null ) {
236 return false;
237 }
238
239 return $array;
240 }
241
242 /**
243 * @param mysqli_result $res
244 * @return mixed
245 */
246 protected function mysqlNumRows( $res ) {
247 return $res->num_rows;
248 }
249
250 /**
251 * @param mysqli_result $res
252 * @return mixed
253 */
254 protected function mysqlNumFields( $res ) {
255 return $res->field_count;
256 }
257
258 /**
259 * @param mysqli_result $res
260 * @param int $n
261 * @return mixed
262 */
263 protected function mysqlFetchField( $res, $n ) {
264 $field = $res->fetch_field_direct( $n );
265
266 // Add missing properties to result (using flags property)
267 // which will be part of function mysql-fetch-field for backward compatibility
268 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
269 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
270 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
271 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
272 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
273 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
274 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
275 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
276 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
277
278 return $field;
279 }
280
281 /**
282 * @param mysqli_result $res
283 * @param int $n
284 * @return mixed
285 */
286 protected function mysqlFieldName( $res, $n ) {
287 $field = $res->fetch_field_direct( $n );
288
289 return $field->name;
290 }
291
292 /**
293 * @param mysqli_result $res
294 * @param int $n
295 * @return mixed
296 */
297 protected function mysqlFieldType( $res, $n ) {
298 $field = $res->fetch_field_direct( $n );
299
300 return $field->type;
301 }
302
303 /**
304 * @param mysqli_result $res
305 * @param int $row
306 * @return mixed
307 */
308 protected function mysqlDataSeek( $res, $row ) {
309 return $res->data_seek( $row );
310 }
311
312 /**
313 * @param mysqli|null $conn Optional connection object
314 * @return string
315 */
316 protected function mysqlError( $conn = null ) {
317 if ( $conn === null ) {
318 return mysqli_connect_error();
319 } else {
320 return $conn->error;
321 }
322 }
323
324 /**
325 * Escapes special characters in a string for use in an SQL statement
326 * @param string $s
327 * @return string
328 */
329 protected function mysqlRealEscapeString( $s ) {
330 $conn = $this->getBindingHandle();
331
332 return $conn->real_escape_string( (string)$s );
333 }
334
335 /**
336 * Give an id for the connection
337 *
338 * mysql driver used resource id, but mysqli objects cannot be cast to string.
339 * @return string
340 */
341 public function __toString() {
342 if ( $this->conn instanceof mysqli ) {
343 return (string)$this->conn->thread_id;
344 } else {
345 // mConn might be false or something.
346 return (string)$this->conn;
347 }
348 }
349
350 /**
351 * @return mysqli
352 */
353 protected function getBindingHandle() {
354 return parent::getBindingHandle();
355 }
356 }
357
358 /**
359 * @deprecated since 1.29
360 */
361 class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );