Add support for mysqli extension
DatabaseBase::factory()'s parameter array now takes an optional 'driver' variable to allow specifying a specific driver for the database class if we support more than one. This is so we can gracefully support mysql/mysqli as the former is deprecated, but the concept could be extended to other databases as well. Bug: 45288 Co-Author: Chad Horohoe <chadh@wikimedia.org> Change-Id: I6733fe21c4aa7443e409c5dfa7c789552b2b62b7
This commit is contained in:
parent
c10500aeae
commit
b2d64432ee
6 changed files with 236 additions and 12 deletions
|
|
@ -326,6 +326,7 @@ production.
|
|||
database is created.
|
||||
* (bug 47191) Fixed "Column 'si_title' cannot be part of FULLTEXT index"
|
||||
MySQL error when installing using the binary character set option.
|
||||
* (bug 45288) Support mysqli PHP extension
|
||||
|
||||
=== API changes in 1.22 ===
|
||||
* (bug 25553) The JSON output formatter now leaves forward slashes unescaped
|
||||
|
|
|
|||
|
|
@ -479,6 +479,7 @@ $wgAutoloadLocalClasses = array(
|
|||
'DatabaseMssql' => 'includes/db/DatabaseMssql.php',
|
||||
'DatabaseMysql' => 'includes/db/DatabaseMysql.php',
|
||||
'DatabaseMysqlBase' => 'includes/db/DatabaseMysqlBase.php',
|
||||
'DatabaseMysqli' => 'includes/db/DatabaseMysqli.php',
|
||||
'DatabaseOracle' => 'includes/db/DatabaseOracle.php',
|
||||
'DatabasePostgres' => 'includes/db/DatabasePostgres.php',
|
||||
'DatabaseSqlite' => 'includes/db/DatabaseSqlite.php',
|
||||
|
|
|
|||
|
|
@ -717,7 +717,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
|
|||
/**
|
||||
* Given a DB type, construct the name of the appropriate child class of
|
||||
* DatabaseBase. This is designed to replace all of the manual stuff like:
|
||||
* $class = 'Database' . ucfirst( strtolower( $type ) );
|
||||
* $class = 'Database' . ucfirst( strtolower( $dbType ) );
|
||||
* as well as validate against the canonical list of DB types we have
|
||||
*
|
||||
* This factory function is mostly useful for when you need to connect to a
|
||||
|
|
@ -732,17 +732,43 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
|
|||
*
|
||||
* @param string $dbType A possible DB type
|
||||
* @param array $p An array of options to pass to the constructor.
|
||||
* Valid options are: host, user, password, dbname, flags, tablePrefix
|
||||
* Valid options are: host, user, password, dbname, flags, tablePrefix, driver
|
||||
* @return DatabaseBase subclass or null
|
||||
*/
|
||||
final public static function factory( $dbType, $p = array() ) {
|
||||
$canonicalDBTypes = array(
|
||||
'mysql', 'postgres', 'sqlite', 'oracle', 'mssql'
|
||||
'mysql' => array( 'mysqli', 'mysql' ),
|
||||
'postgres' => array(),
|
||||
'sqlite' => array(),
|
||||
'oracle' => array(),
|
||||
'mssql' => array(),
|
||||
);
|
||||
$dbType = strtolower( $dbType );
|
||||
$class = 'Database' . ucfirst( $dbType );
|
||||
|
||||
if ( in_array( $dbType, $canonicalDBTypes ) || ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) ) {
|
||||
$dbType = strtolower( $dbType );
|
||||
if ( isset( $canonicalDBTypes[$dbType] ) && $canonicalDBTypes[$dbType] ) {
|
||||
$driver = isset( $p['driver'] ) ? $p['driver'] : null;
|
||||
$possibleDrivers = $canonicalDBTypes[$dbType];
|
||||
if ( $driver ) {
|
||||
if ( in_array( $driver, $possibleDrivers ) ) {
|
||||
$dbType = $driver;
|
||||
} else {
|
||||
throw new MWException( __METHOD__ .
|
||||
" cannot construct Database with type '$dbType' and driver '$driver'" );
|
||||
}
|
||||
} else {
|
||||
foreach ( $possibleDrivers as $posDriver ) {
|
||||
if ( extension_loaded( $posDriver ) ) {
|
||||
$dbType = $posDriver;
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw new MWException( __METHOD__ .
|
||||
" no viable database extension found for type '$dbType'" );
|
||||
}
|
||||
}
|
||||
|
||||
$class = 'Database' . ucfirst( $dbType );
|
||||
if ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) {
|
||||
return new $class(
|
||||
isset( $p['host'] ) ? $p['host'] : false,
|
||||
isset( $p['user'] ) ? $p['user'] : false,
|
||||
|
|
|
|||
|
|
@ -1006,7 +1006,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
|
|||
*/
|
||||
class MySQLField implements Field {
|
||||
private $name, $tablename, $default, $max_length, $nullable,
|
||||
$is_pk, $is_unique, $is_multiple, $is_key, $type;
|
||||
$is_pk, $is_unique, $is_multiple, $is_key, $type, $binary;
|
||||
|
||||
function __construct( $info ) {
|
||||
$this->name = $info->name;
|
||||
|
|
@ -1019,6 +1019,7 @@ class MySQLField implements Field {
|
|||
$this->is_multiple = $info->multiple_key;
|
||||
$this->is_key = ( $this->is_pk || $this->is_unique || $this->is_multiple );
|
||||
$this->type = $info->type;
|
||||
$this->binary = isset( $info->binary ) ? $info->binary : false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1066,6 +1067,10 @@ class MySQLField implements Field {
|
|||
function isMultipleKey() {
|
||||
return $this->is_multiple;
|
||||
}
|
||||
|
||||
function isBinary() {
|
||||
return $this->binary;
|
||||
}
|
||||
}
|
||||
|
||||
class MySQLMasterPos implements DBMasterPos {
|
||||
|
|
|
|||
194
includes/db/DatabaseMysqli.php
Normal file
194
includes/db/DatabaseMysqli.php
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
<?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 {
|
||||
|
||||
/**
|
||||
* @param $sql string
|
||||
* @return resource
|
||||
*/
|
||||
protected function doQuery( $sql ) {
|
||||
if ( $this->bufferResults() ) {
|
||||
$ret = $this->mConn->query( $sql );
|
||||
} else {
|
||||
$ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT );
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
protected function mysqlConnect( $realServer ) {
|
||||
# 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();
|
||||
$numAttempts = 2;
|
||||
|
||||
for ( $i = 0; $i < $numAttempts; $i++ ) {
|
||||
if ( $i > 1 ) {
|
||||
usleep( 1000 );
|
||||
}
|
||||
if ( $mysqli->real_connect( $realServer, $this->mUser,
|
||||
$this->mPassword, $this->mDBname, null, null, $connFlags ) )
|
||||
{
|
||||
return $mysqli;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $db
|
||||
* @return bool
|
||||
*/
|
||||
function selectDB( $db ) {
|
||||
$this->mDBname = $db;
|
||||
return $this->mConn->select_db( $db );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function getServerVersion() {
|
||||
return $this->mConn->server_info;
|
||||
}
|
||||
|
||||
protected function mysqlFreeResult( $res ) {
|
||||
$res->free_result();
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function mysqlFetchObject( $res ) {
|
||||
$object = $res->fetch_object();
|
||||
if ( $object === null ) {
|
||||
return false;
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
|
||||
protected function mysqlFetchArray( $res ) {
|
||||
$array = $res->fetch_array();
|
||||
if ( $array === null ) {
|
||||
return false;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
protected function mysqlNumRows( $res ) {
|
||||
return $res->num_rows;
|
||||
}
|
||||
|
||||
protected function mysqlNumFields( $res ) {
|
||||
return $res->field_count;
|
||||
}
|
||||
|
||||
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;
|
||||
return $field;
|
||||
}
|
||||
|
||||
protected function mysqlFieldName( $res, $n ) {
|
||||
$field = $res->fetch_field_direct( $n );
|
||||
return $field->name;
|
||||
}
|
||||
|
||||
protected function mysqlDataSeek( $res, $row ) {
|
||||
return $res->data_seek( $row );
|
||||
}
|
||||
|
||||
protected function mysqlError( $conn = null ) {
|
||||
if ($conn === null) {
|
||||
return mysqli_connect_error();
|
||||
} else {
|
||||
return $conn->error;
|
||||
}
|
||||
}
|
||||
|
||||
protected function mysqlRealEscapeString( $s ) {
|
||||
return $this->mConn->real_escape_string( $s );
|
||||
}
|
||||
|
||||
protected function mysqlPing() {
|
||||
return $this->mConn->ping();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -247,11 +247,8 @@ class MysqlUpdater extends DatabaseUpdater {
|
|||
return true;
|
||||
}
|
||||
|
||||
$tableName = $this->db->tableName( $table );
|
||||
$res = $this->db->query( "SELECT $field FROM $tableName LIMIT 0", __METHOD__ );
|
||||
$flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
|
||||
|
||||
if ( in_array( 'binary', $flags ) ) {
|
||||
$fieldInfo = $this->db->fieldInfo( $table, $field );
|
||||
if ( $fieldInfo->isBinary() ) {
|
||||
$this->output( "...$table table has correct $field encoding.\n" );
|
||||
} else {
|
||||
$this->applyPatch( $patchFile, false, "Fixing $field encoding on $table table" );
|
||||
|
|
|
|||
Loading…
Reference in a new issue