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
← All changes | src/Domain/FileEntryRepository.php +42 -4 1.2trunk View file →
@@ -1,13 +1,15 @@
1 1 <?php
2 2
3 3 namespace Mgleis\DiskUsageInsights\Domain;
4 4
5 +use PDO;
6 +
5 7 class FileEntryRepository {
6 8
7 - private \PDO $db;
9 + private PDO $db;
8 10
9 - public function __construct(\PDO $db) {
11 + public function __construct(PDO $db) {
10 12 $this->db = $db;
11 13 $this->initializeDatabase();
12 14 }
13 15
@@ -68,9 +70,9 @@
68 70 SELECT * FROM fileentries WHERE id =:id
69 71 ");
70 72 $stmt->bindValue(':id', $id);
71 73 $stmt->execute();
72 - $result = $stmt->fetch(\PDO::FETCH_ASSOC);
74 + $result = $stmt->fetch(PDO::FETCH_ASSOC);
73 75 if ($result === false) {
74 76 throw new \Exception(sprintf("No file entry with id %s found.", esc_html($id)));
75 77 }
76 78
@@ -81,8 +83,44 @@
81 83
82 84 return $entry;
83 85 }
84 86
87 + /**
88 + *
89 + * @param string $relativeFilename e.g. '/wp-content/plugins'
90 + * @throws \Exception
91 + * @return FileEntry|null
92 + */
93 + public function findByRelativeName(string $relativeFilename): ?FileEntry {
94 +
95 + $arr = explode('/', $relativeFilename);
96 + $parentId = 0; // root entry
97 + $result = null;
98 + foreach ($arr as $part) {
99 + $stmt = $this->db->prepare("
100 + SELECT * FROM fileentries WHERE parent_id = :parent_id AND name = :name;
101 + ");
102 + $stmt->bindValue(':parent_id', $parentId);
103 + $stmt->bindValue(':name', $part);
104 + $stmt->execute();
105 + $result = $stmt->fetch(PDO::FETCH_ASSOC);
106 + if ($result === false) {
107 + throw new \Exception(sprintf("Error finding %s", esc_html($relativeFilename)));
108 + }
109 + $parentId = $result['id'];
110 + }
111 +
112 + if ($result !== null) {
113 + $entry = new FileEntry();
114 + foreach ($result as $key => $value) {
115 + $entry->$key = $value;
116 + }
117 + return $entry;
118 + } else {
119 + return null;
120 + }
121 + }
122 +
85 123 private static $cache = [];
86 124 public function calcFullPath(FileEntry $fileEntry, string $root = ''): string {
87 125 $full = $fileEntry->name;
88 126 while ($fileEntry->parent_id != 0) {
@@ -120,9 +158,9 @@
120 158 $stmt = $this->db->prepare("
121 159 SELECT * FROM fileentries WHERE type in ($types) LIMIT $count OFFSET $skip
122 160 ");
123 161 $stmt->execute();
124 - $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
162 + $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
125 163
126 164 $results = [];
127 165 foreach ($rows as $row) {
128 166 $entry = new FileEntry();