Ticket #14672: 14672.diff
File 14672.diff, 8.6 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 1024 /** 1025 * Figure out which database server should handle a query, and connect to it. 1026 * 1027 * @param string query (optional) 1028 * @return resource mysql database connection 1029 */ 1020 1030 function db_connect( $query = "SELECT" ) { 1021 1031 global $db_list, $global_db_list; 1022 if ( ! is_array( $db_list ) )1023 return true;1024 1032 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 if ( !defined( 'WP_USE_MULTIPLE_DB' ) || !WP_USE_MULTIPLE_DB ) { 1034 // Single DB mode 1035 $dbhname = 'dbh'; 1036 $this->$dbhname = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); 1037 $dbhost = $this->dbhost; 1038 $dbname = $this->dbname; 1033 1039 } else { 1034 $action = ''; 1035 $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ]; 1036 $this->db_read = $details; 1040 // Multiple DB mode 1041 if ( ! is_array( $db_list ) ) 1042 return true; 1043 1044 if ( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i", $query) ) { 1045 $action = 'global'; 1046 $details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ]; 1047 $this->db_global = $details; 1048 } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i", $query) ) { 1049 $action = 'write'; 1050 $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ]; 1051 $this->db_write = $details; 1052 } else { 1053 $action = ''; 1054 $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ]; 1055 $this->db_read = $details; 1056 } 1057 1058 $dbhname = 'dbh' . $action; 1059 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 1060 $dbhost = $details[ 'db_host' ]; 1061 $dbname = $details[ 'db_name' ]; 1037 1062 } 1038 1063 1039 $dbhname = "dbh" . $action; 1040 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 1041 if (!$this->$dbhname ) { 1064 if ( !$this->$dbhname ) { 1042 1065 $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/" 1043 1066 <h1>Error establishing a database connection</h1> 1044 1067 <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 1071 <li>Are you sure that the database server is running?</li> 1049 1072 </ul> 1050 1073 <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*/, $d etails['db_host']), 'db_connect_fail' );1074 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' ); 1052 1075 } 1053 $this->select( $details[ 'db_name' ], $this->$dbhname ); 1076 1077 $this->set_charset($this->$dbhname); 1078 1079 $this->ready = true; 1080 1081 $this->select( $dbname, $this->$dbhname ); 1054 1082 } 1055 1083 1056 1084 /** … … 1087 1115 // use $this->dbhglobal for gloal table ops 1088 1116 unset( $dbh ); 1089 1117 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 ) ) {1118 if ( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) { 1119 if ( !isset( $this->dbhglobal ) ) 1092 1120 $this->db_connect( $query ); 1093 }1094 1121 $dbh =& $this->dbhglobal; 1095 1122 $this->last_db_used = "global"; 1096 1123 } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) { 1097 if ( false == isset( $this->dbhwrite ) ) {1124 if ( !isset( $this->dbhwrite ) ) 1098 1125 $this->db_connect( $query ); 1099 }1100 1126 $dbh =& $this->dbhwrite; 1101 1127 $this->last_db_used = "write"; 1102 1128 } else { … … 1544 1570 case 'group_concat' : // @since 2.7 1545 1571 case 'subqueries' : // @since 2.7 1546 1572 return version_compare( $version, '4.1', '>=' ); 1573 case 'set_charset' : 1574 return version_compare($version, '5.0.7', '>='); 1547 1575 }; 1548 1576 1549 1577 return false;