'My', 'P' => 'PTS', 'V' => 'VRI', 'T' => 'Thai', 'O' => 'Other']; public function __construct( private readonly PaliSeriesesService $serieses, private readonly TipitakaBookService $books, ) {} /** * 命中段落,按命中权重降序(同分按经文顺序)。 * * @param array{words: list, bold: string|null, book_ids: list} $criteria * @return LengthAwarePaginator, * text: string, * refs: list * }> */ public function paragraphs(array $criteria, int $perPage): LengthAwarePaginator { $page = $this->matches($criteria) ->when($criteria['book_ids'] !== [], fn (Builder $q) => $q->whereIn('pcd_book_id', $criteria['book_ids'])) ->groupBy(['book', 'paragraph']) ->selectRaw('book, paragraph, sum(weight) as score, count(*) as hits') ->orderByDesc('score') ->orderBy('book') ->orderBy('paragraph') ->paginate($perPage); $rows = collect($page->items()); $texts = $this->paliTexts($rows); $refs = $this->pageNumbers($rows); $highlighter = $this->highlighter($criteria['words']); return $page->setCollection($rows->map(function ($row) use ($texts, $refs, $highlighter) { $book = (int) $row->book; $para = (int) $row->paragraph; $text = $texts->get("{$book}-{$para}"); $path = self::path($text?->path); $series = $this->serieses->find($book, $para); return [ 'book' => $book, 'para' => $para, 'score' => (int) $row->score, 'hits' => (int) $row->hits, // 标题段(level < 100)用自己的标题,正文段用所在章节的标题 'title' => $text && $text->level < 100 ? (string) $text->toc : (string) (end($path)['title'] ?? ''), 'level' => (int) ($text?->level ?? 100), 'path' => $path, 'text' => $highlighter((string) ($text?->html ?? '')), 'refs' => [ ...array_map(fn (array $r) => $r + ['title' => match ($r['type']) { 'My' => $series['abbr_my'] ?? null, 'PTS' => $series['abbr_pts'] ?? null, default => null, }], $refs->get("{$book}-{$para}", [])), ['type' => 'WP', 'volume' => null, 'page' => $para, 'title' => $series['abbr_wp'] ?? null], ], ]; })); } /** * 按书统计:每本书(pcd_book_id)命中多少段、多少词次。按段落数降序。 * * @param array{words: list, bold: string|null} $criteria * @return array{ * items: list, paragraphs: int, occurrences: int}>, * paragraphs: int, * occurrences: int * } */ public function books(array $criteria): array { $groups = $this->matches($criteria) ->groupBy('pcd_book_id') ->selectRaw('pcd_book_id, count(*) as occurrences, count(distinct (book, paragraph)) as paragraphs') ->orderByDesc('paragraphs') ->orderBy('pcd_book_id') ->get(); $books = $this->books->find($groups->pluck('pcd_book_id')); $items = $groups->map(function ($group) use ($books) { $book = $books->get((int) $group->pcd_book_id); return [ 'book_id' => (int) $group->pcd_book_id, 'book' => $book['book'] ?? 0, 'para' => $book['para'] ?? 0, 'title' => $book['title'] ?? '', 'layer' => $book['layer'] ?? null, 'tags' => $book['tags'] ?? [], 'paragraphs' => (int) $group->paragraphs, 'occurrences' => (int) $group->occurrences, ]; })->values()->all(); return [ 'items' => $items, 'paragraphs' => array_sum(array_column($items, 'paragraphs')), 'occurrences' => array_sum(array_column($items, 'occurrences')), ]; } /** * 词形与黑体条件。 * * @param array{words: list, bold: string|null} $criteria */ private function matches(array $criteria): Builder { return WbwTemplate::query() ->whereIn('real', $criteria['words']) ->when($criteria['bold'] === 'only', fn (Builder $q) => $q->where('style', 'bld')) ->when($criteria['bold'] === 'exclude', fn (Builder $q) => $q->where( fn (Builder $q) => $q->where('style', '<>', 'bld')->orWhereNull('style'), )); } /** * 本页段落的正文与路径,一次查完。 * * @param Collection $rows * @return Collection */ private function paliTexts(Collection $rows): Collection { if ($rows->isEmpty()) { return collect(); } return PaliText::query() ->where(function (Builder $q) use ($rows) { foreach ($rows as $row) { $q->orWhere(fn (Builder $q) => $q->where('book', $row->book)->where('paragraph', $row->paragraph)); } }) ->get(['book', 'paragraph', 'level', 'toc', 'path', 'html']) ->keyBy(fn (PaliText $t) => "{$t->book}-{$t->paragraph}"); } /** * 本页段落在各印本里的页码,每种版本取段落里第一个页码标记。一次查完。 * * @param Collection $rows * @return Collection> */ private function pageNumbers(Collection $rows): Collection { if ($rows->isEmpty()) { return collect(); } return PageNumber::query() ->where(function (Builder $q) use ($rows) { foreach ($rows as $row) { $q->orWhere(fn (Builder $q) => $q->where('book', $row->book)->where('paragraph', $row->paragraph)); } }) ->orderBy('wid') ->get(['type', 'volume', 'page', 'book', 'paragraph']) ->groupBy(fn (PageNumber $p) => "{$p->book}-{$p->paragraph}") ->map(fn (Collection $pages) => $pages->unique('type')->map(fn (PageNumber $p) => [ 'type' => self::EDITIONS[$p->type] ?? (string) $p->type, 'volume' => $p->volume === null ? null : (int) $p->volume, 'page' => (int) $p->page, ])->values()->all()); } /** * `pali_texts.path`(JSON 字符串,level 有时是字符串)→ 规整的数组。 * * @return list */ private static function path(mixed $raw): array { $items = is_string($raw) ? json_decode($raw, true) : $raw; if (! is_array($items)) { return []; } return array_values(array_map(fn (array $p) => [ 'book' => (int) ($p['book'] ?? 0), 'para' => (int) ($p['paragraph'] ?? 0), 'title' => (string) ($p['title'] ?? ''), 'level' => (int) ($p['level'] ?? 0), ], array_filter($items, 'is_array'))); } /** * 正文 HTML → 只含 ``(黑体)与 ``(命中词形)的安全 HTML。 * * 其余标签全部去掉、正文全部转义,客户端可以直接作为 HTML 渲染。命中词按整词匹配 * (前后不是字母),不区分首字母大小写;词形以 ti / nti 结尾的,正文里常写成 * 「词’’ti」,所以也匹配去掉 ti 后的部分(同 v2)。 * * @param list $words * @return \Closure(string): string */ private function highlighter(array $words): \Closure { $variants = []; foreach ($words as $word) { $variants[] = $word; if (str_ends_with($word, 'nti')) { $variants[] = mb_substr($word, 0, -3, 'UTF-8'); } elseif (str_ends_with($word, 'ti')) { $variants[] = mb_substr($word, 0, -2, 'UTF-8'); } } $variants = array_filter(array_unique($variants), fn (string $v) => mb_strlen($v, 'UTF-8') > 1); usort($variants, fn (string $a, string $b) => mb_strlen($b, 'UTF-8') <=> mb_strlen($a, 'UTF-8')); $alternatives = implode('|', array_map(fn (string $v) => preg_quote(htmlspecialchars($v, ENT_QUOTES, 'UTF-8'), '/'), $variants)); $pattern = $alternatives === '' ? null : '/(? $text = preg_replace('/(.*?)<\/span>/su', "\u{E000}$1\u{E001}", $html) ?? ''; $text = htmlspecialchars(trim(html_entity_decode(strip_tags($text), ENT_QUOTES | ENT_HTML5, 'UTF-8')), ENT_QUOTES, 'UTF-8'); if ($pattern !== null) { $text = preg_replace($pattern, '$1', $text) ?? $text; } return str_replace(["\u{E000}", "\u{E001}"], ['', ''], $text); }; } }