PluginProbe
Disk Usage Insights / 1.2
Disk Usage Insights v1.2
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 / Domain / FileEntryRepository.php

FileEntryRepository.php in Disk Usage Insights 1.2, at src/Domain/FileEntryRepository.php

139 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Mgleis\DiskUsageInsights\Domain;
4
5 class FileEntryRepository {
6
7 private \PDO $db;
8
9 public function __construct(\PDO $db) {
10 $this->db = $db;
11 $this->initializeDatabase();
12 }
13
14 private function initializeDatabase() {
15 $this->db->exec("
16 CREATE TABLE IF NOT EXISTS fileentries (
17 id INTEGER PRIMARY KEY AUTOINCREMENT,
18 parent_id INTEGER,
19 name TEXT NOT NULL,
20 type TEXT NOT NULL,
21 size INTEGER,
22 dir_size INTEGER,
23 dir_recursive_size INTEGER,
24 dir_count INTEGER,
25 dir_recursive_count INTEGER,
26 last_modified_date INTEGER,
27 is_wp_core_file INTEGER
28 )
29 ");
30 }
31
32 public function createOrUpdate(FileEntry &$fileEntry) {
33
34 if ($fileEntry->id == 0) {
35 $stmt = $this->db->prepare("
36 INSERT INTO fileentries
37 (parent_id, name, type, size, dir_size, dir_recursive_size, dir_count, dir_recursive_count, last_modified_date, is_wp_core_file)
38 VALUES (:parent_id, :name, :type, :size, :dir_size, :dir_recursive_size, :dir_count, :dir_recursive_count, :last_modified_date, :is_wp_core_file)
39 ");
40 } else {
41 $stmt = $this->db->prepare("
42 REPLACE INTO fileentries
43 (id, parent_id, name, type, size, dir_size, dir_recursive_size, dir_count, dir_recursive_count, last_modified_date, is_wp_core_file)
44 VALUES (:id, :parent_id, :name, :type, :size, :dir_size, :dir_recursive_size, :dir_count, :dir_recursive_count, :last_modified_date, :is_wp_core_file)
45 ");
46 $stmt->bindValue(':id', $fileEntry->id);
47 }
48 $stmt->bindValue(':parent_id', $fileEntry->parent_id);
49 $stmt->bindValue(':name', $fileEntry->name);
50 $stmt->bindValue(':type', $fileEntry->type);
51 $stmt->bindValue(':size', $fileEntry->size);
52 $stmt->bindValue(':dir_size', $fileEntry->dir_size);
53 $stmt->bindValue(':dir_recursive_size', $fileEntry->dir_recursive_size);
54 $stmt->bindValue(':dir_count', $fileEntry->dir_count);
55 $stmt->bindValue(':dir_recursive_count', $fileEntry->dir_recursive_count);
56 $stmt->bindValue(':last_modified_date', $fileEntry->last_modified_date);
57 $stmt->bindValue(':is_wp_core_file', $fileEntry->is_wp_core_file);
58 $stmt->execute();
59
60 if ($fileEntry->id == 0) {
61 $fileEntry->id = $this->db->lastInsertId();
62 }
63 }
64
65 public function findById(int $id): FileEntry {
66
67 $stmt = $this->db->prepare("
68 SELECT * FROM fileentries WHERE id =:id
69 ");
70 $stmt->bindValue(':id', $id);
71 $stmt->execute();
72 $result = $stmt->fetch(\PDO::FETCH_ASSOC);
73 if ($result === false) {
74 throw new \Exception(sprintf("No file entry with id %s found.", esc_html($id)));
75 }
76
77 $entry = new FileEntry();
78 foreach ($result as $key => $value) {
79 $entry->$key = $value;
80 }
81
82 return $entry;
83 }
84
85 private static $cache = [];
86 public function calcFullPath(FileEntry $fileEntry, string $root = ''): string {
87 $full = $fileEntry->name;
88 while ($fileEntry->parent_id != 0) {
89
90 $parent = self::$cache[$fileEntry->parent_id] ?? false;
91 if ($parent == false) {
92 $parent = $this->findById($fileEntry->parent_id);
93 self::$cache[$fileEntry->parent_id] = $parent;
94 }
95
96 if ($parent->name != '') {
97 $full = $parent->name . '/' . $full;
98 }
99 $fileEntry = $parent;
100 }
101 $full = $root . '/' . $full;
102 return $full;
103 }
104
105 public function count(string $type = ''): int {
106 $types = $type !== ''
107 ? [ $type]
108 : [ FileEntry::TYPE_DIR, FileEntry::TYPE_FILE ];
109 $types = "'" . implode("','", $types) . "'";
110 $stmt = $this->db->query("SELECT COUNT(*) FROM fileentries WHERE type in ($types)");
111
112 return (int) $stmt->fetchColumn();
113 }
114
115 public function get(int $skip, int $count, string $type = ''): array {
116 $types = $type !== ''
117 ? [ $type]
118 : [ FileEntry::TYPE_DIR, FileEntry::TYPE_FILE ];
119 $types = "'" . implode("','", $types) . "'";
120 $stmt = $this->db->prepare("
121 SELECT * FROM fileentries WHERE type in ($types) LIMIT $count OFFSET $skip
122 ");
123 $stmt->execute();
124 $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
125
126 $results = [];
127 foreach ($rows as $row) {
128 $entry = new FileEntry();
129 foreach ($row as $key => $value) {
130 $entry->$key = $value;
131 }
132 $results[] = $entry;
133 }
134
135 return $results;
136 }
137
138 }
139