| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: MySQL Ping |
|---|
| 4 | Plugin URI: http://kaloyan.info/blog/wp-mysql-ping-plugin/ |
|---|
| 5 | Description: This plugin is designed to help you deal with the "MySQL Server Has Gone Away" error. |
|---|
| 6 | Author: Kaloyan K. Tsvetkov |
|---|
| 7 | Version: 0.1 |
|---|
| 8 | Author URI: http://kaloyan.info/ |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * @internal prevent from direct calls |
|---|
| 15 | */ |
|---|
| 16 | if (!defined('ABSPATH')) { |
|---|
| 17 | return ; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * @internal prevent from second inclusion |
|---|
| 22 | */ |
|---|
| 23 | if (!class_exists('wpdb2')) { |
|---|
| 24 | |
|---|
| 25 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * "MySQL Ping" WordPress Plugin |
|---|
| 29 | * |
|---|
| 30 | * Extend the default {@link wpdb} class by |
|---|
| 31 | * adding {@link mysql_ping()} capabilities |
|---|
| 32 | * |
|---|
| 33 | * @author Kaloyan K. Tsvetkov <kaloyan@kaloyan.info> |
|---|
| 34 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|---|
| 35 | */ |
|---|
| 36 | Class wpdb2 Extends wpdb { |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * Attemp to ping the MySQL server |
|---|
| 40 | */ |
|---|
| 41 | function _ping() { |
|---|
| 42 | |
|---|
| 43 | $retry = 5; |
|---|
| 44 | $failed = 1; |
|---|
| 45 | |
|---|
| 46 | // probe w\ a ping |
|---|
| 47 | // |
|---|
| 48 | $ping = mysql_ping( $this->dbh ) ; |
|---|
| 49 | while( !$ping && $failed < $retry) { |
|---|
| 50 | |
|---|
| 51 | //$this->print_error('No ping go MySQL server'); |
|---|
| 52 | |
|---|
| 53 | // reconnect |
|---|
| 54 | // |
|---|
| 55 | $this->dbh = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, 1); |
|---|
| 56 | $this->select(DB_NAME); |
|---|
| 57 | |
|---|
| 58 | if ( !DB_CHARSET && version_compare(mysql_get_server_info($this->dbh), '4.1.0', '>=')) { |
|---|
| 59 | $this->query("SET NAMES '" . DB_CHARSET . "'"); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | // ping again to check the result |
|---|
| 63 | // |
|---|
| 64 | $ping = mysql_ping( $this->dbh ) ; |
|---|
| 65 | if(!$ping ) { |
|---|
| 66 | sleep(2); |
|---|
| 67 | $failed+=1; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | // yet no result ... |
|---|
| 72 | // |
|---|
| 73 | if(!$ping ) { |
|---|
| 74 | $this->print_error( |
|---|
| 75 | 'Attempted to connect for ' |
|---|
| 76 | . $retry |
|---|
| 77 | . ' but failed...' |
|---|
| 78 | ); |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Override the original {@link wpdb::query()} method in |
|---|
| 86 | * order to ping the server before executing every query |
|---|
| 87 | * |
|---|
| 88 | * @param string $query |
|---|
| 89 | * @return mixed |
|---|
| 90 | */ |
|---|
| 91 | function query($query) { |
|---|
| 92 | $this->_ping(); |
|---|
| 93 | return parent::query($query); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | //--end-of-class-- |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 100 | |
|---|
| 101 | // create new object, and copy all object's properties |
|---|
| 102 | // |
|---|
| 103 | $wpdb2 = new wpdb2(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); |
|---|
| 104 | foreach(get_object_vars($wpdb) as $k=>$v) { |
|---|
| 105 | if (is_scalar($v)) { |
|---|
| 106 | $wpdb2->$k = $v; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | $wpdb =& $wpdb2; |
|---|
| 110 | |
|---|
| 111 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 112 | |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | ?> |
|---|