Make WordPress Core

Ticket #12362: 11644.code_cleanup_wpdb-3nd_iteration.patch

File 11644.code_cleanup_wpdb-3nd_iteration.patch, 10.1 KB (added by nacin, 15 years ago)
  • wordpress/wp-includes/wp-db.php

     
    169169         * {@internal Missing Description}}
    170170         *
    171171         * @since 3.0.0
    172          * @access public
     172         * @access private
    173173         * @var int
    174174         */
    175175        var $blogid = 0;
     
    178178         * {@internal Missing Description}}
    179179         *
    180180         * @since 3.0.0
    181          * @access public
     181         * @access private
    182182         * @var int
    183183         */
    184184        var $siteid = 0;
     
    470470         * the actual setting up of the class properties and connection
    471471         * to the database.
    472472         *
     473     * NOTE: register_shutdown_function prevents this object from unloading
     474     *
    473475         * @since 2.0.8
    474476         *
    475477         * @param string $dbuser MySQL database user
     
    533535        /**
    534536         * PHP5 style destructor and will run when database object is destroyed.
    535537         *
     538         *
     539         *   Please see {@link __construct() class constructor} and the
     540         *   {@link register_shutdown_function() register_shutdown_function()}. for
     541         *   more details.
     542         *
     543         * @link http://www.php.net/__destruct
     544         * @link http://www.php.net/register_shutdown_function
     545         *
    536546         * @since 2.0.8
    537547         * @return bool true
    538548         */
     
    567577                        return $old_prefix;
    568578
    569579                $this->prefix = $this->get_blog_prefix( $this->blogid );
    570 
    571                 foreach ( (array) $this->tables( 'blog' ) as $table => $prefixed_table )
     580               
     581                foreach ( array_merge( $this->tables( 'blog' ), $this->tables( 'old' ) ) as $table => $prefixed_table )
    572582                        $this->$table = $prefixed_table;
    573583
    574                 foreach ( (array) $this->tables( 'old' ) as $table => $prefixed_table )
    575                         $this->$table = $prefixed_table;
    576 
    577584                return $old_prefix;
    578585        }
    579586
     
    586593         * @param int $site_id Optional.
    587594         * @return string previous blog id
    588595         */
    589         function set_blog_id( $blog_id, $site_id = 0 ) {
     596        function set_blog_id( $blog_id, $site_id = '' ) {
    590597                if ( ! empty( $site_id ) )
    591598                        $this->siteid = $site_id;
    592599
     
    594601                $this->blogid = $blog_id;
    595602
    596603                $this->prefix = $this->get_blog_prefix( $this->blogid );
    597 
    598                 foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
     604               
     605                foreach ( array_merge( $this->tables( 'blog' ), $this->tables( 'old' ) ) as $table => $prefixed_table )
    599606                        $this->$table = $prefixed_table;
    600607
    601                 foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
    602                         $this->$table = $prefixed_table;
    603 
    604608                return $old_blog_id;
    605609        }
    606610
     
    630634         * override the WordPress users and usersmeta tables that would otherwise
    631635         * be determined by the prefix.
    632636         *
     637         * Scope / Table identifiers:
     638         *   all ........ global and blog tables.
     639         *   blog ....... blog tables
     640         *   blog+old ... blog and old (deprecated) tables
     641         *   global ..... global tables
     642         *   ms_global .. multisite global tables, regardless if current installation is multisite.
     643         *   old ........ old (depreacted) tables
     644         *
     645         * @access public
    633646         * @since 3.0.0
    634647         * @uses wpdb::$tables
    635648         * @uses wpdb::$old_tables
     
    637650         * @uses wpdb::$ms_global_tables
    638651         * @uses is_multisite()
    639652         *
    640          * @param string $scope Can be all, global, ms_global, blog, or old tables. Defaults to all.
     653         * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
    641654         *      'all' returns 'all' and 'global' tables. No old tables are returned.
    642655         *      'global' returns the global tables for the installation, returning multisite tables only if running multisite.
    643656         *      'ms_global' returns the multisite global tables, regardless if current installation is multisite.
    644657         *      'blog' returns the blog-level tables for the queried blog.
    645658         *      'old' returns tables which are deprecated.
    646          * @param bool $prefix Whether to include table prefixes. Default true. If blog
     659         * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog
    647660         *      prefix is requested, then the custom users and usermeta tables will be mapped.
    648          * @param int $blog_id The blog_id to prefix. Defaults to wpdb::blogid. Used only when prefix is requested.
     661         * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::blogid. Used only when prefix is requested.
    649662         * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
    650663         */
    651664        function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
     665
    652666                switch ( $scope ) {
    653                         case 'old' :
    654                                 $tables = $this->old_tables;
     667                        case 'all' :
     668                                $tables = array_merge( $this->global_tables, $this->tables );
     669                                if ( is_multisite() )
     670                                        $tables = array_merge( $tables, $this->ms_global_tables );
    655671                                break;
    656672                        case 'blog' :
    657673                                $tables = $this->tables;
    658674                                break;
    659                         case 'ms_global' :
    660                                 $tables = $this->ms_global_tables;
     675                        case 'blog+old':
     676                                $tables = array_merge( $this->tables, $this->old_tables );
    661677                                break;
    662678                        case 'global' :
    663679                                $tables = $this->global_tables;
    664680                                if ( is_multisite() )
    665681                                        $tables = array_merge( $tables, $this->ms_global_tables );
    666682                                break;
    667                         case 'all' :
    668                                 $tables = array_merge( $this->global_tables, $this->tables );
    669                                 if ( is_multisite() )
    670                                         $tables = array_merge( $tables, $this->ms_global_tables );
     683                        case 'ms_global' :
     684                                $tables = $this->ms_global_tables;
    671685                                break;
     686                        case 'old' :
     687                                $tables = $this->old_tables;
     688                                break;
     689                        default:
     690                                $tables = array();
    672691                }
    673692
    674                 if ( $prefix ) {
     693                if ( count( $tables ) && $prefix ) {
    675694                        if ( ! $blog_id )
    676695                                $blog_id = $this->blogid;
     696
    677697                        $blog_prefix = $this->get_blog_prefix( $blog_id );
    678698                        $base_prefix = $this->base_prefix;
    679699                        $global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
    680                         foreach ( $tables as $k => $table ) {
     700                        foreach ( $tables as $k => $table ) {                           
    681701                                if ( in_array( $table, $global_tables ) )
    682702                                        $tables[ $table ] = $base_prefix . $table;
    683703                                else
     
    725745        /**
    726746         * Weak escape
    727747         *
    728          * @uses addslashes()
     748         * An alias to addslashes().
     749         *
     750         * @see addslashes()
    729751         * @since {@internal Version Unknown}}
    730752         * @access private
    731753         *
     
    739761        /**
    740762         * Real escape
    741763         *
    742          * @uses mysql_real_escape_string()
    743          * @uses addslashes()
     764         * Escape via mysql_real_escape_string() or addslashes() 
     765         *
     766         * @see mysql_real_escape_string()
     767         * @see addslashes()
    744768         * @since 2.8
    745769         * @access private
    746770         *
     
    786810         * Works on arrays.
    787811         *
    788812         * @since 0.71
    789          * @param  string|array $data to escape
     813         * @param string|array $data to escape
    790814         * @return string|array escaped as query safe string
    791815         */
    792816        function escape( $data ) {
     
    809833         *
    810834         * @uses wpdb::_real_escape()
    811835         * @since 2.3.0
    812          * @param  string $string to escape
     836         * @param string $string to escape
    813837         * @return void
    814838         */
    815839        function escape_by_ref( &$string ) {
     
    837861         * Both %d and %s should be left unquoted in the query string.
    838862         *
    839863         * <code>
    840          * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 )
     864         * wpdb::prepare( 'SELECT * FROM `table` WHERE `column` = %s AND `field` = %d', 'foo', 1337 )
    841865         * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
    842866         * </code>
    843867         *
     
    864888                        $args = $args[0];
    865889                $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
    866890                $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
    867                 $query = preg_replace('|(?<!%)%s|', "'%s'", $query); // quote the strings, avoiding escaped strings like %%s
     891                $query = str_replace( '%s', "'%s'", $query ); // quote the strings
    868892                array_walk( $args, array( &$this, 'escape_by_ref' ) );
    869893                return @vsprintf( $query, $args );
    870894        }
     
    893917                else
    894918                        $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);
    895919
    896                 if ( function_exists('error_log') && $log_file = @ini_get('error_log') && ( 'syslog' == $log_file || is_writable( $log_file ) ) )
    897                         @error_log( $error_str, 0 );
     920                if ( function_exists( 'error_log' )
     921                     && ! ( $log_file = @ini_get( 'error_log' ) )
     922                     && ( 'syslog' != $log_file )
     923                     && @is_writable( $log_file ) )
     924                    @error_log( $error_str );
    898925
    899                 // Is error output turned on or not..
     926                // Show Errors ?
    900927                if ( ! $this->show_errors )
    901928                        return false;
    902929
     
    10331060                if ( ! $this->ready )
    10341061                        return false;
    10351062
    1036                 // some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
    10371063                if ( function_exists( 'apply_filters' ) )
    10381064                        $query = apply_filters( 'query', $query );
    10391065
     
    11271153         *
    11281154         * @since 2.5.0
    11291155         * @see wpdb::prepare()
    1130          * @see wpdb::$field_types
    1131          * @see wp_set_wpdb_vars()
    11321156         *
    11331157         * @param string $table table name
    11341158         * @param array $data Data to insert (in column => value pairs).  Both $data columns and $data values should be "raw" (neither should be SQL escaped).
     
    11581182         * Update a row in the table
    11591183         *
    11601184         * <code>
    1161          * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
     1185         * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) )
    11621186         * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
    11631187         * </code>
    11641188         *
     
    14561480         * @see   wpdb::db_version()
    14571481         *
    14581482         * @param string $db_cap the feature
    1459          * @param false|string|resource $dbh_or_table. Not implemented.
    14601483         * @return bool
    14611484         */
    14621485        function has_cap( $db_cap ) {
     
    14891512                foreach ( $trace as $call ) {
    14901513                        if ( isset( $call['class'] ) && __CLASS__ == $call['class'] )
    14911514                                continue; // Filter out wpdb calls.
     1515                        $caller[] = isset( $call['class'] ) ? "{$call['class']}->{$call['function']}" : $call['function'];
    14921516                }
    1493                 $caller = join( ', ', $caller );
    14941517
    1495                 return $caller;
     1518                return join( ', ', $caller );
    14961519        }
    14971520
    14981521        /**
    14991522         * The database version number.
    15001523         *
    1501          * @param false|string|resource $dbh_or_table. Not implemented.
    15021524         * @return false|string false on failure, version number on success
    15031525         */
    15041526        function db_version() {
     
    15141536         */
    15151537        $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    15161538}
    1517 ?>
     1539?>
     1540 No newline at end of file