Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / db / DatabaseMysqli.php
blobe9c4b39f56815ebce38f1decf2bb082b93224be0
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 /**
33 * @param string $sql
34 * @return resource
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 );
43 return $ret;
46 /**
47 * @param string $realServer
48 * @return bool|mysqli
49 * @throws DBConnectionError
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" );
60 // Other than mysql_connect, mysqli_real_connect expects an explicit port
61 // and socket parameters. So we need to parse the port and socket out of
62 // $realServer
63 $port = null;
64 $socket = null;
65 $hostAndPort = IP::splitHostAndPort( $realServer );
66 if ( $hostAndPort ) {
67 $realServer = $hostAndPort[0];
68 if ( $hostAndPort[1] ) {
69 $port = $hostAndPort[1];
71 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
72 // If we have a colon and something that's not a port number
73 // inside the hostname, assume it's the socket location
74 $hostAndSocket = explode( ':', $realServer );
75 $realServer = $hostAndSocket[0];
76 $socket = $hostAndSocket[1];
79 $connFlags = 0;
80 if ( $this->mFlags & DBO_SSL ) {
81 $connFlags |= MYSQLI_CLIENT_SSL;
83 if ( $this->mFlags & DBO_COMPRESS ) {
84 $connFlags |= MYSQLI_CLIENT_COMPRESS;
86 if ( $this->mFlags & DBO_PERSISTENT ) {
87 $realServer = 'p:' . $realServer;
90 $mysqli = mysqli_init();
91 if ( $wgDBmysql5 ) {
92 // Tell the server we're communicating with it in UTF-8.
93 // This may engage various charset conversions.
94 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
95 } else {
96 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
98 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
100 if ( $mysqli->real_connect( $realServer, $this->mUser,
101 $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
103 return $mysqli;
106 return false;
109 protected function connectInitCharset() {
110 // already done in mysqlConnect()
111 return true;
115 * @param string $charset
116 * @return bool
118 protected function mysqlSetCharset( $charset ) {
119 if ( method_exists( $this->mConn, 'set_charset' ) ) {
120 return $this->mConn->set_charset( $charset );
121 } else {
122 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
127 * @return bool
129 protected function closeConnection() {
130 return $this->mConn->close();
134 * @return int
136 function insertId() {
137 return $this->mConn->insert_id;
141 * @return int
143 function lastErrno() {
144 if ( $this->mConn ) {
145 return $this->mConn->errno;
146 } else {
147 return mysqli_connect_errno();
152 * @return int
154 function affectedRows() {
155 return $this->mConn->affected_rows;
159 * @param string $db
160 * @return bool
162 function selectDB( $db ) {
163 $this->mDBname = $db;
165 return $this->mConn->select_db( $db );
169 * @param mysqli $res
170 * @return bool
172 protected function mysqlFreeResult( $res ) {
173 $res->free_result();
175 return true;
179 * @param mysqli $res
180 * @return bool
182 protected function mysqlFetchObject( $res ) {
183 $object = $res->fetch_object();
184 if ( $object === null ) {
185 return false;
188 return $object;
192 * @param mysqli $res
193 * @return bool
195 protected function mysqlFetchArray( $res ) {
196 $array = $res->fetch_array();
197 if ( $array === null ) {
198 return false;
201 return $array;
205 * @param mysqli $res
206 * @return mixed
208 protected function mysqlNumRows( $res ) {
209 return $res->num_rows;
213 * @param mysqli $res
214 * @return mixed
216 protected function mysqlNumFields( $res ) {
217 return $res->field_count;
221 * @param mysqli $res
222 * @param int $n
223 * @return mixed
225 protected function mysqlFetchField( $res, $n ) {
226 $field = $res->fetch_field_direct( $n );
227 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
228 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
229 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
230 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
231 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
233 return $field;
237 * @param resource|ResultWrapper $res
238 * @param int $n
239 * @return mixed
241 protected function mysqlFieldName( $res, $n ) {
242 $field = $res->fetch_field_direct( $n );
244 return $field->name;
248 * @param resource|ResultWrapper $res
249 * @param int $n
250 * @return mixed
252 protected function mysqlFieldType( $res, $n ) {
253 $field = $res->fetch_field_direct( $n );
255 return $field->type;
259 * @param resource|ResultWrapper $res
260 * @param int $row
261 * @return mixed
263 protected function mysqlDataSeek( $res, $row ) {
264 return $res->data_seek( $row );
268 * @param mysqli $conn Optional connection object
269 * @return string
271 protected function mysqlError( $conn = null ) {
272 if ( $conn === null ) {
273 return mysqli_connect_error();
274 } else {
275 return $conn->error;
280 * Escapes special characters in a string for use in an SQL statement
281 * @param string $s
282 * @return string
284 protected function mysqlRealEscapeString( $s ) {
285 return $this->mConn->real_escape_string( $s );
288 protected function mysqlPing() {
289 return $this->mConn->ping();
293 * Give an id for the connection
295 * mysql driver used resource id, but mysqli objects cannot be cast to string.
296 * @return string
298 public function __toString() {
299 if ( $this->mConn instanceof Mysqli ) {
300 return (string)$this->mConn->thread_id;
301 } else {
302 // mConn might be false or something.
303 return (string)$this->mConn;