| 1 |
<?php |
| 2 |
namespace Mgleis\DiskUsageInsights\Frontend\Controller; |
| 3 |
|
| 4 |
use Mgleis\DiskUsageInsights\Domain\Database; |
| 5 |
use Mgleis\DiskUsageInsights\Domain\DatabaseRepository; |
| 6 |
use Mgleis\DiskUsageInsights\Frontend\Table; |
| 7 |
use Mgleis\DiskUsageInsights\Plugin; |
| 8 |
use Mgleis\DiskUsageInsights\WpHelper; |
| 9 |
|
| 10 |
class ResultsController { |
| 11 |
|
| 12 |
private Database $database; |
| 13 |
|
| 14 |
public function execute() { |
| 15 |
// TODO Validate |
| 16 |
$snapshotName = sanitize_file_name(wp_unslash($_GET['snapshot'] ?? '')); |
| 17 |
|
| 18 |
$this->database = (new DatabaseRepository())->loadDatabase($snapshotName); |
| 19 |
$sn = $this->database->snapshotRepository->load(); |
| 20 |
if ($sn->collectPhaseFinished !== 1) { |
| 21 |
echo "Cannot read database because it is corrupt/incomplete. Please create a new snapshot."; |
| 22 |
exit; |
| 23 |
} |
| 24 |
|
| 25 |
$WP_PLUGIN_URL = WpHelper::getPluginUrl(); |
| 26 |
$WP_NONCE = wp_create_nonce(Plugin::NONCE); |
| 27 |
$WP_ADMIN_AJAX_URL = admin_url('admin-ajax.php'); |
| 28 |
// TODO validate |
| 29 |
$snapshot = sanitize_file_name(wp_unslash($_GET['snapshot'] ?? '')); |
| 30 |
$root = $this->database->snapshotRepository->load()->root; |
| 31 |
$totalSize = $this->selectInt("SELECT SUM(size) FROM fileentries;"); |
| 32 |
|
| 33 |
include_once __DIR__ . '/../../../views/results.php'; |
| 34 |
|
| 35 |
// |
| 36 |
// Largest Files |
| 37 |
// |
| 38 |
$rows = $this->fetchAssoc("SELECT * FROM fileentries WHERE type in ('file') ORDER BY size DESC LIMIT 10"); |
| 39 |
$table = []; |
| 40 |
foreach ($rows as $row) { |
| 41 |
$fileEntry = $this->database->fileEntryRepository->findById($row['id']); |
| 42 |
$relativeName = $this->database->fileEntryRepository->calcFullPath($fileEntry); |
| 43 |
$table[] = [ |
| 44 |
$relativeName, |
| 45 |
number_format_i18n($row['size']), |
| 46 |
number_format_i18n(100 * $row['size'] / $totalSize, 2) . '%' |
| 47 |
]; |
| 48 |
} |
| 49 |
(new Table( |
| 50 |
'Largest Files', |
| 51 |
['Filename', 'Size', '%'], |
| 52 |
['', 'DUI-table__col--number', 'DUI-table__col--number'] |
| 53 |
))->withPercentBar(2, 2)->withData($table)->output(); |
| 54 |
|
| 55 |
// |
| 56 |
// Largest Folders (files only) |
| 57 |
// |
| 58 |
$rows = $this->fetchAssoc("SELECT * FROM fileentries WHERE type in ('dir') ORDER BY dir_size DESC LIMIT 10"); |
| 59 |
$table = []; |
| 60 |
foreach ($rows as $row) { |
| 61 |
$fileEntry = $this->database->fileEntryRepository->findById($row['id']); |
| 62 |
$relativeName = $this->database->fileEntryRepository->calcFullPath($fileEntry); |
| 63 |
$table[] = [ |
| 64 |
$relativeName, |
| 65 |
number_format_i18n($row['dir_size']), |
| 66 |
number_format_i18n($row['dir_count']), |
| 67 |
number_format_i18n(round($row['dir_size'] / $row['dir_count'])), |
| 68 |
number_format_i18n(100 * $row['dir_size'] / $totalSize, 2) . '%' |
| 69 |
]; |
| 70 |
} |
| 71 |
(new Table( |
| 72 |
'Largest Folders (files only)', |
| 73 |
['Folder', 'File Sizes', 'File Count', 'Avg File Size', '%'], |
| 74 |
['', 'DUI-table__col--number', 'DUI-table__col--number', 'DUI-table__col--number', 'DUI-table__col--number'] |
| 75 |
))->withPercentBar(4, 4)->withData($table)->output(); |
| 76 |
|
| 77 |
// |
| 78 |
// Largest Folders (incl. sub folders) |
| 79 |
// |
| 80 |
$rows = $this->fetchAssoc("SELECT * FROM fileentries WHERE type in ('dir') ORDER BY dir_recursive_size DESC LIMIT 10"); |
| 81 |
$table = []; |
| 82 |
foreach ($rows as $row) { |
| 83 |
$fileEntry = $this->database->fileEntryRepository->findById($row['id']); |
| 84 |
$relativeName = $this->database->fileEntryRepository->calcFullPath($fileEntry); |
| 85 |
$table[] = [$relativeName, number_format_i18n($row['dir_recursive_size'])]; |
| 86 |
} |
| 87 |
(new Table( |
| 88 |
'Largest Folders (incl. sub folders)', |
| 89 |
['Folder', 'Total Size'], |
| 90 |
['', 'DUI-table__col--number'] |
| 91 |
))->withData($table)->output(); |
| 92 |
|
| 93 |
// |
| 94 |
// Folders with most files |
| 95 |
// |
| 96 |
$rows = $this->fetchAssoc("SELECT * FROM fileentries WHERE type in ('dir') ORDER BY dir_count DESC LIMIT 10"); |
| 97 |
$table = []; |
| 98 |
foreach ($rows as $row) { |
| 99 |
$fileEntry = $this->database->fileEntryRepository->findById($row['id']); |
| 100 |
$relativeName = $this->database->fileEntryRepository->calcFullPath($fileEntry); |
| 101 |
$table[] = [ |
| 102 |
$relativeName, |
| 103 |
number_format_i18n($row['dir_count']), |
| 104 |
number_format_i18n($row['dir_size']), |
| 105 |
number_format_i18n(round($row['dir_size'] / $row['dir_count'])) |
| 106 |
]; |
| 107 |
} |
| 108 |
(new Table( |
| 109 |
'Folders with most files', |
| 110 |
['Folder', 'File Count', 'File Size', 'Avg File Size'], |
| 111 |
['', 'DUI-table__col--number', 'DUI-table__col--number', 'DUI-table__col--number'] |
| 112 |
))->withData($table)->output(); |
| 113 |
|
| 114 |
|
| 115 |
// |
| 116 |
// Largest Directories within /wp-content/plugins |
| 117 |
// |
| 118 |
$pluginDir = substr(WP_PLUGIN_DIR, strlen(rtrim(ABSPATH, '/'))); // e.g. '/wp-content/plugins' TODO store in snapshot |
| 119 |
$fileEntry = $this->database->fileEntryRepository->findByRelativeName($pluginDir); |
| 120 |
if ($fileEntry !== null) { |
| 121 |
|
| 122 |
$rows = $this->fetchAssoc(sprintf("SELECT * FROM fileentries WHERE parent_id = %s AND type = 'dir' ORDER BY dir_recursive_size DESC LIMIT 10", $fileEntry->id)); |
| 123 |
$table = []; |
| 124 |
foreach ($rows as $row) { |
| 125 |
$fileEntry = $this->database->fileEntryRepository->findById($row['id']); |
| 126 |
$relativeName = $row['name']; |
| 127 |
$table[] = [ |
| 128 |
$relativeName, |
| 129 |
number_format_i18n($row['dir_recursive_size']) |
| 130 |
]; |
| 131 |
} |
| 132 |
(new Table( |
| 133 |
'Largest Plugin Folders', |
| 134 |
['Folder', 'Total Size'], |
| 135 |
['', 'DUI-table__col--number'] |
| 136 |
))->withData($table)->output(); |
| 137 |
|
| 138 |
} else { |
| 139 |
echo "plugin dir not found"; |
| 140 |
} |
| 141 |
|
| 142 |
// |
| 143 |
// Largest Directories within /wp-content/themes |
| 144 |
// |
| 145 |
$themeDir = substr(get_theme_root(), strlen(rtrim(ABSPATH, '/'))); // e.g. '/wp-content/themes' TODO store in snapshot |
| 146 |
$fileEntry = $this->database->fileEntryRepository->findByRelativeName($themeDir); |
| 147 |
if ($fileEntry !== null) { |
| 148 |
|
| 149 |
$rows = $this->fetchAssoc(sprintf("SELECT * FROM fileentries WHERE parent_id = %s AND type = 'dir' ORDER BY dir_recursive_size DESC LIMIT 10", $fileEntry->id)); |
| 150 |
$table = []; |
| 151 |
foreach ($rows as $row) { |
| 152 |
$fileEntry = $this->database->fileEntryRepository->findById($row['id']); |
| 153 |
$relativeName = $row['name']; |
| 154 |
$table[] = [ |
| 155 |
$relativeName, |
| 156 |
number_format_i18n($row['dir_recursive_size']) |
| 157 |
]; |
| 158 |
} |
| 159 |
(new Table( |
| 160 |
'Largest Theme Folders', |
| 161 |
['Folder', 'Total Size'], |
| 162 |
['', 'DUI-table__col--number'] |
| 163 |
))->withData($table)->output(); |
| 164 |
|
| 165 |
} else { |
| 166 |
echo "theme dir not found"; |
| 167 |
} |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
private function selectInt(string $sql) { |
| 172 |
$stmt = $this->database->q->db->prepare($sql); |
| 173 |
$stmt->execute(); |
| 174 |
$result = $stmt->fetch(); |
| 175 |
|
| 176 |
return $result[0]; |
| 177 |
} |
| 178 |
|
| 179 |
private function fetchAssoc(string $sql): array { |
| 180 |
$stmt = $this->database->q->db->prepare($sql); |
| 181 |
$stmt->execute(); |
| 182 |
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
| 183 |
|
| 184 |
return $rows; |
| 185 |
} |
| 186 |
} |
| 187 |
|