PluginProbe
Disk Usage Insights / 1.10
Disk Usage Insights v1.10
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.10, at src/Domain/FileEntryRepository.php

175 lines 5.9 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 /**
86 *
87 * @param string $relativeFilename e.g. '/wp-content/plugins'
88 * @throws \Exception
89 * @return FileEntry|null
90 */
91 public function findByRelativeName(string $relativeFilename): ?FileEntry {
92
93 $arr = explode('/', $relativeFilename);
94 $parentId = 0; // root entry
95 $result = null;
96 foreach ($arr as $part) {
97 $stmt = $this->db->prepare("
98 SELECT * FROM fileentries WHERE parent_id = :parent_id AND name = :name;
99 ");
100 $stmt->bindValue(':parent_id', $parentId);
101 $stmt->bindValue(':name', $part);
102 $stmt->execute();
103 $result = $stmt->fetch(\PDO::FETCH_ASSOC);
104 if ($result === false) {
105 throw new \Exception(sprintf("Error finding %s", esc_html($relativeFilename)));
106 }
107 $parentId = $result['id'];
108 }
109
110 if ($result !== null) {
111 $entry = new FileEntry();
112 foreach ($result as $key => $value) {
113 $entry->$key = $value;
114 }
115 return $entry;
116 } else {
117 return null;
118 }
119 }
120
121 private static $cache = [];
122 public function calcFullPath(FileEntry $fileEntry, string $root = ''): string {
123 $full = $fileEntry->name;
124 while ($fileEntry->parent_id != 0) {
125
126 $parent = self::$cache[$fileEntry->parent_id] ?? false;
127 if ($parent == false) {
128 $parent = $this->findById($fileEntry->parent_id);
129 self::$cache[$fileEntry->parent_id] = $parent;
130 }
131
132 if ($parent->name != '') {
133 $full = $parent->name . '/' . $full;
134 }
135 $fileEntry = $parent;
136 }
137 $full = $root . '/' . $full;
138 return $full;
139 }
140
141 public function count(string $type = ''): int {
142 $types = $type !== ''
143 ? [ $type]
144 : [ FileEntry::TYPE_DIR, FileEntry::TYPE_FILE ];
145 $types = "'" . implode("','", $types) . "'";
146 $stmt = $this->db->query("SELECT COUNT(*) FROM fileentries WHERE type in ($types)");
147
148 return (int) $stmt->fetchColumn();
149 }
150
151 public function get(int $skip, int $count, string $type = ''): array {
152 $types = $type !== ''
153 ? [ $type]
154 : [ FileEntry::TYPE_DIR, FileEntry::TYPE_FILE ];
155 $types = "'" . implode("','", $types) . "'";
156 $stmt = $this->db->prepare("
157 SELECT * FROM fileentries WHERE type in ($types) LIMIT $count OFFSET $skip
158 ");
159 $stmt->execute();
160 $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
161
162 $results = [];
163 foreach ($rows as $row) {
164 $entry = new FileEntry();
165 foreach ($row as $key => $value) {
166 $entry->$key = $value;
167 }
168 $results[] = $entry;
169 }
170
171 return $results;
172 }
173
174 }
175