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