Ticket #11644: 11644.code_cleanup_wpdb.4.patch
File 11644.code_cleanup_wpdb.4.patch, 39.6 KB (added by , 11 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*/); 508 595 509 596 if ( is_multisite() ) … … 517 604 foreach ( $this->tables( 'global' ) as $table ) 518 605 $this->$table = $prefix . $table; 519 606 520 if ( defined( 'VHOST') && empty( $this->blogid ) )607 if ( defined( 'VHOST' ) && empty( $this->blogid ) ) 521 608 return $old_prefix; 522 609 523 610 $this->prefix = $this->get_blog_prefix( $this->blogid ); 524 611 612 // @todo (hakre) merge those two operations into one 525 613 foreach ( (array) $this->tables( 'blog' ) as $table ) 526 614 $this->$table = $this->prefix . $table; 527 615 528 616 foreach ( (array) $this->tables( 'old' ) as $table ) 529 617 $this->$table = $this->prefix . $table; 530 618 531 if ( defined( 'CUSTOM_USER_TABLE') )619 if ( defined( 'CUSTOM_USER_TABLE' ) ) 532 620 $this->users = CUSTOM_USER_TABLE; 533 621 534 if ( defined( 'CUSTOM_USER_META_TABLE') )622 if ( defined( 'CUSTOM_USER_META_TABLE' ) ) 535 623 $this->usermeta = CUSTOM_USER_META_TABLE; 536 624 537 625 return $old_prefix; 538 626 } 539 540 function set_blog_id($blog_id, $site_id = '') { 627 628 /** 629 * blog id setter 630 * 631 * {@internal Missing Description}} 632 * 633 * @since 3.0 634 * @access public 635 * @param string $blog_id 636 * @param string $site_id (optional) 637 * @return string previous blog id 638 */ 639 function set_blog_id( $blog_id, $site_id = '' ) { 541 640 if ( !empty($site_id) ) 542 641 $this->siteid = $site_id; 543 642 544 $old_blog_id = $this->blogid;643 $old_blog_id = $this->blogid; 545 644 $this->blogid = $blog_id; 546 645 547 646 $this->prefix = $this->get_blog_prefix( $this->blogid ); … … 555 654 return $old_blog_id; 556 655 } 557 656 657 /** 658 * blog prefix getter 659 * 660 * {@internal Missing Description}} 661 * 662 * @param string $blog_id (optional) 663 * @return unknown_type 664 */ 558 665 function get_blog_prefix( $blog_id = '' ) { 559 666 if ( is_multisite() && $blog_id ) { 560 667 if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) ) … … 569 676 /** 570 677 * Returns an array of WordPress tables. 571 678 * 679 * Scope / Table identifiers: 680 * 681 * - 'all' ........ global and blog tables. 682 * - 'global' ..... global tables 683 * - 'blog' ....... blog tables 684 * - 'old tables' . old (depreacted) tables 685 * 686 * NOTE: This function does not reflect CUSTOM_USER_TABLE (users) 687 * and CUSTOM_USER_META_TABLE (usermeta) and will return 688 * the standard table names instead. 689 * 572 690 * @since 3.0.0 573 * @uses wpdb::tables 574 * @uses wpdb::old_tables 575 * @uses wpdb::global_tables 691 * @uses wpdb::$tables 692 * @uses wpdb::$old_tables 693 * @uses wpdb::$global_tables 694 * @uses wpdb::$ms_tables 576 695 * @uses is_multisite() 577 696 * 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. 697 * @param string|array $scope (optional) Scope, Default 'all'. You can pass multiple scopes per array 698 * @param bool $prefix (optional) Whether to include the blog prefix. Default FALSE. 699 * @param int $blog_id (optional) The blog_id to prefix. 582 700 * @return array Table names. 583 701 */ 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; 702 function tables( $scope = 'all', $prefix = false, $blog_id = '' ) { 703 704 if ( is_array( $scope ) ) { 705 $tables = array(); 706 foreach ( $scope as $each ) 707 $tables = array_merge( $tables, $this->tables($each) ); 708 } else { 709 switch ( $scope ) { 710 case 'old' : 711 $tables = $this->old_tables; 712 break; 713 case 'blog' : 714 $tables = $this->tables; 715 break; 716 case 'global' : 717 $tables = array_merge( $this->global_tables, $this->ms_tables ); 718 if ( is_multisite() ) 719 $tables = array_merge( $tables, $this->ms_tables ); 720 break; 721 case 'all' : 722 $tables = $this->tables( array( 'global', 'blog' ) ); 723 break; 724 } 600 725 } 601 726 602 if ( $prefix ) {727 if ( count( $tables ) && $prefix ) { 603 728 $prefix = $this->get_blog_prefix( $blog_id ); 604 foreach ( $tables as $k => $table ) {729 foreach ( $tables as $k => $table ) 605 730 $tables[$k] = $prefix . $table; 606 }607 731 } 608 732 609 733 return $tables; … … 620 744 * @param string $db MySQL database name 621 745 * @return null Always null. 622 746 */ 623 function select( $db, &$dbh) {624 if ( !@mysql_select_db($db, $dbh)) {747 function select( $db, &$dbh ) { 748 if ( !@mysql_select_db( $db, $dbh ) ) { 625 749 $this->ready = false; 626 $this->bail( sprintf(/*WP_I18N_DB_SELECT_DB*/'750 $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/' 627 751 <h1>Can’t select database</h1> 628 752 <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 753 <ul> … … 631 755 <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li> 632 756 <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 757 </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');758 <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 759 return; 636 760 } 637 761 } 638 762 639 function _weak_escape($string) { 640 return addslashes($string); 763 /** 764 * weak escape 765 * 766 * an alias to addslashes() 767 * 768 * @see addslashes() 769 * 770 * @since {@internal Version Unknown}} 771 * @access private 772 * @param string $string 773 * @return string 774 */ 775 function _weak_escape( $string ) { 776 return addslashes( $string ); 641 777 } 642 778 643 function _real_escape($string) { 779 /** 780 * real escape 781 * 782 * escape via mysql_real_escape_string() or addslashes() 783 * 784 * @since 2.8 785 * @access private 786 * 787 * @param string $string to escape 788 * @return string escaped 789 */ 790 function _real_escape( $string ) { 644 791 if ( $this->dbh && $this->real_escape ) 645 792 return mysql_real_escape_string( $string, $this->dbh ); 646 793 else 647 794 return addslashes( $string ); 648 795 } 649 796 650 function _escape($data) { 651 if ( is_array($data) ) { 797 /** 798 * escape 799 * 800 * escape data, uses {@see _real_escape()} and works 801 * on arrays as well. 802 * 803 * @since 2.8 804 * @access private 805 * 806 * @param string|array $data to escape 807 * @return string|array escaped 808 */ 809 function _escape( $data ) { 810 if ( is_array( $data ) ) { 652 811 foreach ( (array) $data as $k => $v ) { 653 812 if ( is_array($v) ) 654 813 $data[$k] = $this->_escape( $v ); … … 667 826 * 668 827 * @since 0.71 669 828 * 670 * @param string|array $data671 * @return string query safe string829 * @param string|array $data to escape 830 * @return string|array escaped as query safe string 672 831 */ 673 function escape( $data) {674 if ( is_array( $data) ) {832 function escape( $data ) { 833 if ( is_array( $data ) ) { 675 834 foreach ( (array) $data as $k => $v ) { 676 if ( is_array( $v) )835 if ( is_array( $v ) ) 677 836 $data[$k] = $this->escape( $v ); 678 837 else 679 838 $data[$k] = $this->_weak_escape( $v ); … … 690 849 * 691 850 * @since 2.3.0 692 851 * 693 * @param string $s 852 * @param string $string to escape 853 * @return void 694 854 */ 695 function escape_by_ref( &$string) {855 function escape_by_ref( &$string ) { 696 856 $string = $this->_real_escape( $string ); 697 857 } 698 858 699 859 /** 700 860 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. 701 861 * 862 * The following directives can be used in the query format string: 863 * 864 * %d (decimal number) 865 * %s (string) 866 * %% (literal percentage sign - no argument needed) 867 * 868 * Both %d and %s are to be left unquoted in the query string and 869 * they need an argument passed for them. 870 * Literals (%) as parts of the query must be properly written 871 * as %%. 872 * 702 873 * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string). 703 874 * Does not support sign, padding, alignment, width or precision specifiers. 704 875 * Does not support argument numbering/swapping. … … 709 880 * 710 881 * <code> 711 882 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 ) 883 * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); 712 884 * </code> 713 885 * 714 886 * @link http://php.net/sprintf Description of syntax. 715 887 * @since 2.3.0 716 888 * 717 889 * @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()}.890 * @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 891 * @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 string892 * @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 893 */ 722 function prepare( $query = null) { // ( $query, *$args )894 function prepare( $query = null ) { // ( $query, *$args ) 723 895 if ( is_null( $query ) ) 724 896 return; 897 725 898 $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);899 array_shift( $args ); 900 if ( isset( $args[0] ) && is_array( $args[0] ) ) 901 $args = $args[0]; # re-assign args passed as array like in vsprintf 902 903 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it 904 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting 905 $query = str_replace( '%s', "'%s'", $query ); // quote the strings 906 array_walk( $args, array( &$this, 'escape_by_ref' ) ); 907 return @vsprintf( $query, $args ); 735 908 } 736 909 737 910 /** … … 743 916 * @param string $str The error to display 744 917 * @return bool False if the showing of errors is disabled. 745 918 */ 746 function print_error( $str = '') {919 function print_error( $str = '' ) { 747 920 global $EZSQL_ERROR; 748 921 749 if ( !$str) $str = mysql_error($this->dbh);750 $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);922 if ( !$str ) $str = mysql_error( $this->dbh ); 923 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 751 924 752 925 if ( $this->suppress_errors ) 753 926 return false; … … 757 930 else 758 931 $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 932 760 $log_error = true; 761 if ( ! function_exists('error_log') ) 762 $log_error = false; 933 if ( function_exists( 'error_log' ) 934 && !( $log_file = @ini_get( 'error_log' ) ) 935 && ( 'syslog' != $log_file ) 936 && @is_writable( $log_file ) ) 937 @error_log( $error_str ); 763 938 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 939 // Is error output turned on or not.. 772 940 if ( !$this->show_errors ) 773 941 return false; … … 778 946 if ( defined( 'ERRORLOGFILE' ) ) 779 947 error_log( $msg, 3, ERRORLOGFILE ); 780 948 if ( defined( 'DIEONDBERROR' ) ) 781 die( $msg );949 wp_die( $msg ); 782 950 } else { 783 $str = htmlspecialchars($str, ENT_QUOTES);784 $query = htmlspecialchars( $this->last_query, ENT_QUOTES);951 $str = htmlspecialchars( $str, ENT_QUOTES ); 952 $query = htmlspecialchars( $this->last_query, ENT_QUOTES ); 785 953 786 954 print "<div id='error'> 787 955 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> … … 799 967 * errors. 800 968 * 801 969 * @since 0.71 970 * @see hide_errors() 802 971 * 803 972 * @param bool $show Whether to show or hide errors 804 973 * @return bool Old value for showing errors. … … 812 981 /** 813 982 * Disables showing of database errors. 814 983 * 984 * By default database errors are not shown. 985 * 815 986 * @since 0.71 987 * @see show_errors() 816 988 * 817 989 * @return bool Whether showing of errors was active or not 818 990 */ … … 825 997 /** 826 998 * Whether to suppress database errors. 827 999 * 828 * @param unknown_type $suppress 829 * @return unknown 1000 * By default database errors are suppressed, with a simple 1001 * call to this function they can be enabled. 1002 * 1003 * @since 2.5 1004 * @see hide_errors() 1005 * @param bool $suppress (optional) new value, defaults to true 1006 * @return bool old suppress errors value 830 1007 */ 831 1008 function suppress_errors( $suppress = true ) { 832 1009 $errors = $this->suppress_errors; 833 $this->suppress_errors = $suppress;1010 $this->suppress_errors = (bool) $suppress; 834 1011 return $errors; 835 1012 } 836 1013 … … 838 1015 * Kill cached query results. 839 1016 * 840 1017 * @since 0.71 1018 * 1019 * @return void 841 1020 */ 842 1021 function flush() { 843 1022 $this->last_result = array(); 844 $this->col_info = null;845 $this->last_query = null;1023 $this->col_info = null; 1024 $this->last_query = null; 846 1025 } 847 1026 848 1027 function db_connect( $query = "SELECT" ) { … … 865 1044 } 866 1045 867 1046 $dbhname = "dbh" . $action; 868 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 1047 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 869 1048 if (!$this->$dbhname ) { 870 $this->bail( "1049 $this->bail( sprintf( /*WP_I18N_DB_CONNECT_DB*/' 871 1050 <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>1051 <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 1052 <ul> 874 1053 <li>Are you sure you have the correct username and password?</li> 875 1054 <li>Are you sure that you have typed the correct hostname?</li> 876 1055 <li>Are you sure that the database server is running?</li> 877 1056 </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 "); 1057 <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 1058 } 881 1059 $this->select( $details[ 'db_name' ], $this->$dbhname ); 882 1060 } … … 888 1066 * 889 1067 * @since 0.71 890 1068 * 891 * @param string $query 1069 * @param string $query database query 892 1070 * @return int|false Number of rows affected/selected or false on error 893 1071 */ 894 function query( $query) {1072 function query( $query ) { 895 1073 if ( ! $this->ready ) 896 1074 return false; 897 1075 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); 1076 if ( function_exists( 'apply_filters' ) ) 1077 $query = apply_filters( 'query', $query ); 902 1078 903 // init ialise return1079 // init return values and object's state 904 1080 $return_val = 0; 905 1081 $this->flush(); 906 1082 … … 910 1086 // Keep track of the last query for debug.. 911 1087 $this->last_query = $query; 912 1088 913 // Perform the query via std mysql_query function.. 914 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 1089 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 915 1090 $this->timer_start(); 916 1091 917 1092 // use $this->dbh for read ops, and $this->dbhwrite for write ops … … 939 1114 $this->last_db_used = "other/read"; 940 1115 } 941 1116 942 $this->result = @mysql_query($query, $dbh); 943 ++$this->num_queries; 1117 // Perform the query via std mysql_query function.. 1118 $this->result = @mysql_query( $query, $dbh ); 1119 $this->num_queries++; 944 1120 945 1121 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 946 1122 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 947 1123 948 1124 // If there is an error then take note of it.. 949 if ( $this->last_error = mysql_error( $dbh) ) {1125 if ( $this->last_error = mysql_error( $dbh ) ) { 950 1126 $this->print_error(); 951 1127 return false; 952 1128 } 953 1129 954 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {955 $this->rows_affected = mysql_affected_rows( $dbh);1130 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) { 1131 $this->rows_affected = mysql_affected_rows( $dbh ); 956 1132 // Take note of the insert_id 957 if ( preg_match( "/^\\s*(insert|replace) /i",$query) ) {1133 if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) { 958 1134 $this->insert_id = mysql_insert_id($dbh); 959 1135 } 960 1136 // Return number of rows affected 961 1137 $return_val = $this->rows_affected; 962 1138 } else { 963 1139 $i = 0; 964 while ( $i < @mysql_num_fields($this->result)) {965 $this->col_info[$i] = @mysql_fetch_field( $this->result);1140 while ( $i < @mysql_num_fields( $this->result ) ) { 1141 $this->col_info[$i] = @mysql_fetch_field( $this->result ); 966 1142 $i++; 967 1143 } 968 1144 $num_rows = 0; 969 while ( $row = @mysql_fetch_object( $this->result) ) {1145 while ( $row = @mysql_fetch_object( $this->result ) ) { 970 1146 $this->last_result[$num_rows] = $row; 971 1147 $num_rows++; 972 1148 } 973 1149 974 @mysql_free_result( $this->result);1150 @mysql_free_result( $this->result ); 975 1151 976 1152 // Log number of rows the query returned 1153 // and Return number of rows selected 977 1154 $this->num_rows = $num_rows; 978 979 // Return number of rows selected 980 $return_val = $this->num_rows; 1155 $return_val = $num_rows; 981 1156 } 982 1157 983 1158 return $return_val; … … 987 1162 * Insert a row into a table. 988 1163 * 989 1164 * <code> 1165 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) ) 990 1166 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) 991 1167 * </code> 992 1168 * … … 998 1174 * @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 1175 * @return int|false The number of rows inserted, or false on error. 1000 1176 */ 1001 function insert( $table, $data, $format = null) {1177 function insert( $table, $data, $format = null ) { 1002 1178 $formats = $format = (array) $format; 1003 $fields = array_keys( $data);1179 $fields = array_keys( $data ); 1004 1180 $formatted_fields = array(); 1005 1181 foreach ( $fields as $field ) { 1006 if ( !empty( $format) )1007 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];1008 elseif ( isset( $this->field_types[$field]) )1182 if ( !empty( $format ) ) 1183 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1184 elseif ( isset( $this->field_types[$field] ) ) 1009 1185 $form = $this->field_types[$field]; 1010 1186 else 1011 1187 $form = '%s'; 1012 1188 $formatted_fields[] = $form; 1013 1189 } 1014 1190 $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')"; 1015 return $this->query( $this->prepare( $sql, $data ) );1191 return $this->query( $this->prepare( $sql, $data ) ); 1016 1192 } 1017 1193 1018 1194 … … 1020 1196 * Update a row in the table 1021 1197 * 1022 1198 * <code> 1199 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) ) 1023 1200 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) 1024 1201 * </code> 1025 1202 * … … 1033 1210 * @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 1211 * @return int|false The number of rows updated, or false on error. 1035 1212 */ 1036 function update( $table, $data, $where, $format = null, $where_format = null) {1037 if ( !is_array( $ where ) )1213 function update( $table, $data, $where, $format = null, $where_format = null ) { 1214 if ( !is_array( $data ) || !is_array( $where ) ) 1038 1215 return false; 1039 1216 1040 1217 $formats = $format = (array) $format; 1041 1218 $bits = $wheres = array(); 1042 foreach ( (array) array_keys( $data) as $field ) {1043 if ( !empty( $format) )1044 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];1219 foreach ( (array) array_keys( $data ) as $field ) { 1220 if ( !empty( $format ) ) 1221 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1045 1222 elseif ( isset($this->field_types[$field]) ) 1046 1223 $form = $this->field_types[$field]; 1047 1224 else … … 1050 1227 } 1051 1228 1052 1229 $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]) )1230 foreach ( (array) array_keys( $where ) as $field ) { 1231 if ( !empty( $where_format ) ) 1232 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1233 elseif ( isset( $this->field_types[$field] ) ) 1057 1234 $form = $this->field_types[$field]; 1058 1235 else 1059 1236 $form = '%s'; … … 1061 1238 } 1062 1239 1063 1240 $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))) );1241 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); 1065 1242 } 1066 1243 1067 1244 /** … … 1073 1250 * 1074 1251 * @since 0.71 1075 1252 * 1076 * @param string|null $query SQL query. If null, use the result from the previous query.1253 * @param string|null $query (optional) SQL query. Defaults to NULL, use the result from the previous query. 1077 1254 * @param int $x (optional) Column of value to return. Indexed from 0. 1078 1255 * @param int $y (optional) Row of value to return. Indexed from 0. 1079 * @return string Database query result1256 * @return string|null Database query result on success (as string), NULL if a value on failure 1080 1257 */ 1081 function get_var( $query=null, $x = 0, $y = 0) {1082 $this->func_call = "\$db->get_var(\"$query\", $x,$y)";1258 function get_var( $query = null, $x = 0, $y = 0 ) { 1259 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; 1083 1260 if ( $query ) 1084 $this->query( $query);1261 $this->query( $query ); 1085 1262 1086 1263 // Extract var out of cached results based x,y vals 1087 1264 if ( !empty( $this->last_result[$y] ) ) { 1088 $values = array_values( get_object_vars($this->last_result[$y]));1265 $values = array_values( get_object_vars( $this->last_result[$y] ) ); 1089 1266 } 1090 1267 1091 1268 // If there is a value return it else return null 1092 return ( isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;1269 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; 1093 1270 } 1094 1271 1095 1272 /** … … 1102 1279 * @param string|null $query SQL query. 1103 1280 * @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 1281 * @param int $y (optional) Row to return. Indexed from 0. 1105 * @return mixed Database query result in format specifed by $output 1282 * @return mixed Database query result in format specifed by $output or NULL 1283 * @return void 1106 1284 */ 1107 function get_row( $query = null, $output = OBJECT, $y = 0) {1285 function get_row( $query = null, $output = OBJECT, $y = 0 ) { 1108 1286 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; 1109 1287 if ( $query ) 1110 $this->query( $query);1288 $this->query( $query ); 1111 1289 else 1112 1290 return null; 1113 1291 1114 if ( !isset( $this->last_result[$y]) )1292 if ( !isset( $this->last_result[$y] ) ) 1115 1293 return null; 1116 1294 1117 1295 if ( $output == OBJECT ) { 1118 1296 return $this->last_result[$y] ? $this->last_result[$y] : null; 1119 1297 } elseif ( $output == ARRAY_A ) { 1120 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y]) : null;1298 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; 1121 1299 } elseif ( $output == ARRAY_N ) { 1122 return $this->last_result[$y] ? array_values( get_object_vars($this->last_result[$y])) : null;1300 return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; 1123 1301 } else { 1124 1302 $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 1303 } … … 1134 1312 * 1135 1313 * @since 0.71 1136 1314 * 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.1315 * @param string|null $query (optional) SQL query. If null, use the result from the previous query. 1316 * @param int $x (optional) Column to return. Indexed from 0. 1139 1317 * @return array Database query result. Array indexed from 0 by SQL result row number. 1140 1318 */ 1141 function get_col( $query = null , $x = 0) {1319 function get_col( $query = null , $x = 0 ) { 1142 1320 if ( $query ) 1143 $this->query( $query);1321 $this->query( $query ); 1144 1322 1145 1323 $new_array = array(); 1146 1324 // Extract the column values 1147 for ( $i=0; $i < count( $this->last_result); $i++ ) {1148 $new_array[$i] = $this->get_var( null, $x, $i);1325 for ( $i=0; $i < count( $this->last_result ); $i++ ) { 1326 $new_array[$i] = $this->get_var( null, $x, $i ); 1149 1327 } 1150 1328 return $new_array; 1151 1329 } … … 1161 1339 * @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 1340 * @return mixed Database query results 1163 1341 */ 1164 function get_results( $query = null, $output = OBJECT) {1342 function get_results( $query = null, $output = OBJECT ) { 1165 1343 $this->func_call = "\$db->get_results(\"$query\", $output)"; 1166 1344 1167 1345 if ( $query ) 1168 $this->query( $query);1346 $this->query( $query ); 1169 1347 else 1170 1348 return null; 1171 1349 … … 1206 1384 * 1207 1385 * @since 0.71 1208 1386 * 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 type1387 * @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 1388 * @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 1389 * @return mixed Column Results 1212 1390 */ 1213 function get_col_info( $info_type = 'name', $col_offset = -1) {1391 function get_col_info( $info_type = 'name', $col_offset = -1 ) { 1214 1392 if ( $this->col_info ) { 1215 1393 if ( $col_offset == -1 ) { 1216 $i = 0;1394 $i = 0; 1217 1395 $new_array = array(); 1218 1396 foreach( (array) $this->col_info as $col ) { 1219 1397 $new_array[$i] = $col->{$info_type}; … … 1234 1412 * @return true 1235 1413 */ 1236 1414 function timer_start() { 1237 $mtime = microtime(); 1238 $mtime = explode(' ', $mtime); 1415 $mtime = explode( ' ', microtime() ); 1239 1416 $this->time_start = $mtime[1] + $mtime[0]; 1240 1417 return true; 1241 1418 } … … 1248 1425 * @return int Total time spent on the query, in milliseconds 1249 1426 */ 1250 1427 function timer_stop() { 1251 $mtime = microtime(); 1252 $mtime = explode(' ', $mtime); 1253 $time_end = $mtime[1] + $mtime[0]; 1428 $mtime = explode( ' ', microtime() ); 1429 $time_end = $mtime[1] + $mtime[0]; 1254 1430 $time_total = $time_end - $this->time_start; 1255 1431 return $time_total; 1256 1432 } … … 1266 1442 * @param string $error_code (optional) A Computer readable string to identify the error. 1267 1443 * @return false|void 1268 1444 */ 1269 function bail( $message, $error_code = '500') {1445 function bail( $message, $error_code = '500' ) { 1270 1446 if ( !$this->show_errors ) { 1271 if ( class_exists( 'WP_Error') )1447 if ( class_exists( 'WP_Error' ) ) 1272 1448 $this->error = new WP_Error($error_code, $message); 1273 1449 else 1274 1450 $this->error = $message; … … 1282 1458 * 1283 1459 * @since 2.5.0 1284 1460 * @uses $wp_version 1461 * @uses $required_mysql_version 1285 1462 * 1286 1463 * @return WP_Error 1287 1464 */ … … 1289 1466 global $wp_version, $required_mysql_version; 1290 1467 // Make sure the server has the required MySQL version 1291 1468 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));1469 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 1470 } 1294 1471 1295 1472 /** … … 1306 1483 } 1307 1484 1308 1485 /** 1309 * Generic function to determine if a database supports a particular feature 1486 * has capability 1487 * 1488 * Determine if a database supports a particular feature 1489 * 1490 * @since 2.7 1491 * @see db_version() 1492 * 1310 1493 * @param string $db_cap the feature 1311 1494 * @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 1495 * @return bool … … 1318 1501 case 'collation' : // @since 2.5.0 1319 1502 case 'group_concat' : // @since 2.7 1320 1503 case 'subqueries' : // @since 2.7 1321 return version_compare( $version, '4.1', '>=');1504 return version_compare( $version, '4.1', '>=' ); 1322 1505 break; 1323 1506 endswitch; 1324 1507 … … 1336 1519 * @return string The name of the calling function 1337 1520 */ 1338 1521 function get_caller() { 1339 $ bt = debug_backtrace();1522 $trace = array_reverse( debug_backtrace() ); 1340 1523 $caller = array(); 1341 1524 1342 $bt = array_reverse( $bt ); 1343 foreach ( (array) $bt as $call ) { 1525 foreach ( $trace as $call ) { 1344 1526 if ( isset( $call['class'] ) && __CLASS__ == $call['class'] ) 1345 continue; 1527 continue; # filter out function calls of this object's class 1528 1346 1529 $function = $call['function']; 1530 1347 1531 if ( isset( $call['class'] ) ) 1348 1532 $function = $call['class'] . "->$function"; 1533 1349 1534 $caller[] = $function; 1350 1535 } 1351 $caller = join( ', ', $caller );1352 1536 1353 return $caller;1537 return join( ', ', $caller ); 1354 1538 } 1355 1539 1356 1540 /** 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. 1541 * get database version number 1542 * 1543 * ADDITIONAL PARAMETER NOTICE 1544 * 1545 * there once was a proposed second parameter which has never been 1546 * implemented. It was describben as "Which database to test" ($dbh_or_table) 1547 * 1548 * It would have had three different types: 1549 * 1550 * false : currently selected database 1551 * string : database containing this table 1552 * resource : database by mysql resource 1553 * 1554 * regarding that third parameter please see {@see db_version()} as well 1555 * 1556 * @since 2.7 1557 * @see has_cap() 1558 * 1359 1559 * @return false|string false on failure, version number on success 1360 1560 */ 1361 1561 function db_version() { 1362 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));1562 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 1363 1563 } 1364 1564 } 1365 1565 … … 1371 1571 */ 1372 1572 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); 1373 1573 } 1374 ?> 1574 ?> 1575 No newline at end of file