Merge "Type hint against LinkTarget in WatchedItemStore"
[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 /**
129 * @return bool
130 */
131 protected function closeConnection() {
132 $conn = $this->getBindingHandle();
133
134 return $conn->close();
135 }
136
137 /**
138 * @return int
139 */
140 function insertId() {
141 $conn = $this->getBindingHandle();
142
143 return (int)$conn->insert_id;
144 }
145
146 /**
147 * @return int
148 */
149 function lastErrno() {
150 if ( $this->conn instanceof mysqli ) {
151 return $this->conn->errno;
152 } else {
153 return mysqli_connect_errno();
154 }
155 }
156
157 /**
158 * @return int
159 */
160 protected function fetchAffectedRowCount() {
161 $conn = $this->getBindingHandle();
162
163 return $conn->affected_rows;
164 }
165
166 /**
167 * @param mysqli_result $res
168 * @return bool
169 */
170 protected function mysqlFreeResult( $res ) {
171 $res->free_result();
172
173 return true;
174 }
175
176 /**
177 * @param mysqli_result $res
178 * @return stdClass|bool
179 */
180 protected function mysqlFetchObject( $res ) {
181 $object = $res->fetch_object();
182 if ( $object === null ) {
183 return false;
184 }
185
186 return $object;
187 }
188
189 /**
190 * @param mysqli_result $res
191 * @return array|false
192 */
193 protected function mysqlFetchArray( $res ) {
194 $array = $res->fetch_array();
195 if ( $array === null ) {
196 return false;
197 }
198
199 return $array;
200 }
201
202 /**
203 * @param mysqli_result $res
204 * @return mixed
205 */
206 protected function mysqlNumRows( $res ) {
207 return $res->num_rows;
208 }
209
210 /**
211 * @param mysqli_result $res
212 * @return mixed
213 */
214 protected function mysqlNumFields( $res ) {
215 return $res->field_count;
216 }
217
218 /**
219 * @param mysqli_result $res
220 * @param int $n
221 * @return mixed
222 */
223 protected function mysqlFetchField( $res, $n ) {
224 $field = $res->fetch_field_direct( $n );
225
226 // Add missing properties to result (using flags property)
227 // which will be part of function mysql-fetch-field for backward compatibility
228 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
229 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
230 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
231 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
232 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
233 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
234 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
235 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
236 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
237
238 return $field;
239 }
240
241 /**
242 * @param mysqli_result $res
243 * @param int $n
244 * @return mixed
245 */
246 protected function mysqlFieldName( $res, $n ) {
247 $field = $res->fetch_field_direct( $n );
248
249 return $field->name;
250 }
251
252 /**
253 * @param mysqli_result $res
254 * @param int $n
255 * @return mixed
256 */
257 protected function mysqlFieldType( $res, $n ) {
258 $field = $res->fetch_field_direct( $n );
259
260 return $field->type;
261 }
262
263 /**
264 * @param mysqli_result $res
265 * @param int $row
266 * @return mixed
267 */
268 protected function mysqlDataSeek( $res, $row ) {
269 return $res->data_seek( $row );
270 }
271
272 /**
273 * @param mysqli|null $conn Optional connection object
274 * @return string
275 */
276 protected function mysqlError( $conn = null ) {
277 if ( $conn === null ) {
278 return mysqli_connect_error();
279 } else {
280 return $conn->error;
281 }
282 }
283
284 /**
285 * Escapes special characters in a string for use in an SQL statement
286 * @param string $s
287 * @return string
288 */
289 protected function mysqlRealEscapeString( $s ) {
290 $conn = $this->getBindingHandle();
291
292 return $conn->real_escape_string( (string)$s );
293 }
294
295 /**
296 * @return mysqli
297 */
298 protected function getBindingHandle() {
299 return parent::getBindingHandle();
300 }
301 }
302
303 /**
304 * @deprecated since 1.29
305 */
306 class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );