PluginProbe
Disk Usage Insights / 1.9
Disk Usage Insights v1.9
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 1.9, at src/Frontend/Controller/Report/ShowLargestFilesController.php

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