Index: wordpress/wp-includes/wp-db.php
===================================================================
--- wordpress/wp-includes/wp-db.php	(revision 13372)
+++ wordpress/wp-includes/wp-db.php	(working copy)
@@ -169,7 +169,7 @@
 	 * {@internal Missing Description}}
 	 *
 	 * @since 3.0.0
-	 * @access public
+	 * @access private
 	 * @var int
 	 */
 	var $blogid = 0;
@@ -178,7 +178,7 @@
 	 * {@internal Missing Description}}
 	 *
 	 * @since 3.0.0
-	 * @access public
+	 * @access private
 	 * @var int
 	 */
 	var $siteid = 0;
@@ -470,6 +470,8 @@
 	 * the actual setting up of the class properties and connection
 	 * to the database.
 	 *
+     * NOTE: register_shutdown_function prevents this object from unloading
+     *
 	 * @since 2.0.8
 	 *
 	 * @param string $dbuser MySQL database user
@@ -533,6 +535,14 @@
 	/**
 	 * PHP5 style destructor and will run when database object is destroyed.
 	 *
+	 *
+	 *   Please see {@link __construct() class constructor} and the 
+	 *   {@link register_shutdown_function() register_shutdown_function()}. for
+	 *   more details.
+	 *
+	 * @link http://www.php.net/__destruct
+	 * @link http://www.php.net/register_shutdown_function 
+	 *
 	 * @since 2.0.8
 	 * @return bool true
 	 */
@@ -567,13 +577,10 @@
 			return $old_prefix;
 
 		$this->prefix = $this->get_blog_prefix( $this->blogid );
-
-		foreach ( (array) $this->tables( 'blog' ) as $table => $prefixed_table )
+		
+		foreach ( array_merge( $this->tables( 'blog' ), $this->tables( 'old' ) ) as $table => $prefixed_table )
 			$this->$table = $prefixed_table;
 
-		foreach ( (array) $this->tables( 'old' ) as $table => $prefixed_table )
-			$this->$table = $prefixed_table;
-
 		return $old_prefix;
 	}
 
@@ -586,7 +593,7 @@
 	 * @param int $site_id Optional.
 	 * @return string previous blog id
 	 */
-	function set_blog_id( $blog_id, $site_id = 0 ) {
+	function set_blog_id( $blog_id, $site_id = '' ) {
 		if ( ! empty( $site_id ) )
 			$this->siteid = $site_id;
 
@@ -594,13 +601,10 @@
 		$this->blogid = $blog_id;
 
 		$this->prefix = $this->get_blog_prefix( $this->blogid );
-
-		foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
+		
+		foreach ( array_merge( $this->tables( 'blog' ), $this->tables( 'old' ) ) as $table => $prefixed_table )
 			$this->$table = $prefixed_table;
 
-		foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
-			$this->$table = $prefixed_table;
-
 		return $old_blog_id;
 	}
 
@@ -630,6 +634,15 @@
 	 * override the WordPress users and usersmeta tables that would otherwise
 	 * be determined by the prefix.
 	 *
+	 * Scope / Table identifiers:
+	 *   all ........ global and blog tables.
+	 *   blog ....... blog tables
+	 *   blog+old ... blog and old (deprecated) tables
+	 *   global ..... global tables
+	 *   ms_global .. multisite global tables, regardless if current installation is multisite.
+	 *   old ........ old (depreacted) tables
+	 *
+	 * @access public
 	 * @since 3.0.0
 	 * @uses wpdb::$tables
 	 * @uses wpdb::$old_tables
@@ -637,47 +650,54 @@
 	 * @uses wpdb::$ms_global_tables
 	 * @uses is_multisite()
 	 *
-	 * @param string $scope Can be all, global, ms_global, blog, or old tables. Defaults to all.
+	 * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
 	 * 	'all' returns 'all' and 'global' tables. No old tables are returned.
 	 * 	'global' returns the global tables for the installation, returning multisite tables only if running multisite.
 	 * 	'ms_global' returns the multisite global tables, regardless if current installation is multisite.
 	 * 	'blog' returns the blog-level tables for the queried blog.
 	 * 	'old' returns tables which are deprecated.
-	 * @param bool $prefix Whether to include table prefixes. Default true. If blog
+	 * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog
 	 * 	prefix is requested, then the custom users and usermeta tables will be mapped.
-	 * @param int $blog_id The blog_id to prefix. Defaults to wpdb::blogid. Used only when prefix is requested.
+	 * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::blogid. Used only when prefix is requested.
 	 * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
 	 */
 	function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
+
 		switch ( $scope ) {
-			case 'old' :
-				$tables = $this->old_tables;
+			case 'all' :
+				$tables = array_merge( $this->global_tables, $this->tables );
+				if ( is_multisite() )
+					$tables = array_merge( $tables, $this->ms_global_tables );
 				break;
 			case 'blog' :
 				$tables = $this->tables;
 				break;
-			case 'ms_global' :
-				$tables = $this->ms_global_tables;
+			case 'blog+old':
+				$tables = array_merge( $this->tables, $this->old_tables );
 				break;
 			case 'global' :
 				$tables = $this->global_tables;
 				if ( is_multisite() )
 					$tables = array_merge( $tables, $this->ms_global_tables );
 				break;
-			case 'all' :
-				$tables = array_merge( $this->global_tables, $this->tables );
-				if ( is_multisite() )
-					$tables = array_merge( $tables, $this->ms_global_tables );
+			case 'ms_global' :
+				$tables = $this->ms_global_tables;
 				break;
+			case 'old' :
+				$tables = $this->old_tables;
+				break;
+			default:
+				$tables = array();
 		}
 
-		if ( $prefix ) {
+		if ( count( $tables ) && $prefix ) {
 			if ( ! $blog_id )
 				$blog_id = $this->blogid;
+
 			$blog_prefix = $this->get_blog_prefix( $blog_id );
 			$base_prefix = $this->base_prefix;
 			$global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
-			foreach ( $tables as $k => $table ) {
+			foreach ( $tables as $k => $table ) {				
 				if ( in_array( $table, $global_tables ) )
 					$tables[ $table ] = $base_prefix . $table;
 				else
@@ -725,7 +745,9 @@
 	/**
 	 * Weak escape
 	 *
-	 * @uses addslashes()
+	 * An alias to addslashes().
+	 *
+	 * @see addslashes()
 	 * @since {@internal Version Unknown}}
 	 * @access private
 	 *
@@ -739,8 +761,10 @@
 	/**
 	 * Real escape
 	 *
-	 * @uses mysql_real_escape_string()
-	 * @uses addslashes()
+	 * Escape via mysql_real_escape_string() or addslashes()  
+	 *
+	 * @see mysql_real_escape_string()
+	 * @see addslashes()
 	 * @since 2.8
 	 * @access private
 	 *
@@ -786,7 +810,7 @@
 	 * Works on arrays.
 	 *
 	 * @since 0.71
-	 * @param  string|array $data to escape
+	 * @param string|array $data to escape
 	 * @return string|array escaped as query safe string
 	 */
 	function escape( $data ) {
@@ -809,7 +833,7 @@
 	 *
 	 * @uses wpdb::_real_escape()
 	 * @since 2.3.0
-	 * @param  string $string to escape
+	 * @param string $string to escape
 	 * @return void
 	 */
 	function escape_by_ref( &$string ) {
@@ -837,7 +861,7 @@
 	 * Both %d and %s should be left unquoted in the query string.
 	 *
 	 * <code>
-	 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 )
+	 * wpdb::prepare( 'SELECT * FROM `table` WHERE `column` = %s AND `field` = %d', 'foo', 1337 )
 	 * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
 	 * </code>
 	 *
@@ -864,7 +888,7 @@
 			$args = $args[0];
 		$query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
 		$query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
-		$query = preg_replace('|(?<!%)%s|', "'%s'", $query); // quote the strings, avoiding escaped strings like %%s
+		$query = str_replace( '%s', "'%s'", $query ); // quote the strings
 		array_walk( $args, array( &$this, 'escape_by_ref' ) );
 		return @vsprintf( $query, $args );
 	}
@@ -893,10 +917,13 @@
 		else
 			$error_str = sprintf(/*WP_I18N_DB_QUERY_ERROR*/'WordPress database error %1$s for query %2$s'/*/WP_I18N_DB_QUERY_ERROR*/, $str, $this->last_query);
 
-		if ( function_exists('error_log') && $log_file = @ini_get('error_log') && ( 'syslog' == $log_file || is_writable( $log_file ) ) )
-			@error_log( $error_str, 0 );
+		if ( function_exists( 'error_log' )
+		     && ! ( $log_file = @ini_get( 'error_log' ) )
+		     && ( 'syslog' != $log_file )
+		     && @is_writable( $log_file ) ) 
+		    @error_log( $error_str );
 
-		// Is error output turned on or not..
+		// Show Errors ?
 		if ( ! $this->show_errors )
 			return false;
 
@@ -1033,7 +1060,6 @@
 		if ( ! $this->ready )
 			return false;
 
-		// some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
 		if ( function_exists( 'apply_filters' ) )
 			$query = apply_filters( 'query', $query );
 
@@ -1127,8 +1153,6 @@
 	 *
 	 * @since 2.5.0
 	 * @see wpdb::prepare()
-	 * @see wpdb::$field_types
-	 * @see wp_set_wpdb_vars()
 	 *
 	 * @param string $table table name
 	 * @param array $data Data to insert (in column => value pairs).  Both $data columns and $data values should be "raw" (neither should be SQL escaped).
@@ -1158,7 +1182,7 @@
 	 * Update a row in the table
 	 *
 	 * <code>
-	 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
+	 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) )
 	 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
 	 * </code>
 	 *
@@ -1456,7 +1480,6 @@
 	 * @see   wpdb::db_version()
 	 *
 	 * @param string $db_cap the feature
-	 * @param false|string|resource $dbh_or_table. Not implemented.
 	 * @return bool
 	 */
 	function has_cap( $db_cap ) {
@@ -1489,16 +1512,15 @@
 		foreach ( $trace as $call ) {
 			if ( isset( $call['class'] ) && __CLASS__ == $call['class'] )
 				continue; // Filter out wpdb calls.
+			$caller[] = isset( $call['class'] ) ? "{$call['class']}->{$call['function']}" : $call['function'];
 		}
-		$caller = join( ', ', $caller );
 
-		return $caller;
+		return join( ', ', $caller );
 	}
 
 	/**
 	 * The database version number.
 	 *
-	 * @param false|string|resource $dbh_or_table. Not implemented.
 	 * @return false|string false on failure, version number on success
 	 */
 	function db_version() {
@@ -1514,4 +1536,4 @@
 	 */
 	$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
 }
-?>
+?>
\ No newline at end of file
