Make WordPress Core

Ticket #11644: 11644.code_cleanup_wpdb.patch

File 11644.code_cleanup_wpdb.patch, 37.8 KB (added by hakre, 15 years ago)
  • wordpress/wp-includes/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" ) );
    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         * NOTE:
     527         *
     528         *   In difference to the PHP5 behavior of __destruct, this function will
     529         *   be called when the script shuts down. If the garbage collector will
     530         *   remove this object earlier in PHP5, this function will be called prior to
     531         *   script termination.
     532         *
     533         *   Please see {@link __construct() class constructor} and the
     534         *   {@link register_shutdown_function() register_shutdown_function()}. for
     535         *   more details.
     536         *
     537         * @link http://www.php.net/__destruct
     538         * @link http://www.php.net/register_shutdown_function
     539         *
    423540         * @since 2.0.8
    424541         *
    425          * @return bool Always true
     542         * @return void
    426543         */
    427544        function __destruct() {
    428                 return true;
    429545        }
    430546
    431547        /**
     
    439555         * @param string $prefix Alphanumeric name for the new prefix.
    440556         * @return string|WP_Error Old prefix or WP_Error on error
    441557         */
    442         function set_prefix($prefix) {
     558        function set_prefix( $prefix ) {
    443559
    444                 if ( preg_match('|[^a-z0-9_]|i', $prefix) )
     560                if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
    445561                        return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
    446562
    447563                if ( is_multisite() )
     
    455571                foreach ( $this->global_tables as $table )
    456572                        $this->$table = $prefix . $table;
    457573
    458                 if ( defined('VHOST') && empty($this->blogid) )
     574                if ( defined( 'VHOST' ) && empty( $this->blogid ) )
    459575                        return $old_prefix;
    460576
    461577                $this->prefix = $this->get_blog_prefix( $this->blogid );
     
    463579                foreach ( (array) $this->tables as $table )
    464580                        $this->$table = $this->prefix . $table;
    465581
    466                 if ( defined('CUSTOM_USER_TABLE') )
     582                if ( defined( 'CUSTOM_USER_TABLE' ) )
    467583                        $this->users = CUSTOM_USER_TABLE;
    468584
    469                 if ( defined('CUSTOM_USER_META_TABLE') )
     585                if ( defined( 'CUSTOM_USER_META_TABLE' ) )
    470586                        $this->usermeta = CUSTOM_USER_META_TABLE;
    471587
    472588                return $old_prefix;
    473589        }
    474590
    475         function set_blog_id($blog_id, $site_id = '') {
     591        /**
     592         * blog id setter
     593         *
     594         * {@internal Missing Description}}
     595         *
     596         * @since 3.0
     597         * @access public
     598         * @param string $blog_id
     599         * @param string $site_id (optional)
     600         * @return string previous blog id
     601         */     
     602        function set_blog_id( $blog_id, $site_id = '' ) {
    476603                if ( !empty($site_id) )
    477604                        $this->siteid = $site_id;
    478605
    479                 $old_blog_id = $this->blogid;
     606                $old_blog_id  = $this->blogid;
    480607                $this->blogid = $blog_id;
    481608
    482609                $this->prefix = $this->get_blog_prefix( $this->blogid );
     
    487614                return $old_blog_id;
    488615        }
    489616
     617        /**
     618         * blog prefix getter
     619         *
     620         * {@internal Missing Description}}
     621         *
     622         * @param string $blog_id (optional)
     623         * @return unknown_type
     624         */
    490625        function get_blog_prefix( $blog_id = '' ) {
    491626                if ( is_multisite() && $blog_id ) {
    492627                        if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) )
     
    509644         * @param string $db MySQL database name
    510645         * @return null Always null.
    511646         */
    512         function select($db, &$dbh) {
    513                 if (!@mysql_select_db($db, $dbh)) {
     647        function select( $db, &$dbh ) {
     648                if ( !@mysql_select_db( $db, $dbh ) ) {
    514649                        $this->ready = false;
    515                         $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
     650                        $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'
    516651<h1>Can&#8217;t select database</h1>
    517652<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>
    518653<ul>
     
    520655<li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li>
    521656<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>
    522657</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');
     658<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' );
    524659                        return;
    525660                }
    526661        }
    527662
    528         function _weak_escape($string) {
    529                 return addslashes($string);
     663        /**
     664         * weak escape
     665         *
     666         * an alias to addslashes()
     667         *
     668         * @see addslashes()
     669         *
     670         * @since {@internal Version Unknown}}
     671         * @access private
     672         * @param string $string
     673         * @return string
     674         */
     675        function _weak_escape( $string ) {
     676                return addslashes( $string );
    530677        }
    531678
    532         function _real_escape($string) {
     679        /**
     680         * real escape
     681         *
     682         * escape via mysql_real_escape_string() or addslashes() 
     683         *
     684         * @since  2.8
     685         * @access private
     686         *
     687         * @param  string $string to escape
     688         * @return string escaped
     689         */
     690        function _real_escape( $string ) {
    533691                if ( $this->dbh && $this->real_escape )
    534692                        return mysql_real_escape_string( $string, $this->dbh );
    535693                else
    536694                        return addslashes( $string );
    537695        }
    538696
    539         function _escape($data) {
    540                 if ( is_array($data) ) {
     697        /**
     698         * escape
     699         *
     700         * escape data, uses {@see _real_escape()} and works
     701         * on arrays as well.
     702         *
     703         * @since  2.8
     704         * @access private
     705         *
     706         * @param  string|array $data to escape
     707         * @return string|array escaped
     708         */
     709        function _escape( $data ) {
     710                if ( is_array( $data ) ) {
    541711                        foreach ( (array) $data as $k => $v ) {
    542712                                if ( is_array($v) )
    543713                                        $data[$k] = $this->_escape( $v );
     
    556726         *
    557727         * @since 0.71
    558728         *
    559          * @param string|array $data
    560          * @return string query safe string
     729         * @param  string|array $data to escape
     730         * @return string|array escaped as query safe string
    561731         */
    562         function escape($data) {
    563                 if ( is_array($data) ) {
     732        function escape( $data ) {
     733                if ( is_array( $data ) ) {
    564734                        foreach ( (array) $data as $k => $v ) {
    565                                 if ( is_array($v) )
     735                                if ( is_array( $v ) )
    566736                                        $data[$k] = $this->escape( $v );
    567737                                else
    568738                                        $data[$k] = $this->_weak_escape( $v );
     
    579749         *
    580750         * @since 2.3.0
    581751         *
    582          * @param string $s
     752         * @param  string $string to escape
     753         * @return void
    583754         */
    584         function escape_by_ref(&$string) {
     755        function escape_by_ref( &$string ) {
    585756                $string = $this->_real_escape( $string );
    586757        }
    587758
    588759        /**
    589760         * Prepares a SQL query for safe execution.  Uses sprintf()-like syntax.
    590761         *
     762         * The following directives can be used in the query format string:
     763         *
     764         *   %d (decimal number) 
     765         *   %s (string)
     766         *   %% (literal percentage sign - no argument needed)
     767         *
     768         * Both %d and %s are to be left unquoted in the query string and   
     769         * they need an argument passed for them.
     770         * Literals (%) as parts of the query must be properly written
     771         * as %%.
     772         * 
    591773         * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string).
    592774         * Does not support sign, padding, alignment, width or precision specifiers.
    593775         * Does not support argument numbering/swapping.
     
    598780         *
    599781         * <code>
    600782         * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 )
     783         * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
    601784         * </code>
    602785         *
    603786         * @link http://php.net/sprintf Description of syntax.
    604787         * @since 2.3.0
    605788         *
    606789         * @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()}.
     790         * @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()}.
    608791         * @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
     792         * @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
    610793         */
    611         function prepare($query = null) { // ( $query, *$args )
     794        function prepare( $query = null ) { // ( $query, *$args )
    612795                if ( is_null( $query ) )
    613796                        return;
     797
    614798                $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);
     799                array_shift( $args );
     800                if ( isset( $args[0] ) && is_array( $args[0] ) )
     801                        $args = $args[0]; # re-assign args passed as array like in vsprintf
     802
     803                $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
     804                $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
     805                $query = str_replace( '%s', "'%s'", $query ); // quote the strings
     806                array_walk( $args, array( &$this, 'escape_by_ref' ) );
     807                return @vsprintf( $query, $args );
    624808        }
    625809
    626810        /**
     
    632816         * @param string $str The error to display
    633817         * @return bool False if the showing of errors is disabled.
    634818         */
    635         function print_error($str = '') {
     819        function print_error( $str = '' ) {
    636820                global $EZSQL_ERROR;
    637821
    638                 if (!$str) $str = mysql_error($this->dbh);
    639                 $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);
     822                if ( !$str ) $str = mysql_error( $this->dbh );
     823                $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
    640824
    641825                if ( $this->suppress_errors )
    642826                        return false;
     
    646830                else
    647831                        $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);
    648832
    649                 $log_error = true;
    650                 if ( ! function_exists('error_log') )
    651                         $log_error = false;
     833                if ( function_exists( 'error_log' )
     834                     && !( $log_file = @ini_get( 'error_log' ) )
     835                     && ( 'syslog' != $log_file )
     836                     && @is_writable( $log_file ) )
     837                    @error_log( $error_str );
    652838
    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 
    660839                // Is error output turned on or not..
    661840                if ( !$this->show_errors )
    662841                        return false;
     
    667846                        if ( defined( 'ERRORLOGFILE' ) )
    668847                                error_log( $msg, 3, ERRORLOGFILE );
    669848                        if ( defined( 'DIEONDBERROR' ) )
    670                                 die( $msg );
     849                                wp_die( $msg );
    671850                } else {
    672                         $str = htmlspecialchars($str, ENT_QUOTES);
    673                         $query = htmlspecialchars($this->last_query, ENT_QUOTES);
     851                        $str   = htmlspecialchars( $str, ENT_QUOTES );
     852                        $query = htmlspecialchars( $this->last_query, ENT_QUOTES );
    674853
    675854                        print "<div id='error'>
    676855                        <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
     
    688867         * errors.
    689868         *
    690869         * @since 0.71
    691          *
     870         * @see hide_errors()
     871         *
    692872         * @param bool $show Whether to show or hide errors
    693873         * @return bool Old value for showing errors.
    694874         */
     
    700880
    701881        /**
    702882         * Disables showing of database errors.
     883         *
     884         * By default database errors are not shown.
    703885         *
    704886         * @since 0.71
     887         * @see show_errors()
    705888         *
    706889         * @return bool Whether showing of errors was active or not
    707890         */
     
    714897        /**
    715898         * Whether to suppress database errors.
    716899         *
    717          * @param unknown_type $suppress
    718          * @return unknown
     900         * By default database errors are suppressed, with a simple
     901         * call to this function they can be enabled.
     902         *
     903         * @since 2.5
     904         * @see hide_errors()
     905         * @param bool $suppress (optional) new value, defaults to true
     906         * @return bool old suppress errors value
    719907         */
    720908        function suppress_errors( $suppress = true ) {
    721909                $errors = $this->suppress_errors;
    722                 $this->suppress_errors = $suppress;
     910                $this->suppress_errors = (bool) $suppress;
    723911                return $errors;
    724912        }
    725913
     
    727915         * Kill cached query results.
    728916         *
    729917         * @since 0.71
     918         *
     919         * @return void
    730920         */
    731921        function flush() {
    732922                $this->last_result = array();
    733                 $this->col_info = null;
    734                 $this->last_query = null;
     923                $this->col_info    = null;
     924                $this->last_query  = null;
    735925        }
    736926
    737927        function db_connect( $query = "SELECT" ) {
     
    754944                }
    755945
    756946                $dbhname = "dbh" . $action;
    757                 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
     947                $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );           
    758948                if (!$this->$dbhname ) {
    759                         $this->bail("
     949                        $this->bail( sprintf( /*WP_I18N_DB_CONNECT_DB*/'
    760950<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>
     951<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>
    762952<ul>
    763953        <li>Are you sure you have the correct username and password?</li>
    764954        <li>Are you sure that you have typed the correct hostname?</li>
    765955        <li>Are you sure that the database server is running?</li>
    766956</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 ");
     957<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) );
    769958                }
    770959                $this->select( $details[ 'db_name' ], $this->$dbhname );
    771960        }
     
    777966         *
    778967         * @since 0.71
    779968         *
    780          * @param string $query
     969         * @param string $query database query
    781970         * @return int|false Number of rows affected/selected or false on error
    782971         */
    783         function query($query) {
     972        function query( $query ) {
    784973                if ( ! $this->ready )
    785974                        return false;
    786975
    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);
     976                if ( function_exists( 'apply_filters' ) )
     977                        $query = apply_filters( 'query', $query );
    791978
    792                 // initialise return
     979                // init return values and objects state
    793980                $return_val = 0;
    794981                $this->flush();
    795982
     
    799986                // Keep track of the last query for debug..
    800987                $this->last_query = $query;
    801988
    802                 // Perform the query via std mysql_query function..
    803                 if ( defined('SAVEQUERIES') && SAVEQUERIES )
     989                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    804990                        $this->timer_start();
    805991
     992               
    806993                // use $this->dbh for read ops, and $this->dbhwrite for write ops
    807994                // use $this->dbhglobal for gloal table ops
    808995                unset( $dbh );
     
    8281015                        $this->last_db_used = "other/read";
    8291016                }
    8301017
    831                 $this->result = @mysql_query($query, $dbh);
    832                 ++$this->num_queries;
     1018                // Perform the query via std mysql_query function..
     1019                $this->result = @mysql_query( $query, $dbh );
     1020                $this->num_queries++;
    8331021
    8341022                if ( defined('SAVEQUERIES') && SAVEQUERIES )
    8351023                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
    8361024
    8371025                // If there is an error then take note of it..
    838                 if ( $this->last_error = mysql_error($dbh) ) {
     1026                if ( $this->last_error = mysql_error( $dbh ) ) {
    8391027                        $this->print_error();
    8401028                        return false;
    8411029                }
    8421030
    843                 if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {
    844                         $this->rows_affected = mysql_affected_rows($dbh);
     1031                if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
     1032                        $this->rows_affected = mysql_affected_rows( $dbh );
    8451033                        // Take note of the insert_id
    846                         if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
     1034                        if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
    8471035                                $this->insert_id = mysql_insert_id($dbh);
    8481036                        }
    8491037                        // Return number of rows affected
    8501038                        $return_val = $this->rows_affected;
    8511039                } else {
    8521040                        $i = 0;
    853                         while ($i < @mysql_num_fields($this->result)) {
    854                                 $this->col_info[$i] = @mysql_fetch_field($this->result);
     1041                        while ( $i < @mysql_num_fields( $this->result ) ) {
     1042                                $this->col_info[$i] = @mysql_fetch_field( $this->result );
    8551043                                $i++;
    8561044                        }
    8571045                        $num_rows = 0;
    858                         while ( $row = @mysql_fetch_object($this->result) ) {
     1046                        while ( $row = @mysql_fetch_object( $this->result ) ) {
    8591047                                $this->last_result[$num_rows] = $row;
    8601048                                $num_rows++;
    8611049                        }
    8621050
    863                         @mysql_free_result($this->result);
     1051                        @mysql_free_result( $this->result );
    8641052
    8651053                        // Log number of rows the query returned
     1054                        // and Return number of rows selected
    8661055                        $this->num_rows = $num_rows;
    867 
    868                         // Return number of rows selected
    869                         $return_val = $this->num_rows;
     1056                        $return_val     = $num_rows;
    8701057                }
    8711058
    8721059                return $return_val;
     
    8761063         * Insert a row into a table.
    8771064         *
    8781065         * <code>
     1066         * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) )
    8791067         * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
    8801068         * </code>
    8811069         *
     
    8871075         * @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.
    8881076         * @return int|false The number of rows inserted, or false on error.
    8891077         */
    890         function insert($table, $data, $format = null) {
     1078        function insert( $table, $data, $format = null ) {
    8911079                $formats = $format = (array) $format;
    892                 $fields = array_keys($data);
     1080                $fields = array_keys( $data );
    8931081                $formatted_fields = array();
    8941082                foreach ( $fields as $field ) {
    895                         if ( !empty($format) )
    896                                 $form = ( $form = array_shift($formats) ) ? $form : $format[0];
    897                         elseif ( isset($this->field_types[$field]) )
     1083                        if ( !empty( $format ) )
     1084                                $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
     1085                        elseif ( isset( $this->field_types[$field] ) )
    8981086                                $form = $this->field_types[$field];
    8991087                        else
    9001088                                $form = '%s';
    9011089                        $formatted_fields[] = $form;
    9021090                }
    9031091                $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
    904                 return $this->query( $this->prepare( $sql, $data) );
     1092                return $this->query( $this->prepare( $sql, $data ) );
    9051093        }
    9061094
    9071095
     
    9091097         * Update a row in the table
    9101098         *
    9111099         * <code>
     1100         * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) )
    9121101         * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
    9131102         * </code>
    9141103         *
     
    9221111         * @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.
    9231112         * @return int|false The number of rows updated, or false on error.
    9241113         */
    925         function update($table, $data, $where, $format = null, $where_format = null) {
    926                 if ( !is_array( $where ) )
     1114        function update( $table, $data, $where, $format = null, $where_format = null ) {
     1115                if ( !is_array( $data ) || !is_array( $where ) )
    9271116                        return false;
    9281117
    9291118                $formats = $format = (array) $format;
    9301119                $bits = $wheres = array();
    931                 foreach ( (array) array_keys($data) as $field ) {
    932                         if ( !empty($format) )
    933                                 $form = ( $form = array_shift($formats) ) ? $form : $format[0];
     1120                foreach ( (array) array_keys( $data ) as $field ) {
     1121                        if ( !empty( $format ) )
     1122                                $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    9341123                        elseif ( isset($this->field_types[$field]) )
    9351124                                $form = $this->field_types[$field];
    9361125                        else
     
    9391128                }
    9401129
    9411130                $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]) )
     1131                foreach ( (array) array_keys( $where ) as $field ) {
     1132                        if ( !empty( $where_format ) )
     1133                                $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
     1134                        elseif ( isset( $this->field_types[$field] ) )
    9461135                                $form = $this->field_types[$field];
    9471136                        else
    9481137                                $form = '%s';
     
    9501139                }
    9511140
    9521141                $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))) );
     1142                return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
    9541143        }
    9551144
    9561145        /**
     
    9621151         *
    9631152         * @since 0.71
    9641153         *
    965          * @param string|null $query SQL query.  If null, use the result from the previous query.
     1154         * @param string|null $query (optional) SQL query. Defaults to NULL, use the result from the previous query.
    9661155         * @param int $x (optional) Column of value to return.  Indexed from 0.
    9671156         * @param int $y (optional) Row of value to return.  Indexed from 0.
    968          * @return string Database query result
     1157         * @return string|null Database query result on success (as string), NULL if a value on failure
    9691158         */
    970         function get_var($query=null, $x = 0, $y = 0) {
    971                 $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
     1159        function get_var( $query = null, $x = 0, $y = 0 ) {
     1160                $this->func_call = "\$db->get_var(\"$query\", $x, $y)";
    9721161                if ( $query )
    973                         $this->query($query);
     1162                        $this->query( $query );
    9741163
    9751164                // Extract var out of cached results based x,y vals
    9761165                if ( !empty( $this->last_result[$y] ) ) {
    977                         $values = array_values(get_object_vars($this->last_result[$y]));
     1166                        $values = array_values( get_object_vars( $this->last_result[$y] ) );
    9781167                }
    9791168
    9801169                // If there is a value return it else return null
    981                 return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
     1170                return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null;
    9821171        }
    9831172
    9841173        /**
     
    9911180         * @param string|null $query SQL query.
    9921181         * @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.
    9931182         * @param int $y (optional) Row to return.  Indexed from 0.
    994          * @return mixed Database query result in format specifed by $output
     1183         * @return mixed Database query result in format specifed by $output or NULL
     1184         * @return void
    9951185         */
    996         function get_row($query = null, $output = OBJECT, $y = 0) {
     1186        function get_row( $query = null, $output = OBJECT, $y = 0 ) {
    9971187                $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
    9981188                if ( $query )
    999                         $this->query($query);
     1189                        $this->query( $query );
    10001190                else
    10011191                        return null;
    10021192
    1003                 if ( !isset($this->last_result[$y]) )
     1193                if ( !isset( $this->last_result[$y] ) )
    10041194                        return null;
    10051195
    10061196                if ( $output == OBJECT ) {
    10071197                        return $this->last_result[$y] ? $this->last_result[$y] : null;
    10081198                } elseif ( $output == ARRAY_A ) {
    1009                         return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
     1199                        return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
    10101200                } elseif ( $output == ARRAY_N ) {
    1011                         return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
     1201                        return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
    10121202                } else {
    10131203                        $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*/);
    10141204                }
     
    10231213         *
    10241214         * @since 0.71
    10251215         *
    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.
     1216         * @param string|null $query (optional) SQL query. If null, use the result from the previous query.
     1217         * @param int $x (optional) Column to return.  Indexed from 0.
    10281218         * @return array Database query result.  Array indexed from 0 by SQL result row number.
    10291219         */
    1030         function get_col($query = null , $x = 0) {
     1220        function get_col( $query = null , $x = 0 ) {
    10311221                if ( $query )
    1032                         $this->query($query);
     1222                        $this->query( $query );
    10331223
    10341224                $new_array = array();
    10351225                // Extract the column values
    1036                 for ( $i=0; $i < count($this->last_result); $i++ ) {
    1037                         $new_array[$i] = $this->get_var(null, $x, $i);
     1226                for ( $i=0; $i < count( $this->last_result ); $i++ ) {
     1227                        $new_array[$i] = $this->get_var( null, $x, $i );
    10381228                }
    10391229                return $new_array;
    10401230        }
     
    10501240         * @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.
    10511241         * @return mixed Database query results
    10521242         */
    1053         function get_results($query = null, $output = OBJECT) {
     1243        function get_results( $query = null, $output = OBJECT ) {
    10541244                $this->func_call = "\$db->get_results(\"$query\", $output)";
    10551245
    10561246                if ( $query )
    1057                         $this->query($query);
     1247                        $this->query( $query );
    10581248                else
    10591249                        return null;
    10601250
     
    10951285         *
    10961286         * @since 0.71
    10971287         *
    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
     1288         * @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
     1289         * @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
    11001290         * @return mixed Column Results
    11011291         */
    1102         function get_col_info($info_type = 'name', $col_offset = -1) {
     1292        function get_col_info( $info_type = 'name', $col_offset = -1 ) {
    11031293                if ( $this->col_info ) {
    11041294                        if ( $col_offset == -1 ) {
    1105                                 $i = 0;
     1295                                $i         = 0;
    11061296                                $new_array = array();
    11071297                                foreach( (array) $this->col_info as $col ) {
    11081298                                        $new_array[$i] = $col->{$info_type};
     
    11231313         * @return true
    11241314         */
    11251315        function timer_start() {
    1126                 $mtime = microtime();
    1127                 $mtime = explode(' ', $mtime);
     1316                $mtime            = explode( ' ', microtime() );
    11281317                $this->time_start = $mtime[1] + $mtime[0];
    11291318                return true;
    11301319        }
     
    11371326         * @return int Total time spent on the query, in milliseconds
    11381327         */
    11391328        function timer_stop() {
    1140                 $mtime = microtime();
    1141                 $mtime = explode(' ', $mtime);
    1142                 $time_end = $mtime[1] + $mtime[0];
     1329                $mtime      = explode( ' ', microtime() );
     1330                $time_end   = $mtime[1] + $mtime[0];
    11431331                $time_total = $time_end - $this->time_start;
    11441332                return $time_total;
    11451333        }
     
    11551343         * @param string $error_code (optional) A Computer readable string to identify the error.
    11561344         * @return false|void
    11571345         */
    1158         function bail($message, $error_code = '500') {
     1346        function bail( $message, $error_code = '500' ) {
    11591347                if ( !$this->show_errors ) {
    1160                         if ( class_exists('WP_Error') )
     1348                        if ( class_exists( 'WP_Error' ) )
    11611349                                $this->error = new WP_Error($error_code, $message);
    11621350                        else
    11631351                                $this->error = $message;
     
    11711359         *
    11721360         * @since 2.5.0
    11731361         * @uses $wp_version
     1362         * @uses $required_mysql_version
    11741363         *
    11751364         * @return WP_Error
    11761365         */
     
    11781367                global $wp_version, $required_mysql_version;
    11791368                // Make sure the server has the required MySQL version
    11801369                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));
     1370                        return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ));
    11821371        }
    11831372
    11841373        /**
     
    11951384        }
    11961385
    11971386        /**
    1198          * Generic function to determine if a database supports a particular feature
     1387         * has capability
     1388         *
     1389         * Determine if a database supports a particular feature
     1390         *
     1391         * @since  2.7
     1392         * @see    db_version()
     1393         *
    11991394         * @param string $db_cap the feature
    12001395         * @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.
    12011396         * @return bool
     
    12071402                case 'collation' :    // @since 2.5.0
    12081403                case 'group_concat' : // @since 2.7
    12091404                case 'subqueries' :   // @since 2.7
    1210                         return version_compare($version, '4.1', '>=');
     1405                        return version_compare( $version, '4.1', '>=' );
    12111406                        break;
    12121407                endswitch;
    12131408
     
    12251420         * @return string The name of the calling function
    12261421         */
    12271422        function get_caller() {
    1228                 $bt = debug_backtrace();
     1423                $trace  = array_reverse( debug_backtrace() );
    12291424                $caller = array();
    12301425
    1231                 $bt = array_reverse( $bt );
    1232                 foreach ( (array) $bt as $call ) {
     1426                foreach ( $trace as $call ) {
    12331427                        if ( isset( $call['class'] ) && __CLASS__ == $call['class'] )
    1234                                 continue;
     1428                                continue; # filter out function calls of this object's class
     1429
    12351430                        $function = $call['function'];
     1431
    12361432                        if ( isset( $call['class'] ) )
    12371433                                $function = $call['class'] . "->$function";
     1434
    12381435                        $caller[] = $function;
    12391436                }
    1240                 $caller = join( ', ', $caller );
    12411437
    1242                 return $caller;
     1438                return join( ', ', $caller );
    12431439        }
    12441440
    12451441        /**
    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.
     1442         * get database version number
     1443         *
     1444         * ADDITIONAL PARAMETER NOTICE
     1445         *
     1446         * there once was a proposed second parameter which has never been
     1447         * implemented. It was describben as "Which database to test" ($dbh_or_table)
     1448         *
     1449         * It would have had three different types:
     1450         *
     1451         *      false    : currently selected database
     1452         *  string   : database containing this table
     1453         *  resource : database by mysql resource
     1454         *
     1455         * regarding that third parameter please see {@see db_version()} as well
     1456         *
     1457         * @since  2.7
     1458         * @see    has_cap()
     1459         *
    12481460         * @return false|string false on failure, version number on success
    12491461         */
    12501462        function db_version() {
    1251                 return preg_replace('/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));
     1463                return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
    12521464        }
    12531465}
    12541466
     
    12601472         */
    12611473        $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    12621474}
    1263 ?>
     1475?>
     1476 No newline at end of file