Changeset 12667
- Timestamp:
- 01/08/2010 07:26:00 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/wp-db.php
r12573 r12667 136 136 */ 137 137 var $ready = false; 138 var $blogid = 0; 139 var $siteid = 0; 140 var $blogs; 141 var $signups; 142 var $site; 143 var $sitemeta; 144 var $sitecategories; 145 var $global_tables = array('blogs', 'signups', 'site', 'sitemeta', 'users', 'usermeta', 'sitecategories', 'registration_log', 'blog_versions'); 138 146 139 147 /** … … 261 269 * @var array 262 270 */ 263 var $tables = array(' users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',271 var $tables = array('posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options', 264 272 'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta'); 265 273 … … 338 346 */ 339 347 function wpdb($dbuser, $dbpassword, $dbname, $dbhost) { 348 if( defined( "WP_USE_MULTIPLE_DB" ) && CONSTANT( "WP_USE_MULTIPLE_DB" ) == true ) 349 $this->db_connect(); 340 350 return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost); 341 351 } … … 360 370 if ( WP_DEBUG ) 361 371 $this->show_errors(); 372 373 if( is_multisite() ) { 374 $this->charset = 'utf8'; 375 if( defined( 'DB_COLLATE' ) && constant( 'DB_COLLATE' ) != '' ) { 376 $this->collate = constant( 'DB_COLLATE' ); 377 } else { 378 $this->collate = 'utf8_general_ci'; 379 } 380 } 362 381 363 382 if ( defined('DB_CHARSET') ) … … 398 417 } 399 418 400 $this->select($dbname );419 $this->select($dbname, $this->dbh); 401 420 } 402 421 … … 428 447 return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/); 429 448 430 $old_prefix = $this->prefix; 431 $this->prefix = $prefix; 449 if( is_multisite() ) { 450 $old_prefix = ''; 451 } else { 452 $old_prefix = $prefix; 453 } 454 if( isset( $this->base_prefix ) ) 455 $old_prefix = $this->base_prefix; 456 $this->base_prefix = $prefix; 457 foreach ( $this->global_tables as $table ) 458 $this->$table = $prefix . $table; 459 460 if ( defined('VHOST') && empty($this->blogid) ) 461 return $old_prefix; 462 463 $this->prefix = $this->get_blog_prefix( $this->blogid ); 432 464 433 465 foreach ( (array) $this->tables as $table ) … … 443 475 } 444 476 477 function set_blog_id($blog_id, $site_id = '') { 478 if ( !empty($site_id) ) 479 $this->siteid = $site_id; 480 481 $old_blog_id = $this->blogid; 482 $this->blogid = $blog_id; 483 484 $this->prefix = $this->get_blog_prefix( $this->blogid ); 485 486 foreach ( $this->tables as $table ) 487 $this->$table = $this->prefix . $table; 488 489 return $old_blog_id; 490 } 491 492 function get_blog_prefix( $blog_id = '' ) { 493 if ( $blog_id ) { 494 if( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1) ) { 495 return $this->prefix; 496 } else { 497 return $this->base_prefix . $blog_id . '_'; 498 } 499 } else { 500 return $this->base_prefix; 501 } 502 } 503 445 504 /** 446 505 * Selects a database using the current database connection. … … 454 513 * @return null Always null. 455 514 */ 456 function select($db ) {457 if (!@mysql_select_db($db, $ this->dbh)) {515 function select($db, &$dbh) { 516 if (!@mysql_select_db($db, $dbh)) { 458 517 $this->ready = false; 459 518 $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/' … … 606 665 return false; 607 666 608 $str = htmlspecialchars($str, ENT_QUOTES); 609 $query = htmlspecialchars($this->last_query, ENT_QUOTES); 610 611 // If there is an error then take note of it 612 print "<div id='error'> 613 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> 614 <code>$query</code></p> 615 </div>"; 667 // If there is an error then take note of it 668 if( is_multisite() ) { 669 $msg = "WordPress database error: [$str]\n{$this->query}\n"; 670 if( defined( 'ERRORLOGFILE' ) ) 671 error_log( $msg, 3, CONSTANT( 'ERRORLOGFILE' ) ); 672 if( defined( 'DIEONDBERROR' ) ) 673 die( $msg ); 674 } else { 675 $str = htmlspecialchars($str, ENT_QUOTES); 676 $query = htmlspecialchars($this->last_query, ENT_QUOTES); 677 678 print "<div id='error'> 679 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> 680 <code>$query</code></p> 681 </div>"; 682 } 616 683 } 617 684 … … 671 738 } 672 739 740 function db_connect( $query = "SELECT" ) { 741 global $db_list, $global_db_list; 742 if( is_array( $db_list ) == false ) 743 return true; 744 745 if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) { 746 $action = 'global'; 747 $details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ]; 748 $this->db_global = $details; 749 } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) { 750 $action = 'write'; 751 $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ]; 752 $this->db_write = $details; 753 } else { 754 $action = ''; 755 $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ]; 756 $this->db_read = $details; 757 } 758 759 $dbhname = "dbh" . $action; 760 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 761 if (!$this->$dbhname ) { 762 $this->bail(" 763 <h1>Error establishing a database connection</h1> 764 <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> 765 <ul> 766 <li>Are you sure you have the correct username and password?</li> 767 <li>Are you sure that you have typed the correct hostname?</li> 768 <li>Are you sure that the database server is running?</li> 769 </ul> 770 <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> 771 "); 772 } 773 $this->select( $details[ 'db_name' ], $this->$dbhname ); 774 } 775 673 776 /** 674 777 * Perform a MySQL database query, using current database connection. … … 703 806 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 704 807 $this->timer_start(); 705 706 $this->result = @mysql_query($query, $this->dbh); 808 809 // use $this->dbh for read ops, and $this->dbhwrite for write ops 810 // use $this->dbhglobal for gloal table ops 811 unset( $dbh ); 812 if( defined( "WP_USE_MULTIPLE_DB" ) && CONSTANT( "WP_USE_MULTIPLE_DB" ) == true ) { 813 if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) { 814 if( false == isset( $this->dbhglobal ) ) { 815 $this->db_connect( $query ); 816 } 817 $dbh =& $this->dbhglobal; 818 $this->last_db_used = "global"; 819 } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) { 820 if( false == isset( $this->dbhwrite ) ) { 821 $this->db_connect( $query ); 822 } 823 $dbh =& $this->dbhwrite; 824 $this->last_db_used = "write"; 825 } else { 826 $dbh =& $this->dbh; 827 $this->last_db_used = "read"; 828 } 829 } else { 830 $dbh =& $this->dbh; 831 $this->last_db_used = "other/read"; 832 } 833 834 $this->result = @mysql_query($query, $dbh); 707 835 ++$this->num_queries; 708 836 … … 711 839 712 840 // If there is an error then take note of it.. 713 if ( $this->last_error = mysql_error($ this->dbh) ) {841 if ( $this->last_error = mysql_error($dbh) ) { 714 842 $this->print_error(); 715 843 return false; … … 717 845 718 846 if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) { 719 $this->rows_affected = mysql_affected_rows($ this->dbh);847 $this->rows_affected = mysql_affected_rows($dbh); 720 848 // Take note of the insert_id 721 849 if ( preg_match("/^\\s*(insert|replace) /i",$query) ) { 722 $this->insert_id = mysql_insert_id($ this->dbh);850 $this->insert_id = mysql_insert_id($dbh); 723 851 } 724 852 // Return number of rows affected … … 1045 1173 * @since 2.5.0 1046 1174 * @uses $wp_version 1047 * @uses $required_mysql_version1048 1175 * 1049 1176 * @return WP_Error
Note: See TracChangeset
for help on using the changeset viewer.