Make WordPress Core

Ticket #16764: 16764.patch

File 16764.patch, 38.6 KB (added by hakre, 14 years ago)

wpdb with interface and base class

  • wp-admin/includes/deprecated.php

     
    493493                $this->query_from = " FROM $wpdb->users";
    494494                $this->query_where = " WHERE 1=1 $search_sql";
    495495
     496                $prefix = $wpdb->get_blog_prefix();
    496497                if ( $this->role ) {
    497498                        $this->query_from .= " INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id";
    498                         $this->query_where .= $wpdb->prepare(" AND $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $this->role . '%');
     499                        $this->query_where .= $wpdb->prepare(" AND $wpdb->usermeta.meta_key = '{$prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $this->role . '%');
    499500                } elseif ( is_multisite() ) {
    500                         $level_key = $wpdb->prefix . 'capabilities'; // wpmu site admins don't have user_levels
     501                        $level_key = $prefix . 'capabilities'; // wpmu site admins don't have user_levels
    501502                        $this->query_from .= ", $wpdb->usermeta";
    502503                        $this->query_where .= " AND $wpdb->users.ID = $wpdb->usermeta.user_id AND meta_key = '{$level_key}'";
    503504                }
  • wp-admin/includes/upgrade.php

     
    645645        }
    646646
    647647        // Obsolete tables
    648         $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues');
    649         $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes');
    650         $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups');
    651         $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options');
     648        $prefix = $wpdb->get_blog_prefix();
     649        $wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'optionvalues');
     650        $wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'optiontypes');
     651        $wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'optiongroups');
     652        $wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'optiongroup_options');
    652653
    653654        // Update comments table to use comment_type
    654655        $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
     
    689690                if ( !empty( $user->user_nickname ) )
    690691                        update_user_meta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) );
    691692                if ( !empty( $user->user_level ) )
    692                         update_user_meta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level );
     693                        update_user_meta( $user->ID, $wpdb->get_blog_prefix() . 'user_level', $user->user_level );
    693694                if ( !empty( $user->user_icq ) )
    694695                        update_user_meta( $user->ID, 'icq', $wpdb->escape($user->user_icq) );
    695696                if ( !empty( $user->user_aim ) )
     
    714715                endif;
    715716
    716717                // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
    717                 $caps = get_user_meta( $user->ID, $wpdb->prefix . 'capabilities');
     718                $prefix = $wpdb->get_blog_prefix();
     719                $caps = get_user_meta( $user->ID, $prefix . 'capabilities');
    718720                if ( empty($caps) || defined('RESET_CAPS') ) {
    719                         $level = get_user_meta($user->ID, $wpdb->prefix . 'user_level', true);
     721                        $level = get_user_meta($user->ID, $prefix . 'user_level', true);
    720722                        $role = translate_level_to_role($level);
    721                         update_user_meta( $user->ID, $wpdb->prefix . 'capabilities', array($role => true) );
     723                        update_user_meta( $user->ID, $prefix . 'capabilities', array($role => true) );
    722724                }
    723725
    724726        endforeach;
     
    897899                $link_cat_id_map = array();
    898900                $default_link_cat = 0;
    899901                $tt_ids = array();
    900                 $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories');
     902                $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->get_blog_prefix() . 'linkcategories');
    901903                foreach ( $link_cats as $category) {
    902904                        $cat_id = (int) $category->cat_id;
    903905                        $term_id = 0;
     
    955957
    956958        if ( $wp_current_db_version < 4772 ) {
    957959                // Obsolete linkcategories table
    958                 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
     960                $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->get_blog_prefix() . 'linkcategories');
    959961        }
    960962
    961963        // Recalculate all counts
     
    990992 */
    991993function upgrade_230_old_tables() {
    992994        global $wpdb;
    993         $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories');
    994         $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat');
    995         $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post2cat');
     995        $prefix = $wpdb->get_blog_prefix();
     996        $wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'categories');
     997        $wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'link2cat');
     998        $wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'post2cat');
    996999}
    9971000
    9981001/**
  • wp-includes/capabilities.php

     
    9898         */
    9999        function _init () {
    100100                global $wpdb, $wp_user_roles;
    101                 $this->role_key = $wpdb->prefix . 'user_roles';
     101                $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
    102102                if ( ! empty( $wp_user_roles ) ) {
    103103                        $this->roles = $wp_user_roles;
    104104                        $this->use_db = false;
     
    516516        function _init_caps( $cap_key = '' ) {
    517517                global $wpdb;
    518518                if ( empty($cap_key) )
    519                         $this->cap_key = $wpdb->prefix . 'capabilities';
     519                        $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
    520520                else
    521521                        $this->cap_key = $cap_key;
    522522                $this->caps = &$this->{$this->cap_key};
     
    662662        function update_user_level_from_caps() {
    663663                global $wpdb;
    664664                $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
    665                 update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
     665                update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
    666666        }
    667667
    668668        /**
     
    704704                global $wpdb;
    705705                $this->caps = array();
    706706                delete_user_meta( $this->ID, $this->cap_key );
    707                 delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
     707                delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
    708708                $this->get_role_caps();
    709709        }
    710710
  • wp-includes/ms-blogs.php

     
    475475        }
    476476
    477477        $wpdb->set_blog_id($new_blog);
    478         $table_prefix = $wpdb->prefix;
     478        $table_prefix = $wpdb->get_blog_prefix();
    479479        $prev_blog_id = $blog_id;
    480480        $blog_id = $new_blog;
    481481
     
    541541        $wpdb->set_blog_id($blog);
    542542        $prev_blog_id = $blog_id;
    543543        $blog_id = $blog;
    544         $table_prefix = $wpdb->prefix;
     544        $table_prefix = $wpdb->get_blog_prefix();
    545545
    546546        if ( is_object( $wp_roles ) ) {
    547547                $wpdb->suppress_errors();
  • wp-includes/user.php

     
    262262
    263263        // Keys used as object vars cannot have dashes.
    264264        $key = str_replace('-', '', $option);
    265 
    266         if ( isset( $user->{$wpdb->prefix . $key} ) ) // Blog specific
    267                 $result = $user->{$wpdb->prefix . $key};
     265        $prefix = $wpdb->get_blog_prefix();
     266        if ( isset( $user->{$prefix . $key} ) ) // Blog specific
     267                $result = $user->{$prefix . $key};
    268268        elseif ( isset( $user->{$key} ) ) // User specific and cross-blog
    269269                $result = $user->{$key};
    270270        else
     
    295295        global $wpdb;
    296296
    297297        if ( !$global )
    298                 $option_name = $wpdb->prefix . $option_name;
     298                $option_name = $wpdb->get_blog_prefix() . $option_name;
    299299
    300300        // For backward compatibility. See differences between update_user_meta() and deprecated update_usermeta().
    301301        // http://core.trac.wordpress.org/ticket/13088
     
    324324        global $wpdb;
    325325
    326326        if ( !$global )
    327                 $option_name = $wpdb->prefix . $option_name;
     327                $option_name = $wpdb->get_blog_prefix() . $option_name;
    328328        return delete_user_meta( $user_id, $option_name );
    329329}
    330330
     
    11011101                $user->{$key} = $value;
    11021102        }
    11031103
    1104         $level = $wpdb->prefix . 'user_level';
     1104        $level = $wpdb->get_blog_prefix() . 'user_level';
    11051105        if ( isset( $user->{$level} ) )
    11061106                $user->user_level = $user->{$level};
    11071107
  • wp-includes/wp-db.php

     
    3535define( 'ARRAY_N', 'ARRAY_N' );
    3636
    3737/**
    38  * WordPress Database Access Abstraction Object
     38 * WordPress Database Access Abstraction Object Methods
    3939 *
    40  * It is possible to replace this class with your own
    41  * by setting the $wpdb global variable in wp-content/db.php
    42  * file with your class. You can name it wpdb also, since
    43  * this file will not be included, if the other file is
    44  * available.
     40 * @package WordPress
     41 * @subpackage Database
     42 * @since 3.2
     43 */
     44Interface WP_Db_Methods {
     45        public function set_prefix( $prefix, $set_table_names = true );
     46        public function set_blog_id( $blog_id, $site_id = 0 );
     47        public function get_blog_prefix( $blog_id = null );
     48        public function tables( $scope = 'all', $prefix = true, $blog_id = 0 );
     49        public function escape( $data );
     50        public function escape_by_ref( &$string );
     51        public function prepare( $query = null );
     52        public function show_errors( $show = true );
     53        public function hide_errors();
     54        public function suppress_errors( $suppress = true );
     55        public function query( $query );
     56        public function insert( $table, $data, $format = null );
     57        public function replace( $table, $data, $format = null );
     58        public function update( $table, $data, $where, $format = null, $where_format = null );
     59        public function get_var( $query = null, $column = 0, $row = 0 );
     60        public function get_row( $query = null, $output = null, $row = 0 );
     61        public function get_col( $query = null , $column = 0 );
     62        public function get_results( $query = null, $output = null );
     63        public function get_col_info( $info_type = 'name', $col_offset = null );
     64        public function has_cap( $feature );
     65        public function db_version();
     66}
     67
     68/**
     69 * WordPress Database Access Abstraction Data Object
    4570 *
    46  * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
     71 * All public object variables of the WordPress Database
     72 * Acccess Abstraction Object.
    4773 *
    4874 * @package WordPress
    4975 * @subpackage Database
    50  * @since 0.71
     76 * @since 3.2
    5177 */
    52 class wpdb {
     78Class WP_Db_Variables {
    5379
    5480        /**
    55          * Whether to show SQL/DB errors
    56          *
    57          * @since 0.71
    58          * @access private
    59          * @var bool
    60          */
    61         var $show_errors = false;
    62 
    63         /**
    64          * Whether to suppress errors during the DB bootstrapping.
    65          *
    66          * @access private
    67          * @since 2.5.0
    68          * @var bool
    69          */
    70         var $suppress_errors = false;
    71 
    72         /**
    73          * The last error during query.
    74          *
    75          * @see get_last_error()
    76          * @since 2.5.0
    77          * @access private
    78          * @var string
    79          */
    80         var $last_error = '';
    81 
    82         /**
    83          * Amount of queries made
    84          *
    85          * @since 1.2.0
    86          * @access private
    87          * @var int
    88          */
    89         var $num_queries = 0;
    90 
    91         /**
    92          * Count of rows returned by previous query
    93          *
    94          * @since 1.2.0
    95          * @access private
    96          * @var int
    97          */
    98         var $num_rows = 0;
    99 
    100         /**
    101          * Count of affected rows by previous query
    102          *
    103          * @since 0.71
    104          * @access private
    105          * @var int
    106          */
    107         var $rows_affected = 0;
    108 
    109         /**
    11081         * The ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
    11182         *
    11283         * @since 0.71
    11384         * @access public
    11485         * @var int
    11586         */
    116         var $insert_id = 0;
     87        public $insert_id = 0;
    11788
    11889        /**
    119          * Saved result of the last query made
    120          *
    121          * @since 1.2.0
    122          * @access private
    123          * @var array
    124          */
    125         var $last_query;
    126 
    127         /**
    128          * Results of the last query made
    129          *
    130          * @since 1.0.0
    131          * @access private
    132          * @var array|null
    133          */
    134         var $last_result;
    135 
    136         /**
    137          * Saved info on the table column
    138          *
    139          * @since 1.2.0
    140          * @access private
    141          * @var array
    142          */
    143         var $col_info;
    144 
    145         /**
    146          * Saved queries that were executed
    147          *
    148          * @since 1.5.0
    149          * @access private
    150          * @var array
    151          */
    152         var $queries;
    153 
    154         /**
    155          * WordPress table prefix
    156          *
    157          * You can set this to have multiple WordPress installations
    158          * in a single database. The second reason is for possible
    159          * security precautions.
    160          *
    161          * @since 0.71
    162          * @access private
    163          * @var string
    164          */
    165         var $prefix = '';
    166 
    167         /**
    168          * Whether the database queries are ready to start executing.
    169          *
    170          * @since 2.5.0
    171          * @access private
    172          * @var bool
    173          */
    174         var $ready = false;
    175 
    176         /**
    17790         * {@internal Missing Description}}
    17891         *
    17992         * @since 3.0.0
    18093         * @access public
    18194         * @var int
    18295         */
    183         var $blogid = 0;
     96        public $blogid = 0;
    18497
    18598        /**
    18699         * {@internal Missing Description}}
     
    189102         * @access public
    190103         * @var int
    191104         */
    192         var $siteid = 0;
     105        public $siteid = 0;
    193106
    194107        /**
    195          * List of WordPress per-blog tables
    196          *
    197          * @since 2.5.0
    198          * @access private
    199          * @see wpdb::tables()
    200          * @var array
    201          */
    202         var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
    203                 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
    204 
    205         /**
    206          * List of deprecated WordPress tables
    207          *
    208          * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539
    209          *
    210          * @since 2.9.0
    211          * @access private
    212          * @see wpdb::tables()
    213          * @var array
    214          */
    215         var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
    216 
    217         /**
    218          * List of WordPress global tables
    219          *
    220          * @since 3.0.0
    221          * @access private
    222          * @see wpdb::tables()
    223          * @var array
    224          */
    225         var $global_tables = array( 'users', 'usermeta' );
    226 
    227         /**
    228          * List of Multisite global tables
    229          *
    230          * @since 3.0.0
    231          * @access private
    232          * @see wpdb::tables()
    233          * @var array
    234          */
    235         var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
    236                 'sitecategories', 'registration_log', 'blog_versions' );
    237 
    238         /**
    239108         * WordPress Comments table
    240109         *
    241110         * @since 1.5.0
    242111         * @access public
    243112         * @var string
    244113         */
    245         var $comments;
     114        public $comments;
    246115
    247116        /**
    248117         * WordPress Comment Metadata table
     
    251120         * @access public
    252121         * @var string
    253122         */
    254         var $commentmeta;
     123        public $commentmeta;
    255124
    256125        /**
    257126         * WordPress Links table
     
    260129         * @access public
    261130         * @var string
    262131         */
    263         var $links;
     132        public $links;
    264133
    265134        /**
    266135         * WordPress Options table
     
    269138         * @access public
    270139         * @var string
    271140         */
    272         var $options;
     141        public $options;
    273142
    274143        /**
    275144         * WordPress Post Metadata table
     
    278147         * @access public
    279148         * @var string
    280149         */
    281         var $postmeta;
     150        public $postmeta;
    282151
    283152        /**
    284153         * WordPress Posts table
     
    287156         * @access public
    288157         * @var string
    289158         */
    290         var $posts;
     159        public $posts;
    291160
    292161        /**
    293162         * WordPress Terms table
     
    296165         * @access public
    297166         * @var string
    298167         */
    299         var $terms;
     168        public $terms;
    300169
    301170        /**
    302171         * WordPress Term Relationships table
     
    305174         * @access public
    306175         * @var string
    307176         */
    308         var $term_relationships;
     177        public $term_relationships;
    309178
    310179        /**
    311180         * WordPress Term Taxonomy table
     
    314183         * @access public
    315184         * @var string
    316185         */
    317         var $term_taxonomy;
     186        public $term_taxonomy;
    318187
    319188        /*
    320189         * Global and Multisite tables
     
    327196         * @access public
    328197         * @var string
    329198         */
    330         var $usermeta;
     199        public $usermeta;
    331200
    332201        /**
    333202         * WordPress Users table
     
    336205         * @access public
    337206         * @var string
    338207         */
    339         var $users;
     208        public $users;
    340209
    341210        /**
    342211         * Multisite Blogs table
     
    345214         * @access public
    346215         * @var string
    347216         */
    348         var $blogs;
     217        public $blogs;
    349218
    350219        /**
    351220         * Multisite Blog Versions table
     
    354223         * @access public
    355224         * @var string
    356225         */
    357         var $blog_versions;
     226        public $blog_versions;
    358227
    359228        /**
    360229         * Multisite Registration Log table
     
    363232         * @access public
    364233         * @var string
    365234         */
    366         var $registration_log;
     235        public $registration_log;
    367236
    368237        /**
    369238         * Multisite Signups table
     
    372241         * @access public
    373242         * @var string
    374243         */
    375         var $signups;
     244        public $signups;
    376245
    377246        /**
    378247         * Multisite Sites table
     
    381250         * @access public
    382251         * @var string
    383252         */
    384         var $site;
     253        public $site;
    385254
    386255        /**
    387256         * Multisite Sitewide Terms table
     
    390259         * @access public
    391260         * @var string
    392261         */
    393         var $sitecategories;
     262        public $sitecategories;
    394263
    395264        /**
    396265         * Multisite Site Metadata table
     
    399268         * @access public
    400269         * @var string
    401270         */
    402         var $sitemeta;
     271        public $sitemeta;
    403272
    404273        /**
    405274         * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
     
    414283         * @access public
    415284         * @var array
    416285         */
    417         var $field_types = array();
     286        public $field_types = array();
    418287
    419288        /**
    420289         * Database table columns charset
     
    423292         * @access public
    424293         * @var string
    425294         */
    426         var $charset;
     295        public $charset;
    427296
    428297        /**
    429298         * Database table columns collate
     
    432301         * @access public
    433302         * @var string
    434303         */
    435         var $collate;
     304        public $collate;
    436305
    437306        /**
    438307         * Whether to use mysql_real_escape_string
     
    441310         * @access public
    442311         * @var bool
    443312         */
    444         var $real_escape = false;
     313        public $real_escape = false;
    445314
    446315        /**
    447          * Database Username
     316         * A textual description of the last query/get_row/get_var call
    448317         *
    449          * @since 2.9.0
    450          * @access private
     318         * @since 3.0.0
     319         * @access public
    451320         * @var string
    452321         */
    453         var $dbuser;
     322        public $func_call;
     323}
    454324
     325/**
     326 * WordPress Database Access Abstraction Object Base Class
     327 *
     328 * It is possible to replace the database abstraction by
     329 * extending this abtract class.
     330 *
     331 * @link http://core.trac.wordpress.org/ticket/3354
     332 *
     333 * @package WordPress
     334 * @subpackage Database
     335 * @since 3.2
     336 */
     337abstract class WP_Db_Base extends WP_Db_Variables implements WP_Db_Methods {
     338        public function __construct() {
     339                register_shutdown_function( array( $this, '__destruct' ) );
     340        }
     341
    455342        /**
    456          * A textual description of the last query/get_row/get_var call
     343         * Destructor that will run when database object is destroyed.
    457344         *
    458          * @since 3.0.0
    459          * @access public
     345         * Callback of register_shutdown_function
     346         *
     347         * @see __construct()
     348         * @since 2.0.8
     349         * @return bool true
     350         */
     351        public function __destruct() {
     352                return true;
     353        }
     354}
     355
     356
     357/**
     358 * WordPress Database Access Abstraction Object
     359 *
     360 * It is possible to replace this class with your own
     361 * by setting the $wpdb global variable in wp-content/db.php
     362 * file with your class.
     363 *
     364 * You can not name it wpdb, because this file will be included.
     365 *
     366 * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
     367 *
     368 * @package WordPress
     369 * @subpackage Database
     370 * @since 0.71
     371 */
     372class wpdb extends WP_Db_Base {
     373
     374        /**
     375         * Whether to show SQL/DB errors
     376         *
     377         * @since 0.71
     378         * @access private
     379         * @var bool
     380         */
     381        private $show_errors = false;
     382
     383        /**
     384         * Whether to suppress errors during the DB bootstrapping.
     385         *
     386         * @access private
     387         * @since 2.5.0
     388         * @var bool
     389         */
     390        private $suppress_errors = false;
     391
     392        /**
     393         * The last error during query.
     394         *
     395         * @see get_last_error()
     396         * @since 2.5.0
     397         * @access private
    460398         * @var string
    461399         */
    462         var $func_call;
     400        private $last_error = '';
    463401
    464402        /**
    465          * Connects to the database server and selects a database
     403         * Amount of queries made
    466404         *
    467          * PHP4 compatibility layer for calling the PHP5 constructor.
     405         * @since 1.2.0
     406         * @access private
     407         * @var int
     408         */
     409        private $num_queries = 0;
     410
     411        /**
     412         * Count of rows returned by previous query
    468413         *
    469          * @uses wpdb::__construct() Passes parameters and returns result
     414         * @since 1.2.0
     415         * @access private
     416         * @var int
     417         */
     418        private $num_rows = 0;
     419
     420        /**
     421         * Count of affected rows by previous query
     422         *
    470423         * @since 0.71
     424         * @access private
     425         * @var int
     426         */
     427        private $rows_affected = 0;
     428
     429        /**
     430         * Saved result of the last query made
    471431         *
    472          * @param string $dbuser MySQL database user
    473          * @param string $dbpassword MySQL database password
    474          * @param string $dbname MySQL database name
    475          * @param string $dbhost MySQL database host
     432         * @since 1.2.0
     433         * @access private
     434         * @var array
    476435         */
    477         function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) {
    478                 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost );
    479         }
     436        private $last_query;
    480437
    481438        /**
     439         * Results of the last query made
     440         *
     441         * @since 1.0.0
     442         * @access private
     443         * @var array|null
     444         */
     445        private $last_result;
     446
     447        /**
     448         * Saved info on the table column
     449         *
     450         * @since 1.2.0
     451         * @access private
     452         * @var array
     453         */
     454        private $col_info;
     455
     456        /**
     457         * Saved queries that were executed
     458         *
     459         * @since 1.5.0
     460         * @access private
     461         * @var array
     462         */
     463        private $queries;
     464
     465        /**
     466         * Timer start value, for debugging purposes.
     467         *
     468         * @since 1.5.0
     469         * @access private
     470         * @var float
     471         * @see timer_stop()
     472         * @see timer_start()
     473         */
     474        private $time_start;
     475
     476        /**
     477         * WordPress table prefix
     478         *
     479         * You can set this to have multiple WordPress installations
     480         * in a single database. The second reason is for possible
     481         * security precautions.
     482         *
     483         * @since 0.71
     484         * @access private
     485         * @var string
     486         */
     487        private $prefix = '';
     488
     489        /**
     490         * Whether the database queries are ready to start executing.
     491         *
     492         * @since 2.5.0
     493         * @access private
     494         * @var bool
     495         */
     496        private $ready = false;
     497
     498        /**
     499         * List of WordPress per-blog tables
     500         *
     501         * @since 2.5.0
     502         * @access private
     503         * @see wpdb::tables()
     504         * @var array
     505         */
     506        private $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
     507                'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
     508
     509        /**
     510         * List of deprecated WordPress tables
     511         *
     512         * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539
     513         *
     514         * @since 2.9.0
     515         * @access private
     516         * @see wpdb::tables()
     517         * @var array
     518         */
     519        private $old_tables = array( 'categories', 'post2cat', 'link2cat' );
     520
     521        /**
     522         * List of WordPress global tables
     523         *
     524         * @since 3.0.0
     525         * @access private
     526         * @see wpdb::tables()
     527         * @var array
     528         */
     529        private $global_tables = array( 'users', 'usermeta' );
     530
     531        /**
     532         * List of Multisite global tables
     533         *
     534         * @since 3.0.0
     535         * @access private
     536         * @see wpdb::tables()
     537         * @var array
     538         */
     539        private $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
     540                'sitecategories', 'registration_log', 'blog_versions' );
     541
     542        /**
     543         * Database Username
     544         *
     545         * @since 2.9.0
     546         * @access private
     547         * @var string
     548         */
     549        private $dbuser;
     550
     551        /**
    482552         * Connects to the database server and selects a database
    483553         *
    484          * PHP5 style constructor for compatibility with PHP5. Does
     554         * Does
    485555         * the actual setting up of the class properties and connection
    486556         * to the database.
    487557         *
    488          * @link http://core.trac.wordpress.org/ticket/3354
    489558         * @since 2.0.8
    490559         *
    491560         * @param string $dbuser MySQL database user
     
    493562         * @param string $dbname MySQL database name
    494563         * @param string $dbhost MySQL database host
    495564         */
    496         function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
    497                 register_shutdown_function( array( &$this, '__destruct' ) );
     565        public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
     566                parent::__construct();
    498567
    499                 if ( WP_DEBUG )
    500                         $this->show_errors();
    501 
    502568                $this->init_charset();
    503569
    504570                $this->dbuser = $dbuser;
     
    510576        }
    511577
    512578        /**
    513          * PHP5 style destructor and will run when database object is destroyed.
    514          *
    515          * @see wpdb::__construct()
    516          * @since 2.0.8
    517          * @return bool true
    518          */
    519         function __destruct() {
    520                 return true;
    521         }
    522 
    523         /**
    524579         * Set $this->charset and $this->collate
    525580         *
    526581         * @since 3.1.0
    527582         */
    528         function init_charset() {
     583        private function init_charset() {
    529584                if ( function_exists('is_multisite') && is_multisite() ) {
    530585                        $this->charset = 'utf8';
    531586                        if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
     
    549604         * @param string   $charset The character set (optional)
    550605         * @param string   $collate The collation (optional)
    551606         */
    552         function set_charset($dbh, $charset = null, $collate = null) {
     607        private function set_charset($dbh, $charset = null, $collate = null) {
    553608                if ( !isset($charset) )
    554609                        $charset = $this->charset;
    555610                if ( !isset($collate) )
     
    575630         * @param string $prefix Alphanumeric name for the new prefix.
    576631         * @return string|WP_Error Old prefix or WP_Error on error
    577632         */
    578         function set_prefix( $prefix, $set_table_names = true ) {
     633        public function set_prefix( $prefix, $set_table_names = true ) {
    579634
    580635                if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
    581636                        return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
     
    614669         * @param int $site_id Optional.
    615670         * @return string previous blog id
    616671         */
    617         function set_blog_id( $blog_id, $site_id = 0 ) {
     672        public function set_blog_id( $blog_id, $site_id = 0 ) {
    618673                if ( ! empty( $site_id ) )
    619674                        $this->siteid = $site_id;
    620675
     
    640695         * @param int $blog_id Optional.
    641696         * @return string Blog prefix.
    642697         */
    643         function get_blog_prefix( $blog_id = null ) {
     698        public function get_blog_prefix( $blog_id = null ) {
    644699                if ( is_multisite() ) {
    645700                        if ( null === $blog_id )
    646701                                $blog_id = $this->blogid;
     
    681736         * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested.
    682737         * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
    683738         */
    684         function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
     739        public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
    685740                switch ( $scope ) {
    686741                        case 'all' :
    687742                                $tables = array_merge( $this->global_tables, $this->tables );
     
    743798         * @param resource $dbh Optional link identifier.
    744799         * @return null Always null.
    745800         */
    746         function select( $db, $dbh = null) {
     801        private function select( $db, $dbh = null) {
    747802                if ( is_null($dbh) )
    748803                        $dbh = $this->dbh;
    749804
     
    771826         * @param string $string
    772827         * @return string
    773828         */
    774         function _weak_escape( $string ) {
     829        private function _weak_escape( $string ) {
    775830                return addslashes( $string );
    776831        }
    777832
     
    786841         * @param  string $string to escape
    787842         * @return string escaped
    788843         */
    789         function _real_escape( $string ) {
     844        private function _real_escape( $string ) {
    790845                if ( $this->dbh && $this->real_escape )
    791846                        return mysql_real_escape_string( $string, $this->dbh );
    792847                else
     
    804859         * @param  string|array $data
    805860         * @return string|array escaped
    806861         */
    807         function _escape( $data ) {
     862        private function _escape( $data ) {
    808863                if ( is_array( $data ) ) {
    809864                        foreach ( (array) $data as $k => $v ) {
    810865                                if ( is_array($v) )
     
    828883         * @param string|array $data to escape
    829884         * @return string|array escaped as query safe string
    830885         */
    831         function escape( $data ) {
     886        public function escape( $data ) {
    832887                if ( is_array( $data ) ) {
    833888                        foreach ( (array) $data as $k => $v ) {
    834889                                if ( is_array( $v ) )
     
    851906         * @param string $string to escape
    852907         * @return void
    853908         */
    854         function escape_by_ref( &$string ) {
     909        public function escape_by_ref( &$string ) {
    855910                $string = $this->_real_escape( $string );
    856911        }
    857912
     
    891946         * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string
    892947         *      if there was something to prepare
    893948         */
    894         function prepare( $query = null ) { // ( $query, *$args )
     949        public function prepare( $query = null ) { // ( $query, *$args )
    895950                if ( is_null( $query ) )
    896951                        return;
    897952
     
    916971         * @param string $str The error to display
    917972         * @return bool False if the showing of errors is disabled.
    918973         */
    919         function print_error( $str = '' ) {
     974        private function print_error( $str = '' ) {
    920975                global $EZSQL_ERROR;
    921976
    922977                if ( !$str )
     
    9731028         * @param bool $show Whether to show or hide errors
    9741029         * @return bool Old value for showing errors.
    9751030         */
    976         function show_errors( $show = true ) {
     1031        public function show_errors( $show = true ) {
    9771032                $errors = $this->show_errors;
    9781033                $this->show_errors = $show;
    9791034                return $errors;
     
    9891044         *
    9901045         * @return bool Whether showing of errors was active
    9911046         */
    992         function hide_errors() {
     1047        public function hide_errors() {
    9931048                $show = $this->show_errors;
    9941049                $this->show_errors = false;
    9951050                return $show;
     
    10061061         * @param bool $suppress Optional. New value. Defaults to true.
    10071062         * @return bool Old value
    10081063         */
    1009         function suppress_errors( $suppress = true ) {
     1064        public function suppress_errors( $suppress = true ) {
    10101065                $errors = $this->suppress_errors;
    10111066                $this->suppress_errors = (bool) $suppress;
    10121067                return $errors;
     
    10181073         * @since 0.71
    10191074         * @return void
    10201075         */
    1021         function flush() {
     1076        private function flush() {
    10221077                $this->last_result = array();
    10231078                $this->col_info    = null;
    10241079                $this->last_query  = null;
     
    10291084         *
    10301085         * @since 3.0.0
    10311086         */
    1032         function db_connect() {
    1033                 global $db_list, $global_db_list;
    1034 
     1087        private function db_connect() {
    10351088                if ( WP_DEBUG ) {
    10361089                        $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
    10371090                } else {
     
    10751128         * @param string $query Database query
    10761129         * @return int|false Number of rows affected/selected or false on error
    10771130         */
    1078         function query( $query ) {
     1131        public function query( $query ) {
    10791132                if ( ! $this->ready )
    10801133                        return false;
    10811134
     
    11571210         *      A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    11581211         * @return int|false The number of rows inserted, or false on error.
    11591212         */
    1160         function insert( $table, $data, $format = null ) {
     1213        public function insert( $table, $data, $format = null ) {
    11611214                return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
    11621215        }
    11631216
     
    11801233         *      A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    11811234         * @return int|false The number of rows affected, or false on error.
    11821235         */
    1183         function replace( $table, $data, $format = null ) {
     1236        public function replace( $table, $data, $format = null ) {
    11841237                return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
    11851238        }
    11861239
     
    12011254         *      A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    12021255         * @return int|false The number of rows affected, or false on error.
    12031256         */
    1204         function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
     1257        private function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
    12051258                if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
    12061259                        return false;
    12071260                $formats = $format = (array) $format;
     
    12411294         * @param array|string $format_where Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where.  A format is one of '%d', '%s' (decimal number, string).  If omitted, all values in $where will be treated as strings.
    12421295         * @return int|false The number of rows updated, or false on error.
    12431296         */
    1244         function update( $table, $data, $where, $format = null, $where_format = null ) {
     1297        public function update( $table, $data, $where, $format = null, $where_format = null ) {
    12451298                if ( ! is_array( $data ) || ! is_array( $where ) )
    12461299                        return false;
    12471300
     
    12821335         * @since 0.71
    12831336         *
    12841337         * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query.
    1285          * @param int $x Optional. Column of value to return.  Indexed from 0.
    1286          * @param int $y Optional. Row of value to return.  Indexed from 0.
     1338         * @param int $column Optional. Column of value to return.  Indexed from 0.
     1339         * @param int $row Optional. Row of value to return.  Indexed from 0.
    12871340         * @return string|null Database query result (as string), or null on failure
    12881341         */
    1289         function get_var( $query = null, $x = 0, $y = 0 ) {
    1290                 $this->func_call = "\$db->get_var(\"$query\", $x, $y)";
     1342        public function get_var( $query = null, $column = 0, $row = 0 ) {
     1343                $this->func_call = "\$db->get_var(\"$query\", $column, $row)";
    12911344                if ( $query )
    12921345                        $this->query( $query );
    12931346
    12941347                // Extract var out of cached results based x,y vals
    1295                 if ( !empty( $this->last_result[$y] ) ) {
    1296                         $values = array_values( get_object_vars( $this->last_result[$y] ) );
     1348                if ( !empty( $this->last_result[$row] ) ) {
     1349                        $values = array_values( get_object_vars( $this->last_result[$row] ) );
    12971350                }
    12981351
    12991352                // If there is a value return it else return null
    1300                 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null;
     1353                return ( isset( $values[$column] ) && $values[$column] !== '' ) ? $values[$column] : null;
    13011354        }
    13021355
    13031356        /**
     
    13101363         * @param string|null $query SQL query.
    13111364         * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...),
    13121365         *      a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively.
    1313          * @param int $y Optional. Row to return. Indexed from 0.
     1366         * @param int $row Optional. Row to return. Indexed from 0.
    13141367         * @return mixed Database query result in format specifed by $output or null on failure
    13151368         */
    1316         function get_row( $query = null, $output = OBJECT, $y = 0 ) {
    1317                 $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
     1369        public function get_row( $query = null, $output = null, $row = 0 ) {
     1370                $this->func_call = "\$db->get_row(\"$query\",$output,$row)";
    13181371                if ( $query )
    13191372                        $this->query( $query );
    13201373                else
    13211374                        return null;
    13221375
    1323                 if ( !isset( $this->last_result[$y] ) )
     1376                if ( !isset( $this->last_result[$row] ) )
    13241377                        return null;
    13251378
    1326                 if ( $output == OBJECT ) {
    1327                         return $this->last_result[$y] ? $this->last_result[$y] : null;
     1379                if ( null === $output || $output == OBJECT ) {
     1380                        return $this->last_result[$row] ? $this->last_result[$row] : null;
    13281381                } elseif ( $output == ARRAY_A ) {
    1329                         return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
     1382                        return $this->last_result[$row] ? get_object_vars( $this->last_result[$row] ) : null;
    13301383                } elseif ( $output == ARRAY_N ) {
    1331                         return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
     1384                        return $this->last_result[$row] ? array_values( get_object_vars( $this->last_result[$row] ) ) : null;
    13321385                } else {
    13331386                        $this->print_error(/*WP_I18N_DB_GETROW_ERROR*/" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"/*/WP_I18N_DB_GETROW_ERROR*/);
    13341387                }
     
    13441397         * @since 0.71
    13451398         *
    13461399         * @param string|null $query Optional. SQL query. Defaults to previous query.
    1347          * @param int $x Optional. Column to return. Indexed from 0.
     1400         * @param int $column Optional. Column to return. Indexed from 0.
    13481401         * @return array Database query result. Array indexed from 0 by SQL result row number.
    13491402         */
    1350         function get_col( $query = null , $x = 0 ) {
     1403        public function get_col( $query = null , $column = 0 ) {
    13511404                if ( $query )
    13521405                        $this->query( $query );
    13531406
    13541407                $new_array = array();
    13551408                // Extract the column values
    13561409                for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
    1357                         $new_array[$i] = $this->get_var( null, $x, $i );
     1410                        $new_array[$i] = $this->get_var( null, $column, $i );
    13581411                }
    13591412                return $new_array;
    13601413        }
     
    13721425         *      With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value.  Duplicate keys are discarded.
    13731426         * @return mixed Database query results
    13741427         */
    1375         function get_results( $query = null, $output = OBJECT ) {
     1428        public function get_results( $query = null, $output = null ) {
    13761429                $this->func_call = "\$db->get_results(\"$query\", $output)";
    13771430
    13781431                if ( $query )
     
    13811434                        return null;
    13821435
    13831436                $new_array = array();
    1384                 if ( $output == OBJECT ) {
     1437                if ( $output === null || $output == OBJECT ) {
    13851438                        // Return an integer-keyed array of row objects
    13861439                        return $this->last_result;
    13871440                } elseif ( $output == OBJECT_K ) {
     
    14201473         * @param int $col_offset Optional. 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type
    14211474         * @return mixed Column Results
    14221475         */
    1423         function get_col_info( $info_type = 'name', $col_offset = -1 ) {
     1476        public function get_col_info( $info_type = 'name', $col_offset = null ) {
    14241477                if ( $this->col_info ) {
    1425                         if ( $col_offset == -1 ) {
     1478                        if ( $col_offset === null ) {
    14261479                                $i = 0;
    14271480                                $new_array = array();
    14281481                                foreach( (array) $this->col_info as $col ) {
     
    14431496         *
    14441497         * @return true
    14451498         */
    1446         function timer_start() {
    1447                 $mtime            = explode( ' ', microtime() );
    1448                 $this->time_start = $mtime[1] + $mtime[0];
     1499        private function timer_start() {
     1500                $this->time_start = microtime( true );
    14491501                return true;
    14501502        }
    14511503
     
    14561508         *
    14571509         * @return int Total time spent on the query, in milliseconds
    14581510         */
    1459         function timer_stop() {
    1460                 $mtime      = explode( ' ', microtime() );
    1461                 $time_end   = $mtime[1] + $mtime[0];
    1462                 $time_total = $time_end - $this->time_start;
    1463                 return $time_total;
     1511        private function timer_stop() {
     1512                $time_total = microtime( true ) - $this->time_start;
     1513                return (int) $time_total;
    14641514        }
    14651515
    14661516        /**
     
    14741524         * @param string $error_code Optional. A Computer readable string to identify the error.
    14751525         * @return false|void
    14761526         */
    1477         function bail( $message, $error_code = '500' ) {
     1527        private function bail( $message, $error_code = '500' ) {
    14781528                if ( !$this->show_errors ) {
    14791529                        if ( class_exists( 'WP_Error' ) )
    14801530                                $this->error = new WP_Error($error_code, $message);
     
    14941544         *
    14951545         * @return WP_Error
    14961546         */
    1497         function check_database_version() {
     1547        public function check_database_version() {
    14981548                global $wp_version, $required_mysql_version;
    14991549                // Make sure the server has the required MySQL version
    15001550                if ( version_compare($this->db_version(), $required_mysql_version, '<') )
     
    15021552        }
    15031553
    15041554        /**
    1505          * Whether the database supports collation.
    1506          *
    1507          * Called when WordPress is generating the table scheme.
    1508          *
    1509          * @since 2.5.0
    1510          *
    1511          * @return bool True if collation is supported, false if version does not
    1512          */
    1513         function supports_collation() {
    1514                 return $this->has_cap( 'collation' );
    1515         }
    1516 
    1517         /**
    15181555         * Determine if a database supports a particular feature
    15191556         *
    15201557         * @since 2.7.0
    15211558         * @see   wpdb::db_version()
    15221559         *
    1523          * @param string $db_cap the feature
     1560         * @param string $feature the database capabilitiy feature
    15241561         * @return bool
    15251562         */
    1526         function has_cap( $db_cap ) {
     1563        public function has_cap( $feature ) {
    15271564                $version = $this->db_version();
    15281565
    1529                 switch ( strtolower( $db_cap ) ) {
     1566                switch ( strtolower( $feature ) ) {
    15301567                        case 'collation' :    // @since 2.5.0
    15311568                        case 'group_concat' : // @since 2.7
    15321569                        case 'subqueries' :   // @since 2.7
     
    15481585         *
    15491586         * @return string The name of the calling function
    15501587         */
    1551         function get_caller() {
     1588        private function get_caller() {
    15521589                $trace  = array_reverse( debug_backtrace() );
    15531590                $caller = array();
    15541591
     
    15681605         *
    15691606         * @return false|string false on failure, version number on success
    15701607         */
    1571         function db_version() {
     1608        public function db_version() {
    15721609                return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
    15731610        }
    15741611}