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 / ShowLargestFilesController.php

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

81 lines 2.6 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\Frontend\Pagination;
6 use Mgleis\DiskUsageInsights\Frontend\Table;
7 use Mgleis\DiskUsageInsights\Plugin;
8 use PDO;
9
10
11 class ShowLargestFilesController {
12
13 public function execute(Database $database) {
14 check_ajax_referer(Plugin::NONCE);
15
16 $WP_ADMIN_AJAX_URL = admin_url('admin-ajax.php');
17 $WP_SNAPSHOT_FILE = sanitize_file_name(wp_unslash($_GET['snapshot'] ?? ''));
18
19 $totalSize = $this->selectInt($database, "SELECT SUM(size) FROM fileentries;");
20
21 if (isset($_GET['p'])) {
22
23 $pagination = Pagination::parseFromString(esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'] ?? '')));
24
25 } else {
26
27 $url = sprintf('%s?action=dui_results_table&table=%s&snapshot=%s',
28 esc_url($WP_ADMIN_AJAX_URL),
29 'largest-files',
30 esc_js($WP_SNAPSHOT_FILE)
31 );
32
33 $totalFileCount = $this->selectInt($database, "SELECT COUNT(*) FROM fileentries WHERE type in ('file')");
34
35 $pagination = new Pagination($url, $totalFileCount, 0, 10);
36 }
37
38 //
39 // Largest Files
40 //
41 $rows = $this->fetchAssoc($database, sprintf("SELECT * FROM fileentries WHERE type in ('file') ORDER BY size DESC LIMIT %s OFFSET %s", $pagination->getItemsPerPage(), $pagination->calcOffset()));
42 $table = [];
43 foreach ($rows as $row) {
44 $fileEntry = $database->fileEntryRepository->findById($row['id']);
45 $relativeName = $database->fileEntryRepository->calcFullPath($fileEntry);
46 $table[] = [
47 $relativeName,
48 number_format_i18n($row['size']),
49 number_format_i18n(100 * $row['size'] / $totalSize, 2) . '%'
50 ];
51 }
52
53 (new Table(
54 'Largest Files',
55 ['Filename', 'Size', '%'],
56 ['DUI-table__col--grow', 'DUI-table__col--number', 'DUI-table__col--number']
57 ))
58 ->withPercentBar(2, 2)
59 ->withData($table)
60 ->withPagination($pagination)
61 ->output();
62
63 }
64
65 private function fetchAssoc(Database $database, string $sql): array {
66 $stmt = $database->q->db->prepare($sql);
67 $stmt->execute();
68 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
69
70 return $rows;
71 }
72
73 private function selectInt(Database $database, string $sql) {
74 $stmt = $database->q->db->prepare($sql);
75 $stmt->execute();
76 $result = $stmt->fetch();
77
78 return $result[0];
79 }
80
81 }