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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2013-10-17 00:03:01 +00:00
|
|
|
* @return resource
|
|
|
|
|
*/
|
|
|
|
|
protected function doQuery( $sql ) {
|
|
|
|
|
if ( $this->bufferResults() ) {
|
|
|
|
|
$ret = $this->mConn->query( $sql );
|
|
|
|
|
} else {
|
|
|
|
|
$ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT );
|
|
|
|
|
}
|
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
|
|
|
|
|
* @return bool|mysqli
|
|
|
|
|
* @throws DBConnectionError
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlConnect( $realServer ) {
|
2013-12-27 22:40:15 +00:00
|
|
|
global $wgDBmysql5;
|
2013-10-17 00:03:01 +00:00
|
|
|
# Fail now
|
|
|
|
|
# Otherwise we get a suppressed fatal error, which is very hard to track down
|
|
|
|
|
if ( !function_exists( 'mysqli_init' ) ) {
|
|
|
|
|
throw new DBConnectionError( $this, "MySQLi functions missing,"
|
|
|
|
|
. " have you compiled PHP with the --with-mysqli option?\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$connFlags = 0;
|
|
|
|
|
if ( $this->mFlags & DBO_SSL ) {
|
|
|
|
|
$connFlags |= MYSQLI_CLIENT_SSL;
|
|
|
|
|
}
|
|
|
|
|
if ( $this->mFlags & DBO_COMPRESS ) {
|
|
|
|
|
$connFlags |= MYSQLI_CLIENT_COMPRESS;
|
|
|
|
|
}
|
|
|
|
|
if ( $this->mFlags & DBO_PERSISTENT ) {
|
|
|
|
|
$realServer = 'p:' . $realServer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mysqli = mysqli_init();
|
2013-12-27 22:40:15 +00:00
|
|
|
if ( $wgDBmysql5 ) {
|
|
|
|
|
// 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' );
|
|
|
|
|
}
|
2013-10-17 00:03:01 +00:00
|
|
|
|
2013-12-27 22:40:15 +00:00
|
|
|
$numAttempts = 2;
|
2013-10-17 00:03:01 +00:00
|
|
|
for ( $i = 0; $i < $numAttempts; $i++ ) {
|
|
|
|
|
if ( $i > 1 ) {
|
|
|
|
|
usleep( 1000 );
|
|
|
|
|
}
|
|
|
|
|
if ( $mysqli->real_connect( $realServer, $this->mUser,
|
2013-11-20 06:58:22 +00:00
|
|
|
$this->mPassword, $this->mDBname, null, null, $connFlags )
|
|
|
|
|
) {
|
2013-10-17 00:03:01 +00:00
|
|
|
return $mysqli;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 ) {
|
|
|
|
|
if ( method_exists( $this->mConn, 'set_charset' ) ) {
|
|
|
|
|
return $this->mConn->set_charset( $charset );
|
|
|
|
|
} else {
|
|
|
|
|
return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function closeConnection() {
|
|
|
|
|
return $this->mConn->close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
function insertId() {
|
|
|
|
|
return $this->mConn->insert_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
function lastErrno() {
|
|
|
|
|
if ( $this->mConn ) {
|
|
|
|
|
return $this->mConn->errno;
|
|
|
|
|
} else {
|
|
|
|
|
return mysqli_connect_errno();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
function affectedRows() {
|
|
|
|
|
return $this->mConn->affected_rows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $db
|
2013-10-17 00:03:01 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function selectDB( $db ) {
|
|
|
|
|
$this->mDBname = $db;
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-10-17 00:03:01 +00:00
|
|
|
return $this->mConn->select_db( $db );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function getServerVersion() {
|
|
|
|
|
return $this->mConn->server_info;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
|
|
|
|
* @param mysqli $res
|
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
|
* @param mysqli $res
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
|
* @param mysqli $res
|
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
|
* @param mysqli $res
|
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
|
* @param mysqli $res
|
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
|
* @param mysqli $res
|
|
|
|
|
* @param int $n
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-10-17 00:03:01 +00:00
|
|
|
protected function mysqlFetchField( $res, $n ) {
|
|
|
|
|
$field = $res->fetch_field_direct( $n );
|
|
|
|
|
$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;
|
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
|
|
|
/**
|
|
|
|
|
* @param resource|ResultWrapper $res
|
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
|
* @param resource|ResultWrapper $res
|
|
|
|
|
* @param int $n
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-12-07 20:08:47 +00:00
|
|
|
protected function mysqlFieldType( $res, $n ) {
|
|
|
|
|
$field = $res->fetch_field_direct( $n );
|
|
|
|
|
return $field->type;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
|
|
|
|
* @param resource|ResultWrapper $res
|
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
|
* @param mysqli $conn Optional connection object
|
|
|
|
|
* @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 ) {
|
|
|
|
|
return $this->mConn->real_escape_string( $s );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function mysqlPing() {
|
|
|
|
|
return $this->mConn->ping();
|
|
|
|
|
}
|
|
|
|
|
}
|