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