Make WordPress Core

Ticket #11644: 11644.code_cleanup_wpdb-2nd_iteration.patch

File 11644.code_cleanup_wpdb-2nd_iteration.patch, 18.9 KB (added by hakre, 15 years ago)

In for the next iteration

  • wp-db.php

     
    9393         * Count of rows returned by previous query
    9494         *
    9595         * @since 1.2
     96         * @access private
    9697         * @var int
    9798         */
    9899        var $num_rows = 0;
     
    101102         * Count of affected rows by previous query
    102103         *
    103104         * @since 0.71
     105         * @access private
    104106         * @var int
    105107         */
    106108        var $rows_affected = 0;
     
    118120         * Results of the last query made
    119121         *
    120122         * @since {@internal Version Unknown}}
    121          * @var mixed
     123         * @access private
     124         * @var array|null
    122125         */
    123126        var $last_result;
    124127
     
    166169         * {@internal Missing Description}}
    167170         *
    168171         * @since 3.0.0
     172         * @access private
    169173         * @var int
    170174         */
    171175        var $blogid = 0;
     
    174178         * {@internal Missing Description}}
    175179         *
    176180         * @since 3.0.0
     181         * @access private
    177182         * @var int
    178183         */
    179184        var $siteid = 0;
     
    290295
    291296        /**
    292297         * List of deprecated WordPress tables
     298         *
     299         * - categories, post2cat and link2cat are deprecated
     300         *   since 2.3.0 / db version >= 5539.
    293301         *
    294          * @deprecated
    295302         * @since 2.9.0
    296303         * @access private
    297304         * @see wpdb::tables()
     
    460467         * the actual setting up of the class properties and connection
    461468         * to the database.
    462469         *
     470     * NOTE: register_shutdown_function prevents this object from unloading
     471     *
    463472         * @since 2.0.8
    464473         *
    465474         * @param string $dbuser MySQL database user
     
    523532        /**
    524533         * PHP5 style destructor and will run when database object is destroyed.
    525534         *
     535         *
     536         *   Please see {@link __construct() class constructor} and the
     537         *   {@link register_shutdown_function() register_shutdown_function()}. for
     538         *   more details.
     539         *
     540         * @link http://www.php.net/__destruct
     541         * @link http://www.php.net/register_shutdown_function
     542         *
    526543         * @since 2.0.8
    527544         * @return bool true
    528545         */
     
    547564
    548565                if ( isset( $this->base_prefix ) )
    549566                        $old_prefix = $this->base_prefix;
     567
    550568                $this->base_prefix = $prefix;
     569
    551570                foreach ( $this->tables( 'global' ) as $table => $prefixed_table )
    552571                        $this->$table = $prefixed_table;
    553572
     
    556575
    557576                $this->prefix = $this->get_blog_prefix( $this->blogid );
    558577
    559                 foreach ( (array) $this->tables( 'blog' ) as $table => $prefixed_table )
     578                foreach ( $this->tables( 'blog+old' ) as $table => $prefixed_table )
    560579                        $this->$table = $prefixed_table;
    561580
    562                 foreach ( (array) $this->tables( 'old' ) as $table => $prefixed_table )
    563                         $this->$table = $prefixed_table;
    564 
    565581                return $old_prefix;
    566582        }
    567583
     
    571587         * @since 3.0.0
    572588         * @access public
    573589         * @param string $blog_id
    574          * @param string $site_id. Optional.
     590         * @param string $site_id Optional.
    575591         * @return string previous blog id
    576592         */
    577593        function set_blog_id( $blog_id, $site_id = '' ) {
     
    583599
    584600                $this->prefix = $this->get_blog_prefix( $this->blogid );
    585601
    586                 foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
     602                foreach ( $this->tables( 'blog+old' ) as $table => $prefixed_table )
    587603                        $this->$table = $prefixed_table;
    588604
    589                 foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
    590                         $this->$table = $prefixed_table;
    591 
    592605                return $old_blog_id;
    593606        }
    594607
     
    597610         *
    598611         * @uses is_multisite()
    599612         * @since 3.0.0
    600          * @param int $blog_id. Optional.
     613         * @param int $blog_id Optional.
    601614         * @return string Blog prefix.
    602615         */
    603616        function get_blog_prefix( $blog_id = 0 ) {
    604617                if ( is_multisite() && $blog_id ) {
    605                         if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) )
     618                        if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) )
    606619                                return $this->base_prefix;
    607620                        else
    608621                                return $this->base_prefix . $blog_id . '_';
     
    618631         * override the WordPress users and usersmeta tables that would otherwise
    619632         * be determined by the prefix.
    620633         *
     634         * Scope / Table identifiers:
     635         *   all ........ global and blog tables.
     636         *   blog ....... blog tables
     637         *   blog+old ... blog and old (deprecated) tables
     638         *   global ..... global tables
     639         *   old ........ old (depreacted) tables
     640         *
     641         * @access public
    621642         * @since 3.0.0
    622          * @uses wpdb::tables
    623          * @uses wpdb::old_tables
    624          * @uses wpdb::global_tables
    625          * @uses wpdb::ms_global_tables
     643         * @uses wpdb::$tables
     644         * @uses wpdb::$old_tables
     645         * @uses wpdb::$global_tables
     646         * @uses wpdb::$ms_global_tables
    626647         * @uses is_multisite()
    627648         *
    628          * @param string $scope Can be all, global, blog, or old tables. Default all.
    629          *      All returns the blog tables for the queried blog and all global tables.
    630          * @param bool $prefix Whether to include table prefixes. Default true. If blog
     649         * @param string|array $scope Optional. Scope, Default all. You can pass multiple scopes per array
     650         * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog
    631651         *      prefix is requested, then the custom users and usermeta tables will be mapped.
    632652         * @param int $blog_id The blog_id to prefix. Defaults to main blog. Used only when prefix is requested.
    633          * @return array Table names. When a prefix is requested, the key is the
    634          *      unprefixed table name.
     653         * @return array Table names.
    635654         */
    636655        function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
    637                 switch ( $scope ) {
    638                         case 'old' :
    639                                 $tables = $this->old_tables;
    640                                 break;
    641                         case 'blog' :
    642                                 $tables = $this->tables;
    643                                 break;
    644                         case 'global' :
    645                                 $tables = $this->global_tables;
    646                                 if ( is_multisite() )
    647                                         $tables = array_merge( $tables, $this->ms_global_tables );
    648                                 break;
    649                         case 'all' :
    650                                 $tables = array_merge( $this->global_tables, $this->tables );
    651                                 if ( is_multisite() )
    652                                         $tables = array_merge( $tables, $this->ms_global_tables );
    653                                 break;
     656               
     657                if ( is_array( $scope ) ) {
     658                        $tables = array();
     659                        foreach ( $scope as $each )
     660                                $tables = array_merge( $tables, $this->tables( $each ) );
     661                } else {
     662                        switch ( $scope ) {
     663                                case 'old' :
     664                                        $tables = $this->old_tables;
     665                                        break;
     666                                case 'blog' :
     667                                        $tables = $this->tables;
     668                                        break;
     669                                case 'blog+old':
     670                                        $tables = array_merge( $this->tables, $this->old_tables );
     671                                        break;         
     672                                case 'global' :
     673                                        $tables = $this->global_tables;
     674                                        if ( is_multisite() )
     675                                                $tables = array_merge( $tables, $this->ms_global_tables );
     676                                        break;
     677                                case 'all' :
     678                                        $tables = array_merge( $this->global_tables, $this->tables );
     679                                        if ( is_multisite() )
     680                                                $tables = array_merge( $tables, $this->ms_global_tables );
     681                                        break;
     682                        }
    654683                }
    655684
    656                 if ( $prefix ) {
    657                         $prefix = $this->get_blog_prefix( $blog_id );
    658                         $base_prefix = $this->base_prefix;
     685                if ( count( $tables ) && $prefix ) {
     686                        $blog_prefix  = $this->get_blog_prefix( $blog_id );
     687                        $base_prefix   = $this->base_prefix;
    659688                        $global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
    660689                        foreach ( $tables as $k => $table ) {
     690                                unset( $tables[ $k ] );
    661691                                if ( in_array( $table, $global_tables ) )
    662692                                        $tables[ $table ] = $base_prefix . $table;
    663693                                else
    664                                         $tables[ $table ] = $prefix . $table;
    665                                 unset( $tables[ $k ] );
     694                                        $tables[ $table ] = $blog_prefix . $table;
    666695                        }
    667696
    668697                        if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
     
    705734        /**
    706735         * Weak escape
    707736         *
     737         * An alias to addslashes().
     738         *
    708739         * @see addslashes()
    709          * @since  unknown
     740         * @since {@internal Version Unknown}}
    710741         * @access private
    711742         *
    712743         * @param  string $string
     
    719750        /**
    720751         * Real escape
    721752         *
     753         * Escape via mysql_real_escape_string() or addslashes() 
     754         *
    722755         * @see mysql_real_escape_string()
    723756         * @see addslashes()
    724757         * @since 2.8
     
    737770        /**
    738771         * Escape data.
    739772         *
    740          * @see esc_sql()
     773         * escape data, uses {@see _real_escape()} and works
     774         * on arrays as well.
     775         *
    741776         * @since  2.8
    742777         * @access private
    743778         *
     
    816851         *
    817852         * <code>
    818853         * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 )
     854         * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
    819855         * </code>
    820856         *
    821857         * @link http://php.net/sprintf Description of syntax.
     
    833869        function prepare( $query = null ) { // ( $query, *$args )
    834870                if ( is_null( $query ) )
    835871                        return;
     872
    836873                $args = func_get_args();
    837874                array_shift( $args );
    838875                // If args were passed as an array (as in vsprintf), move them up
     
    869906                else
    870907                        $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);
    871908
    872                 $log_error = true;
    873                 if ( ! function_exists('error_log') )
    874                         $log_error = false;
     909                if ( function_exists( 'error_log' )
     910                     && ! ( $log_file = @ini_get( 'error_log' ) )
     911                     && ( 'syslog' != $log_file )
     912                     && @is_writable( $log_file ) )
     913                    @error_log( $error_str );
    875914
    876                 $log_file = @ini_get('error_log');
    877                 if ( !empty($log_file) && ('syslog' != $log_file) && !@is_writable($log_file) )
    878                         $log_error = false;
    879 
    880                 if ( $log_error )
    881                         @error_log($error_str, 0);
    882 
    883                 // Is error output turned on or not..
    884                 if ( !$this->show_errors )
     915                // Show Errors ?
     916                if ( ! $this->show_errors )
    885917                        return false;
    886918
    887919                // If there is an error then take note of it
     
    890922                        if ( defined( 'ERRORLOGFILE' ) )
    891923                                error_log( $msg, 3, ERRORLOGFILE );
    892924                        if ( defined( 'DIEONDBERROR' ) )
    893                                 die( $msg );
     925                                wp_die( $msg );
    894926                } else {
    895927                        $str   = htmlspecialchars( $str, ENT_QUOTES );
    896928                        $query = htmlspecialchars( $this->last_query, ENT_QUOTES );
     
    10171049                if ( ! $this->ready )
    10181050                        return false;
    10191051
    1020                 // filter the query, if filters are available
    1021                 // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
    10221052                if ( function_exists( 'apply_filters' ) )
    10231053                        $query = apply_filters( 'query', $query );
    10241054
    1025                 // initialize return
    10261055                $return_val = 0;
    10271056                $this->flush();
    10281057
     
    10611090                }
    10621091
    10631092                $this->result = @mysql_query( $query, $dbh );
    1064                 ++$this->num_queries;
     1093                $this->num_queries++;
    10651094
    1066                 if ( defined('SAVEQUERIES') && SAVEQUERIES )
     1095                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    10671096                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
    10681097
    10691098                // If there is an error then take note of it..
     
    11071136         * Insert a row into a table.
    11081137         *
    11091138         * <code>
     1139         * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) )
    11101140         * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
    11111141         * </code>
    11121142         *
     
    11151145         *
    11161146         * @param string $table table name
    11171147         * @param array $data Data to insert (in column => value pairs).  Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    1118          * @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.
     1148         * @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.
    11191149         * @return int|false The number of rows inserted, or false on error.
    11201150         */
    11211151        function insert( $table, $data, $format = null ) {
     
    11401170         * Update a row in the table
    11411171         *
    11421172         * <code>
     1173         * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) )
    11431174         * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
    11441175         * </code>
    11451176         *
     
    11491180         * @param string $table table name
    11501181         * @param array $data Data to update (in column => value pairs).  Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    11511182         * @param array $where A named array of WHERE clauses (in column => value pairs).  Multiple clauses will be joined with ANDs.  Both $where columns and $where values should be "raw".
    1152          * @param array|string $format (optional) An array of formats to be mapped to each of the values 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.
    1153          * @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.
     1183         * @param array|string $format Optional. An array of formats to be mapped to each of the values 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.
     1184         * @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.
    11541185         * @return int|false The number of rows updated, or false on error.
    11551186         */
    11561187        function update( $table, $data, $where, $format = null, $where_format = null ) {
    1157                 if ( ! is_array( $where ) )
     1188                if ( ! is_array( $data ) || ! is_array( $where ) )
    11581189                        return false;
    11591190
    11601191                $formats = $format = (array) $format;
     
    11931224         *
    11941225         * @since 0.71
    11951226         *
    1196          * @param string|null $query Optional. SQL query. If null (default), uses the result from the previous query.
    1197          * @param int $x (optional) Column of value to return.  Indexed from 0.
    1198          * @param int $y (optional) Row of value to return.  Indexed from 0.
    1199          * @return string|null Database query result, or null on failure
     1227         * @param string|null $query Optional. SQL query. Defaults to NULL, use the result from the previous query.
     1228         * @param int $x Optional. Column of value to return.  Indexed from 0.
     1229         * @param int $y Optional. Row of value to return.  Indexed from 0.
     1230         * @return string|null Database query result (as string), or NULL on failure
    12001231         */
    12011232        function get_var( $query = null, $x = 0, $y = 0 ) {
    12021233                $this->func_call = "\$db->get_var(\"$query\", $x, $y)";
     
    12201251         * @since 0.71
    12211252         *
    12221253         * @param string|null $query SQL query.
    1223          * @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.
    1224          * @param int $y (optional) Row to return.  Indexed from 0.
    1225          * @return mixed Database query result in format specifed by $output or null on failure
     1254         * @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.
     1255         * @param int $y Optional. Row to return.  Indexed from 0.
     1256         * @return mixed Database query result in format specifed by $output or NULL on failure
    12261257         */
    12271258        function get_row( $query = null, $output = OBJECT, $y = 0 ) {
    12281259                $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
     
    12541285         *
    12551286         * @since 0.71
    12561287         *
    1257          * @param string|null $query Optional. SQL query. If null (default), use the result from the previous query.
     1288         * @param string|null $query Optional. SQL query. Defaults to previous query.
    12581289         * @param int $x Optional. Column to return. Indexed from 0.
    12591290         * @return array Database query result. Array indexed from 0 by SQL result row number.
    12601291         */
     
    12781309         * @since 0.71
    12791310         *
    12801311         * @param string $query SQL query.
    1281          * @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.
     1312         * @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.
    12821313         * @return mixed Database query results
    12831314         */
    12841315        function get_results( $query = null, $output = OBJECT ) {
     
    13811412         * @since 1.5.0
    13821413         *
    13831414         * @param string $message The Error message
    1384          * @param string $error_code (optional) A Computer readable string to identify the error.
     1415         * @param string $error_code Optional. A Computer readable string to identify the error.
    13851416         * @return false|void
    13861417         */
    13871418        function bail( $message, $error_code = '500' ) {
     
    14591490         * @return string The name of the calling function
    14601491         */
    14611492        function get_caller() {
    1462                 $bt = debug_backtrace();
     1493                $trace  = array_reverse( debug_backtrace() );
    14631494                $caller = array();
    14641495
    1465                 $bt = array_reverse( $bt );
    1466                 foreach ( (array) $bt as $call ) {
     1496                foreach ( $trace as $call ) {
    14671497                        if ( isset( $call['class'] ) && __CLASS__ == $call['class'] )
    1468                                 continue;
    1469                         $function = $call['function'];
    1470                         if ( isset( $call['class'] ) )
    1471                                 $function = $call['class'] . "->$function";
    1472                         $caller[] = $function;
     1498                                continue; # filter out function calls of this object's class
     1499                        $caller[] = isset( $call['class'] ) ? "{$call['class']}->{$call['function']}" : $call['function'];
    14731500                }
    1474                 $caller = join( ', ', $caller );
    14751501
    1476                 return $caller;
     1502                return join( ', ', $caller );
    14771503        }
    14781504
    14791505        /**
    14801506         * The database version number
    1481          * @param false|string|resource $dbh_or_table. Not implemented.
    1482          *      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.
     1507         *
     1508         * ADDITIONAL PARAMETER NOTICE
     1509         *
     1510         * there once was a proposed second parameter which has never been
     1511         * implemented. It was describben as "Which database to test" ($dbh_or_table)
     1512         *
     1513         * It would have had three different types:
     1514         *
     1515         *      false    : currently selected database
     1516         *  string   : database containing this table
     1517         *  resource : database by mysql resource
     1518         *
     1519         * regarding that third parameter please see {@see db_version()} as well
     1520         *
     1521         * @since  2.7
     1522         * @see    has_cap()
     1523         *
    14831524         * @return false|string false on failure, version number on success
    14841525         */
    14851526        function db_version() {
     
    14951536         */
    14961537        $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    14971538}
    1498 ?>
     1539?>
     1540 No newline at end of file