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