PluginProbe
Disk Usage Insights / trunk
Disk Usage Insights vtrunk
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 / Report / ShowBrowserController.php

ShowBrowserController.php in Disk Usage Insights trunk, at src/Frontend/Controller/Report/ShowBrowserController.php

135 lines 4.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\Report;
3
4 use Mgleis\DiskUsageInsights\Domain\Database;
5 use Mgleis\DiskUsageInsights\Domain\FileEntry;
6 use Mgleis\DiskUsageInsights\Frontend\Pagination;
7 use Mgleis\DiskUsageInsights\Frontend\Table;
8 use Mgleis\DiskUsageInsights\Plugin;
9 use PDO;
10
11 class ShowBrowserController {
12
13 private PDO $db;
14
15 public function execute(Database $database) {
16 check_ajax_referer(Plugin::NONCE);
17
18 $this->db = $database->q->db;
19
20 $WP_ADMIN_AJAX_URL = admin_url('admin-ajax.php');
21 $WP_SNAPSHOT_FILE = sanitize_file_name(wp_unslash($_GET['snapshot'] ?? ''));
22
23 $totalSize = $this->selectInt($database, "SELECT SUM(size) FROM fileentries;");
24
25 $parent_id = intval(wp_unslash($_GET['parent_id'] ?? 1));
26
27 $parentDir = $this->findById($parent_id);
28 $breadCrumbs = $this->calcDirBreadcrumbs($parentDir);
29 $retval = [];
30 foreach ($breadCrumbs as $bc) {
31 $retval[] = [
32 'name' => $bc->name,
33 'link' => sprintf(
34 '%s?action=dui_results_table&table=browser&snapshot=%s&parent_id=%s',
35 esc_url($WP_ADMIN_AJAX_URL),
36 esc_js($WP_SNAPSHOT_FILE),
37 esc_js($bc->id)
38 )
39 ];
40 }
41 $breadCrumbs = $retval;
42
43 $pagination = new Pagination('', 0, 0, 10);
44
45 $rows = $this->fetchAssoc($database, sprintf(
46 "SELECT *, CASE WHEN type = 'dir' THEN dir_recursive_size ELSE size END AS ordersize FROM fileentries WHERE parent_id = %s ORDER BY ordersize DESC LIMIT %s OFFSET %s",
47 $parent_id, $pagination->getItemsPerPage(), $pagination->calcOffset())
48 );
49 $items = [];
50
51 foreach ($rows as $row) {
52 $items[] = [
53 'id' => $row['id'],
54 'name' => $row['name'],
55 'size' => $row['type'] == 'dir' ? $row['dir_recursive_size'] : $row['size'],
56 'type' => $row['type'],
57 'link' => $row['type'] == 'dir'
58 ? sprintf(
59 '%s?action=dui_results_table&table=browser&snapshot=%s&parent_id=%s',
60 esc_url($WP_ADMIN_AJAX_URL),
61 esc_js($WP_SNAPSHOT_FILE),
62 esc_js($row['id'])
63 )
64 : ''
65 ];
66 }
67 $totalSize = array_reduce($items, fn($carry, $item) => $carry + $item['size'], 0);
68 for ($i = 0; $i < count($items); $i++) {
69 $items[$i]['percent'] = $totalSize > 0 ? round(100 * $items[$i]['size'] / $totalSize, 2) : 0;
70 }
71 $last = 0;
72 for ($i = 0; $i < count($items); $i++) {
73 $current = $last + $items[$i]['percent'];
74 $items[$i]['conic'] = "{$last}% {$current}%";
75 $last = $current;
76 }
77
78 include_once __DIR__ . '/../../../../views/results/browser.php';
79
80 wp_die(); // All ajax handlers should die when finished
81
82 }
83
84 private function fetchAssoc(Database $database, string $sql): array {
85 $stmt = $database->q->db->prepare($sql);
86 $stmt->execute();
87 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
88
89 return $rows;
90 }
91
92 private function selectInt(Database $database, string $sql) {
93 $stmt = $database->q->db->prepare($sql);
94 $stmt->execute();
95 $result = $stmt->fetch();
96
97 return $result[0];
98 }
99
100 private static $cache = [];
101 public function calcDirBreadcrumbs(FileEntry $fileEntry): array {
102 $retval = [];
103
104 while ($fileEntry->parent_id != 0) {
105 $retval[] = $fileEntry;
106 $fileEntry = $this->findById($fileEntry->parent_id);
107 }
108 $fileEntry->name = 'ROOT';
109 $retval[] = $fileEntry;
110
111 return array_reverse($retval);
112 }
113
114 public function findById(int $id): FileEntry {
115
116 $stmt = $this->db->prepare("
117 SELECT * FROM fileentries WHERE id =:id
118 ");
119 $stmt->bindValue(':id', $id);
120 $stmt->execute();
121 $result = $stmt->fetch(PDO::FETCH_ASSOC);
122 if ($result === false) {
123 throw new \Exception(sprintf("No file entry with id %s found.", esc_html($id)));
124 }
125
126 $entry = new FileEntry();
127 foreach ($result as $key => $value) {
128 $entry->$key = $value;
129 }
130
131 return $entry;
132 }
133
134
135 }