Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
57 lines
2.1 KiB
PHP
57 lines
2.1 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\Pages;
|
||
|
||
use App\Models\User;
|
||
use App\Pagebuilder\Content;
|
||
use App\Search\GlobalSearch;
|
||
use Inhaus\Plugins\Pages\Models\Page;
|
||
|
||
/** Suchquelle für die globale Suche: veröffentlichte, sichtbare Seiten */
|
||
class PageSearch
|
||
{
|
||
public function __construct(private readonly PageTree $tree, private readonly Content $content) {}
|
||
|
||
/**
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function __invoke(User $user, string $search, int $limit): array
|
||
{
|
||
$query = Page::query()->whereNotNull('published_at');
|
||
// Inhalt liegt als JSON vor (Umlaute als ü) – daher zusätzlich die JSON-Schreibweise suchen
|
||
$encoded = trim((string) json_encode($search), '"');
|
||
$query->where(function ($q) use ($search, $encoded) {
|
||
GlobalSearch::whereTerms($q, ['title', 'content'], $search);
|
||
|
||
if ($encoded !== $search) {
|
||
$q->orWhere(fn ($w) => GlobalSearch::whereTerms($w, ['content'], $encoded));
|
||
}
|
||
});
|
||
|
||
$terms = GlobalSearch::terms($search);
|
||
$results = [];
|
||
|
||
foreach ($query->orderBy('title')->limit($limit * 3)->get() as $page) {
|
||
if (count($results) >= $limit || ! $this->tree->isVisible($page->id, $user)) {
|
||
continue;
|
||
}
|
||
|
||
// Treffer und Auszug nur aus dem, was diese Person auf der Seite sieht
|
||
// (Blöcke mit eingeschränkter Sichtbarkeit zählen nicht)
|
||
$text = $this->content->plainText($this->content->render($page->content, $user, ['page_id' => $page->id]));
|
||
|
||
if (! collect($terms)->every(fn (string $term) => mb_stripos($page->title.' '.$text, $term) !== false)) {
|
||
continue;
|
||
}
|
||
|
||
$results[] = [
|
||
'title' => $page->title,
|
||
'url' => $page->url(),
|
||
'excerpt' => GlobalSearch::excerpt($text, $search),
|
||
'meta' => collect($this->tree->ancestors($page->id))->pluck('title')->filter()->implode(' › ') ?: 'Seite',
|
||
];
|
||
}
|
||
|
||
return $results;
|
||
}
|
||
}
|