+ /**
+ * WordPress Terms table
+ *
+ * @since 2.3.0
+ * @access public
+ * @var string
+ */
+ var $terms;
+
+ /**
+ * WordPress Term Relationships table
+ *
+ * @since 2.3.0
+ * @access public
+ * @var string
+ */
+ var $term_relationships;
+
+ /**
+ * WordPress Term Taxonomy table
+ *
+ * @since 2.3.0
+ * @access public
+ * @var string
+ */
+ var $term_taxonomy;
+
+ /*
+ * Global and Multisite tables
+ */
+
+ /**
+ * WordPress User Metadata table
+ *
+ * @since 2.3.0
+ * @access public
+ * @var string
+ */
+ var $usermeta;
+
+ /**
+ * WordPress Users table
+ *
+ * @since 1.5.0
+ * @access public
+ * @var string
+ */
+ var $users;
+
+ /**
+ * Multisite Blogs table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ var $blogs;
+
+ /**
+ * Multisite Blog Versions table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ var $blog_versions;
+
+ /**
+ * Multisite Registration Log table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ var $registration_log;
+
+ /**
+ * Multisite Signups table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ var $signups;
+
+ /**
+ * Multisite Sites table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ var $site;
+
+ /**
+ * Multisite Sitewide Terms table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ var $sitecategories;
+
+ /**
+ * Multisite Site Metadata table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ var $sitemeta;
+
+ /**
+ * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
+ *
+ * Keys are column names, values are format types: 'ID' => '%d'
+ *
+ * @since 2.8.0
+ * @see wpdb:prepare()
+ * @see wpdb:insert()
+ * @see wpdb:update()
+ * @see wp_set_wpdb_vars()
+ * @access public
+ * @var array
+ */
+ var $field_types = array();
+
+ /**
+ * Database table columns charset
+ *
+ * @since 2.2.0
+ * @access public
+ * @var string
+ */
+ var $charset;
+
+ /**
+ * Database table columns collate
+ *
+ * @since 2.2.0
+ * @access public
+ * @var string
+ */
+ var $collate;
+
+ /**
+ * Whether to use mysql_real_escape_string
+ *
+ * @since 2.8.0
+ * @access public
+ * @var bool
+ */
+ var $real_escape = false;
+
+ /**
+ * Database Username
+ *
+ * @since 2.9.0
+ * @access private
+ * @var string
+ */
+ var $dbuser;
+
+ /**
+ * A textual description of the last query/get_row/get_var call
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ var $func_call;
+
+ /**
+ * Whether MySQL is used as the database engine.
+ *
+ * Set in WPDB::db_connect() to true, by default. This is used when checking
+ * against the required MySQL version for WordPress. Normally, a replacement
+ * database drop-in (db.php) will skip these checks, but setting this to true
+ * will force the checks to occur.
+ *
+ * @since 3.3.0
+ * @access public
+ * @var bool
+ */
+ public $is_mysql = null;
+
+ /**
+ * Connects to the database server and selects a database
+ *
+ * PHP5 style constructor for compatibility with PHP5. Does
+ * the actual setting up of the class properties and connection
+ * to the database.
+ *
+ * @link http://core.trac.wordpress.org/ticket/3354
+ * @since 2.0.8
+ *
+ * @param string $dbuser MySQL database user
+ * @param string $dbpassword MySQL database password
+ * @param string $dbname MySQL database name
+ * @param string $dbhost MySQL database host
+ */
+ function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
+ register_shutdown_function( array( &$this, '__destruct' ) );
+
+ if ( WP_DEBUG )
+ $this->show_errors();
+
+ $this->init_charset();
+
+ $this->dbuser = $dbuser;
+ $this->dbpassword = $dbpassword;
+ $this->dbname = $dbname;
+ $this->dbhost = $dbhost;
+
+ $this->db_connect();
+ }
+
+ /**
+ * PHP5 style destructor and will run when database object is destroyed.
+ *
+ * @see wpdb::__construct()
+ * @since 2.0.8
+ * @return bool true
+ */
+ function __destruct() {
+ return true;
+ }
+
+ /**
+ * Set $this->charset and $this->collate
+ *
+ * @since 3.1.0
+ */
+ function init_charset() {
+ if ( function_exists('is_multisite') && is_multisite() ) {
+ $this->charset = 'utf8';
+ if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
+ $this->collate = DB_COLLATE;
+ else
+ $this->collate = 'utf8_general_ci';
+ } elseif ( defined( 'DB_COLLATE' ) ) {
+ $this->collate = DB_COLLATE;
+ }
+
+ if ( defined( 'DB_CHARSET' ) )
+ $this->charset = DB_CHARSET;
+ }
+
+ /**
+ * Sets the connection's character set.
+ *
+ * @since 3.1.0
+ *
+ * @param resource $dbh The resource given by mysql_connect
+ * @param string $charset The character set (optional)
+ * @param string $collate The collation (optional)
+ */
+ function set_charset($dbh, $charset = null, $collate = null) {
+ if ( !isset($charset) )
+ $charset = $this->charset;
+ if ( !isset($collate) )
+ $collate = $this->collate;
+ if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) {
+ if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) {
+ mysql_set_charset( $charset, $dbh );
+ $this->real_escape = true;
+ } else {
+ $query = $this->prepare( 'SET NAMES %s', $charset );
+ if ( ! empty( $collate ) )
+ $query .= $this->prepare( ' COLLATE %s', $collate );
+ mysql_query( $query, $dbh );
+ }
+ }
+ }
+
+ /**
+ * Sets the table prefix for the WordPress tables.
+ *
+ * @since 2.5.0
+ *
+ * @param string $prefix Alphanumeric name for the new prefix.
+ * @param bool $set_table_names Optional. Whether the table names, e.g. wpdb::$posts, should be updated or not.
+ * @return string|WP_Error Old prefix or WP_Error on error
+ */
+ function set_prefix( $prefix, $set_table_names = true ) {
+
+ if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
+ return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
+
+ $old_prefix = is_multisite() ? '' : $prefix;
+
+ if ( isset( $this->base_prefix ) )
+ $old_prefix = $this->base_prefix;
+
+ $this->base_prefix = $prefix;
+
+ if ( $set_table_names ) {
+ foreach ( $this->tables( 'global' ) as $table => $prefixed_table )
+ $this->$table = $prefixed_table;
+
+ if ( is_multisite() && empty( $this->blogid ) )
+ return $old_prefix;
+
+ $this->prefix = $this->get_blog_prefix();
+
+ foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
+ $this->$table = $prefixed_table;
+
+ foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
+ $this->$table = $prefixed_table;
+ }
+ return $old_prefix;
+ }
+
+ /**
+ * Sets blog id.
+ *
+ * @since 3.0.0
+ * @access public
+ * @param int $blog_id
+ * @param int $site_id Optional.
+ * @return string previous blog id
+ */
+ function set_blog_id( $blog_id, $site_id = 0 ) {
+ if ( ! empty( $site_id ) )
+ $this->siteid = $site_id;
+
+ $old_blog_id = $this->blogid;
+ $this->blogid = $blog_id;
+
+ $this->prefix = $this->get_blog_prefix();
+
+ foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
+ $this->$table = $prefixed_table;
+
+ foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
+ $this->$table = $prefixed_table;
+
+ return $old_blog_id;
+ }
+
+ /**
+ * Gets blog prefix.
+ *
+ * @uses is_multisite()
+ * @since 3.0.0
+ * @param int $blog_id Optional.
+ * @return string Blog prefix.
+ */
+ function get_blog_prefix( $blog_id = null ) {
+ if ( is_multisite() ) {
+ if ( null === $blog_id )
+ $blog_id = $this->blogid;
+ $blog_id = (int) $blog_id;
+ if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) )
+ return $this->base_prefix;
+ else
+ return $this->base_prefix . $blog_id . '_';
+ } else {
+ return $this->base_prefix;
+ }
+ }
+
+ /**
+ * Returns an array of WordPress tables.
+ *
+ * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
+ * override the WordPress users and usermeta tables that would otherwise
+ * be determined by the prefix.
+ *
+ * The scope argument can take one of the following:
+ *
+ * 'all' - returns 'all' and 'global' tables. No old tables are returned.
+ * 'blog' - returns the blog-level tables for the queried blog.
+ * '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.
+ * 'old' - returns tables which are deprecated.
+ *
+ * @since 3.0.0
+ * @uses wpdb::$tables
+ * @uses wpdb::$old_tables
+ * @uses wpdb::$global_tables
+ * @uses wpdb::$ms_global_tables
+ * @uses is_multisite()
+ *
+ * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
+ * @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 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 '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 'global' :
+ $tables = $this->global_tables;
+ if ( is_multisite() )
+ $tables = array_merge( $tables, $this->ms_global_tables );
+ break;
+ case 'ms_global' :
+ $tables = $this->ms_global_tables;
+ break;
+ case 'old' :
+ $tables = $this->old_tables;
+ break;
+ default :
+ return array();
+ break;