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
|
|
|
|
|
*
|
2012-04-26 08:47:10 +00:00
|
|
|
* This file deals with database interface functions
|
|
|
|
|
* and query specifics/optimisations.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
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
|
|
|
* @file
|
|
|
|
|
* @ingroup Database
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
|
|
|
|
|
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
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $server database server host
|
|
|
|
|
* @param string $user database user name
|
|
|
|
|
* @param string $password database user password
|
|
|
|
|
* @param string $dbName database name
|
2010-09-08 17:48:11 +00:00
|
|
|
* @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.
|
2013-02-18 14:39:29 +00:00
|
|
|
* If no more rows are available, false is returned.
|
2010-09-08 17:48:11 +00:00
|
|
|
*
|
2011-10-15 17:59:42 +00:00
|
|
|
* @param $res ResultWrapper|object as returned from DatabaseBase::query(), etc.
|
2013-02-18 14:39:29 +00:00
|
|
|
* @return object|bool
|
2010-09-08 17:48:11 +00:00
|
|
|
* @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'].
|
2013-02-18 14:39:29 +00:00
|
|
|
* If no more rows are available, false is returned.
|
2010-09-08 17:48:11 +00:00
|
|
|
*
|
2011-05-25 18:58:02 +00:00
|
|
|
* @param $res ResultWrapper result object as returned from DatabaseBase::query(), etc.
|
2013-02-18 14:39:29 +00:00
|
|
|
* @return array|bool
|
2010-09-08 17:48:11 +00:00
|
|
|
* @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:
|
2013-02-03 18:47:42 +00:00
|
|
|
* $id = $dbw->nextSequenceValue( 'page_page_id_seq' );
|
|
|
|
|
* $dbw->insert( 'page', array( 'page_id' => $id ) );
|
2010-09-08 17:48:11 +00:00
|
|
|
* $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
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @param string $field 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
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $table Table name
|
|
|
|
|
* @param string $index Index name
|
|
|
|
|
* @param string $fname Calling function name
|
2010-11-23 11:14:48 +00:00
|
|
|
* @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()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $s to be slashed.
|
2010-09-08 17:48:11 +00:00
|
|
|
* @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;
|
|
|
|
|
|
|
|
|
|
protected $mConn = null;
|
2008-07-08 10:41:08 +00:00
|
|
|
protected $mOpened = false;
|
2008-03-30 09:48:15 +00:00
|
|
|
|
2013-04-11 06:54:14 +00:00
|
|
|
/** @var array of Closure */
|
2012-11-14 19:40:36 +00:00
|
|
|
protected $mTrxIdleCallbacks = array();
|
2013-04-11 06:54:14 +00:00
|
|
|
/** @var array of Closure */
|
|
|
|
|
protected $mTrxPreCommitCallbacks = array();
|
2012-08-29 19:32:47 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2012-01-11 20:19:55 +00:00
|
|
|
protected $delimiter = ';';
|
|
|
|
|
|
2012-08-27 09:55:15 +00:00
|
|
|
/**
|
2012-09-22 08:14:35 +00:00
|
|
|
* Remembers the function name given for starting the most recent transaction via begin().
|
2012-08-27 09:55:15 +00:00
|
|
|
* Used to provide additional context for error reporting.
|
|
|
|
|
*
|
|
|
|
|
* @var String
|
|
|
|
|
* @see DatabaseBase::mTrxLevel
|
|
|
|
|
*/
|
|
|
|
|
private $mTrxFname = null;
|
|
|
|
|
|
2012-09-22 08:14:35 +00:00
|
|
|
/**
|
|
|
|
|
* Record if possible write queries were done in the last transaction started
|
|
|
|
|
*
|
|
|
|
|
* @var Bool
|
|
|
|
|
* @see DatabaseBase::mTrxLevel
|
|
|
|
|
*/
|
|
|
|
|
private $mTrxDoneWrites = false;
|
|
|
|
|
|
2012-10-05 08:19:42 +00:00
|
|
|
/**
|
|
|
|
|
* Record if the current transaction was started implicitly due to DBO_TRX being set.
|
|
|
|
|
*
|
|
|
|
|
* @var Bool
|
|
|
|
|
* @see DatabaseBase::mTrxLevel
|
|
|
|
|
*/
|
|
|
|
|
private $mTrxAutomatic = false;
|
|
|
|
|
|
2012-11-22 03:53:24 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.21
|
|
|
|
|
* @var file handle for upgrade
|
|
|
|
|
*/
|
|
|
|
|
protected $fileHandle = null;
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-25 10:39:32 +00:00
|
|
|
/**
|
|
|
|
|
* @return string: command delimiter used by this database engine
|
|
|
|
|
*/
|
|
|
|
|
public function getDelimiter() {
|
|
|
|
|
return $this->delimiter;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
*
|
2012-02-09 19:30:01 +00:00
|
|
|
* @return bool|null previous value of the flag
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
|
|
*
|
2012-02-09 19:30:01 +00:00
|
|
|
* @return null|bool The previous value of the flag
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param int $level An integer (0 or 1), or omitted to leave it unchanged.
|
2012-02-09 19:30:01 +00:00
|
|
|
* @return int The previous value
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param int $count The count to set, or omitted to leave it unchanged.
|
2012-02-09 19:30:01 +00:00
|
|
|
* @return int The error count
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $prefix The table prefix to set, or omitted to leave it unchanged.
|
2012-02-09 19:30:01 +00:00
|
|
|
* @return string The previous table prefix.
|
2011-06-20 06:52:44 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
|
2013-02-03 18:47:42 +00:00
|
|
|
/**
|
2012-11-22 03:53:24 +00:00
|
|
|
* Set the filehandle to copy write statements to.
|
|
|
|
|
*
|
|
|
|
|
* @param $fh filehandle
|
|
|
|
|
*/
|
|
|
|
|
public function setFileHandle( $fh ) {
|
|
|
|
|
$this->fileHandle = $fh;
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $name 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function setFakeSlaveLag( $lag ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function setFakeMaster( $enabled = true ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function cascadingDeletes() {
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function cleanupTriggers() {
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function strictIPs() {
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
2013-03-07 16:27:38 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function realTimestamps() {
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function implicitGroupby() {
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function implicitOrderby() {
|
2008-03-30 09:48:15 +00:00
|
|
|
return true;
|
2006-06-06 23:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function lastQuery() {
|
2011-05-25 18:58:02 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function doneWrites() {
|
2011-05-25 18:58:02 +00:00
|
|
|
return $this->mDoneWrites;
|
|
|
|
|
}
|
2009-02-17 14:06:42 +00:00
|
|
|
|
2012-11-14 19:40:36 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if there is a transaction open with possible write
|
2013-04-11 06:54:14 +00:00
|
|
|
* queries or transaction pre-commit/idle callbacks waiting on it to finish.
|
2012-11-14 19:40:36 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function writesOrCallbacksPending() {
|
2013-04-11 06:54:14 +00:00
|
|
|
return $this->mTrxLevel && (
|
|
|
|
|
$this->mTrxDoneWrites || $this->mTrxIdleCallbacks || $this->mTrxPreCommitCallbacks
|
|
|
|
|
);
|
2012-11-14 19:40:36 +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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function isOpen() {
|
2011-05-25 18:58:02 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function setFlag( $flag ) {
|
2012-03-30 00:10:52 +00:00
|
|
|
global $wgDebugDBTransactions;
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->mFlags |= $flag;
|
2013-04-27 12:02:08 +00:00
|
|
|
if ( ( $flag & DBO_TRX ) & $wgDebugDBTransactions ) {
|
2013-02-03 18:47:42 +00:00
|
|
|
wfDebug( "Implicit transactions are now disabled.\n" );
|
2012-03-30 00:10:52 +00:00
|
|
|
}
|
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
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function clearFlag( $flag ) {
|
2012-03-30 00:10:52 +00:00
|
|
|
global $wgDebugDBTransactions;
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->mFlags &= ~$flag;
|
2012-03-30 00:10:52 +00:00
|
|
|
if ( ( $flag & DBO_TRX ) && $wgDebugDBTransactions ) {
|
2013-02-03 18:47:42 +00:00
|
|
|
wfDebug( "Implicit transactions are now disabled.\n" );
|
2012-03-30 00:10:52 +00:00
|
|
|
}
|
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
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function getProperty( $name ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return $this->$name;
|
|
|
|
|
}
|
2006-06-06 23:07:26 +00:00
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $server database server host
|
|
|
|
|
* @param string $user database user name
|
|
|
|
|
* @param string $password database user password
|
|
|
|
|
* @param string $dbName database name
|
2008-03-30 09:48:15 +00:00
|
|
|
* @param $flags
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $tablePrefix database table prefixes. By default use the prefix gave in LocalSettings.php
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
) {
|
2012-03-30 00:10:52 +00:00
|
|
|
global $wgDBprefix, $wgCommandLineMode, $wgDebugDBTransactions;
|
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;
|
2012-03-30 00:10:52 +00:00
|
|
|
if ( $wgDebugDBTransactions ) {
|
2013-02-03 18:47:42 +00:00
|
|
|
wfDebug( "Implicit transaction open disabled.\n" );
|
2012-03-30 00:10:52 +00:00
|
|
|
}
|
2006-06-06 23:07:26 +00:00
|
|
|
} else {
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->mFlags |= DBO_TRX;
|
2012-03-30 00:10:52 +00:00
|
|
|
if ( $wgDebugDBTransactions ) {
|
2013-02-03 18:47:42 +00:00
|
|
|
wfDebug( "Implicit transaction open enabled.\n" );
|
2012-03-30 00:10:52 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
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 ForeignDBRepo::getMasterDB()
|
|
|
|
|
* @see WebInstaller_DBConnect::execute()
|
|
|
|
|
*
|
2012-02-14 04:05:49 +00:00
|
|
|
* @since 1.18
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $dbType A possible DB type
|
|
|
|
|
* @param array $p 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
|
|
|
|
|
*/
|
2013-01-26 19:49:50 +00:00
|
|
|
final public static function factory( $dbType, $p = array() ) {
|
2011-01-07 22:32:52 +00:00
|
|
|
$canonicalDBTypes = array(
|
2013-02-25 18:55:50 +00:00
|
|
|
'mysql', 'postgres', 'sqlite', 'oracle', 'mssql'
|
2011-01-07 22:32:52 +00:00
|
|
|
);
|
|
|
|
|
$dbType = strtolower( $dbType );
|
2012-01-03 19:55:45 +00:00
|
|
|
$class = 'Database' . ucfirst( $dbType );
|
2011-01-24 16:31:36 +00:00
|
|
|
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( in_array( $dbType, $canonicalDBTypes ) || ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) ) {
|
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
|
|
|
);
|
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 );
|
2012-03-01 13:11:25 +00:00
|
|
|
$error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
|
2008-10-06 00:45:18 +00:00
|
|
|
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
|
|
|
|
|
*/
|
2013-02-03 18:47:42 +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
|
|
|
|
|
*
|
2012-12-09 03:09:48 +00:00
|
|
|
* @throws MWException
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return Bool operation success. true if already closed.
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-02-28 14:42:08 +00:00
|
|
|
public function close() {
|
2012-11-14 19:40:36 +00:00
|
|
|
if ( count( $this->mTrxIdleCallbacks ) ) { // sanity
|
|
|
|
|
throw new MWException( "Transaction idle callbacks still pending." );
|
|
|
|
|
}
|
2012-02-28 14:42:08 +00:00
|
|
|
$this->mOpened = false;
|
|
|
|
|
if ( $this->mConn ) {
|
|
|
|
|
if ( $this->trxLevel() ) {
|
2012-10-09 10:26:47 +00:00
|
|
|
if ( !$this->mTrxAutomatic ) {
|
2012-10-05 08:19:42 +00:00
|
|
|
wfWarn( "Transaction still in progress (from {$this->mTrxFname}), " .
|
|
|
|
|
" performing implicit commit before closing connection!" );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-09 13:34:47 +00:00
|
|
|
$this->commit( __METHOD__, 'flush' );
|
2012-02-28 14:42:08 +00:00
|
|
|
}
|
2012-10-05 08:19:42 +00:00
|
|
|
|
2012-02-28 14:42:08 +00:00
|
|
|
$ret = $this->closeConnection();
|
|
|
|
|
$this->mConn = false;
|
|
|
|
|
return $ret;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-06-16 20:22:11 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2012-02-28 14:42:08 +00:00
|
|
|
/**
|
|
|
|
|
* Closes underlying database connection
|
|
|
|
|
* @since 1.20
|
|
|
|
|
* @return bool: Whether connection was closed successfully
|
|
|
|
|
*/
|
2013-01-26 19:00:09 +00:00
|
|
|
abstract protected function closeConnection();
|
2012-02-28 14:42:08 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $error fallback error message, used if none is given by DB
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws DBConnectionError
|
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
|
|
|
*/
|
2013-01-26 19:00:09 +00:00
|
|
|
abstract protected function doQuery( $sql );
|
2011-06-20 07:19:20 +00:00
|
|
|
|
2009-02-17 14:06:42 +00:00
|
|
|
/**
|
|
|
|
|
* Determine whether a query writes to the DB.
|
|
|
|
|
* Should return true if unsure.
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function isWriteQuery( $sql ) {
|
2012-05-05 09:24:07 +00:00
|
|
|
return !preg_match( '/^(?:SELECT|BEGIN|ROLLBACK|COMMIT|SET|SHOW|EXPLAIN|\(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?
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
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
|
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 = '';
|
|
|
|
|
}
|
2012-12-01 10:52:23 +00:00
|
|
|
|
|
|
|
|
// Add trace comment to the begin of the sql string, right after the operator.
|
|
|
|
|
// Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (bug 42598)
|
|
|
|
|
$commentedSql = preg_replace( '/\s|$/', " /* $fname $userName */ ", $sql, 1 );
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
# If DBO_TRX is set, start a transaction
|
2012-09-22 08:14:35 +00:00
|
|
|
if ( ( $this->mFlags & DBO_TRX ) && !$this->mTrxLevel &&
|
|
|
|
|
$sql != 'BEGIN' && $sql != 'COMMIT' && $sql != 'ROLLBACK' )
|
|
|
|
|
{
|
|
|
|
|
# Avoid establishing transactions for SHOW and SET statements too -
|
2011-06-20 06:52:44 +00:00
|
|
|
# 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)
|
2012-03-30 00:10:52 +00:00
|
|
|
if ( strpos( $sqlstart, "SHOW " ) !== 0 && strpos( $sqlstart, "SET " ) !== 0 ) {
|
|
|
|
|
global $wgDebugDBTransactions;
|
|
|
|
|
if ( $wgDebugDBTransactions ) {
|
2013-02-03 18:47:42 +00:00
|
|
|
wfDebug( "Implicit transaction start.\n" );
|
2012-03-30 00:10:52 +00:00
|
|
|
}
|
2012-01-18 12:10:16 +00:00
|
|
|
$this->begin( __METHOD__ . " ($fname)" );
|
2012-10-05 08:19:42 +00:00
|
|
|
$this->mTrxAutomatic = true;
|
2012-03-30 00:10:52 +00:00
|
|
|
}
|
2005-10-29 01:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-22 08:14:35 +00:00
|
|
|
# Keep track of whether the transaction has write queries pending
|
|
|
|
|
if ( $this->mTrxLevel && !$this->mTrxDoneWrites && $this->isWriteQuery( $sql ) ) {
|
|
|
|
|
$this->mTrxDoneWrites = true;
|
|
|
|
|
}
|
|
|
|
|
|
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 ) {
|
2013-04-07 16:34:58 +00:00
|
|
|
if ( !Profiler::instance()->isStub() ) {
|
|
|
|
|
wfProfileOut( $queryProf );
|
|
|
|
|
wfProfileOut( $totalProf );
|
|
|
|
|
}
|
2009-02-04 09:10:32 +00:00
|
|
|
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;
|
2012-11-14 19:40:36 +00:00
|
|
|
$this->mTrxIdleCallbacks = array(); // cancel
|
2013-04-11 06:54:14 +00:00
|
|
|
$this->mTrxPreCommitCallbacks = array(); // cancel
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws DBQueryError
|
2006-08-16 00:58:42 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
# 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-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
|
|
|
*/
|
2012-06-16 05:24:59 +00:00
|
|
|
protected 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
|
|
|
*/
|
2012-06-16 05:24:59 +00:00
|
|
|
protected 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
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $prepared the prepared sql
|
2008-11-29 18:50:39 +00:00
|
|
|
* @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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
/**
|
2012-06-16 05:24:59 +00:00
|
|
|
* For faking prepared SQL statements on DBs that don't support it directly.
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $preparedQuery a 'preparable' SQL statement
|
|
|
|
|
* @param array $args 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function fillPrepared( $preparedQuery, $args ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws DBUnexpectedError
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return String
|
2007-07-30 14:10:42 +00:00
|
|
|
*/
|
2012-06-16 05:24:59 +00:00
|
|
|
protected function fillPreparedArg( $matches ) {
|
2013-04-11 05:29:05 +00:00
|
|
|
switch ( $matches[1] ) {
|
|
|
|
|
case '\\?':
|
|
|
|
|
return '?';
|
|
|
|
|
case '\\!':
|
|
|
|
|
return '!';
|
|
|
|
|
case '\\&':
|
|
|
|
|
return '&';
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2013-02-03 18:47:42 +00:00
|
|
|
list( /* $n */, $arg ) = each( $this->preparedArgs );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2013-04-11 05:29:05 +00:00
|
|
|
switch ( $matches[1] ) {
|
|
|
|
|
case '?':
|
|
|
|
|
return $this->addQuotes( $arg );
|
|
|
|
|
case '!':
|
|
|
|
|
return $arg;
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
*/
|
2013-04-11 05:29:05 +00:00
|
|
|
public function freeResult( $res ) {
|
|
|
|
|
}
|
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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string|array $table Table name. See DatabaseBase::select() for details.
|
|
|
|
|
* @param string $var 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.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string|array $cond The condition array. See DatabaseBase::select() for details.
|
|
|
|
|
* @param string $fname The function name of the caller.
|
|
|
|
|
* @param string|array $options The query options. See DatabaseBase::select() for details.
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2012-02-09 17:42:35 +00:00
|
|
|
* @return bool|mixed The value from the field, or false on failure.
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function selectField( $table, $var, $cond = '', $fname = 'DatabaseBase::selectField',
|
2013-04-11 05:29:05 +00:00
|
|
|
$options = array()
|
|
|
|
|
) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( !is_array( $options ) ) {
|
|
|
|
|
$options = array( $options );
|
2005-10-28 01:08:49 +00:00
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$options['LIMIT'] = 1;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$res = $this->select( $table, $var, $cond, $fname, $options );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $res === false || !$this->numRows( $res ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$row = $this->fetchRow( $res );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $row !== false ) {
|
2009-01-09 03:53:28 +00:00
|
|
|
return reset( $row );
|
2004-01-10 16:44:31 +00:00
|
|
|
} else {
|
2008-03-30 09:48:15 +00:00
|
|
|
return false;
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-03 16:36:46 +00:00
|
|
|
/**
|
2008-03-30 09:48:15 +00:00
|
|
|
* Returns an optional USE INDEX clause to go after the table, and a
|
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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $options 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
|
2013-01-26 09:43:20 +00:00
|
|
|
$preLimitTail .= $this->makeGroupByWithHaving( $options );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2013-01-26 09:43:20 +00:00
|
|
|
$preLimitTail .= $this->makeOrderBy( $options );
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-26 09:43:20 +00:00
|
|
|
/**
|
|
|
|
|
* Returns an optional GROUP BY with an optional HAVING
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $options associative array of options
|
2013-01-26 09:43:20 +00:00
|
|
|
* @return string
|
|
|
|
|
* @see DatabaseBase::select()
|
|
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
public function makeGroupByWithHaving( $options ) {
|
|
|
|
|
$sql = '';
|
|
|
|
|
if ( isset( $options['GROUP BY'] ) ) {
|
|
|
|
|
$gb = is_array( $options['GROUP BY'] )
|
|
|
|
|
? implode( ',', $options['GROUP BY'] )
|
|
|
|
|
: $options['GROUP BY'];
|
|
|
|
|
$sql .= ' GROUP BY ' . $gb;
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $options['HAVING'] ) ) {
|
|
|
|
|
$having = is_array( $options['HAVING'] )
|
|
|
|
|
? $this->makeList( $options['HAVING'], LIST_AND )
|
|
|
|
|
: $options['HAVING'];
|
|
|
|
|
$sql .= ' HAVING ' . $having;
|
|
|
|
|
}
|
|
|
|
|
return $sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an optional ORDER BY
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $options associative array of options
|
2013-01-26 09:43:20 +00:00
|
|
|
* @return string
|
|
|
|
|
* @see DatabaseBase::select()
|
|
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
public function makeOrderBy( $options ) {
|
|
|
|
|
if ( isset( $options['ORDER BY'] ) ) {
|
|
|
|
|
$ob = is_array( $options['ORDER BY'] )
|
|
|
|
|
? implode( ',', $options['ORDER BY'] )
|
|
|
|
|
: $options['ORDER BY'];
|
|
|
|
|
return ' ORDER BY ' . $ob;
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string|array $table Table name
|
|
|
|
|
* @param string|array $vars Field names
|
|
|
|
|
* @param string|array $conds Conditions
|
|
|
|
|
* @param string $fname Caller function name
|
|
|
|
|
* @param array $options Query options
|
2011-06-26 23:01:29 +00:00
|
|
|
* @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
|
2012-08-15 13:16:09 +00:00
|
|
|
* can be complete fragments of SQL, for direct inclusion into the SELECT
|
|
|
|
|
* query. If an array is given, field aliases can be specified, for example:
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2012-08-15 13:16:09 +00:00
|
|
|
* array( 'maxrev' => 'MAX(rev_id)' )
|
|
|
|
|
*
|
|
|
|
|
* This includes an expression with the alias "maxrev" in the query.
|
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.
|
|
|
|
|
*
|
2012-08-31 18:12:19 +00:00
|
|
|
* - HAVING: May be either an string containing a HAVING clause or an array of
|
|
|
|
|
* conditions building the HAVING clause. If an array is given, the conditions
|
|
|
|
|
* constructed from each element are combined with AND.
|
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:
|
|
|
|
|
*
|
2013-02-03 18:47:42 +00:00
|
|
|
* array( 'page' => array( 'LEFT JOIN', 'page_latest=rev_id' ) )
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
|
|
|
|
* @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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function select( $table, $vars, $conds = '', $fname = 'DatabaseBase::select',
|
2011-07-01 02:57:31 +00:00
|
|
|
$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
|
2012-06-16 05:24:59 +00:00
|
|
|
* is returned, instead of being immediately executed. This can be useful for
|
|
|
|
|
* doing UNION queries, where the SQL text of each query is needed. In general,
|
|
|
|
|
* however, callers outside of Database classes should just use select().
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string|array $table Table name
|
|
|
|
|
* @param string|array $vars Field names
|
|
|
|
|
* @param string|array $conds Conditions
|
|
|
|
|
* @param string $fname Caller function name
|
|
|
|
|
* @param string|array $options Query options
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $join_conds string|array Join conditions
|
2008-05-13 04:56:14 +00:00
|
|
|
*
|
2012-02-09 19:30:01 +00:00
|
|
|
* @return string SQL query string.
|
2011-06-20 06:52:44 +00:00
|
|
|
* @see DatabaseBase::select()
|
2008-05-13 04:56:14 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function selectSQLText( $table, $vars, $conds = '', $fname = 'DatabaseBase::select',
|
|
|
|
|
$options = array(), $join_conds = array() )
|
|
|
|
|
{
|
2010-09-04 13:48:16 +00:00
|
|
|
if ( is_array( $vars ) ) {
|
2012-08-15 13:16:09 +00:00
|
|
|
$vars = implode( ',', $this->fieldNamesWithAlias( $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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string|array $table Table name
|
|
|
|
|
* @param string|array $vars Field names
|
|
|
|
|
* @param array $conds Conditions
|
|
|
|
|
* @param string $fname Caller function name
|
|
|
|
|
* @param string|array $options Query options
|
2011-07-01 02:57:31 +00:00
|
|
|
* @param $join_conds array|string Join conditions
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2012-03-03 20:22:18 +00:00
|
|
|
* @return object|bool
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function selectRow( $table, $vars, $conds, $fname = 'DatabaseBase::selectRow',
|
2011-07-01 02:57:31 +00:00
|
|
|
$options = array(), $join_conds = array() )
|
2011-06-20 06:52:44 +00:00
|
|
|
{
|
2012-05-07 13:52:55 +00:00
|
|
|
$options = (array)$options;
|
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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @param array|string $vars : unused
|
|
|
|
|
* @param array|string $conds : filters on the table
|
|
|
|
|
* @param string $fname function name for profiling
|
|
|
|
|
* @param array $options options for select
|
2010-07-04 14:41:26 +00:00
|
|
|
* @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;
|
2012-08-15 13:16:09 +00:00
|
|
|
$res = $this->select( $table, array( 'rowcount' => 'COUNT(*)' ), $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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $sql A SQL Query
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @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.
|
2013-03-24 10:01:51 +00:00
|
|
|
# $sql = preg_replace( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql );
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2013-03-24 10:01:51 +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
|
2013-03-24 10:01:51 +00:00
|
|
|
$sql = preg_replace( '/\s+/', ' ', $sql );
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
# All numbers => N
|
2013-03-24 10:01:51 +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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @param string $field filed to check on that table
|
|
|
|
|
* @param string $fname calling function name (optional)
|
2010-07-04 14:41:26 +00:00
|
|
|
* @return Boolean: whether $table has filed $field
|
2004-10-18 07:25:56 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function indexExists( $table, $index, $fname = 'DatabaseBase::indexExists' ) {
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( !$this->tableExists( $table ) ) {
|
2012-11-22 03:53:24 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function indexUnique( $table, $index ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$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
|
|
|
|
|
*/
|
2012-06-16 05:24:59 +00:00
|
|
|
protected 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
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $options of options
|
2010-08-25 00:18:47 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
|
2012-11-22 03:53:24 +00:00
|
|
|
$fh = null;
|
|
|
|
|
if ( isset( $options['fileHandle'] ) ) {
|
|
|
|
|
$fh = $options['fileHandle'];
|
|
|
|
|
}
|
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
|
|
|
|
2012-11-22 03:53:24 +00:00
|
|
|
if ( $fh !== null && false === fwrite( $fh, $sql ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
} elseif ( $fh !== null ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $options The options passed to DatabaseBase::update
|
2008-03-30 09:48:15 +00:00
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-06-16 05:24:59 +00:00
|
|
|
protected 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().
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $values 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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $options 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
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $a containing the data
|
2012-10-07 23:35:26 +00:00
|
|
|
* @param int $mode 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
|
|
|
*
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException|DBUnexpectedError
|
2011-02-18 22:58:02 +00:00
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function makeList( $a, $mode = LIST_COMMA ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
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 ) {
|
2012-10-23 15:23:22 +00:00
|
|
|
throw new MWException( __METHOD__ . ": empty input for field $field" );
|
2010-09-04 13:48:16 +00:00
|
|
|
} 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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $data organized as 2-d
|
2012-07-10 12:48:06 +00:00
|
|
|
* array(baseKeyVal => array(subKeyVal => [ignored], ...), ...)
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $baseKey field name to match the base-level keys to (eg 'pl_namespace')
|
|
|
|
|
* @param string $subKey field name to match the sub-level keys to (eg 'pl_title')
|
2010-07-04 14:41:26 +00:00
|
|
|
* @return Mixed: string SQL fragment, or false if no items in array.
|
2010-04-16 01:40:05 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
|
2010-04-16 01:40:05 +00:00
|
|
|
$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
|
|
|
/**
|
2012-06-16 05:24:59 +00:00
|
|
|
* Return aggregated value alias
|
|
|
|
|
*
|
|
|
|
|
* @param $valuedata
|
|
|
|
|
* @param $valuename string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2009-06-13 06:31:38 +00:00
|
|
|
*/
|
2012-06-16 05:24:59 +00:00
|
|
|
public function aggregateValue( $valuedata, $valuename = 'value' ) {
|
|
|
|
|
return $valuename;
|
|
|
|
|
}
|
2009-06-13 06:31:38 +00:00
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function bitOr( $fieldLeft, $fieldRight ) {
|
2009-06-14 20:13:17 +00:00
|
|
|
return "($fieldLeft | $fieldRight)";
|
2009-06-13 06:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-16 05:24:59 +00:00
|
|
|
/**
|
|
|
|
|
* Build a concatenation list to feed into a SQL query
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $stringList list of raw SQL expressions; caller is responsible for any quoting
|
2012-06-16 05:24:59 +00:00
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public function buildConcat( $stringList ) {
|
|
|
|
|
return 'CONCAT(' . implode( ',', $stringList ) . ')';
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function getDBname() {
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function getServer() {
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $name database table name
|
|
|
|
|
* @param string $format One of:
|
2011-09-06 20:44:03 +00:00
|
|
|
* 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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.
|
2012-11-05 19:15:40 +00:00
|
|
|
$dbDetails = explode( '.', $name, 2 );
|
|
|
|
|
if ( count( $dbDetails ) == 2 ) {
|
|
|
|
|
list( $database, $table ) = $dbDetails;
|
|
|
|
|
# We don't want any prefix added in this case
|
|
|
|
|
$prefix = '';
|
2010-08-25 17:45:00 +00:00
|
|
|
} else {
|
2011-07-04 15:00:30 +00:00
|
|
|
list( $table ) = $dbDetails;
|
2012-11-05 19:15:40 +00:00
|
|
|
if ( $wgSharedDB !== null # We have a shared database
|
|
|
|
|
&& !$this->isQuotedIdentifier( $table ) # Paranoia check to prevent shared tables listing '`table`'
|
|
|
|
|
&& in_array( $table, $wgSharedTables ) # A shared table is selected
|
|
|
|
|
) {
|
|
|
|
|
$database = $wgSharedDB;
|
2013-03-07 16:50:43 +00:00
|
|
|
$prefix = $wgSharedPrefix === null ? $this->mTablePrefix : $wgSharedPrefix;
|
2012-11-05 19:15:40 +00:00
|
|
|
} else {
|
|
|
|
|
$database = null;
|
|
|
|
|
$prefix = $this->mTablePrefix; # Default prefix
|
|
|
|
|
}
|
2011-07-25 20:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-05 19:15:40 +00:00
|
|
|
# Quote $table and apply the prefix if not quoted.
|
|
|
|
|
$tableName = "{$prefix}{$table}";
|
|
|
|
|
if ( $format == 'quoted' && !$this->isQuotedIdentifier( $tableName ) ) {
|
|
|
|
|
$tableName = $this->addIdentifierQuotes( $tableName );
|
2007-04-07 07:35:54 +00:00
|
|
|
}
|
2010-03-06 18:14:11 +00:00
|
|
|
|
2012-11-05 19:15:40 +00:00
|
|
|
# Quote $database and merge it with the table name if needed
|
|
|
|
|
if ( $database !== null ) {
|
2012-07-18 08:18:49 +00:00
|
|
|
if ( $format == 'quoted' && !$this->isQuotedIdentifier( $database ) ) {
|
|
|
|
|
$database = $this->addIdentifierQuotes( $database );
|
|
|
|
|
}
|
2012-11-05 19:15:40 +00:00
|
|
|
$tableName = $database . '.' . $tableName;
|
2010-08-25 17:45:00 +00:00
|
|
|
}
|
2011-06-17 15:59:55 +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:
|
2013-02-03 18:47:42 +00:00
|
|
|
* extract( $dbr->tableNames( 'user', 'watchlist' ) );
|
2008-03-30 09:48:15 +00:00
|
|
|
* $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:
|
2013-02-03 18:47:42 +00:00
|
|
|
* list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' );
|
2008-03-30 09:48:15 +00:00
|
|
|
* $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
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $name Table name, see tableName()
|
|
|
|
|
* @param string|bool $alias 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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-15 13:16:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get an aliased field name
|
|
|
|
|
* e.g. fieldName AS newFieldName
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $name Field name
|
|
|
|
|
* @param string|bool $alias Alias (optional)
|
2012-08-15 13:16:09 +00:00
|
|
|
* @return string SQL name for aliased field. Will not alias a field to its own name
|
|
|
|
|
*/
|
|
|
|
|
public function fieldNameWithAlias( $name, $alias = false ) {
|
|
|
|
|
if ( !$alias || (string)$alias === (string)$name ) {
|
|
|
|
|
return $name;
|
|
|
|
|
} else {
|
|
|
|
|
return $name . ' AS ' . $alias; //PostgreSQL needs AS
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets an array of aliased field names
|
|
|
|
|
*
|
|
|
|
|
* @param $fields array( [alias] => field )
|
|
|
|
|
* @return array of strings, see fieldNameWithAlias()
|
|
|
|
|
*/
|
|
|
|
|
public function fieldNamesWithAlias( $fields ) {
|
|
|
|
|
$retval = array();
|
|
|
|
|
foreach ( $fields as $alias => $field ) {
|
|
|
|
|
if ( is_numeric( $alias ) ) {
|
|
|
|
|
$alias = $field;
|
|
|
|
|
}
|
|
|
|
|
$retval[] = $this->fieldNameWithAlias( $field, $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
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $tables ( [alias] => table )
|
2011-08-06 00:24:18 +00:00
|
|
|
* @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
|
|
|
*/
|
2012-06-16 05:24:59 +00:00
|
|
|
protected function indexName( $index ) {
|
2009-01-19 13:56:08 +00:00
|
|
|
// Backwards-compatibility hack
|
2009-01-19 14:12:59 +00:00
|
|
|
$renamed = array(
|
2013-04-20 20:28:52 +00:00
|
|
|
'ar_usertext_timestamp' => 'usertext_timestamp',
|
|
|
|
|
'un_user_id' => 'user_id',
|
|
|
|
|
'un_user_ip' => 'user_ip',
|
2009-01-19 14:12:59 +00:00
|
|
|
);
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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 ) == '"';
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function buildLike() {
|
2009-10-21 19:53:03 +00:00
|
|
|
$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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function anyChar() {
|
2009-10-21 19:53:03 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function anyString() {
|
2009-10-21 19:53:03 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $table The table to replace the row(s) in.
|
|
|
|
|
* @param array $rows 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()
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $uniqueIndexes 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
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $fname Calling function name (use __METHOD__) for logs/profiling
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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 );
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-20 20:28:52 +00:00
|
|
|
foreach ( $rows as $row ) {
|
2011-06-23 03:14:11 +00:00
|
|
|
# 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
|
2013-03-23 11:18:40 +00:00
|
|
|
$this->insert( $table, $row, $fname );
|
2011-06-23 03:14:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE
|
|
|
|
|
* statement.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $table Table name
|
|
|
|
|
* @param array $rows Rows to insert
|
|
|
|
|
* @param string $fname 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
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws DBUnexpectedError
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
|
2011-07-01 02:57:31 +00:00
|
|
|
$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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function textFieldSize( $table, $field ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $table Table name
|
|
|
|
|
* @param string|array $conds of conditions. See $conds in DatabaseBase::select() for
|
2011-06-20 06:52:44 +00:00
|
|
|
* the format. Use $conds == "*" to delete all rows
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $fname name of the calling function
|
2005-07-18 02:30:04 +00:00
|
|
|
*
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws DBUnexpectedError
|
2013-01-18 14:40:05 +00:00
|
|
|
* @return bool|ResultWrapper
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $destTable The table name to insert into
|
|
|
|
|
* @param string|array $srcTable 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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $varMap 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()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $conds 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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $fname The function name of the caller, from __METHOD__
|
2011-06-20 06:52:44 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $insertOptions Options for the INSERT part of the query, see
|
2011-06-20 06:52:44 +00:00
|
|
|
* DatabaseBase::insert() for details.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $selectOptions 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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 ) ) {
|
2013-02-03 18:47:42 +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.
|
|
|
|
|
*
|
2012-06-16 05:24:59 +00:00
|
|
|
* $sql is expected to be a SELECT, if that makes a difference.
|
2009-06-16 21:00:38 +00:00
|
|
|
*
|
|
|
|
|
* The version provided by default works in MySQL and SQLite. It will very
|
|
|
|
|
* likely need to be overridden for most other DBMSes.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $sql SQL query we will append the limit too
|
2011-08-06 00:24:18 +00:00
|
|
|
* @param $limit Integer the SQL limit
|
2012-02-09 18:01:54 +00:00
|
|
|
* @param $offset Integer|bool the SQL offset (default false)
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws DBUnexpectedError
|
2011-05-25 18:58:02 +00:00
|
|
|
* @return string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function limitResult( $sql, $limit, $offset = false ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
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
|
|
|
}
|
2008-05-08 15:50:53 +00:00
|
|
|
return "$sql LIMIT "
|
2012-06-16 05:24:59 +00:00
|
|
|
. ( ( is_numeric( $offset ) && $offset != 0 ) ? "{$offset}," : "" )
|
|
|
|
|
. "{$limit} ";
|
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
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function unionSupportsOrderAndLimit() {
|
2009-10-17 12:23:23 +00:00
|
|
|
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.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $sqls SQL statements to combine
|
2009-05-19 16:58:56 +00:00
|
|
|
* @param $all Boolean: use UNION ALL
|
|
|
|
|
* @return String: SQL fragment
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string|array $cond SQL expression which will result in a boolean value
|
|
|
|
|
* @param string $trueVal SQL expression to return if true
|
|
|
|
|
* @param string $falseVal SQL expression to return if false
|
2008-11-29 18:50:39 +00:00
|
|
|
* @return String: SQL fragment
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function conditional( $cond, $trueVal, $falseVal ) {
|
2012-08-25 18:24:59 +00:00
|
|
|
if ( is_array( $cond ) ) {
|
|
|
|
|
$cond = $this->makeList( $cond, LIST_AND );
|
|
|
|
|
}
|
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
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $orig column to modify
|
|
|
|
|
* @param string $old column to seek
|
|
|
|
|
* @param string $new column to replace with
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2008-05-03 13:09:34 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function strreplace( $orig, $old, $new ) {
|
2008-05-03 13:09:34 +00:00
|
|
|
return "REPLACE({$orig}, {$old}, {$new})";
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-24 19:55:38 +00:00
|
|
|
/**
|
|
|
|
|
* Determines how long the server has been up
|
|
|
|
|
* STUB
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function getServerUptime() {
|
2011-11-24 19:55:38 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function wasLockTimeout() {
|
2011-11-23 23:45:46 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function wasReadOnlyError() {
|
* 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;
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function deadlockLoop() {
|
2012-01-18 12:10:16 +00:00
|
|
|
$this->begin( __METHOD__ );
|
2008-03-30 09:48:15 +00:00
|
|
|
$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 ) {
|
2012-01-06 18:30:11 +00:00
|
|
|
$this->rollback( __METHOD__ );
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->reportQueryError( $error, $errno, $sql, $fname );
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2012-01-06 18:30:11 +00:00
|
|
|
$this->commit( __METHOD__ );
|
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
|
|
|
*
|
2012-02-09 19:30:01 +00:00
|
|
|
* @return 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function masterPosWait( DBMasterPos $pos, $timeout ) {
|
2012-01-06 18:30:11 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
if ( !is_null( $this->mFakeSlaveLag ) ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
$wait = intval( ( $pos->pos - microtime( true ) + $this->mFakeSlaveLag ) * 1e6 );
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $wait > $timeout * 1e6 ) {
|
|
|
|
|
wfDebug( "Fake slave timed out waiting for $pos ($wait us)\n" );
|
2012-01-06 18:30:11 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2008-03-30 09:48:15 +00:00
|
|
|
return -1;
|
|
|
|
|
} elseif ( $wait > 0 ) {
|
|
|
|
|
wfDebug( "Fake slave waiting $wait us\n" );
|
|
|
|
|
usleep( $wait );
|
2012-01-06 18:30:11 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2008-03-30 09:48:15 +00:00
|
|
|
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" );
|
2012-01-06 18:30:11 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
|
2012-01-06 18:30:11 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function getSlavePos() {
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function getMasterPos() {
|
2008-03-30 09:48:15 +00:00
|
|
|
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
|
|
|
|
2012-08-29 19:32:47 +00:00
|
|
|
/**
|
|
|
|
|
* Run an anonymous function as soon as there is no transaction pending.
|
|
|
|
|
* If there is a transaction and it is rolled back, then the callback is cancelled.
|
2013-04-25 20:32:55 +00:00
|
|
|
* Queries in the function will run in AUTO-COMMIT mode unless there are begin() calls.
|
2012-08-29 19:32:47 +00:00
|
|
|
* Callbacks must commit any transactions that they begin.
|
|
|
|
|
*
|
2013-04-16 07:28:45 +00:00
|
|
|
* This is useful for updates to different systems or when separate transactions are needed.
|
|
|
|
|
* For example, one might want to enqueue jobs into a system outside the database, but only
|
|
|
|
|
* after the database is updated so that the jobs will see the data when they actually run.
|
2013-04-11 06:54:14 +00:00
|
|
|
* It can also be used for updates that easily cause deadlocks if locks are held too long.
|
2012-08-29 19:32:47 +00:00
|
|
|
*
|
2013-04-11 06:54:14 +00:00
|
|
|
* @param Closure $callback
|
2012-10-03 16:56:31 +00:00
|
|
|
* @since 1.20
|
2013-04-11 06:54:14 +00:00
|
|
|
*/
|
|
|
|
|
final public function onTransactionIdle( Closure $callback ) {
|
|
|
|
|
$this->mTrxIdleCallbacks[] = $callback;
|
|
|
|
|
if ( !$this->mTrxLevel ) {
|
|
|
|
|
$this->runOnTransactionIdleCallbacks();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run an anonymous function before the current transaction commits or now if there is none.
|
|
|
|
|
* If there is a transaction and it is rolled back, then the callback is cancelled.
|
|
|
|
|
* Callbacks must not start nor commit any transactions.
|
|
|
|
|
*
|
|
|
|
|
* This is useful for updates that easily cause deadlocks if locks are held too long
|
2013-04-16 07:28:45 +00:00
|
|
|
* but where atomicity is strongly desired for these updates and some related updates.
|
2012-10-03 16:56:31 +00:00
|
|
|
*
|
2012-08-29 19:32:47 +00:00
|
|
|
* @param Closure $callback
|
2013-04-11 06:54:14 +00:00
|
|
|
* @since 1.22
|
2012-08-29 19:32:47 +00:00
|
|
|
*/
|
2013-04-11 06:54:14 +00:00
|
|
|
final public function onTransactionPreCommitOrIdle( Closure $callback ) {
|
2012-08-29 19:32:47 +00:00
|
|
|
if ( $this->mTrxLevel ) {
|
2013-04-11 06:54:14 +00:00
|
|
|
$this->mTrxPreCommitCallbacks[] = $callback;
|
2012-08-29 19:32:47 +00:00
|
|
|
} else {
|
2013-04-11 06:54:14 +00:00
|
|
|
$this->onTransactionIdle( $callback ); // this will trigger immediately
|
2012-08-29 19:32:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-11 06:54:14 +00:00
|
|
|
* Actually any "on transaction idle" callbacks.
|
2012-10-03 16:56:31 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.20
|
2012-08-29 19:32:47 +00:00
|
|
|
*/
|
|
|
|
|
protected function runOnTransactionIdleCallbacks() {
|
2012-11-14 19:40:36 +00:00
|
|
|
$autoTrx = $this->getFlag( DBO_TRX ); // automatic begin() enabled?
|
|
|
|
|
|
2012-08-29 19:32:47 +00:00
|
|
|
$e = null; // last exception
|
|
|
|
|
do { // callbacks may add callbacks :)
|
2012-11-14 19:40:36 +00:00
|
|
|
$callbacks = $this->mTrxIdleCallbacks;
|
|
|
|
|
$this->mTrxIdleCallbacks = array(); // recursion guard
|
2012-08-29 19:32:47 +00:00
|
|
|
foreach ( $callbacks as $callback ) {
|
|
|
|
|
try {
|
2012-11-14 19:40:36 +00:00
|
|
|
$this->clearFlag( DBO_TRX ); // make each query its own transaction
|
2012-08-29 19:32:47 +00:00
|
|
|
$callback();
|
2012-11-14 19:40:36 +00:00
|
|
|
$this->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore automatic begin()
|
2013-04-11 05:29:05 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
}
|
2012-08-29 19:32:47 +00:00
|
|
|
}
|
2012-11-14 19:40:36 +00:00
|
|
|
} while ( count( $this->mTrxIdleCallbacks ) );
|
2012-08-29 19:32:47 +00:00
|
|
|
|
|
|
|
|
if ( $e instanceof Exception ) {
|
|
|
|
|
throw $e; // re-throw any last exception
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-11 06:54:14 +00:00
|
|
|
/**
|
|
|
|
|
* Actually any "on transaction pre-commit" callbacks.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
protected function runOnTransactionPreCommitCallbacks() {
|
|
|
|
|
$e = null; // last exception
|
|
|
|
|
do { // callbacks may add callbacks :)
|
|
|
|
|
$callbacks = $this->mTrxPreCommitCallbacks;
|
|
|
|
|
$this->mTrxPreCommitCallbacks = array(); // recursion guard
|
|
|
|
|
foreach ( $callbacks as $callback ) {
|
|
|
|
|
try {
|
|
|
|
|
$callback();
|
|
|
|
|
} catch ( Exception $e ) {}
|
|
|
|
|
}
|
|
|
|
|
} while ( count( $this->mTrxPreCommitCallbacks ) );
|
|
|
|
|
|
|
|
|
|
if ( $e instanceof Exception ) {
|
|
|
|
|
throw $e; // re-throw any last exception
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-10-02 16:10:39 +00:00
|
|
|
/**
|
2012-09-26 10:38:44 +00:00
|
|
|
* Begin a transaction. If a transaction is already in progress, that transaction will be committed before the
|
|
|
|
|
* new transaction is started.
|
|
|
|
|
*
|
|
|
|
|
* Note that when the DBO_TRX flag is set (which is usually the case for web requests, but not for maintenance scripts),
|
|
|
|
|
* any previous database query will have started a transaction automatically.
|
|
|
|
|
*
|
2012-10-05 08:19:42 +00:00
|
|
|
* Nesting of transactions is not supported. Attempts to nest transactions will cause a warning, unless the current
|
|
|
|
|
* transaction was started automatically because of the DBO_TRX flag.
|
2011-08-11 23:58:29 +00:00
|
|
|
*
|
|
|
|
|
* @param $fname string
|
2005-10-02 16:10:39 +00:00
|
|
|
*/
|
2012-08-29 19:32:47 +00:00
|
|
|
final public function begin( $fname = 'DatabaseBase::begin' ) {
|
2012-09-26 10:38:44 +00:00
|
|
|
global $wgDebugDBTransactions;
|
|
|
|
|
|
|
|
|
|
if ( $this->mTrxLevel ) { // implicit commit
|
2012-10-05 08:19:42 +00:00
|
|
|
if ( !$this->mTrxAutomatic ) {
|
2013-02-11 21:53:40 +00:00
|
|
|
// We want to warn about inadvertently nested begin/commit pairs, but not about
|
|
|
|
|
// auto-committing implicit transactions that were started by query() via DBO_TRX
|
|
|
|
|
$msg = "$fname: Transaction already in progress (from {$this->mTrxFname}), " .
|
|
|
|
|
" performing implicit commit!";
|
|
|
|
|
wfWarn( $msg );
|
|
|
|
|
wfLogDBError( $msg );
|
2012-10-05 08:19:42 +00:00
|
|
|
} else {
|
|
|
|
|
// if the transaction was automatic and has done write operations,
|
|
|
|
|
// log it if $wgDebugDBTransactions is enabled.
|
|
|
|
|
if ( $this->mTrxDoneWrites && $wgDebugDBTransactions ) {
|
2013-02-11 21:53:40 +00:00
|
|
|
wfDebug( "$fname: Automatic transaction with writes in progress" .
|
2013-04-11 05:29:05 +00:00
|
|
|
" (from {$this->mTrxFname}), performing implicit commit!\n"
|
|
|
|
|
);
|
2012-10-05 08:19:42 +00:00
|
|
|
}
|
2012-09-26 10:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-11 06:54:14 +00:00
|
|
|
$this->runOnTransactionPreCommitCallbacks();
|
2012-08-29 19:32:47 +00:00
|
|
|
$this->doCommit( $fname );
|
|
|
|
|
$this->runOnTransactionIdleCallbacks();
|
|
|
|
|
}
|
2012-09-26 10:38:44 +00:00
|
|
|
|
2012-08-29 19:32:47 +00:00
|
|
|
$this->doBegin( $fname );
|
2012-08-27 09:55:15 +00:00
|
|
|
$this->mTrxFname = $fname;
|
2012-09-22 08:14:35 +00:00
|
|
|
$this->mTrxDoneWrites = false;
|
2012-10-05 08:19:42 +00:00
|
|
|
$this->mTrxAutomatic = false;
|
2012-08-29 19:32:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-09-26 10:38:44 +00:00
|
|
|
* Issues the BEGIN command to the database server.
|
|
|
|
|
*
|
2012-08-29 19:32:47 +00:00
|
|
|
* @see DatabaseBase::begin()
|
|
|
|
|
* @param type $fname
|
|
|
|
|
*/
|
|
|
|
|
protected function doBegin( $fname ) {
|
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
|
|
|
/**
|
2012-09-26 10:38:44 +00:00
|
|
|
* Commits a transaction previously started using begin().
|
|
|
|
|
* If no transaction is in progress, a warning is issued.
|
|
|
|
|
*
|
|
|
|
|
* Nesting of transactions is not supported.
|
2011-08-06 00:24:18 +00:00
|
|
|
*
|
|
|
|
|
* @param $fname string
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $flush Flush flag, set to 'flush' to disable warnings about explicitly committing implicit
|
2012-10-09 13:34:47 +00:00
|
|
|
* transactions, or calling commit when no transaction is in progress.
|
|
|
|
|
* This will silently break any ongoing explicit transaction. Only set the flush flag if you are sure
|
|
|
|
|
* that it is safe to ignore these warnings in your context.
|
2012-10-09 10:26:47 +00:00
|
|
|
*/
|
2012-10-09 13:34:47 +00:00
|
|
|
final public function commit( $fname = 'DatabaseBase::commit', $flush = '' ) {
|
|
|
|
|
if ( $flush != 'flush' ) {
|
2012-10-09 10:26:47 +00:00
|
|
|
if ( !$this->mTrxLevel ) {
|
|
|
|
|
wfWarn( "$fname: No transaction to commit, something got out of sync!" );
|
2013-04-20 20:28:52 +00:00
|
|
|
} elseif ( $this->mTrxAutomatic ) {
|
2012-10-09 10:26:47 +00:00
|
|
|
wfWarn( "$fname: Explicit commit of implicit transaction. Something may be out of sync!" );
|
|
|
|
|
}
|
2012-10-16 11:15:50 +00:00
|
|
|
} else {
|
|
|
|
|
if ( !$this->mTrxLevel ) {
|
|
|
|
|
return; // nothing to do
|
2013-04-20 20:28:52 +00:00
|
|
|
} elseif ( !$this->mTrxAutomatic ) {
|
2012-10-16 11:15:50 +00:00
|
|
|
wfWarn( "$fname: Flushing an explicit transaction, getting out of sync!" );
|
|
|
|
|
}
|
2012-08-27 09:55:15 +00:00
|
|
|
}
|
2012-10-05 08:19:42 +00:00
|
|
|
|
2013-04-11 06:54:14 +00:00
|
|
|
$this->runOnTransactionPreCommitCallbacks();
|
2012-08-29 19:32:47 +00:00
|
|
|
$this->doCommit( $fname );
|
|
|
|
|
$this->runOnTransactionIdleCallbacks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-09-26 10:38:44 +00:00
|
|
|
* Issues the COMMIT command to the database server.
|
|
|
|
|
*
|
2012-08-29 19:32:47 +00:00
|
|
|
* @see DatabaseBase::commit()
|
|
|
|
|
* @param type $fname
|
|
|
|
|
*/
|
|
|
|
|
protected function doCommit( $fname ) {
|
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
|
|
|
/**
|
2012-09-26 10:38:44 +00:00
|
|
|
* Rollback a transaction previously started using begin().
|
|
|
|
|
* If no transaction is in progress, a warning is issued.
|
|
|
|
|
*
|
2008-03-30 09:48:15 +00:00
|
|
|
* 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
|
|
|
*/
|
2012-08-29 19:32:47 +00:00
|
|
|
final public function rollback( $fname = 'DatabaseBase::rollback' ) {
|
2012-08-27 09:55:15 +00:00
|
|
|
if ( !$this->mTrxLevel ) {
|
|
|
|
|
wfWarn( "$fname: No transaction to rollback, something got out of sync!" );
|
|
|
|
|
}
|
2012-08-29 19:32:47 +00:00
|
|
|
$this->doRollback( $fname );
|
2012-11-14 19:40:36 +00:00
|
|
|
$this->mTrxIdleCallbacks = array(); // cancel
|
2013-04-11 06:54:14 +00:00
|
|
|
$this->mTrxPreCommitCallbacks = array(); // cancel
|
2012-08-29 19:32:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-09-26 10:38:44 +00:00
|
|
|
* Issues the ROLLBACK command to the database server.
|
|
|
|
|
*
|
2012-08-29 19:32:47 +00:00
|
|
|
* @see DatabaseBase::rollback()
|
|
|
|
|
* @param type $fname
|
|
|
|
|
*/
|
|
|
|
|
protected function doRollback( $fname ) {
|
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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $oldName name of table whose structure should be copied
|
|
|
|
|
* @param string $newName name of table to be created
|
2009-11-06 10:17:44 +00:00
|
|
|
* @param $temporary Boolean: whether the new table should be temporary
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $fname calling function name
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2009-11-06 10:17:44 +00:00
|
|
|
* @return Boolean: true if operation was successful
|
|
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function duplicateTableStructure( $oldName, $newName, $temporary = false,
|
2013-04-11 05:29:05 +00:00
|
|
|
$fname = 'DatabaseBase::duplicateTableStructure'
|
|
|
|
|
) {
|
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
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $prefix Only show tables with this prefix, e.g. mw_
|
|
|
|
|
* @param string $fname calling function name
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2010-12-28 17:15:50 +00:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function timestamp( $ts = 0 ) {
|
2010-09-04 13:48:16 +00:00
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
|
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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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.
|
|
|
|
|
*
|
2012-02-09 18:01:54 +00:00
|
|
|
* @return int Database replication lag in seconds
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public 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
|
|
|
|
2011-11-23 19:25:59 +00:00
|
|
|
/**
|
|
|
|
|
* 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
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-11-23 19:25:59 +00:00
|
|
|
*/
|
2013-04-11 05:29:05 +00:00
|
|
|
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).
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $filename File name to open
|
2012-10-07 23:35:26 +00:00
|
|
|
* @param bool|callable $lineCallback Optional function called before reading each line
|
|
|
|
|
* @param bool|callable $resultCallback Optional function called for each MySQL result
|
|
|
|
|
* @param bool|string $fname Calling function name or false if name should be
|
2011-06-20 06:52:44 +00:00
|
|
|
* generated dynamically using $filename
|
2012-11-22 03:53:24 +00:00
|
|
|
* @param bool|callable $inputCallback Callback: Optional function called for each complete line sent
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2012-12-09 03:09:48 +00:00
|
|
|
* @throws Exception|MWException
|
2011-11-29 21:04:20 +00:00
|
|
|
* @return bool|string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function sourceFile(
|
2012-11-22 03:53:24 +00:00
|
|
|
$filename, $lineCallback = false, $resultCallback = false, $fname = false, $inputCallback = false
|
2012-07-16 17:56:00 +00:00
|
|
|
) {
|
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 {
|
2012-11-22 03:53:24 +00:00
|
|
|
$error = $this->sourceStream( $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
|
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
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $patch The name of the patch, like patch-something.sql
|
2009-08-09 15:22:34 +00:00
|
|
|
* @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.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param bool|array $vars mapping variable name to value.
|
* 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
|
|
|
*/
|
2012-07-16 17:56:00 +00:00
|
|
|
public function setSchemaVars( $vars ) {
|
* 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
|
|
|
$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
|
2012-11-22 03:53:24 +00:00
|
|
|
* @param $lineCallback Callback: Optional function called before reading each query
|
2008-11-29 18:50:39 +00:00
|
|
|
* @param $resultCallback Callback: Optional function called for each MySQL result
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $fname Calling function name
|
2012-11-22 03:53:24 +00:00
|
|
|
* @param $inputCallback Callback: Optional function called for each complete query sent
|
2011-11-29 21:04:20 +00:00
|
|
|
* @return bool|string
|
2004-09-03 16:36:46 +00:00
|
|
|
*/
|
2012-01-12 22:57:51 +00:00
|
|
|
public function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
|
2012-01-12 22:10:24 +00:00
|
|
|
$fname = 'DatabaseBase::sourceStream', $inputCallback = false )
|
2011-06-20 06:52:44 +00:00
|
|
|
{
|
2012-01-11 20:19:55 +00:00
|
|
|
$cmd = '';
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2012-01-11 20:19:55 +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
|
|
|
|
2012-01-11 20:19:55 +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
|
|
|
|
2012-01-11 20:19:55 +00:00
|
|
|
$done = $this->streamStatementEnd( $cmd, $line );
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
$cmd .= "$line\n";
|
|
|
|
|
|
2012-01-11 20:19:55 +00:00
|
|
|
if ( $done || feof( $fp ) ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
$cmd = $this->replaceVars( $cmd );
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2012-11-22 03:53:24 +00:00
|
|
|
if ( ( $inputCallback && call_user_func( $inputCallback, $cmd ) ) || !$inputCallback ) {
|
|
|
|
|
$res = $this->query( $cmd, $fname );
|
2008-03-30 09:48:15 +00:00
|
|
|
|
2012-11-22 03:53:24 +00:00
|
|
|
if ( $resultCallback ) {
|
|
|
|
|
call_user_func( $resultCallback, $res, $this );
|
|
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
|
2012-11-22 03:53:24 +00:00
|
|
|
if ( false === $res ) {
|
|
|
|
|
$err = $this->lastError();
|
|
|
|
|
return "Query \"{$cmd}\" failed with error code \"$err\".\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
$cmd = '';
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 13:48:16 +00:00
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-11 20:19:55 +00:00
|
|
|
/**
|
|
|
|
|
* Called by sourceStream() to check if we've reached a statement end
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $sql SQL assembled so far
|
|
|
|
|
* @param string $newLine New line about to be added to $sql
|
2012-01-12 22:57:51 +00:00
|
|
|
* @return Bool Whether $newLine contains end of the statement
|
2012-01-11 20:19:55 +00:00
|
|
|
*/
|
2012-01-12 22:57:51 +00:00
|
|
|
public function streamStatementEnd( &$sql, &$newLine ) {
|
2012-01-11 20:19:55 +00:00
|
|
|
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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $ins SQL statement to replace variables in
|
2010-12-04 09:27:02 +00:00
|
|
|
* @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*/
|
2013-02-03 18:47:42 +00:00
|
|
|
$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] );
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-10 22:42:59 +00:00
|
|
|
/**
|
|
|
|
|
* Check to see if a named lock is available. This is non-blocking.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $lockName name of lock to poll
|
|
|
|
|
* @param string $method name of method calling us
|
2012-05-10 22:42:59 +00:00
|
|
|
* @return Boolean
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
public function lockIsFree( $lockName, $method ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $lockName name of lock to aquire
|
|
|
|
|
* @param string $method name of method calling us
|
2010-07-04 14:41:26 +00:00
|
|
|
* @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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $lockName Name of lock to release
|
|
|
|
|
* @param string $method Name of method calling us
|
2009-06-25 00:40:12 +00:00
|
|
|
*
|
2012-02-09 19:30:01 +00:00
|
|
|
* @return int 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
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $read of tables to lock for read access
|
|
|
|
|
* @param array $write of tables to lock for write access
|
|
|
|
|
* @param string $method name of caller
|
|
|
|
|
* @param bool $lowPriority 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
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $method 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' ) {
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( !$this->tableExists( $tableName, $fName ) ) {
|
2010-12-28 01:19:16 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$sql = "DROP TABLE " . $this->tableName( $tableName );
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( $this->cascadingDeletes() ) {
|
2010-12-28 01:19:16 +00:00
|
|
|
$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
|
|
|
/**
|
2012-03-03 11:49:02 +00:00
|
|
|
* Encode an expiry time into the DBMS dependent format
|
2011-03-18 19:15:56 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $expiry timestamp for expiry, or the 'infinity' string
|
2011-03-18 19:15:56 +00:00
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public function encodeExpiry( $expiry ) {
|
2012-03-03 11:49:02 +00:00
|
|
|
return ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() )
|
|
|
|
|
? $this->getInfinity()
|
|
|
|
|
: $this->timestamp( $expiry );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Decode an expiry time into a DBMS independent format
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $expiry DB timestamp field value for expiry
|
2012-03-03 11:49:02 +00:00
|
|
|
* @param $format integer: TS_* constant, defaults to TS_MW
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public function decodeExpiry( $expiry, $format = TS_MW ) {
|
|
|
|
|
return ( $expiry == '' || $expiry == $this->getInfinity() )
|
|
|
|
|
? 'infinity'
|
|
|
|
|
: wfTimestamp( $format, $expiry );
|
2011-03-18 19:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2012-03-30 00:10:52 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.19
|
|
|
|
|
*/
|
|
|
|
|
public function __toString() {
|
|
|
|
|
return (string)$this->mConn;
|
|
|
|
|
}
|
2012-11-14 19:40:36 +00:00
|
|
|
|
|
|
|
|
public function __destruct() {
|
2013-04-11 06:54:14 +00:00
|
|
|
if ( count( $this->mTrxIdleCallbacks ) || count( $this->mTrxPreCommitCallbacks ) ) {
|
|
|
|
|
trigger_error( "Transaction idle or pre-commit callbacks still pending." ); // sanity
|
2012-11-14 19:40:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|