| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dev4Press\Plugin\SweepPress\Basic; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Database { |
| 10 |
private $_system = array( |
| 11 |
'options', |
| 12 |
'blog_versions', |
| 13 |
'blogs', |
| 14 |
'site', |
| 15 |
'sitemeta', |
| 16 |
'users', |
| 17 |
'usermeta', |
| 18 |
); |
| 19 |
private $_wordpress = array( |
| 20 |
'blog' => array( |
| 21 |
'posts', |
| 22 |
'comments', |
| 23 |
'links', |
| 24 |
'options', |
| 25 |
'postmeta', |
| 26 |
'terms', |
| 27 |
'term_taxonomy', |
| 28 |
'term_relationships', |
| 29 |
'termmeta', |
| 30 |
'commentmeta', |
| 31 |
), |
| 32 |
'global' => array( |
| 33 |
'users', |
| 34 |
'usermeta', |
| 35 |
), |
| 36 |
'network' => array( |
| 37 |
'blogs', |
| 38 |
'blogmeta', |
| 39 |
'signups', |
| 40 |
'site', |
| 41 |
'sitemeta', |
| 42 |
'registration_log', |
| 43 |
), |
| 44 |
); |
| 45 |
private $_prefix; |
| 46 |
private $_base_prefix; |
| 47 |
private $_tables; |
| 48 |
private $_core; |
| 49 |
private $_optimize; |
| 50 |
private $_engines = array(); |
| 51 |
private $_collation = array(); |
| 52 |
private $_totals = array( |
| 53 |
'size' => 0, |
| 54 |
'free' => 0, |
| 55 |
'index' => 0, |
| 56 |
'rows' => 0, |
| 57 |
'total' => 0, |
| 58 |
'to_optimize' => 0, |
| 59 |
'to_repair' => 0, |
| 60 |
'tables' => 0, |
| 61 |
); |
| 62 |
private $_totals_wp = array( |
| 63 |
'size' => 0, |
| 64 |
'free' => 0, |
| 65 |
'index' => 0, |
| 66 |
'rows' => 0, |
| 67 |
'total' => 0, |
| 68 |
'to_optimize' => 0, |
| 69 |
'to_repair' => 0, |
| 70 |
'tables' => 0, |
| 71 |
'estimate' => 'tiny', |
| 72 |
); |
| 73 |
|
| 74 |
public function __construct() { |
| 75 |
$this->_prefix = sweeppress_prepare()->prefix(); |
| 76 |
$this->_base_prefix = sweeppress_prepare()->base_prefix(); |
| 77 |
|
| 78 |
$this->_optimize = array( |
| 79 |
'threshold' => sweeppress_settings()->get( 'db_table_optimize_threshold', 'sweepers' ), |
| 80 |
'min_size' => sweeppress_settings()->get( 'db_table_optimize_min_size', 'sweepers' ) * 1024 * 1024, |
| 81 |
); |
| 82 |
|
| 83 |
$this->_tables = Prepare::instance()->get_tables_status(); |
| 84 |
$this->_core = array( |
| 85 |
'blog' => $this->_prepare_wp_tables( 'blog' ), |
| 86 |
'global' => $this->_prepare_wp_tables( 'global' ), |
| 87 |
'network' => $this->_prepare_wp_tables( 'network' ), |
| 88 |
); |
| 89 |
|
| 90 |
foreach ( $this->_tables as $name => $table ) { |
| 91 |
$this->_process_table( $name, $table ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( $this->_totals_wp['size'] > 1048576 * 14400 ) { |
| 95 |
$this->_totals_wp['estimate'] = 'huge'; |
| 96 |
} else if ( $this->_totals_wp['size'] > 1048576 * 7200 ) { |
| 97 |
$this->_totals_wp['estimate'] = 'large'; |
| 98 |
} else if ( $this->_totals_wp['size'] > 1048576 * 2400 ) { |
| 99 |
$this->_totals_wp['estimate'] = 'big'; |
| 100 |
} else if ( $this->_totals_wp['size'] > 1048576 * 800 ) { |
| 101 |
$this->_totals_wp['estimate'] = 'medium'; |
| 102 |
} else if ( $this->_totals_wp['size'] > 1048576 * 200 ) { |
| 103 |
$this->_totals_wp['estimate'] = 'small'; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public static function instance() : Database { |
| 108 |
static $instance = null; |
| 109 |
|
| 110 |
if ( is_null( $instance ) ) { |
| 111 |
$instance = new Database(); |
| 112 |
} |
| 113 |
|
| 114 |
return $instance; |
| 115 |
} |
| 116 |
|
| 117 |
public function get_engines() : array { |
| 118 |
return $this->_engines; |
| 119 |
} |
| 120 |
|
| 121 |
public function get_collations() : array { |
| 122 |
return $this->_collation; |
| 123 |
} |
| 124 |
|
| 125 |
public function get_tables() : array { |
| 126 |
return $this->_tables; |
| 127 |
} |
| 128 |
|
| 129 |
public function total( string $name = '' ) { |
| 130 |
return $this->_totals[ $name ] ?? $this->_totals; |
| 131 |
} |
| 132 |
|
| 133 |
public function total_wp( string $name = '' ) { |
| 134 |
return $this->_totals_wp[ $name ] ?? $this->_totals_wp; |
| 135 |
} |
| 136 |
|
| 137 |
public function table( $name ) : array { |
| 138 |
$name = strtolower( $name ); |
| 139 |
|
| 140 |
return $this->_tables[ $name ] ?? $this->_empty_stats( $name ); |
| 141 |
} |
| 142 |
|
| 143 |
public function calculate( string $value, array $tables ) : int { |
| 144 |
$size = 0; |
| 145 |
|
| 146 |
if ( empty( $tables ) ) { |
| 147 |
foreach ( $this->_tables as $obj ) { |
| 148 |
$size += $obj[ $value ]; |
| 149 |
} |
| 150 |
} else { |
| 151 |
foreach ( $tables as $table ) { |
| 152 |
$name = Prepare::instance()->wpdb()->$table ?? ''; |
| 153 |
|
| 154 |
if ( ! empty( $name ) ) { |
| 155 |
$obj = $this->table( $name ); |
| 156 |
$size += $obj[ $value ]; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
return $size; |
| 162 |
} |
| 163 |
|
| 164 |
public function size() : ?string { |
| 165 |
switch ( $this->_totals_wp['estimate'] ) { |
| 166 |
default: |
| 167 |
case 'tiny': |
| 168 |
return __( 'Tiny', 'sweeppress' ); |
| 169 |
case 'small': |
| 170 |
return __( 'Small', 'sweeppress' ); |
| 171 |
case 'medium': |
| 172 |
return __( 'Medium', 'sweeppress' ); |
| 173 |
case 'big': |
| 174 |
return __( 'Big', 'sweeppress' ); |
| 175 |
case 'large': |
| 176 |
return __( 'Large', 'sweeppress' ); |
| 177 |
case 'huge': |
| 178 |
return __( 'Huge', 'sweeppress' ); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
private function _prepare_wp_tables( string $type ) : array { |
| 183 |
$list = $this->_wordpress[ $type ] ?? array(); |
| 184 |
$tables = array(); |
| 185 |
|
| 186 |
foreach ( $list as $table ) { |
| 187 |
$prefix = $type == 'blog' ? $this->_prefix : $this->_base_prefix; |
| 188 |
|
| 189 |
$tables[ $table ] = $prefix . $table; |
| 190 |
} |
| 191 |
|
| 192 |
return $tables; |
| 193 |
} |
| 194 |
|
| 195 |
private function _process_table( string $name, array $table ) { |
| 196 |
if ( ! in_array( $table['engine'], $this->_engines ) ) { |
| 197 |
$this->_engines[ $table['engine'] ] = $table['engine']; |
| 198 |
} |
| 199 |
|
| 200 |
if ( ! in_array( $table['collation'], $this->_collation ) ) { |
| 201 |
$this->_collation[ $table['collation'] ] = $table['collation']; |
| 202 |
} |
| 203 |
|
| 204 |
foreach ( $this->_core as $type => $tables ) { |
| 205 |
foreach ( $tables as $real => $tbl_name ) { |
| 206 |
if ( $name == $tbl_name ) { |
| 207 |
$this->_tables[ $name ]['wp_table'] = $real; |
| 208 |
$this->_tables[ $name ][ 'is_wp_' . $type ] = true; |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
$this->_tables[ $name ]['is_wp'] = $this->_tables[ $name ]['is_wp_blog'] || $this->_tables[ $name ]['is_wp_global'] || $this->_tables[ $name ]['is_wp_network']; |
| 215 |
$this->_tables[ $name ]['is_non_wp'] = ! $this->_tables[ $name ]['is_wp']; |
| 216 |
|
| 217 |
if ( ! empty( $this->_tables[ $name ]['wp_table'] ) ) { |
| 218 |
if ( in_array( $this->_tables[ $name ]['wp_table'], $this->_system ) ) { |
| 219 |
$this->_tables[ $name ]['is_core'] = true; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if ( $table['fragment'] >= $this->_optimize['threshold'] && $table['total'] > $this->_optimize['min_size'] ) { |
| 224 |
$this->_tables[ $name ]['for_optimization'] = true; |
| 225 |
$this->_totals['to_optimize'] ++; |
| 226 |
} |
| 227 |
|
| 228 |
if ( $table['is_corrupted'] ) { |
| 229 |
$this->_totals['to_repair'] ++; |
| 230 |
} |
| 231 |
|
| 232 |
$this->_totals['size'] += $table['size']; |
| 233 |
$this->_totals['free'] += $table['free']; |
| 234 |
$this->_totals['index'] += $table['index']; |
| 235 |
$this->_totals['rows'] += $table['rows']; |
| 236 |
$this->_totals['total'] += $table['total']; |
| 237 |
$this->_totals['tables'] ++; |
| 238 |
|
| 239 |
if ( $this->_tables[ $name ]['is_wp'] ) { |
| 240 |
$this->_totals_wp['size'] += $table['size']; |
| 241 |
$this->_totals_wp['free'] += $table['free']; |
| 242 |
$this->_totals_wp['index'] += $table['index']; |
| 243 |
$this->_totals_wp['rows'] += $table['rows']; |
| 244 |
$this->_totals_wp['total'] += $table['total']; |
| 245 |
$this->_totals_wp['tables'] ++; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
private function _empty_stats( string $name ) : array { |
| 250 |
return array( |
| 251 |
'table' => $name, |
| 252 |
'engine' => '', |
| 253 |
'total' => 0, |
| 254 |
'size' => 0, |
| 255 |
'free' => 0, |
| 256 |
'index' => 0, |
| 257 |
'rows' => 0, |
| 258 |
'average_row_size' => 0, |
| 259 |
'auto_increment' => 0, |
| 260 |
'fragment' => 0, |
| 261 |
'created' => '', |
| 262 |
'updated' => '', |
| 263 |
'collation' => '', |
| 264 |
'comment' => '', |
| 265 |
'is_corrupted' => '', |
| 266 |
'is_wp' => false, |
| 267 |
'is_wp_blog' => false, |
| 268 |
'is_wp_global' => false, |
| 269 |
'is_wp_network' => false, |
| 270 |
'is_core' => false, |
| 271 |
'is_non_wp' => false, |
| 272 |
'wp_table' => '', |
| 273 |
'detected_plugin' => '', |
| 274 |
'for_optimization' => false, |
| 275 |
); |
| 276 |
} |
| 277 |
} |
| 278 |
|