Ticket #11644: 11644.code_cleanup_wpdb.5.patch
| File 11644.code_cleanup_wpdb.5.patch, 40.0 KB (added by , 16 years ago) |
|---|
-
wp-db.php
12 12 /** 13 13 * @since 0.71 14 14 */ 15 define( 'EZSQL_VERSION', 'WP1.25');15 define( 'EZSQL_VERSION', 'WP1.25' ); 16 16 17 17 /** 18 18 * @since 0.71 19 19 */ 20 define( 'OBJECT', 'OBJECT', true);20 define( 'OBJECT', 'OBJECT', true ); 21 21 22 22 /** 23 23 * @since {@internal Version Unknown}} 24 24 */ 25 define( 'OBJECT_K', 'OBJECT_K', false);25 define( 'OBJECT_K', 'OBJECT_K', false ); 26 26 27 27 /** 28 28 * @since 0.71 29 29 */ 30 define( 'ARRAY_A', 'ARRAY_A', false);30 define( 'ARRAY_A', 'ARRAY_A', false ); 31 31 32 32 /** 33 33 * @since 0.71 34 34 */ 35 define( 'ARRAY_N', 'ARRAY_N', false);35 define( 'ARRAY_N', 'ARRAY_N', false ); 36 36 37 37 /** 38 38 * WordPress Database Access Abstraction Object … … 65 65 * Whether to suppress errors during the DB bootstrapping. 66 66 * 67 67 * @access private 68 * @since {@internal Version Unknown}}68 * @since 2.5 69 69 * @var bool 70 70 */ 71 71 var $suppress_errors = false; … … 73 73 /** 74 74 * The last error during query. 75 75 * 76 * @since {@internal Version Unknown}} 76 * @see get_last_error() 77 * @since 2.5 78 * @access private 77 79 * @var string 78 80 */ 79 81 var $last_error = ''; … … 88 90 var $num_queries = 0; 89 91 90 92 /** 93 * Amount of rows returned by last query operation 94 * 95 * @since 1.2 96 * @access private 97 * @var int 98 */ 99 var $num_rows = 0; 100 101 /** 102 * Number of affected rows by last query operation 103 * 104 * @since 0.71 105 * @access private 106 * @var integer 107 */ 108 var $rows_affected = 0; 109 110 111 /** 91 112 * Saved result of the last query made 92 113 * 93 114 * @since 1.2.0 … … 97 118 var $last_query; 98 119 99 120 /** 121 * Saved resultset of the last query made 122 * 123 * @since {@internal Version Unknown} 124 * @access private 125 * @var array (null if unintialized) 126 */ 127 var $last_result; 128 129 /** 100 130 * Saved info on the table column 101 131 * 102 132 * @since 1.2.0 … … 135 165 * @var bool 136 166 */ 137 167 var $ready = false; 168 169 /** 170 * {@internal Missing Description}} 171 * 172 * @since 3.0 173 * @access private 174 * @var unknown_type 175 */ 138 176 var $blogid = 0; 177 178 /** 179 * {@internal Missing Description}} 180 * 181 * @since 3.0 182 * @access private 183 * @var unknown_type 184 */ 139 185 var $siteid = 0; 140 186 141 187 /** … … 157 203 var $users; 158 204 159 205 /** 206 * WordPress Categories table 207 * 208 * @since 1.5.0 209 * @deprecated 2.3.0 / db version 5539 210 * @access public 211 * @var string 212 */ 213 var $categories; 214 215 /** 216 * WordPress Post to Category table 217 * 218 * @since 1.5.0 219 * @deprecated 2.3.0 / db version 5539 220 * @access public 221 * @var string 222 */ 223 var $post2cat; 224 225 /** 226 * WordPress Link to Category table 227 * 228 * @since 1.5.0 229 * @deprecated 2.3.0 / db version 5539 230 * @access public 231 * @var string 232 */ 233 var $link2cat; 234 235 /** 160 236 * WordPress Comments table 161 237 * 162 238 * @since 1.5.0 … … 250 326 251 327 /** 252 328 * List of deprecated WordPress tables 329 * 330 * - categories, post2cat and link2cat are deprecated 331 * since 2.3.0 / db version >= 5539. 253 332 * 254 333 * @since 2.9.0 255 334 * @access private … … 406 485 * @param string $dbname MySQL database name 407 486 * @param string $dbhost MySQL database host 408 487 */ 409 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost) {488 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) { 410 489 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) 411 490 $this->db_connect(); 412 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost);491 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost ); 413 492 } 414 493 415 494 /** … … 426 505 * @param string $dbname MySQL database name 427 506 * @param string $dbhost MySQL database host 428 507 */ 429 function __construct( $dbuser, $dbpassword, $dbname, $dbhost) {430 register_shutdown_function( array(&$this, "__destruct"));508 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { 509 register_shutdown_function( array( &$this, '__destruct' ) ); # prevents the object from unloading 431 510 432 511 if ( WP_DEBUG ) 433 512 $this->show_errors(); … … 442 521 $this->collate = DB_COLLATE; 443 522 } 444 523 445 if ( defined( 'DB_CHARSET') )524 if ( defined( 'DB_CHARSET' ) ) 446 525 $this->charset = DB_CHARSET; 447 526 448 527 $this->dbuser = $dbuser; 449 528 450 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true);451 if ( !$this->dbh) {452 $this->bail( sprintf(/*WP_I18N_DB_CONN_ERROR*/"529 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true ); 530 if ( !$this->dbh ) { 531 $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/" 453 532 <h1>Error establishing a database connection</h1> 454 533 <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> 455 534 <ul> … … 458 537 <li>Are you sure that the database server is running?</li> 459 538 </ul> 460 539 <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> 461 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail');540 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' ); 462 541 return; 463 542 } 464 543 465 544 $this->ready = true; 466 545 467 if ( $this->has_cap( 'collation' ) && !empty( $this->charset) ) {468 if ( function_exists( 'mysql_set_charset') ) {469 mysql_set_charset( $this->charset, $this->dbh);546 if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) { 547 if ( function_exists( 'mysql_set_charset' ) ) { 548 mysql_set_charset( $this->charset, $this->dbh ); 470 549 $this->real_escape = true; 471 550 } else { 472 $ collation_query = "SET NAMES '{$this->charset}'";473 if ( !empty( $this->collate) )474 $ collation_query .= " COLLATE '{$this->collate}'";475 $this->query( $collation_query);551 $query = $this->prepare( 'SET NAMES %s', $this->charset ); 552 if ( !empty( $this->collate ) ) 553 $query .= $this->prepare( ' COLLATE %s', $this->collate ); 554 $this->query( $query ); 476 555 } 477 556 } 478 557 479 $this->select( $dbname, $this->dbh);558 $this->select( $dbname, $this->dbh ); 480 559 } 481 560 482 561 /** 483 562 * PHP5 style destructor and will run when database object is destroyed. 484 563 * 564 * 565 * Please see {@link __construct() class constructor} and the 566 * {@link register_shutdown_function() register_shutdown_function()}. for 567 * more details. 568 * 569 * @link http://www.php.net/__destruct 570 * @link http://www.php.net/register_shutdown_function 571 * 485 572 * @since 2.0.8 486 573 * 487 * @return bool Alwaystrue574 * @return bool true 488 575 */ 489 576 function __destruct() { 490 577 return true; … … 503 590 */ 504 591 function set_prefix( $prefix ) { 505 592 506 if ( preg_match( '|[^a-z0-9_]|i', $prefix) )593 if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) 507 594 return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/); 595 596 $old_prefix = is_multisite() ? '' : $prefix; 508 597 509 if ( is_multisite() )510 $old_prefix = '';511 else512 $old_prefix = $prefix;513 514 598 if ( isset( $this->base_prefix ) ) 515 599 $old_prefix = $this->base_prefix; 600 516 601 $this->base_prefix = $prefix; 602 517 603 foreach ( $this->tables( 'global' ) as $table ) 518 604 $this->$table = $prefix . $table; 519 605 520 if ( defined( 'VHOST') && empty( $this->blogid ) )606 if ( defined( 'VHOST' ) && empty( $this->blogid ) ) 521 607 return $old_prefix; 522 608 523 609 $this->prefix = $this->get_blog_prefix( $this->blogid ); 524 610 525 foreach ( (array) $this->tables( 'blog') as $table )611 foreach ( $this->tables( array('blog', 'old' ) ) as $table ) 526 612 $this->$table = $this->prefix . $table; 527 528 foreach ( (array) $this->tables( 'old' ) as $table ) 529 $this->$table = $this->prefix . $table; 530 531 if ( defined('CUSTOM_USER_TABLE') ) 613 614 if ( defined( 'CUSTOM_USER_TABLE' ) ) 532 615 $this->users = CUSTOM_USER_TABLE; 533 616 534 if ( defined( 'CUSTOM_USER_META_TABLE') )617 if ( defined( 'CUSTOM_USER_META_TABLE' ) ) 535 618 $this->usermeta = CUSTOM_USER_META_TABLE; 536 619 537 620 return $old_prefix; 538 621 } 539 622 540 function set_blog_id($blog_id, $site_id = '') { 623 /** 624 * blog id setter 625 * 626 * {@internal Missing Description}} 627 * 628 * @since 3.0 629 * @access public 630 * @param string $blog_id 631 * @param string $site_id (optional) 632 * @return string previous blog id 633 */ 634 function set_blog_id( $blog_id, $site_id = '' ) { 541 635 if ( !empty($site_id) ) 542 636 $this->siteid = $site_id; 543 637 544 $old_blog_id = $this->blogid;638 $old_blog_id = $this->blogid; 545 639 $this->blogid = $blog_id; 546 640 547 641 $this->prefix = $this->get_blog_prefix( $this->blogid ); 548 642 549 foreach ( $this->tables( 'blog') as $table )643 foreach ( $this->tables( array('blog', 'old') ) as $table ) 550 644 $this->$table = $this->prefix . $table; 551 645 552 foreach ( $this->tables( 'old' ) as $table )553 $this->$table = $this->prefix . $table;554 555 646 return $old_blog_id; 556 647 } 557 648 649 /** 650 * blog prefix getter 651 * 652 * {@internal Missing Description}} 653 * 654 * @param string $blog_id (optional) 655 * @return unknown_type 656 */ 558 657 function get_blog_prefix( $blog_id = '' ) { 559 658 if ( is_multisite() && $blog_id ) { 560 659 if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) ) … … 569 668 /** 570 669 * Returns an array of WordPress tables. 571 670 * 671 * Scope / Table identifiers: 672 * 673 * - 'all' ........ global and blog tables. 674 * - 'global' ..... global tables 675 * - 'blog' ....... blog tables 676 * - 'old tables' . old (depreacted) tables 677 * 678 * NOTE: This function does not reflect CUSTOM_USER_TABLE 679 * and CUSTOM_USER_META_TABLE and will return 680 * the standard table names (users, usermeta) instead. 681 * 682 * @access public 572 683 * @since 3.0.0 573 * @uses wpdb::tables 574 * @uses wpdb::old_tables 575 * @uses wpdb::global_tables 684 * @uses wpdb::$tables 685 * @uses wpdb::$old_tables 686 * @uses wpdb::$global_tables 687 * @uses wpdb::$ms_tables 576 688 * @uses is_multisite() 577 689 * 578 * @param string $scope Can be all, global, blog, or old tables. Default all. 579 * All returns all global tables and the blog tables for the queried blog. 580 * @param bool $prefix Whether to include the blog prefix. Default false. 581 * @param int $blog_id The blog_id to prefix. Defaults to main blog. 690 * @param string|array $scope (optional) Scope, Default 'all'. You can pass multiple scopes per array 691 * @param bool $prefix (optional) Whether to include the blog prefix. Default FALSE. 692 * @param int $blog_id (optional) The blog_id to prefix. 582 693 * @return array Table names. 583 694 */ 584 function tables( $scope = 'all', $prefix = false, $blog_id = 0 ) { 585 switch ( $scope ) { 586 case 'old' : 587 $tables = $this->old_tables; 588 break; 589 case 'blog' : 590 $tables = $this->tables; 591 break; 592 case 'global' : 593 $tables = array_merge( $this->global_tables, $this->ms_tables ); 594 break; 595 case 'all' : 596 $tables = array_merge( $this->global_tables, $this->tables ); 597 if ( is_multisite() ) 598 $tables = array_merge( $tables, $this->ms_tables ); 599 break; 695 function tables( $scope = 'all', $prefix = false, $blog_id = '' ) { 696 697 if ( is_array( $scope ) ) { 698 $tables = array(); 699 foreach ( $scope as $each ) 700 $tables = array_merge( $tables, $this->tables( $each ) ); 701 } else { 702 switch ( $scope ) { 703 case 'old' : 704 $tables = $this->old_tables; 705 break; 706 case 'blog' : 707 $tables = $this->tables; 708 break; 709 case 'global' : 710 $tables = array_merge( $this->global_tables, $this->ms_tables ); 711 if ( is_multisite() ) 712 $tables = array_merge( $tables, $this->ms_tables ); 713 break; 714 case 'all' : 715 $tables = $this->tables( array( 'global', 'blog' ) ); 716 break; 717 } 600 718 } 601 719 602 if ( $prefix ) {720 if ( count( $tables ) && $prefix ) { 603 721 $prefix = $this->get_blog_prefix( $blog_id ); 604 foreach ( $tables as $k => $table ) {722 foreach ( $tables as $k => $table ) 605 723 $tables[$k] = $prefix . $table; 606 }607 724 } 608 725 609 726 return $tables; … … 620 737 * @param string $db MySQL database name 621 738 * @return null Always null. 622 739 */ 623 function select( $db, &$dbh) {624 if ( !@mysql_select_db($db, $dbh)) {740 function select( $db, &$dbh ) { 741 if ( !@mysql_select_db( $db, $dbh ) ) { 625 742 $this->ready = false; 626 $this->bail( sprintf(/*WP_I18N_DB_SELECT_DB*/'743 $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/' 627 744 <h1>Can’t select database</h1> 628 745 <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> 629 746 <ul> … … 631 748 <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li> 632 749 <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> 633 750 </ul> 634 <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');751 <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' ); 635 752 return; 636 753 } 637 754 } 638 755 639 function _weak_escape($string) { 640 return addslashes($string); 756 /** 757 * weak escape 758 * 759 * an alias to addslashes() 760 * 761 * @see addslashes() 762 * 763 * @since {@internal Version Unknown}} 764 * @access private 765 * @param string $string 766 * @return string 767 */ 768 function _weak_escape( $string ) { 769 return addslashes( $string ); 641 770 } 642 771 643 function _real_escape($string) { 772 /** 773 * real escape 774 * 775 * escape via mysql_real_escape_string() or addslashes() 776 * 777 * @since 2.8 778 * @access private 779 * 780 * @param string $string to escape 781 * @return string escaped 782 */ 783 function _real_escape( $string ) { 644 784 if ( $this->dbh && $this->real_escape ) 645 785 return mysql_real_escape_string( $string, $this->dbh ); 646 786 else 647 787 return addslashes( $string ); 648 788 } 649 789 650 function _escape($data) { 651 if ( is_array($data) ) { 790 /** 791 * escape 792 * 793 * escape data, uses {@see _real_escape()} and works 794 * on arrays as well. 795 * 796 * @since 2.8 797 * @access private 798 * 799 * @param string|array $data to escape 800 * @return string|array escaped 801 */ 802 function _escape( $data ) { 803 if ( is_array( $data ) ) { 652 804 foreach ( (array) $data as $k => $v ) { 653 805 if ( is_array($v) ) 654 806 $data[$k] = $this->_escape( $v ); … … 667 819 * 668 820 * @since 0.71 669 821 * 670 * @param string|array $data671 * @return string query safe string822 * @param string|array $data to escape 823 * @return string|array escaped as query safe string 672 824 */ 673 function escape( $data) {674 if ( is_array( $data) ) {825 function escape( $data ) { 826 if ( is_array( $data ) ) { 675 827 foreach ( (array) $data as $k => $v ) { 676 if ( is_array( $v) )828 if ( is_array( $v ) ) 677 829 $data[$k] = $this->escape( $v ); 678 830 else 679 831 $data[$k] = $this->_weak_escape( $v ); … … 690 842 * 691 843 * @since 2.3.0 692 844 * 693 * @param string $s 845 * @param string $string to escape 846 * @return void 694 847 */ 695 function escape_by_ref( &$string) {848 function escape_by_ref( &$string ) { 696 849 $string = $this->_real_escape( $string ); 697 850 } 698 851 699 852 /** 700 853 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. 701 854 * 855 * The following directives can be used in the query format string: 856 * 857 * %d (decimal number) 858 * %s (string) 859 * %% (literal percentage sign - no argument needed) 860 * 861 * Both %d and %s are to be left unquoted in the query string and 862 * they need an argument passed for them. 863 * Literals (%) as parts of the query must be properly written 864 * as %%. 865 * 702 866 * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string). 703 867 * Does not support sign, padding, alignment, width or precision specifiers. 704 868 * Does not support argument numbering/swapping. … … 709 873 * 710 874 * <code> 711 875 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 ) 876 * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); 712 877 * </code> 713 878 * 714 879 * @link http://php.net/sprintf Description of syntax. 715 880 * @since 2.3.0 716 881 * 717 882 * @param string $query Query statement with sprintf()-like placeholders 718 * @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()}.883 * @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()}. 719 884 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}. 720 * @return null| string Sanitized query string885 * @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 721 886 */ 722 function prepare( $query = null) { // ( $query, *$args )887 function prepare( $query = null ) { // ( $query, *$args ) 723 888 if ( is_null( $query ) ) 724 889 return; 890 725 891 $args = func_get_args(); 726 array_shift( $args);727 // If args were passed as an array (as in vsprintf), move them up728 if ( isset($args[0]) && is_array($args[0]) )729 $args = $args[0]; 730 $query = str_replace( "'%s'", '%s', $query); // in case someone mistakenly already singlequoted it731 $query = str_replace( '"%s"', '%s', $query); // doublequote unquoting732 $query = str_replace( '%s', "'%s'", $query); // quote the strings733 array_walk( $args, array(&$this, 'escape_by_ref'));734 return @vsprintf( $query, $args);892 array_shift( $args ); 893 if ( isset( $args[0] ) && is_array( $args[0] ) ) 894 $args = $args[0]; # re-assign args passed as array like in vsprintf 895 896 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it 897 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting 898 $query = str_replace( '%s', "'%s'", $query ); // quote the strings 899 array_walk( $args, array( &$this, 'escape_by_ref' ) ); 900 return @vsprintf( $query, $args ); 735 901 } 736 902 737 903 /** … … 743 909 * @param string $str The error to display 744 910 * @return bool False if the showing of errors is disabled. 745 911 */ 746 function print_error( $str = '') {912 function print_error( $str = '' ) { 747 913 global $EZSQL_ERROR; 748 914 749 if ( !$str) $str = mysql_error($this->dbh);750 $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);915 if ( !$str ) $str = mysql_error( $this->dbh ); 916 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 751 917 752 918 if ( $this->suppress_errors ) 753 919 return false; … … 757 923 else 758 924 $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); 759 925 760 $log_error = true; 761 if ( ! function_exists('error_log') ) 762 $log_error = false; 926 if ( function_exists( 'error_log' ) 927 && !( $log_file = @ini_get( 'error_log' ) ) 928 && ( 'syslog' != $log_file ) 929 && @is_writable( $log_file ) ) 930 @error_log( $error_str ); 763 931 764 $log_file = @ini_get('error_log');765 if ( !empty($log_file) && ('syslog' != $log_file) && !@is_writable($log_file) )766 $log_error = false;767 768 if ( $log_error )769 @error_log($error_str, 0);770 771 932 // Is error output turned on or not.. 772 933 if ( !$this->show_errors ) 773 934 return false; … … 778 939 if ( defined( 'ERRORLOGFILE' ) ) 779 940 error_log( $msg, 3, ERRORLOGFILE ); 780 941 if ( defined( 'DIEONDBERROR' ) ) 781 die( $msg );942 wp_die( $msg ); 782 943 } else { 783 $str = htmlspecialchars($str, ENT_QUOTES);784 $query = htmlspecialchars( $this->last_query, ENT_QUOTES);944 $str = htmlspecialchars( $str, ENT_QUOTES ); 945 $query = htmlspecialchars( $this->last_query, ENT_QUOTES ); 785 946 786 947 print "<div id='error'> 787 948 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> … … 799 960 * errors. 800 961 * 801 962 * @since 0.71 963 * @see hide_errors() 802 964 * 803 965 * @param bool $show Whether to show or hide errors 804 966 * @return bool Old value for showing errors. … … 812 974 /** 813 975 * Disables showing of database errors. 814 976 * 977 * By default database errors are not shown. 978 * 815 979 * @since 0.71 980 * @see show_errors() 816 981 * 817 982 * @return bool Whether showing of errors was active or not 818 983 */ … … 825 990 /** 826 991 * Whether to suppress database errors. 827 992 * 828 * @param unknown_type $suppress 829 * @return unknown 993 * By default database errors are suppressed, with a simple 994 * call to this function they can be enabled. 995 * 996 * @since 2.5 997 * @see hide_errors() 998 * @param bool $suppress (optional) new value, defaults to true 999 * @return bool old suppress errors value 830 1000 */ 831 1001 function suppress_errors( $suppress = true ) { 832 1002 $errors = $this->suppress_errors; 833 $this->suppress_errors = $suppress;1003 $this->suppress_errors = (bool) $suppress; 834 1004 return $errors; 835 1005 } 836 1006 … … 838 1008 * Kill cached query results. 839 1009 * 840 1010 * @since 0.71 1011 * 1012 * @return void 841 1013 */ 842 1014 function flush() { 843 1015 $this->last_result = array(); 844 $this->col_info = null;845 $this->last_query = null;1016 $this->col_info = null; 1017 $this->last_query = null; 846 1018 } 847 1019 848 1020 function db_connect( $query = "SELECT" ) { … … 865 1037 } 866 1038 867 1039 $dbhname = "dbh" . $action; 868 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 1040 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 869 1041 if (!$this->$dbhname ) { 870 $this->bail( "1042 $this->bail( sprintf( /*WP_I18N_DB_CONNECT_DB*/' 871 1043 <h1>Error establishing a database connection</h1> 872 <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>{$details['db_host']}</code>. This could mean your host's database server is down.</p>1044 <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> 873 1045 <ul> 874 1046 <li>Are you sure you have the correct username and password?</li> 875 1047 <li>Are you sure that you have typed the correct hostname?</li> 876 1048 <li>Are you sure that the database server is running?</li> 877 1049 </ul> 878 <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> 879 "); 1050 <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) ); 880 1051 } 881 1052 $this->select( $details[ 'db_name' ], $this->$dbhname ); 882 1053 } … … 888 1059 * 889 1060 * @since 0.71 890 1061 * 891 * @param string $query 1062 * @param string $query database query 892 1063 * @return int|false Number of rows affected/selected or false on error 893 1064 */ 894 function query( $query) {1065 function query( $query ) { 895 1066 if ( ! $this->ready ) 896 1067 return false; 897 1068 898 // filter the query, if filters are available 899 // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method 900 if ( function_exists('apply_filters') ) 901 $query = apply_filters('query', $query); 1069 if ( function_exists( 'apply_filters' ) ) 1070 $query = apply_filters( 'query', $query ); 902 1071 903 // init ialise return1072 // init return values and object's state 904 1073 $return_val = 0; 905 1074 $this->flush(); 906 1075 … … 910 1079 // Keep track of the last query for debug.. 911 1080 $this->last_query = $query; 912 1081 913 // Perform the query via std mysql_query function.. 914 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 1082 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 915 1083 $this->timer_start(); 916 1084 917 1085 // use $this->dbh for read ops, and $this->dbhwrite for write ops … … 939 1107 $this->last_db_used = "other/read"; 940 1108 } 941 1109 942 $this->result = @mysql_query($query, $dbh); 943 ++$this->num_queries; 1110 // Perform the query via std mysql_query function.. 1111 $this->result = @mysql_query( $query, $dbh ); 1112 $this->num_queries++; 944 1113 945 1114 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 946 1115 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 947 1116 948 1117 // If there is an error then take note of it.. 949 if ( $this->last_error = mysql_error( $dbh) ) {1118 if ( $this->last_error = mysql_error( $dbh ) ) { 950 1119 $this->print_error(); 951 1120 return false; 952 1121 } 953 1122 954 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {955 $this->rows_affected = mysql_affected_rows( $dbh);1123 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) { 1124 $this->rows_affected = mysql_affected_rows( $dbh ); 956 1125 // Take note of the insert_id 957 if ( preg_match( "/^\\s*(insert|replace) /i",$query) ) {1126 if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) { 958 1127 $this->insert_id = mysql_insert_id($dbh); 959 1128 } 960 1129 // Return number of rows affected 961 1130 $return_val = $this->rows_affected; 962 1131 } else { 963 1132 $i = 0; 964 while ( $i < @mysql_num_fields($this->result)) {965 $this->col_info[$i] = @mysql_fetch_field( $this->result);1133 while ( $i < @mysql_num_fields( $this->result ) ) { 1134 $this->col_info[$i] = @mysql_fetch_field( $this->result ); 966 1135 $i++; 967 1136 } 968 1137 $num_rows = 0; 969 while ( $row = @mysql_fetch_object( $this->result) ) {1138 while ( $row = @mysql_fetch_object( $this->result ) ) { 970 1139 $this->last_result[$num_rows] = $row; 971 1140 $num_rows++; 972 1141 } 973 1142 974 @mysql_free_result( $this->result);1143 @mysql_free_result( $this->result ); 975 1144 976 1145 // Log number of rows the query returned 1146 // and Return number of rows selected 977 1147 $this->num_rows = $num_rows; 978 979 // Return number of rows selected 980 $return_val = $this->num_rows; 1148 $return_val = $num_rows; 981 1149 } 982 1150 983 1151 return $return_val; … … 987 1155 * Insert a row into a table. 988 1156 * 989 1157 * <code> 1158 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) ) 990 1159 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) 991 1160 * </code> 992 1161 * … … 998 1167 * @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. 999 1168 * @return int|false The number of rows inserted, or false on error. 1000 1169 */ 1001 function insert( $table, $data, $format = null) {1170 function insert( $table, $data, $format = null ) { 1002 1171 $formats = $format = (array) $format; 1003 $fields = array_keys( $data);1172 $fields = array_keys( $data ); 1004 1173 $formatted_fields = array(); 1005 1174 foreach ( $fields as $field ) { 1006 if ( !empty( $format) )1007 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];1008 elseif ( isset( $this->field_types[$field]) )1175 if ( !empty( $format ) ) 1176 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1177 elseif ( isset( $this->field_types[$field] ) ) 1009 1178 $form = $this->field_types[$field]; 1010 1179 else 1011 1180 $form = '%s'; 1012 1181 $formatted_fields[] = $form; 1013 1182 } 1014 1183 $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')"; 1015 return $this->query( $this->prepare( $sql, $data ) );1184 return $this->query( $this->prepare( $sql, $data ) ); 1016 1185 } 1017 1186 1018 1187 … … 1020 1189 * Update a row in the table 1021 1190 * 1022 1191 * <code> 1192 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) ) 1023 1193 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) 1024 1194 * </code> 1025 1195 * … … 1033 1203 * @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. 1034 1204 * @return int|false The number of rows updated, or false on error. 1035 1205 */ 1036 function update( $table, $data, $where, $format = null, $where_format = null) {1037 if ( !is_array( $ where ) )1206 function update( $table, $data, $where, $format = null, $where_format = null ) { 1207 if ( !is_array( $data ) || !is_array( $where ) ) 1038 1208 return false; 1039 1209 1040 1210 $formats = $format = (array) $format; 1041 1211 $bits = $wheres = array(); 1042 foreach ( (array) array_keys( $data) as $field ) {1043 if ( !empty( $format) )1044 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];1212 foreach ( (array) array_keys( $data ) as $field ) { 1213 if ( !empty( $format ) ) 1214 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1045 1215 elseif ( isset($this->field_types[$field]) ) 1046 1216 $form = $this->field_types[$field]; 1047 1217 else … … 1050 1220 } 1051 1221 1052 1222 $where_formats = $where_format = (array) $where_format; 1053 foreach ( (array) array_keys( $where) as $field ) {1054 if ( !empty( $where_format) )1055 $form = ( $form = array_shift( $where_formats) ) ? $form : $where_format[0];1056 elseif ( isset( $this->field_types[$field]) )1223 foreach ( (array) array_keys( $where ) as $field ) { 1224 if ( !empty( $where_format ) ) 1225 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1226 elseif ( isset( $this->field_types[$field] ) ) 1057 1227 $form = $this->field_types[$field]; 1058 1228 else 1059 1229 $form = '%s'; … … 1061 1231 } 1062 1232 1063 1233 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); 1064 return $this->query( $this->prepare( $sql, array_merge( array_values($data), array_values($where))) );1234 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); 1065 1235 } 1066 1236 1067 1237 /** … … 1073 1243 * 1074 1244 * @since 0.71 1075 1245 * 1076 * @param string|null $query SQL query. If null, use the result from the previous query.1246 * @param string|null $query (optional) SQL query. Defaults to NULL, use the result from the previous query. 1077 1247 * @param int $x (optional) Column of value to return. Indexed from 0. 1078 1248 * @param int $y (optional) Row of value to return. Indexed from 0. 1079 * @return string Database query result1249 * @return string|null Database query result on success (as string), NULL if a value on failure 1080 1250 */ 1081 function get_var( $query=null, $x = 0, $y = 0) {1082 $this->func_call = "\$db->get_var(\"$query\", $x,$y)";1251 function get_var( $query = null, $x = 0, $y = 0 ) { 1252 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; 1083 1253 if ( $query ) 1084 $this->query( $query);1254 $this->query( $query ); 1085 1255 1086 1256 // Extract var out of cached results based x,y vals 1087 1257 if ( !empty( $this->last_result[$y] ) ) { 1088 $values = array_values( get_object_vars($this->last_result[$y]));1258 $values = array_values( get_object_vars( $this->last_result[$y] ) ); 1089 1259 } 1090 1260 1091 1261 // If there is a value return it else return null 1092 return ( isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;1262 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; 1093 1263 } 1094 1264 1095 1265 /** … … 1102 1272 * @param string|null $query SQL query. 1103 1273 * @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. 1104 1274 * @param int $y (optional) Row to return. Indexed from 0. 1105 * @return mixed Database query result in format specifed by $output 1275 * @return mixed Database query result in format specifed by $output or NULL 1276 * @return void 1106 1277 */ 1107 function get_row( $query = null, $output = OBJECT, $y = 0) {1278 function get_row( $query = null, $output = OBJECT, $y = 0 ) { 1108 1279 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; 1109 1280 if ( $query ) 1110 $this->query( $query);1281 $this->query( $query ); 1111 1282 else 1112 1283 return null; 1113 1284 1114 if ( !isset( $this->last_result[$y]) )1285 if ( !isset( $this->last_result[$y] ) ) 1115 1286 return null; 1116 1287 1117 1288 if ( $output == OBJECT ) { 1118 1289 return $this->last_result[$y] ? $this->last_result[$y] : null; 1119 1290 } elseif ( $output == ARRAY_A ) { 1120 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y]) : null;1291 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; 1121 1292 } elseif ( $output == ARRAY_N ) { 1122 return $this->last_result[$y] ? array_values( get_object_vars($this->last_result[$y])) : null;1293 return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; 1123 1294 } else { 1124 1295 $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*/); 1125 1296 } … … 1134 1305 * 1135 1306 * @since 0.71 1136 1307 * 1137 * @param string|null $query SQL query.If null, use the result from the previous query.1138 * @param int $x Column to return. Indexed from 0.1308 * @param string|null $query (optional) SQL query. If null, use the result from the previous query. 1309 * @param int $x (optional) Column to return. Indexed from 0. 1139 1310 * @return array Database query result. Array indexed from 0 by SQL result row number. 1140 1311 */ 1141 function get_col( $query = null , $x = 0) {1312 function get_col( $query = null , $x = 0 ) { 1142 1313 if ( $query ) 1143 $this->query( $query);1314 $this->query( $query ); 1144 1315 1145 1316 $new_array = array(); 1146 1317 // Extract the column values 1147 for ( $i=0; $i < count( $this->last_result); $i++ ) {1148 $new_array[$i] = $this->get_var( null, $x, $i);1318 for ( $i=0; $i < count( $this->last_result ); $i++ ) { 1319 $new_array[$i] = $this->get_var( null, $x, $i ); 1149 1320 } 1150 1321 return $new_array; 1151 1322 } … … 1161 1332 * @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. 1162 1333 * @return mixed Database query results 1163 1334 */ 1164 function get_results( $query = null, $output = OBJECT) {1335 function get_results( $query = null, $output = OBJECT ) { 1165 1336 $this->func_call = "\$db->get_results(\"$query\", $output)"; 1166 1337 1167 1338 if ( $query ) 1168 $this->query( $query);1339 $this->query( $query ); 1169 1340 else 1170 1341 return null; 1171 1342 … … 1206 1377 * 1207 1378 * @since 0.71 1208 1379 * 1209 * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill1210 * @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 type1380 * @param string $info_type(optional) type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill 1381 * @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 1211 1382 * @return mixed Column Results 1212 1383 */ 1213 function get_col_info( $info_type = 'name', $col_offset = -1) {1384 function get_col_info( $info_type = 'name', $col_offset = -1 ) { 1214 1385 if ( $this->col_info ) { 1215 1386 if ( $col_offset == -1 ) { 1216 $i = 0;1387 $i = 0; 1217 1388 $new_array = array(); 1218 1389 foreach( (array) $this->col_info as $col ) { 1219 1390 $new_array[$i] = $col->{$info_type}; … … 1234 1405 * @return true 1235 1406 */ 1236 1407 function timer_start() { 1237 $mtime = microtime(); 1238 $mtime = explode(' ', $mtime); 1408 $mtime = explode( ' ', microtime() ); 1239 1409 $this->time_start = $mtime[1] + $mtime[0]; 1240 1410 return true; 1241 1411 } … … 1248 1418 * @return int Total time spent on the query, in milliseconds 1249 1419 */ 1250 1420 function timer_stop() { 1251 $mtime = microtime(); 1252 $mtime = explode(' ', $mtime); 1253 $time_end = $mtime[1] + $mtime[0]; 1421 $mtime = explode( ' ', microtime() ); 1422 $time_end = $mtime[1] + $mtime[0]; 1254 1423 $time_total = $time_end - $this->time_start; 1255 1424 return $time_total; 1256 1425 } … … 1266 1435 * @param string $error_code (optional) A Computer readable string to identify the error. 1267 1436 * @return false|void 1268 1437 */ 1269 function bail( $message, $error_code = '500') {1438 function bail( $message, $error_code = '500' ) { 1270 1439 if ( !$this->show_errors ) { 1271 if ( class_exists( 'WP_Error') )1440 if ( class_exists( 'WP_Error' ) ) 1272 1441 $this->error = new WP_Error($error_code, $message); 1273 1442 else 1274 1443 $this->error = $message; … … 1282 1451 * 1283 1452 * @since 2.5.0 1284 1453 * @uses $wp_version 1454 * @uses $required_mysql_version 1285 1455 * 1286 1456 * @return WP_Error 1287 1457 */ … … 1289 1459 global $wp_version, $required_mysql_version; 1290 1460 // Make sure the server has the required MySQL version 1291 1461 if ( version_compare($this->db_version(), $required_mysql_version, '<') ) 1292 return new WP_Error('database_version', sprintf(__('<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher'), $wp_version, $required_mysql_version));1462 return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version )); 1293 1463 } 1294 1464 1295 1465 /** … … 1306 1476 } 1307 1477 1308 1478 /** 1309 * Generic function to determine if a database supports a particular feature 1479 * has capability 1480 * 1481 * Determine if a database supports a particular feature 1482 * 1483 * @since 2.7 1484 * @see db_version() 1485 * 1310 1486 * @param string $db_cap the feature 1311 1487 * @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. 1312 1488 * @return bool … … 1318 1494 case 'collation' : // @since 2.5.0 1319 1495 case 'group_concat' : // @since 2.7 1320 1496 case 'subqueries' : // @since 2.7 1321 return version_compare( $version, '4.1', '>=');1497 return version_compare( $version, '4.1', '>=' ); 1322 1498 break; 1323 1499 endswitch; 1324 1500 … … 1336 1512 * @return string The name of the calling function 1337 1513 */ 1338 1514 function get_caller() { 1339 $ bt = debug_backtrace();1515 $trace = array_reverse( debug_backtrace() ); 1340 1516 $caller = array(); 1341 1517 1342 $bt = array_reverse( $bt ); 1343 foreach ( (array) $bt as $call ) { 1518 foreach ( $trace as $call ) { 1344 1519 if ( isset( $call['class'] ) && __CLASS__ == $call['class'] ) 1345 continue; 1520 continue; # filter out function calls of this object's class 1521 1346 1522 $function = $call['function']; 1523 1347 1524 if ( isset( $call['class'] ) ) 1348 1525 $function = $call['class'] . "->$function"; 1526 1349 1527 $caller[] = $function; 1350 1528 } 1351 $caller = join( ', ', $caller );1352 1529 1353 return $caller;1530 return join( ', ', $caller ); 1354 1531 } 1355 1532 1356 1533 /** 1357 * The database version number 1358 * @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. 1534 * get database version number 1535 * 1536 * ADDITIONAL PARAMETER NOTICE 1537 * 1538 * there once was a proposed second parameter which has never been 1539 * implemented. It was describben as "Which database to test" ($dbh_or_table) 1540 * 1541 * It would have had three different types: 1542 * 1543 * false : currently selected database 1544 * string : database containing this table 1545 * resource : database by mysql resource 1546 * 1547 * regarding that third parameter please see {@see db_version()} as well 1548 * 1549 * @since 2.7 1550 * @see has_cap() 1551 * 1359 1552 * @return false|string false on failure, version number on success 1360 1553 */ 1361 1554 function db_version() { 1362 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));1555 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 1363 1556 } 1364 1557 } 1365 1558 … … 1371 1564 */ 1372 1565 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); 1373 1566 } 1374 ?> 1567 ?> 1568 No newline at end of file