CorpusController.php 42 KB

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