This repository was archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathwp-optimize-tables.php
224 lines (188 loc) · 5.31 KB
/
wp-optimize-tables.php
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
# --------------------------------------- #
# prevent file from being accessed directly
# --------------------------------------- #
if ( ! defined( 'WPINC' ) ) {
die;
}
if (isset($_POST["optimize-db"])) {
optimizeTables(true);
}
else optimizeTables(false);
?>
<?php
Function optimizeTables($Optimize=false){
?>
<h3>
<?php
_e('Database Name:', 'wp-optimize'); ?> '<?php _e(DB_NAME, 'wp-optimize');
echo "'";
?></h3>
<?php if($Optimize){
?>
<p><?php _e('Optimized all the tables found in the database.', 'wp-optimize')?></p>
<?php } ?>
<br style="clear" />
<table class="widefat">
<thead>
<tr>
<th><?php _e('No.', 'wp-optimize'); ?></th>
<th><?php _e('Tables', 'wp-optimize'); ?></th>
<th><?php _e('Records', 'wp-optimize'); ?></th>
<th><?php _e('Data Size', 'wp-optimize'); ?></th>
<th><?php _e('Index Size', 'wp-optimize'); ?></th>
<th><?php _e('Type', 'wp-optimize'); ?></th>
<th><?php _e('Overhead', 'wp-optimize');?></th>
</tr>
</thead>
<tbody id="the-list">
<?php
$alternate = ' class="alternate"';
global $wpdb;
// Read SQL Version and act accordingly
// Check for InnoDB tables
// Check for windows servers
$sqlversion = $wpdb->get_var("SELECT VERSION() AS version");
$total_gain = 0;
$no = 0;
$row_usage = 0;
$data_usage = 0;
$index_usage = 0;
$overhead_usage = 0;
$tablesstatus = $wpdb->get_results("SHOW TABLE STATUS");
foreach($tablesstatus as $tablestatus) {
if($no%2 == 0) {
$style = '';
} else {
$style = ' class="alternate"';
}
$no++;
echo "<tr$style>\n";
echo '<td>'.number_format_i18n($no).'</td>'."\n";
echo "<td>$tablestatus->Name</td>\n";
echo '<td>'.number_format_i18n($tablestatus->Rows).'</td>'."\n";
echo '<td>'.wpo_format_size($tablestatus->Data_length).'</td>'."\n";
echo '<td>'.wpo_format_size($tablestatus->Index_length).'</td>'."\n";;
echo '<td>'.$tablestatus->Engine.'</td>'."\n";;
//echo '<td>'.wpo_format_size($tablestatus->Data_free).'</td>'."\n";
if ($tablestatus->Engine != 'InnoDB'){
echo '<td>';
if (isset($_POST["optimize-db"])) {
if($tablestatus->Data_free>0){
echo '<font color="blue">';
echo wpo_format_size($tablestatus->Data_free);
echo '</font>';
}
else {
echo '<font color="green">';
echo wpo_format_size($tablestatus->Data_free);
echo '</font>';
}
}
else {
if($tablestatus->Data_free>0){
echo '<font color="red">';
echo wpo_format_size($tablestatus->Data_free);
echo '</font>';
}
else {
echo '<font color="green">';
echo wpo_format_size($tablestatus->Data_free);
echo '</font>';
}
}
echo '</td>'."\n";
}
else {
echo '<td>';
echo '<font color="blue">';
echo '-';
echo '</font>';
echo '</td>'."\n";
}
$row_usage += $tablestatus->Rows;
$data_usage += $tablestatus->Data_length;
$index_usage += $tablestatus->Index_length;
if ($tablestatus->Engine != 'InnoDB'){
$overhead_usage += $tablestatus->Data_free;
$total_gain += $tablestatus->Data_free;
} else {
$overhead_usage += 0;
$total_gain += 0;
}
echo '</tr>'."\n";
}
echo '<tr class="thead">'."\n";
echo '<th>'.__('Total:', 'wp-optimize').'</th>'."\n";
echo '<th>'.sprintf(_n('%d Table', '%d Tables', $no, 'wp-optimize'), number_format_i18n($no)).'</th>'."\n";
echo '<th>'.sprintf(_n('%d Record', '%d Records', $row_usage, 'wp-optimize'), number_format_i18n($row_usage)).'</th>'."\n";
echo '<th>'.wpo_format_size($data_usage).'</th>'."\n";
echo '<th>'.wpo_format_size($index_usage).'</th>'."\n";
echo '<th>'.'-'.'</th>'."\n";
echo '<th>';
if (isset($_POST["optimize-db"])) {
if($overhead_usage>0){
echo '<font color="blue">';
echo wpo_format_size($overhead_usage);
echo '</font>';
}
else {
echo '<font color="green">';
echo wpo_format_size($overhead_usage);
echo '</font>';
}
}
else {
if($overhead_usage>0){
echo '<font color="red">';
echo wpo_format_size($overhead_usage);
echo '</font>';
}
else {
echo '<font color="green">';
echo wpo_format_size($overhead_usage);
echo '</font>';
}
}
echo '</th>'."\n";
echo '</tr>';
?>
</tbody>
</table>
<h3><?php _e('Total Size of Database:', 'wp-optimize'); ?></h3>
<h2><?php
list ($part1, $part2) = wpo_getCurrentDBSize();
echo $part1;
?></h2>
<?php if (isset($_POST["optimize-db"])) {
?>
<?php //$total_gain = round ($total_gain,3);?>
<h3><?php _e('Optimization Results:', 'wp-optimize'); ?></h3>
<p style="color: #0000FF;">
<?php
if ($total_gain > 0){
_e('Total Space Saved:', 'wp-optimize');
echo wpo_format_size($total_gain); wpo_updateTotalCleaned(strval($total_gain));
}
?></p>
<?php } else { ?>
<?php //$total_gain = round ($total_gain,3); ?>
<?php if(!$total_gain==0){ ?>
<h3><?php
if ($total_gain > 0){
_e('Optimization Possibility:', 'wp-optimize');
}
?></h3>
<p style="color: #FF0000;">
<?php if ($total_gain > 0){
_e('Total space that can be saved:', 'wp-optimize'); ?> <?php echo wpo_format_size($total_gain);
echo ' ';
}
?></p>
<?php } ?>
<?php
}
?>
<?php
} //end of optimize function
?>