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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2011-10-15 17:59:42 +00:00
|
|
|
* @param $res ResultWrapper|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 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
|
|
|
|
2011-09-16 17:58:50 +00:00
|
|
|
protected $preparedArgs;
|
|
|
|
|
|
2011-11-29 00:09:04 +00:00
|
|
|
protected $htmlErrors;
|
|
|
|
|
|
2011-12-27 12:29:36 +00:00
|
|
|
protected $delimiter = ';';
|
|
|
|
|
|
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
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Boolean, controls output of large amounts of debug information.
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $debug bool|null
|
2011-06-20 06:52:44 +00:00
|
|
|
* - true to enable debugging
|
|
|
|
|
* - false to disable debugging
|
|
|
|
|
* - omitted or null to do nothing
|
|
|
|
|
*
|
|
|
|
|
* @return The previous value of the flag
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Turns buffering of SQL result sets on (true) or off (false). Default is
|
|
|
|
|
* "on".
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
2011-06-20 06:52:44 +00:00
|
|
|
* Unbuffered queries are very troublesome in MySQL:
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - If another query is executed while the first query is being read
|
2011-06-20 06:52:44 +00:00
|
|
|
* out, the first query is killed. This means you can't call normal
|
|
|
|
|
* MediaWiki functions while you are reading an unbuffered query result
|
|
|
|
|
* from a normal wfGetDB() connection.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - Unbuffered queries cause the MySQL server to use large amounts of
|
2011-06-20 06:52:44 +00:00
|
|
|
* memory and to hold broad locks which block other queries.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* If you want to limit client-side memory, it's almost always better to
|
2011-06-20 06:52:44 +00:00
|
|
|
* split up queries into batches using a LIMIT clause than to switch off
|
|
|
|
|
* buffering.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $buffer null|bool
|
|
|
|
|
*
|
2011-06-20 06:52:44 +00:00
|
|
|
* @return The previous value of the flag
|
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.
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-09-16 17:58:50 +00:00
|
|
|
* @param $ignoreErrors bool|null
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
2011-11-29 21:04:20 +00:00
|
|
|
* @return bool The previous value of the flag.
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* Gets or sets the current transaction level.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* Historically, transactions were allowed to be "nested". This is no
|
2011-06-20 06:52:44 +00:00
|
|
|
* longer supported, so this function really only returns a boolean.
|
|
|
|
|
*
|
|
|
|
|
* @param $level An integer (0 or 1), or omitted to leave it unchanged.
|
|
|
|
|
* @return The previous value
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* Get/set the number of errors logged. Only useful when errors are ignored
|
|
|
|
|
* @param $count The count to set, or omitted to leave it unchanged.
|
|
|
|
|
* @return The error count
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
2011-06-20 06:52:44 +00:00
|
|
|
/**
|
|
|
|
|
* Get/set the table prefix.
|
|
|
|
|
* @param $prefix The table prefix to set, or omitted to leave it unchanged.
|
|
|
|
|
* @return The previous table prefix.
|
|
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function tablePrefix( $prefix = null ) {
|
2011-07-20 23:27:34 +00:00
|
|
|
return wfSetVar( $this->mTablePrefix, $prefix );
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Get properties passed down from the server info array of the load
|
2011-06-20 06:52:44 +00:00
|
|
|
* balancer.
|
|
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $name string The entry of the info array to get, or null to get the
|
2011-06-20 06:52:44 +00:00
|
|
|
* whole array
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return LoadBalancer|null
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Set the LB info array, or a member of it. If called with one parameter,
|
|
|
|
|
* the LB info array is set to that parameter. If it is called with two
|
2011-06-20 06:52:44 +00:00
|
|
|
* parameters, the member with the given name is set to the given value.
|
|
|
|
|
*
|
2011-05-25 18:58:02 +00:00
|
|
|
* @param $name
|
|
|
|
|
* @param $value
|
|
|
|
|
*/
|
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
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @param $lag int
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @param $enabled bool
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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)
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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.
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
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
|
2011-09-16 17:58:50 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
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';
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2011-09-16 17:58:50 +00:00
|
|
|
*
|
|
|
|
|
* @param $name string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
/**
|
2011-06-20 07:16:09 +00:00
|
|
|
* Return a path to the DBMS-specific schema file, otherwise default to tables.sql
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2010-09-01 19:03:56 +00:00
|
|
|
*/
|
2011-06-20 07:16:09 +00:00
|
|
|
public function getSchemaPath() {
|
2010-09-01 19:03:56 +00:00
|
|
|
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
|
|
|
|
2011-10-15 01:33:19 +00:00
|
|
|
/**
|
|
|
|
|
* Called by serialize. Throw an exception when DB connection is serialized.
|
2011-10-26 03:44:47 +00:00
|
|
|
* This causes problems on some database engines because the connection is
|
2011-10-15 01:33:19 +00:00
|
|
|
* not restored on unserialize.
|
|
|
|
|
*/
|
|
|
|
|
public function __sleep() {
|
|
|
|
|
throw new MWException( 'Database serialization may cause problems, since the connection is not restored on wakeup.' );
|
2011-09-24 20:07:21 +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
|
|
|
*
|
2011-11-29 00:09:04 +00:00
|
|
|
* @param $server
|
|
|
|
|
* @param $user
|
|
|
|
|
* @param $password
|
|
|
|
|
* @param $dbName
|
|
|
|
|
* @param $flags int
|
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 ) {
|
2011-12-13 05:19:05 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.17' );
|
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-12-06 22:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Same as new factory( ... ), kept for backward compatibility
|
|
|
|
|
* @deprecated since 1.18
|
|
|
|
|
* @see Database::factory()
|
|
|
|
|
*/
|
|
|
|
|
public final static function newFromType( $dbType, $p = array() ) {
|
2011-12-13 05:19:05 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.18' );
|
2011-12-06 22:37:38 +00:00
|
|
|
if ( isset( $p['tableprefix'] ) ) {
|
|
|
|
|
$p['tablePrefix'] = $p['tableprefix'];
|
|
|
|
|
}
|
|
|
|
|
return self::factory( $dbType, $p );
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2011-06-20 07:40:07 +00:00
|
|
|
* Valid options are: host, user, password, dbname, flags, tablePrefix
|
2011-01-07 22:32:52 +00:00
|
|
|
* @return DatabaseBase subclass or null
|
|
|
|
|
*/
|
2011-06-20 07:00:50 +00:00
|
|
|
public final static function factory( $dbType, $p = array() ) {
|
2011-01-07 22:32:52 +00:00
|
|
|
$canonicalDBTypes = array(
|
|
|
|
|
'mysql', 'postgres', 'sqlite', 'oracle', 'mssql', 'ibm_db2'
|
|
|
|
|
);
|
|
|
|
|
$dbType = strtolower( $dbType );
|
2012-01-03 19:55:45 +00:00
|
|
|
$class = 'Database' . ucfirst( $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
|
|
|
return new $class(
|
|
|
|
|
isset( $p['host'] ) ? $p['host'] : false,
|
|
|
|
|
isset( $p['user'] ) ? $p['user'] : false,
|
|
|
|
|
isset( $p['password'] ) ? $p['password'] : false,
|
2011-06-20 07:40:07 +00:00
|
|
|
isset( $p['dbname'] ) ? $p['dbname'] : false,
|
2011-01-24 16:31:36 +00:00
|
|
|
isset( $p['flags'] ) ? $p['flags'] : 0,
|
2011-06-20 07:00:50 +00:00
|
|
|
isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : 'get from global'
|
2011-01-24 16:31:36 +00:00
|
|
|
);
|
2012-01-05 18:49:19 +00:00
|
|
|
} elseif ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) {
|
2012-01-04 16:43:46 +00:00
|
|
|
return new $class( $p );
|
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' ) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-06 00:24:18 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool|string
|
|
|
|
|
*/
|
2008-06-04 01:44:36 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2011-11-29 00:09:04 +00:00
|
|
|
/**
|
|
|
|
|
* @param $errno
|
|
|
|
|
* @param $errstr
|
|
|
|
|
*/
|
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
|
|
|
|
2011-06-20 07:19:20 +00:00
|
|
|
/**
|
|
|
|
|
* The DBMS-dependent part of query()
|
|
|
|
|
*
|
|
|
|
|
* @param $sql String: SQL query.
|
2011-09-16 17:58:50 +00:00
|
|
|
* @return ResultWrapper Result object to feed to fetchObject, fetchRow, ...; or false on failure
|
2011-06-20 07:19:20 +00:00
|
|
|
*/
|
|
|
|
|
protected abstract function doQuery( $sql );
|
|
|
|
|
|
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
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $sql string
|
|
|
|
|
*
|
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
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Run an SQL query and return the result. Normally throws a DBQueryError
|
2011-06-20 06:52:44 +00:00
|
|
|
* on failure. If errors are ignored, returns false instead.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* In new code, the query wrappers select(), insert(), update(), delete(),
|
|
|
|
|
* etc. should be used where possible, since they give much better DBMS
|
2011-06-20 06:52:44 +00:00
|
|
|
* independence and automatically quote or validate user input in a variety
|
|
|
|
|
* of contexts. This function is generally only useful for queries which are
|
2011-07-01 02:57:31 +00:00
|
|
|
* explicitly DBMS-dependent and are unsupported by the query wrappers, such
|
2011-06-20 06:52:44 +00:00
|
|
|
* as CREATE TABLE.
|
|
|
|
|
*
|
|
|
|
|
* However, the query wrappers themselves should call this function.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
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?
|
2011-07-01 02:57:31 +00:00
|
|
|
* @return boolean|ResultWrapper. true for a successful write query, ResultWrapper object
|
2011-06-20 06:52:44 +00:00
|
|
|
* for a successful read query, 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
|
|
|
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 ) ) {
|
2011-06-20 06:52:44 +00:00
|
|
|
# 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
|
2011-06-20 06:52:44 +00:00
|
|
|
global $wgUser;
|
|
|
|
|
if ( is_object( $wgUser ) && $wgUser->isItemLoaded( 'name' ) ) {
|
|
|
|
|
$userName = $wgUser->getName();
|
|
|
|
|
if ( mb_strlen( $userName ) > 15 ) {
|
|
|
|
|
$userName = mb_substr( $userName, 0, 15 ) . '...';
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
2011-06-20 06:52:44 +00:00
|
|
|
$userName = str_replace( '/', '', $userName );
|
|
|
|
|
} else {
|
|
|
|
|
$userName = '';
|
|
|
|
|
}
|
|
|
|
|
$commentedSql = preg_replace( '/\s/', " /* $fname $userName */ ", $sql, 1 );
|
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' ) {
|
2011-06-20 06:52:44 +00:00
|
|
|
# avoid establishing transactions for SHOW and SET statements too -
|
|
|
|
|
# that would delay transaction initializations to once connection
|
|
|
|
|
# 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)
|
2011-10-26 03:44:47 +00:00
|
|
|
if ( strpos( $sqlstart, "SHOW " ) !== 0 && 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
|
|
|
|
2012-01-06 18:23:52 +00:00
|
|
|
|
|
|
|
|
$master = $isMaster ? 'master' : 'slave';
|
|
|
|
|
wfDebug( "Query {$this->mDBname} ($cnt) ($master): $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' );
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-04 18:29:57 +00:00
|
|
|
$queryId = MWDebug::query( $sql, $fname, $isMaster );
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
# Do the query and handle errors
|
|
|
|
|
$ret = $this->doQuery( $commentedSql );
|
|
|
|
|
|
2011-12-04 18:29:57 +00:00
|
|
|
MWDebug::queryTime( $queryId );
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
# 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
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Report a query error. Log the error, and if neither the object ignore
|
2011-06-20 06:52:44 +00:00
|
|
|
* flag nor the $tempIgnore flag is set, throw a DBQueryError.
|
|
|
|
|
*
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* This function should not be used directly by new code outside of the
|
2011-06-20 06:52:44 +00:00
|
|
|
* database classes. The query wrapper functions (select() etc.) should be
|
|
|
|
|
* used instead.
|
|
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $sql string
|
|
|
|
|
* @param $func string
|
|
|
|
|
*
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 06:52:44 +00:00
|
|
|
/**
|
|
|
|
|
* Free a prepared query, generated by prepare().
|
2011-11-29 21:04:20 +00:00
|
|
|
* @param $prepared
|
2011-06-20 06:52:44 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
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.
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* This function should not be used directly by new code outside of the
|
2011-06-20 06:52:44 +00:00
|
|
|
* database classes. The query wrapper functions (select() etc.) should be
|
|
|
|
|
* used instead.
|
|
|
|
|
*
|
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
|
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
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Free a result object returned by query() or select(). It's usually not
|
|
|
|
|
* necessary to call this, just use unset() or let the variable holding
|
2011-06-20 06:52:44 +00:00
|
|
|
* the result object go out of scope.
|
|
|
|
|
*
|
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 ) {
|
|
|
|
|
}
|
2006-06-06 23:07:26 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* Simple UPDATE wrapper.
|
|
|
|
|
* Usually throws a DBQueryError on failure.
|
2008-03-30 09:48:15 +00:00
|
|
|
* 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
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $table string
|
|
|
|
|
* @param $var
|
|
|
|
|
* @param $value
|
|
|
|
|
* @param $cond
|
|
|
|
|
* @param $fname string
|
|
|
|
|
*
|
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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* A SELECT wrapper which returns a single field from a single result row.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* Usually throws a DBQueryError on failure. If errors are explicitly
|
2011-06-20 06:52:44 +00:00
|
|
|
* ignored, returns false on failure.
|
|
|
|
|
*
|
|
|
|
|
* If no result rows are returned from the query, false is returned.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $table string|array Table name. See DatabaseBase::select() for details.
|
|
|
|
|
* @param $var string The field name to select. This must be a valid SQL
|
2011-06-20 06:52:44 +00:00
|
|
|
* fragment: do not use unvalidated user input.
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $cond string|array The condition array. See DatabaseBase::select() for details.
|
|
|
|
|
* @param $fname string The function name of the caller.
|
|
|
|
|
* @param $options string|array The query options. See DatabaseBase::select() for details.
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-18 20:11:53 +00:00
|
|
|
* @return false|mixed The value from the field, or false on failure.
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2011-07-01 02:57:31 +00:00
|
|
|
function selectField( $table, $var, $cond = '', $fname = 'DatabaseBase::selectField',
|
|
|
|
|
$options = array() )
|
2011-06-20 06:52:44 +00:00
|
|
|
{
|
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
|
2011-07-01 02:57:31 +00:00
|
|
|
* string to go at the end of the query.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
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
|
2011-06-20 06:52:44 +00:00
|
|
|
* @see DatabaseBase::select()
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2011-06-20 12:09:22 +00:00
|
|
|
function makeSelectOptions( $options ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$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 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* Execute a SELECT query constructed using the various parameters provided.
|
|
|
|
|
* See below for full details of the parameters.
|
|
|
|
|
*
|
2011-06-26 23:01:29 +00:00
|
|
|
* @param $table String|Array Table name
|
|
|
|
|
* @param $vars String|Array Field names
|
|
|
|
|
* @param $conds String|Array Conditions
|
|
|
|
|
* @param $fname String Caller function name
|
|
|
|
|
* @param $options Array Query options
|
|
|
|
|
* @param $join_conds Array Join conditions
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $table string|array
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* May be either an array of table names, or a single string holding a table
|
|
|
|
|
* name. If an array is given, table aliases can be specified, for example:
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
|
|
|
|
* array( 'a' => 'user' )
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* This includes the user table in the query, with the alias "a" available
|
2011-06-20 06:52:44 +00:00
|
|
|
* for use in field names (e.g. a.user_name).
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* All of the table names given here are automatically run through
|
|
|
|
|
* DatabaseBase::tableName(), which causes the table prefix (if any) to be
|
2011-06-20 06:52:44 +00:00
|
|
|
* added, and various other table name mappings to be performed.
|
|
|
|
|
*
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $vars string|array
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* May be either a field name or an array of field names. The field names
|
|
|
|
|
* here are complete fragments of SQL, for direct inclusion into the SELECT
|
2011-06-20 06:52:44 +00:00
|
|
|
* query. Expressions and aliases may be specified as in SQL, for example:
|
|
|
|
|
*
|
|
|
|
|
* array( 'MAX(rev_id) AS maxrev' )
|
2011-07-01 02:57:31 +00:00
|
|
|
*
|
|
|
|
|
* If an expression is given, care must be taken to ensure that it is
|
2011-06-20 06:52:44 +00:00
|
|
|
* DBMS-independent.
|
|
|
|
|
*
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $conds string|array
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* May be either a string containing a single condition, or an array of
|
|
|
|
|
* conditions. If an array is given, the conditions constructed from each
|
|
|
|
|
* element are combined with AND.
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
|
|
|
|
* Array elements may take one of two forms:
|
|
|
|
|
*
|
|
|
|
|
* - Elements with a numeric key are interpreted as raw SQL fragments.
|
2011-07-01 02:57:31 +00:00
|
|
|
* - Elements with a string key are interpreted as equality conditions,
|
2011-06-20 06:52:44 +00:00
|
|
|
* where the key is the field name.
|
2011-07-01 02:57:31 +00:00
|
|
|
* - If the value of such an array element is a scalar (such as a
|
|
|
|
|
* string), it will be treated as data and thus quoted appropriately.
|
2011-06-20 06:52:44 +00:00
|
|
|
* If it is null, an IS NULL clause will be added.
|
2011-07-01 02:57:31 +00:00
|
|
|
* - If the value is an array, an IN(...) clause will be constructed,
|
|
|
|
|
* such that the field name may match any of the elements in the
|
2011-06-20 06:52:44 +00:00
|
|
|
* array. The elements of the array will be quoted.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* Note that expressions are often DBMS-dependent in their syntax.
|
|
|
|
|
* DBMS-independent wrappers are provided for constructing several types of
|
2011-06-20 06:52:44 +00:00
|
|
|
* expression commonly used in condition queries. See:
|
|
|
|
|
* - DatabaseBase::buildLike()
|
|
|
|
|
* - DatabaseBase::conditional()
|
|
|
|
|
*
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $options string|array
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* Optional: Array of query options. Boolean options are specified by
|
|
|
|
|
* including them in the array as a string value with a numeric key, for
|
2011-06-20 06:52:44 +00:00
|
|
|
* example:
|
|
|
|
|
*
|
|
|
|
|
* array( 'FOR UPDATE' )
|
|
|
|
|
*
|
|
|
|
|
* The supported options are:
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - OFFSET: Skip this many rows at the start of the result set. OFFSET
|
2011-06-20 06:52:44 +00:00
|
|
|
* with LIMIT can theoretically be used for paging through a result set,
|
2011-07-01 02:57:31 +00:00
|
|
|
* but this is discouraged in MediaWiki for performance reasons.
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - LIMIT: Integer: return at most this many rows. The rows are sorted
|
2011-06-20 06:52:44 +00:00
|
|
|
* and then the first rows are taken until the limit is reached. LIMIT
|
|
|
|
|
* is applied to a result set after OFFSET.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - FOR UPDATE: Boolean: lock the returned rows so that they can't be
|
2011-06-20 06:52:44 +00:00
|
|
|
* changed until the next COMMIT.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2011-06-20 06:52:44 +00:00
|
|
|
* - DISTINCT: Boolean: return only unique result rows.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - GROUP BY: May be either an SQL fragment string naming a field or
|
2011-06-20 06:52:44 +00:00
|
|
|
* expression to group by, or an array of such SQL fragments.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - HAVING: A string containing a HAVING clause.
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - ORDER BY: May be either an SQL fragment giving a field name or
|
2011-06-20 06:52:44 +00:00
|
|
|
* expression to order by, or an array of such SQL fragments.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - USE INDEX: This may be either a string giving the index name to use
|
|
|
|
|
* for the query, or an array. If it is an associative array, each key
|
2011-06-20 06:52:44 +00:00
|
|
|
* gives the table name (or alias), each value gives the index name to
|
2011-07-01 02:57:31 +00:00
|
|
|
* use for that table. All strings are SQL fragments and so should be
|
2011-06-20 06:52:44 +00:00
|
|
|
* validated by the caller.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - EXPLAIN: In MySQL, this causes an EXPLAIN SELECT query to be run,
|
2011-06-20 06:52:44 +00:00
|
|
|
* instead of SELECT.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* And also the following boolean MySQL extensions, see the MySQL manual
|
2011-06-20 06:52:44 +00:00
|
|
|
* for documentation:
|
|
|
|
|
*
|
|
|
|
|
* - LOCK IN SHARE MODE
|
|
|
|
|
* - STRAIGHT_JOIN
|
|
|
|
|
* - HIGH_PRIORITY
|
|
|
|
|
* - SQL_BIG_RESULT
|
|
|
|
|
* - SQL_BUFFER_RESULT
|
|
|
|
|
* - SQL_SMALL_RESULT
|
|
|
|
|
* - SQL_CALC_FOUND_ROWS
|
|
|
|
|
* - SQL_CACHE
|
|
|
|
|
* - SQL_NO_CACHE
|
|
|
|
|
*
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $join_conds string|array
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* Optional associative array of table-specific join conditions. In the
|
|
|
|
|
* most common case, this is unnecessary, since the join condition can be
|
2011-06-20 06:52:44 +00:00
|
|
|
* in $conds. However, it is useful for doing a LEFT JOIN.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* The key of the array contains the table name or alias. The value is an
|
|
|
|
|
* array with two elements, numbered 0 and 1. The first gives the type of
|
|
|
|
|
* join, the second is an SQL fragment giving the join condition for that
|
2011-06-20 06:52:44 +00:00
|
|
|
* table. For example:
|
|
|
|
|
*
|
|
|
|
|
* array( 'page' => array('LEFT JOIN','page_latest=rev_id') )
|
|
|
|
|
*
|
|
|
|
|
* @return ResultWrapper. If the query returned no rows, a ResultWrapper
|
2011-07-01 02:57:31 +00:00
|
|
|
* with no rows in it will be returned. If there was a query error, a
|
2011-06-20 06:52:44 +00:00
|
|
|
* DBQueryError exception will be thrown, except if the "ignore errors"
|
|
|
|
|
* option was set, in which case false will be returned.
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2011-07-01 02:57:31 +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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* The equivalent of DatabaseBase::select() except that the constructed SQL
|
|
|
|
|
* is returned, instead of being immediately executed.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $table string|array Table name
|
|
|
|
|
* @param $vars string|array Field names
|
|
|
|
|
* @param $conds string|array Conditions
|
|
|
|
|
* @param $fname string Caller function name
|
|
|
|
|
* @param $options string|array Query options
|
|
|
|
|
* @param $join_conds string|array Join conditions
|
2008-05-13 04:56:14 +00:00
|
|
|
*
|
2011-06-20 06:52:44 +00:00
|
|
|
* @return SQL query string.
|
|
|
|
|
* @see DatabaseBase::select()
|
2008-05-13 04:56:14 +00:00
|
|
|
*/
|
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
|
|
|
|
2011-06-20 23:21:24 +00:00
|
|
|
$options = (array)$options;
|
2010-09-04 13:48:16 +00:00
|
|
|
|
|
|
|
|
if ( is_array( $table ) ) {
|
2011-06-20 23:21:24 +00:00
|
|
|
$useIndex = ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
|
|
|
|
|
? $options['USE INDEX']
|
|
|
|
|
: array();
|
|
|
|
|
if ( count( $join_conds ) || count( $useIndex ) ) {
|
|
|
|
|
$from = ' FROM ' .
|
|
|
|
|
$this->tableNamesWithUseIndexOrJOIN( $table, $useIndex, $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 != '' ) {
|
2011-09-27 16:15:29 +00:00
|
|
|
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
|
|
|
|
2011-06-20 23:21:24 +00:00
|
|
|
if ( isset( $options['LIMIT'] ) ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
$sql = $this->limitResult( $sql, $options['LIMIT'],
|
|
|
|
|
isset( $options['OFFSET'] ) ? $options['OFFSET'] : false );
|
2011-06-20 23:21:24 +00:00
|
|
|
}
|
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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* Single row SELECT wrapper. Equivalent to DatabaseBase::select(), except
|
2011-07-01 02:57:31 +00:00
|
|
|
* that a single row object is returned. If the query returns no rows,
|
2011-06-20 06:52:44 +00:00
|
|
|
* false is returned.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $table string|array Table name
|
|
|
|
|
* @param $vars string|array Field names
|
|
|
|
|
* @param $conds|array Conditions
|
|
|
|
|
* @param $fname string Caller function name
|
|
|
|
|
* @param $options string|array Query options
|
|
|
|
|
* @param $join_conds array|string Join conditions
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @return ResultWrapper|bool
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2011-07-01 02:57:31 +00:00
|
|
|
function selectRow( $table, $vars, $conds, $fname = 'DatabaseBase::selectRow',
|
|
|
|
|
$options = array(), $join_conds = array() )
|
2011-06-20 06:52:44 +00:00
|
|
|
{
|
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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* Estimate rows in dataset.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* MySQL allows you to estimate the number of rows that would be returned
|
2011-06-20 06:52:44 +00:00
|
|
|
* by a SELECT query, using EXPLAIN SELECT. The estimate is provided using
|
|
|
|
|
* index cardinality statistics, and is notoriously inaccurate, especially
|
|
|
|
|
* when large numbers of rows have recently been added or deleted.
|
|
|
|
|
*
|
|
|
|
|
* For DBMSs that don't support fast result size estimation, this function
|
2011-07-01 02:57:31 +00:00
|
|
|
* will actually perform the SELECT COUNT(*).
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
|
|
|
|
* Takes the 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
|
2011-09-16 17:58:50 +00:00
|
|
|
* @param Array|string $vars : unused
|
|
|
|
|
* @param Array|string $conds : filters on the table
|
2010-07-04 14:41:26 +00:00
|
|
|
* @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
|
|
|
*/
|
2011-07-01 02:57:31 +00:00
|
|
|
public function estimateRowCount( $table, $vars = '*', $conds = '',
|
|
|
|
|
$fname = 'DatabaseBase::estimateRowCount', $options = array() )
|
2011-06-20 06:52:44 +00:00
|
|
|
{
|
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
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $sql String A SQL Query
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
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
|
2011-06-20 06:52:44 +00:00
|
|
|
* Usually throws a DBQueryError on failure
|
2008-03-30 09:48:15 +00:00
|
|
|
* If errors are explicitly ignored, returns NULL on failure
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $table
|
|
|
|
|
* @param $index
|
|
|
|
|
* @param $fname string
|
|
|
|
|
*
|
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
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $table string
|
2011-11-10 20:39:23 +00:00
|
|
|
* @param $fname string
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2011-11-10 20:39:23 +00:00
|
|
|
function tableExists( $table, $fname = __METHOD__ ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$table = $this->tableName( $table );
|
|
|
|
|
$old = $this->ignoreErrors( true );
|
2011-11-10 20:39:23 +00:00
|
|
|
$res = $this->query( "SELECT 1 FROM $table LIMIT 1", $fname );
|
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
|
2011-11-29 00:09:04 +00:00
|
|
|
* @param $res
|
|
|
|
|
* @param $index
|
|
|
|
|
* @return string
|
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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* Helper for DatabaseBase::insert().
|
|
|
|
|
*
|
2011-05-02 20:14:17 +00:00
|
|
|
* @param $options array
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-06-20 12:09:22 +00:00
|
|
|
function makeInsertOptions( $options ) {
|
2011-05-02 20:14:17 +00:00
|
|
|
return implode( ' ', $options );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* INSERT wrapper, inserts an array into a table.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2011-06-20 06:52:44 +00:00
|
|
|
* $a may be either:
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2011-06-20 06:52:44 +00:00
|
|
|
* - A single associative array. The array keys are the field names, and
|
2011-07-01 02:57:31 +00:00
|
|
|
* the values are the values to insert. The values are treated as data
|
|
|
|
|
* and will be quoted appropriately. If NULL is inserted, this will be
|
2011-06-20 06:52:44 +00:00
|
|
|
* converted to a database NULL.
|
2011-07-01 02:57:31 +00:00
|
|
|
* - An array with numeric keys, holding a list of associative arrays.
|
2011-06-20 06:52:44 +00:00
|
|
|
* This causes a multi-row INSERT on DBMSs that support it. The keys in
|
|
|
|
|
* each subarray must be identical to each other, and in the same order.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* Usually throws a DBQueryError on failure. If errors are explicitly ignored,
|
2011-06-20 06:52:44 +00:00
|
|
|
* returns success.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* $options is an array of options, with boolean options encoded as values
|
|
|
|
|
* with numeric keys, in the same style as $options in
|
2011-06-20 06:52:44 +00:00
|
|
|
* DatabaseBase::select(). Supported options are:
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - IGNORE: Boolean: if present, duplicate key errors are ignored, and
|
|
|
|
|
* any rows which cause duplicate key errors are not inserted. It's
|
|
|
|
|
* possible to determine how many rows were successfully inserted using
|
2011-06-20 06:52:44 +00:00
|
|
|
* DatabaseBase::affectedRows().
|
2010-08-25 00:18:47 +00:00
|
|
|
*
|
2011-06-26 23:01:29 +00:00
|
|
|
* @param $table String Table name. This will be passed through
|
2011-06-20 06:52:44 +00:00
|
|
|
* DatabaseBase::tableName().
|
2011-09-28 16:37:27 +00:00
|
|
|
* @param $a Array of rows to insert
|
2011-06-26 23:01:29 +00:00
|
|
|
* @param $fname String Calling function name (use __METHOD__) for logs/profiling
|
2011-06-20 06:52:44 +00:00
|
|
|
* @param $options Array of options
|
2010-08-25 00:18:47 +00:00
|
|
|
*
|
|
|
|
|
* @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
|
|
|
*
|
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
|
|
|
*/
|
2011-06-20 12:09:22 +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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* UPDATE wrapper. Takes a condition array and a SET array.
|
|
|
|
|
*
|
2011-12-02 00:29:46 +00:00
|
|
|
* @param $table String name of the table to UPDATE. This will be passed through
|
2011-06-20 06:52:44 +00:00
|
|
|
* DatabaseBase::tableName().
|
|
|
|
|
*
|
2011-12-02 00:29:46 +00:00
|
|
|
* @param $values Array: An array of values to SET. For each array element,
|
2011-06-20 06:52:44 +00:00
|
|
|
* the key gives the field name, and the value gives the data
|
2011-07-01 02:57:31 +00:00
|
|
|
* to set that field to. The data will be quoted by
|
2011-06-20 06:52:44 +00:00
|
|
|
* DatabaseBase::addQuotes().
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2011-12-02 00:29:46 +00:00
|
|
|
* @param $conds Array: An array of conditions (WHERE). See
|
2011-07-01 02:57:31 +00:00
|
|
|
* DatabaseBase::select() for the details of the format of
|
2011-06-20 06:52:44 +00:00
|
|
|
* condition arrays. Use '*' to update all rows.
|
|
|
|
|
*
|
2011-12-02 00:29:46 +00:00
|
|
|
* @param $fname String: The function name of the caller (from __METHOD__),
|
2011-06-20 06:52:44 +00:00
|
|
|
* for logging and profiling.
|
|
|
|
|
*
|
2011-12-02 00:29:46 +00:00
|
|
|
* @param $options Array: An array of UPDATE options, can be:
|
2011-06-20 06:52:44 +00:00
|
|
|
* - IGNORE: Ignore unique key conflicts
|
|
|
|
|
* - LOW_PRIORITY: MySQL-specific, see MySQL manual.
|
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() ) {
|
2011-12-02 00:29:46 +00:00
|
|
|
$table = $this->tableName( $table );
|
2008-03-30 09:48:15 +00:00
|
|
|
$opts = $this->makeUpdateOptions( $options );
|
|
|
|
|
$sql = "UPDATE $opts $table SET " . $this->makeList( $values, LIST_SET );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2011-08-25 17:47:33 +00:00
|
|
|
if ( $conds !== array() && $conds !== '*' ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$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-06-20 06:52:44 +00:00
|
|
|
* @param $a Array containing the data
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $mode int Constant
|
2011-06-20 06:52:44 +00:00
|
|
|
* - LIST_COMMA: comma separated, no field names
|
2011-07-01 02:57:31 +00:00
|
|
|
* - LIST_AND: ANDed WHERE clause (without the WHERE). See
|
2011-06-20 06:52:44 +00:00
|
|
|
* the documentation for $conds in DatabaseBase::select().
|
|
|
|
|
* - 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
|
|
|
*
|
|
|
|
|
* @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
|
|
|
|
|
|
|
|
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-09-13 00:19:04 +00:00
|
|
|
$list .= $field . " = " . $this->addQuotes( $value[0] );
|
2008-03-30 09:48:15 +00:00
|
|
|
} else {
|
2011-09-13 00:19:04 +00:00
|
|
|
$list .= $field . " IN (" . $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.
|
|
|
|
|
*
|
2011-06-20 06:52:44 +00:00
|
|
|
* @param $data Array: organized as 2-d
|
|
|
|
|
* array(baseKeyVal => array(subKeyVal => <ignored>, ...), ...)
|
2010-07-04 14:41:26 +00:00
|
|
|
* @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.
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @param $db
|
|
|
|
|
*
|
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-09-06 20:44:03 +00:00
|
|
|
* @param $format String One of:
|
|
|
|
|
* quoted - Automatically pass the table name through addIdentifierQuotes()
|
|
|
|
|
* so that it can be used in a query.
|
|
|
|
|
* raw - Do not add identifier quotes to the table name
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return String: full database name
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2011-09-06 20:44:03 +00:00
|
|
|
function tableName( $name, $format = 'quoted' ) {
|
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] ) ) {
|
2011-07-04 15:00:30 +00:00
|
|
|
list( $table, $database ) = $dbDetails;
|
2010-08-25 17:45:00 +00:00
|
|
|
} else {
|
2011-07-04 15:00:30 +00:00
|
|
|
list( $table ) = $dbDetails;
|
2010-08-25 17:45:00 +00:00
|
|
|
}
|
2008-05-07 04:44:04 +00:00
|
|
|
$prefix = $this->mTablePrefix; # Default prefix
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2011-08-06 00:24:18 +00:00
|
|
|
# A database name has been specified in input. We don't want any
|
2011-07-25 20:08:49 +00:00
|
|
|
# prefixes added.
|
|
|
|
|
if ( isset( $database ) ) {
|
|
|
|
|
$prefix = '';
|
|
|
|
|
}
|
|
|
|
|
|
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-09-06 20:44:03 +00:00
|
|
|
$database = ( $format == 'quoted' || $this->isQuotedIdentifier( $database ) ? $database : $this->addIdentifierQuotes( $database ) );
|
2010-08-25 17:45:00 +00:00
|
|
|
}
|
2011-06-17 15:59:55 +00:00
|
|
|
|
2011-04-12 18:52:16 +00:00
|
|
|
$table = "{$prefix}{$table}";
|
2011-09-06 20:44:03 +00:00
|
|
|
if ( $format == 'quoted' && !$this->isQuotedIdentifier( $table ) ) {
|
2011-04-12 18:52:16 +00:00
|
|
|
$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";
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
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";
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
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()
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $alias string|bool Alias (optional)
|
2010-12-02 19:39:32 +00:00
|
|
|
* @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-06-20 23:21:24 +00:00
|
|
|
* Get the aliased table name clause for a FROM clause
|
|
|
|
|
* which might have a JOIN and/or USE INDEX clause
|
|
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $tables array ( [alias] => table )
|
|
|
|
|
* @param $use_index array Same as for select()
|
|
|
|
|
* @param $join_conds array Same as for select()
|
2011-04-30 23:18:34 +00:00
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2011-06-20 23:21:24 +00:00
|
|
|
protected 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();
|
2011-06-20 23:21:24 +00:00
|
|
|
$use_index = (array)$use_index;
|
|
|
|
|
$join_conds = (array)$join_conds;
|
2010-09-04 13:48:16 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2011-06-20 23:21:24 +00:00
|
|
|
// Is there a JOIN clause for this table?
|
|
|
|
|
if ( isset( $join_conds[$alias] ) ) {
|
|
|
|
|
list( $joinType, $conds ) = $join_conds[$alias];
|
|
|
|
|
$tableClause = $joinType;
|
|
|
|
|
$tableClause .= ' ' . $this->tableNameWithAlias( $table, $alias );
|
|
|
|
|
if ( isset( $use_index[$alias] ) ) { // has USE INDEX?
|
|
|
|
|
$use = $this->useIndexClause( implode( ',', (array)$use_index[$alias] ) );
|
|
|
|
|
if ( $use != '' ) {
|
|
|
|
|
$tableClause .= ' ' . $use;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$on = $this->makeList( (array)$conds, 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;
|
2011-06-20 23:21:24 +00:00
|
|
|
// Is there an INDEX clause for this table?
|
|
|
|
|
} elseif ( isset( $use_index[$alias] ) ) {
|
2010-12-02 19:39:32 +00:00
|
|
|
$tableClause = $this->tableNameWithAlias( $table, $alias );
|
2011-06-20 23:21:24 +00:00
|
|
|
$tableClause .= ' ' . $this->useIndexClause(
|
|
|
|
|
implode( ',', (array)$use_index[$alias] ) );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2011-06-20 23:21:24 +00:00
|
|
|
$ret[] = $tableClause;
|
2008-05-10 00:48:07 +00:00
|
|
|
} else {
|
2010-12-02 19:39:32 +00:00
|
|
|
$tableClause = $this->tableNameWithAlias( $table, $alias );
|
2011-06-20 23:21:24 +00:00
|
|
|
|
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
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $s string
|
|
|
|
|
*
|
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
|
|
|
/**
|
2011-06-17 15:59:55 +00:00
|
|
|
* Returns if the given identifier looks quoted or not according to
|
2011-04-12 16:34:12 +00:00
|
|
|
* 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
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $s string
|
|
|
|
|
*
|
2011-05-25 18:58:02 +00:00
|
|
|
* @return string
|
2010-12-04 15:14:08 +00:00
|
|
|
*/
|
|
|
|
|
function quote_ident( $s ) {
|
2011-12-13 05:19:05 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.18' );
|
2010-12-04 15:14:08 +00:00
|
|
|
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 ???
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @param $s string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2010-07-29 18:37:45 +00:00
|
|
|
public function escapeLike( $s ) {
|
2011-12-13 05:19:05 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.17' );
|
2010-07-29 18:37:45 +00:00
|
|
|
return $this->escapeLikeInternal( $s );
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-06 00:24:18 +00:00
|
|
|
/**
|
|
|
|
|
* @param $s string
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-07-29 18:37:45 +00:00
|
|
|
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
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @return 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()
|
2011-11-13 11:28:56 +00:00
|
|
|
*
|
|
|
|
|
* Any implementation of this function should *not* involve reusing
|
|
|
|
|
* sequence numbers created for rolled-back transactions.
|
|
|
|
|
* See http://bugs.mysql.com/bug.php?id=30767 for details.
|
2011-12-05 16:50:58 +00:00
|
|
|
* @param $seqName string
|
2011-11-29 21:04:20 +00:00
|
|
|
* @return null
|
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.
|
2011-11-29 00:09:04 +00:00
|
|
|
* @param $index
|
|
|
|
|
* @return string
|
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
|
|
|
/**
|
2011-06-23 03:14:11 +00:00
|
|
|
* REPLACE query wrapper.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2011-06-23 03:14:11 +00:00
|
|
|
* REPLACE is a very handy MySQL extension, which functions like an INSERT
|
|
|
|
|
* except that when there is a duplicate key error, the old row is deleted
|
2011-07-01 02:57:31 +00:00
|
|
|
* and the new row is inserted in its place.
|
2011-06-23 03:14:11 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* We simulate this with standard SQL with a DELETE followed by INSERT. To
|
|
|
|
|
* perform the delete, we need to know what the unique indexes are so that
|
2011-06-23 03:14:11 +00:00
|
|
|
* we know how to find the conflicting rows.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* 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
|
2011-06-23 03:14:11 +00:00
|
|
|
* errors which wouldn't have occurred in MySQL.
|
2011-07-01 02:57:31 +00:00
|
|
|
*
|
2011-08-24 12:47:42 +00:00
|
|
|
* @param $table String: The table to replace the row(s) in.
|
|
|
|
|
* @param $rows array Can be either a single row to insert, or multiple rows,
|
2011-06-23 03:14:11 +00:00
|
|
|
* in the same format as for DatabaseBase::insert()
|
2011-08-24 12:47:42 +00:00
|
|
|
* @param $uniqueIndexes array is an array of indexes. Each element may be either
|
2011-06-23 03:14:11 +00:00
|
|
|
* a field name or an array of field names
|
2010-08-25 18:50:36 +00:00
|
|
|
* @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' ) {
|
2011-06-23 03:14:11 +00:00
|
|
|
$quotedTable = $this->tableName( $table );
|
|
|
|
|
|
|
|
|
|
if ( count( $rows ) == 0 ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Single row case
|
|
|
|
|
if ( !is_array( reset( $rows ) ) ) {
|
|
|
|
|
$rows = array( $rows );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach( $rows as $row ) {
|
|
|
|
|
# Delete rows which collide
|
|
|
|
|
if ( $uniqueIndexes ) {
|
|
|
|
|
$sql = "DELETE FROM $quotedTable WHERE ";
|
|
|
|
|
$first = true;
|
|
|
|
|
foreach ( $uniqueIndexes as $index ) {
|
|
|
|
|
if ( $first ) {
|
|
|
|
|
$first = false;
|
|
|
|
|
$sql .= '( ';
|
|
|
|
|
} else {
|
|
|
|
|
$sql .= ' ) OR ( ';
|
|
|
|
|
}
|
|
|
|
|
if ( is_array( $index ) ) {
|
|
|
|
|
$first2 = true;
|
|
|
|
|
foreach ( $index as $col ) {
|
|
|
|
|
if ( $first2 ) {
|
|
|
|
|
$first2 = false;
|
|
|
|
|
} else {
|
|
|
|
|
$sql .= ' AND ';
|
|
|
|
|
}
|
|
|
|
|
$sql .= $col . '=' . $this->addQuotes( $row[$col] );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$sql .= $index . '=' . $this->addQuotes( $row[$index] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$sql .= ' )';
|
|
|
|
|
$this->query( $sql, $fname );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Now insert the row
|
|
|
|
|
$this->insert( $table, $row );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE
|
|
|
|
|
* statement.
|
|
|
|
|
*
|
2011-12-05 16:50:58 +00:00
|
|
|
* @param $table string Table name
|
|
|
|
|
* @param $rows array Rows to insert
|
|
|
|
|
* @param $fname string Caller function name
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return ResultWrapper
|
2011-06-23 03:14:11 +00:00
|
|
|
*/
|
|
|
|
|
protected function nativeReplace( $table, $rows, $fname ) {
|
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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* DELETE where the condition is a join.
|
2004-09-03 16:36:46 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* MySQL overrides this to use a multi-table DELETE syntax, in other databases
|
2011-06-20 06:52:44 +00:00
|
|
|
* we use sub-selects
|
2004-09-03 16:36:46 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* For safety, an empty $conds will not delete everything. If you want to
|
2011-06-20 06:52:44 +00:00
|
|
|
* delete all rows where the join condition matches, set $conds='*'.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2011-06-20 06:52:44 +00:00
|
|
|
* DO NOT put the join condition in $conds.
|
|
|
|
|
*
|
|
|
|
|
* @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.
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $conds Array: Condition array of field names mapped to variables,
|
2011-06-20 06:52:44 +00:00
|
|
|
* ANDed together in the WHERE clause
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $fname String: Calling function name (use __METHOD__) for
|
2011-06-20 06:52:44 +00:00
|
|
|
* logs/profiling
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2011-07-01 02:57:31 +00:00
|
|
|
function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
|
|
|
|
|
$fname = 'DatabaseBase::deleteJoin' )
|
2011-06-20 06:52:44 +00:00
|
|
|
{
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( !$conds ) {
|
2011-07-01 02:57:31 +00:00
|
|
|
throw new DBUnexpectedError( $this,
|
2011-06-20 06:52:44 +00:00
|
|
|
'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 );
|
2011-06-18 20:26:31 +00:00
|
|
|
$sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $conds != '*' ) {
|
2011-06-18 20:26:31 +00:00
|
|
|
$sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2011-06-18 20:26:31 +00:00
|
|
|
$sql .= ')';
|
2004-07-24 07:24:04 +00:00
|
|
|
|
2011-06-18 20:26:31 +00:00
|
|
|
$this->query( $sql, $fname );
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
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"
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @param $table string
|
|
|
|
|
* @param $field string
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @return string Returns the text of the low priority option if it is
|
2011-06-20 06:52:44 +00:00
|
|
|
* supported, or a blank string otherwise
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* DELETE query wrapper.
|
|
|
|
|
*
|
2011-06-26 23:01:29 +00:00
|
|
|
* @param $table Array Table name
|
|
|
|
|
* @param $conds String|Array of conditions. See $conds in DatabaseBase::select() for
|
2011-06-20 06:52:44 +00:00
|
|
|
* the format. Use $conds == "*" to delete all rows
|
2011-06-26 23:01:29 +00:00
|
|
|
* @param $fname String name of the calling function
|
2005-07-18 02:30:04 +00:00
|
|
|
*
|
2011-06-20 06:52:44 +00:00
|
|
|
* @return bool
|
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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* INSERT SELECT wrapper. Takes data from a SELECT query and inserts it
|
|
|
|
|
* into another table.
|
|
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $destTable string The table name to insert into
|
|
|
|
|
* @param $srcTable string|array May be either a table name, or an array of table names
|
2011-06-20 06:52:44 +00:00
|
|
|
* to include in a join.
|
|
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $varMap array must be an associative array of the form
|
2011-07-01 02:57:31 +00:00
|
|
|
* array( 'dest1' => 'source1', ...). Source items may be literals
|
|
|
|
|
* rather than field names, but strings should be quoted with
|
2011-06-20 06:52:44 +00:00
|
|
|
* DatabaseBase::addQuotes()
|
|
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $conds array Condition array. See $conds in DatabaseBase::select() for
|
2011-07-01 02:57:31 +00:00
|
|
|
* the details of the format of condition arrays. May be "*" to copy the
|
2011-06-20 06:52:44 +00:00
|
|
|
* whole table.
|
|
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $fname string The function name of the caller, from __METHOD__
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $insertOptions array Options for the INSERT part of the query, see
|
2011-06-20 06:52:44 +00:00
|
|
|
* DatabaseBase::insert() for details.
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $selectOptions array Options for the SELECT part of the query, see
|
2011-06-20 06:52:44 +00:00
|
|
|
* DatabaseBase::select() for details.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return ResultWrapper
|
2005-07-18 02:30:04 +00:00
|
|
|
*/
|
2011-07-01 02:57:31 +00:00
|
|
|
function insertSelect( $destTable, $srcTable, $varMap, $conds,
|
2011-06-20 06:52:44 +00:00
|
|
|
$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 != '*' ) {
|
2011-11-07 17:19:08 +00:00
|
|
|
if ( is_array( $conds ) ) {
|
|
|
|
|
$conds = $this->makeList( $conds, LIST_AND );
|
|
|
|
|
}
|
|
|
|
|
$sql .= " WHERE $conds";
|
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.
|
|
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $sql String SQL query we will append the limit too
|
|
|
|
|
* @param $limit Integer the SQL limit
|
|
|
|
|
* @param $offset Integer|false 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
|
|
|
|
2011-08-06 00:24:18 +00:00
|
|
|
/**
|
|
|
|
|
* @param $sql
|
|
|
|
|
* @param $num
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
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})";
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-24 19:55:38 +00:00
|
|
|
/**
|
|
|
|
|
* Determines how long the server has been up
|
|
|
|
|
* STUB
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
function getServerUptime() {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2011-11-23 23:45:46 +00:00
|
|
|
/**
|
|
|
|
|
* Determines if the last failure was due to a lock timeout
|
|
|
|
|
* STUB
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function wasLockTimeout() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-23 03:14:11 +00:00
|
|
|
* Wait for the slave to catch up to a given master position.
|
|
|
|
|
*
|
|
|
|
|
* @param $pos DBMasterPos object
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $timeout Integer: the maximum number of seconds to wait for
|
2011-06-23 03:14:11 +00:00
|
|
|
* synchronisation
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2011-06-23 03:14:11 +00:00
|
|
|
* @return An integer: zero if the slave was past that position already,
|
2011-07-01 02:57:31 +00:00
|
|
|
* greater than zero if we waited for some period of time, less than
|
2011-06-23 03:14:11 +00:00
|
|
|
* zero if we timed out.
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2011-06-23 03:14:11 +00:00
|
|
|
function masterPosWait( DBMasterPos $pos, $timeout ) {
|
2010-08-25 01:24:47 +00:00
|
|
|
$fname = 'DatabaseBase::masterPosWait';
|
2008-03-30 09:48:15 +00:00
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-06-23 03:14:11 +00:00
|
|
|
wfProfileOut( $fname );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2011-06-23 03:14:11 +00:00
|
|
|
# Real waits are implemented in the subclass.
|
|
|
|
|
return 0;
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2011-06-23 03:14:11 +00:00
|
|
|
* Get the replication position of this slave
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
2011-06-23 03:14:11 +00:00
|
|
|
* @return DBMasterPos, or false if this is not a slave.
|
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;
|
|
|
|
|
} else {
|
2011-06-23 03:14:11 +00:00
|
|
|
# Stub
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
/**
|
2011-06-23 03:14:11 +00:00
|
|
|
* Get the position of this master
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
2011-06-23 03:14:11 +00:00
|
|
|
* @return DBMasterPos, or false if this is not a master
|
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 ) );
|
|
|
|
|
} 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
|
2011-08-11 23:58:29 +00:00
|
|
|
*
|
|
|
|
|
* @param $fname string
|
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
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @param $fname string
|
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.
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @param $fname string
|
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-06-17 15:59:55 +00:00
|
|
|
* The table names passed to this function shall not be quoted (this
|
2011-04-12 18:52:16 +00:00
|
|
|
* 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
|
|
|
|
|
*/
|
2011-07-01 02:57:31 +00:00
|
|
|
function duplicateTableStructure( $oldName, $newName, $temporary = false,
|
|
|
|
|
$fname = 'DatabaseBase::duplicateTableStructure' )
|
2011-06-20 06:52:44 +00:00
|
|
|
{
|
2011-07-01 02:57:31 +00:00
|
|
|
throw new MWException(
|
2011-06-20 06:52:44 +00:00
|
|
|
'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' ) {
|
2011-06-03 18:39:10 +00:00
|
|
|
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
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Convert a timestamp in one of the formats accepted by wfTimestamp()
|
2011-06-20 06:52:44 +00:00
|
|
|
* to the format used for inserting into timestamp fields in this DBMS.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* The result is unquoted, and needs to be passed through addQuotes()
|
2011-06-20 06:52:44 +00:00
|
|
|
* before it can be included in raw SQL.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $ts string|int
|
|
|
|
|
*
|
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
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Convert a timestamp in one of the formats accepted by wfTimestamp()
|
|
|
|
|
* to the format used for inserting into timestamp fields in this DBMS. If
|
|
|
|
|
* NULL is input, it is passed through, allowing NULL values to be inserted
|
2011-06-20 06:52:44 +00:00
|
|
|
* into timestamp fields.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* The result is unquoted, and needs to be passed through addQuotes()
|
2011-06-20 06:52:44 +00:00
|
|
|
* before it can be included in raw SQL.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $ts string|int
|
|
|
|
|
*
|
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
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Take the result from a query, and wrap it in a ResultWrapper if
|
|
|
|
|
* necessary. Boolean values are passed through as is, to indicate success
|
|
|
|
|
* of write queries or failure.
|
2011-06-20 12:09:22 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* Once upon a time, DatabaseBase::query() returned a bare MySQL result
|
2011-06-20 12:09:22 +00:00
|
|
|
* resource, and it was necessary to call this function to convert it to
|
|
|
|
|
* a wrapper. Nowadays, raw database objects are never exposed to external
|
2011-07-01 02:57:31 +00:00
|
|
|
* callers, so this is unnecessary in external code. For compatibility with
|
2011-06-20 12:09:22 +00:00
|
|
|
* old code, ResultWrapper objects are passed through unaltered.
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @param $result bool|ResultWrapper
|
|
|
|
|
*
|
2011-11-29 21:04:20 +00:00
|
|
|
* @return bool|ResultWrapper
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2011-06-20 12:09:22 +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
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @param $valuedata
|
|
|
|
|
* @param $valuename string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2011-08-29 05:04:55 +00:00
|
|
|
* Get slave lag. Currently supported only by MySQL.
|
|
|
|
|
*
|
2011-09-13 10:59:40 +00:00
|
|
|
* Note that this function will generate a fatal error on many
|
|
|
|
|
* installations. Most callers should use LoadBalancer::safeGetLag()
|
2011-08-29 05:04:55 +00:00
|
|
|
* instead.
|
|
|
|
|
*
|
2010-07-02 13:17:28 +00:00
|
|
|
* @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
|
|
|
* Return the maximum number of items allowed in a list, or 0 for unlimited.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
2011-08-06 00:24:18 +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
|
|
|
|
2011-06-20 06:52:44 +00:00
|
|
|
/**
|
2011-07-01 02:57:31 +00:00
|
|
|
* Some DBMSs have a special format for inserting into blob fields, they
|
|
|
|
|
* don't allow simple quoted strings to be inserted. To insert into such
|
|
|
|
|
* a field, pass the data through this function before passing it to
|
|
|
|
|
* DatabaseBase::insert().
|
2011-12-05 16:50:58 +00:00
|
|
|
* @param $b string
|
|
|
|
|
* @return string
|
2011-06-20 06:52:44 +00:00
|
|
|
*/
|
2010-09-04 13:48:16 +00:00
|
|
|
function encodeBlob( $b ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return $b;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 06:52:44 +00:00
|
|
|
/**
|
|
|
|
|
* Some DBMSs return a special placeholder object representing blob fields
|
2011-07-01 02:57:31 +00:00
|
|
|
* in result objects. Pass the object through this function to return the
|
2011-06-20 06:52:44 +00:00
|
|
|
* original string.
|
2011-12-05 16:50:58 +00:00
|
|
|
* @param $b string
|
|
|
|
|
* @return string
|
2011-06-20 06:52:44 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2011-11-23 19:25:59 +00:00
|
|
|
* Override database's default connection timeout
|
2009-06-16 21:00:38 +00:00
|
|
|
*
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $timeout Integer in seconds
|
2011-11-23 19:25:59 +00:00
|
|
|
* @return void
|
|
|
|
|
* @deprecated since 1.19; use setSessionOptions()
|
2004-09-09 00:02:38 +00:00
|
|
|
*/
|
2011-11-23 19:25:59 +00:00
|
|
|
public function setTimeout( $timeout ) {
|
2011-12-13 05:19:05 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.19' );
|
2011-11-23 19:25:59 +00:00
|
|
|
$this->setSessionOptions( array( 'connTimeout' => $timeout ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Override database's default behavior. $options include:
|
|
|
|
|
* 'connTimeout' : Set the connection timeout value in seconds.
|
|
|
|
|
* May be useful for very long batch queries such as
|
|
|
|
|
* full-wiki dumps, where a single query reads out over
|
|
|
|
|
* hours or days.
|
|
|
|
|
*
|
|
|
|
|
* @param $options Array
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function setSessionOptions( array $options ) {}
|
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.
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* Returns true on success, error string or exception on failure (depending
|
2011-06-20 06:52:44 +00:00
|
|
|
* 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
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $fname String: Calling function name or false if name should be
|
2011-06-20 06:52:44 +00:00
|
|
|
* generated dynamically using $filename
|
2011-11-29 21:04:20 +00:00
|
|
|
* @return bool|string
|
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
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* Read and execute commands from an open file handle.
|
|
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* Returns true on success, error string or exception on failure (depending
|
2011-06-20 06:52:44 +00:00
|
|
|
* 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
|
2011-11-29 21:04:20 +00:00
|
|
|
* @return bool|string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2011-07-01 02:57:31 +00:00
|
|
|
function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
|
|
|
|
|
$fname = 'DatabaseBase::sourceStream' )
|
2011-06-20 06:52:44 +00:00
|
|
|
{
|
2011-12-27 12:29:36 +00:00
|
|
|
$cmd = '';
|
2008-03-30 09:48:15 +00:00
|
|
|
$done = false;
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2011-12-27 12:29:36 +00:00
|
|
|
while ( !feof( $fp ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
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 ) );
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2011-12-27 12:29:36 +00:00
|
|
|
if ( $line == '' ) {
|
2010-08-30 20:28:32 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2011-09-27 16:15:29 +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-08-30 20:28:32 +00:00
|
|
|
if ( $cmd != '' ) {
|
|
|
|
|
$cmd .= ' ';
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2011-12-27 12:29:36 +00:00
|
|
|
$done = $this->streamStatementEnd( $cmd, $line );
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$cmd .= "$line\n";
|
|
|
|
|
|
2011-12-27 12:29:36 +00:00
|
|
|
if ( $done || feof( $fp ) ) {
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-27 12:29:36 +00:00
|
|
|
/**
|
|
|
|
|
* Called by sourceStream() to check if we've reached a statement end
|
2012-01-04 16:43:46 +00:00
|
|
|
*
|
2011-12-27 12:29:36 +00:00
|
|
|
* @param $sql String: SQL assembled so far
|
|
|
|
|
* @param $newLine String: New line about to be added to $sql
|
|
|
|
|
* @returns Bool: Whether $newLine contains end of the statement
|
|
|
|
|
*/
|
|
|
|
|
protected function streamStatementEnd( &$sql, &$newLine ) {
|
|
|
|
|
if ( $this->delimiter ) {
|
|
|
|
|
$prev = $newLine;
|
|
|
|
|
$newLine = preg_replace( '/' . preg_quote( $this->delimiter, '/' ) . '$/', '', $newLine );
|
|
|
|
|
if ( $newLine != $prev ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-04 09:27:02 +00:00
|
|
|
/**
|
2011-06-20 06:52:44 +00:00
|
|
|
* Database independent variable replacement. Replaces a set of variables
|
|
|
|
|
* in an SQL statement with their contents as given by $this->getSchemaVars().
|
|
|
|
|
*
|
|
|
|
|
* Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables.
|
2011-01-31 20:58:06 +00:00
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* - '{$var}' should be used for text and is passed through the database's
|
2011-06-20 06:52:44 +00:00
|
|
|
* addQuotes method.
|
2011-07-01 02:57:31 +00:00
|
|
|
* - `{$var}` should be used for identifiers (eg: table and database names),
|
|
|
|
|
* it is passed through the database's addIdentifierQuotes method which
|
2011-06-20 06:52:44 +00:00
|
|
|
* can be overridden if the database uses something other than backticks.
|
2011-07-01 02:57:31 +00:00
|
|
|
* - / *$var* / is just encoded, besides traditional table prefix and
|
2011-06-20 06:52:44 +00:00
|
|
|
* table options its 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
|
2011-07-01 02:57:31 +00:00
|
|
|
*
|
|
|
|
|
* @param $ins string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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.
|
2011-11-01 23:48:09 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
* 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 getSchemaVars() {
|
|
|
|
|
if ( $this->mSchemaVars ) {
|
|
|
|
|
return $this->mSchemaVars;
|
|
|
|
|
} else {
|
|
|
|
|
return $this->getDefaultSchemaVars();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get schema variables to use if none have been set via setSchemaVars().
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2011-01-31 20:58:06 +00:00
|
|
|
* Override this in derived classes to provide variables for tables.sql
|
|
|
|
|
* and SQL patch files.
|
2011-07-01 02:57:31 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
* 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
|
2011-07-01 02:57:31 +00:00
|
|
|
*
|
|
|
|
|
* @param $matches array
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
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
|
2011-07-01 02:57:31 +00:00
|
|
|
*
|
|
|
|
|
* @param $matches array
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2009-01-19 13:56:08 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2011-07-01 02:57:31 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
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
|
2011-07-01 02:57:31 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2009-06-25 00:40:12 +00:00
|
|
|
*/
|
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
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $tableName string
|
|
|
|
|
* @param $fName string
|
|
|
|
|
* @return bool|ResultWrapper
|
2011-10-30 13:44:27 +00:00
|
|
|
* @since 1.18
|
2010-12-28 01:19:16 +00:00
|
|
|
*/
|
2010-12-28 01:22:38 +00:00
|
|
|
public function dropTable( $tableName, $fName = 'DatabaseBase::dropTable' ) {
|
2011-11-10 20:39:23 +00:00
|
|
|
if( !$this->tableExists( $tableName, $fName ) ) {
|
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
|
|
|
*
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $value Mixed: true for allow, false for deny, or "default" to
|
2011-06-20 06:52:44 +00:00
|
|
|
* 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
|
|
|
}
|