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