Ticket #21663: 21663.3.patch
File 21663.3.patch, 21.5 KB (added by , 12 years ago) |
---|
-
wordpress/wp-includes/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 -
wordpress/wp-includes/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 } -
wordpress/wp-includes/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 -
wordpress/wp-includes/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 return $error[2]; 61 } 62 63 /** 64 * Free memory associated with the resultset 65 * @return void 66 */ 67 public function flush() { 68 if ( $this->result instanceof PDOStatement ) { 69 $this->result->closeCursor(); 70 } 71 $this->result = null; 72 $this->col_info = null; 73 $this->fetched_rows = array(); 74 } 75 76 /** 77 * Connect to database 78 * @return bool 79 */ 80 public function connect( $host, $user, $pass, $port = 3306 ) { 81 $dsn = sprintf( 'mysql:host=%1$s;port=%2$d', $host, $port ); 82 try { 83 $this->dbh = new PDO( $dsn, $user, $pass ); 84 } catch ( Exception $e ) { 85 return false; 86 } 87 return true; 88 } 89 90 /** 91 * Select database 92 * @return void 93 */ 94 public function select( $db ) { 95 $this->dbh->exec( sprintf( 'USE %s', $db ) ); 96 } 97 98 /** 99 * Perform a MySQL database query, using current database connection. 100 * @param string $query Database query 101 * @return int|false Number of rows affected/selected or false on error 102 */ 103 public function query( $query ) { 104 try { 105 $this->result = $this->dbh->query( $query ); 106 } catch ( Exception $e ) { 107 } 108 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 109 $return_val = $this->result; 110 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 111 $return_val = $this->affected_rows(); 112 } elseif ( preg_match( '/^\s*select\s/i', $query ) ) { 113 $this->get_results(); 114 return count( $this->fetched_rows ); 115 } 116 return true; 117 } 118 119 /** 120 * Get number of rows affected 121 * @return int 122 */ 123 public function affected_rows() { 124 return $this->result->rowCount(); 125 } 126 127 /** 128 * Get last insert id 129 * @return int 130 */ 131 public function insert_id() { 132 return $this->dbh->lastInsertId(); 133 } 134 135 /** 136 * Get results 137 * @return array 138 */ 139 public function get_results() { 140 if ( !empty( $this->fetched_rows ) ) { 141 return $this->fetched_rows; 142 } 143 $this->fetched_rows = array(); 144 while ( $row = $this->result->fetchObject() ) { 145 $this->fetched_rows[] = $row; 146 } 147 return $this->fetched_rows; 148 } 149 150 /** 151 * Load the column metadata from the last query. 152 * @return array 153 */ 154 public function load_col_info() { 155 if ( $this->col_info ) 156 return $this->col_info; 157 for ( $i = 0; $i < $this->result->columnCount() ; $i++ ) { 158 $this->col_info[ $i ] = $this->result->fetchColumn( $i ); 159 } 160 return $this->col_info; 161 } 162 163 /** 164 * The database version number. 165 * @return false|string false on failure, version number on success 166 */ 167 public function db_version() { 168 return preg_replace( '/[^0-9.].*/', '', $this->dbh->getAttribute( PDO::ATTR_SERVER_VERSION ) ); 169 } 170 } 171 No newline at end of file -
wordpress/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( $driver = null ) { 535 536 // Auto-pick the driver 537 if ( null === $driver ) { 538 if ( extension_loaded( 'mysql' ) ) { 539 $driver = 'mysql'; 540 } elseif ( extension_loaded( 'mysqli' ) ) { 541 $driver = 'mysqli'; 542 } elseif ( extension_loaded( 'pdo_mysql' ) ) { 543 $driver = 'pdo_mysql'; 544 } else { 545 wp_load_translations_early(); 546 $this->bail( sprintf( __( " 547 <h1>No database drivers found</h1>. 548 <p>WordPress requires the mysql, mysqli, or pdo_mysql extension to talk to your database.</p> 549 <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> 550 "), 'db_connect_fail' ) ); 551 } 552 } elseif ( !in_array( $driver, array( 'mysql', 'mysqli', 'pdo_mysql' ) ) ) { 553 wp_load_translations_early(); 554 $this->bail( sprintf( __( " 555 <h1>No database drivers found</h1>. 556 <p>WordPress requires the mysql, mysqli, or pdo_mysql extension to talk to your database.</p> 557 <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> 558 "), 'db_connect_fail' ) ); 559 } 560 561 // Get the new driver 562 require_once(dirname(dirname(__FILE__)) . '/wp-includes/wp-db-driver.interface.php'); 563 require_once(dirname(dirname(__FILE__)) . '/wp-includes/wp-db-driver-' . $driver . '.php'); 564 $class = 'wpdb_driver_' . $driver; 565 $this->dbh = new $class(); 566 } 567 568 /** 522 569 * Connects to the database server and selects a database 523 570 * 524 571 * PHP5 style constructor for compatibility with PHP5. Does … … 534 581 * @param string $dbhost MySQL database host 535 582 */ 536 583 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { 584 585 $this->get_driver(); 586 537 587 register_shutdown_function( array( &$this, '__destruct' ) ); 538 588 539 589 if ( WP_DEBUG ) … … 646 696 if ( !isset($collate) ) 647 697 $collate = $this->collate; 648 698 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 } 699 $query = $this->prepare( 'SET NAMES %s', $charset ); 700 if ( ! empty( $collate ) ) 701 $query .= $this->prepare( ' COLLATE %s', $collate ); 702 $this->dbh->query( $query ); 658 703 } 659 704 } 660 705 … … 837 882 * @return null Always null. 838 883 */ 839 884 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 } 885 $this->dbh->select( $db ); 856 886 } 857 887 858 888 /** … … 882 912 */ 883 913 function _real_escape( $string ) { 884 914 if ( $this->dbh && $this->real_escape ) 885 return mysql_real_escape_string( $string, $this->dbh);915 return $this->dbh->escape( $string ); 886 916 else 887 917 return addslashes( $string ); 888 918 } … … 1017 1047 global $EZSQL_ERROR; 1018 1048 1019 1049 if ( !$str ) 1020 $str = mysql_error( $this->dbh);1050 $str = $this->dbh->get_error_message(); 1021 1051 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 1022 1052 1023 1053 if ( $this->suppress_errors ) … … 1118 1148 * @return void 1119 1149 */ 1120 1150 function flush() { 1121 $this->last_result = array(); 1122 $this->col_info = null; 1123 $this->last_query = null; 1124 1125 if ( is_resource( $this->result ) ) 1126 mysql_free_result( $this->result ); 1151 $this->dbh->flush(); 1127 1152 } 1128 1153 1129 1154 /** … … 1135 1160 1136 1161 $this->is_mysql = true; 1137 1162 1138 $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; 1139 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; 1140 1141 if ( WP_DEBUG ) { 1142 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1143 } else { 1144 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1163 $host = $this->dbhost; 1164 $port = 3306; 1165 if ( false !== strpos( $this->dbhost, ':' ) ) { 1166 list( $host, $port ) = explode( ':', $this->dbhost ); 1145 1167 } 1146 1147 if ( !$this->dbh ) { 1168 if ( !$this->dbh->connect( $host, $this->dbuser, $this->dbpassword, $port ) ) { 1148 1169 wp_load_translations_early(); 1149 1170 $this->bail( sprintf( __( " 1150 1171 <h1>Error establishing a database connection</h1> … … 1196 1217 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1197 1218 $this->timer_start(); 1198 1219 1199 $ this->result = @mysql_query( $query, $this->dbh);1220 $result = $this->dbh->query( $query ); 1200 1221 $this->num_queries++; 1201 1222 1202 1223 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1203 1224 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1204 1225 1205 1226 // If there is an error then take note of it.. 1206 if ( $this->last_error = mysql_error( $this->dbh) ) {1227 if ( $this->last_error = $this->dbh->get_error_message() ) { 1207 1228 $this->print_error(); 1208 1229 return false; 1209 1230 } … … 1211 1232 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 1212 1233 $return_val = $this->result; 1213 1234 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 1214 $this->rows_affected = mysql_affected_rows( $this->dbh);1235 $this->rows_affected = $this->dbh->affected_rows(); 1215 1236 // Take note of the insert_id 1216 1237 if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { 1217 $this->insert_id = mysql_insert_id($this->dbh);1238 $this->insert_id = $this->dbh->insert_id(); 1218 1239 } 1219 1240 // Return number of rows affected 1220 1241 $return_val = $this->rows_affected; 1221 1242 } else { 1222 $num_rows = 0; 1223 while ( $row = @mysql_fetch_object( $this->result ) ) { 1224 $this->last_result[$num_rows] = $row; 1225 $num_rows++; 1226 } 1227 1228 // Log number of rows the query returned 1229 // and return number of rows selected 1230 $this->num_rows = $num_rows; 1231 $return_val = $num_rows; 1243 $this->last_result = $this->dbh->get_results(); 1244 $return_val = $this->num_rows = count( $this->last_result ); 1232 1245 } 1233 1246 1234 1247 return $return_val; 1235 1248 } 1236 1249 … … 1560 1573 * @access protected 1561 1574 */ 1562 1575 protected function load_col_info() { 1563 if ( $this->col_info ) 1564 return; 1565 1566 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 1567 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1568 } 1576 $this->col_info = $this->dbh->load_col_info(); 1569 1577 } 1570 1578 1571 1579 /** … … 1736 1744 * @return false|string false on failure, version number on success 1737 1745 */ 1738 1746 function db_version() { 1739 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));1747 return $this->dbh->db_version(); 1740 1748 } 1741 1749 }