CorpusController.php 54 KB

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