Only store currently-existing categories in the categories table
[mediawiki.git] / includes / db / DatabaseMysqli.php
blobd45805ad462a7f635dc47774dba73d3ba5281b23
1 <?php
2 /**
3 * This is the MySQLi database abstraction layer.
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.
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.
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
20 * @file
21 * @ingroup Database
24 /**
25 * Database abstraction object for PHP extension mysqli.
27 * @ingroup Database
28 * @since 1.22
29 * @see Database
31 class DatabaseMysqli extends DatabaseMysqlBase {
32 /** @var mysqli */
33 protected $mConn;
35 /**
36 * @param string $sql
37 * @return resource
39 protected function doQuery( $sql ) {
40 $conn = $this->getBindingHandle();
42 if ( $this->bufferResults() ) {
43 $ret = $conn->query( $sql );
44 } else {
45 $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
48 return $ret;
51 /**
52 * @param string $realServer
53 * @return bool|mysqli
54 * @throws DBConnectionError
56 protected function mysqlConnect( $realServer ) {
57 global $wgDBmysql5;
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" );
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];
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];
84 $connFlags = 0;
85 if ( $this->mFlags & DBO_SSL ) {
86 $connFlags |= MYSQLI_CLIENT_SSL;
88 if ( $this->mFlags & DBO_COMPRESS ) {
89 $connFlags |= MYSQLI_CLIENT_COMPRESS;
91 if ( $this->mFlags & DBO_PERSISTENT ) {
92 $realServer = 'p:' . $realServer;
95 $mysqli = mysqli_init();
96 if ( $wgDBmysql5 ) {
97 // Tell the server we're communicating with it in UTF-8.
98 // This may engage various charset conversions.
99 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
100 } else {
101 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
103 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
105 if ( $mysqli->real_connect( $realServer, $this->mUser,
106 $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
108 return $mysqli;
111 return false;
114 protected function connectInitCharset() {
115 // already done in mysqlConnect()
116 return true;
120 * @param string $charset
121 * @return bool
123 protected function mysqlSetCharset( $charset ) {
124 $conn = $this->getBindingHandle();
126 if ( method_exists( $conn, 'set_charset' ) ) {
127 return $conn->set_charset( $charset );
128 } else {
129 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
134 * @return bool
136 protected function closeConnection() {
137 $conn = $this->getBindingHandle();
139 return $conn->close();
143 * @return int
145 function insertId() {
146 $conn = $this->getBindingHandle();
148 return (int)$conn->insert_id;
152 * @return int
154 function lastErrno() {
155 if ( $this->mConn ) {
156 return $this->mConn->errno;
157 } else {
158 return mysqli_connect_errno();
163 * @return int
165 function affectedRows() {
166 $conn = $this->getBindingHandle();
168 return $conn->affected_rows;
172 * @param string $db
173 * @return bool
175 function selectDB( $db ) {
176 $conn = $this->getBindingHandle();
178 $this->mDBname = $db;
180 return $conn->select_db( $db );
184 * @param mysqli $res
185 * @return bool
187 protected function mysqlFreeResult( $res ) {
188 $res->free_result();
190 return true;
194 * @param mysqli $res
195 * @return bool
197 protected function mysqlFetchObject( $res ) {
198 $object = $res->fetch_object();
199 if ( $object === null ) {
200 return false;
203 return $object;
207 * @param mysqli $res
208 * @return bool
210 protected function mysqlFetchArray( $res ) {
211 $array = $res->fetch_array();
212 if ( $array === null ) {
213 return false;
216 return $array;
220 * @param mysqli $res
221 * @return mixed
223 protected function mysqlNumRows( $res ) {
224 return $res->num_rows;
228 * @param mysqli $res
229 * @return mixed
231 protected function mysqlNumFields( $res ) {
232 return $res->field_count;
236 * @param mysqli $res
237 * @param int $n
238 * @return mixed
240 protected function mysqlFetchField( $res, $n ) {
241 $field = $res->fetch_field_direct( $n );
243 // Add missing properties to result (using flags property)
244 // which will be part of function mysql-fetch-field for backward compatibility
245 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
246 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
247 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
248 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
249 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
250 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
251 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
252 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
253 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
255 return $field;
259 * @param resource|ResultWrapper $res
260 * @param int $n
261 * @return mixed
263 protected function mysqlFieldName( $res, $n ) {
264 $field = $res->fetch_field_direct( $n );
266 return $field->name;
270 * @param resource|ResultWrapper $res
271 * @param int $n
272 * @return mixed
274 protected function mysqlFieldType( $res, $n ) {
275 $field = $res->fetch_field_direct( $n );
277 return $field->type;
281 * @param resource|ResultWrapper $res
282 * @param int $row
283 * @return mixed
285 protected function mysqlDataSeek( $res, $row ) {
286 return $res->data_seek( $row );
290 * @param mysqli $conn Optional connection object
291 * @return string
293 protected function mysqlError( $conn = null ) {
294 if ( $conn === null ) {
295 return mysqli_connect_error();
296 } else {
297 return $conn->error;
302 * Escapes special characters in a string for use in an SQL statement
303 * @param string $s
304 * @return string
306 protected function mysqlRealEscapeString( $s ) {
307 $conn = $this->getBindingHandle();
309 return $conn->real_escape_string( $s );
312 protected function mysqlPing() {
313 $conn = $this->getBindingHandle();
315 return $conn->ping();
319 * Give an id for the connection
321 * mysql driver used resource id, but mysqli objects cannot be cast to string.
322 * @return string
324 public function __toString() {
325 if ( $this->mConn instanceof mysqli ) {
326 return (string)$this->mConn->thread_id;
327 } else {
328 // mConn might be false or something.
329 return (string)$this->mConn;