-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,481 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bnomei; | ||
|
||
use Kirby\Filesystem\F; | ||
use Kirby\Toolkit\Str; | ||
|
||
trait Khulan | ||
use Kirby\Cms\Collection; | ||
use Kirby\Cms\File; | ||
use Kirby\Cms\Files; | ||
use Kirby\Cms\Page; | ||
use Kirby\Cms\Pages; | ||
use Kirby\Cms\Site; | ||
use Kirby\Cms\User; | ||
use Kirby\Cms\Users; | ||
use Kirby\Toolkit\A; | ||
|
||
class Khulan | ||
{ | ||
/** @var bool */ | ||
private bool $khulanCacheWillBeDeleted; | ||
|
||
public function hasKhulan(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function setBoostWillBeDeleted(bool $value): void | ||
public static function index(): array | ||
{ | ||
$this->khulanCacheWillBeDeleted = $value; | ||
} | ||
|
||
public function keyKhulan(?string $languageCode = null): string | ||
{ | ||
$key = hash('xxh3', $this->id()); // can not use UUID since content not loaded yet | ||
if (! $languageCode) { | ||
$languageCode = kirby()->languages()->count() ? kirby()->language()->code() : null; | ||
} | ||
if ($languageCode) { | ||
$key = $key.'-'.$languageCode; | ||
$count = 0; | ||
$hash = []; | ||
|
||
// reading a field like the title will make sure | ||
// that the page is loaded and cached | ||
foreach (site()->index(true) as $page) { | ||
$hash[] = $page->title()->value(); | ||
$count++; | ||
} | ||
// TODO: files, users | ||
|
||
return $key; | ||
return [ | ||
'count' => $count, | ||
'hash' => hash('xxh3', implode('|', $hash)), | ||
]; | ||
} | ||
|
||
public function readContentCache(?string $languageCode = null): ?array | ||
public static function flush(): bool | ||
{ | ||
// TODO: change to direct client findByID | ||
return Mongodb::singleton()->get( | ||
$this->keyKhulan($languageCode).'-content', | ||
null | ||
); | ||
} | ||
|
||
public function readContent(?string $languageCode = null): array | ||
{ | ||
// read from boostedCache if exists | ||
$data = option('bnomei.mongodb.khulan.read') === false || option('debug') ? null : $this->readContentCache($languageCode); | ||
|
||
// read from file and update boostedCache | ||
if (! $data) { | ||
$data = parent::readContent($languageCode); | ||
if ($data && $this->khulanCacheWillBeDeleted !== true) { | ||
$this->writeKhulan($data, $languageCode); | ||
} | ||
} | ||
mongo()->contentCollection()->drop(); | ||
|
||
return $data; | ||
return true; | ||
} | ||
|
||
public function writeKhulan(?array $data = null, ?string $languageCode = null): bool | ||
public static function documentsToModels(iterable $documents): Collection|Pages|Files|Users|null | ||
{ | ||
$cache = Mongodb::singleton(); | ||
if (! $cache || option('bnomei.mongodb.khulan.write') === false) { | ||
return true; | ||
} | ||
|
||
$modified = $this->modified(); | ||
$documents = iterator_to_array($documents); | ||
|
||
// in rare case file does not exists or is not readable | ||
if ($modified === false) { | ||
$this->deleteKhulan(); // whatever was in the cache is no longer valid | ||
|
||
return false; // try again another time | ||
if (empty($documents)) { | ||
return null; | ||
} | ||
|
||
// TODO: change to direct client insertOne | ||
return $cache->set( | ||
$this->keyKhulan($languageCode).'-content', | ||
array_filter($data, fn ($content) => $content !== null), | ||
option('bnomei.mongodb.expire') | ||
); | ||
} | ||
$models = []; | ||
|
||
public function writeContent(array $data, ?string $languageCode = null): bool | ||
{ | ||
// write to file and cache | ||
return parent::writeContent($data, $languageCode) && | ||
$this->writeKhulan($data, $languageCode); | ||
} | ||
|
||
public function deleteKhulan(): bool | ||
{ | ||
$cache = Mongodb::singleton(); | ||
if (! $cache) { | ||
return true; | ||
foreach ($documents as $document) { | ||
$models[] = self::documentToModel($document); | ||
} | ||
|
||
$this->setBoostWillBeDeleted(true); | ||
|
||
foreach (kirby()->languages() as $language) { | ||
// TODO: change to direct client deleteByID | ||
$cache->remove( | ||
$this->keyKhulan($language->code()).'-content' | ||
); | ||
$models = array_filter($models, function ($obj) { | ||
return $obj !== null; | ||
}); | ||
|
||
$modelTypes = array_count_values(array_map(function ($document) { | ||
return $document['modelType']; | ||
}, $documents)); | ||
|
||
if (count($modelTypes) === 1) { | ||
$modelType = array_key_first($modelTypes); | ||
if ($modelType === 'file') { | ||
return new Files($models); | ||
} elseif ($modelType === 'user') { | ||
return new Users($models); | ||
} elseif ($modelType === 'page') { | ||
return new Pages($models); | ||
} | ||
} else { | ||
return new Collection($models); | ||
} | ||
|
||
// TODO: change to direct client deleteByID | ||
$cache->remove( | ||
$this->keyKhulan().'-content' | ||
); | ||
|
||
return true; | ||
return null; | ||
} | ||
|
||
public function delete(bool $force = false): bool | ||
public static function documentToModel($document = null): Page|File|User|Site|null | ||
{ | ||
$cache = Mongodb::singleton(); | ||
if (! $cache) { | ||
return parent::delete($force); | ||
if (! $document) { | ||
return null; | ||
} | ||
|
||
$success = parent::delete($force); | ||
$this->deleteKhulan(); | ||
if ($document['modelType'] === 'file') { | ||
return kirby()->file($document['id']); | ||
} elseif ($document['modelType'] === 'user') { | ||
return kirby()->user($document['id']); | ||
} elseif ($document['modelType'] === 'site') { | ||
return kirby()->site(); | ||
} elseif ($document['modelType'] === 'page') { | ||
$document = iterator_to_array($document); | ||
$id = A::get($document, 'uuid', A::get($document, 'id')); | ||
|
||
return kirby()->page($id); | ||
} | ||
|
||
return $success; | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ | |
|
||
class KhulanFile extends \Kirby\Cms\File | ||
{ | ||
use Khulan; | ||
// use ModelWithKhulan; // TODO: breaks stuff | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ | |
|
||
class KhulanPage extends \Kirby\Cms\Page | ||
{ | ||
use Khulan; | ||
use ModelWithKhulan; | ||
} |
Oops, something went wrong.