Ticket #11644: 11644.code_cleanup_wpdb.patch
File 11644.code_cleanup_wpdb.patch, 37.8 KB (added by , 15 years ago) |
---|
-
wordpress/wp-includes/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" ) ); 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 * NOTE: 527 * 528 * In difference to the PHP5 behavior of __destruct, this function will 529 * be called when the script shuts down. If the garbage collector will 530 * remove this object earlier in PHP5, this function will be called prior to 531 * script termination. 532 * 533 * Please see {@link __construct() class constructor} and the 534 * {@link register_shutdown_function() register_shutdown_function()}. for 535 * more details. 536 * 537 * @link http://www.php.net/__destruct 538 * @link http://www.php.net/register_shutdown_function 539 * 423 540 * @since 2.0.8 424 541 * 425 * @return bool Always true542 * @return void 426 543 */ 427 544 function __destruct() { 428 return true;429 545 } 430 546 431 547 /** … … 439 555 * @param string $prefix Alphanumeric name for the new prefix. 440 556 * @return string|WP_Error Old prefix or WP_Error on error 441 557 */ 442 function set_prefix( $prefix) {558 function set_prefix( $prefix ) { 443 559 444 if ( preg_match( '|[^a-z0-9_]|i', $prefix) )560 if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) 445 561 return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/); 446 562 447 563 if ( is_multisite() ) … … 455 571 foreach ( $this->global_tables as $table ) 456 572 $this->$table = $prefix . $table; 457 573 458 if ( defined( 'VHOST') && empty($this->blogid) )574 if ( defined( 'VHOST' ) && empty( $this->blogid ) ) 459 575 return $old_prefix; 460 576 461 577 $this->prefix = $this->get_blog_prefix( $this->blogid ); … … 463 579 foreach ( (array) $this->tables as $table ) 464 580 $this->$table = $this->prefix . $table; 465 581 466 if ( defined( 'CUSTOM_USER_TABLE') )582 if ( defined( 'CUSTOM_USER_TABLE' ) ) 467 583 $this->users = CUSTOM_USER_TABLE; 468 584 469 if ( defined( 'CUSTOM_USER_META_TABLE') )585 if ( defined( 'CUSTOM_USER_META_TABLE' ) ) 470 586 $this->usermeta = CUSTOM_USER_META_TABLE; 471 587 472 588 return $old_prefix; 473 589 } 474 590 475 function set_blog_id($blog_id, $site_id = '') { 591 /** 592 * blog id setter 593 * 594 * {@internal Missing Description}} 595 * 596 * @since 3.0 597 * @access public 598 * @param string $blog_id 599 * @param string $site_id (optional) 600 * @return string previous blog id 601 */ 602 function set_blog_id( $blog_id, $site_id = '' ) { 476 603 if ( !empty($site_id) ) 477 604 $this->siteid = $site_id; 478 605 479 $old_blog_id = $this->blogid;606 $old_blog_id = $this->blogid; 480 607 $this->blogid = $blog_id; 481 608 482 609 $this->prefix = $this->get_blog_prefix( $this->blogid ); … … 487 614 return $old_blog_id; 488 615 } 489 616 617 /** 618 * blog prefix getter 619 * 620 * {@internal Missing Description}} 621 * 622 * @param string $blog_id (optional) 623 * @return unknown_type 624 */ 490 625 function get_blog_prefix( $blog_id = '' ) { 491 626 if ( is_multisite() && $blog_id ) { 492 627 if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) ) … … 509 644 * @param string $db MySQL database name 510 645 * @return null Always null. 511 646 */ 512 function select( $db, &$dbh) {513 if ( !@mysql_select_db($db, $dbh)) {647 function select( $db, &$dbh ) { 648 if ( !@mysql_select_db( $db, $dbh ) ) { 514 649 $this->ready = false; 515 $this->bail( sprintf(/*WP_I18N_DB_SELECT_DB*/'650 $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/' 516 651 <h1>Can’t select database</h1> 517 652 <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 653 <ul> … … 520 655 <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li> 521 656 <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 657 </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');658 <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 659 return; 525 660 } 526 661 } 527 662 528 function _weak_escape($string) { 529 return addslashes($string); 663 /** 664 * weak escape 665 * 666 * an alias to addslashes() 667 * 668 * @see addslashes() 669 * 670 * @since {@internal Version Unknown}} 671 * @access private 672 * @param string $string 673 * @return string 674 */ 675 function _weak_escape( $string ) { 676 return addslashes( $string ); 530 677 } 531 678 532 function _real_escape($string) { 679 /** 680 * real escape 681 * 682 * escape via mysql_real_escape_string() or addslashes() 683 * 684 * @since 2.8 685 * @access private 686 * 687 * @param string $string to escape 688 * @return string escaped 689 */ 690 function _real_escape( $string ) { 533 691 if ( $this->dbh && $this->real_escape ) 534 692 return mysql_real_escape_string( $string, $this->dbh ); 535 693 else 536 694 return addslashes( $string ); 537 695 } 538 696 539 function _escape($data) { 540 if ( is_array($data) ) { 697 /** 698 * escape 699 * 700 * escape data, uses {@see _real_escape()} and works 701 * on arrays as well. 702 * 703 * @since 2.8 704 * @access private 705 * 706 * @param string|array $data to escape 707 * @return string|array escaped 708 */ 709 function _escape( $data ) { 710 if ( is_array( $data ) ) { 541 711 foreach ( (array) $data as $k => $v ) { 542 712 if ( is_array($v) ) 543 713 $data[$k] = $this->_escape( $v ); … … 556 726 * 557 727 * @since 0.71 558 728 * 559 * @param string|array $data560 * @return string query safe string729 * @param string|array $data to escape 730 * @return string|array escaped as query safe string 561 731 */ 562 function escape( $data) {563 if ( is_array( $data) ) {732 function escape( $data ) { 733 if ( is_array( $data ) ) { 564 734 foreach ( (array) $data as $k => $v ) { 565 if ( is_array( $v) )735 if ( is_array( $v ) ) 566 736 $data[$k] = $this->escape( $v ); 567 737 else 568 738 $data[$k] = $this->_weak_escape( $v ); … … 579 749 * 580 750 * @since 2.3.0 581 751 * 582 * @param string $s 752 * @param string $string to escape 753 * @return void 583 754 */ 584 function escape_by_ref( &$string) {755 function escape_by_ref( &$string ) { 585 756 $string = $this->_real_escape( $string ); 586 757 } 587 758 588 759 /** 589 760 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. 590 761 * 762 * The following directives can be used in the query format string: 763 * 764 * %d (decimal number) 765 * %s (string) 766 * %% (literal percentage sign - no argument needed) 767 * 768 * Both %d and %s are to be left unquoted in the query string and 769 * they need an argument passed for them. 770 * Literals (%) as parts of the query must be properly written 771 * as %%. 772 * 591 773 * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string). 592 774 * Does not support sign, padding, alignment, width or precision specifiers. 593 775 * Does not support argument numbering/swapping. … … 598 780 * 599 781 * <code> 600 782 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 ) 783 * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); 601 784 * </code> 602 785 * 603 786 * @link http://php.net/sprintf Description of syntax. 604 787 * @since 2.3.0 605 788 * 606 789 * @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()}.790 * @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 791 * @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 string792 * @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 793 */ 611 function prepare( $query = null) { // ( $query, *$args )794 function prepare( $query = null ) { // ( $query, *$args ) 612 795 if ( is_null( $query ) ) 613 796 return; 797 614 798 $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);799 array_shift( $args ); 800 if ( isset( $args[0] ) && is_array( $args[0] ) ) 801 $args = $args[0]; # re-assign args passed as array like in vsprintf 802 803 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it 804 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting 805 $query = str_replace( '%s', "'%s'", $query ); // quote the strings 806 array_walk( $args, array( &$this, 'escape_by_ref' ) ); 807 return @vsprintf( $query, $args ); 624 808 } 625 809 626 810 /** … … 632 816 * @param string $str The error to display 633 817 * @return bool False if the showing of errors is disabled. 634 818 */ 635 function print_error( $str = '') {819 function print_error( $str = '' ) { 636 820 global $EZSQL_ERROR; 637 821 638 if ( !$str) $str = mysql_error($this->dbh);639 $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);822 if ( !$str ) $str = mysql_error( $this->dbh ); 823 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 640 824 641 825 if ( $this->suppress_errors ) 642 826 return false; … … 646 830 else 647 831 $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 832 649 $log_error = true; 650 if ( ! function_exists('error_log') ) 651 $log_error = false; 833 if ( function_exists( 'error_log' ) 834 && !( $log_file = @ini_get( 'error_log' ) ) 835 && ( 'syslog' != $log_file ) 836 && @is_writable( $log_file ) ) 837 @error_log( $error_str ); 652 838 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 839 // Is error output turned on or not.. 661 840 if ( !$this->show_errors ) 662 841 return false; … … 667 846 if ( defined( 'ERRORLOGFILE' ) ) 668 847 error_log( $msg, 3, ERRORLOGFILE ); 669 848 if ( defined( 'DIEONDBERROR' ) ) 670 die( $msg );849 wp_die( $msg ); 671 850 } else { 672 $str = htmlspecialchars($str, ENT_QUOTES);673 $query = htmlspecialchars( $this->last_query, ENT_QUOTES);851 $str = htmlspecialchars( $str, ENT_QUOTES ); 852 $query = htmlspecialchars( $this->last_query, ENT_QUOTES ); 674 853 675 854 print "<div id='error'> 676 855 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> … … 688 867 * errors. 689 868 * 690 869 * @since 0.71 691 * 870 * @see hide_errors() 871 * 692 872 * @param bool $show Whether to show or hide errors 693 873 * @return bool Old value for showing errors. 694 874 */ … … 700 880 701 881 /** 702 882 * Disables showing of database errors. 883 * 884 * By default database errors are not shown. 703 885 * 704 886 * @since 0.71 887 * @see show_errors() 705 888 * 706 889 * @return bool Whether showing of errors was active or not 707 890 */ … … 714 897 /** 715 898 * Whether to suppress database errors. 716 899 * 717 * @param unknown_type $suppress 718 * @return unknown 900 * By default database errors are suppressed, with a simple 901 * call to this function they can be enabled. 902 * 903 * @since 2.5 904 * @see hide_errors() 905 * @param bool $suppress (optional) new value, defaults to true 906 * @return bool old suppress errors value 719 907 */ 720 908 function suppress_errors( $suppress = true ) { 721 909 $errors = $this->suppress_errors; 722 $this->suppress_errors = $suppress;910 $this->suppress_errors = (bool) $suppress; 723 911 return $errors; 724 912 } 725 913 … … 727 915 * Kill cached query results. 728 916 * 729 917 * @since 0.71 918 * 919 * @return void 730 920 */ 731 921 function flush() { 732 922 $this->last_result = array(); 733 $this->col_info = null;734 $this->last_query = null;923 $this->col_info = null; 924 $this->last_query = null; 735 925 } 736 926 737 927 function db_connect( $query = "SELECT" ) { … … 754 944 } 755 945 756 946 $dbhname = "dbh" . $action; 757 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 947 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 758 948 if (!$this->$dbhname ) { 759 $this->bail( "949 $this->bail( sprintf( /*WP_I18N_DB_CONNECT_DB*/' 760 950 <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>951 <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 952 <ul> 763 953 <li>Are you sure you have the correct username and password?</li> 764 954 <li>Are you sure that you have typed the correct hostname?</li> 765 955 <li>Are you sure that the database server is running?</li> 766 956 </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 "); 957 <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 958 } 770 959 $this->select( $details[ 'db_name' ], $this->$dbhname ); 771 960 } … … 777 966 * 778 967 * @since 0.71 779 968 * 780 * @param string $query 969 * @param string $query database query 781 970 * @return int|false Number of rows affected/selected or false on error 782 971 */ 783 function query( $query) {972 function query( $query ) { 784 973 if ( ! $this->ready ) 785 974 return false; 786 975 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); 976 if ( function_exists( 'apply_filters' ) ) 977 $query = apply_filters( 'query', $query ); 791 978 792 // init ialise return979 // init return values and objects state 793 980 $return_val = 0; 794 981 $this->flush(); 795 982 … … 799 986 // Keep track of the last query for debug.. 800 987 $this->last_query = $query; 801 988 802 // Perform the query via std mysql_query function.. 803 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 989 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 804 990 $this->timer_start(); 805 991 992 806 993 // use $this->dbh for read ops, and $this->dbhwrite for write ops 807 994 // use $this->dbhglobal for gloal table ops 808 995 unset( $dbh ); … … 828 1015 $this->last_db_used = "other/read"; 829 1016 } 830 1017 831 $this->result = @mysql_query($query, $dbh); 832 ++$this->num_queries; 1018 // Perform the query via std mysql_query function.. 1019 $this->result = @mysql_query( $query, $dbh ); 1020 $this->num_queries++; 833 1021 834 1022 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 835 1023 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 836 1024 837 1025 // If there is an error then take note of it.. 838 if ( $this->last_error = mysql_error( $dbh) ) {1026 if ( $this->last_error = mysql_error( $dbh ) ) { 839 1027 $this->print_error(); 840 1028 return false; 841 1029 } 842 1030 843 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {844 $this->rows_affected = mysql_affected_rows( $dbh);1031 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) { 1032 $this->rows_affected = mysql_affected_rows( $dbh ); 845 1033 // Take note of the insert_id 846 if ( preg_match( "/^\\s*(insert|replace) /i",$query) ) {1034 if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) { 847 1035 $this->insert_id = mysql_insert_id($dbh); 848 1036 } 849 1037 // Return number of rows affected 850 1038 $return_val = $this->rows_affected; 851 1039 } else { 852 1040 $i = 0; 853 while ( $i < @mysql_num_fields($this->result)) {854 $this->col_info[$i] = @mysql_fetch_field( $this->result);1041 while ( $i < @mysql_num_fields( $this->result ) ) { 1042 $this->col_info[$i] = @mysql_fetch_field( $this->result ); 855 1043 $i++; 856 1044 } 857 1045 $num_rows = 0; 858 while ( $row = @mysql_fetch_object( $this->result) ) {1046 while ( $row = @mysql_fetch_object( $this->result ) ) { 859 1047 $this->last_result[$num_rows] = $row; 860 1048 $num_rows++; 861 1049 } 862 1050 863 @mysql_free_result( $this->result);1051 @mysql_free_result( $this->result ); 864 1052 865 1053 // Log number of rows the query returned 1054 // and Return number of rows selected 866 1055 $this->num_rows = $num_rows; 867 868 // Return number of rows selected 869 $return_val = $this->num_rows; 1056 $return_val = $num_rows; 870 1057 } 871 1058 872 1059 return $return_val; … … 876 1063 * Insert a row into a table. 877 1064 * 878 1065 * <code> 1066 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) ) 879 1067 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) 880 1068 * </code> 881 1069 * … … 887 1075 * @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 1076 * @return int|false The number of rows inserted, or false on error. 889 1077 */ 890 function insert( $table, $data, $format = null) {1078 function insert( $table, $data, $format = null ) { 891 1079 $formats = $format = (array) $format; 892 $fields = array_keys( $data);1080 $fields = array_keys( $data ); 893 1081 $formatted_fields = array(); 894 1082 foreach ( $fields as $field ) { 895 if ( !empty( $format) )896 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];897 elseif ( isset( $this->field_types[$field]) )1083 if ( !empty( $format ) ) 1084 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1085 elseif ( isset( $this->field_types[$field] ) ) 898 1086 $form = $this->field_types[$field]; 899 1087 else 900 1088 $form = '%s'; 901 1089 $formatted_fields[] = $form; 902 1090 } 903 1091 $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')"; 904 return $this->query( $this->prepare( $sql, $data ) );1092 return $this->query( $this->prepare( $sql, $data ) ); 905 1093 } 906 1094 907 1095 … … 909 1097 * Update a row in the table 910 1098 * 911 1099 * <code> 1100 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ) ) 912 1101 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) 913 1102 * </code> 914 1103 * … … 922 1111 * @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 1112 * @return int|false The number of rows updated, or false on error. 924 1113 */ 925 function update( $table, $data, $where, $format = null, $where_format = null) {926 if ( !is_array( $ where ) )1114 function update( $table, $data, $where, $format = null, $where_format = null ) { 1115 if ( !is_array( $data ) || !is_array( $where ) ) 927 1116 return false; 928 1117 929 1118 $formats = $format = (array) $format; 930 1119 $bits = $wheres = array(); 931 foreach ( (array) array_keys( $data) as $field ) {932 if ( !empty( $format) )933 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];1120 foreach ( (array) array_keys( $data ) as $field ) { 1121 if ( !empty( $format ) ) 1122 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 934 1123 elseif ( isset($this->field_types[$field]) ) 935 1124 $form = $this->field_types[$field]; 936 1125 else … … 939 1128 } 940 1129 941 1130 $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]) )1131 foreach ( (array) array_keys( $where ) as $field ) { 1132 if ( !empty( $where_format ) ) 1133 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1134 elseif ( isset( $this->field_types[$field] ) ) 946 1135 $form = $this->field_types[$field]; 947 1136 else 948 1137 $form = '%s'; … … 950 1139 } 951 1140 952 1141 $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))) );1142 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); 954 1143 } 955 1144 956 1145 /** … … 962 1151 * 963 1152 * @since 0.71 964 1153 * 965 * @param string|null $query SQL query. If null, use the result from the previous query.1154 * @param string|null $query (optional) SQL query. Defaults to NULL, use the result from the previous query. 966 1155 * @param int $x (optional) Column of value to return. Indexed from 0. 967 1156 * @param int $y (optional) Row of value to return. Indexed from 0. 968 * @return string Database query result1157 * @return string|null Database query result on success (as string), NULL if a value on failure 969 1158 */ 970 function get_var( $query=null, $x = 0, $y = 0) {971 $this->func_call = "\$db->get_var(\"$query\", $x,$y)";1159 function get_var( $query = null, $x = 0, $y = 0 ) { 1160 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; 972 1161 if ( $query ) 973 $this->query( $query);1162 $this->query( $query ); 974 1163 975 1164 // Extract var out of cached results based x,y vals 976 1165 if ( !empty( $this->last_result[$y] ) ) { 977 $values = array_values( get_object_vars($this->last_result[$y]));1166 $values = array_values( get_object_vars( $this->last_result[$y] ) ); 978 1167 } 979 1168 980 1169 // If there is a value return it else return null 981 return ( isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;1170 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; 982 1171 } 983 1172 984 1173 /** … … 991 1180 * @param string|null $query SQL query. 992 1181 * @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 1182 * @param int $y (optional) Row to return. Indexed from 0. 994 * @return mixed Database query result in format specifed by $output 1183 * @return mixed Database query result in format specifed by $output or NULL 1184 * @return void 995 1185 */ 996 function get_row( $query = null, $output = OBJECT, $y = 0) {1186 function get_row( $query = null, $output = OBJECT, $y = 0 ) { 997 1187 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; 998 1188 if ( $query ) 999 $this->query( $query);1189 $this->query( $query ); 1000 1190 else 1001 1191 return null; 1002 1192 1003 if ( !isset( $this->last_result[$y]) )1193 if ( !isset( $this->last_result[$y] ) ) 1004 1194 return null; 1005 1195 1006 1196 if ( $output == OBJECT ) { 1007 1197 return $this->last_result[$y] ? $this->last_result[$y] : null; 1008 1198 } elseif ( $output == ARRAY_A ) { 1009 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y]) : null;1199 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; 1010 1200 } elseif ( $output == ARRAY_N ) { 1011 return $this->last_result[$y] ? array_values( get_object_vars($this->last_result[$y])) : null;1201 return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; 1012 1202 } else { 1013 1203 $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 1204 } … … 1023 1213 * 1024 1214 * @since 0.71 1025 1215 * 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.1216 * @param string|null $query (optional) SQL query. If null, use the result from the previous query. 1217 * @param int $x (optional) Column to return. Indexed from 0. 1028 1218 * @return array Database query result. Array indexed from 0 by SQL result row number. 1029 1219 */ 1030 function get_col( $query = null , $x = 0) {1220 function get_col( $query = null , $x = 0 ) { 1031 1221 if ( $query ) 1032 $this->query( $query);1222 $this->query( $query ); 1033 1223 1034 1224 $new_array = array(); 1035 1225 // Extract the column values 1036 for ( $i=0; $i < count( $this->last_result); $i++ ) {1037 $new_array[$i] = $this->get_var( null, $x, $i);1226 for ( $i=0; $i < count( $this->last_result ); $i++ ) { 1227 $new_array[$i] = $this->get_var( null, $x, $i ); 1038 1228 } 1039 1229 return $new_array; 1040 1230 } … … 1050 1240 * @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 1241 * @return mixed Database query results 1052 1242 */ 1053 function get_results( $query = null, $output = OBJECT) {1243 function get_results( $query = null, $output = OBJECT ) { 1054 1244 $this->func_call = "\$db->get_results(\"$query\", $output)"; 1055 1245 1056 1246 if ( $query ) 1057 $this->query( $query);1247 $this->query( $query ); 1058 1248 else 1059 1249 return null; 1060 1250 … … 1095 1285 * 1096 1286 * @since 0.71 1097 1287 * 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 type1288 * @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 1289 * @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 1290 * @return mixed Column Results 1101 1291 */ 1102 function get_col_info( $info_type = 'name', $col_offset = -1) {1292 function get_col_info( $info_type = 'name', $col_offset = -1 ) { 1103 1293 if ( $this->col_info ) { 1104 1294 if ( $col_offset == -1 ) { 1105 $i = 0;1295 $i = 0; 1106 1296 $new_array = array(); 1107 1297 foreach( (array) $this->col_info as $col ) { 1108 1298 $new_array[$i] = $col->{$info_type}; … … 1123 1313 * @return true 1124 1314 */ 1125 1315 function timer_start() { 1126 $mtime = microtime(); 1127 $mtime = explode(' ', $mtime); 1316 $mtime = explode( ' ', microtime() ); 1128 1317 $this->time_start = $mtime[1] + $mtime[0]; 1129 1318 return true; 1130 1319 } … … 1137 1326 * @return int Total time spent on the query, in milliseconds 1138 1327 */ 1139 1328 function timer_stop() { 1140 $mtime = microtime(); 1141 $mtime = explode(' ', $mtime); 1142 $time_end = $mtime[1] + $mtime[0]; 1329 $mtime = explode( ' ', microtime() ); 1330 $time_end = $mtime[1] + $mtime[0]; 1143 1331 $time_total = $time_end - $this->time_start; 1144 1332 return $time_total; 1145 1333 } … … 1155 1343 * @param string $error_code (optional) A Computer readable string to identify the error. 1156 1344 * @return false|void 1157 1345 */ 1158 function bail( $message, $error_code = '500') {1346 function bail( $message, $error_code = '500' ) { 1159 1347 if ( !$this->show_errors ) { 1160 if ( class_exists( 'WP_Error') )1348 if ( class_exists( 'WP_Error' ) ) 1161 1349 $this->error = new WP_Error($error_code, $message); 1162 1350 else 1163 1351 $this->error = $message; … … 1171 1359 * 1172 1360 * @since 2.5.0 1173 1361 * @uses $wp_version 1362 * @uses $required_mysql_version 1174 1363 * 1175 1364 * @return WP_Error 1176 1365 */ … … 1178 1367 global $wp_version, $required_mysql_version; 1179 1368 // Make sure the server has the required MySQL version 1180 1369 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));1370 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 1371 } 1183 1372 1184 1373 /** … … 1195 1384 } 1196 1385 1197 1386 /** 1198 * Generic function to determine if a database supports a particular feature 1387 * has capability 1388 * 1389 * Determine if a database supports a particular feature 1390 * 1391 * @since 2.7 1392 * @see db_version() 1393 * 1199 1394 * @param string $db_cap the feature 1200 1395 * @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 1396 * @return bool … … 1207 1402 case 'collation' : // @since 2.5.0 1208 1403 case 'group_concat' : // @since 2.7 1209 1404 case 'subqueries' : // @since 2.7 1210 return version_compare( $version, '4.1', '>=');1405 return version_compare( $version, '4.1', '>=' ); 1211 1406 break; 1212 1407 endswitch; 1213 1408 … … 1225 1420 * @return string The name of the calling function 1226 1421 */ 1227 1422 function get_caller() { 1228 $ bt = debug_backtrace();1423 $trace = array_reverse( debug_backtrace() ); 1229 1424 $caller = array(); 1230 1425 1231 $bt = array_reverse( $bt ); 1232 foreach ( (array) $bt as $call ) { 1426 foreach ( $trace as $call ) { 1233 1427 if ( isset( $call['class'] ) && __CLASS__ == $call['class'] ) 1234 continue; 1428 continue; # filter out function calls of this object's class 1429 1235 1430 $function = $call['function']; 1431 1236 1432 if ( isset( $call['class'] ) ) 1237 1433 $function = $call['class'] . "->$function"; 1434 1238 1435 $caller[] = $function; 1239 1436 } 1240 $caller = join( ', ', $caller );1241 1437 1242 return $caller;1438 return join( ', ', $caller ); 1243 1439 } 1244 1440 1245 1441 /** 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. 1442 * get database version number 1443 * 1444 * ADDITIONAL PARAMETER NOTICE 1445 * 1446 * there once was a proposed second parameter which has never been 1447 * implemented. It was describben as "Which database to test" ($dbh_or_table) 1448 * 1449 * It would have had three different types: 1450 * 1451 * false : currently selected database 1452 * string : database containing this table 1453 * resource : database by mysql resource 1454 * 1455 * regarding that third parameter please see {@see db_version()} as well 1456 * 1457 * @since 2.7 1458 * @see has_cap() 1459 * 1248 1460 * @return false|string false on failure, version number on success 1249 1461 */ 1250 1462 function db_version() { 1251 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));1463 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 1252 1464 } 1253 1465 } 1254 1466 … … 1260 1472 */ 1261 1473 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); 1262 1474 } 1263 ?> 1475 ?> 1476 No newline at end of file