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