| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimization_repairtables extends WP_Optimization { |
| 6 |
|
| 7 |
public $available_for_auto = false; |
| 8 |
|
| 9 |
public $setting_default = true; |
| 10 |
|
| 11 |
public $changes_table_data = true; |
| 12 |
|
| 13 |
public $run_multisite = false; |
| 14 |
|
| 15 |
public $support_preview = false; |
| 16 |
|
| 17 |
/** |
| 18 |
* Display or hide optimization in optimizations list. |
| 19 |
* |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
public function display_in_optimizations_list() { |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Run optimization. |
| 28 |
*/ |
| 29 |
public function optimize() { |
| 30 |
// check if single table name posted or optimize all tables. |
| 31 |
if (isset($this->data['optimization_table']) && '' != $this->data['optimization_table']) { |
| 32 |
$table = $this->optimizer->get_table($this->data['optimization_table']); |
| 33 |
|
| 34 |
$result = (false === $table) ? false : $this->repair_table($table); |
| 35 |
|
| 36 |
if ($result) { |
| 37 |
$wp_optimize = WP_Optimize(); |
| 38 |
$tablestatus = $wp_optimize->get_db_info()->get_table_status($table->Name, true); |
| 39 |
|
| 40 |
$is_optimizable = $wp_optimize->get_db_info()->is_table_optimizable($table->Name); |
| 41 |
|
| 42 |
$tableinfo = array( |
| 43 |
'rows' => number_format_i18n($tablestatus->Rows), |
| 44 |
'data_size' => $wp_optimize->format_size($tablestatus->Data_length), |
| 45 |
'index_size' => $wp_optimize->format_size($tablestatus->Index_length), |
| 46 |
'overhead' => $is_optimizable ? $wp_optimize->format_size($tablestatus->Data_free) : '-', |
| 47 |
'type' => $table->Engine, |
| 48 |
'is_optimizable' => $is_optimizable, |
| 49 |
); |
| 50 |
|
| 51 |
$this->register_meta('tableinfo', $tableinfo); |
| 52 |
} |
| 53 |
|
| 54 |
$this->register_meta('success', $result); |
| 55 |
} else { |
| 56 |
$tables = $this->optimizer->get_tables(); |
| 57 |
$repaired = $corrupted = 0; |
| 58 |
|
| 59 |
foreach ($tables as $table) { |
| 60 |
if (false == $table->is_needing_repair) continue; |
| 61 |
|
| 62 |
if ($this->repair_table($table)) { |
| 63 |
$repaired++; |
| 64 |
} else { |
| 65 |
$corrupted++; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
$this->register_output(sprintf(_n('%s table repaired', '%s tables repaired', $repaired), $repaired)); |
| 70 |
|
| 71 |
if ($corrupted > 0) { |
| 72 |
$this->register_output(sprintf(_n('Repairing %s table was unsuccessful', 'Repairing %s tables were unsuccessful', $corrupted), $corrupted)); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Repair table. |
| 79 |
* |
| 80 |
* @param object $table_obj object contains information about database table. |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
private function repair_table($table_obj) { |
| 85 |
global $wpdb; |
| 86 |
|
| 87 |
$success = false; |
| 88 |
|
| 89 |
if (false == $table_obj->is_needing_repair) return true; |
| 90 |
|
| 91 |
$this->logger->info('REPAIR TABLE '.$table_obj->Name); |
| 92 |
|
| 93 |
$results = $wpdb->get_results('REPAIR TABLE '.$table_obj->Name); |
| 94 |
|
| 95 |
if (!empty($results)) { |
| 96 |
foreach ($results as $row) { |
| 97 |
if ('status' == strtolower($row->Msg_type) && 'ok' == strtolower($row->Msg_text)) { |
| 98 |
$success = true; |
| 99 |
} |
| 100 |
|
| 101 |
$this->logger->info($row->Msg_text); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// update info in options table and use it to notify user about corrupted tables. |
| 106 |
$this->get_corrupted_tables_count(); |
| 107 |
|
| 108 |
return $success; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Returns count of corrupted tables and update corrupted-tables-count value in options used to show |
| 113 |
* information for user in sidebar about corrupted tables count. |
| 114 |
*/ |
| 115 |
public function get_corrupted_tables_count() { |
| 116 |
$tablesinfo = $this->optimizer->get_tables(); |
| 117 |
|
| 118 |
$corrupted_tables = 0; |
| 119 |
|
| 120 |
if (!empty($tablesinfo)) { |
| 121 |
foreach ($tablesinfo as $tableinfo) { |
| 122 |
if ($tableinfo->is_needing_repair) { |
| 123 |
$corrupted_tables++; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// save results to options table and use it to notify user about corrupted tables. |
| 129 |
$this->options->update_option('corrupted-tables-count', $corrupted_tables); |
| 130 |
|
| 131 |
return $corrupted_tables; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Register info about optimization. |
| 136 |
*/ |
| 137 |
public function get_info() { |
| 138 |
|
| 139 |
$corrupted_tables = $this->get_corrupted_tables_count(); |
| 140 |
|
| 141 |
if (0 == $corrupted_tables) { |
| 142 |
$this->register_output(__('No corrupted tables found', 'wp-optimize')); |
| 143 |
} else { |
| 144 |
$this->register_output(sprintf(_n('%s corrupted table found', '%s corrupted tables found', $corrupted_tables), $corrupted_tables)); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns settings label. |
| 150 |
* |
| 151 |
* @return string |
| 152 |
*/ |
| 153 |
public function settings_label() { |
| 154 |
return __('Repair database tables', 'wp-optimize'); |
| 155 |
} |
| 156 |
} |
| 157 |
|