2013-10-17 00:03:01 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This is the MySQLi database abstraction layer.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Database
|
|
|
|
|
*/
|
2017-02-07 04:49:57 +00:00
|
|
|
namespace Wikimedia\Rdbms;
|
2013-10-17 00:03:01 +00:00
|
|
|
|
2017-02-07 04:49:57 +00:00
|
|
|
use mysqli;
|
2017-07-29 09:17:24 +00:00
|
|
|
use mysqli_result;
|
2017-02-07 04:49:57 +00:00
|
|
|
use IP;
|
2018-03-02 10:11:35 +00:00
|
|
|
use stdClass;
|
2017-02-19 05:03:13 +00:00
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
/**
|
|
|
|
|
* Database abstraction object for PHP extension mysqli.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Database
|
|
|
|
|
* @since 1.22
|
|
|
|
|
* @see Database
|
|
|
|
|
*/
|
|
|
|
|
class DatabaseMysqli extends DatabaseMysqlBase {
|
|
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $sql
|
2018-02-28 20:56:34 +00:00
|
|
|
* @return mysqli_result
|
2013-10-17 00:03:01 +00:00
|
|
|
*/
|
|
|
|
|
protected function doQuery( $sql ) {
|
2015-06-26 05:58:23 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
if ( $this->bufferResults() ) {
|
2015-06-26 05:58:23 +00:00
|
|
|
$ret = $conn->query( $sql );
|
2013-10-17 00:03:01 +00:00
|
|
|
} else {
|
2015-06-26 05:58:23 +00:00
|
|
|
$ret = $conn->query( $sql, MYSQLI_USE_RESULT );
|
2013-10-17 00:03:01 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $realServer
|
2018-08-14 23:44:41 +00:00
|
|
|
* @param string|null $dbName
|
2013-12-27 01:54:51 +00:00
|
|
|
* @return bool|mysqli
|
|
|
|
|
* @throws DBConnectionError
|
|
|
|
|
*/
|
2018-08-14 23:44:41 +00:00
|
|
|
protected function mysqlConnect( $realServer, $dbName ) {
|
2015-06-26 05:58:23 +00:00
|
|
|
# Avoid suppressed fatal error, which is very hard to track down
|
2013-10-17 00:03:01 +00:00
|
|
|
if ( !function_exists( 'mysqli_init' ) ) {
|
|
|
|
|
throw new DBConnectionError( $this, "MySQLi functions missing,"
|
|
|
|
|
. " have you compiled PHP with the --with-mysqli option?\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-27 09:10:50 +00:00
|
|
|
// Other than mysql_connect, mysqli_real_connect expects an explicit port
|
2014-06-01 13:54:48 +00:00
|
|
|
// and socket parameters. So we need to parse the port and socket out of
|
|
|
|
|
// $realServer
|
2014-01-27 09:10:50 +00:00
|
|
|
$port = null;
|
2014-06-01 13:54:48 +00:00
|
|
|
$socket = null;
|
2014-01-27 09:10:50 +00:00
|
|
|
$hostAndPort = IP::splitHostAndPort( $realServer );
|
|
|
|
|
if ( $hostAndPort ) {
|
|
|
|
|
$realServer = $hostAndPort[0];
|
|
|
|
|
if ( $hostAndPort[1] ) {
|
|
|
|
|
$port = $hostAndPort[1];
|
|
|
|
|
}
|
2014-06-01 13:54:48 +00:00
|
|
|
} elseif ( substr_count( $realServer, ':' ) == 1 ) {
|
|
|
|
|
// If we have a colon and something that's not a port number
|
|
|
|
|
// inside the hostname, assume it's the socket location
|
2018-05-20 14:50:41 +00:00
|
|
|
$hostAndSocket = explode( ':', $realServer, 2 );
|
2014-06-01 13:54:48 +00:00
|
|
|
$realServer = $hostAndSocket[0];
|
|
|
|
|
$socket = $hostAndSocket[1];
|
2014-01-27 09:10:50 +00:00
|
|
|
}
|
2014-03-20 18:59:20 +00:00
|
|
|
|
2016-08-22 17:37:31 +00:00
|
|
|
$mysqli = mysqli_init();
|
|
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
$connFlags = 0;
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( $this->flags & self::DBO_SSL ) {
|
2013-10-17 00:03:01 +00:00
|
|
|
$connFlags |= MYSQLI_CLIENT_SSL;
|
2016-08-22 17:37:31 +00:00
|
|
|
$mysqli->ssl_set(
|
|
|
|
|
$this->sslKeyPath,
|
|
|
|
|
$this->sslCertPath,
|
2017-08-24 23:54:19 +00:00
|
|
|
$this->sslCAFile,
|
2016-08-22 17:37:31 +00:00
|
|
|
$this->sslCAPath,
|
|
|
|
|
$this->sslCiphers
|
|
|
|
|
);
|
2013-10-17 00:03:01 +00:00
|
|
|
}
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( $this->flags & self::DBO_COMPRESS ) {
|
2013-10-17 00:03:01 +00:00
|
|
|
$connFlags |= MYSQLI_CLIENT_COMPRESS;
|
|
|
|
|
}
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( $this->flags & self::DBO_PERSISTENT ) {
|
2013-10-17 00:03:01 +00:00
|
|
|
$realServer = 'p:' . $realServer;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 19:55:45 +00:00
|
|
|
if ( $this->utf8Mode ) {
|
2013-12-27 22:40:15 +00:00
|
|
|
// Tell the server we're communicating with it in UTF-8.
|
|
|
|
|
// This may engage various charset conversions.
|
|
|
|
|
$mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
|
|
|
|
|
} else {
|
|
|
|
|
$mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
|
|
|
|
|
}
|
2014-06-19 20:20:26 +00:00
|
|
|
$mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
|
2013-10-17 00:03:01 +00:00
|
|
|
|
2018-08-14 23:44:41 +00:00
|
|
|
if ( $mysqli->real_connect(
|
|
|
|
|
$realServer,
|
|
|
|
|
$this->user,
|
|
|
|
|
$this->password,
|
|
|
|
|
$dbName,
|
|
|
|
|
$port,
|
|
|
|
|
$socket,
|
|
|
|
|
$connFlags
|
|
|
|
|
) ) {
|
2014-06-19 20:20:26 +00:00
|
|
|
return $mysqli;
|
2013-10-17 00:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 22:40:15 +00:00
|
|
|
protected function connectInitCharset() {
|
|
|
|
|
// already done in mysqlConnect()
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-15 22:21:40 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $charset
|
2013-11-15 22:21:40 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function mysqlSetCharset( $charset ) {
|
2015-06-26 05:58:23 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
|
|
|
|
if ( method_exists( $conn, 'set_charset' ) ) {
|
|
|
|
|
return $conn->set_charset( $charset );
|
2013-11-15 22:21:40 +00:00
|
|
|
} else {
|
|
|
|
|
return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function closeConnection() {
|
2015-06-26 05:58:23 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
|
|
|
|
return $conn->close();
|
2013-10-17 00:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
function insertId() {
|
2015-06-26 05:58:23 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
|
|
|
|
return (int)$conn->insert_id;
|
2013-10-17 00:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
function lastErrno() {
|
2018-03-02 10:11:35 +00:00
|
|
|
if ( $this->conn instanceof mysqli ) {
|
2018-02-13 06:58:57 +00:00
|
|
|
return $this->conn->errno;
|
2013-10-17 00:03:01 +00:00
|
|
|
} else {
|
|
|
|
|
return mysqli_connect_errno();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2018-01-28 14:10:39 +00:00
|
|
|
protected function fetchAffectedRowCount() {
|
2015-06-26 05:58:23 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
|
|
|
|
return $conn->affected_rows;
|
2013-10-17 00:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
2018-08-14 23:44:41 +00:00
|
|
|
function doSelectDomain( DatabaseDomain $domain ) {
|
2015-06-26 05:58:23 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
2018-08-14 23:44:41 +00:00
|
|
|
if ( $domain->getSchema() !== null ) {
|
|
|
|
|
throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$database = $domain->getDatabase();
|
|
|
|
|
if ( !$conn->select_db( $database ) ) {
|
|
|
|
|
throw new DBExpectedError( $this, "Could not select database '$database'." );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update that domain fields on success (no exception thrown)
|
|
|
|
|
$this->currentDomain = $domain;
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2018-08-14 23:44:41 +00:00
|
|
|
return true;
|
2013-10-17 00:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
2017-07-29 09:17:24 +00:00
|
|
|
* @param mysqli_result $res
|
2013-12-27 01:54:51 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlFreeResult( $res ) {
|
|
|
|
|
$res->free_result();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
2017-07-29 09:17:24 +00:00
|
|
|
* @param mysqli_result $res
|
2018-03-02 10:11:35 +00:00
|
|
|
* @return stdClass|bool
|
2013-12-27 01:54:51 +00:00
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlFetchObject( $res ) {
|
|
|
|
|
$object = $res->fetch_object();
|
|
|
|
|
if ( $object === null ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
return $object;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
2017-07-29 09:17:24 +00:00
|
|
|
* @param mysqli_result $res
|
2013-12-27 01:54:51 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlFetchArray( $res ) {
|
|
|
|
|
$array = $res->fetch_array();
|
|
|
|
|
if ( $array === null ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
2017-07-29 09:17:24 +00:00
|
|
|
* @param mysqli_result $res
|
2013-12-27 01:54:51 +00:00
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlNumRows( $res ) {
|
|
|
|
|
return $res->num_rows;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
2018-03-02 10:11:35 +00:00
|
|
|
* @param mysqli_result $res
|
2013-12-27 01:54:51 +00:00
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlNumFields( $res ) {
|
|
|
|
|
return $res->field_count;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
2018-03-02 10:11:35 +00:00
|
|
|
* @param mysqli_result $res
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param int $n
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlFetchField( $res, $n ) {
|
|
|
|
|
$field = $res->fetch_field_direct( $n );
|
2015-02-18 18:34:30 +00:00
|
|
|
|
|
|
|
|
// Add missing properties to result (using flags property)
|
|
|
|
|
// which will be part of function mysql-fetch-field for backward compatibility
|
2013-10-17 00:03:01 +00:00
|
|
|
$field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
|
|
|
|
|
$field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
|
|
|
|
|
$field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
|
|
|
|
|
$field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
|
|
|
|
|
$field->binary = $field->flags & MYSQLI_BINARY_FLAG;
|
2015-02-18 18:34:30 +00:00
|
|
|
$field->numeric = $field->flags & MYSQLI_NUM_FLAG;
|
|
|
|
|
$field->blob = $field->flags & MYSQLI_BLOB_FLAG;
|
|
|
|
|
$field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
|
|
|
|
|
$field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
return $field;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
2018-03-02 10:11:35 +00:00
|
|
|
* @param mysqli_result $res
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param int $n
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlFieldName( $res, $n ) {
|
|
|
|
|
$field = $res->fetch_field_direct( $n );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
return $field->name;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
2018-03-02 10:11:35 +00:00
|
|
|
* @param mysqli_result $res
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param int $n
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-12-07 20:08:47 +00:00
|
|
|
protected function mysqlFieldType( $res, $n ) {
|
|
|
|
|
$field = $res->fetch_field_direct( $n );
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2013-12-07 20:08:47 +00:00
|
|
|
return $field->type;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
2017-07-29 09:17:24 +00:00
|
|
|
* @param mysqli_result $res
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param int $row
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlDataSeek( $res, $row ) {
|
|
|
|
|
return $res->data_seek( $row );
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param mysqli|null $conn Optional connection object
|
2013-12-27 01:54:51 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlError( $conn = null ) {
|
2013-11-19 18:03:54 +00:00
|
|
|
if ( $conn === null ) {
|
2013-10-17 00:03:01 +00:00
|
|
|
return mysqli_connect_error();
|
|
|
|
|
} else {
|
|
|
|
|
return $conn->error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
|
|
|
|
* Escapes special characters in a string for use in an SQL statement
|
|
|
|
|
* @param string $s
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlRealEscapeString( $s ) {
|
2015-06-26 05:58:23 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
2017-10-01 00:46:51 +00:00
|
|
|
return $conn->real_escape_string( (string)$s );
|
2013-10-17 00:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-01 03:55:00 +00:00
|
|
|
/**
|
|
|
|
|
* Give an id for the connection
|
|
|
|
|
*
|
|
|
|
|
* mysql driver used resource id, but mysqli objects cannot be cast to string.
|
2014-08-23 20:34:25 +00:00
|
|
|
* @return string
|
2014-03-01 03:55:00 +00:00
|
|
|
*/
|
|
|
|
|
public function __toString() {
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( $this->conn instanceof mysqli ) {
|
|
|
|
|
return (string)$this->conn->thread_id;
|
2014-03-01 03:55:00 +00:00
|
|
|
} else {
|
|
|
|
|
// mConn might be false or something.
|
2018-02-13 06:58:57 +00:00
|
|
|
return (string)$this->conn;
|
2014-03-01 03:55:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-28 20:56:34 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mysqli
|
|
|
|
|
*/
|
|
|
|
|
protected function getBindingHandle() {
|
|
|
|
|
return parent::getBindingHandle();
|
|
|
|
|
}
|
2013-10-17 00:03:01 +00:00
|
|
|
}
|
2017-02-07 04:49:57 +00:00
|
|
|
|
2018-05-29 16:21:31 +00:00
|
|
|
/**
|
|
|
|
|
* @deprecated since 1.29
|
|
|
|
|
*/
|
2017-02-07 04:49:57 +00:00
|
|
|
class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );
|