Merge "Drop index oi_name_archive_name on table oldimage"
[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
24 use Wikimedia\Rdbms\ResultWrapper;
25
26 /**
27 * Database abstraction object for PHP extension mysqli.
28 *
29 * @ingroup Database
30 * @since 1.22
31 * @see Database
32 */
33 class DatabaseMysqli extends DatabaseMysqlBase {
34 /** @var $mConn mysqli */
35
36 /**
37 * @param string $sql
38 * @return resource
39 */
40 protected function doQuery( $sql ) {
41 $conn = $this->getBindingHandle();
42
43 if ( $this->bufferResults() ) {
44 $ret = $conn->query( $sql );
45 } else {
46 $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
47 }
48
49 return $ret;
50 }
51
52 /**
53 * @param string $realServer
54 * @return bool|mysqli
55 * @throws DBConnectionError
56 */
57 protected function mysqlConnect( $realServer ) {
58 # Avoid suppressed fatal error, which is very hard to track down
59 if ( !function_exists( 'mysqli_init' ) ) {
60 throw new DBConnectionError( $this, "MySQLi functions missing,"
61 . " have you compiled PHP with the --with-mysqli option?\n" );
62 }
63
64 // Other than mysql_connect, mysqli_real_connect expects an explicit port
65 // and socket parameters. So we need to parse the port and socket out of
66 // $realServer
67 $port = null;
68 $socket = null;
69 $hostAndPort = IP::splitHostAndPort( $realServer );
70 if ( $hostAndPort ) {
71 $realServer = $hostAndPort[0];
72 if ( $hostAndPort[1] ) {
73 $port = $hostAndPort[1];
74 }
75 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
76 // If we have a colon and something that's not a port number
77 // inside the hostname, assume it's the socket location
78 $hostAndSocket = explode( ':', $realServer );
79 $realServer = $hostAndSocket[0];
80 $socket = $hostAndSocket[1];
81 }
82
83 $mysqli = mysqli_init();
84
85 $connFlags = 0;
86 if ( $this->mFlags & self::DBO_SSL ) {
87 $connFlags |= MYSQLI_CLIENT_SSL;
88 $mysqli->ssl_set(
89 $this->sslKeyPath,
90 $this->sslCertPath,
91 null,
92 $this->sslCAPath,
93 $this->sslCiphers
94 );
95 }
96 if ( $this->mFlags & self::DBO_COMPRESS ) {
97 $connFlags |= MYSQLI_CLIENT_COMPRESS;
98 }
99 if ( $this->mFlags & self::DBO_PERSISTENT ) {
100 $realServer = 'p:' . $realServer;
101 }
102
103 if ( $this->utf8Mode ) {
104 // Tell the server we're communicating with it in UTF-8.
105 // This may engage various charset conversions.
106 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
107 } else {
108 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
109 }
110 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
111
112 if ( $mysqli->real_connect( $realServer, $this->mUser,
113 $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
114 ) {
115 return $mysqli;
116 }
117
118 return false;
119 }
120
121 protected function connectInitCharset() {
122 // already done in mysqlConnect()
123 return true;
124 }
125
126 /**
127 * @param string $charset
128 * @return bool
129 */
130 protected function mysqlSetCharset( $charset ) {
131 $conn = $this->getBindingHandle();
132
133 if ( method_exists( $conn, 'set_charset' ) ) {
134 return $conn->set_charset( $charset );
135 } else {
136 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
137 }
138 }
139
140 /**
141 * @return bool
142 */
143 protected function closeConnection() {
144 $conn = $this->getBindingHandle();
145
146 return $conn->close();
147 }
148
149 /**
150 * @return int
151 */
152 function insertId() {
153 $conn = $this->getBindingHandle();
154
155 return (int)$conn->insert_id;
156 }
157
158 /**
159 * @return int
160 */
161 function lastErrno() {
162 if ( $this->mConn ) {
163 return $this->mConn->errno;
164 } else {
165 return mysqli_connect_errno();
166 }
167 }
168
169 /**
170 * @return int
171 */
172 function affectedRows() {
173 $conn = $this->getBindingHandle();
174
175 return $conn->affected_rows;
176 }
177
178 /**
179 * @param string $db
180 * @return bool
181 */
182 function selectDB( $db ) {
183 $conn = $this->getBindingHandle();
184
185 $this->mDBname = $db;
186
187 return $conn->select_db( $db );
188 }
189
190 /**
191 * @param mysqli $res
192 * @return bool
193 */
194 protected function mysqlFreeResult( $res ) {
195 $res->free_result();
196
197 return true;
198 }
199
200 /**
201 * @param mysqli $res
202 * @return bool
203 */
204 protected function mysqlFetchObject( $res ) {
205 $object = $res->fetch_object();
206 if ( $object === null ) {
207 return false;
208 }
209
210 return $object;
211 }
212
213 /**
214 * @param mysqli $res
215 * @return bool
216 */
217 protected function mysqlFetchArray( $res ) {
218 $array = $res->fetch_array();
219 if ( $array === null ) {
220 return false;
221 }
222
223 return $array;
224 }
225
226 /**
227 * @param mysqli $res
228 * @return mixed
229 */
230 protected function mysqlNumRows( $res ) {
231 return $res->num_rows;
232 }
233
234 /**
235 * @param mysqli $res
236 * @return mixed
237 */
238 protected function mysqlNumFields( $res ) {
239 return $res->field_count;
240 }
241
242 /**
243 * @param mysqli $res
244 * @param int $n
245 * @return mixed
246 */
247 protected function mysqlFetchField( $res, $n ) {
248 $field = $res->fetch_field_direct( $n );
249
250 // Add missing properties to result (using flags property)
251 // which will be part of function mysql-fetch-field for backward compatibility
252 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
253 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
254 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
255 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
256 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
257 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
258 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
259 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
260 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
261
262 return $field;
263 }
264
265 /**
266 * @param resource|ResultWrapper $res
267 * @param int $n
268 * @return mixed
269 */
270 protected function mysqlFieldName( $res, $n ) {
271 $field = $res->fetch_field_direct( $n );
272
273 return $field->name;
274 }
275
276 /**
277 * @param resource|ResultWrapper $res
278 * @param int $n
279 * @return mixed
280 */
281 protected function mysqlFieldType( $res, $n ) {
282 $field = $res->fetch_field_direct( $n );
283
284 return $field->type;
285 }
286
287 /**
288 * @param resource|ResultWrapper $res
289 * @param int $row
290 * @return mixed
291 */
292 protected function mysqlDataSeek( $res, $row ) {
293 return $res->data_seek( $row );
294 }
295
296 /**
297 * @param mysqli $conn Optional connection object
298 * @return string
299 */
300 protected function mysqlError( $conn = null ) {
301 if ( $conn === null ) {
302 return mysqli_connect_error();
303 } else {
304 return $conn->error;
305 }
306 }
307
308 /**
309 * Escapes special characters in a string for use in an SQL statement
310 * @param string $s
311 * @return string
312 */
313 protected function mysqlRealEscapeString( $s ) {
314 $conn = $this->getBindingHandle();
315
316 return $conn->real_escape_string( $s );
317 }
318
319 /**
320 * Give an id for the connection
321 *
322 * mysql driver used resource id, but mysqli objects cannot be cast to string.
323 * @return string
324 */
325 public function __toString() {
326 if ( $this->mConn instanceof mysqli ) {
327 return (string)$this->mConn->thread_id;
328 } else {
329 // mConn might be false or something.
330 return (string)$this->mConn;
331 }
332 }
333 }