Ticket #11644: 11644.code_cleanup_wpdb.2.patch
File 11644.code_cleanup_wpdb.2.patch, 37.5 KB (added by , 15 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 … … 64 64 /** 65 65 * Whether to suppress errors during the DB bootstrapping. 66 66 * 67 * @since 2.5 67 68 * @access private 68 * @since {@internal Version Unknown}}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}} 77 * @var string 76 * @see get_last_error() 77 * 78 * @since 2.5 79 * @access private 80 * @var string 78 81 */ 79 82 var $last_error = ''; 80 83 … … 88 91 var $num_queries = 0; 89 92 90 93 /** 94 * Amount of rows returned by last query operation 95 * 96 * @since 1.2 97 * @access private 98 * @var int 99 */ 100 var $num_rows = 0; 101 102 /** 103 * Number of affected rows by last query operation 104 * 105 * @since 0.71 106 * @access private 107 * @var integer 108 */ 109 var $rows_affected = 0; 110 111 112 /** 91 113 * Saved result of the last query made 92 114 * 93 115 * @since 1.2.0 … … 97 119 var $last_query; 98 120 99 121 /** 122 * Saved resultset of the last query made 123 * 124 * @since {@internal Version Unknown} 125 * @access private 126 * @var array (null if unintialized) 127 */ 128 var $last_result; 129 130 131 /** 100 132 * Saved info on the table column 101 133 * 102 134 * @since 1.2.0 … … 135 167 * @var bool 136 168 */ 137 169 var $ready = false; 170 171 /** 172 * {@internal Missing Description}} 173 * 174 * @since 3.0 175 * @access private 176 * @var unknown_type 177 */ 138 178 var $blogid = 0; 179 180 /** 181 * {@internal Missing Description}} 182 * 183 * @since 3.0 184 * @access private 185 * @var unknown_type 186 */ 139 187 var $siteid = 0; 188 189 /** 190 * {@internal Missing Description}} 191 * 192 * @since 3.0 193 * @access private 194 * @var unknown_type 195 */ 140 196 var $blogs; 197 198 /** 199 * {@internal Missing Description}} 200 * 201 * @since 3.0 202 * @access private 203 * @var unknown_type 204 */ 141 205 var $signups; 206 207 /** 208 * {@internal Missing Description}} 209 * 210 * @since 3.0 211 * @access private 212 * @var unknown_type 213 */ 142 214 var $site; 215 216 /** 217 * {@internal Missing Description}} 218 * 219 * @since 3.0 220 * @access private 221 * @var unknown_type 222 */ 223 224 /** 225 * {@internal Missing Description}} 226 * 227 * @since 3.0 228 * @access private 229 * @var unknown_type 230 */ 143 231 var $sitemeta; 232 233 /** 234 * {@internal Missing Description}} 235 * 236 * @since 3.0 237 * @access private 238 * @var unknown_type 239 */ 144 240 var $sitecategories; 145 var $global_tables = array('blogs', 'signups', 'site', 'sitemeta', 'users', 'usermeta', 'sitecategories', 'registration_log', 'blog_versions'); 241 242 /** 243 * {@internal Missing Description}} 244 * 245 * @since 3.0 246 * @access private 247 * @var unknown_type 248 */ 249 var $global_tables = array( 'blogs', 'signups', 'site', 'sitemeta', 'users', 'usermeta', 'sitecategories', 'registration_log', 'blog_versions' ); 146 250 147 251 /** 148 252 * WordPress Posts table … … 280 384 */ 281 385 var $old_tables = array('categories', 'post2cat', 'link2cat'); 282 386 283 284 387 /** 285 388 * Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php. 286 389 * … … 344 447 * @param string $dbname MySQL database name 345 448 * @param string $dbhost MySQL database host 346 449 */ 347 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost) {450 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) { 348 451 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) 349 452 $this->db_connect(); 350 453 return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost); … … 364 467 * @param string $dbname MySQL database name 365 468 * @param string $dbhost MySQL database host 366 469 */ 367 function __construct( $dbuser, $dbpassword, $dbname, $dbhost) {368 register_shutdown_function( array(&$this, "__destruct"));470 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { 471 register_shutdown_function( array( &$this, '__destruct' ) ); # prevents the object from unloading 369 472 370 473 if ( WP_DEBUG ) 371 474 $this->show_errors(); … … 380 483 $this->collate = DB_COLLATE; 381 484 } 382 485 383 if ( defined( 'DB_CHARSET') )486 if ( defined( 'DB_CHARSET' ) ) 384 487 $this->charset = DB_CHARSET; 385 488 386 489 $this->dbuser = $dbuser; 387 490 388 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true);389 if ( !$this->dbh) {390 $this->bail( sprintf(/*WP_I18N_DB_CONN_ERROR*/"491 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true ); 492 if ( !$this->dbh ) { 493 $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/" 391 494 <h1>Error establishing a database connection</h1> 392 495 <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> 393 496 <ul> … … 396 499 <li>Are you sure that the database server is running?</li> 397 500 </ul> 398 501 <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> 399 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail');502 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' ); 400 503 return; 401 504 } 402 505 403 506 $this->ready = true; 404 507 405 if ( $this->has_cap( 'collation' ) && !empty( $this->charset) ) {406 if ( function_exists( 'mysql_set_charset') ) {407 mysql_set_charset( $this->charset, $this->dbh);508 if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) { 509 if ( function_exists( 'mysql_set_charset' ) ) { 510 mysql_set_charset( $this->charset, $this->dbh ); 408 511 $this->real_escape = true; 409 512 } else { 410 $ collation_query = "SET NAMES '{$this->charset}'";411 if ( !empty( $this->collate) )412 $ collation_query .= " COLLATE '{$this->collate}'";413 $this->query( $collation_query);513 $query = $this->prepare( 'SET NAMES %s', $this->charset ); 514 if ( !empty( $this->collate ) ) 515 $query .= $this->prepare( ' COLLATE %s', $this->collate ); 516 $this->query( $query ); 414 517 } 415 518 } 416 519 417 $this->select( $dbname, $this->dbh);520 $this->select( $dbname, $this->dbh ); 418 521 } 419 522 420 523 /** 421 524 * PHP5 style destructor and will run when database object is destroyed. 422 525 * 526 * 527 * Please see {@link __construct() class constructor} and the 528 * {@link register_shutdown_function() register_shutdown_function()}. for 529 * more details. 530 * 531 * @link http://www.php.net/__destruct 532 * @link http://www.php.net/register_shutdown_function 533 * 423 534 * @since 2.0.8 424 535 * 425 * @return bool Alwaystrue536 * @return bool true 426 537 */ 427 538 function __destruct() { 428 539 return true; … … 439 550 * @param string $prefix Alphanumeric name for the new prefix. 440 551 * @return string|WP_Error Old prefix or WP_Error on error 441 552 */ 442 function set_prefix( $prefix) {553 function set_prefix( $prefix ) { 443 554 444 if ( preg_match( '|[^a-z0-9_]|i', $prefix) )555 if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) 445 556 return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/); 446 557 447 558 if ( is_multisite() ) … … 455 566 foreach ( $this->global_tables as $table ) 456 567 $this->$table = $prefix . $table; 457 568 458 if ( defined( 'VHOST') && empty($this->blogid) )569 if ( defined( 'VHOST' ) && empty( $this->blogid ) ) 459 570 return $old_prefix; 460 571 461 572 $this->prefix = $this->get_blog_prefix( $this->blogid ); … … 463 574 foreach ( (array) $this->tables as $table ) 464 575 $this->$table = $this->prefix . $table; 465 576 466 if ( defined( 'CUSTOM_USER_TABLE') )577 if ( defined( 'CUSTOM_USER_TABLE' ) ) 467 578 $this->users = CUSTOM_USER_TABLE; 468 579 469 if ( defined( 'CUSTOM_USER_META_TABLE') )580 if ( defined( 'CUSTOM_USER_META_TABLE' ) ) 470 581 $this->usermeta = CUSTOM_USER_META_TABLE; 471 582 472 583 return $old_prefix; 473 584 } 474 585 475 function set_blog_id($blog_id, $site_id = '') { 586 /** 587 * blog id setter 588 * 589 * {@internal Missing Description}} 590 * 591 * @since 3.0 592 * @access public 593 * @param string $blog_id 594 * @param string $site_id (optional) 595 * @return string previous blog id 596 */ 597 function set_blog_id( $blog_id, $site_id = '' ) { 476 598 if ( !empty($site_id) ) 477 599 $this->siteid = $site_id; 478 600 479 $old_blog_id = $this->blogid;601 $old_blog_id = $this->blogid; 480 602 $this->blogid = $blog_id; 481 603 482 604 $this->prefix = $this->get_blog_prefix( $this->blogid ); … … 487 609 return $old_blog_id; 488 610 } 489 611 612 /** 613 * blog prefix getter 614 * 615 * {@internal Missing Description}} 616 * 617 * @param string $blog_id (optional) 618 * @return unknown_type 619 */ 490 620 function get_blog_prefix( $blog_id = '' ) { 491 621 if ( is_multisite() && $blog_id ) { 492 622 if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) ) … … 509 639 * @param string $db MySQL database name 510 640 * @return null Always null. 511 641 */ 512 function select( $db, &$dbh) {513 if ( !@mysql_select_db($db, $dbh)) {642 function select( $db, &$dbh ) { 643 if ( !@mysql_select_db( $db, $dbh ) ) { 514 644 $this->ready = false; 515 $this->bail( sprintf(/*WP_I18N_DB_SELECT_DB*/'645 $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/' 516 646 <h1>Can’t select database</h1> 517 647 <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> 518 648 <ul> … … 520 650 <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li> 521 651 <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> 522 652 </ul> 523 <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');653 <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' ); 524 654 return; 525 655 } 526 656 } 527 657 528 function _weak_escape($string) { 529 return addslashes($string); 658 /** 659 * weak escape 660 * 661 * an alias to addslashes() 662 * 663 * @see addslashes() 664 * 665 * @since {@internal Version Unknown}} 666 * @access private 667 * @param string $string 668 * @return string 669 */ 670 function _weak_escape( $string ) { 671 return addslashes( $string ); 530 672 } 531 673 532 function _real_escape($string) { 674 /** 675 * real escape 676 * 677 * escape via mysql_real_escape_string() or addslashes() 678 * 679 * @since 2.8 680 * @access private 681 * 682 * @param string $string to escape 683 * @return string escaped 684 */ 685 function _real_escape( $string ) { 533 686 if ( $this->dbh && $this->real_escape ) 534 687 return mysql_real_escape_string( $string, $this->dbh ); 535 688 else 536 689 return addslashes( $string ); 537 690 } 538 691 539 function _escape($data) { 540 if ( is_array($data) ) { 692 /** 693 * escape 694 * 695 * escape data, uses {@see _real_escape()} and works 696 * on arrays as well. 697 * 698 * @since 2.8 699 * @access private 700 * 701 * @param string|array $data to escape 702 * @return string|array escaped 703 */ 704 function _escape( $data ) { 705 if ( is_array( $data ) ) { 541 706 foreach ( (array) $data as $k => $v ) { 542 707 if ( is_array($v) ) 543 708 $data[$k] = $this->_escape( $v ); … … 556 721 * 557 722 * @since 0.71 558 723 * 559 * @param string|array $data560 * @return string query safe string724 * @param string|array $data to escape 725 * @return string|array escaped as query safe string 561 726 */ 562 function escape( $data) {563 if ( is_array( $data) ) {727 function escape( $data ) { 728 if ( is_array( $data ) ) { 564 729 foreach ( (array) $data as $k => $v ) { 565 if ( is_array( $v) )730 if ( is_array( $v ) ) 566 731 $data[$k] = $this->escape( $v ); 567 732 else 568 733 $data[$k] = $this->_weak_escape( $v ); … … 579 744 * 580 745 * @since 2.3.0 581 746 * 582 * @param string $s 747 * @param string $string to escape 748 * @return void 583 749 */ 584 function escape_by_ref( &$string) {750 function escape_by_ref( &$string ) { 585 751 $string = $this->_real_escape( $string ); 586 752 } 587 753 588 754 /** 589 755 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. 590 756 * 757 * The following directives can be used in the query format string: 758 * 759 * %d (decimal number) 760 * %s (string) 761 * %% (literal percentage sign - no argument needed) 762 * 763 * Both %d and %s are to be left unquoted in the query string and 764 * they need an argument passed for them. 765 * Literals (%) as parts of the query must be properly written 766 * as %%. 767 * 591 768 * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string). 592 769 * Does not support sign, padding, alignment, width or precision specifiers. 593 770 * Does not support argument numbering/swapping. … … 598 775 * 599 776 * <code> 600 777 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 ) 778 * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); 601 779 * </code> 602 780 * 603 781 * @link http://php.net/sprintf Description of syntax. 604 782 * @since 2.3.0 605 783 * 606 784 * @param string $query Query statement with sprintf()-like placeholders 607 * @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()}.785 * @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()}. 608 786 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}. 609 * @return null| string Sanitized query string787 * @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 610 788 */ 611 function prepare( $query = null) { // ( $query, *$args )789 function prepare( $query = null ) { // ( $query, *$args ) 612 790 if ( is_null( $query ) ) 613 791 return; 792 614 793 $args = func_get_args(); 615 array_shift( $args);616 // If args were passed as an array (as in vsprintf), move them up617 if ( isset($args[0]) && is_array($args[0]) )618 $args = $args[0]; 619 $query = str_replace( "'%s'", '%s', $query); // in case someone mistakenly already singlequoted it620 $query = str_replace( '"%s"', '%s', $query); // doublequote unquoting621 $query = str_replace( '%s', "'%s'", $query); // quote the strings622 array_walk( $args, array(&$this, 'escape_by_ref'));623 return @vsprintf( $query, $args);794 array_shift( $args ); 795 if ( isset( $args[0] ) && is_array( $args[0] ) ) 796 $args = $args[0]; # re-assign args passed as array like in vsprintf 797 798 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it 799 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting 800 $query = str_replace( '%s', "'%s'", $query ); // quote the strings 801 array_walk( $args, array( &$this, 'escape_by_ref' ) ); 802 return @vsprintf( $query, $args ); 624 803 } 625 804 626 805 /** … … 632 811 * @param string $str The error to display 633 812 * @return bool False if the showing of errors is disabled. 634 813 */ 635 function print_error( $str = '') {814 function print_error( $str = '' ) { 636 815 global $EZSQL_ERROR; 637 816 638 if ( !$str) $str = mysql_error($this->dbh);639 $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);817 if ( !$str ) $str = mysql_error( $this->dbh ); 818 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 640 819 641 820 if ( $this->suppress_errors ) 642 821 return false; … … 646 825 else 647 826 $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); 648 827 649 $log_error = true; 650 if ( ! function_exists('error_log') ) 651 $log_error = false; 828 if ( function_exists( 'error_log' ) 829 && !( $log_file = @ini_get( 'error_log' ) ) 830 && ( 'syslog' != $log_file ) 831 && @is_writable( $log_file ) ) 832 @error_log( $error_str ); 652 833 653 $log_file = @ini_get('error_log');654 if ( !empty($log_file) && ('syslog' != $log_file) && !@is_writable($log_file) )655 $log_error = false;656 657 if ( $log_error )658 @error_log($error_str, 0);659 660 834 // Is error output turned on or not.. 661 835 if ( !$this->show_errors ) 662 836 return false; … … 667 841 if ( defined( 'ERRORLOGFILE' ) ) 668 842 error_log( $msg, 3, ERRORLOGFILE ); 669 843 if ( defined( 'DIEONDBERROR' ) ) 670 die( $msg );844 wp_die( $msg ); 671 845 } else { 672 $str = htmlspecialchars($str, ENT_QUOTES);673 $query = htmlspecialchars( $this->last_query, ENT_QUOTES);846 $str = htmlspecialchars( $str, ENT_QUOTES ); 847 $query = htmlspecialchars( $this->last_query, ENT_QUOTES ); 674 848 675 849 print "<div id='error'> 676 850 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> … … 688 862 * errors. 689 863 * 690 864 * @since 0.71 691 * 865 * @see hide_errors() 866 * 692 867 * @param bool $show Whether to show or hide errors 693 868 * @return bool Old value for showing errors. 694 869 */ … … 700 875 701 876 /** 702 877 * Disables showing of database errors. 878 * 879 * By default database errors are not shown. 703 880 * 704 881 * @since 0.71 882 * @see show_errors() 705 883 * 706 884 * @return bool Whether showing of errors was active or not 707 885 */ … … 714 892 /** 715 893 * Whether to suppress database errors. 716 894 * 717 * @param unknown_type $suppress 718 * @return unknown 895 * By default database errors are suppressed, with a simple 896 * call to this function they can be enabled. 897 * 898 * @since 2.5 899 * @see hide_errors() 900 * @param bool $suppress (optional) new value, defaults to true 901 * @return bool old suppress errors value 719 902 */ 720 903 function suppress_errors( $suppress = true ) { 721 904 $errors = $this->suppress_errors; 722 $this->suppress_errors = $suppress;905 $this->suppress_errors = (bool) $suppress; 723 906 return $errors; 724 907 } 725 908 … … 727 910 * Kill cached query results. 728 911 * 729 912 * @since 0.71 913 * 914 * @return void 730 915 */ 731 916 function flush() { 732 917 $this->last_result = array(); 733 $this->col_info = null;734 $this->last_query = null;918 $this->col_info = null; 919 $this->last_query = null; 735 920 } 736 921 737 922 function db_connect( $query = "SELECT" ) { … … 754 939 } 755 940 756 941 $dbhname = "dbh" . $action; 757 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 942 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 758 943 if (!$this->$dbhname ) { 759 $this->bail( "944 $this->bail( sprintf( /*WP_I18N_DB_CONNECT_DB*/' 760 945 <h1>Error establishing a database connection</h1> 761 <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>$dbhost</code>. This could mean your host's database server is down.</p>946 <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> 762 947 <ul> 763 948 <li>Are you sure you have the correct username and password?</li> 764 949 <li>Are you sure that you have typed the correct hostname?</li> 765 950 <li>Are you sure that the database server is running?</li> 766 951 </ul> 767 <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> 768 "); 952 <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) ); 769 953 } 770 954 $this->select( $details[ 'db_name' ], $this->$dbhname ); 771 955 } … … 777 961 * 778 962 * @since 0.71 779 963 * 780 * @param string $query 964 * @param string $query database query 781 965 * @return int|false Number of rows affected/selected or false on error 782 966 */ 783 function query( $query) {967 function query( $query ) { 784 968 if ( ! $this->ready ) 785 969 return false; 786 970 787 // filter the query, if filters are available 788 // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method 789 if ( function_exists('apply_filters') ) 790 $query = apply_filters('query', $query); 971 if ( function_exists( 'apply_filters' ) ) 972 $query = apply_filters( 'query', $query ); 791 973 792 // init ialise return974 // init return values and objects state 793 975 $return_val = 0; 794 976 $this->flush(); 795 977 … … 799 981 // Keep track of the last query for debug.. 800 982 $this->last_query = $query; 801 983 802 // Perform the query via std mysql_query function.. 803 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 984 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 804 985 $this->timer_start(); 805 986 987 806 988 // use $this->dbh for read ops, and $this->dbhwrite for write ops 807 989 // use $this->dbhglobal for gloal table ops 808 990 unset( $dbh ); … … 828 1010 $this->last_db_used = "other/read"; 829 1011 } 830 1012 831 $this->result = @mysql_query($query, $dbh); 832 ++$this->num_queries; 1013 // Perform the query via std mysql_query function.. 1014 $this->result = @mysql_query( $query, $dbh ); 1015 $this->num_queries++; 833 1016 834 1017 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 835 1018 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 836 1019 837 1020 // If there is an error then take note of it.. 838 if ( $this->last_error = mysql_error( $dbh) ) {1021 if ( $this->last_error = mysql_error( $dbh ) ) { 839 1022 $this->print_error(); 840 1023 return false; 841 1024 } 842 1025 843 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {844 $this->rows_affected = mysql_affected_rows( $dbh);1026 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) { 1027 $this->rows_affected = mysql_affected_rows( $dbh ); 845 1028 // Take note of the insert_id 846 if ( preg_match( "/^\\s*(insert|replace) /i",$query) ) {1029 if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) { 847 1030 $this->insert_id = mysql_insert_id($dbh); 848 1031 } 849 1032 // Return number of rows affected 850 1033 $return_val = $this->rows_affected; 851 1034 } else { 852 1035 $i = 0; 853 while ( $i < @mysql_num_fields($this->result)) {854 $this->col_info[$i] = @mysql_fetch_field( $this->result);1036 while ( $i < @mysql_num_fields( $this->result ) ) { 1037 $this->col_info[$i] = @mysql_fetch_field( $this->result ); 855 1038 $i++; 856 1039 } 857 1040 $num_rows = 0; 858 while ( $row = @mysql_fetch_object( $this->result) ) {1041 while ( $row = @mysql_fetch_object( $this->result ) ) { 859 1042 $this->last_result[$num_rows] = $row; 860 1043 $num_rows++; 861 1044 } 862 1045 863 @mysql_free_result( $this->result);1046 @mysql_free_result( $this->result ); 864 1047 865 1048 // Log number of rows the query returned 1049 // and Return number of rows selected 866 1050 $this->num_rows = $num_rows; 867 868 // Return number of rows selected 869 $return_val = $this->num_rows; 1051 $return_val = $num_rows; 870 1052 } 871 1053 872 1054 return $return_val; … … 876 1058 * Insert a row into a table. 877 1059 * 878 1060 * <code> 1061 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) ) 879 1062 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) 880 1063 * </code> 881 1064 * … … 887 1070 * @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. 888 1071 * @return int|false The number of rows inserted, or false on error. 889 1072 */ 890 function insert( $table, $data, $format = null) {1073 function insert( $table, $data, $format = null ) { 891 1074 $formats = $format = (array) $format; 892 $fields = array_keys( $data);1075 $fields = array_keys( $data ); 893 1076 $formatted_fields = array(); 894 1077 foreach ( $fields as $field ) { 895 if ( !empty( $format) )896 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];897 elseif ( isset( $this->field_types[$field]) )1078 if ( !empty( $format ) ) 1079 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1080 elseif ( isset( $this->field_types[$field] ) ) 898 1081 $form = $this->field_types[$field]; 899 1082 else 900 1083 $form = '%s'; 901 1084 $formatted_fields[] = $form; 902 1085 } 903 1086 $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')"; 904 return $this->query( $this->prepare( $sql, $data ) );1087 return $this->query( $this->prepare( $sql, $data ) ); 905 1088 } 906 1089 907 1090 … … 909 1092 * Update a row in the table 910 1093 * 911 1094 * <code> 1095 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) ) 912 1096 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) 913 1097 * </code> 914 1098 * … … 922 1106 * @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. 923 1107 * @return int|false The number of rows updated, or false on error. 924 1108 */ 925 function update( $table, $data, $where, $format = null, $where_format = null) {926 if ( !is_array( $ where ) )1109 function update( $table, $data, $where, $format = null, $where_format = null ) { 1110 if ( !is_array( $data ) || !is_array( $where ) ) 927 1111 return false; 928 1112 929 1113 $formats = $format = (array) $format; 930 1114 $bits = $wheres = array(); 931 foreach ( (array) array_keys( $data) as $field ) {932 if ( !empty( $format) )933 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];1115 foreach ( (array) array_keys( $data ) as $field ) { 1116 if ( !empty( $format ) ) 1117 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 934 1118 elseif ( isset($this->field_types[$field]) ) 935 1119 $form = $this->field_types[$field]; 936 1120 else … … 939 1123 } 940 1124 941 1125 $where_formats = $where_format = (array) $where_format; 942 foreach ( (array) array_keys( $where) as $field ) {943 if ( !empty( $where_format) )944 $form = ( $form = array_shift( $where_formats) ) ? $form : $where_format[0];945 elseif ( isset( $this->field_types[$field]) )1126 foreach ( (array) array_keys( $where ) as $field ) { 1127 if ( !empty( $where_format ) ) 1128 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1129 elseif ( isset( $this->field_types[$field] ) ) 946 1130 $form = $this->field_types[$field]; 947 1131 else 948 1132 $form = '%s'; … … 950 1134 } 951 1135 952 1136 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); 953 return $this->query( $this->prepare( $sql, array_merge( array_values($data), array_values($where))) );1137 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); 954 1138 } 955 1139 956 1140 /** … … 962 1146 * 963 1147 * @since 0.71 964 1148 * 965 * @param string|null $query SQL query. If null, use the result from the previous query.1149 * @param string|null $query (optional) SQL query. Defaults to NULL, use the result from the previous query. 966 1150 * @param int $x (optional) Column of value to return. Indexed from 0. 967 1151 * @param int $y (optional) Row of value to return. Indexed from 0. 968 * @return string Database query result1152 * @return string|null Database query result on success (as string), NULL if a value on failure 969 1153 */ 970 function get_var( $query=null, $x = 0, $y = 0) {971 $this->func_call = "\$db->get_var(\"$query\", $x,$y)";1154 function get_var( $query = null, $x = 0, $y = 0 ) { 1155 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; 972 1156 if ( $query ) 973 $this->query( $query);1157 $this->query( $query ); 974 1158 975 1159 // Extract var out of cached results based x,y vals 976 1160 if ( !empty( $this->last_result[$y] ) ) { 977 $values = array_values( get_object_vars($this->last_result[$y]));1161 $values = array_values( get_object_vars( $this->last_result[$y] ) ); 978 1162 } 979 1163 980 1164 // If there is a value return it else return null 981 return ( isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;1165 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; 982 1166 } 983 1167 984 1168 /** … … 991 1175 * @param string|null $query SQL query. 992 1176 * @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. 993 1177 * @param int $y (optional) Row to return. Indexed from 0. 994 * @return mixed Database query result in format specifed by $output 1178 * @return mixed Database query result in format specifed by $output or NULL 1179 * @return void 995 1180 */ 996 function get_row( $query = null, $output = OBJECT, $y = 0) {1181 function get_row( $query = null, $output = OBJECT, $y = 0 ) { 997 1182 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; 998 1183 if ( $query ) 999 $this->query( $query);1184 $this->query( $query ); 1000 1185 else 1001 1186 return null; 1002 1187 1003 if ( !isset( $this->last_result[$y]) )1188 if ( !isset( $this->last_result[$y] ) ) 1004 1189 return null; 1005 1190 1006 1191 if ( $output == OBJECT ) { 1007 1192 return $this->last_result[$y] ? $this->last_result[$y] : null; 1008 1193 } elseif ( $output == ARRAY_A ) { 1009 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y]) : null;1194 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; 1010 1195 } elseif ( $output == ARRAY_N ) { 1011 return $this->last_result[$y] ? array_values( get_object_vars($this->last_result[$y])) : null;1196 return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; 1012 1197 } else { 1013 1198 $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*/); 1014 1199 } … … 1023 1208 * 1024 1209 * @since 0.71 1025 1210 * 1026 * @param string|null $query SQL query.If null, use the result from the previous query.1027 * @param int $x Column to return. Indexed from 0.1211 * @param string|null $query (optional) SQL query. If null, use the result from the previous query. 1212 * @param int $x (optional) Column to return. Indexed from 0. 1028 1213 * @return array Database query result. Array indexed from 0 by SQL result row number. 1029 1214 */ 1030 function get_col( $query = null , $x = 0) {1215 function get_col( $query = null , $x = 0 ) { 1031 1216 if ( $query ) 1032 $this->query( $query);1217 $this->query( $query ); 1033 1218 1034 1219 $new_array = array(); 1035 1220 // Extract the column values 1036 for ( $i=0; $i < count( $this->last_result); $i++ ) {1037 $new_array[$i] = $this->get_var( null, $x, $i);1221 for ( $i=0; $i < count( $this->last_result ); $i++ ) { 1222 $new_array[$i] = $this->get_var( null, $x, $i ); 1038 1223 } 1039 1224 return $new_array; 1040 1225 } … … 1050 1235 * @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. 1051 1236 * @return mixed Database query results 1052 1237 */ 1053 function get_results( $query = null, $output = OBJECT) {1238 function get_results( $query = null, $output = OBJECT ) { 1054 1239 $this->func_call = "\$db->get_results(\"$query\", $output)"; 1055 1240 1056 1241 if ( $query ) 1057 $this->query( $query);1242 $this->query( $query ); 1058 1243 else 1059 1244 return null; 1060 1245 … … 1095 1280 * 1096 1281 * @since 0.71 1097 1282 * 1098 * @param string $info_ type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill1099 * @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 type1283 * @param string $info_(optional) type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill 1284 * @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 1100 1285 * @return mixed Column Results 1101 1286 */ 1102 function get_col_info( $info_type = 'name', $col_offset = -1) {1287 function get_col_info( $info_type = 'name', $col_offset = -1 ) { 1103 1288 if ( $this->col_info ) { 1104 1289 if ( $col_offset == -1 ) { 1105 $i = 0;1290 $i = 0; 1106 1291 $new_array = array(); 1107 1292 foreach( (array) $this->col_info as $col ) { 1108 1293 $new_array[$i] = $col->{$info_type}; … … 1123 1308 * @return true 1124 1309 */ 1125 1310 function timer_start() { 1126 $mtime = microtime(); 1127 $mtime = explode(' ', $mtime); 1311 $mtime = explode( ' ', microtime() ); 1128 1312 $this->time_start = $mtime[1] + $mtime[0]; 1129 1313 return true; 1130 1314 } … … 1137 1321 * @return int Total time spent on the query, in milliseconds 1138 1322 */ 1139 1323 function timer_stop() { 1140 $mtime = microtime(); 1141 $mtime = explode(' ', $mtime); 1142 $time_end = $mtime[1] + $mtime[0]; 1324 $mtime = explode( ' ', microtime() ); 1325 $time_end = $mtime[1] + $mtime[0]; 1143 1326 $time_total = $time_end - $this->time_start; 1144 1327 return $time_total; 1145 1328 } … … 1155 1338 * @param string $error_code (optional) A Computer readable string to identify the error. 1156 1339 * @return false|void 1157 1340 */ 1158 function bail( $message, $error_code = '500') {1341 function bail( $message, $error_code = '500' ) { 1159 1342 if ( !$this->show_errors ) { 1160 if ( class_exists( 'WP_Error') )1343 if ( class_exists( 'WP_Error' ) ) 1161 1344 $this->error = new WP_Error($error_code, $message); 1162 1345 else 1163 1346 $this->error = $message; … … 1171 1354 * 1172 1355 * @since 2.5.0 1173 1356 * @uses $wp_version 1357 * @uses $required_mysql_version 1174 1358 * 1175 1359 * @return WP_Error 1176 1360 */ … … 1178 1362 global $wp_version, $required_mysql_version; 1179 1363 // Make sure the server has the required MySQL version 1180 1364 if ( version_compare($this->db_version(), $required_mysql_version, '<') ) 1181 return new WP_Error('database_version', sprintf(__('<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher'), $wp_version, $required_mysql_version));1365 return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version )); 1182 1366 } 1183 1367 1184 1368 /** … … 1195 1379 } 1196 1380 1197 1381 /** 1198 * Generic function to determine if a database supports a particular feature 1382 * has capability 1383 * 1384 * Determine if a database supports a particular feature 1385 * 1386 * @since 2.7 1387 * @see db_version() 1388 * 1199 1389 * @param string $db_cap the feature 1200 1390 * @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. 1201 1391 * @return bool … … 1207 1397 case 'collation' : // @since 2.5.0 1208 1398 case 'group_concat' : // @since 2.7 1209 1399 case 'subqueries' : // @since 2.7 1210 return version_compare( $version, '4.1', '>=');1400 return version_compare( $version, '4.1', '>=' ); 1211 1401 break; 1212 1402 endswitch; 1213 1403 … … 1225 1415 * @return string The name of the calling function 1226 1416 */ 1227 1417 function get_caller() { 1228 $ bt = debug_backtrace();1418 $trace = array_reverse( debug_backtrace() ); 1229 1419 $caller = array(); 1230 1420 1231 $bt = array_reverse( $bt ); 1232 foreach ( (array) $bt as $call ) { 1421 foreach ( $trace as $call ) { 1233 1422 if ( isset( $call['class'] ) && __CLASS__ == $call['class'] ) 1234 continue; 1423 continue; # filter out function calls of this object's class 1424 1235 1425 $function = $call['function']; 1426 1236 1427 if ( isset( $call['class'] ) ) 1237 1428 $function = $call['class'] . "->$function"; 1429 1238 1430 $caller[] = $function; 1239 1431 } 1240 $caller = join( ', ', $caller );1241 1432 1242 return $caller;1433 return join( ', ', $caller ); 1243 1434 } 1244 1435 1245 1436 /** 1246 * The database version number 1247 * @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. 1437 * get database version number 1438 * 1439 * ADDITIONAL PARAMETER NOTICE 1440 * 1441 * there once was a proposed second parameter which has never been 1442 * implemented. It was describben as "Which database to test" ($dbh_or_table) 1443 * 1444 * It would have had three different types: 1445 * 1446 * false : currently selected database 1447 * string : database containing this table 1448 * resource : database by mysql resource 1449 * 1450 * regarding that third parameter please see {@see db_version()} as well 1451 * 1452 * @since 2.7 1453 * @see has_cap() 1454 * 1248 1455 * @return false|string false on failure, version number on success 1249 1456 */ 1250 1457 function db_version() { 1251 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));1458 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 1252 1459 } 1253 1460 } 1254 1461 … … 1260 1467 */ 1261 1468 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); 1262 1469 } 1263 ?> 1470 ?> 1471 No newline at end of file