PaliContentService.php 40 KB

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