PluginProbe
Disk Usage Insights / 1.3
Disk Usage Insights v1.3
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.3, at src/Frontend/Controller/ResultsController.php

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