Ticket #14672: 14672.2.diff
File 14672.2.diff, 8.2 KB (added by , 15 years ago) |
---|
-
wp-includes/wp-db.php
476 476 * @param string $dbhost MySQL database host 477 477 */ 478 478 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) { 479 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB )480 $this->db_connect();481 479 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost ); 482 480 } 483 481 … … 502 500 if ( WP_DEBUG ) 503 501 $this->show_errors(); 504 502 505 if ( is_multisite() ) { 503 $this->init_charset(); 504 505 $this->dbuser = $dbuser; 506 $this->dbpassword = $dbpassword; 507 $this->dbname = $dbname; 508 $this->dbhost = $dbhost; 509 510 $this->db_connect(); 511 } 512 513 /** 514 * PHP5 style destructor and will run when database object is destroyed. 515 * 516 * @see wpdb::__construct() 517 * @since 2.0.8 518 * @return bool true 519 */ 520 function __destruct() { 521 return true; 522 } 523 524 /** 525 * Set $this->charset and $this->collate 526 */ 527 function init_charset() { 528 if ( function_exists('is_multisite') && is_multisite() ) { 506 529 $this->charset = 'utf8'; 507 530 if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) 508 531 $this->collate = DB_COLLATE; … … 514 537 515 538 if ( defined( 'DB_CHARSET' ) ) 516 539 $this->charset = DB_CHARSET; 540 } 517 541 518 $this->dbuser = $dbuser; 519 520 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true ); 521 if ( !$this->dbh ) { 522 $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/" 523 <h1>Error establishing a database connection</h1> 524 <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> 525 <ul> 526 <li>Are you sure you have the correct username and password?</li> 527 <li>Are you sure that you have typed the correct hostname?</li> 528 <li>Are you sure that the database server is running?</li> 529 </ul> 530 <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> 531 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' ); 532 return; 533 } 534 535 $this->ready = true; 536 537 if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) { 538 if ( function_exists( 'mysql_set_charset' ) ) { 539 mysql_set_charset( $this->charset, $this->dbh ); 542 /** 543 * Sets the connection's character set. 544 * 545 * @param resource $dbh The resource given by mysql_connect 546 * @param string $charset The character set (optional) 547 * @param string $collate The collation (optional) 548 */ 549 function set_charset($dbh, $charset = null, $collate = null) { 550 if ( !isset($charset) ) 551 $charset = $this->charset; 552 if ( !isset($collate) ) 553 $collate = $this->collate; 554 if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) { 555 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) { 556 mysql_set_charset( $charset, $dbh ); 540 557 $this->real_escape = true; 541 558 } else { 542 $query = $this->prepare( 'SET NAMES %s', $ this->charset );543 if ( ! empty( $ this->collate ) )544 $query .= $this->prepare( ' COLLATE %s', $ this->collate );545 $this->query( $query);559 $query = $this->prepare( 'SET NAMES %s', $charset ); 560 if ( ! empty( $collate ) ) 561 $query .= $this->prepare( ' COLLATE %s', $collate ); 562 mysql_query( $query, $dbh ); 546 563 } 547 564 } 548 549 $this->select( $dbname, $this->dbh );550 565 } 551 566 552 567 /** 553 * PHP5 style destructor and will run when database object is destroyed.554 *555 * @see wpdb::__construct()556 * @since 2.0.8557 * @return bool true558 */559 function __destruct() {560 return true;561 }562 563 /**564 568 * Sets the table prefix for the WordPress tables. 565 569 * 566 570 * @since 2.5.0 … … 1017 1021 $this->last_query = null; 1018 1022 } 1019 1023 1020 function db_connect( $query = "SELECT" ) { 1024 /** 1025 * Connect to and select database 1026 */ 1027 function db_connect() { 1021 1028 global $db_list, $global_db_list; 1022 if ( ! is_array( $db_list ) )1023 return true;1024 1029 1025 if ( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) { 1026 $action = 'global'; 1027 $details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ]; 1028 $this->db_global = $details; 1029 } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) { 1030 $action = 'write'; 1031 $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ]; 1032 $this->db_write = $details; 1033 } else { 1034 $action = ''; 1035 $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ]; 1036 $this->db_read = $details; 1037 } 1030 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); 1038 1031 1039 $dbhname = "dbh" . $action; 1040 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 1041 if (!$this->$dbhname ) { 1032 if ( !$this->dbh ) { 1042 1033 $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/" 1043 1034 <h1>Error establishing a database connection</h1> 1044 1035 <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> … … 1048 1039 <li>Are you sure that the database server is running?</li> 1049 1040 </ul> 1050 1041 <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> 1051 "/*/WP_I18N_DB_CONN_ERROR*/, $ details['db_host']), 'db_connect_fail' );1042 "/*/WP_I18N_DB_CONN_ERROR*/, $this->dbhost ), 'db_connect_fail' ); 1052 1043 } 1053 $this->select( $details[ 'db_name' ], $this->$dbhname ); 1044 1045 $this->set_charset( $this->dbh ); 1046 1047 $this->ready = true; 1048 1049 $this->select( $this->dbname, $this->dbh ); 1054 1050 } 1055 1051 1056 1052 /** … … 1083 1079 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1084 1080 $this->timer_start(); 1085 1081 1086 // use $this->dbh for read ops, and $this->dbhwrite for write ops 1087 // use $this->dbhglobal for gloal table ops 1088 unset( $dbh ); 1089 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) { 1090 if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) { 1091 if( false == isset( $this->dbhglobal ) ) { 1092 $this->db_connect( $query ); 1093 } 1094 $dbh =& $this->dbhglobal; 1095 $this->last_db_used = "global"; 1096 } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) { 1097 if( false == isset( $this->dbhwrite ) ) { 1098 $this->db_connect( $query ); 1099 } 1100 $dbh =& $this->dbhwrite; 1101 $this->last_db_used = "write"; 1102 } else { 1103 $dbh =& $this->dbh; 1104 $this->last_db_used = "read"; 1105 } 1106 } else { 1107 $dbh =& $this->dbh; 1108 $this->last_db_used = "other/read"; 1109 } 1110 1111 $this->result = @mysql_query( $query, $dbh ); 1082 $this->result = @mysql_query( $query, $this->dbh ); 1112 1083 $this->num_queries++; 1113 1084 1114 1085 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1115 1086 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1116 1087 1117 1088 // If there is an error then take note of it.. 1118 if ( $this->last_error = mysql_error( $ dbh ) ) {1089 if ( $this->last_error = mysql_error( $this->dbh ) ) { 1119 1090 $this->print_error(); 1120 1091 return false; 1121 1092 } 1122 1093 1123 1094 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) { 1124 $this->rows_affected = mysql_affected_rows( $ dbh );1095 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1125 1096 // Take note of the insert_id 1126 1097 if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) { 1127 $this->insert_id = mysql_insert_id($ dbh);1098 $this->insert_id = mysql_insert_id($this->dbh); 1128 1099 } 1129 1100 // Return number of rows affected 1130 1101 $return_val = $this->rows_affected; … … 1544 1515 case 'group_concat' : // @since 2.7 1545 1516 case 'subqueries' : // @since 2.7 1546 1517 return version_compare( $version, '4.1', '>=' ); 1518 case 'set_charset' : 1519 return version_compare($version, '5.0.7', '>='); 1547 1520 }; 1548 1521 1549 1522 return false;