CorpusController.php 54 KB

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