PaliContentService.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. <?php
  2. // api-v8/app/Services/PaliContentService.php
  3. namespace App\Services;
  4. use App\Facades\RedisCache;
  5. use App\Http\Api\ChannelApi;
  6. use App\Http\Api\MdRender;
  7. use App\Http\Api\StudioApi;
  8. use App\Http\Api\SuggestionApi;
  9. use App\Http\Api\UserApi;
  10. use App\Models\Channel;
  11. use App\Models\Discussion;
  12. use App\Models\PaliSentence;
  13. use App\Models\PaliText;
  14. use App\Models\Sentence;
  15. use App\Models\SentSimIndex;
  16. use App\Models\Wbw;
  17. use App\Models\WbwBlock;
  18. use App\Services\Markup\Annotation\AnnotationSet;
  19. use App\Services\Markup\Format;
  20. use App\Services\Markup\MarkupService;
  21. use App\Services\Markup\RenderOptions;
  22. use Illuminate\Support\Arr;
  23. use Illuminate\Support\Facades\Log;
  24. use Illuminate\Support\Str;
  25. class PaliContentService
  26. {
  27. protected $result = [
  28. 'uid' => '',
  29. 'title' => '',
  30. 'path' => [],
  31. 'sub_title' => '',
  32. 'summary' => '',
  33. 'content' => '',
  34. 'content_type' => 'html',
  35. 'toc' => [],
  36. 'status' => 30,
  37. 'lang' => '',
  38. 'created_at' => '',
  39. 'updated_at' => '',
  40. ];
  41. protected $wbwChannels = [];
  42. // 句子需要查询的列
  43. protected $selectCol = [
  44. 'uid',
  45. 'book_id',
  46. 'paragraph',
  47. 'word_start',
  48. 'word_end',
  49. 'channel_uid',
  50. 'content',
  51. 'content_type',
  52. 'editor_uid',
  53. 'acceptor_uid',
  54. 'pr_edit_at',
  55. 'fork_at',
  56. 'create_time',
  57. 'modify_time',
  58. 'created_at',
  59. 'updated_at',
  60. ];
  61. protected $userUuid = null;
  62. protected $debug = [];
  63. public static function _sentCanReadCount($book, $para, $start, $end, $userUuid = null)
  64. {
  65. $keyCanRead = '/channel/can-read/';
  66. if ($userUuid) {
  67. $keyCanRead .= $userUuid;
  68. } else {
  69. $keyCanRead .= 'guest';
  70. }
  71. $channelCanRead = RedisCache::remember(
  72. $keyCanRead,
  73. config('mint.cache.expire'),
  74. function () use ($userUuid) {
  75. return ChannelApi::getCanReadByUser($userUuid);
  76. }
  77. );
  78. $channels = Sentence::where('book_id', $book)
  79. ->where('paragraph', $para)
  80. ->where('word_start', $start)
  81. ->where('word_end', $end)
  82. ->where('strlen', '<>', 0)
  83. ->whereIn('channel_uid', $channelCanRead)
  84. ->select('channel_uid')
  85. ->groupBy('channel_uid')
  86. ->get();
  87. $channelList = [];
  88. foreach ($channels as $key => $value) {
  89. // code...
  90. if (Str::isUuid($value->channel_uid)) {
  91. $channelList[] = $value->channel_uid;
  92. }
  93. }
  94. $simId = PaliSentence::where('book', $book)
  95. ->where('paragraph', $para)
  96. ->where('word_begin', $start)
  97. ->where('word_end', $end)
  98. ->value('id');
  99. if ($simId) {
  100. $output['simNum'] = SentSimIndex::where('sent_id', $simId)->value('count');
  101. } else {
  102. $output['simNum'] = 0;
  103. }
  104. $channelInfo = Channel::whereIn('uid', $channelList)->select('type')->get();
  105. $output['tranNum'] = 0;
  106. $output['nissayaNum'] = 0;
  107. $output['commNum'] = 0;
  108. $output['originNum'] = 0;
  109. foreach ($channelInfo as $key => $value) {
  110. // code...
  111. switch ($value->type) {
  112. case 'translation':
  113. $output['tranNum']++;
  114. break;
  115. case 'nissaya':
  116. $output['nissayaNum']++;
  117. break;
  118. case 'commentary':
  119. $output['commNum']++;
  120. break;
  121. case 'original':
  122. $output['originNum']++;
  123. break;
  124. }
  125. }
  126. return $output;
  127. }
  128. private function newSent($book, $para, $word_start, $word_end)
  129. {
  130. $sent = [
  131. 'id' => "{$book}-{$para}-{$word_start}-{$word_end}",
  132. 'book' => $book,
  133. 'para' => $para,
  134. 'wordStart' => $word_start,
  135. 'wordEnd' => $word_end,
  136. 'origin' => [],
  137. 'translation' => [],
  138. 'commentaries' => [],
  139. ];
  140. if ($book < 1000) {
  141. // 生成channel 数量列表
  142. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  143. $channelCount = self::_sentCanReadCount($book, $para, $word_start, $word_end, $this->userUuid);
  144. $path = json_decode(PaliText::where('book', $book)->where('paragraph', $para)->value('path'), true);
  145. $sent['path'] = [];
  146. foreach ($path as $key => $value) {
  147. // code...
  148. $value['paliTitle'] = $value['title'];
  149. $sent['path'][] = $value;
  150. }
  151. $sent['tranNum'] = $channelCount['tranNum'];
  152. $sent['nissayaNum'] = $channelCount['nissayaNum'];
  153. $sent['commNum'] = $channelCount['commNum'];
  154. $sent['originNum'] = $channelCount['originNum'];
  155. $sent['simNum'] = $channelCount['simNum'];
  156. }
  157. return $sent;
  158. }
  159. /**
  160. * 根据句子库数据生成以段落为单位的文章内容
  161. * $record 句子数据
  162. * $mode read | edit | wbw
  163. * $indexChannel channel索引
  164. * $indexedHeading 标题索引 用于给段落加标题标签 <h1> ect.
  165. */
  166. public function makeContentObj($record, $mode, $indexChannel, $format = 'react')
  167. {
  168. $content = [];
  169. // 逐词解析(wbw)默认使用第一个 translation channel 的数据。
  170. // 系统原文 channel 固定在末尾(见 CorpusController / ChapterContentController 的约定),
  171. // 这里从 indexChannel 挑出 translation channel 作为 wbw 的读写目标;
  172. // 没有 translation channel 时(如直接编辑系统原文)保持为空,回落到系统原文。
  173. $this->wbwChannels = [];
  174. foreach ($indexChannel as $channelId => $info) {
  175. if ($info->type === 'translation') {
  176. $this->wbwChannels[] = $channelId;
  177. break;
  178. }
  179. }
  180. // 获取句子编号列表
  181. $paraIndex = [];
  182. foreach ($record as $value) {
  183. $currSentId = "{$value->book_id}-{$value->paragraph}-{$value->word_start}-{$value->word_end}";
  184. $value->sid = "{$currSentId}_{$value->channel_uid}";
  185. $currParaId = "{$value->book_id}-{$value->paragraph}";
  186. if (! isset($paraIndex[$currParaId])) {
  187. $paraIndex[$currParaId] = [];
  188. }
  189. $paraIndex[$currParaId][] = $value;
  190. }
  191. $channelsId = [];
  192. foreach ($indexChannel as $channelId => $info) {
  193. $channelsId[] = $channelId;
  194. }
  195. array_pop($channelsId);
  196. // 遍历列表查找每个句子的所有channel的数据,并填充
  197. $paragraphs = [];
  198. foreach ($paraIndex as $currParaId => $sentData) {
  199. $arrParaId = explode('-', $currParaId);
  200. $sentIndex = [];
  201. foreach ($sentData as $sent) {
  202. $currSentId = "{$sent->book_id}-{$sent->paragraph}-{$sent->word_start}-{$sent->word_end}";
  203. $sentIndex[$currSentId] = [$sent->book_id, $sent->paragraph, $sent->word_start, $sent->word_end];
  204. }
  205. $sentInPara = array_values($sentIndex);
  206. $paraProps = [
  207. 'book' => $arrParaId[0],
  208. 'para' => $arrParaId[1],
  209. 'channels' => $channelsId,
  210. 'sentences' => $sentInPara,
  211. 'mode' => $mode,
  212. 'children' => [],
  213. ];
  214. // 建立段落里面的句子列表
  215. foreach ($sentIndex as $ids => $arrSentId) {
  216. $sentNode = $this->newSent($arrSentId[0], $arrSentId[1], $arrSentId[2], $arrSentId[3]);
  217. foreach ($indexChannel as $channelId => $info) {
  218. // code...
  219. $sid = "{$ids}_{$channelId}";
  220. if (isset($info->studio)) {
  221. $studioInfo = $info->studio;
  222. } else {
  223. $studioInfo = null;
  224. }
  225. $newSent = [
  226. 'content' => '',
  227. 'html' => '',
  228. 'book' => $arrSentId[0],
  229. 'para' => $arrSentId[1],
  230. 'wordStart' => $arrSentId[2],
  231. 'wordEnd' => $arrSentId[3],
  232. 'channel' => [
  233. 'name' => $info->name,
  234. 'type' => $info->type,
  235. 'id' => $info->uid,
  236. 'lang' => $info->lang,
  237. ],
  238. 'studio' => $studioInfo,
  239. 'updateAt' => '',
  240. 'suggestionCount' => SuggestionApi::getCountBySent($arrSentId[0], $arrSentId[1], $arrSentId[2], $arrSentId[3], $channelId),
  241. ];
  242. $row = Arr::first($sentData, function ($value, $key) use ($sid) {
  243. return $value->sid === $sid;
  244. });
  245. if ($row) {
  246. $newSent['id'] = $row->uid;
  247. $newSent['content'] = $row->content;
  248. $newSent['contentType'] = $row->content_type;
  249. $newSent['html'] = '';
  250. $newSent['editor'] = UserApi::getByUuid($row->editor_uid);
  251. /**
  252. * TODO 刷库改数据
  253. * 旧版api没有更新updated_at所以造成旧版的数据updated_at数据比modify_time 要晚
  254. */
  255. $newSent['forkAt'] = $row->fork_at; //
  256. $newSent['updateAt'] = $row->updated_at; //
  257. $newSent['updateAt'] = date('Y-m-d H:i:s.', $row->modify_time / 1000).($row->modify_time % 1000).' UTC';
  258. $newSent['createdAt'] = $row->created_at;
  259. if ($mode !== 'read') {
  260. if (isset($row->acceptor_uid) && ! empty($row->acceptor_uid)) {
  261. $newSent['acceptor'] = UserApi::getByUuid($row->acceptor_uid);
  262. $newSent['prEditAt'] = $row->pr_edit_at;
  263. }
  264. }
  265. switch ($info->type) {
  266. case 'wbw':
  267. case 'original':
  268. //
  269. // 在编辑模式下。
  270. // 如果是原文,查看是否有逐词解析数据,
  271. // 有的话优先显示。
  272. // 阅读模式直接显示html原文
  273. // 传过来的数据一定有一个原文channel
  274. //
  275. if ($mode === 'read') {
  276. $newSent['content'] = '';
  277. $newSent['html'] = MdRender::render(
  278. $row->content,
  279. [$row->channel_uid],
  280. null,
  281. $mode,
  282. 'translation',
  283. $row->content_type,
  284. $format
  285. );
  286. } else {
  287. if ($row->content_type === 'json') {
  288. $newSent['channel']['type'] = 'wbw';
  289. if (isset($this->wbwChannels[0])) {
  290. $newSent['channel']['name'] = $indexChannel[$this->wbwChannels[0]]->name;
  291. $newSent['channel']['lang'] = $indexChannel[$this->wbwChannels[0]]->lang;
  292. $newSent['channel']['id'] = $this->wbwChannels[0];
  293. // 存在一个translation channel
  294. // 尝试查找逐词解析数据。找到,替换现有数据
  295. $wbwData = $this->getWbw(
  296. $arrSentId[0],
  297. $arrSentId[1],
  298. $arrSentId[2],
  299. $arrSentId[3],
  300. $this->wbwChannels[0]
  301. );
  302. if ($wbwData) {
  303. $newSent['content'] = $wbwData;
  304. $newSent['contentType'] = 'json';
  305. $newSent['html'] = '';
  306. $newSent['studio'] = $indexChannel[$this->wbwChannels[0]]->studio;
  307. }
  308. }
  309. } else {
  310. $newSent['content'] = $row->content;
  311. $newSent['html'] = MdRender::render(
  312. $row->content,
  313. [$row->channel_uid],
  314. null,
  315. $mode,
  316. 'translation',
  317. $row->content_type,
  318. $format
  319. );
  320. }
  321. }
  322. break;
  323. case 'nissaya':
  324. $newSent['html'] = RedisCache::remember(
  325. "/sent/{$channelId}/{$ids}/{$format}",
  326. config('mint.cache.expire'),
  327. function () use ($row, $mode, $format) {
  328. return MdRender::render(
  329. $row->content,
  330. [$row->channel_uid],
  331. null,
  332. $mode,
  333. 'nissaya',
  334. $row->content_type,
  335. $format
  336. );
  337. }
  338. );
  339. break;
  340. case 'commentary':
  341. $options = [
  342. 'debug' => $this->debug,
  343. 'format' => $format,
  344. 'mode' => $mode,
  345. 'channelType' => 'translation',
  346. 'contentType' => $row->content_type,
  347. ];
  348. $mdRender = new MdRender($options);
  349. $newSent['html'] = $mdRender->convert($row->content, $channelsId);
  350. break;
  351. default:
  352. $options = [
  353. 'debug' => $this->debug,
  354. 'format' => $format,
  355. 'mode' => $mode,
  356. 'channelType' => 'translation',
  357. 'contentType' => $row->content_type,
  358. ];
  359. $mdRender = new MdRender($options);
  360. $newSent['html'] = $mdRender->convert($row->content, [$row->channel_uid]);
  361. // Log::debug('md render', ['content' => $row->content, 'options' => $options, 'render' => $newSent['html']]);
  362. break;
  363. }
  364. } else {
  365. Log::warning('no sentence record');
  366. }
  367. switch ($info->type) {
  368. case 'wbw':
  369. case 'original':
  370. array_push($sentNode['origin'], $newSent);
  371. break;
  372. case 'commentary':
  373. array_push($sentNode['commentaries'], $newSent);
  374. break;
  375. default:
  376. array_push($sentNode['translation'], $newSent);
  377. break;
  378. }
  379. }
  380. $paraProps['children'][] = $sentNode;
  381. }
  382. $paragraphs[] = $paraProps;
  383. }
  384. return $paragraphs;
  385. }
  386. public function getWbw($book, $para, $start, $end, $channel)
  387. {
  388. /**
  389. * 非阅读模式下。原文使用逐词解析数据。
  390. * 优先加载第一个translation channel 如果没有。加载默认逐词解析。
  391. */
  392. // 获取逐词解析数据
  393. $wbwBlock = WbwBlock::where('channel_uid', $channel)
  394. ->where('book_id', $book)
  395. ->where('paragraph', $para)
  396. ->select('uid')
  397. ->first();
  398. if (! $wbwBlock) {
  399. return false;
  400. }
  401. // 找到逐词解析数据
  402. $wbwData = Wbw::where('block_uid', $wbwBlock->uid)
  403. ->whereBetween('wid', [$start, $end])
  404. ->select(['book_id', 'paragraph', 'wid', 'data', 'uid', 'editor_id', 'created_at', 'updated_at'])
  405. ->orderBy('wid')
  406. ->get();
  407. $wbwContent = [];
  408. foreach ($wbwData as $wbwrow) {
  409. $wbw = str_replace('&nbsp;', ' ', $wbwrow->data);
  410. $wbw = str_replace('<br>', ' ', $wbw);
  411. $xmlString = '<root>'.$wbw.'</root>';
  412. try {
  413. $xmlWord = simplexml_load_string($xmlString);
  414. } catch (\Exception $e) {
  415. Log::error('corpus', ['error' => $e]);
  416. continue;
  417. }
  418. $wordsList = $xmlWord->xpath('//word');
  419. foreach ($wordsList as $word) {
  420. $case = \str_replace(['#', '.'], ['$', ''], $word->case->__toString());
  421. $case = \str_replace('$$', '$', $case);
  422. $case = trim($case);
  423. $case = trim($case, '$');
  424. $wbwId = explode('-', $word->id->__toString());
  425. $wbwData = [
  426. 'uid' => $wbwrow->uid,
  427. 'book' => $wbwrow->book_id,
  428. 'para' => $wbwrow->paragraph,
  429. 'sn' => array_slice($wbwId, 2),
  430. 'word' => ['value' => $word->pali->__toString(), 'status' => 0],
  431. 'real' => ['value' => $word->real->__toString(), 'status' => 0],
  432. 'meaning' => ['value' => $word->mean->__toString(), 'status' => 0],
  433. 'type' => ['value' => $word->type->__toString(), 'status' => 0],
  434. 'grammar' => ['value' => $word->gramma->__toString(), 'status' => 0],
  435. 'case' => ['value' => $word->case->__toString(), 'status' => 0],
  436. 'parent' => ['value' => $word->parent->__toString(), 'status' => 0],
  437. 'style' => ['value' => $word->style->__toString(), 'status' => 0],
  438. 'factors' => ['value' => $word->org->__toString(), 'status' => 0],
  439. 'factorMeaning' => ['value' => $word->om->__toString(), 'status' => 0],
  440. 'confidence' => $word->cf->__toString(),
  441. 'created_at' => $wbwrow->created_at,
  442. 'updated_at' => $wbwrow->updated_at,
  443. 'hasComment' => Discussion::where('res_id', $wbwrow->uid)->exists(),
  444. ];
  445. if (isset($word->parent2)) {
  446. $wbwData['parent2']['value'] = $word->parent2->__toString();
  447. if (isset($word->parent2['status'])) {
  448. $wbwData['parent2']['status'] = (int) $word->parent2['status'];
  449. } else {
  450. $wbwData['parent2']['status'] = 0;
  451. }
  452. }
  453. if (isset($word->pg)) {
  454. $wbwData['grammar2']['value'] = $word->pg->__toString();
  455. if (isset($word->pg['status'])) {
  456. $wbwData['grammar2']['status'] = (int) $word->pg['status'];
  457. } else {
  458. $wbwData['grammar2']['status'] = 0;
  459. }
  460. }
  461. if (isset($word->rela)) {
  462. $wbwData['relation']['value'] = $word->rela->__toString();
  463. if (isset($word->rela['status'])) {
  464. $wbwData['relation']['status'] = (int) $word->rela['status'];
  465. } else {
  466. $wbwData['relation']['status'] = 7;
  467. }
  468. }
  469. if (isset($word->bmt)) {
  470. $wbwData['bookMarkText']['value'] = $word->bmt->__toString();
  471. if (isset($word->bmt['status'])) {
  472. $wbwData['bookMarkText']['status'] = (int) $word->bmt['status'];
  473. } else {
  474. $wbwData['bookMarkText']['status'] = 7;
  475. }
  476. }
  477. if (isset($word->bmc)) {
  478. $wbwData['bookMarkColor']['value'] = $word->bmc->__toString();
  479. if (isset($word->bmc['status'])) {
  480. $wbwData['bookMarkColor']['status'] = (int) $word->bmc['status'];
  481. } else {
  482. $wbwData['bookMarkColor']['status'] = 7;
  483. }
  484. }
  485. if (isset($word->note)) {
  486. $wbwData['note']['value'] = $word->note->__toString();
  487. if (isset($word->note['status'])) {
  488. $wbwData['note']['status'] = (int) $word->note['status'];
  489. } else {
  490. $wbwData['note']['status'] = 7;
  491. }
  492. }
  493. if (isset($word->cf)) {
  494. $wbwData['confidence'] = (float) $word->cf->__toString();
  495. }
  496. if (isset($word->attachments)) {
  497. $wbwData['attachments'] = json_decode($word->attachments->__toString());
  498. }
  499. if (isset($word->pali['status'])) {
  500. $wbwData['word']['status'] = (int) $word->pali['status'];
  501. }
  502. if (isset($word->real['status'])) {
  503. $wbwData['real']['status'] = (int) $word->real['status'];
  504. }
  505. if (isset($word->mean['status'])) {
  506. $wbwData['meaning']['status'] = (int) $word->mean['status'];
  507. }
  508. if (isset($word->type['status'])) {
  509. $wbwData['type']['status'] = (int) $word->type['status'];
  510. }
  511. if (isset($word->gramma['status'])) {
  512. $wbwData['grammar']['status'] = (int) $word->gramma['status'];
  513. }
  514. if (isset($word->case['status'])) {
  515. $wbwData['case']['status'] = (int) $word->case['status'];
  516. }
  517. if (isset($word->parent['status'])) {
  518. $wbwData['parent']['status'] = (int) $word->parent['status'];
  519. }
  520. if (isset($word->org['status'])) {
  521. $wbwData['factors']['status'] = (int) $word->org['status'];
  522. }
  523. if (isset($word->om['status'])) {
  524. $wbwData['factorMeaning']['status'] = (int) $word->om['status'];
  525. }
  526. $wbwContent[] = $wbwData;
  527. }
  528. }
  529. if (count($wbwContent) === 0) {
  530. return false;
  531. }
  532. return \json_encode($wbwContent, JSON_UNESCAPED_UNICODE);
  533. }
  534. public function getChannelIndex($channels, $type = null)
  535. {
  536. // 获取channel索引表
  537. $channelInfo = Channel::whereIn('uid', $channels)
  538. ->select(['uid', 'type', 'name', 'lang', 'owner_uid'])
  539. ->get();
  540. $indexChannel = [];
  541. foreach ($channels as $key => $channelId) {
  542. $channelInfo = Channel::where('uid', $channelId)
  543. ->select(['uid', 'type', 'name', 'lang', 'owner_uid'])->first();
  544. if (! $channelInfo) {
  545. Log::error('no channel id'.$channelId);
  546. continue;
  547. }
  548. if ($type !== null && $channelInfo->type !== $type) {
  549. continue;
  550. }
  551. $indexChannel[$channelId] = $channelInfo;
  552. $indexChannel[$channelId]->studio = StudioApi::getById($channelInfo->owner_uid);
  553. }
  554. return $indexChannel;
  555. }
  556. public function sentences(array $sentenceIds, array $channelIds, string $mode)
  557. {
  558. $query = [];
  559. foreach ($sentenceIds as $id) {
  560. // code...
  561. $query[] = explode('-', $id);
  562. }
  563. $record = Sentence::select($this->selectCol)
  564. ->whereIns(['book_id', 'paragraph', 'word_start', 'word_end'], $query)
  565. ->whereIn('channel_uid', $channelIds)
  566. ->get();
  567. $indexChannel = $this->getChannelIndex($channelIds);
  568. $result = $this->makeContentObj($record, $mode, $indexChannel);
  569. return $result;
  570. }
  571. public function paragraphs(int $book, int $start, int $end, array $channelIds, array $param)
  572. {
  573. $channels = [];
  574. foreach ($channelIds as $key => $channel) {
  575. if (Str::isUuid($channel)) {
  576. $channels[] = $channel;
  577. }
  578. }
  579. $channelId = false;
  580. if ($param['original']) {
  581. if ($param['mode'] === 'read') {
  582. // 阅读模式加载html格式原文
  583. $channelId = app(ChannelService::class)->systemUid('_System_Pali_VRI_');
  584. } else {
  585. // 翻译模式加载json格式原文
  586. $channelId = app(ChannelService::class)->systemUid('_System_Wbw_VRI_');
  587. }
  588. }
  589. if ($channelId !== false) {
  590. $channels[] = $channelId;
  591. }
  592. // 获取channel索引表
  593. $tranChannels = [];
  594. $channelInfo = Channel::whereIn('uid', $channels)
  595. ->select(['uid', 'type', 'lang', 'name'])->get();
  596. foreach ($channelInfo as $key => $value) {
  597. // code...
  598. if ($value->type === 'translation') {
  599. $tranChannels[] = $value->uid;
  600. }
  601. }
  602. $indexChannel = [];
  603. $paliService = app(PaliContentService::class);
  604. $indexChannel = $paliService->getChannelIndex($channels);
  605. // the end of channel
  606. // content
  607. $record = Sentence::select($this->selectCol)
  608. ->where('book_id', $book)
  609. ->whereBetween('paragraph', [$start, $end])
  610. ->whereIn('channel_uid', $channels)
  611. ->orderBy('paragraph')
  612. ->orderBy('word_start')
  613. ->get();
  614. $result = $paliService->makeContentObj($record, $param['mode'], $indexChannel, $param['format']);
  615. return $result;
  616. }
  617. /**
  618. * 阅读模式渲染单个段落。
  619. * 直接从 sentences 表取单个 channel 的记录,渲染成 html。
  620. * 与 paragraphs() 不同:没有译文的句子不保留占位,有多少句子输出多少句子。
  621. *
  622. * @return array{para: int, display: string, sentences: array<int, array{sid: string, html: string}>}
  623. */
  624. public function readParagraph(int $book, int $para, string $channelUid, string $format = 'html'): array
  625. {
  626. $level = $this->paragraphLevel($book, $para);
  627. // 缓存句子,段落外壳与标题级别有关,不进缓存
  628. $key = self::paragraphCacheKey($book, $para, $channelUid, $format);
  629. $cached = RedisCache::remember(
  630. $key,
  631. config('mint.cache.expire'),
  632. function () use ($book, $para, $channelUid, $format) {
  633. return $this->renderReadSentences($book, $para, $channelUid, $format);
  634. }
  635. );
  636. $result = [
  637. 'para' => $para,
  638. 'display' => '',
  639. 'sentences' => $cached['sentences'],
  640. ];
  641. if (count($cached['display']) === 0) {
  642. return $result;
  643. }
  644. $pClass = PaliText::where('book', $book)
  645. ->where('paragraph', $para)
  646. ->value('class');
  647. if ($format === 'html') {
  648. // html 格式加段落外壳;段后追加段落脚注列表(本段义注/复注条目)
  649. $content = implode('', $cached['display']);
  650. $inner = $level > 0 ? "<h{$level}>{$content}</h{$level}>" : "<div class='para-block'>{$content}</div>";
  651. $footnoteList = $this->renderFootnoteList($cached['notes'] ?? []);
  652. $result['display'] = "<div id='para-{$para}' class='{$cached['area']} {$pClass}' data-para='{$para}'>{$inner}{$footnoteList}</div>";
  653. } else {
  654. // 其他格式一行一句
  655. $result['display'] = implode("\n", $cached['display']);
  656. }
  657. return $result;
  658. }
  659. /**
  660. * 段落是章节标题时返回标题级别,否则 0
  661. */
  662. public function paragraphLevel(int $book, int $para): int
  663. {
  664. $level = PaliText::where('book', $book)
  665. ->where('paragraph', $para)
  666. ->where('level', '<', 8)
  667. ->value('level');
  668. return $level ? (int) $level : 0;
  669. }
  670. /**
  671. * 阅读模式支持的全部格式。forgetParagraph 需要逐格式清除缓存 key。
  672. *
  673. * @var array<int, string>
  674. */
  675. private const FORMATS = ['html', 'markdown', 'text'];
  676. /**
  677. * 阅读格式 → App\Services\Markup 的输出格式。
  678. */
  679. private static function markupFormat(string $format): Format
  680. {
  681. return match ($format) {
  682. 'markdown' => Format::Markdown,
  683. 'text' => Format::Text,
  684. default => Format::Html,
  685. };
  686. }
  687. /**
  688. * 义注对应(commentary)批注最多渲染几句。
  689. * 一条批注可能挂着十几句义注,全铺进边注会把正文挤没,只取前几句。
  690. */
  691. private const MAX_COMMENTARY_SENTENCES = 3;
  692. /**
  693. * 段落阅读模式缓存的 key
  694. */
  695. public static function paragraphCacheKey(int $book, int $para, string $channelUid, string $format): string
  696. {
  697. // markup:App\Services\Markup 渲染的缓存,与 MdRender 时代的旧缓存分开(旧 key 自然过期)
  698. return "/read-para/markup/{$book}-{$para}/{$channelUid}/{$format}";
  699. }
  700. /**
  701. * 删除某个段落的阅读模式缓存。句子有增改删时调用,下次 readParagraph 自动重建。
  702. */
  703. public static function forgetParagraph(int $book, int $para, string $channelUid): void
  704. {
  705. foreach (self::FORMATS as $format) {
  706. RedisCache::forget(self::paragraphCacheKey($book, $para, $channelUid, $format));
  707. }
  708. }
  709. /**
  710. * 段落批注内嵌:把锚定到该句子的批注排进句子 content,再交给 App\Services\Markup 渲染。
  711. *
  712. * 四种批注,显示方式不同(排版规则与交叠处理见 {@see AnnotationLayout}):
  713. * - `commentary`(注释书对应):content 是义注 / 复注的句子模板 `{{book-para-start-end}}`,
  714. * 渲染成这几句的译文,作为 {{note}} 边注插在锚点末尾,并追加 <cite> 跳转出处;
  715. * - `note`(普通边注):content 就是注解正文(markdown),同样插成 {{note}},没有出处;
  716. * - `glossary`(外挂术语):锚点处的原文换成 `[[title#原文]]`,渲染成术语(title 是词头);
  717. * - `issue`:锚点处的原文包一层高亮,带 discussion id,阅读器点击看内容;
  718. * 没有位置的合成一个三角叹号图标放在句尾。纯文本 / markdown 里不显示。
  719. * - `assessment`(译文评估):句尾一个对勾图标,颜色按分数分档,带 discussion id,
  720. * 阅读器点击看分数与理由。一句只有一条(有并发残留时取最近更新的)。纯文本 / markdown 里不显示。
  721. * 同段里其它没有评估的句子由 {@see self::renderReadSentences()} 补灰色对勾。
  722. * issue / glossary 只注入未关闭的;批注与评估没有开关状态。
  723. *
  724. * 位置按 pos_* 定位,与 quote_exact 对不上(正文改过)时按摘录重新定位。
  725. *
  726. * 插入点写的是结构化标记({@see AnnotationSet})而不是 HTML:content 里留下占位符,渲染时变成
  727. * anno-issue / anno-issue-mark / anno-assessment / note 模板节点,任意格式都能输出。
  728. *
  729. * @return array{content: string, notes: list<array{fnId: string, noteHtml: string, citeHtml: string}>, assessed: bool, annotations: AnnotationSet}
  730. */
  731. protected function injectAnnotationNotes(Sentence $row, string $channelType, string $format = 'html'): array
  732. {
  733. $content = (string) $row->content;
  734. $annotations = new AnnotationSet;
  735. $result = ['content' => $content, 'notes' => [], 'assessed' => false, 'annotations' => $annotations];
  736. $records = Discussion::where('res_type', 'sentence')
  737. ->where('res_id', $row->uid)
  738. ->whereIn('type', Discussion::READING_TYPES)
  739. // 只注入话题本身;回复(类型跟随话题)不进阅读页
  740. ->whereNull('parent')
  741. ->where(fn ($q) => $q->whereIn('type', Discussion::STATELESS_TYPES)->orWhere('status', 'active'))
  742. ->orderBy('created_at')
  743. ->get();
  744. if ($records->isEmpty()) {
  745. return $result;
  746. }
  747. $len = mb_strlen($content, 'UTF-8');
  748. $locate = fn (Discussion $d) => AnnotationLayout::locate(
  749. $content, $d->pos_start, $d->pos_end, $d->quote_exact, $d->quote_prefix, $d->quote_suffix
  750. );
  751. $glossaries = [];
  752. $issues = [];
  753. $notes = [];
  754. $footnotes = [];
  755. $assessment = null;
  756. foreach ($records as $record) {
  757. if ($record->type === Discussion::TYPE_ASSESSMENT) {
  758. if ($record->score !== null && ($assessment === null || $record->updated_at >= $assessment['updated_at'])) {
  759. $assessment = ['id' => $record->id, 'score' => (int) $record->score, 'updated_at' => $record->updated_at];
  760. }
  761. continue;
  762. }
  763. if ($record->type === Discussion::TYPE_GLOSSARY) {
  764. $range = $locate($record);
  765. if ($range !== null) {
  766. $glossaries[] = ['id' => $record->id, 'start' => $range[0], 'end' => $range[1], 'title' => (string) $record->title];
  767. }
  768. continue;
  769. }
  770. if ($record->type === 'issue') {
  771. $range = $locate($record);
  772. $issues[] = ['id' => $record->id, 'start' => $range[0] ?? null, 'end' => $range[1] ?? null, 'severity' => $record->severity];
  773. continue;
  774. }
  775. $note = $this->renderAnnotationNote($record, $row, $channelType);
  776. if ($note === null) {
  777. continue;
  778. }
  779. // 插入点:锚点末尾;定位不到时沿用 pos_end,null / 越界都算句尾
  780. $range = $locate($record);
  781. $at = $range[1] ?? $record->pos_end;
  782. if ($at === null || $at < 0 || $at > $len) {
  783. $at = $len;
  784. }
  785. $notes[] = [
  786. 'id' => $record->id,
  787. 'at' => $at,
  788. 'order' => $note['order'],
  789. 'html' => $annotations->note($note['body'], $note['params'], $record->effectiveContentType()),
  790. ];
  791. $footnotes[$record->id] = [
  792. 'fnId' => 'fn-'.$record->id,
  793. 'noteHtml' => $note['noteHtml'],
  794. 'citeHtml' => $note['citeHtml'],
  795. ];
  796. }
  797. // 结构化注入:插入点写批注标记(不是 HTML),渲染时变成模板节点,各格式自己决定怎么显示
  798. $layout = AnnotationLayout::apply(
  799. $content,
  800. $glossaries,
  801. $issues,
  802. $notes,
  803. true,
  804. $assessment === null ? null : ['id' => $assessment['id'], 'score' => $assessment['score']],
  805. $annotations,
  806. );
  807. $result['content'] = $layout['content'];
  808. $result['notes'] = array_map(fn ($id) => $footnotes[$id], $layout['notes']);
  809. $result['assessed'] = $assessment !== null;
  810. Log::info('reading-annotations: inject', [
  811. 'sid' => "{$row->book_id}-{$row->paragraph}-{$row->word_start}-{$row->word_end}",
  812. 'count' => $records->count(),
  813. 'content' => $result['content'],
  814. ]);
  815. return $result;
  816. }
  817. /**
  818. * 把一条批注(commentary / note)整理成要插入正文的 note 模板(正文 + 参数)与段后脚注条目。
  819. * 不合格的(正文为空、commentary 不是句子模板)返回 null,不插角标也不进脚注列表。
  820. *
  821. * @return array{body: string, params: array<string, string>, noteHtml: string, citeHtml: string, order: array<int, int>}|null
  822. */
  823. protected function renderAnnotationNote(Discussion $note, Sentence $row, string $channelType): ?array
  824. {
  825. $noteContent = trim((string) $note->content);
  826. if ($noteContent === '') {
  827. return null;
  828. }
  829. $sents = [];
  830. if ($note->type === Discussion::TYPE_COMMENTARY) {
  831. // content 是一个或多个义注句子模板 {{book-para-start-end}}(一个词可能由义注
  832. // 多句解释,按顺序并列,如 {{135-404-50-54}}{{135-404-55-65}})。
  833. if (! preg_match(Discussion::COMMENTARY_PATTERN, $noteContent)) {
  834. return null;
  835. }
  836. preg_match_all('/\{\{(\d+)-(\d+)-(\d+)-(\d+)\}\}/', $noteContent, $sents, PREG_SET_ORDER);
  837. // 义注正文只需译文(不要巴利原文):把每个裸句模板 {{book-para-start-end}}
  838. // 转成 {{sent|id=…|text=translation}},让 sent 模板只输出 translation。
  839. // 句子可能有很多条,只渲染前 MAX_COMMENTARY_SENTENCES 句;
  840. // 跳转锚点仍指向第一句,点进去能看到完整义注。
  841. $source = implode(' ', array_map(
  842. fn ($s) => '{{sent|id='.$s[1].'-'.$s[2].'-'.$s[3].'-'.$s[4].'|text=translation}}',
  843. array_slice($sents, 0, self::MAX_COMMENTARY_SENTENCES)
  844. ));
  845. } else {
  846. // 普通边注:content 就是注解正文,照原样渲染。
  847. $source = $noteContent;
  848. }
  849. // 段后脚注列表用的纯文本(边注本身是结构化的 note 节点,正文照 markdown 渲染)
  850. $noteHtml = (string) app(MarkupService::class)->render($source, new RenderOptions(
  851. format: Format::Text,
  852. channels: [(string) $row->channel_uid],
  853. channelType: $channelType,
  854. contentType: $note->effectiveContentType(),
  855. ));
  856. // 义注被截断时补省略号,提示后面还有;想看全文点 <cite> 跳过去。
  857. if (count($sents) > self::MAX_COMMENTARY_SENTENCES) {
  858. $noteHtml = rtrim($noteHtml).'...';
  859. $source .= '...';
  860. }
  861. // 出处只有 commentary 有。跳转目标(义注书-段-起-止)同时挂在 <cite> 和角标
  862. // <label> 上,前者用于「点链接跳页」,后者用于平板双栏「点角标跨栏高亮」。
  863. // 多句时跳转到第一句(义注对这个词的解释从那里开始)。
  864. $citeHtml = '';
  865. $params = [];
  866. if ($sents) {
  867. $m = $sents[0];
  868. $target = ' data-book="'.$m[1].'" data-para="'.$m[2].'" data-start="'.$m[3].'" data-end="'.$m[4].'"';
  869. $citeHtml = '<cite class="anno-jump"'.$target.'>义注</cite>';
  870. $params = ['cite' => '义注', 'citelink' => $m[1].'-'.$m[2].'-'.$m[3].'-'.$m[4]];
  871. }
  872. return [
  873. // note 模板的正文(markdown)与参数:渲染成 tufte sidenote(label + input + span.sidenote)
  874. 'body' => $source,
  875. 'params' => $params,
  876. 'noteHtml' => $noteHtml,
  877. 'citeHtml' => $citeHtml,
  878. // 同一插入点上的先后:按义注原文的先后;普通 note 没有坐标,取 0,排在义注对应之前
  879. 'order' => $sents ? [(int) $sents[0][1], (int) $sents[0][2], (int) $sents[0][3]] : [0, 0, 0],
  880. ];
  881. }
  882. /**
  883. * 段落脚注列表:把本段的注释条目逐条列在段后,每条默认收起为可配置行数(见阅读器 CSS)。
  884. */
  885. protected function renderFootnoteList(array $notes): string
  886. {
  887. if (empty($notes)) {
  888. return '';
  889. }
  890. $html = '<div class="anno-footnotes">';
  891. foreach ($notes as $note) {
  892. $html .= '<div class="anno-footnote">'
  893. .'<input type="checkbox" id="'.$note['fnId'].'" class="anno-fn-toggle"/>'
  894. .'<label for="'.$note['fnId'].'" class="anno-fn-label">'
  895. .'<span class="anno-fn-body">'.e($note['noteHtml']).'</span>'
  896. .'</label>'
  897. .$note['citeHtml']
  898. .'</div>';
  899. }
  900. $html .= '</div>';
  901. return $html;
  902. }
  903. /**
  904. * 渲染段落里的句子。不含段落外壳,可直接缓存。
  905. *
  906. * @return array{area: string, display: array<int, string>, sentences: array<int, array{sid: string, html: string}>}
  907. */
  908. protected function renderReadSentences(int $book, int $para, string $channelUid, string $format): array
  909. {
  910. $result = ['area' => 'translation', 'display' => [], 'sentences' => []];
  911. $channel = Channel::where('uid', $channelUid)
  912. ->select(['uid', 'type', 'lang', 'name'])->first();
  913. if (! $channel) {
  914. return $result;
  915. }
  916. $isOrigin = $channel->type === 'original' || $channel->type === 'wbw';
  917. $channelType = $channel->type === 'nissaya' ? 'nissaya' : 'translation';
  918. $result['area'] = $isOrigin ? 'original' : 'translation';
  919. $records = Sentence::select($this->selectCol)
  920. ->where('book_id', $book)
  921. ->where('paragraph', $para)
  922. ->where('channel_uid', $channelUid)
  923. ->orderBy('word_start')
  924. ->get();
  925. $allNotes = [];
  926. $injections = $records->map(fn (Sentence $row) => $this->injectAnnotationNotes($row, $channelType, $format));
  927. // 段里有句子评估过,其余没评估的句子在句尾补灰色对勾:不然读者会把它与下一句的
  928. // 对勾连起来,误以为是一句。整段都没评估就都不补(段落本身有分隔)。
  929. $markUnassessed = $injections->contains('assessed', true);
  930. $sources = [];
  931. foreach ($records as $i => $row) {
  932. $injected = $injections[$i];
  933. $content = $injected['content'];
  934. if ($markUnassessed && ! $injected['assessed'] && trim($content) !== '') {
  935. $content .= $injected['annotations']->unassessedMark();
  936. }
  937. $allNotes = array_merge($allNotes, $injected['notes']);
  938. $sources[$i] = [
  939. 'source' => $content,
  940. 'channels' => [(string) $row->channel_uid],
  941. 'contentType' => (string) $row->content_type,
  942. 'annotations' => $injected['annotations'],
  943. // 边注的 HTML id 带上句子坐标,整页唯一
  944. 'idPrefix' => "{$row->book_id}-{$row->paragraph}-{$row->word_start}-{$row->word_end}",
  945. ];
  946. }
  947. // 整段一起渲染:各句里的术语、句子模板(含义注对应)合在一起批量查
  948. $rendered = $sources === [] ? [] : app(MarkupService::class)->renderMany(
  949. $sources,
  950. new RenderOptions(format: self::markupFormat($format), channelType: $channelType),
  951. );
  952. foreach ($records as $i => $row) {
  953. $html = $rendered[$i] ?? '';
  954. if (empty($html)) {
  955. continue;
  956. }
  957. $sid = "{$row->book_id}-{$row->paragraph}-{$row->word_start}-{$row->word_end}";
  958. $result['sentences'][] = ['sid' => $sid, 'html' => $html];
  959. if ($format === 'html') {
  960. $class = $isOrigin ? 'sentence origin' : 'sentence';
  961. $result['display'][] = "<div class='{$class}' lang='{$channel->lang}' data-sid='{$sid}'>{$html}</div>";
  962. } else {
  963. $result['display'][] = $html;
  964. }
  965. }
  966. $result['notes'] = $allNotes;
  967. return $result;
  968. }
  969. }