Ticket #21663: 21663.6.patch
File 21663.6.patch, 19.9 KB (added by , 12 years ago) |
---|
-
new file wp-includes/class-wp-db-driver-mysql.php
diff --git wp-includes/class-wp-db-driver-mysql.php wp-includes/class-wp-db-driver-mysql.php new file mode 100644 index 0000000..237a3b5
- + 1 <?php 2 3 /** 4 * Database driver, using the mysql extension. 5 * 6 * @link http://php.net/manual/en/book.mysqli.php 7 * 8 * @package WordPress 9 * @subpackage Database 10 * @since 3.6.0 11 */ 12 class wpdb_driver_mysql implements wpdb_driver { 13 14 /** 15 * Database link 16 * @var resource 17 */ 18 private $dbh = null; 19 20 /** 21 * Result set 22 * @var resource 23 */ 24 private $result = null; 25 26 /** 27 * Cached column info 28 * @var array|null 29 */ 30 private $col_info = null; 31 32 /** 33 * Escape with mysql_real_escape_string() 34 * @param string $string 35 * @return string 36 */ 37 public function escape( $string ) { 38 return mysql_real_escape_string( $string, $this->dbh ); 39 } 40 41 /** 42 * Get the latest error message from the DB driver 43 * @return string 44 */ 45 public function get_error_message() { 46 return mysql_error( $this->dbh ); 47 } 48 49 /** 50 * Free memory associated with the resultset 51 * @return void 52 */ 53 public function flush() { 54 if ( is_resource( $this->result ) ) { 55 mysql_free_result( $this->result ); 56 } 57 $this->result = null; 58 $this->col_info = null; 59 } 60 61 /** 62 * Connect to database 63 * @return bool 64 */ 65 public function connect( $host, $user, $pass, $port = 3306 ) { 66 67 $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; 68 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; 69 70 if ( WP_DEBUG ) { 71 $this->dbh = mysql_connect( "$host:$port", $user, $pass, $new_link, $client_flags ); 72 } else { 73 $this->dbh = @mysql_connect( "$host:$port", $user, $pass, $new_link, $client_flags ); 74 } 75 return ( false !== $this->dbh ); 76 } 77 78 /** 79 * Select database 80 * @return void 81 */ 82 public function select( $db ) { 83 if ( WP_DEBUG ) { 84 mysql_select_db( $db, $this->dbh ); 85 } else { 86 @mysql_select_db( $db, $this->dbh ); 87 } 88 } 89 90 /** 91 * Perform a MySQL database query, using current database connection. 92 * @param string $query Database query 93 * @return int|false Number of rows affected/selected or false on error 94 */ 95 public function query( $query ) { 96 $this->result = @mysql_query( $query, $this->dbh ); 97 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 98 $return_val = $this->result; 99 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 100 $return_val = $this->affected_rows(); 101 } elseif ( preg_match( '/^\s*select\s/i', $query ) ) { 102 return is_resource( $this->result ) ? mysql_num_rows( $this->result ) : false ; 103 } 104 return true; 105 } 106 107 /** 108 * Get number of rows affected 109 * @return int 110 */ 111 public function affected_rows() { 112 return mysql_affected_rows( $this->dbh ); 113 } 114 115 /** 116 * Get last insert id 117 * @return int 118 */ 119 public function insert_id() { 120 return mysql_insert_id( $this->dbh ); 121 } 122 123 /** 124 * Get results 125 * @return array 126 */ 127 public function get_results() { 128 $ret = array(); 129 while ( $row = @mysql_fetch_object( $this->result ) ) { 130 $ret[] = $row; 131 } 132 return $ret; 133 } 134 135 /** 136 * Load the column metadata from the last query. 137 * @return array 138 */ 139 public function load_col_info() { 140 if ( $this->col_info ) 141 return $this->col_info; 142 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 143 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 144 } 145 return $this->col_info; 146 } 147 148 /** 149 * The database version number. 150 * @return false|string false on failure, version number on success 151 */ 152 public function db_version() { 153 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 154 } 155 } -
new file wp-includes/class-wp-db-driver-mysqli.php
diff --git wp-includes/class-wp-db-driver-mysqli.php wp-includes/class-wp-db-driver-mysqli.php new file mode 100644 index 0000000..fd3f681
- + 1 <?php 2 3 /** 4 * Database driver, using the mysqli extension. 5 * 6 * @link http://php.net/manual/en/book.mysqli.php 7 * 8 * @package WordPress 9 * @subpackage Database 10 * @since 3.6.0 11 */ 12 class wpdb_driver_mysqli implements wpdb_driver { 13 14 /** 15 * Database link 16 * @var mysqli 17 */ 18 private $dbh = null; 19 20 /** 21 * Result set 22 * @var mysqli_stmt|mysqli_result 23 */ 24 private $result = null; 25 26 /** 27 * Cached column info 28 * @var array|null 29 */ 30 private $col_info = null; 31 32 /** 33 * Escape with mysql_real_escape_string() 34 * @param string $string 35 * @return string 36 */ 37 public function escape( $string ) { 38 return $this->dbh->escape_string( $string ); 39 } 40 41 /** 42 * Get the latest error message from the DB driver 43 * @return string 44 */ 45 public function get_error_message() { 46 return $this->dbh->error; 47 } 48 49 /** 50 * Free memory associated with the resultset 51 * @return void 52 */ 53 public function flush() { 54 if ( $this->result instanceof mysqli_stmt ) { 55 $this->result->free_result(); 56 } 57 $this->result = null; 58 $this->col_info = null; 59 } 60 61 /** 62 * Connect to database 63 * @return bool 64 */ 65 public function connect( $host, $user, $pass, $port = 3306 ) { 66 $this->dbh = new mysqli( $host, $user, $pass, '', $port ); 67 return ( !mysqli_connect_error() ); 68 } 69 70 /** 71 * Select database 72 * @return void 73 */ 74 public function select( $db ) { 75 $this->dbh->select_db( $db ); 76 } 77 78 /** 79 * Perform a MySQL database query, using current database connection. 80 * @param string $query Database query 81 * @return int|false Number of rows affected/selected or false on error 82 */ 83 public function query( $query ) { 84 $this->result = $this->dbh->query( $query ); 85 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 86 $return_val = $this->result; 87 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 88 $return_val = $this->affected_rows(); 89 } elseif ( preg_match( '/^\s*select\s/i', $query ) ) { 90 return $this->result->num_rows; 91 } 92 return true; 93 } 94 95 /** 96 * Get number of rows affected 97 * @return int 98 */ 99 public function affected_rows() { 100 return $this->dbh->affected_rows; 101 } 102 103 /** 104 * Get last insert id 105 * @return int 106 */ 107 public function insert_id() { 108 return $this->dbh->insert_id; 109 } 110 111 /** 112 * Get results 113 * @return array 114 */ 115 public function get_results() { 116 $ret = array(); 117 while ( $row = $this->result->fetch_object() ) { 118 $ret[] = $row; 119 } 120 return $ret; 121 } 122 123 /** 124 * Load the column metadata from the last query. 125 * @return array 126 */ 127 public function load_col_info() { 128 if ( $this->col_info ) 129 return $this->col_info; 130 for ( $i = 0; $i < $this->result->field_count ; $i++ ) { 131 $this->col_info[ $i ] = $this->result->fetch_field_direct( $i ); 132 } 133 return $this->col_info; 134 } 135 136 /** 137 * The database version number. 138 * @return false|string false on failure, version number on success 139 */ 140 public function db_version() { 141 return preg_replace( '/[^0-9.].*/', '', $this->dbh->server_version ); 142 } 143 } -
new file wp-includes/class-wp-db-driver-pdo_mysql.php
diff --git wp-includes/class-wp-db-driver-pdo_mysql.php wp-includes/class-wp-db-driver-pdo_mysql.php new file mode 100644 index 0000000..6a2c47f
- + 1 <?php 2 3 /** 4 * Database driver, using the PDO extension. 5 * 6 * @link http://php.net/manual/en/book.pdo.php 7 * 8 * @package WordPress 9 * @subpackage Database 10 * @since 3.6.0 11 */ 12 class wpdb_driver_pdo_mysql implements wpdb_driver { 13 14 /** 15 * Database link 16 * @var PDO 17 */ 18 private $dbh = null; 19 20 /** 21 * Result set 22 * @var PDOStatement 23 */ 24 private $result = null; 25 26 /** 27 * Cached column info 28 * @var array|null 29 */ 30 private $col_info = null; 31 32 /** 33 * Array of fetched rows. 34 * PDO doesn't have a "count rows" feature, so we have to fetch the rows 35 * up front, and cache them here 36 * @var array 37 */ 38 private $fetched_rows = array(); 39 40 /** 41 * Escape with mysql_real_escape_string() 42 * @param string $string 43 * @return string 44 */ 45 public function escape( $string ) { 46 return substr( $this->dbh->quote( $string ), 1, -1 ); 47 } 48 49 /** 50 * Get the latest error message from the DB driver 51 * @return string 52 */ 53 public function get_error_message() { 54 $error = $this->dbh->errorInfo(); 55 if ( isset( $error[2] ) ) { 56 return $error[2]; 57 } 58 return ''; 59 } 60 61 /** 62 * Free memory associated with the resultset 63 * @return void 64 */ 65 public function flush() { 66 if ( $this->result instanceof PDOStatement ) { 67 $this->result->closeCursor(); 68 } 69 $this->result = null; 70 $this->col_info = null; 71 $this->fetched_rows = array(); 72 } 73 74 /** 75 * Connect to database 76 * @return bool 77 */ 78 public function connect( $host, $user, $pass, $port = 3306 ) { 79 $dsn = sprintf( 'mysql:host=%1$s;port=%2$d', $host, $port ); 80 try { 81 $this->dbh = new PDO( $dsn, $user, $pass ); 82 } catch ( Exception $e ) { 83 return false; 84 } 85 return true; 86 } 87 88 /** 89 * Select database 90 * @return void 91 */ 92 public function select( $db ) { 93 $this->dbh->exec( sprintf( 'USE %s', $db ) ); 94 } 95 96 /** 97 * Perform a MySQL database query, using current database connection. 98 * @param string $query Database query 99 * @return int|false Number of rows affected/selected or false on error 100 */ 101 public function query( $query ) { 102 try { 103 $this->result = $this->dbh->query( $query ); 104 } catch ( Exception $e ) { 105 } 106 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 107 $return_val = $this->result; 108 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 109 $return_val = $this->affected_rows(); 110 } elseif ( preg_match( '/^\s*select\s/i', $query ) ) { 111 $this->get_results(); 112 return count( $this->fetched_rows ); 113 } 114 return true; 115 } 116 117 /** 118 * Get number of rows affected 119 * @return int 120 */ 121 public function affected_rows() { 122 if ( $this->result instanceof PDOStatement ) { 123 return $this->result->rowCount(); 124 } 125 return 0; 126 } 127 128 /** 129 * Get last insert id 130 * @return int 131 */ 132 public function insert_id() { 133 return $this->dbh->lastInsertId(); 134 } 135 136 /** 137 * Get results 138 * @return array 139 */ 140 public function get_results() { 141 if ( !empty( $this->fetched_rows ) ) { 142 return $this->fetched_rows; 143 } 144 $this->fetched_rows = array(); 145 if ( !empty( $this->result ) ) { 146 while ( $row = $this->result->fetchObject() ) { 147 $this->fetched_rows[] = $row; 148 } 149 } 150 return $this->fetched_rows; 151 } 152 153 /** 154 * Load the column metadata from the last query. 155 * @return array 156 */ 157 public function load_col_info() { 158 if ( $this->col_info ) 159 return $this->col_info; 160 for ( $i = 0; $i < $this->result->columnCount() ; $i++ ) { 161 $this->col_info[ $i ] = $this->result->fetchColumn( $i ); 162 } 163 return $this->col_info; 164 } 165 166 /** 167 * The database version number. 168 * @return false|string false on failure, version number on success 169 */ 170 public function db_version() { 171 return preg_replace( '/[^0-9.].*/', '', $this->dbh->getAttribute( PDO::ATTR_SERVER_VERSION ) ); 172 } 173 } -
new file wp-includes/interface-wp-db-driver.php
diff --git wp-includes/interface-wp-db-driver.php wp-includes/interface-wp-db-driver.php new file mode 100644 index 0000000..8bb421a
- + 1 <?php 2 3 interface wpdb_driver { 4 public function escape( $string ); 5 public function get_error_message(); 6 public function flush(); 7 public function connect( $host, $user, $pass, $port = 3306 ); 8 public function select( $name ); 9 public function query( $query ); 10 public function load_col_info(); 11 public function db_version(); 12 public function affected_rows(); 13 public function insert_id(); 14 public function get_results(); 15 } -
wp-includes/load.php
diff --git wp-includes/load.php wp-includes/load.php index 1742af4..16e8b5e 100644
function wp_set_lang_dir() { 326 326 function require_wp_db() { 327 327 global $wpdb; 328 328 329 require( ABSPATH . WPINC . '/interface-wp-db-driver.php' ); 329 330 require_once( ABSPATH . WPINC . '/wp-db.php' ); 330 331 if ( file_exists( WP_CONTENT_DIR . '/db.php' ) ) 331 332 require_once( WP_CONTENT_DIR . '/db.php' ); -
wp-includes/wp-db.php
diff --git wp-includes/wp-db.php wp-includes/wp-db.php index 391cfa4..de17d7b 100644
class wpdb { 487 487 protected $dbhost; 488 488 489 489 /** 490 * Database Handle 490 * Database driver object 491 * 492 * Allow for simple, and backwards compatible, pluggable database drivers 493 * like PDO_mysql and mysqli to power wpdb 491 494 * 492 * @since 0.71493 495 * @access protected 494 * @var string 496 * @since 3.5.0 497 * @var wpdb_driver 495 498 */ 496 499 protected $dbh; 497 500 … … class wpdb { 519 522 public $is_mysql = null; 520 523 521 524 /** 525 * Pick the adapter to be used for performing the actual queries. 526 * 527 * @since 3.6.0 528 */ 529 private function set_driver() { 530 531 // Auto-pick the driver 532 if ( defined( 'WPDB_DRIVER' ) ) { 533 $driver = WPDB_DRIVER; 534 } elseif ( extension_loaded( 'pdo_mysql' ) ) { 535 $driver = 'pdo_mysql'; 536 } elseif ( extension_loaded( 'mysqli' ) ) { 537 $driver = 'mysqli'; 538 } elseif ( extension_loaded( 'mysql' ) ) { 539 $driver = 'mysql'; 540 } 541 542 // Get the new driver 543 if ( in_array( $driver, array( 'mysql', 'mysqli', 'pdo_mysql' ) ) ) { 544 require_once( ABSPATH . WPINC . '/class-wp-db-driver-' . $driver . '.php' ); 545 } 546 $class = 'wpdb_driver_' . $driver; 547 548 if ( !class_exists( $class ) ) { 549 wp_load_translations_early(); 550 $this->bail( sprintf( __( " 551 <h1>No database drivers found</h1>. 552 <p>WordPress requires the mysql, mysqli, or pdo_mysql extension to talk to your database.</p> 553 <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> 554 "), 'db_connect_fail' ) ); 555 } 556 557 $this->dbh = new $class(); 558 } 559 560 /** 522 561 * Connects to the database server and selects a database 523 562 * 524 563 * PHP5 style constructor for compatibility with PHP5. Does … … class wpdb { 534 573 * @param string $dbhost MySQL database host 535 574 */ 536 575 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { 576 $this->set_driver(); 577 537 578 register_shutdown_function( array( $this, '__destruct' ) ); 538 579 539 580 if ( WP_DEBUG ) … … class wpdb { 646 687 if ( !isset($collate) ) 647 688 $collate = $this->collate; 648 689 if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) { 649 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) { 650 mysql_set_charset( $charset, $dbh ); 651 $this->real_escape = true; 652 } else { 653 $query = $this->prepare( 'SET NAMES %s', $charset ); 654 if ( ! empty( $collate ) ) 655 $query .= $this->prepare( ' COLLATE %s', $collate ); 656 mysql_query( $query, $dbh ); 657 } 690 $query = $this->prepare( 'SET NAMES %s', $charset ); 691 if ( ! empty( $collate ) ) 692 $query .= $this->prepare( ' COLLATE %s', $collate ); 693 $this->dbh->query( $query ); 658 694 } 659 695 } 660 696 … … class wpdb { 837 873 * @return null Always null. 838 874 */ 839 875 function select( $db, $dbh = null ) { 840 if ( is_null($dbh) ) 841 $dbh = $this->dbh; 842 843 if ( !@mysql_select_db( $db, $dbh ) ) { 844 $this->ready = false; 845 wp_load_translations_early(); 846 $this->bail( sprintf( __( '<h1>Can’t select database</h1> 847 <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> 848 <ul> 849 <li>Are you sure it exists?</li> 850 <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li> 851 <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> 852 </ul> 853 <p>If you don\'t know how to set up 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>' ), htmlspecialchars( $db, ENT_QUOTES ), htmlspecialchars( $this->dbuser, ENT_QUOTES ) ), 'db_select_fail' ); 854 return; 855 } 876 $this->dbh->select( $db ); 856 877 } 857 878 858 879 /** … … class wpdb { 882 903 */ 883 904 function _real_escape( $string ) { 884 905 if ( $this->dbh && $this->real_escape ) 885 return mysql_real_escape_string( $string, $this->dbh);906 return $this->dbh->escape( $string ); 886 907 else 887 908 return addslashes( $string ); 888 909 } … … class wpdb { 1017 1038 global $EZSQL_ERROR; 1018 1039 1019 1040 if ( !$str ) 1020 $str = mysql_error( $this->dbh);1041 $str = $this->dbh->get_error_message(); 1021 1042 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 1022 1043 1023 1044 if ( $this->suppress_errors ) … … class wpdb { 1114 1135 * @return void 1115 1136 */ 1116 1137 function flush() { 1117 $this->last_result = array(); 1118 $this->col_info = null; 1119 $this->last_query = null; 1120 1121 if ( is_resource( $this->result ) ) 1122 mysql_free_result( $this->result ); 1138 $this->dbh->flush(); 1123 1139 } 1124 1140 1125 1141 /** … … class wpdb { 1131 1147 1132 1148 $this->is_mysql = true; 1133 1149 1134 $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; 1135 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; 1136 1137 if ( WP_DEBUG ) { 1138 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1139 } else { 1140 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1150 $host = $this->dbhost; 1151 $port = 3306; 1152 if ( false !== strpos( $this->dbhost, ':' ) ) { 1153 list( $host, $port ) = explode( ':', $this->dbhost ); 1141 1154 } 1142 1143 if ( !$this->dbh ) { 1155 if ( !$this->dbh->connect( $host, $this->dbuser, $this->dbpassword, $port ) ) { 1144 1156 wp_load_translations_early(); 1145 1157 $this->bail( sprintf( __( " 1146 1158 <h1>Error establishing a database connection</h1> … … class wpdb { 1192 1204 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1193 1205 $this->timer_start(); 1194 1206 1195 $ this->result = @mysql_query( $query, $this->dbh);1207 $result = $this->dbh->query( $query ); 1196 1208 $this->num_queries++; 1197 1209 1198 1210 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1199 1211 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1200 1212 1201 1213 // If there is an error then take note of it.. 1202 if ( $this->last_error = mysql_error( $this->dbh) ) {1214 if ( $this->last_error = $this->dbh->get_error_message() ) { 1203 1215 $this->print_error(); 1204 1216 return false; 1205 1217 } … … class wpdb { 1207 1219 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 1208 1220 $return_val = $this->result; 1209 1221 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 1210 $this->rows_affected = mysql_affected_rows( $this->dbh);1222 $this->rows_affected = $this->dbh->affected_rows(); 1211 1223 // Take note of the insert_id 1212 1224 if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { 1213 $this->insert_id = mysql_insert_id($this->dbh);1225 $this->insert_id = $this->dbh->insert_id(); 1214 1226 } 1215 1227 // Return number of rows affected 1216 1228 $return_val = $this->rows_affected; 1217 1229 } else { 1218 $num_rows = 0; 1219 while ( $row = @mysql_fetch_object( $this->result ) ) { 1220 $this->last_result[$num_rows] = $row; 1221 $num_rows++; 1222 } 1223 1224 // Log number of rows the query returned 1225 // and return number of rows selected 1226 $this->num_rows = $num_rows; 1227 $return_val = $num_rows; 1230 $return_val = $this->num_rows = count( $this->last_result ); 1228 1231 } 1229 1232 1233 $this->last_result = $this->dbh->get_results(); 1230 1234 return $return_val; 1231 1235 } 1232 1236 … … class wpdb { 1556 1560 * @access protected 1557 1561 */ 1558 1562 protected function load_col_info() { 1559 if ( $this->col_info ) 1560 return; 1561 1562 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 1563 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1564 } 1563 $this->col_info = $this->dbh->load_col_info(); 1565 1564 } 1566 1565 1567 1566 /** … … class wpdb { 1732 1731 * @return false|string false on failure, version number on success 1733 1732 */ 1734 1733 function db_version() { 1735 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));1734 return $this->dbh->db_version(); 1736 1735 } 1737 1736 }