CorpusController.php 42 KB

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