Ticket #21663: 21663.5.patch
File 21663.5.patch, 21.5 KB (added by , 12 years ago) |
---|
-
wp-includes/class.wp-db-driver-mysql.php
1 <?php 2 3 /** 4 * WordPress Database Access Abstraction Object 5 * 6 * It is possible to replace this class with your own 7 * by setting the $wpdb global variable in wp-content/db.php 8 * file to your class. The wpdb class will still be included, 9 * so you can extend it or simply use your own. 10 * 11 * @link http://codex.wordpress.org/Function_Reference/wpdb_Class 12 * 13 * @package WordPress 14 * @subpackage Database 15 * @since 0.71 16 */ 17 class wpdb_driver_mysql implements wpdb_driver { 18 19 /** 20 * Database link 21 * @var resource 22 */ 23 private $dbh = null; 24 25 /** 26 * Result set 27 * @var resource 28 */ 29 private $result = null; 30 31 /** 32 * Cached column info 33 * @var array|null 34 */ 35 private $col_info = null; 36 37 /** 38 * Escape with mysql_real_escape_string() 39 * @param string $string 40 * @return string 41 */ 42 public function escape( $string ) { 43 return mysql_real_escape_string( $string, $this->dbh ); 44 } 45 46 /** 47 * Get the latest error message from the DB driver 48 * @return string 49 */ 50 public function get_error_message() { 51 return mysql_error( $this->dbh ); 52 } 53 54 /** 55 * Free memory associated with the resultset 56 * @return void 57 */ 58 public function flush() { 59 if ( is_resource( $this->result ) ) { 60 mysql_free_result( $this->result ); 61 } 62 $this->result = null; 63 $this->col_info = null; 64 } 65 66 /** 67 * Connect to database 68 * @return bool 69 */ 70 public function connect( $host, $user, $pass, $port = 3306 ) { 71 72 $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; 73 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; 74 75 if ( WP_DEBUG ) { 76 $this->dbh = mysql_connect( "$host:$port", $user, $pass, $new_link, $client_flags ); 77 } else { 78 $this->dbh = @mysql_connect( "$host:$port", $user, $pass, $new_link, $client_flags ); 79 } 80 return ( false !== $this->dbh ); 81 } 82 83 /** 84 * Select database 85 * @return void 86 */ 87 public function select( $db ) { 88 if ( WP_DEBUG ) { 89 mysql_select_db( $db, $this->dbh ); 90 } else { 91 @mysql_select_db( $db, $this->dbh ); 92 } 93 } 94 95 /** 96 * Perform a MySQL database query, using current database connection. 97 * @param string $query Database query 98 * @return int|false Number of rows affected/selected or false on error 99 */ 100 public function query( $query ) { 101 $this->result = @mysql_query( $query, $this->dbh ); 102 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 103 $return_val = $this->result; 104 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 105 $return_val = $this->affected_rows(); 106 } elseif ( preg_match( '/^\s*select\s/i', $query ) ) { 107 return is_resource( $this->result ) ? mysql_num_rows( $this->result ) : false ; 108 } 109 return true; 110 } 111 112 /** 113 * Get number of rows affected 114 * @return int 115 */ 116 public function affected_rows() { 117 return mysql_affected_rows( $this->dbh ); 118 } 119 120 /** 121 * Get last insert id 122 * @return int 123 */ 124 public function insert_id() { 125 return mysql_insert_id( $this->dbh ); 126 } 127 128 /** 129 * Get results 130 * @return array 131 */ 132 public function get_results() { 133 $ret = array(); 134 while ( $row = @mysql_fetch_object( $this->result ) ) { 135 $ret[] = $row; 136 } 137 return $ret; 138 } 139 140 /** 141 * Load the column metadata from the last query. 142 * @return array 143 */ 144 public function load_col_info() { 145 if ( $this->col_info ) 146 return $this->col_info; 147 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 148 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 149 } 150 return $this->col_info; 151 } 152 153 /** 154 * The database version number. 155 * @return false|string false on failure, version number on success 156 */ 157 public function db_version() { 158 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 159 } 160 } 161 No newline at end of file -
wp-includes/class.wp-db-driver-mysqli.php
1 <?php 2 3 /** 4 * WordPress Database Access Abstraction Object 5 * 6 * It is possible to replace this class with your own 7 * by setting the $wpdb global variable in wp-content/db.php 8 * file to your class. The wpdb class will still be included, 9 * so you can extend it or simply use your own. 10 * 11 * @link http://codex.wordpress.org/Function_Reference/wpdb_Class 12 * 13 * @package WordPress 14 * @subpackage Database 15 * @since 0.71 16 */ 17 class wpdb_driver_mysqli implements wpdb_driver { 18 19 /** 20 * Database link 21 * @var mysqli 22 */ 23 private $dbh = null; 24 25 /** 26 * Result set 27 * @var mysqli_stmt|mysqli_result 28 */ 29 private $result = null; 30 31 /** 32 * Cached column info 33 * @var array|null 34 */ 35 private $col_info = null; 36 37 /** 38 * Escape with mysql_real_escape_string() 39 * @param string $string 40 * @return string 41 */ 42 public function escape( $string ) { 43 return $this->dbh->escape_string( $string ); 44 } 45 46 /** 47 * Get the latest error message from the DB driver 48 * @return string 49 */ 50 public function get_error_message() { 51 return $this->dbh->error; 52 } 53 54 /** 55 * Free memory associated with the resultset 56 * @return void 57 */ 58 public function flush() { 59 if ( $this->result instanceof mysqli_stmt ) { 60 $this->result->free_result(); 61 } 62 $this->result = null; 63 $this->col_info = null; 64 } 65 66 /** 67 * Connect to database 68 * @return bool 69 */ 70 public function connect( $host, $user, $pass, $port = 3306 ) { 71 $this->dbh = new mysqli( $host, $user, $pass, '', $port ); 72 return ( !mysqli_connect_error() ); 73 } 74 75 /** 76 * Select database 77 * @return void 78 */ 79 public function select( $db ) { 80 $this->dbh->select_db( $db ); 81 } 82 83 /** 84 * Perform a MySQL database query, using current database connection. 85 * @param string $query Database query 86 * @return int|false Number of rows affected/selected or false on error 87 */ 88 public function query( $query ) { 89 $this->result = $this->dbh->query( $query ); 90 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 91 $return_val = $this->result; 92 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 93 $return_val = $this->affected_rows(); 94 } elseif ( preg_match( '/^\s*select\s/i', $query ) ) { 95 return $this->result->num_rows; 96 } 97 return true; 98 } 99 100 /** 101 * Get number of rows affected 102 * @return int 103 */ 104 public function affected_rows() { 105 return $this->dbh->affected_rows; 106 } 107 108 /** 109 * Get last insert id 110 * @return int 111 */ 112 public function insert_id() { 113 return $this->dbh->insert_id; 114 } 115 116 /** 117 * Get results 118 * @return array 119 */ 120 public function get_results() { 121 $ret = array(); 122 while ( $row = $this->result->fetch_object() ) { 123 $ret[] = $row; 124 } 125 return $ret; 126 } 127 128 /** 129 * Load the column metadata from the last query. 130 * @return array 131 */ 132 public function load_col_info() { 133 if ( $this->col_info ) 134 return $this->col_info; 135 for ( $i = 0; $i < $this->result->field_count ; $i++ ) { 136 $this->col_info[ $i ] = $this->result->fetch_field_direct( $i ); 137 } 138 return $this->col_info; 139 } 140 141 /** 142 * The database version number. 143 * @return false|string false on failure, version number on success 144 */ 145 public function db_version() { 146 return preg_replace( '/[^0-9.].*/', '', $this->dbh->server_version ); 147 } 148 } 149 No newline at end of file -
wp-includes/class.wp-db-driver-pdo_mysql.php
1 <?php 2 3 /** 4 * WordPress Database Access Abstraction Object 5 * 6 * It is possible to replace this class with your own 7 * by setting the $wpdb global variable in wp-content/db.php 8 * file to your class. The wpdb class will still be included, 9 * so you can extend it or simply use your own. 10 * 11 * @link http://codex.wordpress.org/Function_Reference/wpdb_Class 12 * 13 * @package WordPress 14 * @subpackage Database 15 * @since 0.71 16 */ 17 class wpdb_driver_pdo_mysql implements wpdb_driver { 18 19 /** 20 * Database link 21 * @var PDO 22 */ 23 private $dbh = null; 24 25 /** 26 * Result set 27 * @var PDOStatement 28 */ 29 private $result = null; 30 31 /** 32 * Cached column info 33 * @var array|null 34 */ 35 private $col_info = null; 36 37 /** 38 * Array of fetched rows. 39 * PDO doesn't have a "count rows" feature, so we have to fetch the rows 40 * up front, and cache them here 41 * @var array 42 */ 43 private $fetched_rows = array(); 44 45 /** 46 * Escape with mysql_real_escape_string() 47 * @param string $string 48 * @return string 49 */ 50 public function escape( $string ) { 51 return substr( $this->dbh->quote( $string ), 1, -1 ); 52 } 53 54 /** 55 * Get the latest error message from the DB driver 56 * @return string 57 */ 58 public function get_error_message() { 59 $error = $this->dbh->errorInfo(); 60 if ( isset( $error[2] ) ) { 61 return $error[2]; 62 } 63 return ''; 64 } 65 66 /** 67 * Free memory associated with the resultset 68 * @return void 69 */ 70 public function flush() { 71 if ( $this->result instanceof PDOStatement ) { 72 $this->result->closeCursor(); 73 } 74 $this->result = null; 75 $this->col_info = null; 76 $this->fetched_rows = array(); 77 } 78 79 /** 80 * Connect to database 81 * @return bool 82 */ 83 public function connect( $host, $user, $pass, $port = 3306 ) { 84 $dsn = sprintf( 'mysql:host=%1$s;port=%2$d', $host, $port ); 85 try { 86 $this->dbh = new PDO( $dsn, $user, $pass ); 87 } catch ( Exception $e ) { 88 return false; 89 } 90 return true; 91 } 92 93 /** 94 * Select database 95 * @return void 96 */ 97 public function select( $db ) { 98 $this->dbh->exec( sprintf( 'USE %s', $db ) ); 99 } 100 101 /** 102 * Perform a MySQL database query, using current database connection. 103 * @param string $query Database query 104 * @return int|false Number of rows affected/selected or false on error 105 */ 106 public function query( $query ) { 107 try { 108 $this->result = $this->dbh->query( $query ); 109 } catch ( Exception $e ) { 110 } 111 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 112 $return_val = $this->result; 113 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 114 $return_val = $this->affected_rows(); 115 } elseif ( preg_match( '/^\s*select\s/i', $query ) ) { 116 $this->get_results(); 117 return count( $this->fetched_rows ); 118 } 119 return true; 120 } 121 122 /** 123 * Get number of rows affected 124 * @return int 125 */ 126 public function affected_rows() { 127 if ( $this->result instanceof PDOStatement ) { 128 return $this->result->rowCount(); 129 } 130 return 0; 131 } 132 133 /** 134 * Get last insert id 135 * @return int 136 */ 137 public function insert_id() { 138 return $this->dbh->lastInsertId(); 139 } 140 141 /** 142 * Get results 143 * @return array 144 */ 145 public function get_results() { 146 if ( !empty( $this->fetched_rows ) ) { 147 return $this->fetched_rows; 148 } 149 $this->fetched_rows = array(); 150 if ( !empty( $this->result ) ) { 151 while ( $row = $this->result->fetchObject() ) { 152 $this->fetched_rows[] = $row; 153 } 154 } 155 return $this->fetched_rows; 156 } 157 158 /** 159 * Load the column metadata from the last query. 160 * @return array 161 */ 162 public function load_col_info() { 163 if ( $this->col_info ) 164 return $this->col_info; 165 for ( $i = 0; $i < $this->result->columnCount() ; $i++ ) { 166 $this->col_info[ $i ] = $this->result->fetchColumn( $i ); 167 } 168 return $this->col_info; 169 } 170 171 /** 172 * The database version number. 173 * @return false|string false on failure, version number on success 174 */ 175 public function db_version() { 176 return preg_replace( '/[^0-9.].*/', '', $this->dbh->getAttribute( PDO::ATTR_SERVER_VERSION ) ); 177 } 178 } 179 No newline at end of file -
wp-includes/class.wp-db-driver.interface.php
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
326 326 function require_wp_db() { 327 327 global $wpdb; 328 328 329 require( ABSPATH . WPINC . '/class.wp-db-driver.interface.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
487 487 protected $dbhost; 488 488 489 489 /** 490 * Database Handle490 * Database driver object 491 491 * 492 * @since 0.71 492 * Allow for simple, and backwards compatible, pluggable database drivers 493 * like PDO_mysql and mysqli to power wpdb 494 * 493 495 * @access protected 494 * @var string 496 * @since 3.5.0 497 * @var wpdb_driver 495 498 */ 496 499 protected $dbh; 497 500 … … 519 522 public $is_mysql = null; 520 523 521 524 /** 525 * Pick the right adapter and return an instance 526 * @static 527 * @since 3.5.0 528 * @param string $dbuser MySQL database user 529 * @param string $dbpassword MySQL database password 530 * @param string $dbname MySQL database name 531 * @param string $dbhost MySQL database host 532 * @return wpdb 533 */ 534 public function get_driver( ) { 535 536 // Auto-pick the driver 537 if ( defined( 'WPDB_DRIVER' ) ) { 538 $driver = WPDB_DRIVER; 539 } elseif ( extension_loaded( 'pdo_mysql' ) ) { 540 $driver = 'pdo_mysql'; 541 } elseif ( extension_loaded( 'mysqli' ) ) { 542 $driver = 'mysqli'; 543 } elseif ( extension_loaded( 'mysql' ) ) { 544 $driver = 'mysql'; 545 } 546 547 // Get the new driver 548 if ( in_array( $driver, array( 'mysql', 'mysqli', 'pdo_mysql' ) ) ) { 549 require_once( ABSPATH . WPINC . '/class.wp-db-driver-' . $driver . '.php' ); 550 } 551 $class = 'wpdb_driver_' . $driver; 552 553 if ( !class_exists( $class ) ) { 554 wp_load_translations_early(); 555 $this->bail( sprintf( __( " 556 <h1>No database drivers found</h1>. 557 <p>WordPress requires the mysql, mysqli, or pdo_mysql extension to talk to your database.</p> 558 <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> 559 "), 'db_connect_fail' ) ); 560 } 561 $this->dbh = new $class(); 562 } 563 564 /** 522 565 * Connects to the database server and selects a database 523 566 * 524 567 * PHP5 style constructor for compatibility with PHP5. Does … … 534 577 * @param string $dbhost MySQL database host 535 578 */ 536 579 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { 580 $this->get_driver(); 537 581 register_shutdown_function( array( $this, '__destruct' ) ); 538 582 539 583 if ( WP_DEBUG ) … … 646 690 if ( !isset($collate) ) 647 691 $collate = $this->collate; 648 692 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 } 693 $query = $this->prepare( 'SET NAMES %s', $charset ); 694 if ( ! empty( $collate ) ) 695 $query .= $this->prepare( ' COLLATE %s', $collate ); 696 $this->dbh->query( $query ); 658 697 } 659 698 } 660 699 … … 837 876 * @return null Always null. 838 877 */ 839 878 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 } 879 $this->dbh->select( $db ); 856 880 } 857 881 858 882 /** … … 882 906 */ 883 907 function _real_escape( $string ) { 884 908 if ( $this->dbh && $this->real_escape ) 885 return mysql_real_escape_string( $string, $this->dbh);909 return $this->dbh->escape( $string ); 886 910 else 887 911 return addslashes( $string ); 888 912 } … … 1017 1041 global $EZSQL_ERROR; 1018 1042 1019 1043 if ( !$str ) 1020 $str = mysql_error( $this->dbh);1044 $str = $this->dbh->get_error_message(); 1021 1045 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 1022 1046 1023 1047 if ( $this->suppress_errors ) … … 1114 1138 * @return void 1115 1139 */ 1116 1140 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 ); 1141 $this->dbh->flush(); 1123 1142 } 1124 1143 1125 1144 /** … … 1131 1150 1132 1151 $this->is_mysql = true; 1133 1152 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 ); 1153 $host = $this->dbhost; 1154 $port = 3306; 1155 if ( false !== strpos( $this->dbhost, ':' ) ) { 1156 list( $host, $port ) = explode( ':', $this->dbhost ); 1141 1157 } 1142 1143 if ( !$this->dbh ) { 1158 if ( !$this->dbh->connect( $host, $this->dbuser, $this->dbpassword, $port ) ) { 1144 1159 wp_load_translations_early(); 1145 1160 $this->bail( sprintf( __( " 1146 1161 <h1>Error establishing a database connection</h1> … … 1192 1207 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1193 1208 $this->timer_start(); 1194 1209 1195 $ this->result = @mysql_query( $query, $this->dbh);1210 $result = $this->dbh->query( $query ); 1196 1211 $this->num_queries++; 1197 1212 1198 1213 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1199 1214 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1200 1215 1201 1216 // If there is an error then take note of it.. 1202 if ( $this->last_error = mysql_error( $this->dbh) ) {1217 if ( $this->last_error = $this->dbh->get_error_message() ) { 1203 1218 $this->print_error(); 1204 1219 return false; 1205 1220 } … … 1207 1222 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 1208 1223 $return_val = $this->result; 1209 1224 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 1210 $this->rows_affected = mysql_affected_rows( $this->dbh);1225 $this->rows_affected = $this->dbh->affected_rows(); 1211 1226 // Take note of the insert_id 1212 1227 if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { 1213 $this->insert_id = mysql_insert_id($this->dbh);1228 $this->insert_id = $this->dbh->insert_id(); 1214 1229 } 1215 1230 // Return number of rows affected 1216 1231 $return_val = $this->rows_affected; 1217 1232 } 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; 1233 $return_val = $this->num_rows = count( $this->last_result ); 1228 1234 } 1229 1235 1236 $this->last_result = $this->dbh->get_results(); 1230 1237 return $return_val; 1231 1238 } 1232 1239 … … 1556 1563 * @access protected 1557 1564 */ 1558 1565 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 } 1566 $this->col_info = $this->dbh->load_col_info(); 1565 1567 } 1566 1568 1567 1569 /** … … 1732 1734 * @return false|string false on failure, version number on success 1733 1735 */ 1734 1736 function db_version() { 1735 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));1737 return $this->dbh->db_version(); 1736 1738 } 1737 1739 }