Make WordPress Core

Ticket #11644: 11644.code_cleanup_wpdb.2.patch

File 11644.code_cleanup_wpdb.2.patch, 37.5 KB (added by hakre, 15 years ago)
  • wp-db.php

     
    1212/**
    1313 * @since 0.71
    1414 */
    15 define('EZSQL_VERSION', 'WP1.25');
     15define( 'EZSQL_VERSION', 'WP1.25' );
    1616
    1717/**
    1818 * @since 0.71
    1919 */
    20 define('OBJECT', 'OBJECT', true);
     20define( 'OBJECT', 'OBJECT', true );
    2121
    2222/**
    2323 * @since {@internal Version Unknown}}
    2424 */
    25 define('OBJECT_K', 'OBJECT_K', false);
     25define( 'OBJECT_K', 'OBJECT_K', false );
    2626
    2727/**
    2828 * @since 0.71
    2929 */
    30 define('ARRAY_A', 'ARRAY_A', false);
     30define( 'ARRAY_A', 'ARRAY_A', false );
    3131
    3232/**
    3333 * @since 0.71
    3434 */
    35 define('ARRAY_N', 'ARRAY_N', false);
     35define( 'ARRAY_N', 'ARRAY_N', false );
    3636
    3737/**
    3838 * WordPress Database Access Abstraction Object
     
    6464        /**
    6565         * Whether to suppress errors during the DB bootstrapping.
    6666         *
     67         * @since 2.5
    6768         * @access private
    68          * @since {@internal Version Unknown}}
    6969         * @var bool
    7070         */
    7171        var $suppress_errors = false;
     
    7373        /**
    7474         * The last error during query.
    7575         *
    76          * @since {@internal Version Unknown}}
    77          * @var string
     76         * @see get_last_error()
     77         *
     78         * @since  2.5
     79         * @access private
     80         * @var    string
    7881         */
    7982        var $last_error = '';
    8083
     
    8891        var $num_queries = 0;
    8992
    9093        /**
     94         * Amount of rows returned by last query operation
     95         *
     96         * @since  1.2
     97         * @access private
     98         * @var    int
     99         */
     100        var $num_rows = 0;
     101
     102        /**
     103         * Number of affected rows by last query operation
     104         *
     105         * @since  0.71
     106         * @access private
     107         * @var    integer
     108         */
     109        var $rows_affected = 0;
     110
     111
     112        /**
    91113         * Saved result of the last query made
    92114         *
    93115         * @since 1.2.0
     
    97119        var $last_query;
    98120
    99121        /**
     122         * Saved resultset of the last query made
     123         *
     124         * @since {@internal Version Unknown}
     125         * @access private
     126         * @var array (null if unintialized)
     127         */
     128        var $last_result;
     129
     130
     131        /**
    100132         * Saved info on the table column
    101133         *
    102134         * @since 1.2.0
     
    135167         * @var bool
    136168         */
    137169        var $ready = false;
     170       
     171        /**
     172         * {@internal Missing Description}}
     173         *
     174         * @since 3.0
     175         * @access private
     176         * @var unknown_type
     177         */
    138178        var $blogid = 0;
     179       
     180        /**
     181         * {@internal Missing Description}}
     182         *
     183         * @since 3.0
     184         * @access private
     185         * @var unknown_type
     186         */
    139187        var $siteid = 0;
     188       
     189        /**
     190         * {@internal Missing Description}}
     191         *
     192         * @since 3.0
     193         * @access private
     194         * @var unknown_type
     195         */
    140196        var $blogs;
     197       
     198        /**
     199         * {@internal Missing Description}}
     200         *
     201         * @since 3.0
     202         * @access private
     203         * @var unknown_type
     204         */
    141205        var $signups;
     206       
     207        /**
     208         * {@internal Missing Description}}
     209         *
     210         * @since 3.0
     211         * @access private
     212         * @var unknown_type
     213         */
    142214        var $site;
     215       
     216        /**
     217         * {@internal Missing Description}}
     218         *
     219         * @since 3.0
     220         * @access private
     221         * @var unknown_type
     222         */
     223       
     224        /**
     225         * {@internal Missing Description}}
     226         *
     227         * @since 3.0
     228         * @access private
     229         * @var unknown_type
     230         */
    143231        var $sitemeta;
     232       
     233        /**
     234         * {@internal Missing Description}}
     235         *
     236         * @since 3.0
     237         * @access private
     238         * @var unknown_type
     239         */     
    144240        var $sitecategories;
    145         var $global_tables = array('blogs', 'signups', 'site', 'sitemeta', 'users', 'usermeta', 'sitecategories', 'registration_log', 'blog_versions');
     241       
     242        /**
     243         * {@internal Missing Description}}
     244         *
     245         * @since 3.0
     246         * @access private
     247         * @var unknown_type
     248         */     
     249        var $global_tables = array( 'blogs', 'signups', 'site', 'sitemeta', 'users', 'usermeta', 'sitecategories', 'registration_log', 'blog_versions' );
    146250
    147251        /**
    148252         * WordPress Posts table
     
    280384         */
    281385        var $old_tables = array('categories', 'post2cat', 'link2cat');
    282386
    283 
    284387        /**
    285388         * Format specifiers for DB columns. Columns not listed here default to %s.  Initialized in wp-settings.php.
    286389         *
     
    344447         * @param string $dbname MySQL database name
    345448         * @param string $dbhost MySQL database host
    346449         */
    347         function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
     450        function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) {
    348451                if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB )
    349452                        $this->db_connect();
    350453                return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
     
    364467         * @param string $dbname MySQL database name
    365468         * @param string $dbhost MySQL database host
    366469         */
    367         function __construct($dbuser, $dbpassword, $dbname, $dbhost) {
    368                 register_shutdown_function(array(&$this, "__destruct"));
     470        function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {           
     471                register_shutdown_function( array( &$this, '__destruct' ) ); # prevents the object from unloading
    369472
    370473                if ( WP_DEBUG )
    371474                        $this->show_errors();
     
    380483                        $this->collate = DB_COLLATE;
    381484                }
    382485
    383                 if ( defined('DB_CHARSET') )
     486                if ( defined( 'DB_CHARSET' ) )
    384487                        $this->charset = DB_CHARSET;
    385488
    386489                $this->dbuser = $dbuser;
    387490
    388                 $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
    389                 if (!$this->dbh) {
    390                         $this->bail(sprintf(/*WP_I18N_DB_CONN_ERROR*/"
     491                $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true );
     492                if ( !$this->dbh ) {
     493                        $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
    391494<h1>Error establishing a database connection</h1>
    392495<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p>
    393496<ul>
     
    396499        <li>Are you sure that the database server is running?</li>
    397500</ul>
    398501<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
    399 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost), 'db_connect_fail');
     502"/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' );
    400503                        return;
    401504                }
    402505
    403506                $this->ready = true;
    404507
    405                 if ( $this->has_cap( 'collation' ) && !empty($this->charset) ) {
    406                         if ( function_exists('mysql_set_charset') ) {
    407                                 mysql_set_charset($this->charset, $this->dbh);
     508                if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) {
     509                        if ( function_exists( 'mysql_set_charset' ) ) {
     510                                mysql_set_charset( $this->charset, $this->dbh );
    408511                                $this->real_escape = true;
    409512                        } else {
    410                                 $collation_query = "SET NAMES '{$this->charset}'";
    411                                 if ( !empty($this->collate) )
    412                                         $collation_query .= " COLLATE '{$this->collate}'";
    413                                 $this->query($collation_query);
     513                                $query = $this->prepare( 'SET NAMES %s', $this->charset );
     514                                if ( !empty( $this->collate ) )
     515                                        $query .= $this->prepare( ' COLLATE %s', $this->collate );
     516                                $this->query( $query );
    414517                        }
    415518                }
    416519
    417                 $this->select($dbname, $this->dbh);
     520                $this->select( $dbname, $this->dbh );
    418521        }
    419522
    420523        /**
    421524         * PHP5 style destructor and will run when database object is destroyed.
    422525         *
     526         *
     527         *   Please see {@link __construct() class constructor} and the
     528         *   {@link register_shutdown_function() register_shutdown_function()}. for
     529         *   more details.
     530         *
     531         * @link http://www.php.net/__destruct
     532         * @link http://www.php.net/register_shutdown_function
     533         *
    423534         * @since 2.0.8
    424535         *
    425          * @return bool Always true
     536         * @return bool true
    426537         */
    427538        function __destruct() {
    428539                return true;
     
    439550         * @param string $prefix Alphanumeric name for the new prefix.
    440551         * @return string|WP_Error Old prefix or WP_Error on error
    441552         */
    442         function set_prefix($prefix) {
     553        function set_prefix( $prefix ) {
    443554
    444                 if ( preg_match('|[^a-z0-9_]|i', $prefix) )
     555                if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
    445556                        return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
    446557
    447558                if ( is_multisite() )
     
    455566                foreach ( $this->global_tables as $table )
    456567                        $this->$table = $prefix . $table;
    457568
    458                 if ( defined('VHOST') && empty($this->blogid) )
     569                if ( defined( 'VHOST' ) && empty( $this->blogid ) )
    459570                        return $old_prefix;
    460571
    461572                $this->prefix = $this->get_blog_prefix( $this->blogid );
     
    463574                foreach ( (array) $this->tables as $table )
    464575                        $this->$table = $this->prefix . $table;
    465576
    466                 if ( defined('CUSTOM_USER_TABLE') )
     577                if ( defined( 'CUSTOM_USER_TABLE' ) )
    467578                        $this->users = CUSTOM_USER_TABLE;
    468579
    469                 if ( defined('CUSTOM_USER_META_TABLE') )
     580                if ( defined( 'CUSTOM_USER_META_TABLE' ) )
    470581                        $this->usermeta = CUSTOM_USER_META_TABLE;
    471582
    472583                return $old_prefix;
    473584        }
    474585
    475         function set_blog_id($blog_id, $site_id = '') {
     586        /**
     587         * blog id setter
     588         *
     589         * {@internal Missing Description}}
     590         *
     591         * @since 3.0
     592         * @access public
     593         * @param string $blog_id
     594         * @param string $site_id (optional)
     595         * @return string previous blog id
     596         */     
     597        function set_blog_id( $blog_id, $site_id = '' ) {
    476598                if ( !empty($site_id) )
    477599                        $this->siteid = $site_id;
    478600
    479                 $old_blog_id = $this->blogid;
     601                $old_blog_id  = $this->blogid;
    480602                $this->blogid = $blog_id;
    481603
    482604                $this->prefix = $this->get_blog_prefix( $this->blogid );
     
    487609                return $old_blog_id;
    488610        }
    489611
     612        /**
     613         * blog prefix getter
     614         *
     615         * {@internal Missing Description}}
     616         *
     617         * @param string $blog_id (optional)
     618         * @return unknown_type
     619         */
    490620        function get_blog_prefix( $blog_id = '' ) {
    491621                if ( is_multisite() && $blog_id ) {
    492622                        if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) )
     
    509639         * @param string $db MySQL database name
    510640         * @return null Always null.
    511641         */
    512         function select($db, &$dbh) {
    513                 if (!@mysql_select_db($db, $dbh)) {
     642        function select( $db, &$dbh ) {
     643                if ( !@mysql_select_db( $db, $dbh ) ) {
    514644                        $this->ready = false;
    515                         $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
     645                        $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'
    516646<h1>Can&#8217;t select database</h1>
    517647<p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>%1$s</code> database.</p>
    518648<ul>
     
    520650<li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li>
    521651<li>On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?</li>
    522652</ul>
    523 <p>If you don\'t know how to setup a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="http://wordpress.org/support/">WordPress Support Forums</a>.</p>'/*/WP_I18N_DB_SELECT_DB*/, $db, $this->dbuser), 'db_select_fail');
     653<p>If you don\'t know how to setup a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="http://wordpress.org/support/">WordPress Support Forums</a>.</p>'/*/WP_I18N_DB_SELECT_DB*/, $db, $this->dbuser ), 'db_select_fail' );
    524654                        return;
    525655                }
    526656        }
    527657
    528         function _weak_escape($string) {
    529                 return addslashes($string);
     658        /**
     659         * weak escape
     660         *
     661         * an alias to addslashes()
     662         *
     663         * @see addslashes()
     664         *
     665         * @since {@internal Version Unknown}}
     666         * @access private
     667         * @param string $string
     668         * @return string
     669         */
     670        function _weak_escape( $string ) {
     671                return addslashes( $string );
    530672        }
    531673
    532         function _real_escape($string) {
     674        /**
     675         * real escape
     676         *
     677         * escape via mysql_real_escape_string() or addslashes() 
     678         *
     679         * @since  2.8
     680         * @access private
     681         *
     682         * @param  string $string to escape
     683         * @return string escaped
     684         */
     685        function _real_escape( $string ) {
    533686                if ( $this->dbh && $this->real_escape )
    534687                        return mysql_real_escape_string( $string, $this->dbh );
    535688                else
    536689                        return addslashes( $string );
    537690        }
    538691
    539         function _escape($data) {
    540                 if ( is_array($data) ) {
     692        /**
     693         * escape
     694         *
     695         * escape data, uses {@see _real_escape()} and works
     696         * on arrays as well.
     697         *
     698         * @since  2.8
     699         * @access private
     700         *
     701         * @param  string|array $data to escape
     702         * @return string|array escaped
     703         */
     704        function _escape( $data ) {
     705                if ( is_array( $data ) ) {
    541706                        foreach ( (array) $data as $k => $v ) {
    542707                                if ( is_array($v) )
    543708                                        $data[$k] = $this->_escape( $v );
     
    556721         *
    557722         * @since 0.71
    558723         *
    559          * @param string|array $data
    560          * @return string query safe string
     724         * @param  string|array $data to escape
     725         * @return string|array escaped as query safe string
    561726         */
    562         function escape($data) {
    563                 if ( is_array($data) ) {
     727        function escape( $data ) {
     728                if ( is_array( $data ) ) {
    564729                        foreach ( (array) $data as $k => $v ) {
    565                                 if ( is_array($v) )
     730                                if ( is_array( $v ) )
    566731                                        $data[$k] = $this->escape( $v );
    567732                                else
    568733                                        $data[$k] = $this->_weak_escape( $v );
     
    579744         *
    580745         * @since 2.3.0
    581746         *
    582          * @param string $s
     747         * @param  string $string to escape
     748         * @return void
    583749         */
    584         function escape_by_ref(&$string) {
     750        function escape_by_ref( &$string ) {
    585751                $string = $this->_real_escape( $string );
    586752        }
    587753
    588754        /**
    589755         * Prepares a SQL query for safe execution.  Uses sprintf()-like syntax.
    590756         *
     757         * The following directives can be used in the query format string:
     758         *
     759         *   %d (decimal number) 
     760         *   %s (string)
     761         *   %% (literal percentage sign - no argument needed)
     762         *
     763         * Both %d and %s are to be left unquoted in the query string and   
     764         * they need an argument passed for them.
     765         * Literals (%) as parts of the query must be properly written
     766         * as %%.
     767         * 
    591768         * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string).
    592769         * Does not support sign, padding, alignment, width or precision specifiers.
    593770         * Does not support argument numbering/swapping.
     
    598775         *
    599776         * <code>
    600777         * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 )
     778         * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
    601779         * </code>
    602780         *
    603781         * @link http://php.net/sprintf Description of syntax.
    604782         * @since 2.3.0
    605783         *
    606784         * @param string $query Query statement with sprintf()-like placeholders
    607          * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
     785         * @param mixed $args The array of variables to substitute into the query's placeholders if being called like {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
    608786         * @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
    609          * @return null|string Sanitized query string
     787         * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string if there was something to prepare
    610788         */
    611         function prepare($query = null) { // ( $query, *$args )
     789        function prepare( $query = null ) { // ( $query, *$args )
    612790                if ( is_null( $query ) )
    613791                        return;
     792
    614793                $args = func_get_args();
    615                 array_shift($args);
    616                 // If args were passed as an array (as in vsprintf), move them up
    617                 if ( isset($args[0]) && is_array($args[0]) )
    618                         $args = $args[0];
    619                 $query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it
    620                 $query = str_replace('"%s"', '%s', $query); // doublequote unquoting
    621                 $query = str_replace('%s', "'%s'", $query); // quote the strings
    622                 array_walk($args, array(&$this, 'escape_by_ref'));
    623                 return @vsprintf($query, $args);
     794                array_shift( $args );
     795                if ( isset( $args[0] ) && is_array( $args[0] ) )
     796                        $args = $args[0]; # re-assign args passed as array like in vsprintf
     797
     798                $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
     799                $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
     800                $query = str_replace( '%s', "'%s'", $query ); // quote the strings
     801                array_walk( $args, array( &$this, 'escape_by_ref' ) );
     802                return @vsprintf( $query, $args );
    624803        }
    625804
    626805        /**
     
    632811         * @param string $str The error to display
    633812         * @return bool False if the showing of errors is disabled.
    634813         */
    635         function print_error($str = '') {
     814        function print_error( $str = '' ) {
    636815                global $EZSQL_ERROR;
    637816
    638                 if (!$str) $str = mysql_error($this->dbh);
    639                 $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);
     817                if ( !$str ) $str = mysql_error( $this->dbh );
     818                $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
    640819
    641820                if ( $this->suppress_errors )
    642821                        return false;
     
    646825                else
    647826                        $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);
    648827
    649                 $log_error = true;
    650                 if ( ! function_exists('error_log') )
    651                         $log_error = false;
     828                if ( function_exists( 'error_log' )
     829                     && !( $log_file = @ini_get( 'error_log' ) )
     830                     && ( 'syslog' != $log_file )
     831                     && @is_writable( $log_file ) )
     832                    @error_log( $error_str );
    652833
    653                 $log_file = @ini_get('error_log');
    654                 if ( !empty($log_file) && ('syslog' != $log_file) && !@is_writable($log_file) )
    655                         $log_error = false;
    656 
    657                 if ( $log_error )
    658                         @error_log($error_str, 0);
    659 
    660834                // Is error output turned on or not..
    661835                if ( !$this->show_errors )
    662836                        return false;
     
    667841                        if ( defined( 'ERRORLOGFILE' ) )
    668842                                error_log( $msg, 3, ERRORLOGFILE );
    669843                        if ( defined( 'DIEONDBERROR' ) )
    670                                 die( $msg );
     844                                wp_die( $msg );
    671845                } else {
    672                         $str = htmlspecialchars($str, ENT_QUOTES);
    673                         $query = htmlspecialchars($this->last_query, ENT_QUOTES);
     846                        $str   = htmlspecialchars( $str, ENT_QUOTES );
     847                        $query = htmlspecialchars( $this->last_query, ENT_QUOTES );
    674848
    675849                        print "<div id='error'>
    676850                        <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
     
    688862         * errors.
    689863         *
    690864         * @since 0.71
    691          *
     865         * @see hide_errors()
     866         *
    692867         * @param bool $show Whether to show or hide errors
    693868         * @return bool Old value for showing errors.
    694869         */
     
    700875
    701876        /**
    702877         * Disables showing of database errors.
     878         *
     879         * By default database errors are not shown.
    703880         *
    704881         * @since 0.71
     882         * @see show_errors()
    705883         *
    706884         * @return bool Whether showing of errors was active or not
    707885         */
     
    714892        /**
    715893         * Whether to suppress database errors.
    716894         *
    717          * @param unknown_type $suppress
    718          * @return unknown
     895         * By default database errors are suppressed, with a simple
     896         * call to this function they can be enabled.
     897         *
     898         * @since 2.5
     899         * @see hide_errors()
     900         * @param bool $suppress (optional) new value, defaults to true
     901         * @return bool old suppress errors value
    719902         */
    720903        function suppress_errors( $suppress = true ) {
    721904                $errors = $this->suppress_errors;
    722                 $this->suppress_errors = $suppress;
     905                $this->suppress_errors = (bool) $suppress;
    723906                return $errors;
    724907        }
    725908
     
    727910         * Kill cached query results.
    728911         *
    729912         * @since 0.71
     913         *
     914         * @return void
    730915         */
    731916        function flush() {
    732917                $this->last_result = array();
    733                 $this->col_info = null;
    734                 $this->last_query = null;
     918                $this->col_info    = null;
     919                $this->last_query  = null;
    735920        }
    736921
    737922        function db_connect( $query = "SELECT" ) {
     
    754939                }
    755940
    756941                $dbhname = "dbh" . $action;
    757                 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
     942                $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );           
    758943                if (!$this->$dbhname ) {
    759                         $this->bail("
     944                        $this->bail( sprintf( /*WP_I18N_DB_CONNECT_DB*/'
    760945<h1>Error establishing a database connection</h1>
    761 <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
     946<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can\'t contact the database server at <code>%s</code>. This could mean your host\'s database server is down.</p>
    762947<ul>
    763948        <li>Are you sure you have the correct username and password?</li>
    764949        <li>Are you sure that you have typed the correct hostname?</li>
    765950        <li>Are you sure that the database server is running?</li>
    766951</ul>
    767 <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
    768 ");
     952<p>If you\'re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="http://wordpress.org/support/">WordPress Support Forums</a>.</p>'/*/WP_I18N_DB_CONNECT_DB*/, $dbhost) );
    769953                }
    770954                $this->select( $details[ 'db_name' ], $this->$dbhname );
    771955        }
     
    777961         *
    778962         * @since 0.71
    779963         *
    780          * @param string $query
     964         * @param string $query database query
    781965         * @return int|false Number of rows affected/selected or false on error
    782966         */
    783         function query($query) {
     967        function query( $query ) {
    784968                if ( ! $this->ready )
    785969                        return false;
    786970
    787                 // filter the query, if filters are available
    788                 // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
    789                 if ( function_exists('apply_filters') )
    790                         $query = apply_filters('query', $query);
     971                if ( function_exists( 'apply_filters' ) )
     972                        $query = apply_filters( 'query', $query );
    791973
    792                 // initialise return
     974                // init return values and objects state
    793975                $return_val = 0;
    794976                $this->flush();
    795977
     
    799981                // Keep track of the last query for debug..
    800982                $this->last_query = $query;
    801983
    802                 // Perform the query via std mysql_query function..
    803                 if ( defined('SAVEQUERIES') && SAVEQUERIES )
     984                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    804985                        $this->timer_start();
    805986
     987               
    806988                // use $this->dbh for read ops, and $this->dbhwrite for write ops
    807989                // use $this->dbhglobal for gloal table ops
    808990                unset( $dbh );
     
    8281010                        $this->last_db_used = "other/read";
    8291011                }
    8301012
    831                 $this->result = @mysql_query($query, $dbh);
    832                 ++$this->num_queries;
     1013                // Perform the query via std mysql_query function..
     1014                $this->result = @mysql_query( $query, $dbh );
     1015                $this->num_queries++;
    8331016
    8341017                if ( defined('SAVEQUERIES') && SAVEQUERIES )
    8351018                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
    8361019
    8371020                // If there is an error then take note of it..
    838                 if ( $this->last_error = mysql_error($dbh) ) {
     1021                if ( $this->last_error = mysql_error( $dbh ) ) {
    8391022                        $this->print_error();
    8401023                        return false;
    8411024                }
    8421025
    843                 if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {
    844                         $this->rows_affected = mysql_affected_rows($dbh);
     1026                if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
     1027                        $this->rows_affected = mysql_affected_rows( $dbh );
    8451028                        // Take note of the insert_id
    846                         if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
     1029                        if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
    8471030                                $this->insert_id = mysql_insert_id($dbh);
    8481031                        }
    8491032                        // Return number of rows affected
    8501033                        $return_val = $this->rows_affected;
    8511034                } else {
    8521035                        $i = 0;
    853                         while ($i < @mysql_num_fields($this->result)) {
    854                                 $this->col_info[$i] = @mysql_fetch_field($this->result);
     1036                        while ( $i < @mysql_num_fields( $this->result ) ) {
     1037                                $this->col_info[$i] = @mysql_fetch_field( $this->result );
    8551038                                $i++;
    8561039                        }
    8571040                        $num_rows = 0;
    858                         while ( $row = @mysql_fetch_object($this->result) ) {
     1041                        while ( $row = @mysql_fetch_object( $this->result ) ) {
    8591042                                $this->last_result[$num_rows] = $row;
    8601043                                $num_rows++;
    8611044                        }
    8621045
    863                         @mysql_free_result($this->result);
     1046                        @mysql_free_result( $this->result );
    8641047
    8651048                        // Log number of rows the query returned
     1049                        // and Return number of rows selected
    8661050                        $this->num_rows = $num_rows;
    867 
    868                         // Return number of rows selected
    869                         $return_val = $this->num_rows;
     1051                        $return_val     = $num_rows;
    8701052                }
    8711053
    8721054                return $return_val;
     
    8761058         * Insert a row into a table.
    8771059         *
    8781060         * <code>
     1061         * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) )
    8791062         * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
    8801063         * </code>
    8811064         *
     
    8871070         * @param array|string $format (optional) An array of formats to be mapped to each of the value in $data.  If string, that format will be used for all of the values in $data.  A format is one of '%d', '%s' (decimal number, string).  If omitted, all values in $data will be treated as strings.
    8881071         * @return int|false The number of rows inserted, or false on error.
    8891072         */
    890         function insert($table, $data, $format = null) {
     1073        function insert( $table, $data, $format = null ) {
    8911074                $formats = $format = (array) $format;
    892                 $fields = array_keys($data);
     1075                $fields = array_keys( $data );
    8931076                $formatted_fields = array();
    8941077                foreach ( $fields as $field ) {
    895                         if ( !empty($format) )
    896                                 $form = ( $form = array_shift($formats) ) ? $form : $format[0];
    897                         elseif ( isset($this->field_types[$field]) )
     1078                        if ( !empty( $format ) )
     1079                                $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
     1080                        elseif ( isset( $this->field_types[$field] ) )
    8981081                                $form = $this->field_types[$field];
    8991082                        else
    9001083                                $form = '%s';
    9011084                        $formatted_fields[] = $form;
    9021085                }
    9031086                $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
    904                 return $this->query( $this->prepare( $sql, $data) );
     1087                return $this->query( $this->prepare( $sql, $data ) );
    9051088        }
    9061089
    9071090
     
    9091092         * Update a row in the table
    9101093         *
    9111094         * <code>
     1095         * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) )
    9121096         * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
    9131097         * </code>
    9141098         *
     
    9221106         * @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.
    9231107         * @return int|false The number of rows updated, or false on error.
    9241108         */
    925         function update($table, $data, $where, $format = null, $where_format = null) {
    926                 if ( !is_array( $where ) )
     1109        function update( $table, $data, $where, $format = null, $where_format = null ) {
     1110                if ( !is_array( $data ) || !is_array( $where ) )
    9271111                        return false;
    9281112
    9291113                $formats = $format = (array) $format;
    9301114                $bits = $wheres = array();
    931                 foreach ( (array) array_keys($data) as $field ) {
    932                         if ( !empty($format) )
    933                                 $form = ( $form = array_shift($formats) ) ? $form : $format[0];
     1115                foreach ( (array) array_keys( $data ) as $field ) {
     1116                        if ( !empty( $format ) )
     1117                                $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    9341118                        elseif ( isset($this->field_types[$field]) )
    9351119                                $form = $this->field_types[$field];
    9361120                        else
     
    9391123                }
    9401124
    9411125                $where_formats = $where_format = (array) $where_format;
    942                 foreach ( (array) array_keys($where) as $field ) {
    943                         if ( !empty($where_format) )
    944                                 $form = ( $form = array_shift($where_formats) ) ? $form : $where_format[0];
    945                         elseif ( isset($this->field_types[$field]) )
     1126                foreach ( (array) array_keys( $where ) as $field ) {
     1127                        if ( !empty( $where_format ) )
     1128                                $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
     1129                        elseif ( isset( $this->field_types[$field] ) )
    9461130                                $form = $this->field_types[$field];
    9471131                        else
    9481132                                $form = '%s';
     
    9501134                }
    9511135
    9521136                $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
    953                 return $this->query( $this->prepare( $sql, array_merge(array_values($data), array_values($where))) );
     1137                return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
    9541138        }
    9551139
    9561140        /**
     
    9621146         *
    9631147         * @since 0.71
    9641148         *
    965          * @param string|null $query SQL query.  If null, use the result from the previous query.
     1149         * @param string|null $query (optional) SQL query. Defaults to NULL, use the result from the previous query.
    9661150         * @param int $x (optional) Column of value to return.  Indexed from 0.
    9671151         * @param int $y (optional) Row of value to return.  Indexed from 0.
    968          * @return string Database query result
     1152         * @return string|null Database query result on success (as string), NULL if a value on failure
    9691153         */
    970         function get_var($query=null, $x = 0, $y = 0) {
    971                 $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
     1154        function get_var( $query = null, $x = 0, $y = 0 ) {
     1155                $this->func_call = "\$db->get_var(\"$query\", $x, $y)";
    9721156                if ( $query )
    973                         $this->query($query);
     1157                        $this->query( $query );
    9741158
    9751159                // Extract var out of cached results based x,y vals
    9761160                if ( !empty( $this->last_result[$y] ) ) {
    977                         $values = array_values(get_object_vars($this->last_result[$y]));
     1161                        $values = array_values( get_object_vars( $this->last_result[$y] ) );
    9781162                }
    9791163
    9801164                // If there is a value return it else return null
    981                 return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
     1165                return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null;
    9821166        }
    9831167
    9841168        /**
     
    9911175         * @param string|null $query SQL query.
    9921176         * @param string $output (optional) one of ARRAY_A | ARRAY_N | OBJECT constants.  Return an associative array (column => value, ...), a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively.
    9931177         * @param int $y (optional) Row to return.  Indexed from 0.
    994          * @return mixed Database query result in format specifed by $output
     1178         * @return mixed Database query result in format specifed by $output or NULL
     1179         * @return void
    9951180         */
    996         function get_row($query = null, $output = OBJECT, $y = 0) {
     1181        function get_row( $query = null, $output = OBJECT, $y = 0 ) {
    9971182                $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
    9981183                if ( $query )
    999                         $this->query($query);
     1184                        $this->query( $query );
    10001185                else
    10011186                        return null;
    10021187
    1003                 if ( !isset($this->last_result[$y]) )
     1188                if ( !isset( $this->last_result[$y] ) )
    10041189                        return null;
    10051190
    10061191                if ( $output == OBJECT ) {
    10071192                        return $this->last_result[$y] ? $this->last_result[$y] : null;
    10081193                } elseif ( $output == ARRAY_A ) {
    1009                         return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
     1194                        return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
    10101195                } elseif ( $output == ARRAY_N ) {
    1011                         return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
     1196                        return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
    10121197                } else {
    10131198                        $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*/);
    10141199                }
     
    10231208         *
    10241209         * @since 0.71
    10251210         *
    1026          * @param string|null $query SQL query. If null, use the result from the previous query.
    1027          * @param int $x Column to return.  Indexed from 0.
     1211         * @param string|null $query (optional) SQL query. If null, use the result from the previous query.
     1212         * @param int $x (optional) Column to return.  Indexed from 0.
    10281213         * @return array Database query result.  Array indexed from 0 by SQL result row number.
    10291214         */
    1030         function get_col($query = null , $x = 0) {
     1215        function get_col( $query = null , $x = 0 ) {
    10311216                if ( $query )
    1032                         $this->query($query);
     1217                        $this->query( $query );
    10331218
    10341219                $new_array = array();
    10351220                // Extract the column values
    1036                 for ( $i=0; $i < count($this->last_result); $i++ ) {
    1037                         $new_array[$i] = $this->get_var(null, $x, $i);
     1221                for ( $i=0; $i < count( $this->last_result ); $i++ ) {
     1222                        $new_array[$i] = $this->get_var( null, $x, $i );
    10381223                }
    10391224                return $new_array;
    10401225        }
     
    10501235         * @param string $output (optional) ane of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants.  With one of the first three, return an array of rows indexed from 0 by SQL result row number.  Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively.  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.
    10511236         * @return mixed Database query results
    10521237         */
    1053         function get_results($query = null, $output = OBJECT) {
     1238        function get_results( $query = null, $output = OBJECT ) {
    10541239                $this->func_call = "\$db->get_results(\"$query\", $output)";
    10551240
    10561241                if ( $query )
    1057                         $this->query($query);
     1242                        $this->query( $query );
    10581243                else
    10591244                        return null;
    10601245
     
    10951280         *
    10961281         * @since 0.71
    10971282         *
    1098          * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
    1099          * @param int $col_offset 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
     1283         * @param string $info_(optional) type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
     1284         * @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
    11001285         * @return mixed Column Results
    11011286         */
    1102         function get_col_info($info_type = 'name', $col_offset = -1) {
     1287        function get_col_info( $info_type = 'name', $col_offset = -1 ) {
    11031288                if ( $this->col_info ) {
    11041289                        if ( $col_offset == -1 ) {
    1105                                 $i = 0;
     1290                                $i         = 0;
    11061291                                $new_array = array();
    11071292                                foreach( (array) $this->col_info as $col ) {
    11081293                                        $new_array[$i] = $col->{$info_type};
     
    11231308         * @return true
    11241309         */
    11251310        function timer_start() {
    1126                 $mtime = microtime();
    1127                 $mtime = explode(' ', $mtime);
     1311                $mtime            = explode( ' ', microtime() );
    11281312                $this->time_start = $mtime[1] + $mtime[0];
    11291313                return true;
    11301314        }
     
    11371321         * @return int Total time spent on the query, in milliseconds
    11381322         */
    11391323        function timer_stop() {
    1140                 $mtime = microtime();
    1141                 $mtime = explode(' ', $mtime);
    1142                 $time_end = $mtime[1] + $mtime[0];
     1324                $mtime      = explode( ' ', microtime() );
     1325                $time_end   = $mtime[1] + $mtime[0];
    11431326                $time_total = $time_end - $this->time_start;
    11441327                return $time_total;
    11451328        }
     
    11551338         * @param string $error_code (optional) A Computer readable string to identify the error.
    11561339         * @return false|void
    11571340         */
    1158         function bail($message, $error_code = '500') {
     1341        function bail( $message, $error_code = '500' ) {
    11591342                if ( !$this->show_errors ) {
    1160                         if ( class_exists('WP_Error') )
     1343                        if ( class_exists( 'WP_Error' ) )
    11611344                                $this->error = new WP_Error($error_code, $message);
    11621345                        else
    11631346                                $this->error = $message;
     
    11711354         *
    11721355         * @since 2.5.0
    11731356         * @uses $wp_version
     1357         * @uses $required_mysql_version
    11741358         *
    11751359         * @return WP_Error
    11761360         */
     
    11781362                global $wp_version, $required_mysql_version;
    11791363                // Make sure the server has the required MySQL version
    11801364                if ( version_compare($this->db_version(), $required_mysql_version, '<') )
    1181                         return new WP_Error('database_version',sprintf(__('<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher'), $wp_version, $required_mysql_version));
     1365                        return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ));
    11821366        }
    11831367
    11841368        /**
     
    11951379        }
    11961380
    11971381        /**
    1198          * Generic function to determine if a database supports a particular feature
     1382         * has capability
     1383         *
     1384         * Determine if a database supports a particular feature
     1385         *
     1386         * @since  2.7
     1387         * @see    db_version()
     1388         *
    11991389         * @param string $db_cap the feature
    12001390         * @param false|string|resource $dbh_or_table (not implemented) Which database to test.  False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
    12011391         * @return bool
     
    12071397                case 'collation' :    // @since 2.5.0
    12081398                case 'group_concat' : // @since 2.7
    12091399                case 'subqueries' :   // @since 2.7
    1210                         return version_compare($version, '4.1', '>=');
     1400                        return version_compare( $version, '4.1', '>=' );
    12111401                        break;
    12121402                endswitch;
    12131403
     
    12251415         * @return string The name of the calling function
    12261416         */
    12271417        function get_caller() {
    1228                 $bt = debug_backtrace();
     1418                $trace  = array_reverse( debug_backtrace() );
    12291419                $caller = array();
    12301420
    1231                 $bt = array_reverse( $bt );
    1232                 foreach ( (array) $bt as $call ) {
     1421                foreach ( $trace as $call ) {
    12331422                        if ( isset( $call['class'] ) && __CLASS__ == $call['class'] )
    1234                                 continue;
     1423                                continue; # filter out function calls of this object's class
     1424
    12351425                        $function = $call['function'];
     1426
    12361427                        if ( isset( $call['class'] ) )
    12371428                                $function = $call['class'] . "->$function";
     1429
    12381430                        $caller[] = $function;
    12391431                }
    1240                 $caller = join( ', ', $caller );
    12411432
    1242                 return $caller;
     1433                return join( ', ', $caller );
    12431434        }
    12441435
    12451436        /**
    1246          * The database version number
    1247          * @param false|string|resource $dbh_or_table (not implemented) Which database to test.  False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
     1437         * get database version number
     1438         *
     1439         * ADDITIONAL PARAMETER NOTICE
     1440         *
     1441         * there once was a proposed second parameter which has never been
     1442         * implemented. It was describben as "Which database to test" ($dbh_or_table)
     1443         *
     1444         * It would have had three different types:
     1445         *
     1446         *      false    : currently selected database
     1447         *  string   : database containing this table
     1448         *  resource : database by mysql resource
     1449         *
     1450         * regarding that third parameter please see {@see db_version()} as well
     1451         *
     1452         * @since  2.7
     1453         * @see    has_cap()
     1454         *
    12481455         * @return false|string false on failure, version number on success
    12491456         */
    12501457        function db_version() {
    1251                 return preg_replace('/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));
     1458                return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
    12521459        }
    12531460}
    12541461
     
    12601467         */
    12611468        $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    12621469}
    1263 ?>
     1470?>
     1471 No newline at end of file