Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Commit

Permalink
✨ performance improvements regarding modified helper
Browse files Browse the repository at this point in the history
- site object can have autoid (and modified check)
- modified check will not read files unless not index
- index will stop after 20sec to avoid error page. use caps AUTOID pagemethod in frontend code to counter missing index entries.

Signed-off-by: Bruno Meilick <[email protected]>
  • Loading branch information
bnomei committed Mar 30, 2020
1 parent 52e52ce commit 4dd74d7
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 132 deletions.
25 changes: 24 additions & 1 deletion classes/AutoID.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Kirby\Cms\File;
use Kirby\Cms\FileVersion;
use Kirby\Cms\Page;
use Kirby\Cms\Site;
use Kirby\Toolkit\Iterator;

final class AutoID
Expand Down Expand Up @@ -35,7 +36,8 @@ public static function index(bool $force = false): int
{
$indexed = 0;
if (AutoIDDatabase::singleton()->count() === 0 || $force) {
set_time_limit(0);
$break = time() + 20;

// site
if (self::push(site())) {
$indexed++;
Expand All @@ -45,6 +47,9 @@ public static function index(bool $force = false): int
if (self::push($page)) {
$indexed++;
}
if (!$force && time() >= $break) {
break;
}
}
}
return $indexed;
Expand Down Expand Up @@ -129,13 +134,31 @@ public static function modified($autoid)
return AutoIDDatabase::singleton()->modifiedByArray($autoid);
}

if (is_a($autoid, Site::class)) {
$item = AutoIDDatabase::singleton()->findByID('$');
if ($item) {
return $item->modified();
}
return $autoid->modified();
}

if (is_a($autoid, Page::class) ||
is_a($autoid, File::class) ||
is_a($autoid, FileVersion::class)) {

// try finding without reading the file
$item = AutoIDDatabase::singleton()->findByID($autoid->id());
if ($item) {
return $item->modified();
}
// if fails do not index the object but just check
// the file timestamp since that is the fastest thing to do
/*
if ($autoid->{AutoID::FIELDNAME}()->isNotEmpty()) {
// make sure it exists using AUTOID (in caps)
return self::modified($autoid->AUTOID());
}
*/
return $autoid->modified();
}

Expand Down
37 changes: 26 additions & 11 deletions classes/AutoIDProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,28 @@ private function indexPageOrFile(): void
private function indexStructures(): void
{
$data = [];

foreach ($this->object->blueprint()->fields() as $field) {
if (A::get($field, 'type') !== 'structure') {
continue;
}
$fieldname = A::get($field, 'name');
if (! $fieldname) {
continue;
}
$field = $this->object->{$fieldname}();
if ($field->isEmpty()) {
continue;
}
try {
if (A::get($field, 'type') !== 'structure') {
continue;
}
$fieldname = A::get($field, 'name');
if (! $fieldname) {
continue;
}
$field = $this->object->{$fieldname}();
if ($field->isEmpty()) {
continue;
}

$yaml = Yaml::decode($field->value());
$yaml = $this->indexArray($yaml, [$fieldname]);
$data[$fieldname] = Yaml::encode($yaml);
} catch (Exception $exception) {
}
}

$this->update = array_merge($this->update, $data);
}

Expand Down Expand Up @@ -163,6 +166,8 @@ private function createItem(string $autoid, array $tree = null): AutoIDItem
$data = $this->itemFromStructureObject($this->object, $tree);
} elseif (is_a($this->object, Page::class)) {
$data = $this->itemFromPage($this->object);
} elseif (is_a($this->object, Site::class)) {
$data = $this->itemFromSite($this->object);
} elseif (is_a($this->object, File::class)) {
$data = $this->itemFromFile($this->object);
}
Expand All @@ -182,6 +187,16 @@ private function itemFromPage($object): array
];
}

private function itemFromSite($object): array
{
return [
'page' => '$',
'modified' => $object->modified(),
'kind' => AutoIDItem::KIND_PAGE,
'template' => 'site',
];
}

private function itemFromFile($object): array
{
return [
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/kirby3-autoid",
"type": "kirby-plugin",
"version": "2.4.11",
"version": "2.5.0",
"description": "Automatic unique ID for Pages, Files and nested Structures including performant helpers to retrieve them. Bonus: Tiny-URL.",
"license": "MIT",
"authors": [
Expand Down
Loading

0 comments on commit 4dd74d7

Please sign in to comment.