| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dev4Press\Plugin\SweepPress\Basic; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Database { |
| 10 |
private $_tables; |
| 11 |
private $_totals = array( |
| 12 |
'size' => 0, |
| 13 |
'free' => 0, |
| 14 |
'index' => 0, |
| 15 |
'rows' => 0 |
| 16 |
); |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
$this->_tables = DB::instance()->get_tables_status(); |
| 20 |
|
| 21 |
foreach ( $this->_tables as $table ) { |
| 22 |
$this->_totals['size'] += $table['size']; |
| 23 |
$this->_totals['free'] += $table['free']; |
| 24 |
$this->_totals['index'] += $table['index']; |
| 25 |
$this->_totals['rows'] += $table['rows']; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public static function instance() : Database { |
| 30 |
static $instance = null; |
| 31 |
|
| 32 |
if ( is_null( $instance ) ) { |
| 33 |
$instance = new Database(); |
| 34 |
} |
| 35 |
|
| 36 |
return $instance; |
| 37 |
} |
| 38 |
|
| 39 |
public function total( string $name = '' ) { |
| 40 |
return $this->_totals[ $name ] ?? $this->_totals; |
| 41 |
} |
| 42 |
|
| 43 |
public function table( $name ) : array { |
| 44 |
$name = strtolower( $name ); |
| 45 |
|
| 46 |
return $this->_tables[ $name ] ?? $this->_empty_stats( $name ); |
| 47 |
} |
| 48 |
|
| 49 |
public function calculate( string $value, array $tables ) : int { |
| 50 |
$size = 0; |
| 51 |
|
| 52 |
if ( empty( $tables ) ) { |
| 53 |
foreach ( $this->_tables as $obj ) { |
| 54 |
$size += $obj[ $value ]; |
| 55 |
} |
| 56 |
} else { |
| 57 |
foreach ( $tables as $table ) { |
| 58 |
$name = DB::instance()->wpdb()->$table ?? ''; |
| 59 |
|
| 60 |
if ( ! empty( $name ) ) { |
| 61 |
$obj = $this->table( $name ); |
| 62 |
$size += $obj[ $value ]; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return $size; |
| 68 |
} |
| 69 |
|
| 70 |
private function _empty_stats( $name ) : array { |
| 71 |
return array( |
| 72 |
'table' => $name, |
| 73 |
'engine' => '', |
| 74 |
'size' => 0, |
| 75 |
'free' => 0, |
| 76 |
'index' => 0, |
| 77 |
'rows' => 0, |
| 78 |
'average_row_size' => 0, |
| 79 |
'auto_increment' => 0, |
| 80 |
'created' => '', |
| 81 |
'updated' => '', |
| 82 |
'fragment' => 0 |
| 83 |
); |
| 84 |
} |
| 85 |
} |
| 86 |
|