2009-06-15 18:39:41 +00:00
|
|
|
<?php
|
2010-08-01 21:13:44 +00:00
|
|
|
/**
|
|
|
|
|
* This is the MySQL database abstraction layer.
|
|
|
|
|
*
|
2012-04-26 08:47:10 +00:00
|
|
|
* 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
|
|
|
|
|
*
|
2010-08-01 21:13:44 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Database
|
|
|
|
|
*/
|
|
|
|
|
|
2009-06-15 18:39:41 +00:00
|
|
|
/**
|
2013-05-03 01:39:16 +00:00
|
|
|
* Database abstraction object for PHP extension mysql.
|
2009-06-15 18:39:41 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Database
|
|
|
|
|
* @see Database
|
|
|
|
|
*/
|
2013-05-03 01:39:16 +00:00
|
|
|
class DatabaseMysql extends DatabaseMysqlBase {
|
2011-11-29 21:04:20 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $sql
|
|
|
|
|
* @return resource False on error
|
2011-11-29 21:04:20 +00:00
|
|
|
*/
|
2011-06-20 12:09:22 +00:00
|
|
|
protected function doQuery( $sql ) {
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( $this->bufferResults() ) {
|
2009-06-15 18:39:41 +00:00
|
|
|
$ret = mysql_query( $sql, $this->mConn );
|
|
|
|
|
} else {
|
|
|
|
|
$ret = mysql_unbuffered_query( $sql, $this->mConn );
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2009-06-15 18:39:41 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 01:54:51 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $realServer
|
|
|
|
|
* @return bool|resource MySQL Database connection or false on failure to connect
|
|
|
|
|
* @throws DBConnectionError
|
|
|
|
|
*/
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlConnect( $realServer ) {
|
2009-06-15 18:39:41 +00:00
|
|
|
# Fail now
|
|
|
|
|
# Otherwise we get a suppressed fatal error, which is very hard to track down
|
2013-10-09 16:46:57 +00:00
|
|
|
if ( !extension_loaded( 'mysql' ) ) {
|
2013-11-20 10:13:51 +00:00
|
|
|
throw new DBConnectionError(
|
|
|
|
|
$this,
|
|
|
|
|
"MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
|
|
|
|
|
);
|
2009-06-15 18:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-15 20:44:41 +00:00
|
|
|
$connFlags = 0;
|
|
|
|
|
if ( $this->mFlags & DBO_SSL ) {
|
|
|
|
|
$connFlags |= MYSQL_CLIENT_SSL;
|
|
|
|
|
}
|
|
|
|
|
if ( $this->mFlags & DBO_COMPRESS ) {
|
|
|
|
|
$connFlags |= MYSQL_CLIENT_COMPRESS;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-15 18:39:41 +00:00
|
|
|
if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
|
|
|
|
|
$numAttempts = 2;
|
|
|
|
|
} else {
|
|
|
|
|
$numAttempts = 1;
|
|
|
|
|
}
|
2013-05-03 01:39:16 +00:00
|
|
|
|
|
|
|
|
$conn = false;
|
|
|
|
|
|
|
|
|
|
for ( $i = 0; $i < $numAttempts && !$conn; $i++ ) {
|
2009-06-15 18:39:41 +00:00
|
|
|
if ( $i > 1 ) {
|
|
|
|
|
usleep( 1000 );
|
|
|
|
|
}
|
|
|
|
|
if ( $this->mFlags & DBO_PERSISTENT ) {
|
2013-05-03 01:39:16 +00:00
|
|
|
$conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags );
|
2009-06-15 18:39:41 +00:00
|
|
|
} else {
|
|
|
|
|
# Create a new connection...
|
2013-05-03 01:39:16 +00:00
|
|
|
$conn = mysql_connect( $realServer, $this->mUser, $this->mPassword, true, $connFlags );
|
2009-06-15 18:39:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-05-19 02:12:59 +00:00
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
return $conn;
|
2009-06-15 18:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
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 ( function_exists( 'mysql_set_charset' ) ) {
|
|
|
|
|
return mysql_set_charset( $charset, $this->mConn );
|
|
|
|
|
} else {
|
|
|
|
|
return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-16 17:58:50 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2012-02-28 14:42:08 +00:00
|
|
|
protected function closeConnection() {
|
|
|
|
|
return mysql_close( $this->mConn );
|
2009-06-15 18:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-16 17:58:50 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
function insertId() {
|
|
|
|
|
return mysql_insert_id( $this->mConn );
|
|
|
|
|
}
|
2009-06-15 18:39:41 +00:00
|
|
|
|
2011-11-29 21:04:20 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2009-06-15 18:39:41 +00:00
|
|
|
function lastErrno() {
|
|
|
|
|
if ( $this->mConn ) {
|
|
|
|
|
return mysql_errno( $this->mConn );
|
|
|
|
|
} else {
|
|
|
|
|
return mysql_errno();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-16 17:58:50 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
function affectedRows() {
|
|
|
|
|
return mysql_affected_rows( $this->mConn );
|
|
|
|
|
}
|
2010-05-19 02:12:59 +00:00
|
|
|
|
2011-09-16 17:58:50 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $db
|
2011-09-16 17:58:50 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2009-06-15 18:39:41 +00:00
|
|
|
function selectDB( $db ) {
|
|
|
|
|
$this->mDBname = $db;
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2009-06-15 18:39:41 +00:00
|
|
|
return mysql_select_db( $db, $this->mConn );
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlFreeResult( $res ) {
|
|
|
|
|
return mysql_free_result( $res );
|
2011-11-24 19:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlFetchObject( $res ) {
|
|
|
|
|
return mysql_fetch_object( $res );
|
* Converted BagOStuff.php from the style of memcached-client.php to the standard MediaWiki style, including camel case, using protected visibility instead of initial underscore, abstract functions instead of stubs, stylize.php.
* In SqlBagOStuff, ignore errors due to a read-only database, per my comments on CR r42796. Same for LocalisationCache.
* Merged SqlBagOStuff and MediaWikiBagOStuff, that proved to be an awkward and unnecessary generalisation. Use the standard quoting wrapper functions instead of $db->query().
* Implemented atomic incr() and decr() functions for SqlBagOStuff.
* Made incr() and decr() generally work roughly the same as it does in memcached, respecting negative steps instead of ignoring such operations. This allows decr() to be implemented in terms of incr().
* Per bug 11533, in MessageCache.php, don't retry 20 times on a cache failure, that's really memcached-specific and won't be useful for other cache types. It's not really very useful for memcached either.
* Moved MySQL-specific implementations of wasDeadlock() and wasErrorReissuable() to DatabaseMysql.
* Briefly tested page views with $wgReadOnly=read_only=1, fixed an error from Article::viewUpdates(). A CentralAuth fix will be in a subsequent commit.
2009-08-15 03:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlFetchArray( $res ) {
|
|
|
|
|
return mysql_fetch_array( $res );
|
2011-11-23 23:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlNumRows( $res ) {
|
|
|
|
|
return mysql_num_rows( $res );
|
* Converted BagOStuff.php from the style of memcached-client.php to the standard MediaWiki style, including camel case, using protected visibility instead of initial underscore, abstract functions instead of stubs, stylize.php.
* In SqlBagOStuff, ignore errors due to a read-only database, per my comments on CR r42796. Same for LocalisationCache.
* Merged SqlBagOStuff and MediaWikiBagOStuff, that proved to be an awkward and unnecessary generalisation. Use the standard quoting wrapper functions instead of $db->query().
* Implemented atomic incr() and decr() functions for SqlBagOStuff.
* Made incr() and decr() generally work roughly the same as it does in memcached, respecting negative steps instead of ignoring such operations. This allows decr() to be implemented in terms of incr().
* Per bug 11533, in MessageCache.php, don't retry 20 times on a cache failure, that's really memcached-specific and won't be useful for other cache types. It's not really very useful for memcached either.
* Moved MySQL-specific implementations of wasDeadlock() and wasErrorReissuable() to DatabaseMysql.
* Briefly tested page views with $wgReadOnly=read_only=1, fixed an error from Article::viewUpdates(). A CentralAuth fix will be in a subsequent commit.
2009-08-15 03:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlNumFields( $res ) {
|
|
|
|
|
return mysql_num_fields( $res );
|
2010-11-21 19:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlFetchField( $res, $n ) {
|
|
|
|
|
return mysql_fetch_field( $res, $n );
|
2010-11-21 19:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlFieldName( $res, $n ) {
|
|
|
|
|
return mysql_field_name( $res, $n );
|
2010-11-21 19:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
2013-12-07 20:08:47 +00:00
|
|
|
protected function mysqlFieldType( $res, $n ) {
|
|
|
|
|
return mysql_field_type( $res, $n );
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlDataSeek( $res, $row ) {
|
|
|
|
|
return mysql_data_seek( $res, $row );
|
2010-11-21 19:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlError( $conn = null ) {
|
2013-06-16 22:56:41 +00:00
|
|
|
return ( $conn !== null ) ? mysql_error( $conn ) : mysql_error(); // avoid warning
|
2010-11-21 19:56:51 +00:00
|
|
|
}
|
2009-06-15 18:39:41 +00:00
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlRealEscapeString( $s ) {
|
|
|
|
|
return mysql_real_escape_string( $s, $this->mConn );
|
2009-06-15 18:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-03 01:39:16 +00:00
|
|
|
protected function mysqlPing() {
|
|
|
|
|
return mysql_ping( $this->mConn );
|
2009-06-15 18:39:41 +00:00
|
|
|
}
|
|
|
|
|
}
|