From 72e90545454c0e014318fa3c81658e035aac58c1 Mon Sep 17 00:00:00 2001
From: Pierre Schmitz
Date: Wed, 10 Jun 2009 13:00:47 +0200
Subject: applying patch to version 1.15.0
---
includes/db/Database.php | 248 ++++--
includes/db/DatabaseIbm_db2.php | 1796 ++++++++++++++++++++++++++++++++++++++
includes/db/DatabasePostgres.php | 65 +-
includes/db/DatabaseSqlite.php | 238 +++--
includes/db/LBFactory.php | 26 +-
includes/db/LoadBalancer.php | 2 +-
6 files changed, 2200 insertions(+), 175 deletions(-)
create mode 100644 includes/db/DatabaseIbm_db2.php
(limited to 'includes/db')
diff --git a/includes/db/Database.php b/includes/db/Database.php
index 84b88643..52a59c11 100644
--- a/includes/db/Database.php
+++ b/includes/db/Database.php
@@ -26,6 +26,7 @@ class Database {
#------------------------------------------------------------------------------
protected $mLastQuery = '';
+ protected $mDoneWrites = false;
protected $mPHPError = false;
protected $mServer, $mUser, $mPassword, $mConn = null, $mDBname;
@@ -210,7 +211,14 @@ class Database {
* @return String
*/
function lastQuery() { return $this->mLastQuery; }
-
+
+
+ /**
+ * Returns true if the connection may have been used for write queries.
+ * Should return true if unsure.
+ */
+ function doneWrites() { return $this->mDoneWrites; }
+
/**
* Is a connection to the database open?
* @return Boolean
@@ -492,6 +500,14 @@ class Database {
}
}
+ /**
+ * Determine whether a query writes to the DB.
+ * Should return true if unsure.
+ */
+ function isWriteQuery( $sql ) {
+ return !preg_match( '/^(?:SELECT|BEGIN|COMMIT|SET|SHOW)\b/i', $sql );
+ }
+
/**
* Usually aborts on failure. If errors are explicitly ignored, returns success.
*
@@ -527,6 +543,11 @@ class Database {
}
$this->mLastQuery = $sql;
+ if ( !$this->mDoneWrites && $this->isWriteQuery( $sql ) ) {
+ // Set a flag indicating that writes have been done
+ wfDebug( __METHOD__.": Writes done: $sql\n" );
+ $this->mDoneWrites = true;
+ }
# Add a comment for easy SHOW PROCESSLIST interpretation
#if ( $fname ) {
@@ -566,11 +587,15 @@ class Database {
}
}
+ if ( istainted( $sql ) & TC_MYSQL ) {
+ throw new MWException( 'Tainted query found' );
+ }
+
# Do the query and handle errors
$ret = $this->doQuery( $commentedSql );
# Try reconnecting if the connection was lost
- if ( false === $ret && ( $this->lastErrno() == 2013 || $this->lastErrno() == 2006 ) ) {
+ if ( false === $ret && $this->wasErrorReissuable() ) {
# Transaction is gone, like it or not
$this->mTrxLevel = 0;
wfDebug( "Connection lost, reconnecting...\n" );
@@ -1191,6 +1216,7 @@ class Database {
# SHOW INDEX should work for 3.x and up:
# http://dev.mysql.com/doc/mysql/en/SHOW_INDEX.html
$table = $this->tableName( $table );
+ $index = $this->indexName( $index );
$sql = 'SHOW INDEX FROM '.$table;
$res = $this->query( $sql, $fname );
if ( !$res ) {
@@ -1396,7 +1422,7 @@ class Database {
} else {
$list .= $field." IN (".$this->makeList($value).") ";
}
- } elseif( is_null($value) ) {
+ } elseif( $value === null ) {
if ( $mode == LIST_AND || $mode == LIST_OR ) {
$list .= "$field IS ";
} elseif ( $mode == LIST_SET ) {
@@ -1573,6 +1599,23 @@ class Database {
return implode(' ',array($straightJoins,$otherJoins) );
}
+ /**
+ * Get the name of an index in a given table
+ */
+ function indexName( $index ) {
+ // Backwards-compatibility hack
+ $renamed = array(
+ 'ar_usertext_timestamp' => 'usertext_timestamp',
+ 'un_user_id' => 'user_id',
+ 'un_user_ip' => 'user_ip',
+ );
+ if( isset( $renamed[$index] ) ) {
+ return $renamed[$index];
+ } else {
+ return $index;
+ }
+ }
+
/**
* Wrapper for addslashes()
* @param $s String: to be slashed.
@@ -1587,7 +1630,7 @@ class Database {
* Otherwise returns as-is
*/
function addQuotes( $s ) {
- if ( is_null( $s ) ) {
+ if ( $s === null ) {
return 'NULL';
} else {
# This will also quote numeric values. This should be harmless,
@@ -1602,6 +1645,7 @@ class Database {
* Escape string for safe LIKE usage
*/
function escapeLike( $s ) {
+ $s=str_replace('\\','\\\\',$s);
$s=$this->strencode( $s );
$s=str_replace(array('%','_'),array('\%','\_'),$s);
return $s;
@@ -1621,7 +1665,7 @@ class Database {
* PostgreSQL doesn't have them and returns ""
*/
function useIndexClause( $index ) {
- return "FORCE INDEX ($index)";
+ return "FORCE INDEX (" . $this->indexName( $index ) . ")";
}
/**
@@ -1816,6 +1860,14 @@ class Database {
return $this->lastErrno() == 1213;
}
+ /**
+ * Determines if the last query error was something that should be dealt
+ * with by pinging the connection and reissuing the query
+ */
+ function wasErrorReissuable() {
+ return $this->lastErrno() == 2013 || $this->lastErrno() == 2006;
+ }
+
/**
* Perform a deadlock-prone transaction.
*
@@ -2250,8 +2302,12 @@ class Database {
}
// Table prefixes
- $ins = preg_replace_callback( '/\/\*(?:\$wgDBprefix|_)\*\/([a-zA-Z_0-9]*)/',
- array( &$this, 'tableNameCallback' ), $ins );
+ $ins = preg_replace_callback( '!/\*(?:\$wgDBprefix|_)\*/([a-zA-Z_0-9]*)!',
+ array( $this, 'tableNameCallback' ), $ins );
+
+ // Index names
+ $ins = preg_replace_callback( '!/\*i\*/([a-zA-Z_0-9]*)!',
+ array( $this, 'indexNameCallback' ), $ins );
return $ins;
}
@@ -2263,6 +2319,13 @@ class Database {
return $this->tableName( $matches[1] );
}
+ /**
+ * Index name callback
+ */
+ protected function indexNameCallback( $matches ) {
+ return $this->indexName( $matches[1] );
+ }
+
/*
* Build a concatenation list to feed into a SQL query
*/
@@ -2480,44 +2543,27 @@ class DBConnectionError extends DBError {
}
function getPageTitle() {
- global $wgSitename;
- return "$wgSitename has a problem";
+ global $wgSitename, $wgLang;
+ $header = "$wgSitename has a problem";
+ if ( $wgLang instanceof Language ) {
+ $header = htmlspecialchars( $wgLang->getMessage( 'dberr-header' ) );
+ }
+
+ return $header;
}
function getHTML() {
- global $wgTitle, $wgUseFileCache, $title, $wgInputEncoding;
- global $wgSitename, $wgServer, $wgMessageCache;
-
- # I give up, Brion is right. Getting the message cache to work when there is no DB is tricky.
- # Hard coding strings instead.
+ global $wgLang, $wgMessageCache, $wgUseFileCache;
- $noconnect = "Sorry! This site is experiencing technical difficulties.
Try waiting a few minutes and reloading.
(Can't contact the database server: $1)
";
- $mainpage = 'Main Page';
- $searchdisabled = <<$wgSitename search is disabled for performance reasons. You can search via Google in the meantime.
-Note that their indexes of $wgSitename content may be out of date.
',
-EOT;
+ $sorry = 'Sorry! This site is experiencing technical difficulties.';
+ $again = 'Try waiting a few minutes and reloading.';
+ $info = '(Can\'t contact the database server: $1)';
- $googlesearch = "
-
-
-";
- $cachederror = "The following is a cached copy of the requested page, and may not be up to date. ";
+ if ( $wgLang instanceof Language ) {
+ $sorry = htmlspecialchars( $wgLang->getMessage( 'dberr-problems' ) );
+ $again = htmlspecialchars( $wgLang->getMessage( 'dberr-again' ) );
+ $info = htmlspecialchars( $wgLang->getMessage( 'dberr-info' ) );
+ }
# No database access
if ( is_object( $wgMessageCache ) ) {
@@ -2528,6 +2574,7 @@ border=\"0\" ALT=\"Google\">
$this->error = $this->db->getProperty('mServer');
}
+ $noconnect = "$sorry
$again
$info
";
$text = str_replace( '$1', $this->error, $noconnect );
/*
@@ -2537,38 +2584,95 @@ border=\"0\" ALT=\"Google\">
"\n";
}*/
- if($wgUseFileCache) {
- if($wgTitle) {
- $t =& $wgTitle;
- } else {
- if($title) {
- $t = Title::newFromURL( $title );
- } elseif (@/**/$_REQUEST['search']) {
- $search = $_REQUEST['search'];
- return $searchdisabled .
- str_replace( array( '$1', '$2' ), array( htmlspecialchars( $search ),
- $wgInputEncoding ), $googlesearch );
- } else {
- $t = Title::newFromText( $mainpage );
+ $extra = $this->searchForm();
+
+ if( $wgUseFileCache ) {
+ $cache = $this->fileCachedPage();
+ # Cached version on file system?
+ if( $cache !== null ) {
+ # Hack: extend the body for error messages
+ $cache = str_replace( array('