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