Loheide-eu-mitarbeiterplatt.../app/Search/GlobalSearch.php
Friederich Loheide b276e1c6d0
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m24s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m26s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m46s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m31s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m18s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
Globale Live-Suche, Kontaktkarten-Personensuche, neue Beitragsansicht, Termine mit Seitenleiste, Demo-Inhalte, Plugin-Upload-Fix
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:07:13 +00:00

129 lines
4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Search;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Throwable;
/**
* Globale Suche (Kopfzeile und /suche). Kern und Plugins melden Quellen an;
* jede Quelle liefert nur, was die Person sehen darf. Personen sind bewusst
* nicht dabei – dafür gibt es das Verzeichnis und die Kachel „Personen finden“.
*
* Treffer: ['title' => …, 'url' => …, 'excerpt' => ?, 'meta' => ?, 'image' => ?, 'external' => ?bool]
*/
class GlobalSearch
{
public const MIN_LENGTH = 2;
/** @var array<string, array{label: string, icon: string, sort: int, search: callable}> */
private array $sources = [];
/**
* @param string $icon Lucide-Name, z. B. „newspaper“
* @param callable(User, string, int): list<array<string, mixed>> $search
*/
public function add(string $key, string $label, string $icon, callable $search, int $sort = 50): void
{
$this->sources[$key] = compact('label', 'icon', 'sort', 'search');
}
public function remove(string $key): void
{
unset($this->sources[$key]);
}
/**
* @return list<array{key: string, label: string, icon: string, items: list<array<string, mixed>>}>
*/
public function run(User $user, string $query, int $limit): array
{
$query = trim($query);
if (mb_strlen($query) < self::MIN_LENGTH) {
return [];
}
$groups = [];
foreach (collect($this->sources)->sortBy('sort') as $key => $source) {
try {
$items = array_slice(array_values(($source['search'])($user, $query, $limit)), 0, $limit);
} catch (Throwable $e) {
// Eine fehlerhafte Quelle darf die Suche nicht lahmlegen.
Log::error("Suchquelle {$key} fehlgeschlagen: ".$e->getMessage(), ['exception' => $e]);
continue;
}
if ($items !== []) {
$groups[] = ['key' => $key, 'label' => $source['label'], 'icon' => $source['icon'], 'items' => $items];
}
}
return $groups;
}
/**
* Suchbegriffe (alle müssen vorkommen).
*
* @return list<string>
*/
public static function terms(string $query): array
{
return array_values(array_filter(preg_split('/\s+/u', trim($query)) ?: [], fn ($t) => $t !== ''));
}
/**
* Jeder Begriff muss in mindestens einer der Spalten vorkommen.
*
* @param Builder<Model> $query
* @param list<string> $columns
*/
public static function whereTerms(Builder $query, array $columns, string $search): void
{
foreach (self::terms($search) as $term) {
$like = '%'.str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $term).'%';
$query->where(function (Builder $q) use ($columns, $like) {
foreach ($columns as $column) {
$q->orWhere($column, 'like', $like);
}
});
}
}
/**
* Textausschnitt rund um den ersten Treffer (ohne HTML).
*/
public static function excerpt(?string $text, string $query, int $length = 160): ?string
{
$plain = trim((string) preg_replace('/\s+/u', ' ', html_entity_decode(strip_tags(str_replace('>', '> ', (string) $text)), ENT_QUOTES | ENT_HTML5)));
if ($plain === '') {
return null;
}
$position = null;
foreach (self::terms($query) as $term) {
$found = mb_stripos($plain, $term);
if ($found !== false && ($position === null || $found < $position)) {
$position = $found;
}
}
if ($position === null || $position < $length / 3) {
return Str::limit($plain, $length);
}
$start = max(0, $position - (int) ($length / 3));
$snippet = mb_substr($plain, $start, $length);
return '… '.trim($snippet).(mb_strlen($plain) > $start + $length ? ' …' : '');
}
}