Ticket #11644: 11644.code_cleanup_wpdb.6.patch
| File 11644.code_cleanup_wpdb.6.patch, 39.8 KB (added by hakre, 3 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 /** 91 111 * Saved result of the last query made 92 112 * 93 113 * @since 1.2.0 … … 97 117 var $last_query; 98 118 99 119 /** 120 * Saved resultset of the last query made 121 * 122 * @since {@internal Version Unknown} 123 * @access private 124 * @var array (null if unintialized) 125 */ 126 var $last_result; 127 128 /** 100 129 * Saved info on the table column 101 130 * 102 131 * @since 1.2.0 … … 135 164 * @var bool 136 165 */ 137 166 var $ready = false; 167 168 /** 169 * {@internal Missing Description}} 170 * 171 * @since 3.0 172 * @access private 173 * @var unknown_type 174 */ 138 175 var $blogid = 0; 176 177 /** 178 * {@internal Missing Description}} 179 * 180 * @since 3.0 181 * @access private 182 * @var unknown_type 183 */ 139 184 var $siteid = 0; 140 185 141 186 /** … … 250 295 251 296 /** 252 297 * List of deprecated WordPress tables 298 * 299 * - categories, post2cat and link2cat are deprecated 300 * since 2.3.0 / db version >= 5539. 253 301 * 254 302 * @since 2.9.0 255 303 * @access private … … 406 454 * @param string $dbname MySQL database name 407 455 * @param string $dbhost MySQL database host 408 456 */ 409 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost) {457 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) { 410 458 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) 411 459 $this->db_connect(); 412 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost);460 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost ); 413 461 } 414 462 415 463 /** … … 426 474 * @param string $dbname MySQL database name 427 475 * @param string $dbhost MySQL database host 428 476 */ 429 function __construct( $dbuser, $dbpassword, $dbname, $dbhost) {430 register_shutdown_function( array(&$this, "__destruct"));477 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { 478 register_shutdown_function( array( &$this, '__destruct' ) ); # prevents the object from unloading 431 479 432 480 if ( WP_DEBUG ) 433 481 $this->show_errors(); … … 442 490 $this->collate = DB_COLLATE; 443 491 } 444 492 445 if ( defined( 'DB_CHARSET') )493 if ( defined( 'DB_CHARSET' ) ) 446 494 $this->charset = DB_CHARSET; 447 495 448 496 $this->dbuser = $dbuser; 449 497 450 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true);451 if ( !$this->dbh) {452 $this->bail( sprintf(/*WP_I18N_DB_CONN_ERROR*/"498 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true ); 499 if ( !$this->dbh ) { 500 $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/" 453 501 <h1>Error establishing a database connection</h1> 454 502 <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 503 <ul> … … 458 506 <li>Are you sure that the database server is running?</li> 459 507 </ul> 460 508 <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');509 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' ); 462 510 return; 463 511 } 464 512 465 513 $this->ready = true; 466 514 467 if ( $this->has_cap( 'collation' ) && !empty( $this->charset) ) {468 if ( function_exists( 'mysql_set_charset') ) {469 mysql_set_charset( $this->charset, $this->dbh);515 if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) { 516 if ( function_exists( 'mysql_set_charset' ) ) { 517 mysql_set_charset( $this->charset, $this->dbh ); 470 518 $this->real_escape = true; 471 519 } else { 472 $ collation_query = "SET NAMES '{$this->charset}'";473 if ( !empty( $this->collate) )474 $ collation_query .= " COLLATE '{$this->collate}'";475 $this->query( $collation_query);520 $query = $this->prepare( 'SET NAMES %s', $this->charset ); 521 if ( !empty( $this->collate ) ) 522 $query .= $this->prepare( ' COLLATE %s', $this->collate ); 523 $this->query( $query ); 476 524 } 477 525 } 478 526 479 $this->select( $dbname, $this->dbh);527 $this->select( $dbname, $this->dbh ); 480 528 } 481 529 482 530 /** 483 531 * PHP5 style destructor and will run when database object is destroyed. 484 532 * 533 * 534 * Please see {@link __construct() class constructor} and the 535 * {@link register_shutdown_function() register_shutdown_function()}. for 536 * more details. 537 * 538 * @link http://www.php.net/__destruct 539 * @link http://www.php.net/register_shutdown_function 540 * 485 541 * @since 2.0.8 486 542 * 487 * @return bool Alwaystrue543 * @return bool true 488 544 */ 489 545 function __destruct() { 490 546 return true; … … 503 559 */ 504 560 function set_prefix( $prefix ) { 505 561 506 if ( preg_match( '|[^a-z0-9_]|i', $prefix) )562 if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) 507 563 return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/); 564 565 $old_prefix = is_multisite() ? '' : $prefix; 508 566 509 if ( is_multisite() )510 $old_prefix = '';511 else512 $old_prefix = $prefix;513 514 567 if ( isset( $this->base_prefix ) ) 515 568 $old_prefix = $this->base_prefix; 569 516 570 $this->base_prefix = $prefix; 571 517 572 foreach ( $this->tables( 'global' ) as $table ) 518 573 $this->$table = $prefix . $table; 519 574 520 if ( defined( 'VHOST') && empty( $this->blogid ) )575 if ( defined( 'VHOST' ) && empty( $this->blogid ) ) 521 576 return $old_prefix; 522 577 523 578 $this->prefix = $this->get_blog_prefix( $this->blogid ); 524 579 525 foreach ( (array) $this->tables( 'blog') as $table )580 foreach ( $this->tables( array('blog', 'old' ) ) as $table ) 526 581 $this->$table = $this->prefix . $table; 527 582 528 foreach ( (array) $this->tables( 'old' ) as $table ) 529 $this->$table = $this->prefix . $table; 530 531 if ( defined('CUSTOM_USER_TABLE') ) 583 if ( defined( 'CUSTOM_USER_TABLE' ) ) 532 584 $this->users = CUSTOM_USER_TABLE; 533 585 534 if ( defined( 'CUSTOM_USER_META_TABLE') )586 if ( defined( 'CUSTOM_USER_META_TABLE' ) ) 535 587 $this->usermeta = CUSTOM_USER_META_TABLE; 536 588 537 589 return $old_prefix; 538 590 } 539 591 540 function set_blog_id($blog_id, $site_id = '') { 592 /** 593 * blog id setter 594 * 595 * {@internal Missing Description}} 596 * 597 * @since 3.0 598 * @access public 599 * @param string $blog_id 600 * @param string $site_id (optional) 601 * @return string previous blog id 602 */ 603 function set_blog_id( $blog_id, $site_id = '' ) { 541 604 if ( !empty($site_id) ) 542 605 $this->siteid = $site_id; 543 606 544 $old_blog_id = $this->blogid;607 $old_blog_id = $this->blogid; 545 608 $this->blogid = $blog_id; 546 609 547 610 $this->prefix = $this->get_blog_prefix( $this->blogid ); 548 611 549 foreach ( $this->tables( 'blog') as $table )612 foreach ( $this->tables( array('blog', 'old') ) as $table ) 550 613 $this->$table = $this->prefix . $table; 551 614 552 foreach ( $this->tables( 'old' ) as $table )553 $this->$table = $this->prefix . $table;554 555 615 return $old_blog_id; 556 616 } 557 617 618 /** 619 * blog prefix getter 620 * 621 * {@internal Missing Description}} 622 * 623 * @param string $blog_id (optional) 624 * @return unknown_type 625 */ 558 626 function get_blog_prefix( $blog_id = '' ) { 559 627 if ( is_multisite() && $blog_id ) { 560 628 if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) ) … … 569 637 /** 570 638 * Returns an array of WordPress tables. 571 639 * 640 * Scope / Table identifiers: 641 * 642 * - 'all' ........ global and blog tables. 643 * - 'global' ..... global tables 644 * - 'blog' ....... blog tables 645 * - 'old tables' . old (depreacted) tables 646 * 647 * NOTE: This function does not reflect CUSTOM_USER_TABLE 648 * and CUSTOM_USER_META_TABLE and will return 649 * the standard table names (users, usermeta) instead. 650 * 651 * FIXME all does not return global and blog, it resturns more on 652 * more on multisite. fix global? merge in all? 653 * 654 * @access public 572 655 * @since 3.0.0 573 * @uses wpdb::tables 574 * @uses wpdb::old_tables 575 * @uses wpdb::global_tables 656 * @uses wpdb::$tables 657 * @uses wpdb::$old_tables 658 * @uses wpdb::$global_tables 659 * @uses wpdb::$ms_tables 576 660 * @uses is_multisite() 577 661 * 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. 662 * @param string|array $scope (optional) Scope, Default 'all'. You can pass multiple scopes per array 663 * @param bool $prefix (optional) Whether to include the blog prefix. Default FALSE. 664 * @param int $blog_id (optional) The blog_id to prefix. 582 665 * @return array Table names. 583 666 */ 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; 667 function tables( $scope = 'all', $prefix = false, $blog_id = '' ) { 668 669 if ( is_array( $scope ) ) { 670 $tables = array(); 671 foreach ( $scope as $each ) 672 $tables = array_merge( $tables, $this->tables( $each ) ); 673 } else { 674 switch ( $scope ) { 675 case 'old' : 676 $tables = $this->old_tables; 677 break; 678 case 'blog' : 679 $tables = $this->tables; 680 break; 681 case 'global' : 682 $tables = array_merge( $this->global_tables, $this->ms_tables ); 683 break; 684 case 'all' : 685 $tables = array_merge( $this->global_tables, $this->tables ); 686 if ( is_multisite() ) 687 $tables = array_merge( $tables, $this->ms_tables ); 688 break; 689 } 600 690 } 601 691 602 if ( $prefix ) {692 if ( count( $tables ) && $prefix ) { 603 693 $prefix = $this->get_blog_prefix( $blog_id ); 604 foreach ( $tables as $k => $table ) {694 foreach ( $tables as $k => $table ) 605 695 $tables[$k] = $prefix . $table; 606 }607 696 } 608 697 609 698 return $tables; … … 620 709 * @param string $db MySQL database name 621 710 * @return null Always null. 622 711 */ 623 function select( $db, &$dbh) {624 if ( !@mysql_select_db($db, $dbh)) {712 function select( $db, &$dbh ) { 713 if ( !@mysql_select_db( $db, $dbh ) ) { 625 714 $this->ready = false; 626 $this->bail( sprintf(/*WP_I18N_DB_SELECT_DB*/'715 $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/' 627 716 <h1>Can’t select database</h1> 628 717 <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 718 <ul> … … 631 720 <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li> 632 721 <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 722 </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');723 <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 724 return; 636 725 } 637 726 } 638 727 639 function _weak_escape($string) { 640 return addslashes($string); 728 /** 729 * weak escape 730 * 731 * an alias to addslashes() 732 * 733 * @see addslashes() 734 * 735 * @since {@internal Version Unknown}} 736 * @access private 737 * @param string $string 738 * @return string 739 */ 740 function _weak_escape( $string ) { 741 return addslashes( $string ); 641 742 } 642 743 643 function _real_escape($string) { 744 /** 745 * real escape 746 * 747 * escape via mysql_real_escape_string() or addslashes() 748 * 749 * @since 2.8 750 * @access private 751 * 752 * @param string $string to escape 753 * @return string escaped 754 */ 755 function _real_escape( $string ) { 644 756 if ( $this->dbh && $this->real_escape ) 645 757 return mysql_real_escape_string( $string, $this->dbh ); 646 758 else 647 759 return addslashes( $string ); 648 760 } 649 761 650 function _escape($data) { 651 if ( is_array($data) ) { 762 /** 763 * escape 764 * 765 * escape data, uses {@see _real_escape()} and works 766 * on arrays as well. 767 * 768 * @since 2.8 769 * @access private 770 * 771 * @param string|array $data to escape 772 * @return string|array escaped 773 */ 774 function _escape( $data ) { 775 if ( is_array( $data ) ) { 652 776 foreach ( (array) $data as $k => $v ) { 653 777 if ( is_array($v) ) 654 778 $data[$k] = $this->_escape( $v ); … … 667 791 * 668 792 * @since 0.71 669 793 * 670 * @param string|array $data671 * @return string query safe string794 * @param string|array $data to escape 795 * @return string|array escaped as query safe string 672 796 */ 673 function escape( $data) {674 if ( is_array( $data) ) {797 function escape( $data ) { 798 if ( is_array( $data ) ) { 675 799 foreach ( (array) $data as $k => $v ) { 676 if ( is_array( $v) )800 if ( is_array( $v ) ) 677 801 $data[$k] = $this->escape( $v ); 678 802 else 679 803 $data[$k] = $this->_weak_escape( $v ); … … 690 814 * 691 815 * @since 2.3.0 692 816 * 693 * @param string $s 817 * @param string $string to escape 818 * @return void 694 819 */ 695 function escape_by_ref( &$string) {820 function escape_by_ref( &$string ) { 696 821 $string = $this->_real_escape( $string ); 697 822 } 698 823 699 824 /** 700 825 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. 701 826 * 827 * The following directives can be used in the query format string: 828 * 829 * %d (decimal number) 830 * %s (string) 831 * %% (literal percentage sign - no argument needed) 832 * 833 * Both %d and %s are to be left unquoted in the query string and 834 * they need an argument passed for them. 835 * Literals (%) as parts of the query must be properly written 836 * as %%. 837 * 702 838 * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string). 703 839 * Does not support sign, padding, alignment, width or precision specifiers. 704 840 * Does not support argument numbering/swapping. … … 709 845 * 710 846 * <code> 711 847 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 ) 848 * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); 712 849 * </code> 713 850 * 714 851 * @link http://php.net/sprintf Description of syntax. 715 852 * @since 2.3.0 716 853 * 717 854 * @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()}.855 * @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 856 * @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 string857 * @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 858 */ 722 function prepare( $query = null) { // ( $query, *$args )859 function prepare( $query = null ) { // ( $query, *$args ) 723 860 if ( is_null( $query ) ) 724 861 return; 862 725 863 $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);864 array_shift( $args ); 865 if ( isset( $args[0] ) && is_array( $args[0] ) ) 866 $args = $args[0]; # re-assign args passed as array like in vsprintf 867 868 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it 869 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting 870 $query = str_replace( '%s', "'%s'", $query ); // quote the strings 871 array_walk( $args, array( &$this, 'escape_by_ref' ) ); 872 return @vsprintf( $query, $args ); 735 873 } 736 874 737 875 /** … … 743 881 * @param string $str The error to display 744 882 * @return bool False if the showing of errors is disabled. 745 883 */ 746 function print_error( $str = '') {884 function print_error( $str = '' ) { 747 885 global $EZSQL_ERROR; 748 886 749 if ( !$str) $str = mysql_error($this->dbh);750 $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);887 if ( !$str ) $str = mysql_error( $this->dbh ); 888 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 751 889 752 890 if ( $this->suppress_errors ) 753 891 return false; … … 757 895 else 758 896 $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 897 760 $log_error = true; 761 if ( ! function_exists('error_log') ) 762 $log_error = false; 898 if ( function_exists( 'error_log' ) 899 && !( $log_file = @ini_get( 'error_log' ) ) 900 && ( 'syslog' != $log_file ) 901 && @is_writable( $log_file ) ) 902 @error_log( $error_str ); 763 903 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 904 // Is error output turned on or not.. 772 905 if ( !$this->show_errors ) 773 906 return false; … … 778 911 if ( defined( 'ERRORLOGFILE' ) ) 779 912 error_log( $msg, 3, ERRORLOGFILE ); 780 913 if ( defined( 'DIEONDBERROR' ) ) 781 die( $msg );914 wp_die( $msg ); 782 915 } else { 783 $str = htmlspecialchars($str, ENT_QUOTES);784 $query = htmlspecialchars( $this->last_query, ENT_QUOTES);916 $str = htmlspecialchars( $str, ENT_QUOTES ); 917 $query = htmlspecialchars( $this->last_query, ENT_QUOTES ); 785 918 786 919 print "<div id='error'> 787 920 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> … … 799 932 * errors. 800 933 * 801 934 * @since 0.71 935 * @see hide_errors() 802 936 * 803 937 * @param bool $show Whether to show or hide errors 804 938 * @return bool Old value for showing errors. … … 812 946 /** 813 947 * Disables showing of database errors. 814 948 * 949 * By default database errors are not shown. 950 * 815 951 * @since 0.71 952 * @see show_errors() 816 953 * 817 954 * @return bool Whether showing of errors was active or not 818 955 */ … … 825 962 /** 826 963 * Whether to suppress database errors. 827 964 * 828 * @param unknown_type $suppress 829 * @return unknown 965 * By default database errors are suppressed, with a simple 966 * call to this function they can be enabled. 967 * 968 * @since 2.5 969 * @see hide_errors() 970 * @param bool $suppress (optional) new value, defaults to true 971 * @return bool old suppress errors value 830 972 */ 831 973 function suppress_errors( $suppress = true ) { 832 974 $errors = $this->suppress_errors; 833 $this->suppress_errors = $suppress;975 $this->suppress_errors = (bool) $suppress; 834 976 return $errors; 835 977 } 836 978 … … 838 980 * Kill cached query results. 839 981 * 840 982 * @since 0.71 983 * 984 * @return void 841 985 */ 842 986 function flush() { 843 987 $this->last_result = array(); 844 $this->col_info = null;845 $this->last_query = null;988 $this->col_info = null; 989 $this->last_query = null; 846 990 } 847 991 848 992 function db_connect( $query = "SELECT" ) { … … 865 1009 } 866 1010 867 1011 $dbhname = "dbh" . $action; 868 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 1012 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 869 1013 if (!$this->$dbhname ) { 870 $this->bail( "1014 $this->bail( sprintf( /*WP_I18N_DB_CONNECT_DB*/' 871 1015 <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>1016 <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 1017 <ul> 874 1018 <li>Are you sure you have the correct username and password?</li> 875 1019 <li>Are you sure that you have typed the correct hostname?</li> 876 1020 <li>Are you sure that the database server is running?</li> 877 1021 </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 "); 1022 <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 1023 } 881 1024 $this->select( $details[ 'db_name' ], $this->$dbhname ); 882 1025 } … … 888 1031 * 889 1032 * @since 0.71 890 1033 * 891 * @param string $query 1034 * @param string $query database query 892 1035 * @return int|false Number of rows affected/selected or false on error 893 1036 */ 894 function query( $query) {1037 function query( $query ) { 895 1038 if ( ! $this->ready ) 896 1039 return false; 897 1040 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); 1041 if ( function_exists( 'apply_filters' ) ) 1042 $query = apply_filters( 'query', $query ); 902 1043 903 // init ialise return1044 // init return values and object's state 904 1045 $return_val = 0; 905 1046 $this->flush(); 906 1047 … … 910 1051 // Keep track of the last query for debug.. 911 1052 $this->last_query = $query; 912 1053 913 // Perform the query via std mysql_query function.. 914 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 1054 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 915 1055 $this->timer_start(); 916 1056 917 1057 // use $this->dbh for read ops, and $this->dbhwrite for write ops … … 939 1079 $this->last_db_used = "other/read"; 940 1080 } 941 1081 942 $this->result = @mysql_query($query, $dbh); 943 ++$this->num_queries; 1082 // Perform the query via std mysql_query function.. 1083 $this->result = @mysql_query( $query, $dbh ); 1084 $this->num_queries++; 944 1085 945 1086 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 946 1087 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 947 1088 948 1089 // If there is an error then take note of it.. 949 if ( $this->last_error = mysql_error( $dbh) ) {1090 if ( $this->last_error = mysql_error( $dbh ) ) { 950 1091 $this->print_error(); 951 1092 return false; 952 1093 } 953 1094 954 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {955 $this->rows_affected = mysql_affected_rows( $dbh);1095 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) { 1096 $this->rows_affected = mysql_affected_rows( $dbh ); 956 1097 // Take note of the insert_id 957 if ( preg_match( "/^\\s*(insert|replace) /i",$query) ) {1098 if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) { 958 1099 $this->insert_id = mysql_insert_id($dbh); 959 1100 } 960 1101 // Return number of rows affected 961 1102 $return_val = $this->rows_affected; 962 1103 } else { 963 1104 $i = 0; 964 while ( $i < @mysql_num_fields($this->result)) {965 $this->col_info[$i] = @mysql_fetch_field( $this->result);1105 while ( $i < @mysql_num_fields( $this->result ) ) { 1106 $this->col_info[$i] = @mysql_fetch_field( $this->result ); 966 1107 $i++; 967 1108 } 968 1109 $num_rows = 0; 969 while ( $row = @mysql_fetch_object( $this->result) ) {1110 while ( $row = @mysql_fetch_object( $this->result ) ) { 970 1111 $this->last_result[$num_rows] = $row; 971 1112 $num_rows++; 972 1113 } 973 1114 974 @mysql_free_result( $this->result);1115 @mysql_free_result( $this->result ); 975 1116 976 1117 // Log number of rows the query returned 1118 // and Return number of rows selected 977 1119 $this->num_rows = $num_rows; 978 979 // Return number of rows selected 980 $return_val = $this->num_rows; 1120 $return_val = $num_rows; 981 1121 } 982 1122 983 1123 return $return_val; … … 987 1127 * Insert a row into a table. 988 1128 * 989 1129 * <code> 1130 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) ) 990 1131 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) 991 1132 * </code> 992 1133 * … … 998 1139 * @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 1140 * @return int|false The number of rows inserted, or false on error. 1000 1141 */ 1001 function insert( $table, $data, $format = null) {1142 function insert( $table, $data, $format = null ) { 1002 1143 $formats = $format = (array) $format; 1003 $fields = array_keys( $data);1144 $fields = array_keys( $data ); 1004 1145 $formatted_fields = array(); 1005 1146 foreach ( $fields as $field ) { 1006 if ( !empty( $format) )1007 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];1008 elseif ( isset( $this->field_types[$field]) )1147 if ( !empty( $format ) ) 1148 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1149 elseif ( isset( $this->field_types[$field] ) ) 1009 1150 $form = $this->field_types[$field]; 1010 1151 else 1011 1152 $form = '%s'; 1012 1153 $formatted_fields[] = $form; 1013 1154 } 1014 1155 $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')"; 1015 return $this->query( $this->prepare( $sql, $data ) );1156 return $this->query( $this->prepare( $sql, $data ) ); 1016 1157 } 1017 1158 1018 1159 … … 1020 1161 * Update a row in the table 1021 1162 * 1022 1163 * <code> 1164 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) ) 1023 1165 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) 1024 1166 * </code> 1025 1167 * … … 1033 1175 * @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 1176 * @return int|false The number of rows updated, or false on error. 1035 1177 */ 1036 function update( $table, $data, $where, $format = null, $where_format = null) {1037 if ( !is_array( $ where ) )1178 function update( $table, $data, $where, $format = null, $where_format = null ) { 1179 if ( !is_array( $data ) || !is_array( $where ) ) 1038 1180 return false; 1039 1181 1040 1182 $formats = $format = (array) $format; 1041 1183 $bits = $wheres = array(); 1042 foreach ( (array) array_keys( $data) as $field ) {1043 if ( !empty( $format) )1044 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];1184 foreach ( (array) array_keys( $data ) as $field ) { 1185 if ( !empty( $format ) ) 1186 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1045 1187 elseif ( isset($this->field_types[$field]) ) 1046 1188 $form = $this->field_types[$field]; 1047 1189 else … … 1050 1192 } 1051 1193 1052 1194 $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]) )1195 foreach ( (array) array_keys( $where ) as $field ) { 1196 if ( !empty( $where_format ) ) 1197 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1198 elseif ( isset( $this->field_types[$field] ) ) 1057 1199 $form = $this->field_types[$field]; 1058 1200 else 1059 1201 $form = '%s'; … … 1061 1203 } 1062 1204 1063 1205 $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))) );1206 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); 1065 1207 } 1066 1208 1067 1209 /** … … 1073 1215 * 1074 1216 * @since 0.71 1075 1217 * 1076 * @param string|null $query SQL query. If null, use the result from the previous query.1218 * @param string|null $query (optional) SQL query. Defaults to NULL, use the result from the previous query. 1077 1219 * @param int $x (optional) Column of value to return. Indexed from 0. 1078 1220 * @param int $y (optional) Row of value to return. Indexed from 0. 1079 * @return string Database query result1221 * @return string|null Database query result on success (as string), NULL if a value on failure 1080 1222 */ 1081 function get_var( $query=null, $x = 0, $y = 0) {1082 $this->func_call = "\$db->get_var(\"$query\", $x,$y)";1223 function get_var( $query = null, $x = 0, $y = 0 ) { 1224 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; 1083 1225 if ( $query ) 1084 $this->query( $query);1226 $this->query( $query ); 1085 1227 1086 1228 // Extract var out of cached results based x,y vals 1087 1229 if ( !empty( $this->last_result[$y] ) ) { 1088 $values = array_values( get_object_vars($this->last_result[$y]));1230 $values = array_values( get_object_vars( $this->last_result[$y] ) ); 1089 1231 } 1090 1232 1091 1233 // If there is a value return it else return null 1092 return ( isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;1234 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; 1093 1235 } 1094 1236 1095 1237 /** … … 1102 1244 * @param string|null $query SQL query. 1103 1245 * @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 1246 * @param int $y (optional) Row to return. Indexed from 0. 1105 * @return mixed Database query result in format specifed by $output 1247 * @return mixed Database query result in format specifed by $output or NULL 1248 * @return void 1106 1249 */ 1107 function get_row( $query = null, $output = OBJECT, $y = 0) {1250 function get_row( $query = null, $output = OBJECT, $y = 0 ) { 1108 1251 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; 1109 1252 if ( $query ) 1110 $this->query( $query);1253 $this->query( $query ); 1111 1254 else 1112 1255 return null; 1113 1256 1114 if ( !isset( $this->last_result[$y]) )1257 if ( !isset( $this->last_result[$y] ) ) 1115 1258 return null; 1116 1259 1117 1260 if ( $output == OBJECT ) { 1118 1261 return $this->last_result[$y] ? $this->last_result[$y] : null; 1119 1262 } elseif ( $output == ARRAY_A ) { 1120 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y]) : null;1263 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; 1121 1264 } elseif ( $output == ARRAY_N ) { 1122 return $this->last_result[$y] ? array_values( get_object_vars($this->last_result[$y])) : null;1265 return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; 1123 1266 } else { 1124 1267 $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 1268 } … … 1134 1277 * 1135 1278 * @since 0.71 1136 1279 * 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.1280 * @param string|null $query (optional) SQL query. If null, use the result from the previous query. 1281 * @param int $x (optional) Column to return. Indexed from 0. 1139 1282 * @return array Database query result. Array indexed from 0 by SQL result row number. 1140 1283 */ 1141 function get_col( $query = null , $x = 0) {1284 function get_col( $query = null , $x = 0 ) { 1142 1285 if ( $query ) 1143 $this->query( $query);1286 $this->query( $query ); 1144 1287 1145 1288 $new_array = array(); 1146 1289 // Extract the column values 1147 for ( $i=0; $i < count( $this->last_result); $i++ ) {1148 $new_array[$i] = $this->get_var( null, $x, $i);1290 for ( $i=0; $i < count( $this->last_result ); $i++ ) { 1291 $new_array[$i] = $this->get_var( null, $x, $i ); 1149 1292 } 1150 1293 return $new_array; 1151 1294 } … … 1161 1304 * @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 1305 * @return mixed Database query results 1163 1306 */ 1164 function get_results( $query = null, $output = OBJECT) {1307 function get_results( $query = null, $output = OBJECT ) { 1165 1308 $this->func_call = "\$db->get_results(\"$query\", $output)"; 1166 1309 1167 1310 if ( $query ) 1168 $this->query( $query);1311 $this->query( $query ); 1169 1312 else 1170 1313 return null; 1171 1314 … … 1206 1349 * 1207 1350 * @since 0.71 1208 1351 * 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 type1352 * @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 1353 * @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 1354 * @return mixed Column Results 1212 1355 */ 1213 function get_col_info( $info_type = 'name', $col_offset = -1) {1356 function get_col_info( $info_type = 'name', $col_offset = -1 ) { 1214 1357 if ( $this->col_info ) { 1215 1358 if ( $col_offset == -1 ) { 1216 $i = 0;1359 $i = 0; 1217 1360 $new_array = array(); 1218 1361 foreach( (array) $this->col_info as $col ) { 1219 1362 $new_array[$i] = $col->{$info_type}; … … 1234 1377 * @return true 1235 1378 */ 1236 1379 function timer_start() { 1237 $mtime = microtime(); 1238 $mtime = explode(' ', $mtime); 1380 $mtime = explode( ' ', microtime() ); 1239 1381 $this->time_start = $mtime[1] + $mtime[0]; 1240 1382 return true; 1241 1383 } … … 1248 1390 * @return int Total time spent on the query, in milliseconds 1249 1391 */ 1250 1392 function timer_stop() { 1251 $mtime = microtime(); 1252 $mtime = explode(' ', $mtime); 1253 $time_end = $mtime[1] + $mtime[0]; 1393 $mtime = explode( ' ', microtime() ); 1394 $time_end = $mtime[1] + $mtime[0]; 1254 1395 $time_total = $time_end - $this->time_start; 1255 1396 return $time_total; 1256 1397 } … … 1266 1407 * @param string $error_code (optional) A Computer readable string to identify the error. 1267 1408 * @return false|void 1268 1409 */ 1269 function bail( $message, $error_code = '500') {1410 function bail( $message, $error_code = '500' ) { 1270 1411 if ( !$this->show_errors ) { 1271 if ( class_exists( 'WP_Error') )1412 if ( class_exists( 'WP_Error' ) ) 1272 1413 $this->error = new WP_Error($error_code, $message); 1273 1414 else 1274 1415 $this->error = $message; … … 1282 1423 * 1283 1424 * @since 2.5.0 1284 1425 * @uses $wp_version 1426 * @uses $required_mysql_version 1285 1427 * 1286 1428 * @return WP_Error 1287 1429 */ … … 1289 1431 global $wp_version, $required_mysql_version; 1290 1432 // Make sure the server has the required MySQL version 1291 1433 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));1434 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 1435 } 1294 1436 1295 1437 /** … … 1306 1448 } 1307 1449 1308 1450 /** 1309 * Generic function to determine if a database supports a particular feature 1451 * has capability 1452 * 1453 * Determine if a database supports a particular feature 1454 * 1455 * @since 2.7 1456 * @see db_version() 1457 * 1310 1458 * @param string $db_cap the feature 1311 1459 * @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 1460 * @return bool … … 1314 1462 function has_cap( $db_cap ) { 1315 1463 $version = $this->db_version(); 1316 1464 1317 switch ( strtolower( $db_cap ) ) : 1318 case 'collation' : // @since 2.5.0 1319 case 'group_concat' : // @since 2.7 1320 case 'subqueries' : // @since 2.7 1321 return version_compare($version, '4.1', '>='); 1322 break; 1323 endswitch; 1465 switch ( strtolower( $db_cap ) ) { 1466 case 'collation' : // @since 2.5.0 1467 case 'group_concat' : // @since 2.7 1468 case 'subqueries' : // @since 2.7 1469 return version_compare( $version, '4.1', '>=' ); 1470 }; 1324 1471 1325 1472 return false; 1326 1473 } … … 1336 1483 * @return string The name of the calling function 1337 1484 */ 1338 1485 function get_caller() { 1339 $ bt = debug_backtrace();1486 $trace = array_reverse( debug_backtrace() ); 1340 1487 $caller = array(); 1341 1488 1342 $bt = array_reverse( $bt ); 1343 foreach ( (array) $bt as $call ) { 1489 foreach ( $trace as $call ) { 1344 1490 if ( isset( $call['class'] ) && __CLASS__ == $call['class'] ) 1345 continue; 1491 continue; # filter out function calls of this object's class 1492 1346 1493 $function = $call['function']; 1494 1347 1495 if ( isset( $call['class'] ) ) 1348 1496 $function = $call['class'] . "->$function"; 1497 1349 1498 $caller[] = $function; 1350 1499 } 1351 $caller = join( ', ', $caller );1352 1500 1353 return $caller;1501 return join( ', ', $caller ); 1354 1502 } 1355 1503 1356 1504 /** 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. 1505 * get database version number 1506 * 1507 * ADDITIONAL PARAMETER NOTICE 1508 * 1509 * there once was a proposed second parameter which has never been 1510 * implemented. It was describben as "Which database to test" ($dbh_or_table) 1511 * 1512 * It would have had three different types: 1513 * 1514 * false : currently selected database 1515 * string : database containing this table 1516 * resource : database by mysql resource 1517 * 1518 * regarding that third parameter please see {@see db_version()} as well 1519 * 1520 * @since 2.7 1521 * @see has_cap() 1522 * 1359 1523 * @return false|string false on failure, version number on success 1360 1524 */ 1361 1525 function db_version() { 1362 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));1526 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 1363 1527 } 1364 1528 } 1365 1529 … … 1371 1535 */ 1372 1536 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); 1373 1537 } 1374 ?> 1538 ?> 1539 No newline at end of file
