CorpusController.php 55 KB

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