PluginProbe
Disk Usage Insights / 1.7
Disk Usage Insights v1.7
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 / ShowResultsController.php

ShowResultsController.php in Disk Usage Insights 1.7, at src/Frontend/Controller/ShowResultsController.php

69 lines 3.0 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 ShowResultsController {
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 // TODO validate
26 $snapshot = sanitize_file_name(wp_unslash($_GET['snapshot'] ?? ''));
27 $root = $sn->root;
28 $totalSize = $this->selectInt("SELECT SUM(size) FROM fileentries;");
29
30 $WP_PLUGIN_URL = WpHelper::getPluginUrl();
31 $WP_NONCE = wp_create_nonce(Plugin::NONCE);
32 $WP_ADMIN_AJAX_URL = admin_url('admin-ajax.php');
33 $WP_SNAPSHOT_FILE = $snapshot;
34
35 // Bar Chart
36 $wpCoreSize = $this->selectInt("SELECT SUM(size) FROM fileentries WHERE is_wp_core_file = 1;");
37 $uploadsSize = $this->selectInt("SELECT dir_recursive_size FROM fileentries WHERE name = 'uploads';"); // TODO wrong
38 $themesSize = $this->selectInt("SELECT dir_recursive_size FROM fileentries WHERE name = 'themes';"); // TODO wrong
39 $pluginsSize = $this->selectInt("SELECT dir_recursive_size FROM fileentries WHERE name = 'plugins';"); // TODO wrong
40 $wpContentSize = $this->selectInt("SELECT dir_recursive_size FROM fileentries WHERE name = 'wp-content';") // TODO wrong
41 - $themesSize - $pluginsSize;
42 $barChart = [
43 ['label' => 'WP Core', 'percent' => round(100 * $wpCoreSize / $totalSize), 'gb' => round($wpCoreSize / 1_000_000, 1)],
44 ['label' => 'Uploads', 'percent' => round(100 * $uploadsSize / $totalSize), 'gb' => round($uploadsSize / 1_000_000, 1)],
45 ['label' => 'Themes', 'percent' => round(100 * $themesSize / $totalSize), 'gb' => round($themesSize / 1_000_000, 1)],
46 ['label' => 'Plugins', 'percent' => round(100 * $pluginsSize / $totalSize), 'gb' => round($pluginsSize / 1_000_000, 1)],
47 ['label' => 'wp-content', 'percent' => round(100 * $wpContentSize / $totalSize), 'gb' => round($wpContentSize / 1_000_000, 1)],
48 ];
49
50 include_once __DIR__ . '/../../../views/results.php';
51 }
52
53 private function selectInt(string $sql) {
54 $stmt = $this->database->q->db->prepare($sql);
55 $stmt->execute();
56 $result = $stmt->fetch();
57
58 return $result[0];
59 }
60
61 private function fetchAssoc(string $sql): array {
62 $stmt = $this->database->q->db->prepare($sql);
63 $stmt->execute();
64 $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
65
66 return $rows;
67 }
68 }
69