QuoteLinkLoader.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Services\Markup\Resolve;
  3. use App\Http\Api\BookTitle;
  4. use App\Models\PageNumber;
  5. use App\Models\PaliText;
  6. use App\Services\Markup\Ir\Call;
  7. /**
  8. * ql(quote-link,引用某版本的页码)的取数。口径同 v2 TemplateRender::render_quote_link():
  9. *
  10. * - 给了书名 + 册 + 页:直接用
  11. * - 给了 book + para:`type=c` 按章节路径取书名与章名,其余按 page_numbers 表取该版本的册页
  12. * - 只给了缅文 title:按缅文书名缩写表拆出书名与页码
  13. *
  14. * 书名再按 `:quote:` tag 查一次术语,得到本地化书名(登记给 {@see TermLoader},下一轮一起查)。
  15. * {@see BookTitle} 是 v2 的静态数据表,只读引用。
  16. */
  17. final class QuoteLinkLoader extends Loader
  18. {
  19. /** @var array<string, Call> */
  20. private array $pending = [];
  21. /** @var array<string, array{props: array<string, mixed>, text: string, termKey: ?string}> */
  22. private array $results = [];
  23. public function request(Call $call): string
  24. {
  25. $key = $call->raw."\x1F".implode(',', $call->scope->channels);
  26. if (! isset($this->results[$key])) {
  27. $this->pending[$key] = $call;
  28. }
  29. return $key;
  30. }
  31. /**
  32. * @return array{props: array<string, mixed>, text: string, termKey: ?string}|null
  33. */
  34. public function result(string $key): ?array
  35. {
  36. return $this->results[$key] ?? null;
  37. }
  38. public function hasPending(): bool
  39. {
  40. return $this->pending !== [];
  41. }
  42. public function load(): void
  43. {
  44. $batch = $this->pending;
  45. $this->pending = [];
  46. foreach ($batch as $key => $call) {
  47. $this->results[$key] = $this->resolve($call);
  48. }
  49. }
  50. /**
  51. * @return array{props: array<string, mixed>, text: string, termKey: ?string}
  52. */
  53. private function resolve(Call $call): array
  54. {
  55. $type = (string) $call->param('type');
  56. $title = (string) $call->param('title');
  57. $bookName = (string) $call->param('bookname');
  58. $volume = (string) $call->param('volume');
  59. $page = (string) $call->param('page');
  60. $book = (string) $call->param('book');
  61. $para = (string) $call->param('para');
  62. $props = ['type' => $type, 'style' => (string) $call->param('style'), 'found' => true];
  63. if ($bookName !== '' && $volume !== '' && $page !== '') {
  64. $props += ['bookName' => $bookName, 'volume' => (int) $volume, 'page' => $page];
  65. } elseif ($book !== '' && $para !== '') {
  66. $props = array_merge($props, $type === 'c'
  67. ? $this->chapterInfo((int) $book, (int) $para)
  68. : $this->pageInfo($type, (int) $book, (int) $para));
  69. } elseif ($title !== '') {
  70. $props = array_merge($props, $this->myanmarTitle($title));
  71. } else {
  72. $props['found'] = false;
  73. }
  74. if ($book !== '' && $para !== '') {
  75. $props['book'] = $book;
  76. $props['para'] = $para;
  77. }
  78. if ($title !== '') {
  79. $props['title'] = $title;
  80. }
  81. $text = '';
  82. $termKey = null;
  83. if (isset($props['bookName'])) {
  84. $field = match ($type) {
  85. 'm' => 'm_title',
  86. 'p' => 'p_title',
  87. default => null,
  88. };
  89. // v2 口径:只有 m / p 两个版本能在书名表里核对,其余版本一律记作未找到
  90. if ($field === null || ! in_array($props['bookName'], array_column(BookTitle::get(), $field), true)) {
  91. $props['found'] = false;
  92. }
  93. $termKey = $this->resolver->loader(TermLoader::class)
  94. ->request((string) $props['bookName'], ':quote:', $call->scope->firstChannel(), $call->scope);
  95. }
  96. // v2 这里误用了 volume / page 参数本身:按 book+para 或缅文标题查到的册页从不显示(输出「 .」)。
  97. // 这里改用查到的值;直接给参数时与 v2 相同
  98. if (isset($props['volume'], $props['page'])) {
  99. $text = " {$props['volume']}.{$props['page']}";
  100. }
  101. return ['props' => $props, 'text' => $text, 'termKey' => $termKey];
  102. }
  103. /**
  104. * @return array<string, mixed>
  105. */
  106. private function chapterInfo(int $book, int $para): array
  107. {
  108. $path = json_decode((string) PaliText::where('book', $book)->where('paragraph', $para)->value('path'), true);
  109. if (! is_array($path) || count($path) <= 2) {
  110. return ['found' => false];
  111. }
  112. return [
  113. 'bookName' => strtolower((string) ($path[0]['title'] ?? '')),
  114. 'chapter' => strtolower((string) (end($path)['title'] ?? '')),
  115. 'found' => true,
  116. ];
  117. }
  118. /**
  119. * @return array<string, mixed>
  120. */
  121. private function pageInfo(string $type, int $book, int $para): array
  122. {
  123. $pageInfo = PageNumber::where('type', strtoupper($type))
  124. ->where('book', $book)
  125. ->where('paragraph', '<=', $para)
  126. ->orderByDesc('paragraph')
  127. ->first(['pcd_book_id', 'volume', 'page']);
  128. if ($pageInfo === null) {
  129. return ['found' => false];
  130. }
  131. $field = match (strtoupper($type)) {
  132. 'M' => 'm_title',
  133. 'P' => 'p_title',
  134. 'V' => 'v_title',
  135. default => 'term',
  136. };
  137. $info = ['volume' => $pageInfo->volume, 'page' => $pageInfo->page, 'found' => true];
  138. foreach (BookTitle::get() as $row) {
  139. if ($row['id'] === $pageInfo->pcd_book_id) {
  140. $info['bookName'] = $row[$field];
  141. break;
  142. }
  143. }
  144. return $info;
  145. }
  146. /**
  147. * 缅文标题(书名缩写 + 页码,如「ဝိ၊၁၊၂၃」)→ 书名 / 册 / 页。
  148. *
  149. * @return array<string, mixed>
  150. */
  151. private function myanmarTitle(string $title): array
  152. {
  153. $titles = array_column(BookTitle::my(), 'title2');
  154. for ($i = mb_strlen($title, 'UTF-8'); $i > 0; $i--) {
  155. $prefix = mb_substr($title, 0, $i);
  156. $found = array_search($prefix, $titles, true);
  157. if ($found === false) {
  158. continue;
  159. }
  160. $page = (int) str_replace(
  161. ['၁', '၂', '၃', '၄', '၅', '၆', '၇', '၈', '၉', '၀'],
  162. ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
  163. (string) preg_replace('/^[၊။\s]+|[၊။\s]+$/u', '', mb_substr($title, $i)),
  164. );
  165. $row = BookTitle::my()[$found];
  166. return [
  167. 'bookName' => $row['bookname'],
  168. 'volume' => $row['volume'],
  169. 'page' => $page,
  170. 'found' => $row['bookname'] !== '',
  171. ];
  172. }
  173. return [];
  174. }
  175. }