X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/74c929b24b048c9f1e31e17db757ae4195cd7673..dc9cc5d707f5a612938cc9371614cc41c328fda2:/includes/db/Database.php 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 = <<$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('