CorpusController.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\MdRender;
  5. use App\Http\Api\StudioApi;
  6. use App\Http\Api\SuggestionApi;
  7. use App\Http\Api\UserApi;
  8. use App\Http\Resources\TocResource;
  9. use App\Models\Channel;
  10. use App\Models\CustomBook;
  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\AuthService;
  19. use Illuminate\Http\Request;
  20. use Illuminate\Http\Response;
  21. use Illuminate\Support\Arr;
  22. use Illuminate\Support\Facades\Cache;
  23. use Illuminate\Support\Facades\Log;
  24. use Illuminate\Support\Facades\Redis;
  25. use Illuminate\Support\Str;
  26. class CorpusController extends Controller
  27. {
  28. protected $result = [
  29. 'uid' => '',
  30. 'title' => '',
  31. 'path' => [],
  32. 'sub_title' => '',
  33. 'summary' => '',
  34. 'content' => '',
  35. 'content_type' => 'html',
  36. 'toc' => [],
  37. 'status' => 30,
  38. 'lang' => '',
  39. 'created_at' => '',
  40. 'updated_at' => '',
  41. ];
  42. protected $wbwChannels = [];
  43. // 句子需要查询的列
  44. protected $selectCol = [
  45. 'uid',
  46. 'book_id',
  47. 'paragraph',
  48. 'word_start',
  49. 'word_end',
  50. 'channel_uid',
  51. 'content',
  52. 'content_type',
  53. 'editor_uid',
  54. 'acceptor_uid',
  55. 'pr_edit_at',
  56. 'fork_at',
  57. 'create_time',
  58. 'modify_time',
  59. 'created_at',
  60. 'updated_at',
  61. ];
  62. protected $userUuid = null;
  63. protected $debug = [];
  64. public function __construct() {}
  65. /**
  66. * Display a listing of the resource.
  67. *
  68. * @return Response
  69. */
  70. public function index(Request $request)
  71. {
  72. //
  73. switch ($request->get('view')) {
  74. case 'para':
  75. return $this->showPara($request);
  76. break;
  77. default:
  78. // code...
  79. break;
  80. }
  81. }
  82. /**
  83. * Store a newly created resource in storage.
  84. *
  85. * @return Response
  86. */
  87. public function store(Request $request)
  88. {
  89. //
  90. }
  91. /**
  92. * Display the specified resource.
  93. *
  94. * @return Response
  95. */
  96. public function show(Sentence $sentence)
  97. {
  98. //
  99. }
  100. public function getSentTpl(
  101. string $id,
  102. array $inputChannels,
  103. $mode = 'edit',
  104. $onlyProps = false,
  105. $format = 'react'
  106. ) {
  107. $sent = [];
  108. $channels = $inputChannels;
  109. $sentId = \explode('-', $id);
  110. if (count($sentId) !== 4) {
  111. return false;
  112. }
  113. $bookId = (int) $sentId[0];
  114. if ($bookId < 1000) {
  115. if ($mode === 'read') {
  116. $originalChannelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  117. } else {
  118. $originalChannelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  119. }
  120. } else {
  121. $originalChannelId = CustomBook::where('book_id', $bookId)->value('channel_id');
  122. }
  123. if (isset($originalChannelId) && $originalChannelId) {
  124. array_push($channels, $originalChannelId);
  125. }
  126. $record = Sentence::select($this->selectCol)
  127. ->where('book_id', $sentId[0])
  128. ->where('paragraph', $sentId[1])
  129. ->where('word_start', (int) $sentId[2])
  130. ->where('word_end', (int) $sentId[3])
  131. ->whereIn('channel_uid', $channels)
  132. ->get();
  133. $channelIndex = $this->getChannelIndex($channels);
  134. if (isset($toSentFormat)) {
  135. foreach ($toSentFormat as $key => $org) {
  136. $record[] = $org;
  137. }
  138. }
  139. // 获取wbw channel
  140. // 目前默认的 wbw channel 是第一个translation channel
  141. foreach ($channels as $channel) {
  142. // code...
  143. if ($channelIndex[$channel]->type === 'translation') {
  144. $this->wbwChannels[] = $channel;
  145. break;
  146. }
  147. }
  148. return $this->makeContent($record, $mode, $channelIndex, [], $onlyProps, false, $format);
  149. }
  150. /**
  151. * Display the specified resource.
  152. *
  153. * @return Response
  154. */
  155. public function showSent(Request $request, string $id)
  156. {
  157. $user = AuthService::current($request);
  158. if ($user) {
  159. $this->userUuid = $user['user_uid'];
  160. }
  161. $channels = \explode('_', $request->get('channels'));
  162. $this->result['uid'] = '';
  163. $this->result['title'] = '';
  164. $this->result['subtitle'] = '';
  165. $this->result['summary'] = '';
  166. $this->result['lang'] = '';
  167. $this->result['status'] = 30;
  168. $this->result['content'] = $this->getSentTpl($id, $channels, $request->get('mode', 'edit'));
  169. return $this->ok($this->result);
  170. }
  171. /**
  172. * 获取某句子的全部译文
  173. *
  174. * @return Response
  175. */
  176. public function showSentences(Request $request, string $type, string $id)
  177. {
  178. $user = AuthService::current($request);
  179. if ($user) {
  180. $this->userUuid = $user['user_uid'];
  181. }
  182. $param = \explode('_', $id);
  183. $sentId = \explode('-', $param[0]);
  184. $channels = [];
  185. // 获取channel类型
  186. $sentChannel = Sentence::select('channel_uid')
  187. ->where('book_id', $sentId[0])
  188. ->where('paragraph', $sentId[1])
  189. ->where('word_start', $sentId[2])
  190. ->where('word_end', $sentId[3])
  191. ->get();
  192. foreach ($sentChannel as $key => $value) {
  193. // code...
  194. $channels[] = $value->channel_uid;
  195. }
  196. $channelInfo = Channel::whereIn('uid', $channels)->select(['uid', 'type', 'lang', 'name'])->get();
  197. $indexChannel = [];
  198. $channels = [];
  199. foreach ($channelInfo as $key => $value) {
  200. // code...
  201. if ($value->type === $type) {
  202. $indexChannel[$value->uid] = $value;
  203. $channels[] = $value->uid;
  204. }
  205. }
  206. // 获取句子数据
  207. $record = Sentence::select($this->selectCol)
  208. ->where('book_id', $sentId[0])
  209. ->where('paragraph', $sentId[1])
  210. ->where('word_start', $sentId[2])
  211. ->where('word_end', $sentId[3])
  212. ->whereIn('channel_uid', $channels)
  213. ->orderBy('paragraph')
  214. ->orderBy('word_start')
  215. ->get();
  216. if (count($record) === 0) {
  217. return $this->error('no data');
  218. }
  219. $this->result['uid'] = '';
  220. $this->result['title'] = '';
  221. $this->result['subtitle'] = '';
  222. $this->result['summary'] = '';
  223. $this->result['lang'] = '';
  224. $this->result['status'] = 30;
  225. $this->result['content'] = $this->makeContent($record, 'read', $indexChannel);
  226. // TODO 检查一下这个read为什么要写死
  227. return $this->ok($this->result);
  228. }
  229. /**
  230. * Store a newly created resource in storage.
  231. *
  232. * @param string $id
  233. * @param string $mode
  234. * @return Response
  235. */
  236. public function showPara(Request $request)
  237. {
  238. if ($request->has('debug')) {
  239. $this->debug = explode(',', $request->get('debug'));
  240. }
  241. $user = AuthService::current($request);
  242. if ($user) {
  243. $this->userUuid = $user['user_uid'];
  244. }
  245. //
  246. $channels = [];
  247. if ($request->get('mode') === 'edit') {
  248. // 翻译模式加载json格式原文
  249. $channels[] = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  250. } else {
  251. // 阅读模式加载html格式原文
  252. $channels[] = ChannelApi::getSysChannel('_System_Pali_VRI_');
  253. }
  254. if ($request->has('channels')) {
  255. if (strpos($request->get('channels'), ',') === false) {
  256. $getChannel = explode('_', $request->get('channels'));
  257. } else {
  258. $getChannel = explode(',', $request->get('channels'));
  259. }
  260. $channels = array_merge($channels, $getChannel);
  261. }
  262. $para = explode(',', $request->get('par'));
  263. // 段落所在章节
  264. $parent = PaliText::where('book', $request->get('book'))
  265. ->where('paragraph', $para[0])->first();
  266. $chapter = PaliText::where('book', $request->get('book'))
  267. ->where('paragraph', $parent->parent)->first();
  268. if ($chapter) {
  269. if (empty($chapter->toc)) {
  270. $this->result['title'] = 'unknown';
  271. } else {
  272. $this->result['title'] = $chapter->toc;
  273. $this->result['sub_title'] = $chapter->toc;
  274. $this->result['path'] = json_decode($parent->path);
  275. }
  276. }
  277. $paraFrom = $para[0];
  278. $paraTo = end($para);
  279. $indexedHeading = [];
  280. // 获取channel索引表
  281. $tranChannels = [];
  282. $channelInfo = Channel::whereIn('uid', $channels)
  283. ->select(['uid', 'type', 'lang', 'name'])->get();
  284. foreach ($channelInfo as $key => $value) {
  285. // code...
  286. if ($value->type === 'translation') {
  287. $tranChannels[] = $value->uid;
  288. }
  289. }
  290. $indexChannel = [];
  291. $indexChannel = $this->getChannelIndex($channels);
  292. // 获取wbw channel
  293. // 目前默认的 wbw channel 是第一个translation channel
  294. foreach ($channels as $key => $value) {
  295. // code...
  296. if (
  297. isset($indexChannel[$value]) &&
  298. $indexChannel[$value]->type === 'translation'
  299. ) {
  300. $this->wbwChannels[] = $value;
  301. break;
  302. }
  303. }
  304. // 章节译文标题
  305. $title = Sentence::select($this->selectCol)
  306. ->where('book_id', $parent->book)
  307. ->where('paragraph', $parent->parent)
  308. ->whereIn('channel_uid', $tranChannels)
  309. ->first();
  310. if ($title) {
  311. $this->result['title'] = MdRender::render($title->content, [$title->channel_uid]);
  312. }
  313. /**
  314. * 获取句子数据
  315. */
  316. $record = Sentence::select($this->selectCol)
  317. ->where('book_id', $request->get('book'))
  318. ->whereIn('paragraph', $para)
  319. ->whereIn('channel_uid', $channels)
  320. ->orderBy('paragraph')
  321. ->orderBy('word_start')
  322. ->get();
  323. if (count($record) === 0) {
  324. $this->result['content'] = '<span>No Data</span>';
  325. } else {
  326. $this->result['content'] = $this->makeContent($record, $request->get('mode', 'read'), $indexChannel, $indexedHeading, false, true);
  327. }
  328. return $this->ok($this->result);
  329. }
  330. /**
  331. * Store a newly created resource in storage.
  332. *
  333. * @return Response
  334. */
  335. public function showChapter(Request $request, string $id)
  336. {
  337. if ($request->has('debug')) {
  338. $this->debug = explode(',', $request->get('debug'));
  339. }
  340. $user = AuthService::current($request);
  341. if ($user) {
  342. $this->userUuid = $user['user_uid'];
  343. }
  344. //
  345. $sentId = \explode('-', $id);
  346. $channels = [];
  347. if ($request->has('channels')) {
  348. if (strpos($request->get('channels'), ',') === false) {
  349. $_channels = explode('_', $request->get('channels'));
  350. } else {
  351. $_channels = explode(',', $request->get('channels'));
  352. }
  353. foreach ($_channels as $key => $channel) {
  354. if (Str::isUuid($channel)) {
  355. $channels[] = $channel;
  356. }
  357. }
  358. }
  359. $mode = $request->get('mode', 'read');
  360. if ($mode === 'read') {
  361. // 阅读模式加载html格式原文
  362. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  363. } else {
  364. // 翻译模式加载json格式原文
  365. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  366. }
  367. if ($channelId !== false) {
  368. $channels[] = $channelId;
  369. }
  370. $chapter = PaliText::where('book', $sentId[0])->where('paragraph', $sentId[1])->first();
  371. if (! $chapter) {
  372. return $this->error('no data');
  373. }
  374. $paraFrom = $sentId[1];
  375. $paraTo = $sentId[1] + $chapter->chapter_len - 1;
  376. if (empty($chapter->toc)) {
  377. $this->result['title'] = 'unknown';
  378. } else {
  379. $this->result['title'] = $chapter->toc;
  380. $this->result['sub_title'] = $chapter->toc;
  381. $this->result['path'] = json_decode($chapter->path);
  382. }
  383. // 获取标题
  384. $heading = PaliText::select(['book', 'paragraph', 'level'])
  385. ->where('book', $sentId[0])
  386. ->whereBetween('paragraph', [$paraFrom, $paraTo])
  387. ->where('level', '<', 8)
  388. ->get();
  389. // 将标题段落转成索引数组 以便输出标题层级
  390. $indexedHeading = [];
  391. foreach ($heading as $key => $value) {
  392. // code...
  393. $indexedHeading["{$value->book}-{$value->paragraph}"] = $value->level;
  394. }
  395. // 获取channel索引表
  396. $tranChannels = [];
  397. $channelInfo = Channel::whereIn('uid', $channels)
  398. ->select(['uid', 'type', 'lang', 'name'])->get();
  399. foreach ($channelInfo as $key => $value) {
  400. // code...
  401. if ($value->type === 'translation') {
  402. $tranChannels[] = $value->uid;
  403. }
  404. }
  405. $indexChannel = [];
  406. $indexChannel = $this->getChannelIndex($channels);
  407. // 获取wbw channel
  408. // 目前默认的 wbw channel 是第一个translation channel
  409. // TODO 处理不存在的channel id
  410. foreach ($channels as $key => $value) {
  411. // code...
  412. if (
  413. isset($indexChannel[$value]) &&
  414. $indexChannel[$value]->type === 'translation'
  415. ) {
  416. $this->wbwChannels[] = $value;
  417. break;
  418. }
  419. }
  420. $title = Sentence::select($this->selectCol)
  421. ->where('book_id', $sentId[0])
  422. ->where('paragraph', $sentId[1])
  423. ->whereIn('channel_uid', $tranChannels)
  424. ->first();
  425. if ($title) {
  426. $this->result['title'] = MdRender::render($title->content, [$title->channel_uid]);
  427. $mdRender = new MdRender(['format' => 'simple']);
  428. $this->result['title_text'] = $mdRender->convert($title->content, [$title->channel_uid]);
  429. }
  430. /**
  431. * 获取句子数据
  432. * 算法:
  433. * 1. 如果标题和下一级第一个标题之间有段落。只输出这些段落和子目录
  434. * 2. 如果标题和下一级第一个标题之间没有间隔 且 chapter 长度大于10000个字符 且有子目录,只输出子目录
  435. * 3. 如果二者都不是,lazy load
  436. */
  437. // 1. 计算 标题和下一级第一个标题之间 是否有间隔
  438. $nextChapter = PaliText::where('book', $sentId[0])
  439. ->where('paragraph', '>', $sentId[1])
  440. ->where('level', '<', 8)
  441. ->orderBy('paragraph')
  442. ->value('paragraph');
  443. $between = $nextChapter - $sentId[1];
  444. // 查找子目录
  445. $chapterLen = $chapter->chapter_len;
  446. $toc = PaliText::where('book', $sentId[0])
  447. ->whereBetween('paragraph', [$paraFrom + 1, $paraFrom + $chapterLen - 1])
  448. ->where('level', '<', 8)
  449. ->orderBy('paragraph')
  450. ->select(['book', 'paragraph', 'level', 'toc'])
  451. ->get();
  452. $maxLen = 3000;
  453. if ($between > 1) {
  454. // 有间隔
  455. $paraTo = $nextChapter - 1;
  456. } else {
  457. if ($chapter->chapter_strlen > $maxLen) {
  458. if (count($toc) > 0) {
  459. // 有子目录只输出标题和目录
  460. $paraTo = $paraFrom;
  461. } else {
  462. // 没有子目录 全部输出
  463. }
  464. } else {
  465. // 章节小。全部输出 不输出子目录
  466. $toc = [];
  467. }
  468. }
  469. $pFrom = $request->get('from', $paraFrom);
  470. $pTo = $request->get('to', $paraTo);
  471. // 根据句子的长度找到这次应该加载的段落
  472. $paliText = PaliText::select(['paragraph', 'lenght'])
  473. ->where('book', $sentId[0])
  474. ->whereBetween('paragraph', [$pFrom, $pTo])
  475. ->orderBy('paragraph')
  476. ->get();
  477. $sumLen = 0;
  478. $currTo = $pTo;
  479. foreach ($paliText as $para) {
  480. $sumLen += $para->lenght;
  481. if ($sumLen > $maxLen) {
  482. $currTo = $para->paragraph;
  483. break;
  484. }
  485. }
  486. $record = Sentence::select($this->selectCol)
  487. ->where('book_id', $sentId[0])
  488. ->whereBetween('paragraph', [$pFrom, $currTo])
  489. ->whereIn('channel_uid', $channels)
  490. ->orderBy('paragraph')
  491. ->orderBy('word_start')
  492. ->get();
  493. if (count($record) === 0) {
  494. return $this->error('no data');
  495. }
  496. $this->result['content'] = $this->makeContent($record, $mode, $indexChannel, $indexedHeading, false, true);
  497. if (! $request->has('from')) {
  498. // 第一次才显示toc
  499. $this->result['toc'] = TocResource::collection($toc);
  500. }
  501. if ($currTo < $pTo) {
  502. $this->result['from'] = $currTo + 1;
  503. $this->result['to'] = $pTo;
  504. $this->result['paraId'] = $id;
  505. $this->result['channels'] = $request->get('channels');
  506. $this->result['mode'] = $request->get('mode');
  507. }
  508. return $this->ok($this->result);
  509. }
  510. private function getChannelIndex($channels, $type = null)
  511. {
  512. // 获取channel索引表
  513. $channelInfo = Channel::whereIn('uid', $channels)
  514. ->select(['uid', 'type', 'name', 'lang', 'owner_uid'])
  515. ->get();
  516. $indexChannel = [];
  517. foreach ($channels as $key => $channelId) {
  518. $channelInfo = Channel::where('uid', $channelId)
  519. ->select(['uid', 'type', 'name', 'lang', 'owner_uid'])->first();
  520. if (! $channelInfo) {
  521. Log::error('no channel id'.$channelId);
  522. continue;
  523. }
  524. if ($type !== null && $channelInfo->type !== $type) {
  525. continue;
  526. }
  527. $indexChannel[$channelId] = $channelInfo;
  528. $indexChannel[$channelId]->studio = StudioApi::getById($channelInfo->owner_uid);
  529. }
  530. return $indexChannel;
  531. }
  532. /**
  533. * 根据句子库数据生成文章内容
  534. * $record 句子数据
  535. * $mode read | edit | wbw
  536. * $indexChannel channel索引
  537. * $indexedHeading 标题索引 用于给段落加标题标签 <h1> ect.
  538. */
  539. private function makeContent($record, $mode, $indexChannel, $indexedHeading = [], $onlyProps = false, $paraMark = false, $format = 'react')
  540. {
  541. $content = [];
  542. $lastSent = '0-0';
  543. $sentCount = 0;
  544. $sent = [];
  545. $sent['origin'] = [];
  546. $sent['translation'] = [];
  547. $sent['commentaries'] = [];
  548. // 获取句子编号列表
  549. $sentList = [];
  550. foreach ($record as $key => $value) {
  551. $currSentId = "{$value->book_id}-{$value->paragraph}-{$value->word_start}-{$value->word_end}";
  552. $sentList[$currSentId] = [$value->book_id, $value->paragraph, $value->word_start, $value->word_end];
  553. $value->sid = "{$currSentId}_{$value->channel_uid}";
  554. }
  555. $channelsId = [];
  556. foreach ($indexChannel as $channelId => $info) {
  557. $channelsId[] = $channelId;
  558. }
  559. array_pop($channelsId);
  560. // 遍历列表查找每个句子的所有channel的数据,并填充
  561. $currPara = '';
  562. foreach ($sentList as $currSentId => $arrSentId) {
  563. $para = $arrSentId[0].'-'.$arrSentId[1];
  564. if ($currPara !== $para) {
  565. $currPara = $para;
  566. // 输出段落标记
  567. if ($paraMark) {
  568. $sentInPara = [];
  569. foreach ($sentList as $sentId => $sentParam) {
  570. if (
  571. $sentParam[0] === $arrSentId[0] &&
  572. $sentParam[1] === $arrSentId[1]
  573. ) {
  574. $sentInPara[] = $sentId;
  575. }
  576. }
  577. // 输出段落起始
  578. if (! empty($currPara)) {
  579. $content[] = '</MdTpl>';
  580. }
  581. $markProps = base64_encode(\json_encode([
  582. 'book' => $arrSentId[0],
  583. 'para' => $arrSentId[1],
  584. 'channels' => $channelsId,
  585. 'sentences' => $sentInPara,
  586. 'mode' => $mode,
  587. ]));
  588. $content[] = "<MdTpl tpl='para-shell' props='{$markProps}' >";
  589. }
  590. }
  591. $sent = $this->newSent($arrSentId[0], $arrSentId[1], $arrSentId[2], $arrSentId[3]);
  592. foreach ($indexChannel as $channelId => $info) {
  593. // code...
  594. $sid = "{$currSentId}_{$channelId}";
  595. if (isset($info->studio)) {
  596. $studioInfo = $info->studio;
  597. } else {
  598. $studioInfo = null;
  599. }
  600. $newSent = [
  601. 'content' => '',
  602. 'html' => '',
  603. 'book' => $arrSentId[0],
  604. 'para' => $arrSentId[1],
  605. 'wordStart' => $arrSentId[2],
  606. 'wordEnd' => $arrSentId[3],
  607. 'channel' => [
  608. 'name' => $info->name,
  609. 'type' => $info->type,
  610. 'id' => $info->uid,
  611. 'lang' => $info->lang,
  612. ],
  613. 'studio' => $studioInfo,
  614. 'updateAt' => '',
  615. 'suggestionCount' => SuggestionApi::getCountBySent($arrSentId[0], $arrSentId[1], $arrSentId[2], $arrSentId[3], $channelId),
  616. ];
  617. $row = Arr::first($record, function ($value, $key) use ($sid) {
  618. return $value->sid === $sid;
  619. });
  620. if ($row) {
  621. $newSent['id'] = $row->uid;
  622. $newSent['content'] = $row->content;
  623. $newSent['contentType'] = $row->content_type;
  624. $newSent['html'] = '';
  625. $newSent['editor'] = UserApi::getByUuid($row->editor_uid);
  626. /**
  627. * TODO 刷库改数据
  628. * 旧版api没有更新updated_at所以造成旧版的数据updated_at数据比modify_time 要晚
  629. */
  630. $newSent['forkAt'] = $row->fork_at; //
  631. $newSent['updateAt'] = $row->updated_at; //
  632. $newSent['updateAt'] = date('Y-m-d H:i:s.', $row->modify_time / 1000).($row->modify_time % 1000).' UTC';
  633. $newSent['createdAt'] = $row->created_at;
  634. if ($mode !== 'read') {
  635. if (isset($row->acceptor_uid) && ! empty($row->acceptor_uid)) {
  636. $newSent['acceptor'] = UserApi::getByUuid($row->acceptor_uid);
  637. $newSent['prEditAt'] = $row->pr_edit_at;
  638. }
  639. }
  640. switch ($info->type) {
  641. case 'wbw':
  642. case 'original':
  643. //
  644. // 在编辑模式下。
  645. // 如果是原文,查看是否有逐词解析数据,
  646. // 有的话优先显示。
  647. // 阅读模式直接显示html原文
  648. // 传过来的数据一定有一个原文channel
  649. //
  650. if ($mode === 'read') {
  651. $newSent['content'] = '';
  652. $newSent['html'] = MdRender::render(
  653. $row->content,
  654. [$row->channel_uid],
  655. null,
  656. $mode,
  657. 'translation',
  658. $row->content_type,
  659. $format
  660. );
  661. } else {
  662. if ($row->content_type === 'json') {
  663. $newSent['channel']['type'] = 'wbw';
  664. if (isset($this->wbwChannels[0])) {
  665. $newSent['channel']['name'] = $indexChannel[$this->wbwChannels[0]]->name;
  666. $newSent['channel']['lang'] = $indexChannel[$this->wbwChannels[0]]->lang;
  667. $newSent['channel']['id'] = $this->wbwChannels[0];
  668. // 存在一个translation channel
  669. // 尝试查找逐词解析数据。找到,替换现有数据
  670. $wbwData = $this->getWbw(
  671. $arrSentId[0],
  672. $arrSentId[1],
  673. $arrSentId[2],
  674. $arrSentId[3],
  675. $this->wbwChannels[0]
  676. );
  677. if ($wbwData) {
  678. $newSent['content'] = $wbwData;
  679. $newSent['contentType'] = 'json';
  680. $newSent['html'] = '';
  681. $newSent['studio'] = $indexChannel[$this->wbwChannels[0]]->studio;
  682. }
  683. }
  684. } else {
  685. $newSent['content'] = $row->content;
  686. $newSent['html'] = MdRender::render(
  687. $row->content,
  688. [$row->channel_uid],
  689. null,
  690. $mode,
  691. 'translation',
  692. $row->content_type,
  693. $format
  694. );
  695. }
  696. }
  697. break;
  698. case 'nissaya':
  699. $newSent['html'] = Cache::remember(
  700. "/sent/{$channelId}/{$currSentId}/{$format}",
  701. config('mint.cache.expire'),
  702. function () use ($row, $mode, $format) {
  703. return MdRender::render(
  704. $row->content,
  705. [$row->channel_uid],
  706. null,
  707. $mode,
  708. 'nissaya',
  709. $row->content_type,
  710. $format
  711. );
  712. }
  713. );
  714. break;
  715. case 'commentary':
  716. $options = [
  717. 'debug' => $this->debug,
  718. 'format' => $format,
  719. 'mode' => $mode,
  720. 'channelType' => 'translation',
  721. 'contentType' => $row->content_type,
  722. ];
  723. $mdRender = new MdRender($options);
  724. $newSent['html'] = $mdRender->convert($row->content, $channelsId);
  725. break;
  726. default:
  727. $options = [
  728. 'debug' => $this->debug,
  729. 'format' => $format,
  730. 'mode' => $mode,
  731. 'channelType' => 'translation',
  732. 'contentType' => $row->content_type,
  733. ];
  734. $mdRender = new MdRender($options);
  735. $newSent['html'] = $mdRender->convert($row->content, [$row->channel_uid]);
  736. // Log::debug('md render', ['content' => $row->content, 'options' => $options, 'render' => $newSent['html']]);
  737. break;
  738. }
  739. }
  740. switch ($info->type) {
  741. case 'wbw':
  742. case 'original':
  743. array_push($sent['origin'], $newSent);
  744. break;
  745. case 'commentary':
  746. array_push($sent['commentaries'], $newSent);
  747. break;
  748. default:
  749. array_push($sent['translation'], $newSent);
  750. break;
  751. }
  752. }
  753. if ($onlyProps) {
  754. return $sent;
  755. }
  756. $content = $this->pushSent($content, $sent, 0, $mode);
  757. }
  758. if ($paraMark) {
  759. $content[] = '</MdTpl>';
  760. }
  761. $output = \implode('', $content);
  762. return "<div>{$output}</div>";
  763. }
  764. public function getWbw($book, $para, $start, $end, $channel)
  765. {
  766. /**
  767. * 非阅读模式下。原文使用逐词解析数据。
  768. * 优先加载第一个translation channel 如果没有。加载默认逐词解析。
  769. */
  770. // 获取逐词解析数据
  771. $wbwBlock = WbwBlock::where('channel_uid', $channel)
  772. ->where('book_id', $book)
  773. ->where('paragraph', $para)
  774. ->select('uid')
  775. ->first();
  776. if (! $wbwBlock) {
  777. return false;
  778. }
  779. // 找到逐词解析数据
  780. $wbwData = Wbw::where('block_uid', $wbwBlock->uid)
  781. ->whereBetween('wid', [$start, $end])
  782. ->select(['book_id', 'paragraph', 'wid', 'data', 'uid', 'editor_id', 'created_at', 'updated_at'])
  783. ->orderBy('wid')
  784. ->get();
  785. $wbwContent = [];
  786. foreach ($wbwData as $wbwrow) {
  787. $wbw = str_replace('&nbsp;', ' ', $wbwrow->data);
  788. $wbw = str_replace('<br>', ' ', $wbw);
  789. $xmlString = '<root>'.$wbw.'</root>';
  790. try {
  791. $xmlWord = simplexml_load_string($xmlString);
  792. $wordsList = $xmlWord->xpath('//word');
  793. } catch (\Exception $e) {
  794. Log::error('corpus getWbw', ['error' => $e, 'data' => $xmlString]);
  795. return false;
  796. }
  797. foreach ($wordsList as $word) {
  798. $case = \str_replace(['#', '.'], ['$', ''], $word->case->__toString());
  799. $case = \str_replace('$$', '$', $case);
  800. $case = trim($case);
  801. $case = trim($case, '$');
  802. $wbwId = explode('-', $word->id->__toString());
  803. $wbwData = [
  804. 'uid' => $wbwrow->uid,
  805. 'book' => $wbwrow->book_id,
  806. 'para' => $wbwrow->paragraph,
  807. 'sn' => array_slice($wbwId, 2),
  808. 'word' => ['value' => $word->pali->__toString(), 'status' => 0],
  809. 'real' => ['value' => $word->real->__toString(), 'status' => 0],
  810. 'meaning' => ['value' => $word->mean->__toString(), 'status' => 0],
  811. 'type' => ['value' => $word->type->__toString(), 'status' => 0],
  812. 'grammar' => ['value' => $word->gramma->__toString(), 'status' => 0],
  813. 'case' => ['value' => $word->case->__toString(), 'status' => 0],
  814. 'parent' => ['value' => $word->parent->__toString(), 'status' => 0],
  815. 'style' => ['value' => $word->style->__toString(), 'status' => 0],
  816. 'factors' => ['value' => $word->org->__toString(), 'status' => 0],
  817. 'factorMeaning' => ['value' => $word->om->__toString(), 'status' => 0],
  818. 'confidence' => $word->cf->__toString(),
  819. 'created_at' => $wbwrow->created_at,
  820. 'updated_at' => $wbwrow->updated_at,
  821. 'hasComment' => Discussion::where('res_id', $wbwrow->uid)->exists(),
  822. ];
  823. if (isset($word->parent2)) {
  824. $wbwData['parent2']['value'] = $word->parent2->__toString();
  825. if (isset($word->parent2['status'])) {
  826. $wbwData['parent2']['status'] = (int) $word->parent2['status'];
  827. } else {
  828. $wbwData['parent2']['status'] = 0;
  829. }
  830. }
  831. if (isset($word->pg)) {
  832. $wbwData['grammar2']['value'] = $word->pg->__toString();
  833. if (isset($word->pg['status'])) {
  834. $wbwData['grammar2']['status'] = (int) $word->pg['status'];
  835. } else {
  836. $wbwData['grammar2']['status'] = 0;
  837. }
  838. }
  839. if (isset($word->rela)) {
  840. $wbwData['relation']['value'] = $word->rela->__toString();
  841. if (isset($word->rela['status'])) {
  842. $wbwData['relation']['status'] = (int) $word->rela['status'];
  843. } else {
  844. $wbwData['relation']['status'] = 7;
  845. }
  846. }
  847. if (isset($word->bmt)) {
  848. $wbwData['bookMarkText']['value'] = $word->bmt->__toString();
  849. if (isset($word->bmt['status'])) {
  850. $wbwData['bookMarkText']['status'] = (int) $word->bmt['status'];
  851. } else {
  852. $wbwData['bookMarkText']['status'] = 7;
  853. }
  854. }
  855. if (isset($word->bmc)) {
  856. $wbwData['bookMarkColor']['value'] = $word->bmc->__toString();
  857. if (isset($word->bmc['status'])) {
  858. $wbwData['bookMarkColor']['status'] = (int) $word->bmc['status'];
  859. } else {
  860. $wbwData['bookMarkColor']['status'] = 7;
  861. }
  862. }
  863. if (isset($word->note)) {
  864. $wbwData['note']['value'] = $word->note->__toString();
  865. if (isset($word->note['status'])) {
  866. $wbwData['note']['status'] = (int) $word->note['status'];
  867. } else {
  868. $wbwData['note']['status'] = 7;
  869. }
  870. }
  871. if (isset($word->cf)) {
  872. $wbwData['confidence'] = (float) $word->cf->__toString();
  873. }
  874. if (isset($word->attachments)) {
  875. $wbwData['attachments'] = json_decode($word->attachments->__toString());
  876. }
  877. if (isset($word->pali['status'])) {
  878. $wbwData['word']['status'] = (int) $word->pali['status'];
  879. }
  880. if (isset($word->real['status'])) {
  881. $wbwData['real']['status'] = (int) $word->real['status'];
  882. }
  883. if (isset($word->mean['status'])) {
  884. $wbwData['meaning']['status'] = (int) $word->mean['status'];
  885. }
  886. if (isset($word->type['status'])) {
  887. $wbwData['type']['status'] = (int) $word->type['status'];
  888. }
  889. if (isset($word->gramma['status'])) {
  890. $wbwData['grammar']['status'] = (int) $word->gramma['status'];
  891. }
  892. if (isset($word->case['status'])) {
  893. $wbwData['case']['status'] = (int) $word->case['status'];
  894. }
  895. if (isset($word->parent['status'])) {
  896. $wbwData['parent']['status'] = (int) $word->parent['status'];
  897. }
  898. if (isset($word->org['status'])) {
  899. $wbwData['factors']['status'] = (int) $word->org['status'];
  900. }
  901. if (isset($word->om['status'])) {
  902. $wbwData['factorMeaning']['status'] = (int) $word->om['status'];
  903. }
  904. $wbwContent[] = $wbwData;
  905. }
  906. }
  907. if (count($wbwContent) === 0) {
  908. return false;
  909. }
  910. return \json_encode($wbwContent, JSON_UNESCAPED_UNICODE);
  911. }
  912. /**
  913. * 将句子放进结果列表
  914. */
  915. private function pushSent($result, $sent, $level = 0, $mode = 'read')
  916. {
  917. $sentProps = base64_encode(\json_encode($sent));
  918. if ($mode === 'read') {
  919. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' ></MdTpl>";
  920. } else {
  921. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' ></MdTpl>";
  922. }
  923. // 增加标题的html标记
  924. if ($level > 0) {
  925. $sentWidget = "<h{$level}>".$sentWidget."</h{$level}>";
  926. }
  927. array_push($result, $sentWidget);
  928. return $result;
  929. }
  930. private function newSent($book, $para, $word_start, $word_end)
  931. {
  932. $sent = [
  933. 'id' => "{$book}-{$para}-{$word_start}-{$word_end}",
  934. 'book' => $book,
  935. 'para' => $para,
  936. 'wordStart' => $word_start,
  937. 'wordEnd' => $word_end,
  938. 'origin' => [],
  939. 'translation' => [],
  940. 'commentaries' => [],
  941. ];
  942. if ($book < 1000) {
  943. // 生成channel 数量列表
  944. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  945. $channelCount = CorpusController::_sentCanReadCount($book, $para, $word_start, $word_end, $this->userUuid);
  946. $path = json_decode(PaliText::where('book', $book)->where('paragraph', $para)->value('path'), true);
  947. $sent['path'] = [];
  948. foreach ($path as $key => $value) {
  949. // code...
  950. $value['paliTitle'] = $value['title'];
  951. $sent['path'][] = $value;
  952. }
  953. $sent['tranNum'] = $channelCount['tranNum'];
  954. $sent['nissayaNum'] = $channelCount['nissayaNum'];
  955. $sent['commNum'] = $channelCount['commNum'];
  956. $sent['originNum'] = $channelCount['originNum'];
  957. $sent['simNum'] = $channelCount['simNum'];
  958. }
  959. return $sent;
  960. }
  961. public static function _sentCanReadCount($book, $para, $start, $end, $userUuid = null)
  962. {
  963. $keyCanRead = '/channel/can-read/';
  964. if ($userUuid) {
  965. $keyCanRead .= $userUuid;
  966. } else {
  967. $keyCanRead .= 'guest';
  968. }
  969. $channelCanRead = Cache::remember(
  970. $keyCanRead,
  971. config('mint.cache.expire'),
  972. function () use ($userUuid) {
  973. return ChannelApi::getCanReadByUser($userUuid);
  974. }
  975. );
  976. $channels = Sentence::where('book_id', $book)
  977. ->where('paragraph', $para)
  978. ->where('word_start', $start)
  979. ->where('word_end', $end)
  980. ->where('strlen', '<>', 0)
  981. ->whereIn('channel_uid', $channelCanRead)
  982. ->select('channel_uid')
  983. ->groupBy('channel_uid')
  984. ->get();
  985. $channelList = [];
  986. foreach ($channels as $key => $value) {
  987. // code...
  988. if (Str::isUuid($value->channel_uid)) {
  989. $channelList[] = $value->channel_uid;
  990. }
  991. }
  992. $simId = PaliSentence::where('book', $book)
  993. ->where('paragraph', $para)
  994. ->where('word_begin', $start)
  995. ->where('word_end', $end)
  996. ->value('id');
  997. if ($simId) {
  998. $output['simNum'] = SentSimIndex::where('sent_id', $simId)->value('count');
  999. } else {
  1000. $output['simNum'] = 0;
  1001. }
  1002. $channelInfo = Channel::whereIn('uid', $channelList)->select('type')->get();
  1003. $output['tranNum'] = 0;
  1004. $output['nissayaNum'] = 0;
  1005. $output['commNum'] = 0;
  1006. $output['originNum'] = 0;
  1007. foreach ($channelInfo as $key => $value) {
  1008. // code...
  1009. switch ($value->type) {
  1010. case 'translation':
  1011. $output['tranNum']++;
  1012. break;
  1013. case 'nissaya':
  1014. $output['nissayaNum']++;
  1015. break;
  1016. case 'commentary':
  1017. $output['commNum']++;
  1018. break;
  1019. case 'original':
  1020. $output['originNum']++;
  1021. break;
  1022. }
  1023. }
  1024. return $output;
  1025. }
  1026. /**
  1027. * 获取某个句子的相关资源的句子数量
  1028. */
  1029. public static function sentCanReadCount($book, $para, $start, $end, $userUuid = null)
  1030. {
  1031. $sentId = "{$book}-{$para}-{$start}-{$end}";
  1032. $hKey = "/sentence/res-count/{$sentId}/";
  1033. if ($userUuid) {
  1034. $key = $userUuid;
  1035. } else {
  1036. $key = 'guest';
  1037. }
  1038. if (Redis::hExists($hKey, $key)) {
  1039. return json_decode(Redis::hGet($hKey, $key), true);
  1040. } else {
  1041. $channelCount = CorpusController::_sentCanReadCount($book, $para, $start, $end, $userUuid);
  1042. Redis::hSet($hKey, $key, json_encode($channelCount));
  1043. return $channelCount;
  1044. }
  1045. }
  1046. private function markdownRender($input) {}
  1047. /**
  1048. * Update the specified resource in storage.
  1049. *
  1050. * @return Response
  1051. */
  1052. public function update(Request $request, Sentence $sentence)
  1053. {
  1054. //
  1055. }
  1056. /**
  1057. * Remove the specified resource from storage.
  1058. *
  1059. * @return Response
  1060. */
  1061. public function destroy(Sentence $sentence)
  1062. {
  1063. //
  1064. }
  1065. }