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