This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
AutoIDDatabase.php
271 lines (227 loc) · 7.75 KB
/
AutoIDDatabase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
declare(strict_types=1);
namespace Bnomei;
use Kirby\Cache\FileCache;
use Kirby\Cms\Collection;
use Kirby\Cms\Field;
use Kirby\Database\Database;
use Kirby\Database\Db;
use Kirby\Toolkit\A;
use Kirby\Toolkit\F;
final class AutoIDDatabase
{
/** @var self */
private static $singleton;
/** @var array */
private $options;
/** @var Database */
private $database;
/**
* @var int
*/
private $count;
public function __construct(array $options = [])
{
$this->options = array_merge([
'template' => realpath(__DIR__ . '/../') . '/autoid-v2-6-0.sqlite',
'target' => self::cacheFolder() . '/autoid-v2-6-0.sqlite',
], $options);
$target = $this->options['target'];
if (! F::exists($target)) {
F::copy($this->options['template'], $target);
}
$this->database = new Database([
'type' => 'sqlite',
'database' => $target,
]);
}
public function databaseFile(): string
{
return $this->options['target'];
}
public function database(): Database
{
return $this->database;
}
public function count(): int
{
if (! is_null($this->count)) {
// fastest
return $this->count;
}
// faster
$this->count = intval($this->database->query('SELECT count(*) as count FROM AUTOID')->first()->count);
return $this->count;
// slow
// return count($this->database->query('SELECT rowid FROM AUTOID'));
}
public function find($autoid): ?AutoIDItem
{
if (is_a($autoid, Field::class)) {
$autoid = (string) $autoid->value();
}
foreach ($this->database->query("SELECT * FROM AUTOID WHERE autoid = '${autoid}'") as $obj) {
return new AutoIDItem($obj);
}
return null;
}
public function findByID($objectid): ?AutoIDItem
{
if (is_a($objectid, Field::class)) {
$objectid = (string) $objectid->value();
}
list($page, $filename, $structure) = $this->pageFilenameFromPath($objectid);
foreach ($this->database->query("SELECT * FROM AUTOID WHERE page = '${page}' AND filename = '${filename}' AND structure = '${structure}'") as $obj) {
return new AutoIDItem($obj);
}
return null;
}
public function findByDiruri(string $objectid): ?AutoIDItem
{
foreach ($this->database->query("SELECT * FROM AUTOID WHERE diruri = '${objectid}'") as $obj) {
return new AutoIDItem($obj);
}
return null;
}
public function findByTemplate(string $template, string $rootId = ''): Collection
{
$rootId = ltrim($rootId, '/');
if (strlen($rootId) > 0) {
$rootId = " AND page LIKE '${rootId}%' AND page != '${rootId}'";
}
$results = [];
$str = "SELECT * FROM AUTOID WHERE template = '${template}'" . $rootId;
foreach ($this->database->query($str) as $obj) {
$results[] = (new AutoIDItem($obj))->toObject();
}
return new Collection($results);
}
public function exists($autoid): bool
{
if (is_a($autoid, Field::class)) {
$autoid = (string) $autoid->value();
}
return $this->find($autoid) !== null;
}
public function modified($autoid): ?int
{
if (is_array($autoid)) {
return $this->modifiedByArray($autoid);
}
if (is_a($autoid, Field::class)) {
$autoid = (string) $autoid->value();
}
$find = $this->find($autoid);
return $find ? $find->modified() : null;
}
public function modifiedByArray(array $autoids): ?int
{
$modified = null;
$list = implode(', ', array_map(static function ($autoid) {
return "'${autoid}'";
}, $autoids));
foreach ($this->database->query("SELECT MAX(modified) as maxmod FROM AUTOID WHERE autoid IN (${list})") as $obj) {
$modified = intval($obj->maxmod);
}
return $modified === 0 ? null : $modified;
}
public function insertOrUpdate(AutoIDItem $item): void
{
// remove all entries for item in db
$current = $this->find($item->autoid());
if ($current && $current->id() !== $item->id()) {
$this->deleteByID($current->id());
}
// remove all with same page AND file props (even if empty)
$this->deleteByID($item->id());
// remove with same autoid
$this->delete($item->autoid());
// enter a new single entry
$this->database->query("
INSERT INTO AUTOID
(autoid, modified, page, filename, structure, kind, template, diruri)
VALUES
('{$item->autoid}', {$item->modified}, '{$item->page}', '{$item->filename}', '{$item->structure}', '{$item->kind}', '{$item->template}', '{$item->diruri}')
");
$this->count = null;
}
public function delete($autoid): void
{
if (is_a($autoid, AutoIDItem::class)) {
$autoid = (string) $autoid->autoid();
} elseif (is_a($autoid, Field::class)) {
$autoid = (string) $autoid->value();
}
if (! is_string($autoid)) {
return;
}
if (strlen(trim($autoid)) === 0) {
return;
}
$this->database->query("DELETE FROM AUTOID WHERE autoid = '${autoid}'");
$this->count = null;
}
public function deleteByDiruri($objectid): void
{
if (is_a($objectid, Field::class)) {
$objectid = (string) $objectid->value();
}
$str = "DELETE FROM AUTOID WHERE diruri LIKE '${objectid}%'";
$this->database->query($str);
$this->count = null;
}
public function deleteByID($objectid): void
{
if (is_a($objectid, Field::class)) {
$objectid = (string) $objectid->value();
}
list($page, $filename, $structure) = $this->pageFilenameFromPath($objectid);
if (strlen($structure) > 0) {
// remove structure by autoid since path to object is not unique
$this->delete($structure);
} else {
$str = "DELETE FROM AUTOID WHERE page = '${page}' AND filename = '${filename}'";
$this->database->query($str);
$this->count = null;
}
}
public function flush(): void
{
$this->database->query("DELETE FROM AUTOID WHERE autoid != ''");
$this->count = null;
}
private function pageFilenameFromPath(string $objectid): array
{
$page = '';
$filename = '';
$structure = '';
if (pathinfo($objectid, PATHINFO_EXTENSION)) {
$pathinfo = pathinfo($objectid);
$page = $pathinfo['dirname'];
$filename = $pathinfo['basename'];
} else {
$pathinfo = pathinfo($objectid);
$page = $pathinfo['dirname'] === '.' ? $pathinfo['basename'] : $pathinfo['dirname'] . '/' . $pathinfo['basename'];
$structure = strpos($page, '#') !== false ? explode('#', $page)[1] : '';
}
return [$page, $filename, $structure];
}
public static function singleton(array $options = []): self
{
if (self::$singleton) {
return self::$singleton;
}
self::$singleton = new self($options);
return self::$singleton;
}
public static function cacheFolder(): string
{
$cache = kirby()->cache('bnomei.autoid');
if (is_a($cache, FileCache::class)) {
return A::get($cache->options(), 'root') . '/' . A::get($cache->options(), 'prefix');
}
// @codeCoverageIgnoreStart
return kirby()->roots()->cache();
// @codeCoverageIgnoreEnd
}
}