PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.19
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.19
4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 3.8.0 All 110 releases
wp-optimize / optimizations / repairtables.php

repairtables.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.19, at optimizations/repairtables.php

160 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
39 $tablestatus = $wp_optimize->get_db_info()->get_table_status($table->Name, true);
40
41 $is_optimizable = $wp_optimize->get_db_info()->is_table_optimizable($table->Name);
42 $is_type_supported = $wp_optimize->get_db_info()->is_table_type_optimize_supported($table->Name);
43
44 $tableinfo = array(
45 'rows' => number_format_i18n($tablestatus->Rows),
46 'data_size' => $wp_optimize->format_size($tablestatus->Data_length),
47 'index_size' => $wp_optimize->format_size($tablestatus->Index_length),
48 'overhead' => $is_optimizable ? $wp_optimize->format_size($tablestatus->Data_free) : '-',
49 'type' => $tablestatus->Engine,
50 'is_optimizable' => $is_optimizable,
51 'is_type_supported' => $is_type_supported,
52 );
53
54 $this->register_meta('tableinfo', $tableinfo);
55 }
56
57 $this->register_meta('success', $result);
58 } else {
59 $tables = $this->optimizer->get_tables();
60 $repaired = $corrupted = 0;
61
62 foreach ($tables as $table) {
63 if (false == $table->is_needing_repair) continue;
64
65 if ($this->repair_table($table)) {
66 $repaired++;
67 } else {
68 $corrupted++;
69 }
70 }
71
72 $this->register_output(sprintf(_n('%s table repaired', '%s tables repaired', $repaired), $repaired));
73
74 if ($corrupted > 0) {
75 $this->register_output(sprintf(_n('Repairing %s table was unsuccessful', 'Repairing %s tables were unsuccessful', $corrupted), $corrupted));
76 }
77 }
78 }
79
80 /**
81 * Repair table.
82 *
83 * @param object $table_obj object contains information about database table.
84 *
85 * @return bool
86 */
87 private function repair_table($table_obj) {
88 global $wpdb;
89
90 $success = false;
91
92 if (false == $table_obj->is_needing_repair) return true;
93
94 $this->logger->info('REPAIR TABLE `'.$table_obj->Name. '`');
95
96 $results = $wpdb->get_results('REPAIR TABLE `'.$table_obj->Name . '`');
97
98 if (!empty($results)) {
99 foreach ($results as $row) {
100 if ('status' == strtolower($row->Msg_type) && 'ok' == strtolower($row->Msg_text)) {
101 $success = true;
102 }
103
104 $this->logger->info($row->Msg_text);
105 }
106 }
107
108 // update info in options table and use it to notify user about corrupted tables.
109 $this->get_corrupted_tables_count();
110
111 return $success;
112 }
113
114 /**
115 * Returns count of corrupted tables and update corrupted-tables-count value in options used to show
116 * information for user in sidebar about corrupted tables count.
117 */
118 public function get_corrupted_tables_count() {
119 $tablesinfo = $this->optimizer->get_tables();
120
121 $corrupted_tables = 0;
122
123 if (!empty($tablesinfo)) {
124 foreach ($tablesinfo as $tableinfo) {
125 if ($tableinfo->is_needing_repair) {
126 $corrupted_tables++;
127 }
128 }
129 }
130
131 // save results to options table and use it to notify user about corrupted tables.
132 $this->options->update_option('corrupted-tables-count', $corrupted_tables);
133
134 return $corrupted_tables;
135 }
136
137 /**
138 * Register info about optimization.
139 */
140 public function get_info() {
141
142 $corrupted_tables = $this->get_corrupted_tables_count();
143
144 if (0 == $corrupted_tables) {
145 $this->register_output(__('No corrupted tables found', 'wp-optimize'));
146 } else {
147 $this->register_output(sprintf(_n('%s corrupted table found', '%s corrupted tables found', $corrupted_tables), $corrupted_tables));
148 }
149 }
150
151 /**
152 * Returns settings label.
153 *
154 * @return string
155 */
156 public function settings_label() {
157 return __('Repair database tables', 'wp-optimize');
158 }
159 }
160