CorpusController.php 43 KB

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