| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimization_optimizetables extends WP_Optimization { |
| 6 |
|
| 7 |
protected $auto_id = 'optimize'; |
| 8 |
|
| 9 |
protected $setting_id = 'optimize'; |
| 10 |
|
| 11 |
protected $dom_id = 'optimize-db'; |
| 12 |
|
| 13 |
public $available_for_saving = true; |
| 14 |
|
| 15 |
public $available_for_auto = true; |
| 16 |
|
| 17 |
public $setting_default = true; |
| 18 |
|
| 19 |
public $changes_table_data = true; |
| 20 |
|
| 21 |
public $ui_sort_order = 500; |
| 22 |
|
| 23 |
public $run_sort_order = 100000; |
| 24 |
|
| 25 |
public $run_multisite = false; |
| 26 |
|
| 27 |
public $support_preview = false; |
| 28 |
|
| 29 |
/** |
| 30 |
* Run optimization. |
| 31 |
*/ |
| 32 |
public function optimize() { |
| 33 |
// check if force optimize sent. |
| 34 |
$force = (isset($this->data['optimization_force']) && $this->data['optimization_force']) ? true : false; |
| 35 |
|
| 36 |
// check if single table name posted or optimize all tables. |
| 37 |
if (isset($this->data['optimization_table']) && '' != $this->data['optimization_table']) { |
| 38 |
$table = $this->optimizer->get_table($this->data['optimization_table']); |
| 39 |
|
| 40 |
if (false !== $table) $this->optimize_table($table, $force); |
| 41 |
} else { |
| 42 |
$tables = $this->optimizer->get_tables(); |
| 43 |
|
| 44 |
foreach ($tables as $table) { |
| 45 |
$this->optimize_table($table, $force); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Optimize table and generate log and output information. |
| 52 |
* |
| 53 |
* @param object $table_obj table object returned by $this->optimizer->get_tables(). |
| 54 |
* @param bool $force if true then will optimize |
| 55 |
*/ |
| 56 |
private function optimize_table($table_obj, $force = false) { |
| 57 |
|
| 58 |
// if not forced and table is not optimizable then exit. |
| 59 |
if (false == $force && (false == $table_obj->is_optimizable || false == $table_obj->is_type_supported)) return; |
| 60 |
|
| 61 |
if ($table_obj->is_type_supported) { |
| 62 |
$this->logger->info('Optimizing: ' . $table_obj->Name); |
| 63 |
$this->query('OPTIMIZE TABLE ' . $table_obj->Name); |
| 64 |
|
| 65 |
// For InnoDB Data_free doesn't contain free size. |
| 66 |
if ('InnoDB' != $table_obj->Engine) { |
| 67 |
$this->optimizer->update_total_cleaned(strval($table_obj->Data_free)); |
| 68 |
} |
| 69 |
|
| 70 |
$this->register_output(__('Optimizing Table:', 'wp-optimize') . ' ' . $table_obj->Name); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Return info about optimization. |
| 76 |
*/ |
| 77 |
public function get_info() { |
| 78 |
// This gathers information to be displayed onscreen before optimization. |
| 79 |
$tablesstatus = $this->optimizer->get_table_information(); |
| 80 |
|
| 81 |
// Check if database is not optimizable. |
| 82 |
if (false === $tablesstatus['is_optimizable']) { |
| 83 |
if (isset($this->data['optimization_table']) && '' != $this->data['optimization_table']) { |
| 84 |
// This is used for grabbing information before optimizations. |
| 85 |
$this->register_output(__('Total gain:', 'wp-optimize').' '.WP_Optimize()->format_size(($tablesstatus['total_gain']))); |
| 86 |
} |
| 87 |
|
| 88 |
if ($tablesstatus['inno_db_tables'] > 0) { |
| 89 |
// Output message for how many InnoDB tables will not be optimized. |
| 90 |
$this->register_output(sprintf(__('Tables using the InnoDB engine (%d) will not be optimized.'), $tablesstatus['inno_db_tables'])); |
| 91 |
|
| 92 |
if ($tablesstatus['non_inno_db_tables'] > 0) { |
| 93 |
$this->register_output(sprintf(__('Other tables will be optimized (%s).', 'wp-optimize'), $tablesstatus['non_inno_db_tables'])); |
| 94 |
} |
| 95 |
|
| 96 |
$faq_url = apply_filters('wpo_faq_url', 'https://getwpo.com/faqs/'); |
| 97 |
$force_db_option = $this->options->get_option('innodb-force-optimize', 'false'); |
| 98 |
$this->register_output('<input id="innodb_force_optimize" name="innodb-force-optimize" type="checkbox" value="true" '.checked($force_db_option, 'true').'><label for="innodb_force_optimize">'.__('Optimize InnoDB tables anyway.', 'wp-optimize').'</label><br><a href="'.$faq_url.'" target="_blank">'.__('Warning: you should read the FAQ on the risks of this operation first.', 'wp-optimize').'</a>'); |
| 99 |
} |
| 100 |
} else { |
| 101 |
$this->register_output(sprintf(__('Tables will be optimized (%s).', 'wp-optimize'), $tablesstatus['non_inno_db_tables'] + $tablesstatus['inno_db_tables'])); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function get_auto_option_description() { |
| 106 |
return __('Optimize database tables', 'wp-optimize'); |
| 107 |
} |
| 108 |
|
| 109 |
public function settings_label() { |
| 110 |
return __('Optimize database tables', 'wp-optimize'); |
| 111 |
} |
| 112 |
} |
| 113 |
|