Ticket #11644: 11644.code_cleanup_wpdb-2nd_iteration.patch
File 11644.code_cleanup_wpdb-2nd_iteration.patch, 18.9 KB (added by , 15 years ago) |
---|
-
wp-db.php
93 93 * Count of rows returned by previous query 94 94 * 95 95 * @since 1.2 96 * @access private 96 97 * @var int 97 98 */ 98 99 var $num_rows = 0; … … 101 102 * Count of affected rows by previous query 102 103 * 103 104 * @since 0.71 105 * @access private 104 106 * @var int 105 107 */ 106 108 var $rows_affected = 0; … … 118 120 * Results of the last query made 119 121 * 120 122 * @since {@internal Version Unknown}} 121 * @var mixed 123 * @access private 124 * @var array|null 122 125 */ 123 126 var $last_result; 124 127 … … 166 169 * {@internal Missing Description}} 167 170 * 168 171 * @since 3.0.0 172 * @access private 169 173 * @var int 170 174 */ 171 175 var $blogid = 0; … … 174 178 * {@internal Missing Description}} 175 179 * 176 180 * @since 3.0.0 181 * @access private 177 182 * @var int 178 183 */ 179 184 var $siteid = 0; … … 290 295 291 296 /** 292 297 * List of deprecated WordPress tables 298 * 299 * - categories, post2cat and link2cat are deprecated 300 * since 2.3.0 / db version >= 5539. 293 301 * 294 * @deprecated295 302 * @since 2.9.0 296 303 * @access private 297 304 * @see wpdb::tables() … … 460 467 * the actual setting up of the class properties and connection 461 468 * to the database. 462 469 * 470 * NOTE: register_shutdown_function prevents this object from unloading 471 * 463 472 * @since 2.0.8 464 473 * 465 474 * @param string $dbuser MySQL database user … … 523 532 /** 524 533 * PHP5 style destructor and will run when database object is destroyed. 525 534 * 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 * 526 543 * @since 2.0.8 527 544 * @return bool true 528 545 */ … … 547 564 548 565 if ( isset( $this->base_prefix ) ) 549 566 $old_prefix = $this->base_prefix; 567 550 568 $this->base_prefix = $prefix; 569 551 570 foreach ( $this->tables( 'global' ) as $table => $prefixed_table ) 552 571 $this->$table = $prefixed_table; 553 572 … … 556 575 557 576 $this->prefix = $this->get_blog_prefix( $this->blogid ); 558 577 559 foreach ( (array) $this->tables( 'blog' ) as $table => $prefixed_table )578 foreach ( $this->tables( 'blog+old' ) as $table => $prefixed_table ) 560 579 $this->$table = $prefixed_table; 561 580 562 foreach ( (array) $this->tables( 'old' ) as $table => $prefixed_table )563 $this->$table = $prefixed_table;564 565 581 return $old_prefix; 566 582 } 567 583 … … 571 587 * @since 3.0.0 572 588 * @access public 573 589 * @param string $blog_id 574 * @param string $site_id .Optional.590 * @param string $site_id Optional. 575 591 * @return string previous blog id 576 592 */ 577 593 function set_blog_id( $blog_id, $site_id = '' ) { … … 583 599 584 600 $this->prefix = $this->get_blog_prefix( $this->blogid ); 585 601 586 foreach ( $this->tables( 'blog ' ) as $table => $prefixed_table )602 foreach ( $this->tables( 'blog+old' ) as $table => $prefixed_table ) 587 603 $this->$table = $prefixed_table; 588 604 589 foreach ( $this->tables( 'old' ) as $table => $prefixed_table )590 $this->$table = $prefixed_table;591 592 605 return $old_blog_id; 593 606 } 594 607 … … 597 610 * 598 611 * @uses is_multisite() 599 612 * @since 3.0.0 600 * @param int $blog_id .Optional.613 * @param int $blog_id Optional. 601 614 * @return string Blog prefix. 602 615 */ 603 616 function get_blog_prefix( $blog_id = 0 ) { 604 617 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 ) ) 606 619 return $this->base_prefix; 607 620 else 608 621 return $this->base_prefix . $blog_id . '_'; … … 618 631 * override the WordPress users and usersmeta tables that would otherwise 619 632 * be determined by the prefix. 620 633 * 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 621 642 * @since 3.0.0 622 * @uses wpdb:: tables623 * @uses wpdb:: old_tables624 * @uses wpdb:: global_tables625 * @uses wpdb:: ms_global_tables643 * @uses wpdb::$tables 644 * @uses wpdb::$old_tables 645 * @uses wpdb::$global_tables 646 * @uses wpdb::$ms_global_tables 626 647 * @uses is_multisite() 627 648 * 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 631 651 * prefix is requested, then the custom users and usermeta tables will be mapped. 632 652 * @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. 635 654 */ 636 655 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 } 654 683 } 655 684 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; 659 688 $global_tables = array_merge( $this->global_tables, $this->ms_global_tables ); 660 689 foreach ( $tables as $k => $table ) { 690 unset( $tables[ $k ] ); 661 691 if ( in_array( $table, $global_tables ) ) 662 692 $tables[ $table ] = $base_prefix . $table; 663 693 else 664 $tables[ $table ] = $prefix . $table; 665 unset( $tables[ $k ] ); 694 $tables[ $table ] = $blog_prefix . $table; 666 695 } 667 696 668 697 if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) ) … … 705 734 /** 706 735 * Weak escape 707 736 * 737 * An alias to addslashes(). 738 * 708 739 * @see addslashes() 709 * @since unknown740 * @since {@internal Version Unknown}} 710 741 * @access private 711 742 * 712 743 * @param string $string … … 719 750 /** 720 751 * Real escape 721 752 * 753 * Escape via mysql_real_escape_string() or addslashes() 754 * 722 755 * @see mysql_real_escape_string() 723 756 * @see addslashes() 724 757 * @since 2.8 … … 737 770 /** 738 771 * Escape data. 739 772 * 740 * @see esc_sql() 773 * escape data, uses {@see _real_escape()} and works 774 * on arrays as well. 775 * 741 776 * @since 2.8 742 777 * @access private 743 778 * … … 816 851 * 817 852 * <code> 818 853 * 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' ); 819 855 * </code> 820 856 * 821 857 * @link http://php.net/sprintf Description of syntax. … … 833 869 function prepare( $query = null ) { // ( $query, *$args ) 834 870 if ( is_null( $query ) ) 835 871 return; 872 836 873 $args = func_get_args(); 837 874 array_shift( $args ); 838 875 // If args were passed as an array (as in vsprintf), move them up … … 869 906 else 870 907 $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); 871 908 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 ); 875 914 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 ) 885 917 return false; 886 918 887 919 // If there is an error then take note of it … … 890 922 if ( defined( 'ERRORLOGFILE' ) ) 891 923 error_log( $msg, 3, ERRORLOGFILE ); 892 924 if ( defined( 'DIEONDBERROR' ) ) 893 die( $msg );925 wp_die( $msg ); 894 926 } else { 895 927 $str = htmlspecialchars( $str, ENT_QUOTES ); 896 928 $query = htmlspecialchars( $this->last_query, ENT_QUOTES ); … … 1017 1049 if ( ! $this->ready ) 1018 1050 return false; 1019 1051 1020 // filter the query, if filters are available1021 // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method1022 1052 if ( function_exists( 'apply_filters' ) ) 1023 1053 $query = apply_filters( 'query', $query ); 1024 1054 1025 // initialize return1026 1055 $return_val = 0; 1027 1056 $this->flush(); 1028 1057 … … 1061 1090 } 1062 1091 1063 1092 $this->result = @mysql_query( $query, $dbh ); 1064 ++$this->num_queries;1093 $this->num_queries++; 1065 1094 1066 if ( defined( 'SAVEQUERIES') && SAVEQUERIES )1095 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1067 1096 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1068 1097 1069 1098 // If there is an error then take note of it.. … … 1107 1136 * Insert a row into a table. 1108 1137 * 1109 1138 * <code> 1139 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) ) 1110 1140 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) 1111 1141 * </code> 1112 1142 * … … 1115 1145 * 1116 1146 * @param string $table table name 1117 1147 * @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. 1119 1149 * @return int|false The number of rows inserted, or false on error. 1120 1150 */ 1121 1151 function insert( $table, $data, $format = null ) { … … 1140 1170 * Update a row in the table 1141 1171 * 1142 1172 * <code> 1173 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) ) 1143 1174 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) 1144 1175 * </code> 1145 1176 * … … 1149 1180 * @param string $table table name 1150 1181 * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1151 1182 * @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. 1154 1185 * @return int|false The number of rows updated, or false on error. 1155 1186 */ 1156 1187 function update( $table, $data, $where, $format = null, $where_format = null ) { 1157 if ( ! is_array( $ where ) )1188 if ( ! is_array( $data ) || ! is_array( $where ) ) 1158 1189 return false; 1159 1190 1160 1191 $formats = $format = (array) $format; … … 1193 1224 * 1194 1225 * @since 0.71 1195 1226 * 1196 * @param string|null $query Optional. SQL query. If null (default), usesthe 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 nullon failure1227 * @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 1200 1231 */ 1201 1232 function get_var( $query = null, $x = 0, $y = 0 ) { 1202 1233 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; … … 1220 1251 * @since 0.71 1221 1252 * 1222 1253 * @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 nullon failure1254 * @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 1226 1257 */ 1227 1258 function get_row( $query = null, $output = OBJECT, $y = 0 ) { 1228 1259 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; … … 1254 1285 * 1255 1286 * @since 0.71 1256 1287 * 1257 * @param string|null $query Optional. SQL query. If null (default), use the result from theprevious query.1288 * @param string|null $query Optional. SQL query. Defaults to previous query. 1258 1289 * @param int $x Optional. Column to return. Indexed from 0. 1259 1290 * @return array Database query result. Array indexed from 0 by SQL result row number. 1260 1291 */ … … 1278 1309 * @since 0.71 1279 1310 * 1280 1311 * @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. 1282 1313 * @return mixed Database query results 1283 1314 */ 1284 1315 function get_results( $query = null, $output = OBJECT ) { … … 1381 1412 * @since 1.5.0 1382 1413 * 1383 1414 * @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. 1385 1416 * @return false|void 1386 1417 */ 1387 1418 function bail( $message, $error_code = '500' ) { … … 1459 1490 * @return string The name of the calling function 1460 1491 */ 1461 1492 function get_caller() { 1462 $ bt = debug_backtrace();1493 $trace = array_reverse( debug_backtrace() ); 1463 1494 $caller = array(); 1464 1495 1465 $bt = array_reverse( $bt ); 1466 foreach ( (array) $bt as $call ) { 1496 foreach ( $trace as $call ) { 1467 1497 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']; 1473 1500 } 1474 $caller = join( ', ', $caller );1475 1501 1476 return $caller;1502 return join( ', ', $caller ); 1477 1503 } 1478 1504 1479 1505 /** 1480 1506 * 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 * 1483 1524 * @return false|string false on failure, version number on success 1484 1525 */ 1485 1526 function db_version() { … … 1495 1536 */ 1496 1537 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); 1497 1538 } 1498 ?> 1539 ?> 1540 No newline at end of file