Merge "Added a separate error message for mkdir failures"
[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
29 /**
30 * Database abstraction object for PHP extension mysqli.
31 *
32 * @ingroup Database
33 * @since 1.22
34 * @see Database
35 */
36 class DatabaseMysqli extends DatabaseMysqlBase {
37 /** @var $mConn mysqli */
38
39 /**
40 * @param string $sql
41 * @return resource
42 */
43 protected function doQuery( $sql ) {
44 $conn = $this->getBindingHandle();
45
46 if ( $this->bufferResults() ) {
47 $ret = $conn->query( $sql );
48 } else {
49 $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
50 }
51
52 return $ret;
53 }
54
55 /**
56 * @param string $realServer
57 * @return bool|mysqli
58 * @throws DBConnectionError
59 */
60 protected function mysqlConnect( $realServer ) {
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 );
82 $realServer = $hostAndSocket[0];
83 $socket = $hostAndSocket[1];
84 }
85
86 $mysqli = mysqli_init();
87
88 $connFlags = 0;
89 if ( $this->mFlags & self::DBO_SSL ) {
90 $connFlags |= MYSQLI_CLIENT_SSL;
91 $mysqli->ssl_set(
92 $this->sslKeyPath,
93 $this->sslCertPath,
94 null,
95 $this->sslCAPath,
96 $this->sslCiphers
97 );
98 }
99 if ( $this->mFlags & self::DBO_COMPRESS ) {
100 $connFlags |= MYSQLI_CLIENT_COMPRESS;
101 }
102 if ( $this->mFlags & 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( $realServer, $this->mUser,
116 $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
117 ) {
118 return $mysqli;
119 }
120
121 return false;
122 }
123
124 protected function connectInitCharset() {
125 // already done in mysqlConnect()
126 return true;
127 }
128
129 /**
130 * @param string $charset
131 * @return bool
132 */
133 protected function mysqlSetCharset( $charset ) {
134 $conn = $this->getBindingHandle();
135
136 if ( method_exists( $conn, 'set_charset' ) ) {
137 return $conn->set_charset( $charset );
138 } else {
139 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
140 }
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->mConn ) {
166 return $this->mConn->errno;
167 } else {
168 return mysqli_connect_errno();
169 }
170 }
171
172 /**
173 * @return int
174 */
175 function affectedRows() {
176 $conn = $this->getBindingHandle();
177
178 return $conn->affected_rows;
179 }
180
181 /**
182 * @param string $db
183 * @return bool
184 */
185 function selectDB( $db ) {
186 $conn = $this->getBindingHandle();
187
188 $this->mDBname = $db;
189
190 return $conn->select_db( $db );
191 }
192
193 /**
194 * @param mysqli_result $res
195 * @return bool
196 */
197 protected function mysqlFreeResult( $res ) {
198 $res->free_result();
199
200 return true;
201 }
202
203 /**
204 * @param mysqli_result $res
205 * @return bool
206 */
207 protected function mysqlFetchObject( $res ) {
208 $object = $res->fetch_object();
209 if ( $object === null ) {
210 return false;
211 }
212
213 return $object;
214 }
215
216 /**
217 * @param mysqli_result $res
218 * @return bool
219 */
220 protected function mysqlFetchArray( $res ) {
221 $array = $res->fetch_array();
222 if ( $array === null ) {
223 return false;
224 }
225
226 return $array;
227 }
228
229 /**
230 * @param mysqli_result $res
231 * @return mixed
232 */
233 protected function mysqlNumRows( $res ) {
234 return $res->num_rows;
235 }
236
237 /**
238 * @param mysqli $res
239 * @return mixed
240 */
241 protected function mysqlNumFields( $res ) {
242 return $res->field_count;
243 }
244
245 /**
246 * @param mysqli $res
247 * @param int $n
248 * @return mixed
249 */
250 protected function mysqlFetchField( $res, $n ) {
251 $field = $res->fetch_field_direct( $n );
252
253 // Add missing properties to result (using flags property)
254 // which will be part of function mysql-fetch-field for backward compatibility
255 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
256 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
257 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
258 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
259 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
260 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
261 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
262 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
263 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
264
265 return $field;
266 }
267
268 /**
269 * @param mysqli $res
270 * @param int $n
271 * @return mixed
272 */
273 protected function mysqlFieldName( $res, $n ) {
274 $field = $res->fetch_field_direct( $n );
275
276 return $field->name;
277 }
278
279 /**
280 * @param mysqli $res
281 * @param int $n
282 * @return mixed
283 */
284 protected function mysqlFieldType( $res, $n ) {
285 $field = $res->fetch_field_direct( $n );
286
287 return $field->type;
288 }
289
290 /**
291 * @param mysqli_result $res
292 * @param int $row
293 * @return mixed
294 */
295 protected function mysqlDataSeek( $res, $row ) {
296 return $res->data_seek( $row );
297 }
298
299 /**
300 * @param mysqli $conn Optional connection object
301 * @return string
302 */
303 protected function mysqlError( $conn = null ) {
304 if ( $conn === null ) {
305 return mysqli_connect_error();
306 } else {
307 return $conn->error;
308 }
309 }
310
311 /**
312 * Escapes special characters in a string for use in an SQL statement
313 * @param string $s
314 * @return string
315 */
316 protected function mysqlRealEscapeString( $s ) {
317 $conn = $this->getBindingHandle();
318
319 return $conn->real_escape_string( $s );
320 }
321
322 /**
323 * Give an id for the connection
324 *
325 * mysql driver used resource id, but mysqli objects cannot be cast to string.
326 * @return string
327 */
328 public function __toString() {
329 if ( $this->mConn instanceof mysqli ) {
330 return (string)$this->mConn->thread_id;
331 } else {
332 // mConn might be false or something.
333 return (string)$this->mConn;
334 }
335 }
336 }
337
338 class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );