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