2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @defgroup Database Database
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Database
|
2010-07-10 10:15:59 +00:00
|
|
|
* This file deals with database interface functions
|
2004-09-02 23:28:24 +00:00
|
|
|
* and query specifics/optimisations
|
|
|
|
|
*/
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/** Number of times to re-try an operation in case of deadlock */
|
2004-08-22 17:24:50 +00:00
|
|
|
define( 'DEADLOCK_TRIES', 4 );
|
2004-09-03 16:36:46 +00:00
|
|
|
/** Minimum time to wait before retry, in microseconds */
|
2004-08-22 17:24:50 +00:00
|
|
|
define( 'DEADLOCK_DELAY_MIN', 500000 );
|
2004-09-03 16:36:46 +00:00
|
|
|
/** Maximum time to wait before retry */
|
2004-08-22 17:24:50 +00:00
|
|
|
define( 'DEADLOCK_DELAY_MAX', 1500000 );
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2010-09-08 17:48:11 +00:00
|
|
|
/**
|
|
|
|
|
* Base interface for all DBMS-specific code. At a bare minimum, all of the
|
|
|
|
|
* following must be implemented to support MediaWiki
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Database
|
|
|
|
|
*/
|
|
|
|
|
interface DatabaseType {
|
|
|
|
|
/**
|
|
|
|
|
* Get the type of the DBMS, as it appears in $wgDBtype.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function getType();
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Open a connection to the database. Usually aborts on failure
|
|
|
|
|
*
|
|
|
|
|
* @param $server String: database server host
|
|
|
|
|
* @param $user String: database user name
|
|
|
|
|
* @param $password String: database user password
|
|
|
|
|
* @param $dbName String: database name
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws DBConnectionError
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function open( $server, $user, $password, $dbName );
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The DBMS-dependent part of query()
|
2011-05-17 22:03:20 +00:00
|
|
|
* @todo FIXME: Make this private someday
|
2010-09-08 17:48:11 +00:00
|
|
|
*
|
|
|
|
|
* @param $sql String: SQL query.
|
|
|
|
|
* @return Result object to feed to fetchObject, fetchRow, ...; or false on failure
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function doQuery( $sql );
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the next row from the given result object, in object form.
|
|
|
|
|
* Fields can be retrieved with $row->fieldname, with fields acting like
|
|
|
|
|
* member variables.
|
|
|
|
|
*
|
|
|
|
|
* @param $res SQL result object as returned from DatabaseBase::query(), etc.
|
|
|
|
|
* @return Row object
|
|
|
|
|
* @throws DBUnexpectedError Thrown if the database returns an error
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function fetchObject( $res );
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the next row from the given result object, in associative array
|
|
|
|
|
* form. Fields are retrieved with $row['fieldname'].
|
|
|
|
|
*
|
2011-05-25 18:58:02 +00:00
|
|
|
* @param $res ResultWrapper result object as returned from DatabaseBase::query(), etc.
|
2010-09-08 17:48:11 +00:00
|
|
|
* @return Row object
|
|
|
|
|
* @throws DBUnexpectedError Thrown if the database returns an error
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function fetchRow( $res );
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the number of rows in a result object
|
|
|
|
|
*
|
|
|
|
|
* @param $res Mixed: A SQL result
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function numRows( $res );
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the number of fields in a result object
|
|
|
|
|
* @see http://www.php.net/mysql_num_fields
|
|
|
|
|
*
|
|
|
|
|
* @param $res Mixed: A SQL result
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function numFields( $res );
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a field name in a result object
|
|
|
|
|
* @see http://www.php.net/mysql_field_name
|
|
|
|
|
*
|
|
|
|
|
* @param $res Mixed: A SQL result
|
|
|
|
|
* @param $n Integer
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function fieldName( $res, $n );
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the inserted value of an auto-increment row
|
|
|
|
|
*
|
|
|
|
|
* The value inserted should be fetched from nextSequenceValue()
|
|
|
|
|
*
|
|
|
|
|
* Example:
|
|
|
|
|
* $id = $dbw->nextSequenceValue('page_page_id_seq');
|
|
|
|
|
* $dbw->insert('page',array('page_id' => $id));
|
|
|
|
|
* $id = $dbw->insertId();
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function insertId();
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Change the position of the cursor in a result object
|
|
|
|
|
* @see http://www.php.net/mysql_data_seek
|
|
|
|
|
*
|
|
|
|
|
* @param $res Mixed: A SQL result
|
|
|
|
|
* @param $row Mixed: Either MySQL row or ResultWrapper
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function dataSeek( $res, $row );
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the last error number
|
|
|
|
|
* @see http://www.php.net/mysql_errno
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function lastErrno();
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a description of the last error
|
|
|
|
|
* @see http://www.php.net/mysql_error
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function lastError();
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* mysql_fetch_field() wrapper
|
|
|
|
|
* Returns false if the field doesn't exist
|
|
|
|
|
*
|
|
|
|
|
* @param $table string: table name
|
|
|
|
|
* @param $field string: field name
|
2011-02-19 00:44:38 +00:00
|
|
|
*
|
|
|
|
|
* @return Field
|
2010-09-08 17:48:11 +00:00
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function fieldInfo( $table, $field );
|
2010-09-08 17:48:11 +00:00
|
|
|
|
2010-11-23 11:14:48 +00:00
|
|
|
/**
|
|
|
|
|
* Get information about an index into an object
|
|
|
|
|
* @param $table string: Table name
|
|
|
|
|
* @param $index string: Index name
|
|
|
|
|
* @param $fname string: Calling function name
|
|
|
|
|
* @return Mixed: Database-specific index description class or false if the index does not exist
|
|
|
|
|
*/
|
2010-11-23 14:08:46 +00:00
|
|
|
function indexInfo( $table, $index, $fname = 'Database::indexInfo' );
|
2010-11-23 11:14:48 +00:00
|
|
|
|
2010-09-08 17:48:11 +00:00
|
|
|
/**
|
|
|
|
|
* Get the number of rows affected by the last write query
|
|
|
|
|
* @see http://www.php.net/mysql_affected_rows
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function affectedRows();
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper for addslashes()
|
|
|
|
|
*
|
|
|
|
|
* @param $s string: to be slashed.
|
|
|
|
|
* @return string: slashed string.
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function strencode( $s );
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a wikitext link to the DB's website, e.g.,
|
2010-10-05 15:10:14 +00:00
|
|
|
* return "[http://www.mysql.com/ MySQL]";
|
2010-09-08 17:48:11 +00:00
|
|
|
* Should at least contain plain text, if for some reason
|
|
|
|
|
* your database has no website.
|
|
|
|
|
*
|
|
|
|
|
* @return string: wikitext of a link to the server software's web site
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
static function getSoftwareLink();
|
2010-09-08 17:48:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A string describing the current software version, like from
|
2010-09-23 19:36:06 +00:00
|
|
|
* mysql_get_server_info().
|
2010-09-08 17:48:11 +00:00
|
|
|
*
|
2010-09-23 19:36:06 +00:00
|
|
|
* @return string: Version information from the database server.
|
2010-09-08 17:48:11 +00:00
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function getServerVersion();
|
2010-09-23 19:36:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A string describing the current software version, and possibly
|
|
|
|
|
* other details in a user-friendly way. Will be listed on Special:Version, etc.
|
|
|
|
|
* Use getServerVersion() to get machine-friendly information.
|
|
|
|
|
*
|
|
|
|
|
* @return string: Version information from the database server
|
|
|
|
|
*/
|
2010-12-14 23:43:03 +00:00
|
|
|
function getServerInfo();
|
2010-09-08 17:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
2007-04-04 05:22:37 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Database abstraction object
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Database
|
2007-04-04 05:22:37 +00:00
|
|
|
*/
|
2010-08-26 23:10:11 +00:00
|
|
|
abstract class DatabaseBase implements DatabaseType {
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2008-03-30 09:48:15 +00:00
|
|
|
# Variables
|
2010-09-04 13:48:16 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
protected $mLastQuery = '';
|
2009-02-17 14:06:42 +00:00
|
|
|
protected $mDoneWrites = false;
|
2008-06-04 01:44:36 +00:00
|
|
|
protected $mPHPError = false;
|
2008-03-30 09:48:15 +00:00
|
|
|
|
2011-05-26 19:21:50 +00:00
|
|
|
protected $mServer, $mUser, $mPassword, $mDBname;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var DatabaseBase
|
|
|
|
|
*/
|
|
|
|
|
protected $mConn = null;
|
2008-07-08 10:41:08 +00:00
|
|
|
protected $mOpened = false;
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
protected $mTablePrefix;
|
|
|
|
|
protected $mFlags;
|
|
|
|
|
protected $mTrxLevel = 0;
|
|
|
|
|
protected $mErrorCount = 0;
|
|
|
|
|
protected $mLBInfo = array();
|
|
|
|
|
protected $mFakeSlaveLag = null, $mFakeMaster = false;
|
2009-05-27 06:10:48 +00:00
|
|
|
protected $mDefaultBigSelects = null;
|
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database.
* Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade.
* Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this.
* Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used.
* Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes.
* Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database.
* In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary.
* On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added.
* Fixed several instances of inappropriate call-by-reference.
* Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler.
* Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892.
* In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString().
* Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging.
* MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
|
|
|
protected $mSchemaVars = false;
|
2008-03-30 09:48:15 +00:00
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2008-03-30 09:48:15 +00:00
|
|
|
# Accessors
|
2010-09-04 13:48:16 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2008-03-30 09:48:15 +00:00
|
|
|
# These optionally set a variable and return the previous state
|
|
|
|
|
|
2010-09-23 19:36:06 +00:00
|
|
|
/**
|
|
|
|
|
* A string describing the current software version, and possibly
|
|
|
|
|
* other details in a user-friendly way. Will be listed on Special:Version, etc.
|
|
|
|
|
* Use getServerVersion() to get machine-friendly information.
|
|
|
|
|
*
|
|
|
|
|
* @return string: Version information from the database server
|
|
|
|
|
*/
|
|
|
|
|
public function getServerInfo() {
|
|
|
|
|
return $this->getServerVersion();
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Boolean, controls output of large amounts of debug information
|
|
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
function debug( $debug = null ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return wfSetBit( $this->mFlags, DBO_DEBUG, $debug );
|
2005-08-02 13:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Turns buffering of SQL result sets on (true) or off (false).
|
|
|
|
|
* Default is "on" and it should not be changed without good reasons.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
function bufferResults( $buffer = null ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( is_null( $buffer ) ) {
|
|
|
|
|
return !(bool)( $this->mFlags & DBO_NOBUFFER );
|
|
|
|
|
} else {
|
|
|
|
|
return !wfSetBit( $this->mFlags, DBO_NOBUFFER, !$buffer );
|
|
|
|
|
}
|
2007-09-23 19:54:56 +00:00
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Turns on (false) or off (true) the automatic generation and sending
|
|
|
|
|
* of a "we're sorry, but there has been a database error" page on
|
|
|
|
|
* database errors. Default is on (false). When turned off, the
|
|
|
|
|
* code should use lastErrno() and lastError() to handle the
|
|
|
|
|
* situation as appropriate.
|
|
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
function ignoreErrors( $ignoreErrors = null ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return wfSetBit( $this->mFlags, DBO_IGNORE, $ignoreErrors );
|
2007-09-23 19:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* The current depth of nested transactions
|
|
|
|
|
* @param $level Integer: , default NULL.
|
|
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
function trxLevel( $level = null ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return wfSetVar( $this->mTrxLevel, $level );
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Number of errors logged, only useful when errors are ignored
|
|
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
function errorCount( $count = null ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return wfSetVar( $this->mErrorCount, $count );
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
function tablePrefix( $prefix = null ) {
|
2010-12-30 17:30:35 +00:00
|
|
|
return wfSetVar( $this->mTablePrefix, $prefix, true );
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Properties passed down from the server info array of the load balancer
|
|
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
function getLBInfo( $name = null ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( is_null( $name ) ) {
|
|
|
|
|
return $this->mLBInfo;
|
|
|
|
|
} else {
|
|
|
|
|
if ( array_key_exists( $name, $this->mLBInfo ) ) {
|
|
|
|
|
return $this->mLBInfo[$name];
|
|
|
|
|
} else {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $value
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
function setLBInfo( $name, $value = null ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( is_null( $value ) ) {
|
|
|
|
|
$this->mLBInfo = $name;
|
|
|
|
|
} else {
|
|
|
|
|
$this->mLBInfo[$name] = $value;
|
|
|
|
|
}
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Set lag time in seconds for a fake slave
|
|
|
|
|
*/
|
|
|
|
|
function setFakeSlaveLag( $lag ) {
|
|
|
|
|
$this->mFakeSlaveLag = $lag;
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Make this connection a fake master
|
|
|
|
|
*/
|
|
|
|
|
function setFakeMaster( $enabled = true ) {
|
|
|
|
|
$this->mFakeMaster = $enabled;
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if this database supports (and uses) cascading deletes
|
|
|
|
|
*/
|
|
|
|
|
function cascadingDeletes() {
|
|
|
|
|
return false;
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if this database supports (and uses) triggers (e.g. on the page table)
|
|
|
|
|
*/
|
|
|
|
|
function cleanupTriggers() {
|
|
|
|
|
return false;
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if this database is strict about what can be put into an IP field.
|
|
|
|
|
* Specifically, it uses a NULL value instead of an empty string.
|
|
|
|
|
*/
|
|
|
|
|
function strictIPs() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2006-06-06 23:07:26 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if this database uses timestamps rather than integers
|
|
|
|
|
*/
|
|
|
|
|
function realTimestamps() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2006-06-06 23:07:26 +00:00
|
|
|
|
|
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Returns true if this database does an implicit sort when doing GROUP BY
|
2006-06-06 23:07:26 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function implicitGroupby() {
|
|
|
|
|
return true;
|
2006-06-06 23:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if this database does an implicit order by when the column has an index
|
|
|
|
|
* For example: SELECT page_title FROM page LIMIT 1
|
|
|
|
|
*/
|
|
|
|
|
function implicitOrderby() {
|
|
|
|
|
return true;
|
2006-06-06 23:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
2009-07-20 02:20:15 +00:00
|
|
|
/**
|
2010-03-06 18:14:11 +00:00
|
|
|
* Returns true if this database requires that SELECT DISTINCT queries require that all
|
2010-09-04 01:06:34 +00:00
|
|
|
ORDER BY expressions occur in the SELECT list per the SQL92 standard
|
2009-07-20 02:20:15 +00:00
|
|
|
*/
|
|
|
|
|
function standardSelectDistinct() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if this database can do a native search on IP columns
|
|
|
|
|
* e.g. this works as expected: .. WHERE rc_ip = '127.42.12.102/32';
|
|
|
|
|
*/
|
|
|
|
|
function searchableIPs() {
|
2006-06-06 23:07:26 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if this database can use functional indexes
|
|
|
|
|
*/
|
|
|
|
|
function functionalIndexes() {
|
2006-06-06 23:07:26 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-05 03:41:31 +00:00
|
|
|
/**
|
2010-08-25 01:24:47 +00:00
|
|
|
* Return the last query that went through DatabaseBase::query()
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return String
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2011-05-25 18:58:02 +00:00
|
|
|
function lastQuery() {
|
|
|
|
|
return $this->mLastQuery;
|
|
|
|
|
}
|
2009-02-17 14:06:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if the connection may have been used for write queries.
|
|
|
|
|
* Should return true if unsure.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2009-02-17 14:06:42 +00:00
|
|
|
*/
|
2011-05-25 18:58:02 +00:00
|
|
|
function doneWrites() {
|
|
|
|
|
return $this->mDoneWrites;
|
|
|
|
|
}
|
2009-02-17 14:06:42 +00:00
|
|
|
|
2008-09-05 03:41:31 +00:00
|
|
|
/**
|
2008-09-08 14:06:04 +00:00
|
|
|
* Is a connection to the database open?
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return Boolean
|
2008-09-05 03:41:31 +00:00
|
|
|
*/
|
2011-05-25 18:58:02 +00:00
|
|
|
function isOpen() {
|
|
|
|
|
return $this->mOpened;
|
|
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
2009-08-24 08:54:28 +00:00
|
|
|
/**
|
|
|
|
|
* Set a flag for this connection
|
|
|
|
|
*
|
|
|
|
|
* @param $flag Integer: DBO_* constants from Defines.php:
|
2010-10-05 15:10:14 +00:00
|
|
|
* - DBO_DEBUG: output some debug info (same as debug())
|
|
|
|
|
* - DBO_NOBUFFER: don't buffer results (inverse of bufferResults())
|
|
|
|
|
* - DBO_IGNORE: ignore errors (same as ignoreErrors())
|
|
|
|
|
* - DBO_TRX: automatically start transactions
|
|
|
|
|
* - DBO_DEFAULT: automatically sets DBO_TRX if not in command line mode
|
|
|
|
|
* and removes it in command line mode
|
|
|
|
|
* - DBO_PERSISTENT: use persistant database connection
|
2009-08-24 08:54:28 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function setFlag( $flag ) {
|
|
|
|
|
$this->mFlags |= $flag;
|
2006-08-02 17:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-24 08:54:28 +00:00
|
|
|
/**
|
|
|
|
|
* Clear a flag for this connection
|
|
|
|
|
*
|
|
|
|
|
* @param $flag: same as setFlag()'s $flag param
|
|
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function clearFlag( $flag ) {
|
|
|
|
|
$this->mFlags &= ~$flag;
|
2006-06-06 23:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-24 08:54:28 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a boolean whether the flag $flag is set for this connection
|
|
|
|
|
*
|
|
|
|
|
* @param $flag: same as setFlag()'s $flag param
|
|
|
|
|
* @return Boolean
|
|
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function getFlag( $flag ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
return !!( $this->mFlags & $flag );
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
2006-06-06 23:07:26 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* General read-only accessor
|
|
|
|
|
*/
|
|
|
|
|
function getProperty( $name ) {
|
|
|
|
|
return $this->$name;
|
|
|
|
|
}
|
2006-06-06 23:07:26 +00:00
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2008-05-10 19:22:14 +00:00
|
|
|
function getWikiID() {
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( $this->mTablePrefix ) {
|
2008-05-10 19:22:14 +00:00
|
|
|
return "{$this->mDBname}-{$this->mTablePrefix}";
|
|
|
|
|
} else {
|
|
|
|
|
return $this->mDBname;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-01 19:03:56 +00:00
|
|
|
/**
|
|
|
|
|
* Return a path to the DBMS-specific schema, otherwise default to tables.sql
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2010-09-01 19:03:56 +00:00
|
|
|
*/
|
|
|
|
|
public function getSchema() {
|
|
|
|
|
global $IP;
|
|
|
|
|
if ( file_exists( "$IP/maintenance/" . $this->getType() . "/tables.sql" ) ) {
|
|
|
|
|
return "$IP/maintenance/" . $this->getType() . "/tables.sql";
|
|
|
|
|
} else {
|
|
|
|
|
return "$IP/maintenance/tables.sql";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2008-03-30 09:48:15 +00:00
|
|
|
# Other functions
|
2010-09-04 13:48:16 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2006-06-06 23:07:26 +00:00
|
|
|
|
2008-11-29 18:50:39 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Constructor.
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $server String: database server host
|
|
|
|
|
* @param $user String: database user name
|
|
|
|
|
* @param $password String: database user password
|
|
|
|
|
* @param $dbName String: database name
|
2008-03-30 09:48:15 +00:00
|
|
|
* @param $flags
|
|
|
|
|
* @param $tablePrefix String: database table prefixes. By default use the prefix gave in LocalSettings.php
|
|
|
|
|
*/
|
|
|
|
|
function __construct( $server = false, $user = false, $password = false, $dbName = false,
|
2010-10-24 21:27:33 +00:00
|
|
|
$flags = 0, $tablePrefix = 'get from global'
|
2010-09-04 13:48:16 +00:00
|
|
|
) {
|
2011-04-03 19:49:50 +00:00
|
|
|
global $wgDBprefix, $wgCommandLineMode;
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->mFlags = $flags;
|
2006-06-06 23:07:26 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $this->mFlags & DBO_DEFAULT ) {
|
|
|
|
|
if ( $wgCommandLineMode ) {
|
|
|
|
|
$this->mFlags &= ~DBO_TRX;
|
2006-06-06 23:07:26 +00:00
|
|
|
} else {
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->mFlags |= DBO_TRX;
|
2006-06-06 23:07:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/** Get the default table prefix*/
|
|
|
|
|
if ( $tablePrefix == 'get from global' ) {
|
|
|
|
|
$this->mTablePrefix = $wgDBprefix;
|
2006-06-06 23:07:26 +00:00
|
|
|
} else {
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->mTablePrefix = $tablePrefix;
|
2006-06-06 23:07:26 +00:00
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
2011-03-21 21:20:11 +00:00
|
|
|
if ( $user ) {
|
2008-09-21 06:42:46 +00:00
|
|
|
$this->open( $server, $user, $password, $dbName );
|
2006-06-06 23:07:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-08-02 17:40:09 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
2009-06-12 17:59:04 +00:00
|
|
|
* Same as new DatabaseMysql( ... ), kept for backward compatibility
|
2011-05-06 21:09:34 +00:00
|
|
|
* @deprecated since 1.17
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return DatabaseMysql
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2010-10-24 21:27:33 +00:00
|
|
|
static function newFromParams( $server, $user, $password, $dbName, $flags = 0 ) {
|
2010-04-28 18:12:42 +00:00
|
|
|
wfDeprecated( __METHOD__ );
|
2010-10-24 21:27:33 +00:00
|
|
|
return new DatabaseMysql( $server, $user, $password, $dbName, $flags );
|
2006-06-06 23:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-07 22:32:52 +00:00
|
|
|
/**
|
|
|
|
|
* 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 ) );
|
|
|
|
|
* as well as validate against the canonical list of DB types we have
|
|
|
|
|
*
|
2011-01-24 16:31:36 +00:00
|
|
|
* This factory function is mostly useful for when you need to connect to a
|
|
|
|
|
* database other than the MediaWiki default (such as for external auth,
|
|
|
|
|
* an extension, et cetera). Do not use this to connect to the MediaWiki
|
|
|
|
|
* database. Example uses in core:
|
|
|
|
|
* @see LoadBalancer::reallyOpenConnection()
|
|
|
|
|
* @see ExternalUser_MediaWiki::initFromCond()
|
|
|
|
|
* @see ForeignDBRepo::getMasterDB()
|
|
|
|
|
* @see WebInstaller_DBConnect::execute()
|
|
|
|
|
*
|
2011-01-07 22:32:52 +00:00
|
|
|
* @param $dbType String A possible DB type
|
2011-01-24 16:31:36 +00:00
|
|
|
* @param $p Array An array of options to pass to the constructor.
|
|
|
|
|
* Valid options are: host, user, password, dbname, flags, tableprefix
|
2011-01-07 22:32:52 +00:00
|
|
|
* @return DatabaseBase subclass or null
|
|
|
|
|
*/
|
2011-01-24 16:31:36 +00:00
|
|
|
public final static function newFromType( $dbType, $p = array() ) {
|
2011-01-07 22:32:52 +00:00
|
|
|
$canonicalDBTypes = array(
|
|
|
|
|
'mysql', 'postgres', 'sqlite', 'oracle', 'mssql', 'ibm_db2'
|
|
|
|
|
);
|
|
|
|
|
$dbType = strtolower( $dbType );
|
2011-01-24 16:31:36 +00:00
|
|
|
|
2011-01-07 22:32:52 +00:00
|
|
|
if( in_array( $dbType, $canonicalDBTypes ) ) {
|
2011-01-24 16:31:36 +00:00
|
|
|
$class = 'Database' . ucfirst( $dbType );
|
|
|
|
|
return new $class(
|
|
|
|
|
isset( $p['host'] ) ? $p['host'] : false,
|
|
|
|
|
isset( $p['user'] ) ? $p['user'] : false,
|
|
|
|
|
isset( $p['password'] ) ? $p['password'] : false,
|
|
|
|
|
isset( $p['dbname'] ) ? $p['dbname'] : false,
|
|
|
|
|
isset( $p['flags'] ) ? $p['flags'] : 0,
|
|
|
|
|
isset( $p['tableprefix'] ) ? $p['tableprefix'] : 'get from global'
|
|
|
|
|
);
|
2011-01-07 22:32:52 +00:00
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-04 01:44:36 +00:00
|
|
|
protected function installErrorHandler() {
|
|
|
|
|
$this->mPHPError = false;
|
2008-10-06 00:45:18 +00:00
|
|
|
$this->htmlErrors = ini_set( 'html_errors', '0' );
|
2008-06-04 01:44:36 +00:00
|
|
|
set_error_handler( array( $this, 'connectionErrorHandler' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function restoreErrorHandler() {
|
|
|
|
|
restore_error_handler();
|
2008-10-06 00:45:18 +00:00
|
|
|
if ( $this->htmlErrors !== false ) {
|
|
|
|
|
ini_set( 'html_errors', $this->htmlErrors );
|
|
|
|
|
}
|
|
|
|
|
if ( $this->mPHPError ) {
|
|
|
|
|
$error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError );
|
|
|
|
|
$error = preg_replace( '!^.*?:(.*)$!', '$1', $error );
|
|
|
|
|
return $error;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-06-04 01:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
2010-10-05 15:10:14 +00:00
|
|
|
protected function connectionErrorHandler( $errno, $errstr ) {
|
2008-06-04 01:44:36 +00:00
|
|
|
$this->mPHPError = $errstr;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Closes a database connection.
|
|
|
|
|
* if it is open : commits any open transactions
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return Bool operation success. true if already closed.
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2009-06-16 20:22:11 +00:00
|
|
|
function close() {
|
|
|
|
|
# Stub, should probably be overridden
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2010-04-28 18:12:42 +00:00
|
|
|
* @param $error String: fallback error message, used if none is given by DB
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function reportConnectionError( $error = 'Unknown error' ) {
|
|
|
|
|
$myError = $this->lastError();
|
|
|
|
|
if ( $myError ) {
|
|
|
|
|
$error = $myError;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-10-24 20:48:48 +00:00
|
|
|
# New method
|
|
|
|
|
throw new DBConnectionError( $this, $error );
|
2004-07-24 07:24:04 +00:00
|
|
|
}
|
2004-01-10 16:44:31 +00:00
|
|
|
|
2009-02-17 14:06:42 +00:00
|
|
|
/**
|
|
|
|
|
* Determine whether a query writes to the DB.
|
|
|
|
|
* Should return true if unsure.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2009-02-17 14:06:42 +00:00
|
|
|
*/
|
|
|
|
|
function isWriteQuery( $sql ) {
|
2010-01-06 04:05:49 +00:00
|
|
|
return !preg_match( '/^(?:SELECT|BEGIN|COMMIT|SET|SHOW|\(SELECT)\b/i', $sql );
|
2009-02-17 14:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Usually aborts on failure. If errors are explicitly ignored, returns success.
|
|
|
|
|
*
|
2010-10-05 15:10:14 +00:00
|
|
|
* @param $sql String: SQL query
|
|
|
|
|
* @param $fname String: Name of the calling function, for profiling/SHOW PROCESSLIST
|
|
|
|
|
* comment (you can use __METHOD__ or add some extra info)
|
|
|
|
|
* @param $tempIgnore Boolean: Whether to avoid throwing an exception on errors...
|
|
|
|
|
* maybe best to catch the exception instead?
|
2010-12-02 19:33:15 +00:00
|
|
|
* @return boolean or ResultWrapper. true for a successful write query, ResultWrapper object for a successful read query,
|
2010-10-05 15:10:14 +00:00
|
|
|
* or false on failure if $tempIgnore set
|
2008-03-30 09:48:15 +00:00
|
|
|
* @throws DBQueryError Thrown when the database returns an error of any kind
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
public function query( $sql, $fname = '', $tempIgnore = false ) {
|
|
|
|
|
$isMaster = !is_null( $this->getLBInfo( 'master' ) );
|
2011-04-21 16:31:02 +00:00
|
|
|
if ( !Profiler::instance()->isStub() ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
# generalizeSQL will probably cut down the query to reasonable
|
|
|
|
|
# logging size most of the time. The substr is really just a sanity check.
|
2004-07-24 07:24:04 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
# Who's been wasting my precious column space? -- TS
|
2010-09-04 13:48:16 +00:00
|
|
|
# $profName = 'query: ' . $fname . ' ' . substr( DatabaseBase::generalizeSQL( $sql ), 0, 255 );
|
2005-04-24 08:31:12 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $isMaster ) {
|
2010-08-25 01:24:47 +00:00
|
|
|
$queryProf = 'query-m: ' . substr( DatabaseBase::generalizeSQL( $sql ), 0, 255 );
|
|
|
|
|
$totalProf = 'DatabaseBase::query-master';
|
2005-10-29 01:41:36 +00:00
|
|
|
} else {
|
2010-08-25 01:24:47 +00:00
|
|
|
$queryProf = 'query: ' . substr( DatabaseBase::generalizeSQL( $sql ), 0, 255 );
|
|
|
|
|
$totalProf = 'DatabaseBase::query';
|
2005-10-29 01:41:36 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
wfProfileIn( $totalProf );
|
|
|
|
|
wfProfileIn( $queryProf );
|
2005-10-29 01:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->mLastQuery = $sql;
|
2009-02-17 14:06:42 +00:00
|
|
|
if ( !$this->mDoneWrites && $this->isWriteQuery( $sql ) ) {
|
|
|
|
|
// Set a flag indicating that writes have been done
|
2010-09-04 13:48:16 +00:00
|
|
|
wfDebug( __METHOD__ . ": Writes done: $sql\n" );
|
2009-02-17 14:06:42 +00:00
|
|
|
$this->mDoneWrites = true;
|
|
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
# Add a comment for easy SHOW PROCESSLIST interpretation
|
2010-09-04 13:48:16 +00:00
|
|
|
# if ( $fname ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
global $wgUser;
|
2011-04-30 14:08:12 +00:00
|
|
|
if ( is_object( $wgUser ) && $wgUser->isItemLoaded( 'name' ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$userName = $wgUser->getName();
|
|
|
|
|
if ( mb_strlen( $userName ) > 15 ) {
|
|
|
|
|
$userName = mb_substr( $userName, 0, 15 ) . '...';
|
|
|
|
|
}
|
|
|
|
|
$userName = str_replace( '/', '', $userName );
|
|
|
|
|
} else {
|
|
|
|
|
$userName = '';
|
|
|
|
|
}
|
2010-11-26 02:37:28 +00:00
|
|
|
$commentedSql = preg_replace( '/\s/', " /* $fname $userName */ ", $sql, 1 );
|
2010-09-04 13:48:16 +00:00
|
|
|
# } else {
|
2008-03-30 09:48:15 +00:00
|
|
|
# $commentedSql = $sql;
|
2010-09-04 13:48:16 +00:00
|
|
|
# }
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
# If DBO_TRX is set, start a transaction
|
2010-03-06 18:14:11 +00:00
|
|
|
if ( ( $this->mFlags & DBO_TRX ) && !$this->trxLevel() &&
|
2010-09-04 13:48:16 +00:00
|
|
|
$sql != 'BEGIN' && $sql != 'COMMIT' && $sql != 'ROLLBACK' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
// avoid establishing transactions for SHOW and SET statements too -
|
2010-03-06 18:14:11 +00:00
|
|
|
// that would delay transaction initializations to once connection
|
2008-03-30 09:48:15 +00:00
|
|
|
// is really used by application
|
2010-09-04 13:48:16 +00:00
|
|
|
$sqlstart = substr( $sql, 0, 10 ); // very much worth it, benchmark certified(tm)
|
|
|
|
|
if ( strpos( $sqlstart, "SHOW " ) !== 0 and strpos( $sqlstart, "SET " ) !== 0 )
|
2010-03-06 18:14:11 +00:00
|
|
|
$this->begin();
|
2005-10-29 01:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $this->debug() ) {
|
2010-08-16 22:29:27 +00:00
|
|
|
static $cnt = 0;
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-16 22:29:27 +00:00
|
|
|
$cnt++;
|
2008-03-30 09:48:15 +00:00
|
|
|
$sqlx = substr( $commentedSql, 0, 500 );
|
2010-10-05 15:10:14 +00:00
|
|
|
$sqlx = strtr( $sqlx, "\t\n", ' ' );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $isMaster ) {
|
2010-08-16 22:29:27 +00:00
|
|
|
wfDebug( "Query $cnt (master): $sqlx\n" );
|
2008-03-30 09:48:15 +00:00
|
|
|
} else {
|
2010-08-16 22:29:27 +00:00
|
|
|
wfDebug( "Query $cnt (slave): $sqlx\n" );
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-04 09:10:32 +00:00
|
|
|
if ( istainted( $sql ) & TC_MYSQL ) {
|
|
|
|
|
throw new MWException( 'Tainted query found' );
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
# Do the query and handle errors
|
|
|
|
|
$ret = $this->doQuery( $commentedSql );
|
|
|
|
|
|
|
|
|
|
# Try reconnecting if the connection was lost
|
2009-01-15 06:56:58 +00:00
|
|
|
if ( false === $ret && $this->wasErrorReissuable() ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
# Transaction is gone, like it or not
|
|
|
|
|
$this->mTrxLevel = 0;
|
|
|
|
|
wfDebug( "Connection lost, reconnecting...\n" );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $this->ping() ) {
|
|
|
|
|
wfDebug( "Reconnected\n" );
|
|
|
|
|
$sqlx = substr( $commentedSql, 0, 500 );
|
2010-10-05 15:10:14 +00:00
|
|
|
$sqlx = strtr( $sqlx, "\t\n", ' ' );
|
2008-03-30 09:48:15 +00:00
|
|
|
global $wgRequestTime;
|
2010-09-04 13:48:16 +00:00
|
|
|
$elapsed = round( microtime( true ) - $wgRequestTime, 3 );
|
2011-02-22 10:26:00 +00:00
|
|
|
if ( $elapsed < 300 ) {
|
|
|
|
|
# Not a database error to lose a transaction after a minute or two
|
|
|
|
|
wfLogDBError( "Connection lost and reconnected after {$elapsed}s, query: $sqlx\n" );
|
|
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
$ret = $this->doQuery( $commentedSql );
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( "Failed\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( false === $ret ) {
|
|
|
|
|
$this->reportQueryError( $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-21 16:31:02 +00:00
|
|
|
if ( !Profiler::instance()->isStub() ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
wfProfileOut( $queryProf );
|
|
|
|
|
wfProfileOut( $totalProf );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $this->resultObject( $ret );
|
2006-07-17 00:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
2006-08-16 00:58:42 +00:00
|
|
|
/**
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $error String
|
|
|
|
|
* @param $errno Integer
|
|
|
|
|
* @param $sql String
|
|
|
|
|
* @param $fname String
|
|
|
|
|
* @param $tempIgnore Boolean
|
2006-08-16 00:58:42 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
|
|
|
|
|
# Ignore errors during error handling to avoid infinite recursion
|
|
|
|
|
$ignore = $this->ignoreErrors( true );
|
|
|
|
|
++$this->mErrorCount;
|
|
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( $ignore || $tempIgnore ) {
|
|
|
|
|
wfDebug( "SQL ERROR (ignored): $error\n" );
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->ignoreErrors( $ignore );
|
|
|
|
|
} else {
|
|
|
|
|
$sql1line = str_replace( "\n", "\\n", $sql );
|
2010-09-04 13:48:16 +00:00
|
|
|
wfLogDBError( "$fname\t{$this->mServer}\t$errno\t$error\t$sql1line\n" );
|
|
|
|
|
wfDebug( "SQL ERROR: " . $error . "\n" );
|
2008-03-30 09:48:15 +00:00
|
|
|
throw new DBQueryError( $this, $error, $errno, $sql, $fname );
|
|
|
|
|
}
|
2006-08-16 00:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
|
2006-11-28 21:40:42 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Intended to be compatible with the PEAR::DB wrapper functions.
|
|
|
|
|
* http://pear.php.net/manual/en/package.database.db.intro-execute.php
|
|
|
|
|
*
|
|
|
|
|
* ? = scalar value, quoted as necessary
|
|
|
|
|
* ! = raw SQL bit (a function for instance)
|
|
|
|
|
* & = filename; reads the file and inserts as a blob
|
2010-10-05 15:10:14 +00:00
|
|
|
* (we don't use this though...)
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function prepare( $sql, $func = 'DatabaseBase::prepare' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
/* MySQL doesn't support prepared statements (yet), so just
|
|
|
|
|
pack up the query for reference. We'll manually replace
|
|
|
|
|
the bits later. */
|
|
|
|
|
return array( 'query' => $sql, 'func' => $func );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function freePrepared( $prepared ) {
|
2010-04-28 18:12:42 +00:00
|
|
|
/* No-op by default */
|
2006-11-28 21:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
2007-01-02 21:34:42 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Execute a prepared query with the various arguments
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $prepared String: the prepared sql
|
|
|
|
|
* @param $args Mixed: Either an array here, or put scalars as varargs
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return ResultWrapper
|
2007-01-02 21:34:42 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function execute( $prepared, $args = null ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( !is_array( $args ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
# Pull the var args
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
array_shift( $args );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$sql = $this->fillPrepared( $prepared['query'], $args );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $this->query( $sql, $prepared['func'] );
|
2007-01-02 21:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-02 18:03:10 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Prepare & execute an SQL statement, quoting and inserting arguments
|
|
|
|
|
* in the appropriate places.
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $query String
|
|
|
|
|
* @param $args ...
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return ResultWrapper
|
2007-09-02 18:03:10 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function safeQuery( $query, $args = null ) {
|
2010-08-25 01:24:47 +00:00
|
|
|
$prepared = $this->prepare( $query, 'DatabaseBase::safeQuery' );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
|
|
|
|
if ( !is_array( $args ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
# Pull the var args
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
array_shift( $args );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$retval = $this->execute( $prepared, $args );
|
|
|
|
|
$this->freePrepared( $prepared );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $retval;
|
2007-09-02 18:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
2007-01-23 14:47:12 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* For faking prepared SQL statements on DBs that don't support
|
|
|
|
|
* it directly.
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $preparedQuery String: a 'preparable' SQL statement
|
|
|
|
|
* @param $args Array of arguments to fill it with
|
2008-03-30 09:48:15 +00:00
|
|
|
* @return string executable SQL
|
2007-01-23 14:47:12 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function fillPrepared( $preparedQuery, $args ) {
|
|
|
|
|
reset( $args );
|
|
|
|
|
$this->preparedArgs =& $args;
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return preg_replace_callback( '/(\\\\[?!&]|[?!&])/',
|
|
|
|
|
array( &$this, 'fillPreparedArg' ), $preparedQuery );
|
2007-01-23 14:47:12 +00:00
|
|
|
}
|
|
|
|
|
|
2007-07-30 14:10:42 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* preg_callback func for fillPrepared()
|
|
|
|
|
* The arguments should be in $this->preparedArgs and must not be touched
|
|
|
|
|
* while we're doing this.
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $matches Array
|
|
|
|
|
* @return String
|
2008-03-30 09:48:15 +00:00
|
|
|
* @private
|
2007-07-30 14:10:42 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function fillPreparedArg( $matches ) {
|
|
|
|
|
switch( $matches[1] ) {
|
|
|
|
|
case '\\?': return '?';
|
|
|
|
|
case '\\!': return '!';
|
|
|
|
|
case '\\&': return '&';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
list( /* $n */ , $arg ) = each( $this->preparedArgs );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
switch( $matches[1] ) {
|
|
|
|
|
case '?': return $this->addQuotes( $arg );
|
|
|
|
|
case '!': return $arg;
|
|
|
|
|
case '&':
|
|
|
|
|
# return $this->addQuotes( file_get_contents( $arg ) );
|
|
|
|
|
throw new DBUnexpectedError( $this, '& mode is not implemented. If it\'s really needed, uncomment the line above.' );
|
|
|
|
|
default:
|
|
|
|
|
throw new DBUnexpectedError( $this, 'Received invalid match. This should never happen!' );
|
|
|
|
|
}
|
2007-07-30 14:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
2006-06-06 23:07:26 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Free a result object
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $res Mixed: A SQL result
|
2006-06-06 23:07:26 +00:00
|
|
|
*/
|
2009-06-16 20:22:11 +00:00
|
|
|
function freeResult( $res ) {
|
2010-10-05 15:10:14 +00:00
|
|
|
# Stub. Might not really need to be overridden, since results should
|
2009-06-16 20:22:11 +00:00
|
|
|
# be freed by PHP when the variable goes out of scope anyway.
|
|
|
|
|
}
|
2006-06-06 23:07:26 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Simple UPDATE wrapper
|
|
|
|
|
* Usually aborts on failure
|
|
|
|
|
* If errors are explicitly ignored, returns success
|
2004-09-03 16:36:46 +00:00
|
|
|
*
|
2010-08-25 01:24:47 +00:00
|
|
|
* This function exists for historical reasons, DatabaseBase::update() has a more standard
|
2008-03-30 09:48:15 +00:00
|
|
|
* calling convention and feature set
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function set( $table, $var, $value, $cond, $fname = 'DatabaseBase::set' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$table = $this->tableName( $table );
|
|
|
|
|
$sql = "UPDATE $table SET $var = '" .
|
|
|
|
|
$this->strencode( $value ) . "' WHERE ($cond)";
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return (bool)$this->query( $sql, $fname );
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Simple SELECT wrapper, returns a single field, input must be encoded
|
|
|
|
|
* Usually aborts on failure
|
|
|
|
|
* If errors are explicitly ignored, returns FALSE on failure
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function selectField( $table, $var, $cond = '', $fname = 'DatabaseBase::selectField', $options = array() ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( !is_array( $options ) ) {
|
|
|
|
|
$options = array( $options );
|
2005-10-28 01:08:49 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$options['LIMIT'] = 1;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$res = $this->select( $table, $var, $cond, $fname, $options );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $res === false || !$this->numRows( $res ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$row = $this->fetchRow( $res );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $row !== false ) {
|
2009-01-09 03:53:28 +00:00
|
|
|
return reset( $row );
|
2004-01-10 16:44:31 +00:00
|
|
|
} else {
|
2008-03-30 09:48:15 +00:00
|
|
|
return false;
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Returns an optional USE INDEX clause to go after the table, and a
|
|
|
|
|
* string to go at the end of the query
|
2007-04-19 02:01:19 +00:00
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* @private
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $options Array: associative array of options to be turned into
|
2010-10-05 15:10:14 +00:00
|
|
|
* an SQL query, valid keys are listed in the function.
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return Array
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function makeSelectOptions( $options ) {
|
|
|
|
|
$preLimitTail = $postLimitTail = '';
|
|
|
|
|
$startOpts = '';
|
2005-10-22 20:52:30 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$noKeyOptions = array();
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
foreach ( $options as $key => $option ) {
|
|
|
|
|
if ( is_numeric( $key ) ) {
|
|
|
|
|
$noKeyOptions[$option] = true;
|
2006-03-28 05:11:40 +00:00
|
|
|
}
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $options['GROUP BY'] ) ) {
|
2011-01-31 21:04:40 +00:00
|
|
|
$gb = is_array( $options['GROUP BY'] )
|
|
|
|
|
? implode( ',', $options['GROUP BY'] )
|
|
|
|
|
: $options['GROUP BY'];
|
|
|
|
|
$preLimitTail .= " GROUP BY {$gb}";
|
2010-08-30 20:28:32 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $options['HAVING'] ) ) {
|
|
|
|
|
$preLimitTail .= " HAVING {$options['HAVING']}";
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $options['ORDER BY'] ) ) {
|
2011-01-31 21:04:40 +00:00
|
|
|
$ob = is_array( $options['ORDER BY'] )
|
|
|
|
|
? implode( ',', $options['ORDER BY'] )
|
|
|
|
|
: $options['ORDER BY'];
|
|
|
|
|
$preLimitTail .= " ORDER BY {$ob}";
|
2010-08-30 20:28:32 +00:00
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
// if (isset($options['LIMIT'])) {
|
2008-03-30 09:48:15 +00:00
|
|
|
// $tailOpts .= $this->limitResult('', $options['LIMIT'],
|
2010-03-06 18:14:11 +00:00
|
|
|
// isset($options['OFFSET']) ? $options['OFFSET']
|
2008-03-30 09:48:15 +00:00
|
|
|
// : false);
|
2010-09-04 13:48:16 +00:00
|
|
|
// }
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
|
|
|
|
|
$postLimitTail .= ' FOR UPDATE';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
|
|
|
|
|
$postLimitTail .= ' LOCK IN SHARE MODE';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
|
|
|
|
|
$startOpts .= 'DISTINCT';
|
|
|
|
|
}
|
2005-03-28 07:56:17 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
# Various MySQL extensions
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) {
|
|
|
|
|
$startOpts .= ' /*! STRAIGHT_JOIN */';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) {
|
|
|
|
|
$startOpts .= ' HIGH_PRIORITY';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) {
|
|
|
|
|
$startOpts .= ' SQL_BIG_RESULT';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) {
|
|
|
|
|
$startOpts .= ' SQL_BUFFER_RESULT';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) {
|
|
|
|
|
$startOpts .= ' SQL_SMALL_RESULT';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) {
|
|
|
|
|
$startOpts .= ' SQL_CALC_FOUND_ROWS';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['SQL_CACHE'] ) ) {
|
|
|
|
|
$startOpts .= ' SQL_CACHE';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) {
|
|
|
|
|
$startOpts .= ' SQL_NO_CACHE';
|
|
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) {
|
|
|
|
|
$useIndex = $this->useIndexClause( $options['USE INDEX'] );
|
|
|
|
|
} else {
|
|
|
|
|
$useIndex = '';
|
|
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SELECT wrapper
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $table Mixed: Array or string, table name(s) (prefix auto-added)
|
2010-10-05 15:10:14 +00:00
|
|
|
* @param $vars Mixed: Array or string, field name(s) to be retrieved
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $conds Mixed: Array or string, condition(s) for WHERE
|
|
|
|
|
* @param $fname String: Calling function name (use __METHOD__) for logs/profiling
|
|
|
|
|
* @param $options Array: Associative array of options (e.g. array('GROUP BY' => 'page_title')),
|
2010-10-05 15:10:14 +00:00
|
|
|
* see DatabaseBase::makeSelectOptions code for list of supported stuff
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $join_conds Array: Associative array of table join conditions (optional)
|
2010-10-05 15:10:14 +00:00
|
|
|
* (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') )
|
2011-04-20 15:26:05 +00:00
|
|
|
* @return ResultWrapper|Bool Database result resource (feed to DatabaseBase::fetchObject
|
|
|
|
|
* or whatever), or false on failure
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function select( $table, $vars, $conds = '', $fname = 'DatabaseBase::select', $options = array(), $join_conds = array() ) {
|
2008-05-13 04:56:14 +00:00
|
|
|
$sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-05-13 04:56:14 +00:00
|
|
|
return $this->query( $sql, $fname );
|
|
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2008-05-13 04:56:14 +00:00
|
|
|
/**
|
|
|
|
|
* SELECT wrapper
|
|
|
|
|
*
|
2010-12-02 19:39:32 +00:00
|
|
|
* @param $table Mixed: Array or string, table name(s) (prefix auto-added). Array keys are table aliases (optional)
|
2010-10-05 15:10:14 +00:00
|
|
|
* @param $vars Mixed: Array or string, field name(s) to be retrieved
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $conds Mixed: Array or string, condition(s) for WHERE
|
|
|
|
|
* @param $fname String: Calling function name (use __METHOD__) for logs/profiling
|
|
|
|
|
* @param $options Array: Associative array of options (e.g. array('GROUP BY' => 'page_title')),
|
2010-10-05 15:10:14 +00:00
|
|
|
* see DatabaseBase::makeSelectOptions code for list of supported stuff
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $join_conds Array: Associative array of table join conditions (optional)
|
2010-10-05 15:10:14 +00:00
|
|
|
* (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') )
|
2008-05-13 04:56:14 +00:00
|
|
|
* @return string, the SQL text
|
|
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function selectSQLText( $table, $vars, $conds = '', $fname = 'DatabaseBase::select', $options = array(), $join_conds = array() ) {
|
|
|
|
|
if ( is_array( $vars ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$vars = implode( ',', $vars );
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
|
|
|
|
if ( !is_array( $options ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$options = array( $options );
|
2005-03-28 07:56:17 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
|
|
|
|
if ( is_array( $table ) ) {
|
|
|
|
|
if ( !empty( $join_conds ) || ( isset( $options['USE INDEX'] ) && is_array( @$options['USE INDEX'] ) ) ) {
|
2008-05-10 09:49:21 +00:00
|
|
|
$from = ' FROM ' . $this->tableNamesWithUseIndexOrJOIN( $table, @$options['USE INDEX'], $join_conds );
|
2010-08-30 20:28:32 +00:00
|
|
|
} else {
|
2010-12-02 19:39:32 +00:00
|
|
|
$from = ' FROM ' . implode( ',', $this->tableNamesWithAlias( $table ) );
|
2010-08-30 20:28:32 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
} elseif ( $table != '' ) {
|
|
|
|
|
if ( $table { 0 } == ' ' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$from = ' FROM ' . $table;
|
2005-04-24 07:21:15 +00:00
|
|
|
} else {
|
2008-03-30 09:48:15 +00:00
|
|
|
$from = ' FROM ' . $this->tableName( $table );
|
2005-04-24 07:21:15 +00:00
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
} else {
|
|
|
|
|
$from = '';
|
2005-04-24 07:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
list( $startOpts, $useIndex, $preLimitTail, $postLimitTail ) = $this->makeSelectOptions( $options );
|
|
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( !empty( $conds ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( is_array( $conds ) ) {
|
|
|
|
|
$conds = $this->makeList( $conds, LIST_AND );
|
|
|
|
|
}
|
|
|
|
|
$sql = "SELECT $startOpts $vars $from $useIndex WHERE $conds $preLimitTail";
|
|
|
|
|
} else {
|
|
|
|
|
$sql = "SELECT $startOpts $vars $from $useIndex $preLimitTail";
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( isset( $options['LIMIT'] ) )
|
|
|
|
|
$sql = $this->limitResult( $sql, $options['LIMIT'],
|
|
|
|
|
isset( $options['OFFSET'] ) ? $options['OFFSET'] : false );
|
2008-03-30 09:48:15 +00:00
|
|
|
$sql = "$sql $postLimitTail";
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( isset( $options['EXPLAIN'] ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$sql = 'EXPLAIN ' . $sql;
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-05-13 04:56:14 +00:00
|
|
|
return $sql;
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Single row SELECT wrapper
|
|
|
|
|
* Aborts or returns FALSE on error
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $table String: table name
|
|
|
|
|
* @param $vars String: the selected variables
|
|
|
|
|
* @param $conds Array: a condition map, terms are ANDed together.
|
2011-03-21 23:03:11 +00:00
|
|
|
* Items with numeric keys are taken to be literal conditions
|
|
|
|
|
* Takes an array of selected variables, and a condition map, which is ANDed
|
|
|
|
|
* e.g: selectRow( "page", array( "page_id" ), array( "page_namespace" =>
|
|
|
|
|
* NS_MAIN, "page_title" => "Astronomy" ) ) would return an object where
|
|
|
|
|
* $obj- >page_id is the ID of the Astronomy article
|
2009-06-15 18:38:15 +00:00
|
|
|
* @param $fname String: Calling function name
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $options Array
|
|
|
|
|
* @param $join_conds Array
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2011-03-21 23:03:11 +00:00
|
|
|
* @return ResultWrapper|Bool
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function selectRow( $table, $vars, $conds, $fname = 'DatabaseBase::selectRow', $options = array(), $join_conds = array() ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$options['LIMIT'] = 1;
|
2008-05-19 17:02:58 +00:00
|
|
|
$res = $this->select( $table, $vars, $conds, $fname, $options, $join_conds );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( $res === false ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return false;
|
2010-08-30 20:28:32 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
|
|
|
|
if ( !$this->numRows( $res ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return false;
|
2005-08-02 13:35:19 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$obj = $this->fetchObject( $res );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $obj;
|
|
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Estimate rows in dataset
|
2009-10-21 12:21:09 +00:00
|
|
|
* Returns estimated count - not necessarily an accurate estimate across different databases,
|
|
|
|
|
* so use sparingly
|
2010-08-25 01:24:47 +00:00
|
|
|
* Takes same arguments as DatabaseBase::select()
|
2009-10-21 12:21:09 +00:00
|
|
|
*
|
2010-07-04 14:41:26 +00:00
|
|
|
* @param $table String: table name
|
|
|
|
|
* @param $vars Array: unused
|
|
|
|
|
* @param $conds Array: filters on the table
|
|
|
|
|
* @param $fname String: function name for profiling
|
|
|
|
|
* @param $options Array: options for select
|
|
|
|
|
* @return Integer: row count
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
public function estimateRowCount( $table, $vars = '*', $conds = '', $fname = 'DatabaseBase::estimateRowCount', $options = array() ) {
|
2009-10-21 12:21:09 +00:00
|
|
|
$rows = 0;
|
|
|
|
|
$res = $this->select ( $table, 'COUNT(*) AS rowcount', $conds, $fname, $options );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2009-10-21 12:21:09 +00:00
|
|
|
if ( $res ) {
|
|
|
|
|
$row = $this->fetchRow( $res );
|
|
|
|
|
$rows = ( isset( $row['rowcount'] ) ) ? $row['rowcount'] : 0;
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2009-10-21 12:21:09 +00:00
|
|
|
return $rows;
|
2004-07-18 08:48:43 +00:00
|
|
|
}
|
2004-09-03 16:36:46 +00:00
|
|
|
|
2004-10-18 07:25:56 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Removes most variables from an SQL query and replaces them with X or N for numbers.
|
|
|
|
|
* It's only slightly flawed. Don't use for anything important.
|
2004-10-18 07:25:56 +00:00
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $sql String: A SQL Query
|
2004-10-18 07:25:56 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
static function generalizeSQL( $sql ) {
|
|
|
|
|
# This does the same as the regexp below would do, but in such a way
|
|
|
|
|
# as to avoid crashing php on some large strings.
|
|
|
|
|
# $sql = preg_replace ( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql);
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
$sql = str_replace ( "\\\\", '', $sql );
|
|
|
|
|
$sql = str_replace ( "\\'", '', $sql );
|
|
|
|
|
$sql = str_replace ( "\\\"", '', $sql );
|
|
|
|
|
$sql = preg_replace ( "/'.*'/s", "'X'", $sql );
|
|
|
|
|
$sql = preg_replace ( '/".*"/s', "'X'", $sql );
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
# All newlines, tabs, etc replaced by single space
|
2010-09-04 13:48:16 +00:00
|
|
|
$sql = preg_replace ( '/\s+/', ' ', $sql );
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
# All numbers => N
|
2010-09-04 13:48:16 +00:00
|
|
|
$sql = preg_replace ( '/-?[0-9]+/s', 'N', $sql );
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
return $sql;
|
2004-10-18 07:25:56 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-10-18 07:25:56 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Determines whether a field exists in a table
|
2010-07-04 14:41:26 +00:00
|
|
|
*
|
|
|
|
|
* @param $table String: table name
|
|
|
|
|
* @param $field String: filed to check on that table
|
|
|
|
|
* @param $fname String: calling function name (optional)
|
|
|
|
|
* @return Boolean: whether $table has filed $field
|
2004-10-18 07:25:56 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function fieldExists( $table, $field, $fname = 'DatabaseBase::fieldExists' ) {
|
2010-07-02 10:01:09 +00:00
|
|
|
$info = $this->fieldInfo( $table, $field );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-07-02 10:01:09 +00:00
|
|
|
return (bool)$info;
|
2004-10-18 07:25:56 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-10-18 07:25:56 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Determines whether an index exists
|
|
|
|
|
* Usually aborts on failure
|
|
|
|
|
* If errors are explicitly ignored, returns NULL on failure
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return bool|null
|
2004-10-18 07:25:56 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function indexExists( $table, $index, $fname = 'DatabaseBase::indexExists' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$info = $this->indexInfo( $table, $index, $fname );
|
|
|
|
|
if ( is_null( $info ) ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2008-03-30 09:48:15 +00:00
|
|
|
} else {
|
|
|
|
|
return $info !== false;
|
2004-10-18 07:25:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Query whether a given table exists
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @string table
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function tableExists( $table ) {
|
|
|
|
|
$table = $this->tableName( $table );
|
|
|
|
|
$old = $this->ignoreErrors( true );
|
2010-10-03 08:05:26 +00:00
|
|
|
$res = $this->query( "SELECT 1 FROM $table LIMIT 1", __METHOD__ );
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->ignoreErrors( $old );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-08 12:27:48 +00:00
|
|
|
return (bool)$res;
|
2004-03-11 09:06:13 +00:00
|
|
|
}
|
2004-09-03 16:36:46 +00:00
|
|
|
|
|
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* mysql_field_type() wrapper
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function fieldType( $res, $index ) {
|
2007-07-05 19:42:18 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
|
|
|
|
$res = $res->result;
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return mysql_field_type( $res, $index );
|
2004-03-11 09:06:13 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Determines if a given index is unique
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @param $table string
|
|
|
|
|
* @param $index string
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function indexUnique( $table, $index ) {
|
|
|
|
|
$indexInfo = $this->indexInfo( $table, $index );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( !$indexInfo ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2007-07-05 19:42:18 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return !$indexInfo[0]->Non_unique;
|
2007-07-05 19:42:18 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2011-05-02 20:14:17 +00:00
|
|
|
/**
|
|
|
|
|
* @param $options array
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function makeInsertOptions( $options ) {
|
|
|
|
|
return implode( ' ', $options );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* INSERT wrapper, inserts an array into a table
|
|
|
|
|
*
|
|
|
|
|
* $a may be a single associative array, or an array of these with numeric keys, for
|
|
|
|
|
* multi-row insert.
|
|
|
|
|
*
|
|
|
|
|
* Usually aborts on failure
|
|
|
|
|
* If errors are explicitly ignored, returns success
|
2010-08-25 00:18:47 +00:00
|
|
|
*
|
|
|
|
|
* @param $table String: table name (prefix auto-added)
|
|
|
|
|
* @param $a Array: Array of rows to insert
|
|
|
|
|
* @param $fname String: Calling function name (use __METHOD__) for logs/profiling
|
|
|
|
|
* @param $options Mixed: Associative array of options
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function insert( $table, $a, $fname = 'DatabaseBase::insert', $options = array() ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
# No rows to insert, easy just return now
|
|
|
|
|
if ( !count( $a ) ) {
|
|
|
|
|
return true;
|
2007-07-05 19:42:18 +00:00
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
$table = $this->tableName( $table );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( !is_array( $options ) ) {
|
|
|
|
|
$options = array( $options );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2011-05-02 20:14:17 +00:00
|
|
|
$options = $this->makeInsertOptions( $options );
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( isset( $a[0] ) && is_array( $a[0] ) ) {
|
|
|
|
|
$multi = true;
|
|
|
|
|
$keys = array_keys( $a[0] );
|
|
|
|
|
} else {
|
|
|
|
|
$multi = false;
|
|
|
|
|
$keys = array_keys( $a );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-02 20:14:17 +00:00
|
|
|
$sql = 'INSERT ' . $options .
|
2008-03-30 09:48:15 +00:00
|
|
|
" INTO $table (" . implode( ',', $keys ) . ') VALUES ';
|
|
|
|
|
|
|
|
|
|
if ( $multi ) {
|
|
|
|
|
$first = true;
|
|
|
|
|
foreach ( $a as $row ) {
|
|
|
|
|
if ( $first ) {
|
|
|
|
|
$first = false;
|
|
|
|
|
} else {
|
|
|
|
|
$sql .= ',';
|
|
|
|
|
}
|
|
|
|
|
$sql .= '(' . $this->makeList( $row ) . ')';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$sql .= '(' . $this->makeList( $a ) . ')';
|
|
|
|
|
}
|
2010-08-25 17:45:00 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return (bool)$this->query( $sql, $fname );
|
2007-07-05 19:42:18 +00:00
|
|
|
}
|
2004-10-24 07:10:33 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2010-08-25 01:24:47 +00:00
|
|
|
* Make UPDATE options for the DatabaseBase::update function
|
2004-10-24 07:10:33 +00:00
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* @private
|
2010-08-25 01:24:47 +00:00
|
|
|
* @param $options Array: The options passed to DatabaseBase::update
|
2008-03-30 09:48:15 +00:00
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function makeUpdateOptions( $options ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( !is_array( $options ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$options = array( $options );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$opts = array();
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-25 17:45:00 +00:00
|
|
|
if ( in_array( 'LOW_PRIORITY', $options ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$opts[] = $this->lowPriorityOption();
|
2010-08-25 17:45:00 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-25 17:45:00 +00:00
|
|
|
if ( in_array( 'IGNORE', $options ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$opts[] = 'IGNORE';
|
2010-08-25 17:45:00 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
|
|
|
|
return implode( ' ', $opts );
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* UPDATE wrapper, takes a condition array and a SET array
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $table String: The table to UPDATE
|
|
|
|
|
* @param $values Array: An array of values to SET
|
|
|
|
|
* @param $conds Array: An array of conditions (WHERE). Use '*' to update all rows.
|
|
|
|
|
* @param $fname String: The Class::Function calling this function
|
2010-10-05 15:10:14 +00:00
|
|
|
* (for the log)
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $options Array: An array of UPDATE options, can be one or
|
2010-10-05 15:10:14 +00:00
|
|
|
* more of IGNORE, LOW_PRIORITY
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return Boolean
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function update( $table, $values, $conds, $fname = 'DatabaseBase::update', $options = array() ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$table = $this->tableName( $table );
|
|
|
|
|
$opts = $this->makeUpdateOptions( $options );
|
|
|
|
|
$sql = "UPDATE $opts $table SET " . $this->makeList( $values, LIST_SET );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $conds != '*' ) {
|
|
|
|
|
$sql .= " WHERE " . $this->makeList( $conds, LIST_AND );
|
2007-07-05 19:42:18 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $this->query( $sql, $fname );
|
2007-07-05 19:42:18 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Makes an encoded list of strings from an array
|
2011-02-18 22:58:02 +00:00
|
|
|
* @param $a Array
|
2011-05-25 18:58:02 +00:00
|
|
|
* @param $mode int
|
2010-10-05 15:10:14 +00:00
|
|
|
* LIST_COMMA - comma separated, no field names
|
2010-12-03 23:50:00 +00:00
|
|
|
* LIST_AND - ANDed WHERE clause (without the WHERE)
|
2010-10-05 15:10:14 +00:00
|
|
|
* LIST_OR - ORed WHERE clause (without the WHERE)
|
|
|
|
|
* LIST_SET - comma separated with field names, like a SET clause
|
|
|
|
|
* LIST_NAMES - comma separated field names
|
2011-02-18 22:58:02 +00:00
|
|
|
*
|
2011-05-13 15:39:45 +00:00
|
|
|
* In LIST_AND or LIST_OR modes, you can suffix a field with an exclamation
|
|
|
|
|
* mark to generate a 'NOT IN' structure.
|
|
|
|
|
* Example:
|
|
|
|
|
* $db->makeList( array( 'field!' => array( 1,2,3 ) );
|
|
|
|
|
* outputs:
|
|
|
|
|
* 'field' NOT IN ('1', '2', '3' );
|
|
|
|
|
|
2011-02-18 22:58:02 +00:00
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function makeList( $a, $mode = LIST_COMMA ) {
|
|
|
|
|
if ( !is_array( $a ) ) {
|
2010-08-25 01:24:47 +00:00
|
|
|
throw new DBUnexpectedError( $this, 'DatabaseBase::makeList called with incorrect parameters' );
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$first = true;
|
|
|
|
|
$list = '';
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
foreach ( $a as $field => $value ) {
|
|
|
|
|
if ( !$first ) {
|
|
|
|
|
if ( $mode == LIST_AND ) {
|
|
|
|
|
$list .= ' AND ';
|
2010-09-04 13:48:16 +00:00
|
|
|
} elseif ( $mode == LIST_OR ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$list .= ' OR ';
|
|
|
|
|
} else {
|
|
|
|
|
$list .= ',';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$first = false;
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2011-05-13 15:39:45 +00:00
|
|
|
// Support 'NOT IN' by suffixing fieldname with an exclamation mark
|
|
|
|
|
$not = false;
|
|
|
|
|
if( substr($field,-1) == '!' ) {
|
|
|
|
|
$not = true;
|
|
|
|
|
$field = substr($field, 0, -1 );
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( ( $mode == LIST_AND || $mode == LIST_OR ) && is_numeric( $field ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$list .= "($value)";
|
2010-09-04 13:48:16 +00:00
|
|
|
} elseif ( ( $mode == LIST_SET ) && is_numeric( $field ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$list .= "$value";
|
2010-09-04 13:48:16 +00:00
|
|
|
} elseif ( ( $mode == LIST_AND || $mode == LIST_OR ) && is_array( $value ) ) {
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( count( $value ) == 0 ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
throw new MWException( __METHOD__ . ': empty input' );
|
|
|
|
|
} elseif ( count( $value ) == 1 ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
// Special-case single values, as IN isn't terribly efficient
|
|
|
|
|
// Don't necessarily assume the single key is 0; we don't
|
|
|
|
|
// enforce linear numeric ordering on other arrays here.
|
|
|
|
|
$value = array_values( $value );
|
2011-05-13 15:39:45 +00:00
|
|
|
|
|
|
|
|
$operator = $not ? ' != ' : ' = ';
|
|
|
|
|
$list .= $field . $operator . $this->addQuotes( $value[0] );
|
2008-03-30 09:48:15 +00:00
|
|
|
} else {
|
2011-05-13 15:39:45 +00:00
|
|
|
$operator = $not ? ' NOT IN ' : ' IN ';
|
|
|
|
|
$list .= $field . $operator . " (" . $this->makeList( $value ) . ") ";
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
2010-08-30 20:28:32 +00:00
|
|
|
} elseif ( $value === null ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $mode == LIST_AND || $mode == LIST_OR ) {
|
|
|
|
|
$list .= "$field IS ";
|
|
|
|
|
} elseif ( $mode == LIST_SET ) {
|
|
|
|
|
$list .= "$field = ";
|
|
|
|
|
}
|
|
|
|
|
$list .= 'NULL';
|
|
|
|
|
} else {
|
|
|
|
|
if ( $mode == LIST_AND || $mode == LIST_OR || $mode == LIST_SET ) {
|
|
|
|
|
$list .= "$field = ";
|
|
|
|
|
}
|
|
|
|
|
$list .= $mode == LIST_NAMES ? $value : $this->addQuotes( $value );
|
|
|
|
|
}
|
2005-01-14 13:00:17 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $list;
|
2005-01-14 13:00:17 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-04-16 01:40:05 +00:00
|
|
|
/**
|
|
|
|
|
* Build a partial where clause from a 2-d array such as used for LinkBatch.
|
|
|
|
|
* The keys on each level may be either integers or strings.
|
|
|
|
|
*
|
2010-07-04 14:41:26 +00:00
|
|
|
* @param $data Array: organized as 2-d array(baseKeyVal => array(subKeyVal => <ignored>, ...), ...)
|
|
|
|
|
* @param $baseKey String: field name to match the base-level keys to (eg 'pl_namespace')
|
|
|
|
|
* @param $subKey String: field name to match the sub-level keys to (eg 'pl_title')
|
|
|
|
|
* @return Mixed: string SQL fragment, or false if no items in array.
|
2010-04-16 01:40:05 +00:00
|
|
|
*/
|
|
|
|
|
function makeWhereFrom2d( $data, $baseKey, $subKey ) {
|
|
|
|
|
$conds = array();
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-04-16 01:40:05 +00:00
|
|
|
foreach ( $data as $base => $sub ) {
|
|
|
|
|
if ( count( $sub ) ) {
|
|
|
|
|
$conds[] = $this->makeList(
|
|
|
|
|
array( $baseKey => $base, $subKey => array_keys( $sub ) ),
|
2010-09-04 13:48:16 +00:00
|
|
|
LIST_AND );
|
2010-04-16 01:40:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $conds ) {
|
|
|
|
|
return $this->makeList( $conds, LIST_OR );
|
|
|
|
|
} else {
|
|
|
|
|
// Nothing to search for...
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-13 06:31:38 +00:00
|
|
|
/**
|
|
|
|
|
* Bitwise operations
|
|
|
|
|
*/
|
|
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function bitNot( $field ) {
|
2010-08-18 10:02:39 +00:00
|
|
|
return "(~$field)";
|
2009-06-13 06:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param $fieldLeft
|
|
|
|
|
* @param $fieldRight
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function bitAnd( $fieldLeft, $fieldRight ) {
|
2009-06-14 20:13:17 +00:00
|
|
|
return "($fieldLeft & $fieldRight)";
|
2009-06-13 06:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param $fieldLeft
|
|
|
|
|
* @param $fieldRight
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function bitOr( $fieldLeft, $fieldRight ) {
|
2009-06-14 20:13:17 +00:00
|
|
|
return "($fieldLeft | $fieldRight)";
|
2009-06-13 06:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Change the current database
|
2009-06-16 20:22:11 +00:00
|
|
|
*
|
2010-09-04 13:48:16 +00:00
|
|
|
* @todo Explain what exactly will fail if this is not overridden.
|
2009-06-16 20:22:11 +00:00
|
|
|
* @return bool Success or failure
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2009-06-16 20:22:11 +00:00
|
|
|
function selectDB( $db ) {
|
2010-10-05 15:10:14 +00:00
|
|
|
# Stub. Shouldn't cause serious problems if it's not overridden, but
|
2009-06-16 20:22:11 +00:00
|
|
|
# if your database engine supports a concept similar to MySQL's
|
2010-09-04 13:48:16 +00:00
|
|
|
# databases you may as well.
|
2011-01-27 08:25:48 +00:00
|
|
|
$this->mDBname = $db;
|
2009-06-16 20:22:11 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2004-09-03 16:36:46 +00:00
|
|
|
|
|
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Get the current DB name
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function getDBname() {
|
|
|
|
|
return $this->mDBname;
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Get the server hostname or IP address
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function getServer() {
|
|
|
|
|
return $this->mServer;
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Format a table name ready for use in constructing an SQL query
|
2005-05-03 07:47:08 +00:00
|
|
|
*
|
2008-05-07 04:44:04 +00:00
|
|
|
* This does two important things: it quotes the table names to clean them up,
|
|
|
|
|
* and it adds a table prefix if only given a table name with no quotes.
|
2005-05-03 07:47:08 +00:00
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* All functions of this object which require a table name call this function
|
|
|
|
|
* themselves. Pass the canonical name to such functions. This is only needed
|
|
|
|
|
* when calling query() directly.
|
2006-12-29 20:56:47 +00:00
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $name String: database table name
|
2011-04-12 17:00:08 +00:00
|
|
|
* @param $quoted Boolean: Automatically pass the table name through
|
|
|
|
|
* addIdentifierQuotes() so that it can be used in a query.
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return String: full database name
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2011-04-12 17:00:08 +00:00
|
|
|
function tableName( $name, $quoted = true ) {
|
2008-05-07 04:44:04 +00:00
|
|
|
global $wgSharedDB, $wgSharedPrefix, $wgSharedTables;
|
|
|
|
|
# Skip the entire process when we have a string quoted on both ends.
|
|
|
|
|
# Note that we check the end so that we will still quote any use of
|
|
|
|
|
# use of `database`.table. But won't break things if someone wants
|
|
|
|
|
# to query a database table with a dot in the name.
|
2011-04-12 16:34:12 +00:00
|
|
|
if ( $this->isQuotedIdentifier( $name ) ) {
|
2010-08-25 17:45:00 +00:00
|
|
|
return $name;
|
|
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2008-05-08 09:24:24 +00:00
|
|
|
# Lets test for any bits of text that should never show up in a table
|
|
|
|
|
# name. Basically anything like JOIN or ON which are actually part of
|
|
|
|
|
# SQL queries, but may end up inside of the table value to combine
|
|
|
|
|
# sql. Such as how the API is doing.
|
|
|
|
|
# Note that we use a whitespace test rather than a \b test to avoid
|
|
|
|
|
# any remote case where a word like on may be inside of a table name
|
|
|
|
|
# surrounded by symbols which may be considered word breaks.
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
|
2010-08-25 17:45:00 +00:00
|
|
|
return $name;
|
|
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2008-05-07 04:44:04 +00:00
|
|
|
# Split database and table into proper variables.
|
|
|
|
|
# We reverse the explode so that database.table and table both output
|
|
|
|
|
# the correct table.
|
Prevent two E_STRICT warnings when running "php maintenance/update.php" to update from 1.12 alpha to 1.13 alpha.
--------------------------------------------------
# php maintenance/update.php
MediaWiki 1.13alpha Updater
Going to run database updates for wikidb
Depending on the size of your database this may take a while!
Abort with control-c in the next five seconds...0
Strict Standards: Type: 8: Undefined offset: 1 in wiki/includes/Database.php on line 1420
Database.tableName("ipblocks") # line 1164, file: wiki/includes/Database.php
Database.tableExists("ipblocks") # line 203, file: wiki/maintenance/updaters.inc
add_field("ipblocks", "ipb_id", "patch-ipblocks.sql") # line unknown, file: unknown
call_user_func_array("add_field", Array[3]) # line 1051, file: wiki/maintenance/updaters.inc
do_all_updates(false, true) # line 60, file: wiki/maintenance/update.php
[ ... snip same warning repeated thousands of times ... ]
--------------------------------------------------
... also ... :
--------------------------------------------------
Strict Standards: Type: 8: Undefined index: USE INDEX in wiki/includes/Database.php on line 977
Database.selectSQLText(Array[2], Array[2], Array[3], "Database::select", Array[0], Array[0]) # line 952, file: wiki/includes/Database.php
Database.select(Array[2], Array[2], Array[3]) # line 32, file: wiki/maintenance/deleteDefaultMessages.php
deleteDefaultMessages() # line 1077, file: wiki/maintenance/updaters.inc
do_all_updates(false, true) # line 60, file: wiki/maintenance/update.php
--------------------------------------------------
2008-05-21 04:59:21 +00:00
|
|
|
$dbDetails = array_reverse( explode( '.', $name, 2 ) );
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( isset( $dbDetails[1] ) ) {
|
2010-08-25 17:45:00 +00:00
|
|
|
@list( $table, $database ) = $dbDetails;
|
|
|
|
|
} else {
|
|
|
|
|
@list( $table ) = $dbDetails;
|
|
|
|
|
}
|
2008-05-07 04:44:04 +00:00
|
|
|
$prefix = $this->mTablePrefix; # Default prefix
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2008-05-07 04:44:04 +00:00
|
|
|
# Note that we use the long format because php will complain in in_array if
|
|
|
|
|
# the input is not an array, and will complain in is_array if it is not set.
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( !isset( $database ) # Don't use shared database if pre selected.
|
2008-05-07 04:44:04 +00:00
|
|
|
&& isset( $wgSharedDB ) # We have a shared database
|
2011-04-12 16:34:12 +00:00
|
|
|
&& !$this->isQuotedIdentifier( $table ) # Paranoia check to prevent shared tables listing '`table`'
|
2008-05-07 04:44:04 +00:00
|
|
|
&& isset( $wgSharedTables )
|
|
|
|
|
&& is_array( $wgSharedTables )
|
|
|
|
|
&& in_array( $table, $wgSharedTables ) ) { # A shared table is selected
|
|
|
|
|
$database = $wgSharedDB;
|
2010-10-05 15:10:14 +00:00
|
|
|
$prefix = isset( $wgSharedPrefix ) ? $wgSharedPrefix : $prefix;
|
2007-04-07 07:35:54 +00:00
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2008-05-07 04:44:04 +00:00
|
|
|
# Quote the $database and $table and apply the prefix if not quoted.
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( isset( $database ) ) {
|
2011-04-12 17:00:08 +00:00
|
|
|
$database = ( !$quoted || $this->isQuotedIdentifier( $database ) ? $database : $this->addIdentifierQuotes( $database ) );
|
2011-04-12 16:57:44 +00:00
|
|
|
$prefix = '';
|
2010-08-25 17:45:00 +00:00
|
|
|
}
|
2011-04-12 18:52:16 +00:00
|
|
|
|
|
|
|
|
$table = "{$prefix}{$table}";
|
|
|
|
|
if ( $quoted && !$this->isQuotedIdentifier( $table ) ) {
|
|
|
|
|
$table = $this->addIdentifierQuotes( "{$table}" );
|
|
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2008-05-07 04:44:04 +00:00
|
|
|
# Merge our database and table into our final table name.
|
2010-09-04 13:48:16 +00:00
|
|
|
$tableName = ( isset( $database ) ? "{$database}.{$table}" : "{$table}" );
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2008-05-07 04:44:04 +00:00
|
|
|
return $tableName;
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2004-09-03 16:36:46 +00:00
|
|
|
|
|
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Fetch a number of table names into an array
|
|
|
|
|
* This is handy when you need to construct SQL for joins
|
2004-09-03 16:36:46 +00:00
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* Example:
|
|
|
|
|
* extract($dbr->tableNames('user','watchlist'));
|
|
|
|
|
* $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
|
2010-10-05 15:10:14 +00:00
|
|
|
* WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
public function tableNames() {
|
|
|
|
|
$inArray = func_get_args();
|
|
|
|
|
$retVal = array();
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
foreach ( $inArray as $name ) {
|
|
|
|
|
$retVal[$name] = $this->tableName( $name );
|
2004-01-17 05:49:39 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $retVal;
|
2004-01-17 05:49:39 +00:00
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2007-04-07 07:35:54 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Fetch a number of table names into an zero-indexed numerical array
|
|
|
|
|
* This is handy when you need to construct SQL for joins
|
|
|
|
|
*
|
|
|
|
|
* Example:
|
|
|
|
|
* list( $user, $watchlist ) = $dbr->tableNamesN('user','watchlist');
|
|
|
|
|
* $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
|
2010-10-05 15:10:14 +00:00
|
|
|
* WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
|
2007-04-07 07:35:54 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
public function tableNamesN() {
|
|
|
|
|
$inArray = func_get_args();
|
|
|
|
|
$retVal = array();
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
foreach ( $inArray as $name ) {
|
|
|
|
|
$retVal[] = $this->tableName( $name );
|
2007-04-07 07:35:54 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $retVal;
|
2007-04-07 07:35:54 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-12-02 19:39:32 +00:00
|
|
|
/**
|
2010-12-03 09:57:53 +00:00
|
|
|
* Get an aliased table name
|
|
|
|
|
* e.g. tableName AS newTableName
|
|
|
|
|
*
|
2010-12-02 19:39:32 +00:00
|
|
|
* @param $name string Table name, see tableName()
|
|
|
|
|
* @param $alias string Alias (optional)
|
|
|
|
|
* @return string SQL name for aliased table. Will not alias a table to its own name
|
|
|
|
|
*/
|
2010-12-03 09:40:11 +00:00
|
|
|
public function tableNameWithAlias( $name, $alias = false ) {
|
2010-12-02 19:39:32 +00:00
|
|
|
if ( !$alias || $alias == $name ) {
|
|
|
|
|
return $this->tableName( $name );
|
|
|
|
|
} else {
|
2011-02-09 18:57:25 +00:00
|
|
|
return $this->tableName( $name ) . ' ' . $this->addIdentifierQuotes( $alias );
|
2010-12-02 19:39:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-12-03 09:57:53 +00:00
|
|
|
* Gets an array of aliased table names
|
|
|
|
|
*
|
2010-12-02 19:39:32 +00:00
|
|
|
* @param $tables array( [alias] => table )
|
|
|
|
|
* @return array of strings, see tableNameWithAlias()
|
|
|
|
|
*/
|
|
|
|
|
public function tableNamesWithAlias( $tables ) {
|
|
|
|
|
$retval = array();
|
|
|
|
|
foreach ( $tables as $alias => $table ) {
|
|
|
|
|
if ( is_numeric( $alias ) ) {
|
|
|
|
|
$alias = $table;
|
|
|
|
|
}
|
|
|
|
|
$retval[] = $this->tableNameWithAlias( $table, $alias );
|
|
|
|
|
}
|
|
|
|
|
return $retval;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2011-04-30 23:18:34 +00:00
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-05-10 00:48:07 +00:00
|
|
|
function tableNamesWithUseIndexOrJOIN( $tables, $use_index = array(), $join_conds = array() ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$ret = array();
|
2008-05-10 00:48:07 +00:00
|
|
|
$retJOIN = array();
|
2010-09-04 13:48:16 +00:00
|
|
|
$use_index_safe = is_array( $use_index ) ? $use_index : array();
|
|
|
|
|
$join_conds_safe = is_array( $join_conds ) ? $join_conds : array();
|
|
|
|
|
|
2010-12-02 19:39:32 +00:00
|
|
|
foreach ( $tables as $alias => $table ) {
|
|
|
|
|
if ( !is_string( $alias ) ) {
|
|
|
|
|
// No alias? Set it equal to the table name
|
|
|
|
|
$alias = $table;
|
|
|
|
|
}
|
2008-05-10 00:48:07 +00:00
|
|
|
// Is there a JOIN and INDEX clause for this table?
|
2010-12-02 19:39:32 +00:00
|
|
|
if ( isset( $join_conds_safe[$alias] ) && isset( $use_index_safe[$alias] ) ) {
|
|
|
|
|
$tableClause = $join_conds_safe[$alias][0] . ' ' . $this->tableNameWithAlias( $table, $alias );
|
|
|
|
|
$tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$alias] ) );
|
|
|
|
|
$on = $this->makeList( (array)$join_conds_safe[$alias][1], LIST_AND );
|
2010-08-14 11:24:07 +00:00
|
|
|
if ( $on != '' ) {
|
|
|
|
|
$tableClause .= ' ON (' . $on . ')';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-05-10 00:48:07 +00:00
|
|
|
$retJOIN[] = $tableClause;
|
|
|
|
|
// Is there an INDEX clause?
|
2010-12-02 19:39:32 +00:00
|
|
|
} else if ( isset( $use_index_safe[$alias] ) ) {
|
|
|
|
|
$tableClause = $this->tableNameWithAlias( $table, $alias );
|
|
|
|
|
$tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$alias] ) );
|
2008-05-10 00:48:07 +00:00
|
|
|
$ret[] = $tableClause;
|
|
|
|
|
// Is there a JOIN clause?
|
2010-12-02 19:39:32 +00:00
|
|
|
} else if ( isset( $join_conds_safe[$alias] ) ) {
|
|
|
|
|
$tableClause = $join_conds_safe[$alias][0] . ' ' . $this->tableNameWithAlias( $table, $alias );
|
|
|
|
|
$on = $this->makeList( (array)$join_conds_safe[$alias][1], LIST_AND );
|
2010-08-14 11:24:07 +00:00
|
|
|
if ( $on != '' ) {
|
|
|
|
|
$tableClause .= ' ON (' . $on . ')';
|
2010-08-31 21:06:01 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-05-10 00:48:07 +00:00
|
|
|
$retJOIN[] = $tableClause;
|
|
|
|
|
} else {
|
2010-12-02 19:39:32 +00:00
|
|
|
$tableClause = $this->tableNameWithAlias( $table, $alias );
|
2008-05-10 00:48:07 +00:00
|
|
|
$ret[] = $tableClause;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-05-10 00:48:07 +00:00
|
|
|
// We can't separate explicit JOIN clauses with ',', use ' ' for those
|
2010-09-04 13:48:16 +00:00
|
|
|
$straightJoins = !empty( $ret ) ? implode( ',', $ret ) : "";
|
|
|
|
|
$otherJoins = !empty( $retJOIN ) ? implode( ' ', $retJOIN ) : "";
|
|
|
|
|
|
2008-05-10 00:48:07 +00:00
|
|
|
// Compile our final table clause
|
2010-09-04 13:48:16 +00:00
|
|
|
return implode( ' ', array( $straightJoins, $otherJoins ) );
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2009-01-19 13:56:08 +00:00
|
|
|
/**
|
|
|
|
|
* Get the name of an index in a given table
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @param $index
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2009-01-19 13:56:08 +00:00
|
|
|
*/
|
|
|
|
|
function indexName( $index ) {
|
|
|
|
|
// Backwards-compatibility hack
|
2009-01-19 14:12:59 +00:00
|
|
|
$renamed = array(
|
|
|
|
|
'ar_usertext_timestamp' => 'usertext_timestamp',
|
|
|
|
|
'un_user_id' => 'user_id',
|
|
|
|
|
'un_user_ip' => 'user_ip',
|
|
|
|
|
);
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( isset( $renamed[$index] ) ) {
|
2009-01-19 14:12:59 +00:00
|
|
|
return $renamed[$index];
|
2009-01-19 13:56:08 +00:00
|
|
|
} else {
|
|
|
|
|
return $index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* If it's a string, adds quotes and backslashes
|
|
|
|
|
* Otherwise returns as-is
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @param $s string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function addQuotes( $s ) {
|
2009-01-25 09:12:41 +00:00
|
|
|
if ( $s === null ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return 'NULL';
|
2004-07-10 03:09:26 +00:00
|
|
|
} else {
|
2008-03-30 09:48:15 +00:00
|
|
|
# This will also quote numeric values. This should be harmless,
|
|
|
|
|
# and protects against weird problems that occur when they really
|
|
|
|
|
# _are_ strings such as article titles and string->number->string
|
|
|
|
|
# conversion is not 1:1.
|
|
|
|
|
return "'" . $this->strencode( $s ) . "'";
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-12-04 09:27:02 +00:00
|
|
|
/**
|
2010-12-04 15:35:36 +00:00
|
|
|
* Quotes an identifier using `backticks` or "double quotes" depending on the database type.
|
|
|
|
|
* MySQL uses `backticks` while basically everything else uses double quotes.
|
|
|
|
|
* Since MySQL is the odd one out here the double quotes are our generic
|
|
|
|
|
* and we implement backticks in DatabaseMysql.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2011-01-31 20:58:06 +00:00
|
|
|
*/
|
2010-12-04 15:14:08 +00:00
|
|
|
public function addIdentifierQuotes( $s ) {
|
2010-12-04 15:35:36 +00:00
|
|
|
return '"' . str_replace( '"', '""', $s ) . '"';
|
2010-12-04 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
2011-04-12 16:34:12 +00:00
|
|
|
/**
|
|
|
|
|
* Returns if the given identifier looks quoted or not according to
|
|
|
|
|
* the database convention for quoting identifiers .
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @param $name string
|
|
|
|
|
*
|
2011-04-12 16:34:12 +00:00
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function isQuotedIdentifier( $name ) {
|
|
|
|
|
return $name[0] == '"' && substr( $name, -1, 1 ) == '"';
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-04 15:14:08 +00:00
|
|
|
/**
|
|
|
|
|
* Backwards compatibility, identifier quoting originated in DatabasePostgres
|
|
|
|
|
* which used quote_ident which does not follow our naming conventions
|
|
|
|
|
* was renamed to addIdentifierQuotes.
|
2011-05-06 21:09:34 +00:00
|
|
|
* @deprecated since 1.18 use addIdentifierQuotes
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2010-12-04 15:14:08 +00:00
|
|
|
*/
|
|
|
|
|
function quote_ident( $s ) {
|
|
|
|
|
wfDeprecated( __METHOD__ );
|
|
|
|
|
return $this->addIdentifierQuotes( $s );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2009-10-21 19:53:03 +00:00
|
|
|
* Escape string for safe LIKE usage.
|
|
|
|
|
* WARNING: you should almost never use this function directly,
|
|
|
|
|
* instead use buildLike() that escapes everything automatically
|
2011-05-06 21:09:34 +00:00
|
|
|
* @deprecated since 1.17, warnings in 1.17, removed in ???
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-07-29 18:37:45 +00:00
|
|
|
public function escapeLike( $s ) {
|
|
|
|
|
wfDeprecated( __METHOD__ );
|
|
|
|
|
return $this->escapeLikeInternal( $s );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function escapeLikeInternal( $s ) {
|
2009-08-25 18:57:47 +00:00
|
|
|
$s = str_replace( '\\', '\\\\', $s );
|
|
|
|
|
$s = $this->strencode( $s );
|
|
|
|
|
$s = str_replace( array( '%', '_' ), array( '\%', '\_' ), $s );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $s;
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2009-10-21 19:53:03 +00:00
|
|
|
/**
|
|
|
|
|
* LIKE statement wrapper, receives a variable-length argument list with parts of pattern to match
|
|
|
|
|
* containing either string literals that will be escaped or tokens returned by anyChar() or anyString().
|
|
|
|
|
* Alternatively, the function could be provided with an array of aforementioned parameters.
|
2010-03-06 18:14:11 +00:00
|
|
|
*
|
2009-10-21 19:53:03 +00:00
|
|
|
* Example: $dbr->buildLike( 'My_page_title/', $dbr->anyString() ) returns a LIKE clause that searches
|
|
|
|
|
* for subpages of 'My page title'.
|
|
|
|
|
* Alternatively: $pattern = array( 'My_page_title/', $dbr->anyString() ); $query .= $dbr->buildLike( $pattern );
|
|
|
|
|
*
|
2010-07-30 20:13:30 +00:00
|
|
|
* @since 1.16
|
2010-07-29 18:37:45 +00:00
|
|
|
* @return String: fully built LIKE statement
|
2009-10-21 19:53:03 +00:00
|
|
|
*/
|
|
|
|
|
function buildLike() {
|
|
|
|
|
$params = func_get_args();
|
2010-09-04 13:48:16 +00:00
|
|
|
|
|
|
|
|
if ( count( $params ) > 0 && is_array( $params[0] ) ) {
|
2009-10-21 19:53:03 +00:00
|
|
|
$params = $params[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$s = '';
|
2010-09-04 13:48:16 +00:00
|
|
|
|
|
|
|
|
foreach ( $params as $value ) {
|
|
|
|
|
if ( $value instanceof LikeMatch ) {
|
2009-10-21 19:53:03 +00:00
|
|
|
$s .= $value->toString();
|
|
|
|
|
} else {
|
2010-07-29 18:37:45 +00:00
|
|
|
$s .= $this->escapeLikeInternal( $value );
|
2009-10-21 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2009-10-21 19:53:03 +00:00
|
|
|
return " LIKE '" . $s . "' ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a token for buildLike() that denotes a '_' to be used in a LIKE query
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return LikeMatch
|
2009-10-21 19:53:03 +00:00
|
|
|
*/
|
|
|
|
|
function anyChar() {
|
|
|
|
|
return new LikeMatch( '_' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a token for buildLike() that denotes a '%' to be used in a LIKE query
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @rerturn LikeMatch
|
2009-10-21 19:53:03 +00:00
|
|
|
*/
|
|
|
|
|
function anyString() {
|
|
|
|
|
return new LikeMatch( '%' );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Returns an appropriately quoted sequence value for inserting a new row.
|
|
|
|
|
* MySQL has autoincrement fields, so this is just NULL. But the PostgreSQL
|
|
|
|
|
* subclass will return an integer, and save the value for insertId()
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function nextSequenceValue( $seqName ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2004-01-17 05:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2010-10-05 15:10:14 +00:00
|
|
|
* USE INDEX clause. Unlikely to be useful for anything but MySQL. This
|
2009-06-16 21:00:38 +00:00
|
|
|
* is only needed because a) MySQL must be as efficient as possible due to
|
|
|
|
|
* its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
|
2010-10-05 15:10:14 +00:00
|
|
|
* which index to pick. Anyway, other databases might have different
|
2009-06-16 21:00:38 +00:00
|
|
|
* indexes on a given table. So don't bother overriding this unless you're
|
|
|
|
|
* MySQL.
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function useIndexClause( $index ) {
|
2009-06-16 21:00:38 +00:00
|
|
|
return '';
|
2004-01-17 05:49:39 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* REPLACE query wrapper
|
|
|
|
|
* PostgreSQL simulates this with a DELETE followed by INSERT
|
|
|
|
|
* $row is the row to insert, an associative array
|
|
|
|
|
* $uniqueIndexes is an array of indexes. Each element may be either a
|
|
|
|
|
* field name or an array of field names
|
|
|
|
|
*
|
|
|
|
|
* It may be more efficient to leave off unique indexes which are unlikely to collide.
|
|
|
|
|
* However if you do this, you run the risk of encountering errors which wouldn't have
|
|
|
|
|
* occurred in MySQL
|
|
|
|
|
*
|
2010-08-25 18:50:36 +00:00
|
|
|
* @param $table String: The table to replace the row(s) in.
|
2010-08-25 19:08:52 +00:00
|
|
|
* @param $uniqueIndexes Array: An associative array of indexes
|
2010-08-25 18:50:36 +00:00
|
|
|
* @param $rows Array: Array of rows to replace
|
|
|
|
|
* @param $fname String: Calling function name (use __METHOD__) for logs/profiling
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseBase::replace' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$table = $this->tableName( $table );
|
|
|
|
|
|
|
|
|
|
# Single row case
|
|
|
|
|
if ( !is_array( reset( $rows ) ) ) {
|
|
|
|
|
$rows = array( $rows );
|
2007-07-05 19:42:18 +00:00
|
|
|
}
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
$sql = "REPLACE INTO $table (" . implode( ',', array_keys( $rows[0] ) ) . ') VALUES ';
|
2008-03-30 09:48:15 +00:00
|
|
|
$first = true;
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
foreach ( $rows as $row ) {
|
|
|
|
|
if ( $first ) {
|
|
|
|
|
$first = false;
|
|
|
|
|
} else {
|
|
|
|
|
$sql .= ',';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$sql .= '(' . $this->makeList( $row ) . ')';
|
2004-07-18 08:48:43 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $this->query( $sql, $fname );
|
2004-07-18 08:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* DELETE where the condition is a join
|
|
|
|
|
* MySQL does this with a multi-table DELETE syntax, PostgreSQL does it with sub-selects
|
2004-09-03 16:36:46 +00:00
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* For safety, an empty $conds will not delete everything. If you want to delete all rows where the
|
|
|
|
|
* join condition matches, set $conds='*'
|
2004-09-03 16:36:46 +00:00
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* DO NOT put the join condition in $conds
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $delTable String: The table to delete from.
|
|
|
|
|
* @param $joinTable String: The other table.
|
|
|
|
|
* @param $delVar String: The variable to join on, in the first table.
|
|
|
|
|
* @param $joinVar String: The variable to join on, in the second table.
|
|
|
|
|
* @param $conds Array: Condition array of field names mapped to variables, ANDed together in the WHERE clause
|
|
|
|
|
* @param $fname String: Calling function name (use __METHOD__) for logs/profiling
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = 'DatabaseBase::deleteJoin' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( !$conds ) {
|
2010-08-25 01:24:47 +00:00
|
|
|
throw new DBUnexpectedError( $this, 'DatabaseBase::deleteJoin() called with empty $conds' );
|
2004-07-24 07:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$delTable = $this->tableName( $delTable );
|
|
|
|
|
$joinTable = $this->tableName( $joinTable );
|
|
|
|
|
$sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $conds != '*' ) {
|
|
|
|
|
$sql .= ' AND ' . $this->makeList( $conds, LIST_AND );
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2004-07-24 07:24:04 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $this->query( $sql, $fname );
|
|
|
|
|
}
|
2004-07-10 03:09:26 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the size of a text field, or -1 for "unlimited"
|
|
|
|
|
*/
|
|
|
|
|
function textFieldSize( $table, $field ) {
|
|
|
|
|
$table = $this->tableName( $table );
|
|
|
|
|
$sql = "SHOW COLUMNS FROM $table LIKE \"$field\";";
|
2010-08-25 01:24:47 +00:00
|
|
|
$res = $this->query( $sql, 'DatabaseBase::textFieldSize' );
|
2008-03-30 09:48:15 +00:00
|
|
|
$row = $this->fetchObject( $res );
|
|
|
|
|
|
|
|
|
|
$m = array();
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( preg_match( '/\((.*)\)/', $row->Type, $m ) ) {
|
|
|
|
|
$size = $m[1];
|
2004-07-10 03:09:26 +00:00
|
|
|
} else {
|
2008-03-30 09:48:15 +00:00
|
|
|
$size = -1;
|
2004-01-17 05:49:39 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-06-16 21:00:38 +00:00
|
|
|
* A string to insert into queries to show that they're low-priority, like
|
|
|
|
|
* MySQL's LOW_PRIORITY. If no such feature exists, return an empty
|
|
|
|
|
* string and nothing bad should happen.
|
|
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* @return string Returns the text of the low priority option if it is supported, or a blank string otherwise
|
|
|
|
|
*/
|
|
|
|
|
function lowPriorityOption() {
|
2009-06-16 21:00:38 +00:00
|
|
|
return '';
|
2004-01-17 05:49:39 +00:00
|
|
|
}
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* DELETE query wrapper
|
2005-07-18 02:30:04 +00:00
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* Use $conds == "*" to delete all rows
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function delete( $table, $conds, $fname = 'DatabaseBase::delete' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( !$conds ) {
|
2010-08-25 01:24:47 +00:00
|
|
|
throw new DBUnexpectedError( $this, 'DatabaseBase::delete() called with no conditions' );
|
2005-07-22 11:29:15 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$table = $this->tableName( $table );
|
|
|
|
|
$sql = "DELETE FROM $table";
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $conds != '*' ) {
|
|
|
|
|
$sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $this->query( $sql, $fname );
|
2005-07-18 02:30:04 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-07-18 02:30:04 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* INSERT SELECT wrapper
|
|
|
|
|
* $varMap must be an associative array of the form array( 'dest1' => 'source1', ...)
|
2010-08-25 01:24:47 +00:00
|
|
|
* Source items may be literals rather than field names, but strings should be quoted with DatabaseBase::addQuotes()
|
2008-03-30 09:48:15 +00:00
|
|
|
* $conds may be "*" to copy the whole table
|
|
|
|
|
* srcTable may be an array of tables.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return ResultWrapper
|
2005-07-18 02:30:04 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'DatabaseBase::insertSelect',
|
2008-03-30 09:48:15 +00:00
|
|
|
$insertOptions = array(), $selectOptions = array() )
|
|
|
|
|
{
|
|
|
|
|
$destTable = $this->tableName( $destTable );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( is_array( $insertOptions ) ) {
|
|
|
|
|
$insertOptions = implode( ' ', $insertOptions );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
|
|
|
|
if ( !is_array( $selectOptions ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$selectOptions = array( $selectOptions );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( is_array( $srcTable ) ) {
|
2010-10-05 15:10:14 +00:00
|
|
|
$srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
|
2008-03-30 09:48:15 +00:00
|
|
|
} else {
|
|
|
|
|
$srcTable = $this->tableName( $srcTable );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$sql = "INSERT $insertOptions INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
|
|
|
|
|
" SELECT $startOpts " . implode( ',', $varMap ) .
|
|
|
|
|
" FROM $srcTable $useIndex ";
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2005-06-19 00:21:49 +00:00
|
|
|
if ( $conds != '*' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
|
2005-06-19 00:21:49 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$sql .= " $tailOpts";
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2007-08-01 21:42:59 +00:00
|
|
|
return $this->query( $sql, $fname );
|
2004-03-23 10:13:59 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2009-06-16 21:00:38 +00:00
|
|
|
* Construct a LIMIT query with optional offset. This is used for query
|
|
|
|
|
* pages. The SQL should be adjusted so that only the first $limit rows
|
|
|
|
|
* are returned. If $offset is provided as well, then the first $offset
|
|
|
|
|
* rows should be discarded, and the next $limit rows should be returned.
|
|
|
|
|
* If the result of the query is not ordered, then the rows to be returned
|
|
|
|
|
* are theoretically arbitrary.
|
|
|
|
|
*
|
2010-10-05 15:10:14 +00:00
|
|
|
* $sql is expected to be a SELECT, if that makes a difference. For
|
2009-06-16 21:00:38 +00:00
|
|
|
* UPDATE, limitResultForUpdate should be used.
|
|
|
|
|
*
|
|
|
|
|
* The version provided by default works in MySQL and SQLite. It will very
|
|
|
|
|
* likely need to be overridden for most other DBMSes.
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $sql String: SQL query we will append the limit too
|
|
|
|
|
* @param $limit Integer: the SQL limit
|
|
|
|
|
* @param $offset Integer the SQL offset (default false)
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function limitResult( $sql, $limit, $offset = false ) {
|
|
|
|
|
if ( !is_numeric( $limit ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
throw new DBUnexpectedError( $this, "Invalid non-numeric limit passed to limitResult()\n" );
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-05-08 15:50:53 +00:00
|
|
|
return "$sql LIMIT "
|
2010-09-04 13:48:16 +00:00
|
|
|
. ( ( is_numeric( $offset ) && $offset != 0 ) ? "{$offset}," : "" )
|
2008-03-30 09:48:15 +00:00
|
|
|
. "{$limit} ";
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2009-06-16 21:00:38 +00:00
|
|
|
function limitResultForUpdate( $sql, $num ) {
|
|
|
|
|
return $this->limitResult( $sql, $num, 0 );
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
2004-07-10 03:09:26 +00:00
|
|
|
|
2009-10-17 12:23:23 +00:00
|
|
|
/**
|
2010-03-06 18:14:11 +00:00
|
|
|
* Returns true if current database backend supports ORDER BY or LIMIT for separate subqueries
|
2009-10-17 12:23:23 +00:00
|
|
|
* within the UNION construct.
|
|
|
|
|
* @return Boolean
|
|
|
|
|
*/
|
|
|
|
|
function unionSupportsOrderAndLimit() {
|
|
|
|
|
return true; // True for almost every DB supported
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-19 16:58:56 +00:00
|
|
|
/**
|
|
|
|
|
* Construct a UNION query
|
|
|
|
|
* This is used for providing overload point for other DB abstractions
|
|
|
|
|
* not compatible with the MySQL syntax.
|
|
|
|
|
* @param $sqls Array: SQL statements to combine
|
|
|
|
|
* @param $all Boolean: use UNION ALL
|
|
|
|
|
* @return String: SQL fragment
|
|
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function unionQueries( $sqls, $all ) {
|
2009-05-19 16:58:56 +00:00
|
|
|
$glue = $all ? ') UNION ALL (' : ') UNION (';
|
2010-09-04 13:48:16 +00:00
|
|
|
return '(' . implode( $glue, $sqls ) . ')';
|
2009-05-19 16:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
2010-10-05 15:10:14 +00:00
|
|
|
* Returns an SQL expression for a simple conditional. This doesn't need
|
2009-06-16 21:00:38 +00:00
|
|
|
* to be overridden unless CASE isn't supported in your DBMS.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $cond String: SQL expression which will result in a boolean value
|
|
|
|
|
* @param $trueVal String: SQL expression to return if true
|
|
|
|
|
* @param $falseVal String: SQL expression to return if false
|
|
|
|
|
* @return String: SQL fragment
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
function conditional( $cond, $trueVal, $falseVal ) {
|
2009-06-16 21:00:38 +00:00
|
|
|
return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
|
2004-01-17 05:49:39 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2008-05-03 13:09:34 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a comand for str_replace function in SQL query.
|
|
|
|
|
* Uses REPLACE() in MySQL
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $orig String: column to modify
|
|
|
|
|
* @param $old String: column to seek
|
|
|
|
|
* @param $new String: column to replace with
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2008-05-03 13:09:34 +00:00
|
|
|
*/
|
|
|
|
|
function strreplace( $orig, $old, $new ) {
|
|
|
|
|
return "REPLACE({$orig}, {$old}, {$new})";
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Determines if the last failure was due to a deadlock
|
* 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
|
|
|
* STUB
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function wasDeadlock() {
|
* 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
|
|
|
return false;
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2004-02-11 13:03:58 +00:00
|
|
|
|
2009-01-15 06:56:58 +00:00
|
|
|
/**
|
2010-03-06 18:14:11 +00:00
|
|
|
* Determines if the last query error was something that should be dealt
|
* 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
|
|
|
* with by pinging the connection and reissuing the query.
|
|
|
|
|
* STUB
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2009-01-15 06:56:58 +00:00
|
|
|
*/
|
|
|
|
|
function wasErrorReissuable() {
|
* 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
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines if the last failure was due to the database being read-only.
|
|
|
|
|
* STUB
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
* 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
|
|
|
*/
|
|
|
|
|
function wasReadOnlyError() {
|
|
|
|
|
return false;
|
2009-01-15 06:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Perform a deadlock-prone transaction.
|
2005-08-02 13:35:19 +00:00
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* This function invokes a callback function to perform a set of write
|
|
|
|
|
* queries. If a deadlock occurs during the processing, the transaction
|
|
|
|
|
* will be rolled back and the callback function will be called again.
|
2005-08-02 13:35:19 +00:00
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* Usage:
|
2010-10-05 15:10:14 +00:00
|
|
|
* $dbw->deadlockLoop( callback, ... );
|
2004-10-24 07:10:33 +00:00
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* Extra arguments are passed through to the specified callback function.
|
|
|
|
|
*
|
|
|
|
|
* Returns whatever the callback function returned on its successful,
|
|
|
|
|
* iteration, or false on error, for example if the retry limit was
|
|
|
|
|
* reached.
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function deadlockLoop() {
|
2010-08-25 01:24:47 +00:00
|
|
|
$myFname = 'DatabaseBase::deadlockLoop';
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
$this->begin();
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
$function = array_shift( $args );
|
|
|
|
|
$oldIgnore = $this->ignoreErrors( true );
|
|
|
|
|
$tries = DEADLOCK_TRIES;
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( is_array( $function ) ) {
|
|
|
|
|
$fname = $function[0];
|
|
|
|
|
} else {
|
|
|
|
|
$fname = $function;
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
do {
|
|
|
|
|
$retVal = call_user_func_array( $function, $args );
|
|
|
|
|
$error = $this->lastError();
|
|
|
|
|
$errno = $this->lastErrno();
|
|
|
|
|
$sql = $this->lastQuery();
|
|
|
|
|
|
|
|
|
|
if ( $errno ) {
|
|
|
|
|
if ( $this->wasDeadlock() ) {
|
|
|
|
|
# Retry
|
|
|
|
|
usleep( mt_rand( DEADLOCK_DELAY_MIN, DEADLOCK_DELAY_MAX ) );
|
|
|
|
|
} else {
|
|
|
|
|
$this->reportQueryError( $error, $errno, $sql, $fname );
|
|
|
|
|
}
|
2004-07-18 08:48:43 +00:00
|
|
|
}
|
2010-08-30 20:28:32 +00:00
|
|
|
} while ( $this->wasDeadlock() && --$tries > 0 );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->ignoreErrors( $oldIgnore );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $tries <= 0 ) {
|
2010-08-06 23:44:00 +00:00
|
|
|
$this->rollback( $myFname );
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->reportQueryError( $error, $errno, $sql, $fname );
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2010-08-06 23:44:00 +00:00
|
|
|
$this->commit( $myFname );
|
2008-03-30 09:48:15 +00:00
|
|
|
return $retVal;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do a SELECT MASTER_POS_WAIT()
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $pos MySQLMasterPos object
|
|
|
|
|
* @param $timeout Integer: the maximum number of seconds to wait for synchronisation
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
function masterPosWait( MySQLMasterPos $pos, $timeout ) {
|
2010-08-25 01:24:47 +00:00
|
|
|
$fname = 'DatabaseBase::masterPosWait';
|
2008-03-30 09:48:15 +00:00
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
|
|
|
|
# Commit any open transactions
|
|
|
|
|
if ( $this->mTrxLevel ) {
|
2009-12-14 23:05:35 +00:00
|
|
|
$this->commit();
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_null( $this->mFakeSlaveLag ) ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
$wait = intval( ( $pos->pos - microtime( true ) + $this->mFakeSlaveLag ) * 1e6 );
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $wait > $timeout * 1e6 ) {
|
|
|
|
|
wfDebug( "Fake slave timed out waiting for $pos ($wait us)\n" );
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return -1;
|
|
|
|
|
} elseif ( $wait > 0 ) {
|
|
|
|
|
wfDebug( "Fake slave waiting $wait us\n" );
|
|
|
|
|
usleep( $wait );
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return 1;
|
2005-04-10 18:26:26 +00:00
|
|
|
} else {
|
2008-03-30 09:48:15 +00:00
|
|
|
wfDebug( "Fake slave up to date ($wait us)\n" );
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return 0;
|
2005-04-10 18:26:26 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
# Call doQuery() directly, to avoid opening a transaction if DBO_TRX is set
|
|
|
|
|
$encFile = $this->addQuotes( $pos->file );
|
|
|
|
|
$encPos = intval( $pos->pos );
|
|
|
|
|
$sql = "SELECT MASTER_POS_WAIT($encFile, $encPos, $timeout)";
|
|
|
|
|
$res = $this->doQuery( $sql );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $res && $row = $this->fetchRow( $res ) ) {
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $row[0];
|
|
|
|
|
} else {
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Get the position of the master from SHOW SLAVE STATUS
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return MySQLMasterPos|false
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function getSlavePos() {
|
|
|
|
|
if ( !is_null( $this->mFakeSlaveLag ) ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
$pos = new MySQLMasterPos( 'fake', microtime( true ) - $this->mFakeSlaveLag );
|
|
|
|
|
wfDebug( __METHOD__ . ": fake slave pos = $pos\n" );
|
2008-03-30 09:48:15 +00:00
|
|
|
return $pos;
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-25 01:24:47 +00:00
|
|
|
$res = $this->query( 'SHOW SLAVE STATUS', 'DatabaseBase::getSlavePos' );
|
2008-03-30 09:48:15 +00:00
|
|
|
$row = $this->fetchObject( $res );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $row ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
$pos = isset( $row->Exec_master_log_pos ) ? $row->Exec_master_log_pos : $row->Exec_Master_Log_Pos;
|
2008-12-09 15:00:17 +00:00
|
|
|
return new MySQLMasterPos( $row->Relay_Master_Log_File, $pos );
|
2008-03-30 09:48:15 +00:00
|
|
|
} else {
|
|
|
|
|
return false;
|
2004-07-18 08:48:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
Changing lines like this: "extract( $dbw->tableNames( 'page', 'archive' ) );" to be like this: "list ($page, $archive) = $dbw->tableNamesN( 'page', 'archive' );".
Three reasons for this:
1) It's better for analysis tools [which want explicit variable declaration]
2) It's easier for a human to read, as it's completely explicit where the variables came from [which is something you don't get with extract() ]
3) It makes it easier to find everywhere where a variable is used with search/grep [which you can't currently do with $tbl_page variables from things like: "extract($db->tableNames( 'page', 'revision'), EXTR_PREFIX_ALL, 'tbl');"].
Otherwise, from a functionality/efficiency perspective the two forms should be identical.
By doing this have been able run static analysis over the usages of these variables, thus eliminating 5 unneeded table names from calls, plus removing 3 unused calls entirely, and it just feels subjectively slightly nicer to me.
2006-11-27 08:36:57 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Get the position of the master from SHOW MASTER STATUS
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return MySQLMasterPos|false
|
Changing lines like this: "extract( $dbw->tableNames( 'page', 'archive' ) );" to be like this: "list ($page, $archive) = $dbw->tableNamesN( 'page', 'archive' );".
Three reasons for this:
1) It's better for analysis tools [which want explicit variable declaration]
2) It's easier for a human to read, as it's completely explicit where the variables came from [which is something you don't get with extract() ]
3) It makes it easier to find everywhere where a variable is used with search/grep [which you can't currently do with $tbl_page variables from things like: "extract($db->tableNames( 'page', 'revision'), EXTR_PREFIX_ALL, 'tbl');"].
Otherwise, from a functionality/efficiency perspective the two forms should be identical.
By doing this have been able run static analysis over the usages of these variables, thus eliminating 5 unneeded table names from calls, plus removing 3 unused calls entirely, and it just feels subjectively slightly nicer to me.
2006-11-27 08:36:57 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function getMasterPos() {
|
|
|
|
|
if ( $this->mFakeMaster ) {
|
|
|
|
|
return new MySQLMasterPos( 'fake', microtime( true ) );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-08-25 01:24:47 +00:00
|
|
|
$res = $this->query( 'SHOW MASTER STATUS', 'DatabaseBase::getMasterPos' );
|
2008-03-30 09:48:15 +00:00
|
|
|
$row = $this->fetchObject( $res );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $row ) {
|
|
|
|
|
return new MySQLMasterPos( $row->File, $row->Position );
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
Changing lines like this: "extract( $dbw->tableNames( 'page', 'archive' ) );" to be like this: "list ($page, $archive) = $dbw->tableNamesN( 'page', 'archive' );".
Three reasons for this:
1) It's better for analysis tools [which want explicit variable declaration]
2) It's easier for a human to read, as it's completely explicit where the variables came from [which is something you don't get with extract() ]
3) It makes it easier to find everywhere where a variable is used with search/grep [which you can't currently do with $tbl_page variables from things like: "extract($db->tableNames( 'page', 'revision'), EXTR_PREFIX_ALL, 'tbl');"].
Otherwise, from a functionality/efficiency perspective the two forms should be identical.
By doing this have been able run static analysis over the usages of these variables, thus eliminating 5 unneeded table names from calls, plus removing 3 unused calls entirely, and it just feels subjectively slightly nicer to me.
2006-11-27 08:36:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-10-02 16:10:39 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Begin a transaction, committing any previously open transaction
|
2005-10-02 16:10:39 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function begin( $fname = 'DatabaseBase::begin' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->query( 'BEGIN', $fname );
|
|
|
|
|
$this->mTrxLevel = 1;
|
2005-10-02 16:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* End a transaction
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function commit( $fname = 'DatabaseBase::commit' ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( $this->mTrxLevel ) {
|
2010-08-06 12:54:39 +00:00
|
|
|
$this->query( 'COMMIT', $fname );
|
|
|
|
|
$this->mTrxLevel = 0;
|
|
|
|
|
}
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Rollback a transaction.
|
|
|
|
|
* No-op on non-transactional databases.
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function rollback( $fname = 'DatabaseBase::rollback' ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( $this->mTrxLevel ) {
|
2010-08-06 12:54:39 +00:00
|
|
|
$this->query( 'ROLLBACK', $fname, true );
|
|
|
|
|
$this->mTrxLevel = 0;
|
|
|
|
|
}
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2009-11-06 10:17:44 +00:00
|
|
|
/**
|
|
|
|
|
* Creates a new table with structure copied from existing table
|
|
|
|
|
* Note that unlike most database abstraction functions, this function does not
|
|
|
|
|
* automatically append database prefix, because it works at a lower
|
|
|
|
|
* abstraction level.
|
2011-04-12 18:52:16 +00:00
|
|
|
* The table names passed to this function shall not be quoted (this
|
|
|
|
|
* function calls addIdentifierQuotes when needed).
|
2009-11-06 10:17:44 +00:00
|
|
|
*
|
|
|
|
|
* @param $oldName String: name of table whose structure should be copied
|
|
|
|
|
* @param $newName String: name of table to be created
|
|
|
|
|
* @param $temporary Boolean: whether the new table should be temporary
|
2010-07-04 14:41:26 +00:00
|
|
|
* @param $fname String: calling function name
|
2009-11-06 10:17:44 +00:00
|
|
|
* @return Boolean: true if operation was successful
|
|
|
|
|
*/
|
2010-08-25 01:24:47 +00:00
|
|
|
function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseBase::duplicateTableStructure' ) {
|
2009-12-11 19:53:10 +00:00
|
|
|
throw new MWException( 'DatabaseBase::duplicateTableStructure is not implemented in descendant class' );
|
2010-12-28 17:15:50 +00:00
|
|
|
}
|
2011-01-31 20:58:06 +00:00
|
|
|
|
2010-12-28 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* List all tables on the database
|
|
|
|
|
*
|
|
|
|
|
* @param $prefix Only show tables with this prefix, e.g. mw_
|
|
|
|
|
* @param $fname String: calling function name
|
|
|
|
|
*/
|
|
|
|
|
function listTables( $prefix = null, $fname = 'DatabaseBase::listTables' ) {
|
|
|
|
|
throw new MWException( 'DatabaseBase::listTables is not implemented in descendant class' );
|
2009-11-06 10:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Return MW-style timestamp used for MySQL schema
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function timestamp( $ts = 0 ) {
|
|
|
|
|
return wfTimestamp( TS_MW, $ts );
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Local database timestamp format or null
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function timestampOrNull( $ts = null ) {
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( is_null( $ts ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
return $this->timestamp( $ts );
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* @todo document
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return ResultWrapper
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function resultObject( $result ) {
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( empty( $result ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return false;
|
|
|
|
|
} elseif ( $result instanceof ResultWrapper ) {
|
|
|
|
|
return $result;
|
|
|
|
|
} elseif ( $result === true ) {
|
|
|
|
|
// Successful write query
|
|
|
|
|
return $result;
|
|
|
|
|
} else {
|
|
|
|
|
return new ResultWrapper( $this, $result );
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Return aggregated value alias
|
|
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function aggregateValue ( $valuedata, $valuename = 'value' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return $valuename;
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Ping the server and try to reconnect if it there is no connection
|
2009-06-16 20:22:11 +00:00
|
|
|
*
|
|
|
|
|
* @return bool Success or failure
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2009-06-16 20:22:11 +00:00
|
|
|
function ping() {
|
2010-10-05 15:10:14 +00:00
|
|
|
# Stub. Not essential to override.
|
2009-06-16 20:22:11 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2004-07-10 03:09:26 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Get slave lag.
|
2010-07-02 13:17:28 +00:00
|
|
|
* Currently supported only by MySQL
|
|
|
|
|
* @return Database replication lag in seconds
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function getLag() {
|
2010-09-28 08:32:09 +00:00
|
|
|
return intval( $this->mFakeSlaveLag );
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Get status information from SHOW STATUS in an associative array
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function getStatus( $which = "%" ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$res = $this->query( "SHOW STATUS LIKE '{$which}'" );
|
|
|
|
|
$status = array();
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-10-13 23:11:40 +00:00
|
|
|
foreach ( $res as $row ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$status[$row->Variable_name] = $row->Value;
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $status;
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2004-07-15 14:50:22 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Return the maximum number of items allowed in a list, or 0 for unlimited.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* return int
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function maxListLen() {
|
|
|
|
|
return 0;
|
2005-08-02 13:35:19 +00:00
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
function encodeBlob( $b ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return $b;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
function decodeBlob( $b ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return $b;
|
2004-07-15 14:50:22 +00:00
|
|
|
}
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2004-09-09 00:02:38 +00:00
|
|
|
/**
|
2010-10-05 15:10:14 +00:00
|
|
|
* Override database's default connection timeout. May be useful for very
|
2009-06-16 21:00:38 +00:00
|
|
|
* long batch queries such as full-wiki dumps, where a single query reads
|
2010-10-05 15:10:14 +00:00
|
|
|
* out over hours or days. May or may not be necessary for non-MySQL
|
2009-06-16 21:00:38 +00:00
|
|
|
* databases. For most purposes, leaving it as a no-op should be fine.
|
|
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $timeout Integer in seconds
|
2004-09-09 00:02:38 +00:00
|
|
|
*/
|
2009-06-16 21:00:38 +00:00
|
|
|
public function setTimeout( $timeout ) {}
|
2004-09-09 00:02:38 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Read and execute SQL commands from a file.
|
2008-06-12 00:15:29 +00:00
|
|
|
* Returns true on success, error string or exception on failure (depending on object's error ignore settings)
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $filename String: File name to open
|
|
|
|
|
* @param $lineCallback Callback: Optional function called before reading each line
|
|
|
|
|
* @param $resultCallback Callback: Optional function called for each MySQL result
|
2010-09-08 18:11:36 +00:00
|
|
|
* @param $fname String: Calling function name or false if name should be generated dynamically
|
2010-10-05 15:10:14 +00:00
|
|
|
* using $filename
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-09-08 18:11:36 +00:00
|
|
|
function sourceFile( $filename, $lineCallback = false, $resultCallback = false, $fname = false ) {
|
2010-12-17 15:40:08 +00:00
|
|
|
wfSuppressWarnings();
|
2008-03-30 09:48:15 +00:00
|
|
|
$fp = fopen( $filename, 'r' );
|
2010-12-17 15:40:08 +00:00
|
|
|
wfRestoreWarnings();
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( false === $fp ) {
|
2010-12-17 15:40:08 +00:00
|
|
|
throw new MWException( "Could not open \"{$filename}\".\n" );
|
2010-04-29 21:49:58 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-09-08 18:11:36 +00:00
|
|
|
if ( !$fname ) {
|
|
|
|
|
$fname = __METHOD__ . "( $filename )";
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-29 21:49:58 +00:00
|
|
|
try {
|
2010-09-08 18:11:36 +00:00
|
|
|
$error = $this->sourceStream( $fp, $lineCallback, $resultCallback, $fname );
|
2010-04-29 21:49:58 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
catch ( MWException $e ) {
|
2010-12-17 15:43:16 +00:00
|
|
|
fclose( $fp );
|
|
|
|
|
throw $e;
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
2010-09-04 01:06:34 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
fclose( $fp );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $error;
|
2004-07-18 08:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-09 15:22:34 +00:00
|
|
|
/**
|
|
|
|
|
* Get the full path of a patch file. Originally based on archive()
|
2010-03-06 18:14:11 +00:00
|
|
|
* from updaters.inc. Keep in mind this always returns a patch, as
|
2009-08-09 15:22:34 +00:00
|
|
|
* it fails back to MySQL if no DB-specific patch can be found
|
|
|
|
|
*
|
|
|
|
|
* @param $patch String The name of the patch, like patch-something.sql
|
|
|
|
|
* @return String Full path to patch file
|
|
|
|
|
*/
|
2010-10-11 15:29:48 +00:00
|
|
|
public function patchPath( $patch ) {
|
|
|
|
|
global $IP;
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2010-10-11 15:29:48 +00:00
|
|
|
$dbType = $this->getType();
|
|
|
|
|
if ( file_exists( "$IP/maintenance/$dbType/archives/$patch" ) ) {
|
|
|
|
|
return "$IP/maintenance/$dbType/archives/$patch";
|
2009-08-09 15:22:34 +00:00
|
|
|
} else {
|
2010-03-14 15:48:29 +00:00
|
|
|
return "$IP/maintenance/archives/$patch";
|
2009-08-09 15:22:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database.
* Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade.
* Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this.
* Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used.
* Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes.
* Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database.
* In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary.
* On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added.
* Fixed several instances of inappropriate call-by-reference.
* Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler.
* Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892.
* In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString().
* Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging.
* MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
|
|
|
/**
|
|
|
|
|
* Set variables to be used in sourceFile/sourceStream, in preference to the
|
2011-01-31 20:58:06 +00:00
|
|
|
* ones in $GLOBALS. If an array is set here, $GLOBALS will not be used at
|
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database.
* Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade.
* Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this.
* Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used.
* Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes.
* Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database.
* In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary.
* On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added.
* Fixed several instances of inappropriate call-by-reference.
* Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler.
* Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892.
* In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString().
* Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging.
* MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
|
|
|
* all. If it's set to false, $GLOBALS will be used.
|
|
|
|
|
*
|
|
|
|
|
* @param $vars False, or array mapping variable name to value.
|
|
|
|
|
*/
|
|
|
|
|
function setSchemaVars( $vars ) {
|
|
|
|
|
$this->mSchemaVars = $vars;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Read and execute commands from an open file handle
|
2008-06-12 00:15:29 +00:00
|
|
|
* Returns true on success, error string or exception on failure (depending on object's error ignore settings)
|
2011-02-06 21:47:58 +00:00
|
|
|
* @param $fp Resource: File handle
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $lineCallback Callback: Optional function called before reading each line
|
|
|
|
|
* @param $resultCallback Callback: Optional function called for each MySQL result
|
2010-09-08 18:11:36 +00:00
|
|
|
* @param $fname String: Calling function name
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-09-08 18:11:36 +00:00
|
|
|
function sourceStream( $fp, $lineCallback = false, $resultCallback = false, $fname = 'DatabaseBase::sourceStream' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$cmd = "";
|
|
|
|
|
$done = false;
|
|
|
|
|
$dollarquote = false;
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
while ( ! feof( $fp ) ) {
|
|
|
|
|
if ( $lineCallback ) {
|
|
|
|
|
call_user_func( $lineCallback );
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2011-05-13 17:24:43 +00:00
|
|
|
$line = trim( fgets( $fp ) );
|
2008-03-30 09:48:15 +00:00
|
|
|
$sl = strlen( $line ) - 1;
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( $sl < 0 ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
|
|
|
|
if ( '-' == $line { 0 } && '-' == $line { 1 } ) {
|
2010-08-30 20:28:32 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
2010-09-04 13:48:16 +00:00
|
|
|
# # Allow dollar quoting for function declarations
|
|
|
|
|
if ( substr( $line, 0, 4 ) == '$mw$' ) {
|
|
|
|
|
if ( $dollarquote ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$dollarquote = false;
|
|
|
|
|
$done = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$dollarquote = true;
|
2004-07-18 08:48:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
else if ( !$dollarquote ) {
|
|
|
|
|
if ( ';' == $line { $sl } && ( $sl < 2 || ';' != $line { $sl - 1 } ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$done = true;
|
|
|
|
|
$line = substr( $line, 0, $sl );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-30 20:28:32 +00:00
|
|
|
if ( $cmd != '' ) {
|
|
|
|
|
$cmd .= ' ';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$cmd .= "$line\n";
|
|
|
|
|
|
|
|
|
|
if ( $done ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
$cmd = str_replace( ';;', ";", $cmd );
|
2008-03-30 09:48:15 +00:00
|
|
|
$cmd = $this->replaceVars( $cmd );
|
2010-09-08 18:11:36 +00:00
|
|
|
$res = $this->query( $cmd, $fname );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $resultCallback ) {
|
2008-08-01 21:00:24 +00:00
|
|
|
call_user_func( $resultCallback, $res, $this );
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( false === $res ) {
|
|
|
|
|
$err = $this->lastError();
|
|
|
|
|
return "Query \"{$cmd}\" failed with error code \"$err\".\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cmd = '';
|
|
|
|
|
$done = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-04 09:27:02 +00:00
|
|
|
/**
|
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database.
* Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade.
* Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this.
* Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used.
* Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes.
* Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database.
* In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary.
* On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added.
* Fixed several instances of inappropriate call-by-reference.
* Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler.
* Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892.
* In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString().
* Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging.
* MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
|
|
|
* Database independent variable replacement, replaces a set of variables
|
|
|
|
|
* in a sql statement with their contents as given by $this->getSchemaVars().
|
2010-12-04 09:27:02 +00:00
|
|
|
* Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables
|
2011-01-31 20:58:06 +00:00
|
|
|
*
|
2010-12-04 09:27:02 +00:00
|
|
|
* '{$var}' should be used for text and is passed through the database's addQuotes method
|
|
|
|
|
* `{$var}` should be used for identifiers (eg: table and database names), it is passed through
|
2010-12-04 15:14:08 +00:00
|
|
|
* the database's addIdentifierQuotes method which can be overridden if the database
|
2010-12-04 09:27:02 +00:00
|
|
|
* uses something other than backticks.
|
|
|
|
|
* / *$var* / is just encoded, besides traditional dbprefix and tableoptions it's use should be avoided
|
2011-01-31 20:58:06 +00:00
|
|
|
*
|
2010-12-04 09:27:02 +00:00
|
|
|
* @param $ins String: SQL statement to replace variables in
|
|
|
|
|
* @return String The new SQL statement with variables replaced
|
|
|
|
|
*/
|
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database.
* Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade.
* Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this.
* Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used.
* Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes.
* Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database.
* In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary.
* On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added.
* Fixed several instances of inappropriate call-by-reference.
* Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler.
* Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892.
* In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString().
* Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging.
* MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
|
|
|
protected function replaceSchemaVars( $ins ) {
|
|
|
|
|
$vars = $this->getSchemaVars();
|
|
|
|
|
foreach ( $vars as $var => $value ) {
|
|
|
|
|
// replace '{$var}'
|
|
|
|
|
$ins = str_replace( '\'{$' . $var . '}\'', $this->addQuotes( $value ), $ins );
|
|
|
|
|
// replace `{$var}`
|
|
|
|
|
$ins = str_replace( '`{$' . $var . '}`', $this->addIdentifierQuotes( $value ), $ins );
|
|
|
|
|
// replace /*$var*/
|
|
|
|
|
$ins = str_replace( '/*$' . $var . '*/', $this->strencode( $value ) , $ins );
|
2010-12-04 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
return $ins;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Replace variables in sourced SQL
|
|
|
|
|
*/
|
|
|
|
|
protected function replaceVars( $ins ) {
|
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database.
* Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade.
* Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this.
* Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used.
* Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes.
* Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database.
* In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary.
* On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added.
* Fixed several instances of inappropriate call-by-reference.
* Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler.
* Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892.
* In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString().
* Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging.
* MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
|
|
|
$ins = $this->replaceSchemaVars( $ins );
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
// Table prefixes
|
2009-01-15 14:20:28 +00:00
|
|
|
$ins = preg_replace_callback( '!/\*(?:\$wgDBprefix|_)\*/([a-zA-Z_0-9]*)!',
|
|
|
|
|
array( $this, 'tableNameCallback' ), $ins );
|
|
|
|
|
|
2009-01-19 13:56:08 +00:00
|
|
|
// Index names
|
2010-03-06 18:14:11 +00:00
|
|
|
$ins = preg_replace_callback( '!/\*i\*/([a-zA-Z_0-9]*)!',
|
2009-01-19 13:56:08 +00:00
|
|
|
array( $this, 'indexNameCallback' ), $ins );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return $ins;
|
2004-07-18 08:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database.
* Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade.
* Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this.
* Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used.
* Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes.
* Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database.
* In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary.
* On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added.
* Fixed several instances of inappropriate call-by-reference.
* Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler.
* Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892.
* In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString().
* Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging.
* MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
|
|
|
/**
|
|
|
|
|
* Get schema variables. If none have been set via setSchemaVars(), then
|
|
|
|
|
* use some defaults from the current object.
|
|
|
|
|
*/
|
|
|
|
|
protected function getSchemaVars() {
|
|
|
|
|
if ( $this->mSchemaVars ) {
|
|
|
|
|
return $this->mSchemaVars;
|
|
|
|
|
} else {
|
|
|
|
|
return $this->getDefaultSchemaVars();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get schema variables to use if none have been set via setSchemaVars().
|
2011-01-31 20:58:06 +00:00
|
|
|
* Override this in derived classes to provide variables for tables.sql
|
|
|
|
|
* and SQL patch files.
|
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database.
* Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade.
* Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this.
* Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used.
* Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes.
* Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database.
* In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary.
* On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added.
* Fixed several instances of inappropriate call-by-reference.
* Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler.
* Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892.
* In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString().
* Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging.
* MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
|
|
|
*/
|
|
|
|
|
protected function getDefaultSchemaVars() {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Table name callback
|
|
|
|
|
* @private
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
protected function tableNameCallback( $matches ) {
|
|
|
|
|
return $this->tableName( $matches[1] );
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-19 13:56:08 +00:00
|
|
|
/**
|
|
|
|
|
* Index name callback
|
|
|
|
|
*/
|
|
|
|
|
protected function indexNameCallback( $matches ) {
|
|
|
|
|
return $this->indexName( $matches[1] );
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-19 18:28:59 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Build a concatenation list to feed into a SQL query
|
2009-08-19 18:28:59 +00:00
|
|
|
* @param $stringList Array: list of raw SQL expressions; caller is responsible for any quoting
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function buildConcat( $stringList ) {
|
|
|
|
|
return 'CONCAT(' . implode( ',', $stringList ) . ')';
|
|
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2008-06-11 00:17:42 +00:00
|
|
|
/**
|
2009-06-25 00:40:12 +00:00
|
|
|
* Acquire a named lock
|
2010-03-06 18:14:11 +00:00
|
|
|
*
|
2008-06-11 00:17:42 +00:00
|
|
|
* Abstracted from Filestore::lock() so child classes can implement for
|
|
|
|
|
* their own needs.
|
2010-03-06 18:14:11 +00:00
|
|
|
*
|
2010-07-04 14:41:26 +00:00
|
|
|
* @param $lockName String: name of lock to aquire
|
|
|
|
|
* @param $method String: name of method calling us
|
|
|
|
|
* @param $timeout Integer: timeout
|
|
|
|
|
* @return Boolean
|
2008-06-11 00:17:42 +00:00
|
|
|
*/
|
2009-07-29 23:41:16 +00:00
|
|
|
public function lock( $lockName, $method, $timeout = 5 ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2008-06-11 00:17:42 +00:00
|
|
|
/**
|
|
|
|
|
* Release a lock.
|
2010-03-06 18:14:11 +00:00
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $lockName String: Name of lock to release
|
|
|
|
|
* @param $method String: Name of method calling us
|
2009-06-25 00:40:12 +00:00
|
|
|
*
|
|
|
|
|
* @return Returns 1 if the lock was released, 0 if the lock was not established
|
2010-03-06 18:14:11 +00:00
|
|
|
* by this thread (in which case the lock is not released), and NULL if the named
|
2009-06-25 00:40:12 +00:00
|
|
|
* lock did not exist
|
2008-06-11 00:17:42 +00:00
|
|
|
*/
|
2009-07-29 23:41:16 +00:00
|
|
|
public function unlock( $lockName, $method ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-06-25 00:40:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Lock specific tables
|
|
|
|
|
*
|
|
|
|
|
* @param $read Array of tables to lock for read access
|
|
|
|
|
* @param $write Array of tables to lock for write access
|
|
|
|
|
* @param $method String name of caller
|
2009-07-29 23:41:16 +00:00
|
|
|
* @param $lowPriority bool Whether to indicate writes to be LOW PRIORITY
|
2009-06-25 00:40:12 +00:00
|
|
|
*/
|
2009-07-29 23:41:16 +00:00
|
|
|
public function lockTables( $read, $write, $method, $lowPriority = true ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2009-06-25 00:40:12 +00:00
|
|
|
/**
|
|
|
|
|
* Unlock specific tables
|
|
|
|
|
*
|
|
|
|
|
* @param $method String the caller
|
|
|
|
|
*/
|
2009-07-29 23:41:16 +00:00
|
|
|
public function unlockTables( $method ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2010-12-28 01:19:16 +00:00
|
|
|
/**
|
|
|
|
|
* Delete a table
|
|
|
|
|
*/
|
2010-12-28 01:22:38 +00:00
|
|
|
public function dropTable( $tableName, $fName = 'DatabaseBase::dropTable' ) {
|
|
|
|
|
if( !$this->tableExists( $tableName ) ) {
|
2010-12-28 01:19:16 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$sql = "DROP TABLE " . $this->tableName( $tableName );
|
|
|
|
|
if( $this->cascadingDeletes() ) {
|
|
|
|
|
$sql .= " CASCADE";
|
|
|
|
|
}
|
2010-12-28 01:22:38 +00:00
|
|
|
return $this->query( $sql, $fName );
|
2010-12-28 01:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-18 15:22:00 +00:00
|
|
|
/**
|
2010-07-22 13:04:37 +00:00
|
|
|
* Get search engine class. All subclasses of this need to implement this
|
|
|
|
|
* if they wish to use searching.
|
2010-03-06 18:14:11 +00:00
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return String
|
2008-08-18 15:22:00 +00:00
|
|
|
*/
|
2010-07-22 13:04:37 +00:00
|
|
|
public function getSearchEngine() {
|
|
|
|
|
return 'SearchEngineDummy';
|
|
|
|
|
}
|
2009-05-27 06:10:48 +00:00
|
|
|
|
2011-01-05 13:43:13 +00:00
|
|
|
/**
|
|
|
|
|
* Find out when 'infinity' is. Most DBMSes support this. This is a special
|
|
|
|
|
* keyword for timestamps in PostgreSQL, and works with CHAR(14) as well
|
|
|
|
|
* because "i" sorts after all numbers.
|
|
|
|
|
*
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public function getInfinity() {
|
|
|
|
|
return 'infinity';
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-18 19:15:56 +00:00
|
|
|
/**
|
|
|
|
|
* Encode an expiry time
|
|
|
|
|
*
|
|
|
|
|
* @param $expiry String: timestamp for expiry, or the 'infinity' string
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public function encodeExpiry( $expiry ) {
|
|
|
|
|
if ( $expiry == '' || $expiry == $this->getInfinity() ) {
|
|
|
|
|
return $this->getInfinity();
|
|
|
|
|
} else {
|
|
|
|
|
return $this->timestamp( $expiry );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-27 06:10:48 +00:00
|
|
|
/**
|
2010-03-06 18:14:11 +00:00
|
|
|
* Allow or deny "big selects" for this session only. This is done by setting
|
2009-05-27 06:10:48 +00:00
|
|
|
* the sql_big_selects session variable.
|
|
|
|
|
*
|
2010-03-06 18:14:11 +00:00
|
|
|
* This is a MySQL-specific feature.
|
2009-05-27 06:10:48 +00:00
|
|
|
*
|
2010-07-04 14:41:26 +00:00
|
|
|
* @param $value Mixed: true for allow, false for deny, or "default" to restore the initial value
|
2009-05-27 06:10:48 +00:00
|
|
|
*/
|
|
|
|
|
public function setBigSelects( $value = true ) {
|
2009-07-09 00:14:43 +00:00
|
|
|
// no-op
|
2009-05-27 06:10:48 +00:00
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|