Jump to content

Wikia code/includes/db/Database.php

From mediawiki.org
--- D:\Programming\SVN\mediawiki\branches\REL1_16\phase3\includes\db\Database.php	2011-07-18 22:30:57.687500000 +0100
+++ D:\Programming\SVN\wikia\trunk\includes\db\Database.php	2011-08-17 15:28:13.507812500 +0100
@@ -483,7 +483,14 @@
 			} else {
 				$userName = '';
 			}
-			$commentedSql = preg_replace('/\s/', " /* $fname $userName */ ", $sql, 1);
+
+			/* Wikia change begin - @author: Marooned */
+			/* add URL to log, DO NOT use any wgObject here! */
+			// back to whiteboard, use wfGetCurrentUrl()
+			$url = "";
+
+			$commentedSql = preg_replace('/\s/', " /* $fname $userName URL:$url */ ", $sql, 1);
+			/* Wikia change end */
 		#} else {
 		#	$commentedSql = $sql;
 		#}
@@ -561,11 +568,15 @@
 	 * @param $tempIgnore Boolean
 	 */
 	function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
-		global $wgCommandLineMode;
+		global $wgCommandLineMode, $wgEnabledDBReadOnlyRedirect;
 		# Ignore errors during error handling to avoid infinite recursion
 		$ignore = $this->ignoreErrors( true );
 		++$this->mErrorCount;
 
+		if ( $wgEnabledDBReadOnlyRedirect && function_exists( 'wfDBReadOnlyFailed' ) && $this->wasReadOnlyError() ) {
+			wfDBReadOnlyFailed();
+		}
+
 		if( $ignore || $tempIgnore ) {
 			wfDebug("SQL ERROR (ignored): $error\n");
 			$this->ignoreErrors( $ignore );
@@ -2088,34 +2099,28 @@
 			wfDebug( "getLag: fake slave lagged {$this->mFakeSlaveLag} seconds\n" );
 			return $this->mFakeSlaveLag;
 		}
-		$res = $this->query( 'SHOW PROCESSLIST', __METHOD__ );
-		# Find slave SQL thread
-		while ( $row = $this->fetchObject( $res ) ) {
-			/* This should work for most situations - when default db 
-			 * for thread is not specified, it had no events executed, 
-			 * and therefore it doesn't know yet how lagged it is.
-			 *
-			 * Relay log I/O thread does not select databases.
-			 */
-			if ( $row->User == 'system user' && 
-				$row->StateĀ != 'Waiting for master to send event' &&
-				$row->StateĀ != 'Connecting to master' && 
-				$row->StateĀ != 'Queueing master event to the relay log' &&
-				$row->StateĀ != 'Waiting for master update' &&
-				$row->StateĀ != 'Requesting binlog dump' && 
-				$row->StateĀ != 'Waiting to reconnect after a failed master event read' &&
-				$row->StateĀ != 'Reconnecting after a failed master event read' &&
-				$row->StateĀ != 'Registering slave on master'
-				) {
-				# This is it, return the time (except -ve)
-				if ( $row->Time > 0x7fffffff ) {
+
+		/**
+		 * @begin of wikia change by eloy, copied from mediawiki trunk
+		 */
+
+		$res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
+		if (Ā !$res ) {
 					return false;
-				} else {
-					return $row->Time;
-				}
 			}
+		$row = $res->fetchObject();
+		if (Ā !$row ) {
+			return false;
 		}
+		if ( strval( $row->Seconds_Behind_Master ) === '' ) {
 		return false;
+		} else {
+			return intval( $row->Seconds_Behind_Master );
+		}
+
+		/**
+		 * @end of wikia change
+		 */
 	}
 
 	/**
@@ -2714,12 +2719,20 @@
 	}
 	
 	function getSQL() {
-		global $wgShowSQLErrors;
+		/* Wikia change begin - @author: Marooned */
+		/* add unique ID to query - useful for users reports to find proper query */
+		global $wgShowSQLErrors, $wgDBname;
+		$time = wfTimestamp( TS_MW );
+		$uri  = $_SERVER[ 'SERVER_NAME' ] . $_SERVER[ 'REQUEST_URI' ];
+		$qry  = $_SERVER[ 'QUERY_STRING' ];
+		error_log("DBQueryError: id=$time, errorNo={$this->errno}, errorMessage={$this->error}, SQL={$this->sql}");
+		error_log("DBQueryError: id=$time, errorNo={$this->errno}, NAME={$wgDBname}, functionName={$this->fname}, URI={$uri}, QUERY={$qry}");
 		if(Ā !$wgShowSQLErrors ) {
-			return $this->msg( 'sqlhidden', 'SQL hidden' );
+			return "/*id=$time*/ " . $this->msg( 'sqlhidden', 'SQL hidden' );
 		} else {
-			return $this->sql;
+			return "/*id=$time*/ " . $this->sql;
 		}
+		/* Wikia change end */
 	}
 	
 	function getLogMessage() {