PluginProbe
Disk Usage Insights / 1.2
Disk Usage Insights v1.2
trunk 1.0 1.10 1.11 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
disk-usage-insights / src / Frontend / Controller / ResultsController.php

ResultsController.php in Disk Usage Insights 1.2, at src/Frontend/Controller/ResultsController.php

143 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_count 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 // TODO
119
120 //
121 // Largest Directories within /wp-content/themes
122 //
123 // TODO
124
125 }
126
127 private function selectInt(string $sql) {
128 $stmt = $this->database->q->db->prepare($sql);
129 $stmt->execute();
130 $result = $stmt->fetch();
131
132 return $result[0];
133 }
134
135 private function fetchAssoc(string $sql): array {
136 $stmt = $this->database->q->db->prepare($sql);
137 $stmt->execute();
138 $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
139
140 return $rows;
141 }
142 }
143