| 1 |
<?php |
| 2 |
|
| 3 |
namespace Mgleis\DiskUsageInsights\Domain; |
| 4 |
|
| 5 |
use PDO; |
| 6 |
|
| 7 |
class FileEntryRepository { |
| 8 |
|
| 9 |
private PDO $db; |
| 10 |
|
| 11 |
public function __construct(PDO $db) { |
| 12 |
$this->db = $db; |
| 13 |
$this->initializeDatabase(); |
| 14 |
} |
| 15 |
|
| 16 |
private function initializeDatabase() { |
| 17 |
$this->db->exec(" |
| 18 |
CREATE TABLE IF NOT EXISTS fileentries ( |
| 19 |
id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 20 |
parent_id INTEGER, |
| 21 |
name TEXT NOT NULL, |
| 22 |
type TEXT NOT NULL, |
| 23 |
size INTEGER, |
| 24 |
dir_size INTEGER, |
| 25 |
dir_recursive_size INTEGER, |
| 26 |
dir_count INTEGER, |
| 27 |
dir_recursive_count INTEGER, |
| 28 |
last_modified_date INTEGER, |
| 29 |
is_wp_core_file INTEGER |
| 30 |
) |
| 31 |
"); |
| 32 |
} |
| 33 |
|
| 34 |
public function createOrUpdate(FileEntry &$fileEntry) { |
| 35 |
|
| 36 |
if ($fileEntry->id == 0) { |
| 37 |
$stmt = $this->db->prepare(" |
| 38 |
INSERT INTO fileentries |
| 39 |
(parent_id, name, type, size, dir_size, dir_recursive_size, dir_count, dir_recursive_count, last_modified_date, is_wp_core_file) |
| 40 |
VALUES (:parent_id, :name, :type, :size, :dir_size, :dir_recursive_size, :dir_count, :dir_recursive_count, :last_modified_date, :is_wp_core_file) |
| 41 |
"); |
| 42 |
} else { |
| 43 |
$stmt = $this->db->prepare(" |
| 44 |
REPLACE INTO fileentries |
| 45 |
(id, parent_id, name, type, size, dir_size, dir_recursive_size, dir_count, dir_recursive_count, last_modified_date, is_wp_core_file) |
| 46 |
VALUES (:id, :parent_id, :name, :type, :size, :dir_size, :dir_recursive_size, :dir_count, :dir_recursive_count, :last_modified_date, :is_wp_core_file) |
| 47 |
"); |
| 48 |
$stmt->bindValue(':id', $fileEntry->id); |
| 49 |
} |
| 50 |
$stmt->bindValue(':parent_id', $fileEntry->parent_id); |
| 51 |
$stmt->bindValue(':name', $fileEntry->name); |
| 52 |
$stmt->bindValue(':type', $fileEntry->type); |
| 53 |
$stmt->bindValue(':size', $fileEntry->size); |
| 54 |
$stmt->bindValue(':dir_size', $fileEntry->dir_size); |
| 55 |
$stmt->bindValue(':dir_recursive_size', $fileEntry->dir_recursive_size); |
| 56 |
$stmt->bindValue(':dir_count', $fileEntry->dir_count); |
| 57 |
$stmt->bindValue(':dir_recursive_count', $fileEntry->dir_recursive_count); |
| 58 |
$stmt->bindValue(':last_modified_date', $fileEntry->last_modified_date); |
| 59 |
$stmt->bindValue(':is_wp_core_file', $fileEntry->is_wp_core_file); |
| 60 |
$stmt->execute(); |
| 61 |
|
| 62 |
if ($fileEntry->id == 0) { |
| 63 |
$fileEntry->id = $this->db->lastInsertId(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public function findById(int $id): FileEntry { |
| 68 |
|
| 69 |
$stmt = $this->db->prepare(" |
| 70 |
SELECT * FROM fileentries WHERE id =:id |
| 71 |
"); |
| 72 |
$stmt->bindValue(':id', $id); |
| 73 |
$stmt->execute(); |
| 74 |
$result = $stmt->fetch(PDO::FETCH_ASSOC); |
| 75 |
if ($result === false) { |
| 76 |
throw new \Exception(sprintf("No file entry with id %s found.", esc_html($id))); |
| 77 |
} |
| 78 |
|
| 79 |
$entry = new FileEntry(); |
| 80 |
foreach ($result as $key => $value) { |
| 81 |
$entry->$key = $value; |
| 82 |
} |
| 83 |
|
| 84 |
return $entry; |
| 85 |
} |
| 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 |
|
| 123 |
private static $cache = []; |
| 124 |
public function calcFullPath(FileEntry $fileEntry, string $root = ''): string { |
| 125 |
$full = $fileEntry->name; |
| 126 |
while ($fileEntry->parent_id != 0) { |
| 127 |
|
| 128 |
$parent = self::$cache[$fileEntry->parent_id] ?? false; |
| 129 |
if ($parent == false) { |
| 130 |
$parent = $this->findById($fileEntry->parent_id); |
| 131 |
self::$cache[$fileEntry->parent_id] = $parent; |
| 132 |
} |
| 133 |
|
| 134 |
if ($parent->name != '') { |
| 135 |
$full = $parent->name . '/' . $full; |
| 136 |
} |
| 137 |
$fileEntry = $parent; |
| 138 |
} |
| 139 |
$full = $root . '/' . $full; |
| 140 |
return $full; |
| 141 |
} |
| 142 |
|
| 143 |
public function count(string $type = ''): int { |
| 144 |
$types = $type !== '' |
| 145 |
? [ $type] |
| 146 |
: [ FileEntry::TYPE_DIR, FileEntry::TYPE_FILE ]; |
| 147 |
$types = "'" . implode("','", $types) . "'"; |
| 148 |
$stmt = $this->db->query("SELECT COUNT(*) FROM fileentries WHERE type in ($types)"); |
| 149 |
|
| 150 |
return (int) $stmt->fetchColumn(); |
| 151 |
} |
| 152 |
|
| 153 |
public function get(int $skip, int $count, string $type = ''): array { |
| 154 |
$types = $type !== '' |
| 155 |
? [ $type] |
| 156 |
: [ FileEntry::TYPE_DIR, FileEntry::TYPE_FILE ]; |
| 157 |
$types = "'" . implode("','", $types) . "'"; |
| 158 |
$stmt = $this->db->prepare(" |
| 159 |
SELECT * FROM fileentries WHERE type in ($types) LIMIT $count OFFSET $skip |
| 160 |
"); |
| 161 |
$stmt->execute(); |
| 162 |
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 163 |
|
| 164 |
$results = []; |
| 165 |
foreach ($rows as $row) { |
| 166 |
$entry = new FileEntry(); |
| 167 |
foreach ($row as $key => $value) { |
| 168 |
$entry->$key = $value; |
| 169 |
} |
| 170 |
$results[] = $entry; |
| 171 |
} |
| 172 |
|
| 173 |
return $results; |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
|