Skip to content

Commit

Permalink
✨ indexing files
Browse files Browse the repository at this point in the history
  • Loading branch information
bnomei committed Jul 7, 2024
1 parent 23dcd6e commit f1872ed
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 20 deletions.
27 changes: 26 additions & 1 deletion classes/Khulan.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Kirby\Cms\Site;
use Kirby\Cms\User;
use Kirby\Cms\Users;
use Kirby\Data\Data;
use Kirby\Filesystem\F;
use Kirby\Toolkit\A;

class Khulan
Expand Down Expand Up @@ -39,7 +41,7 @@ public static function index(int $iterations = 2): array
$count++;
}

/** @var File $file */
/** @var User $user */
foreach (kirby()->users() as $user) {
if ($user->hasKhulan() !== true) {
continue;
Expand All @@ -57,6 +59,28 @@ public static function index(int $iterations = 2): array
$count++;
}

/** @var File $file */
foreach (site()->index(true)->files() as $file) {
if ($file->hasKhulan() !== true) {
continue;
}
if (kirby()->multilang()) {
foreach (kirby()->languages() as $language) {
$contentFile = $file->root().'.'.$language->code().'.txt';
if (! F::exists($contentFile)) {
continue;
}
$hash[] = $file->id().$language->code();
$file->writeKhulan(Data::read($contentFile), $language->code());
}
} else {
$contentFile = $file->root().'.txt';
$hash[] = $file->id();
$file->writeKhulan(Data::read($contentFile));
}
$count++;
}

$meta = [
'count' => $count,
'hash' => hash('xxh3', implode('|', $hash)),
Expand All @@ -75,6 +99,7 @@ public static function index(int $iterations = 2): array
khulan()->createIndex(['language' => 1]);
khulan()->createIndex(['template' => 1]);
khulan()->createIndex(['modelType' => 1]);
khulan()->createIndex(['status' => 1]);
}

return $meta;
Expand Down
2 changes: 1 addition & 1 deletion classes/KhulanFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class KhulanFile extends \Kirby\Cms\File
{
// use ModelWithKhulan; // TODO: breaks stuff
use ModelWithKhulan;
}
60 changes: 52 additions & 8 deletions classes/ModelWithKhulan.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use DateTime;
use Exception;
use Kirby\Cms\Blueprint;
use Kirby\Cms\File;
use Kirby\Cms\Page;
use Kirby\Cms\Site;
use Kirby\Cms\User;
use Kirby\Data\Yaml;
use Kirby\Filesystem\F;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
use MongoDB\BSON\ObjectId;
Expand All @@ -22,6 +24,10 @@ trait ModelWithKhulan

public function hasKhulan(): bool
{
if ($this instanceof File) {
return $this->parent()->hasKhulan() === true;
}

return true;
}

Expand Down Expand Up @@ -107,24 +113,33 @@ public function writeKhulan(?array $data = null, ?string $languageCode = null):
$modelType = 'file';
}

$slug = explode('/', $this->id());
$meta = [
'id' => $this->id() ?? null,
'modified' => $modified,
'modified{}' => new UTCDateTime($modified * 1000),
'slug' => $this->id() ? array_pop($slug) : null,
'class' => $this::class,
'language' => $languageCode,
'modelType' => $modelType,
];
if ($this instanceof Page) {
$slug = explode('/', $this->id());
$meta['num'] = $this->num() ? (int) $this->num() : null;
$meta['slug'] = $this->id() ? array_pop($slug) : null;
$meta['template'] = $this->intendedTemplate()->name();
$meta['status'] = $this->status();
} elseif ($this instanceof File) {
// can not use $file->content() since it would trigger a loop
$meta['sort'] = A::get($data, 'sort') ? (int) A::get($data, 'sort') : null;
$meta['filename'] = $this->filename();
$meta['template'] = A::get($data, 'template');
$meta['mimeType'] = F::mime($this->root());
$meta['parent{}'] = new ObjectId($this->parent()->keyKhulan($languageCode));
} elseif ($this instanceof User) {
$meta['email'] = $this->email();
$meta['name'] = $this->name()->isNotEmpty() ? $this->name()->value() : null;
$meta['role'] = $this->role()->name();
}
$data = $this->encodeKhulan($data, $languageCode) + $meta;
$data = array_merge($this->encodeKhulan($data, $languageCode), $meta);

// _id is not allowed as data key
if (array_key_exists('_id', $data)) {
Expand Down Expand Up @@ -180,11 +195,28 @@ public function delete(bool $force = false): bool

public function encodeKhulan(array $data, ?string $languageCode = null): array
{
$blueprint = null;
if ($this instanceof Page) {
$blueprint = $this->blueprint()->fields();
} elseif ($this instanceof File) {
// $blueprint = $this->blueprint();
// does not work as that would trigger a loop reading the content
// but it can be read manually
$blueprint = Blueprint::find('files/'.A::get($data, 'template', 'default'));
$blueprint = A::get($blueprint, 'fields');
}
if (! $blueprint) {
return $data;
}
// foreach each key value pairs
$copy = $data;
foreach ($data as $key => $value) {
$field = A::get($blueprint, $key);
if (! $field) {
continue;
}
if (is_string($value)) {
$type = A::get($this->blueprint()->field($key), 'type');
$type = A::get($field, 'type');

// if it is a comma separated list unroll it to an array. validate if it is with a regex but allow for spaces chars after the comma

Expand Down Expand Up @@ -299,14 +331,26 @@ public function decodeKhulan(?array $data = []): array
$meta = [
'id',
'modified',
'slug',
'template',
'modified{}',
'class',
'language',
'modelType',
'filename',
'email',
];
if ($this instanceof Page) {
$meta[] = 'num';
$meta[] = 'slug';
$meta[] = 'status';
$meta[] = 'template';
} elseif ($this instanceof File) {
$meta[] = 'sort';
$meta[] = 'filename';
$meta[] = 'mimeType';
$meta[] = 'template';
} elseif ($this instanceof User) {
$meta[] = 'email';
$meta[] = 'name';
$meta[] = 'role';
}
foreach ($meta as $key) {
if (array_key_exists($key, $copy)) {
unset($copy[$key]);
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/kirby-mongodb",
"type": "kirby-plugin",
"version": "1.2.0",
"version": "1.3.0",
"description": "Khulan is a cache driver and content cache with NoSQL interface for Kirby using MongoDB",
"license": "MIT",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function khulan(string|array|null $search = null): mixed
'khulan' => [ // cache for models
'read' => false, // mongodb is most likely slower than file system for the pages
'write' => true,
'patch-files-class' => false, // monkey patch files class
'patch-files-class' => true, // monkey patch files class
],
],
'cacheTypes' => [
Expand Down
10 changes: 9 additions & 1 deletion tests/MongodbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
'tags[,]' => ['$in' => ['Punk']],
]);

expect($pages->count())->toBe(1);
expect($pages->count())->toBe(2);
});

it('can find all pages that have another page linked', function () {
Expand Down Expand Up @@ -185,6 +185,14 @@
->and($user->email())->toBe($email);
});

it('can find a file', function () {
Khulan::index();

$file = khulan('betterharder/image.jpg');
expect($file)->toBeInstanceOf(\Kirby\Cms\File::class)
->and($file->filename())->toBe('image.jpg');
});

it('can run the benchmark', function () {
mongo()->benchmark();
})->skip();
Empty file.
8 changes: 6 additions & 2 deletions tests/content/2_betterharder/default.en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Title: Better Harder

----

Text: ssxxsss
Text: ssxxssss

----

Expand All @@ -18,12 +18,16 @@ Tags: Daft, Punk

----

Category:
Category: books

----

Related: - page://fasterstronger

----

Grass: - file://CVPNHRGDjnSWKsAz

----

Uuid: betterharder
File renamed without changes
9 changes: 9 additions & 0 deletions tests/content/2_betterharder/example2.png.en.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Uuid: CVPNHRGDjnSWKsAz

----

Template: grass

----

Sort: 1
Binary file added tests/content/2_betterharder/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/content/2_betterharder/image.jpg.de.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Textinfile: image DE
9 changes: 9 additions & 0 deletions tests/content/2_betterharder/image.jpg.en.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Textinfile: image ENs

----

Uuid: 0MAa7eq7xsWRqK2u

----

Sort: 2
3 changes: 3 additions & 0 deletions tests/site/blueprints/files/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fields:
textinfile:
type: text
3 changes: 3 additions & 0 deletions tests/site/blueprints/files/grass.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fields:
green:
type: text
7 changes: 7 additions & 0 deletions tests/site/blueprints/tabs/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ sections:
movies: Movies
related:
type: pages
grass:
type: files
uploads:
template: grass
info: "{{ file.template }}"
pages:
type: pages
info: "{{ page.template }}"
files:
type: files
info: "{{ file.template }}"
3 changes: 3 additions & 0 deletions tests/site/blueprints/users/admin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fields:
birthday:
type: date
8 changes: 4 additions & 4 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php return array(
'root' => array(
'name' => 'bnomei/kirby-mongodb',
'pretty_version' => '1.2.0',
'version' => '1.2.0.0',
'pretty_version' => '1.3.0',
'version' => '1.3.0.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand All @@ -11,8 +11,8 @@
),
'versions' => array(
'bnomei/kirby-mongodb' => array(
'pretty_version' => '1.2.0',
'version' => '1.2.0.0',
'pretty_version' => '1.3.0',
'version' => '1.3.0.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand Down

0 comments on commit f1872ed

Please sign in to comment.