-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
executable file
·89 lines (79 loc) · 2.99 KB
/
index.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
<?php
use Kirby\Cms\App;
use Kirby\Cms\Page;
use Maxchene\Typesense\SearchResult;
use Maxchene\Typesense\TypesenseConfig;
use Maxchene\Typesense\TypesenseDocument;
use Maxchene\Typesense\TypesenseSearch;
@include_once __DIR__ . '/vendor/autoload.php';
App::plugin('maxchene/typesense', [
'options' => [
'host' => 'localhost:8108',
'num_typos' => 2,
],
'siteMethods' => [
'typesenseSearch' => function (string $query, int $limit = 30, int $page = 1): SearchResult {
$searchEngine = new TypesenseSearch();
return $searchEngine->search($query, $limit, $page);
}
],
'hooks' => [
'page.update:after' => function (Page $newPage, Page $oldPage) {
if (TypesenseConfig::isIndexable($newPage)) {
$document = new TypesenseDocument($newPage);
$document
->setNormalizer(TypesenseConfig::getNormalizer($newPage))
->upsert();
}
},
'page.changeTitle:after' => function (Page $newPage) {
if (TypesenseConfig::isIndexable($newPage)) {
$document = new TypesenseDocument($newPage);
$document
->setNormalizer(TypesenseConfig::getNormalizer($newPage))
->upsert();
}
},
'page.delete:after' => function (bool $status, Page $page) {
if ($status) {
$document = new TypesenseDocument($page);
$document->delete();
}
},
'page.changeTemplate:after' => function (Page $newPage) {
$document = new TypesenseDocument($newPage);
// if new template is not indexable, remove document
if (!TypesenseConfig::isIndexable($newPage)) {
$document->delete();
} else {
$document
->setNormalizer(TypesenseConfig::getNormalizer($newPage))
->upsert();
}
},
'page.changeStatus:after' => function (Page $newPage, Page $oldPage) {
// if new status is published and page is indexable, upsert
if (TypesenseConfig::isIndexable($newPage)) {
$document = new TypesenseDocument($newPage);
$document
->setNormalizer(TypesenseConfig::getNormalizer($newPage))
->upsert();
}
// if new status is unpublished, delete document from typesense index
if ($newPage->isDraft()) {
$document = new TypesenseDocument($newPage);
$document->delete();
}
}
],
'commands' => [
'typesense:rebuild' => [
'description' => 'Rebuild typesense index, even if you change fields configuration',
'args' => [],
'command' => function (Kirby\CLI\CLI $cli) {
// TODO build command
$cli->success('hello world');
}
]
]
]);