CorpusController.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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'] = $this->makeContent($record, $mode, $indexChannel, $indexedHeading, false, true);
  500. if (!$request->has('from')) {
  501. //第一次才显示toc
  502. $this->result['toc'] = TocResource::collection($toc);
  503. }
  504. if ($currTo < $pTo) {
  505. $this->result['from'] = $currTo + 1;
  506. $this->result['to'] = $pTo;
  507. $this->result['paraId'] = $id;
  508. $this->result['channels'] = $request->get('channels');
  509. $this->result['mode'] = $request->get('mode');
  510. }
  511. return $this->ok($this->result);
  512. }
  513. private function getChannelIndex($channels, $type = null)
  514. {
  515. #获取channel索引表
  516. $channelInfo = Channel::whereIn("uid", $channels)
  517. ->select(['uid', 'type', 'name', 'lang', 'owner_uid'])
  518. ->get();
  519. $indexChannel = [];
  520. foreach ($channels as $key => $channelId) {
  521. $channelInfo = Channel::where("uid", $channelId)
  522. ->select(['uid', 'type', 'name', 'lang', 'owner_uid'])->first();
  523. if (!$channelInfo) {
  524. Log::error('no channel id' . $channelId);
  525. continue;
  526. }
  527. if ($type !== null && $channelInfo->type !== $type) {
  528. continue;
  529. }
  530. $indexChannel[$channelId] = $channelInfo;
  531. $indexChannel[$channelId]->studio = StudioApi::getById($channelInfo->owner_uid);
  532. }
  533. return $indexChannel;
  534. }
  535. /**
  536. * 根据句子库数据生成文章内容
  537. * $record 句子数据
  538. * $mode read | edit | wbw
  539. * $indexChannel channel索引
  540. * $indexedHeading 标题索引 用于给段落加标题标签 <h1> ect.
  541. */
  542. private function makeContent($record, $mode, $indexChannel, $indexedHeading = [], $onlyProps = false, $paraMark = false, $format = 'react')
  543. {
  544. $content = [];
  545. $lastSent = "0-0";
  546. $sentCount = 0;
  547. $sent = [];
  548. $sent["origin"] = [];
  549. $sent["translation"] = [];
  550. $sent["commentaries"] = [];
  551. //获取句子编号列表
  552. $sentList = [];
  553. foreach ($record as $key => $value) {
  554. $currSentId = "{$value->book_id}-{$value->paragraph}-{$value->word_start}-{$value->word_end}";
  555. $sentList[$currSentId] = [$value->book_id, $value->paragraph, $value->word_start, $value->word_end];
  556. $value->sid = "{$currSentId}_{$value->channel_uid}";
  557. }
  558. $channelsId = array();
  559. foreach ($indexChannel as $channelId => $info) {
  560. $channelsId[] = $channelId;
  561. }
  562. array_pop($channelsId);
  563. //遍历列表查找每个句子的所有channel的数据,并填充
  564. $currPara = "";
  565. foreach ($sentList as $currSentId => $arrSentId) {
  566. $para = $arrSentId[0] . "-" . $arrSentId[1];
  567. if ($currPara !== $para) {
  568. $currPara = $para;
  569. //输出段落标记
  570. if ($paraMark) {
  571. $sentInPara = array();
  572. foreach ($sentList as $sentId => $sentParam) {
  573. if (
  574. $sentParam[0] === $arrSentId[0] &&
  575. $sentParam[1] === $arrSentId[1]
  576. ) {
  577. $sentInPara[] = $sentId;
  578. }
  579. }
  580. //输出段落起始
  581. if (!empty($currPara)) {
  582. $content[] = '</MdTpl>';
  583. }
  584. $markProps = base64_encode(\json_encode([
  585. 'book' => $arrSentId[0],
  586. 'para' => $arrSentId[1],
  587. 'channels' => $channelsId,
  588. 'sentences' => $sentInPara,
  589. 'mode' => $mode,
  590. ]));
  591. $content[] = "<MdTpl tpl='para-shell' props='{$markProps}' >";
  592. }
  593. }
  594. $sent = $this->newSent($arrSentId[0], $arrSentId[1], $arrSentId[2], $arrSentId[3]);
  595. foreach ($indexChannel as $channelId => $info) {
  596. # code...
  597. $sid = "{$currSentId}_{$channelId}";
  598. if (isset($info->studio)) {
  599. $studioInfo = $info->studio;
  600. } else {
  601. $studioInfo = null;
  602. }
  603. $newSent = [
  604. "content" => "",
  605. "html" => "",
  606. "book" => $arrSentId[0],
  607. "para" => $arrSentId[1],
  608. "wordStart" => $arrSentId[2],
  609. "wordEnd" => $arrSentId[3],
  610. "channel" => [
  611. "name" => $info->name,
  612. "type" => $info->type,
  613. "id" => $info->uid,
  614. 'lang' => $info->lang,
  615. ],
  616. "studio" => $studioInfo,
  617. "updateAt" => "",
  618. "suggestionCount" => SuggestionApi::getCountBySent($arrSentId[0], $arrSentId[1], $arrSentId[2], $arrSentId[3], $channelId),
  619. ];
  620. $row = Arr::first($record, function ($value, $key) use ($sid) {
  621. return $value->sid === $sid;
  622. });
  623. if ($row) {
  624. $newSent['id'] = $row->uid;
  625. $newSent['content'] = $row->content;
  626. $newSent['contentType'] = $row->content_type;
  627. $newSent['html'] = '';
  628. $newSent["editor"] = UserApi::getByUuid($row->editor_uid);
  629. /**
  630. * TODO 刷库改数据
  631. * 旧版api没有更新updated_at所以造成旧版的数据updated_at数据比modify_time 要晚
  632. */
  633. $newSent['forkAt'] = $row->fork_at; //
  634. $newSent['updateAt'] = $row->updated_at; //
  635. $newSent['updateAt'] = date("Y-m-d H:i:s.", $row->modify_time / 1000) . ($row->modify_time % 1000) . " UTC";
  636. $newSent['createdAt'] = $row->created_at;
  637. if ($mode !== "read") {
  638. if (isset($row->acceptor_uid) && !empty($row->acceptor_uid)) {
  639. $newSent["acceptor"] = UserApi::getByUuid($row->acceptor_uid);
  640. $newSent["prEditAt"] = $row->pr_edit_at;
  641. }
  642. }
  643. switch ($info->type) {
  644. case 'wbw':
  645. case 'original':
  646. //
  647. // 在编辑模式下。
  648. // 如果是原文,查看是否有逐词解析数据,
  649. // 有的话优先显示。
  650. // 阅读模式直接显示html原文
  651. // 传过来的数据一定有一个原文channel
  652. //
  653. if ($mode === "read") {
  654. $newSent['content'] = "";
  655. $newSent['html'] = MdRender::render(
  656. $row->content,
  657. [$row->channel_uid],
  658. null,
  659. $mode,
  660. "translation",
  661. $row->content_type,
  662. $format
  663. );
  664. } else {
  665. if ($row->content_type === 'json') {
  666. $newSent['channel']['type'] = "wbw";
  667. if (isset($this->wbwChannels[0])) {
  668. $newSent['channel']['name'] = $indexChannel[$this->wbwChannels[0]]->name;
  669. $newSent['channel']['lang'] = $indexChannel[$this->wbwChannels[0]]->lang;
  670. $newSent['channel']['id'] = $this->wbwChannels[0];
  671. //存在一个translation channel
  672. //尝试查找逐词解析数据。找到,替换现有数据
  673. $wbwData = $this->getWbw(
  674. $arrSentId[0],
  675. $arrSentId[1],
  676. $arrSentId[2],
  677. $arrSentId[3],
  678. $this->wbwChannels[0]
  679. );
  680. if ($wbwData) {
  681. $newSent['content'] = $wbwData;
  682. $newSent['contentType'] = 'json';
  683. $newSent['html'] = "";
  684. $newSent['studio'] = $indexChannel[$this->wbwChannels[0]]->studio;
  685. }
  686. }
  687. } else {
  688. $newSent['content'] = $row->content;
  689. $newSent['html'] = MdRender::render(
  690. $row->content,
  691. [$row->channel_uid],
  692. null,
  693. $mode,
  694. "translation",
  695. $row->content_type,
  696. $format
  697. );
  698. }
  699. }
  700. break;
  701. case 'nissaya':
  702. $newSent['html'] = Cache::remember(
  703. "/sent/{$channelId}/{$currSentId}/{$format}",
  704. config('mint.cache.expire'),
  705. function () use ($row, $mode, $format) {
  706. if ($row->content_type === 'markdown') {
  707. return MdRender::render(
  708. $row->content,
  709. [$row->channel_uid],
  710. null,
  711. $mode,
  712. "nissaya",
  713. $row->content_type,
  714. $format
  715. );
  716. } else {
  717. return null;
  718. }
  719. }
  720. );
  721. break;
  722. case 'commentary':
  723. $options = [
  724. 'debug' => $this->debug,
  725. 'format' => $format,
  726. 'mode' => $mode,
  727. 'channelType' => 'translation',
  728. 'contentType' => $row->content_type,
  729. ];
  730. $mdRender = new MdRender($options);
  731. $newSent['html'] = $mdRender->convert($row->content, $channelsId);
  732. break;
  733. default:
  734. $options = [
  735. 'debug' => $this->debug,
  736. 'format' => $format,
  737. 'mode' => $mode,
  738. 'channelType' => 'translation',
  739. 'contentType' => $row->content_type,
  740. ];
  741. $mdRender = new MdRender($options);
  742. $newSent['html'] = $mdRender->convert($row->content, [$row->channel_uid]);
  743. //Log::debug('md render', ['content' => $row->content, 'options' => $options, 'render' => $newSent['html']]);
  744. break;
  745. }
  746. }
  747. switch ($info->type) {
  748. case 'wbw':
  749. case 'original':
  750. array_push($sent["origin"], $newSent);
  751. break;
  752. case 'commentary':
  753. array_push($sent["commentaries"], $newSent);
  754. break;
  755. default:
  756. array_push($sent["translation"], $newSent);
  757. break;
  758. }
  759. }
  760. if ($onlyProps) {
  761. return $sent;
  762. }
  763. $content = $this->pushSent($content, $sent, 0, $mode);
  764. }
  765. if ($paraMark) {
  766. $content[] = '</MdTpl>';
  767. }
  768. $output = \implode("", $content);
  769. return "<div>{$output}</div>";
  770. }
  771. public function getWbw($book, $para, $start, $end, $channel)
  772. {
  773. /**
  774. * 非阅读模式下。原文使用逐词解析数据。
  775. * 优先加载第一个translation channel 如果没有。加载默认逐词解析。
  776. */
  777. //获取逐词解析数据
  778. $wbwBlock = WbwBlock::where('channel_uid', $channel)
  779. ->where('book_id', $book)
  780. ->where('paragraph', $para)
  781. ->select('uid')
  782. ->first();
  783. if (!$wbwBlock) {
  784. return false;
  785. }
  786. //找到逐词解析数据
  787. $wbwData = Wbw::where('block_uid', $wbwBlock->uid)
  788. ->whereBetween('wid', [$start, $end])
  789. ->select(['book_id', 'paragraph', 'wid', 'data', 'uid', 'editor_id', 'created_at', 'updated_at'])
  790. ->orderBy('wid')
  791. ->get();
  792. $wbwContent = [];
  793. foreach ($wbwData as $wbwrow) {
  794. $wbw = str_replace("&nbsp;", ' ', $wbwrow->data);
  795. $wbw = str_replace("<br>", ' ', $wbw);
  796. $xmlString = "<root>" . $wbw . "</root>";
  797. try {
  798. $xmlWord = simplexml_load_string($xmlString);
  799. } catch (\Exception $e) {
  800. Log::error('corpus', ['error' => $e]);
  801. continue;
  802. }
  803. $wordsList = $xmlWord->xpath('//word');
  804. foreach ($wordsList as $word) {
  805. $case = \str_replace(['#', '.'], ['$', ''], $word->case->__toString());
  806. $case = \str_replace('$$', '$', $case);
  807. $case = trim($case);
  808. $case = trim($case, "$");
  809. $wbwId = explode('-', $word->id->__toString());
  810. $wbwData = [
  811. 'uid' => $wbwrow->uid,
  812. 'book' => $wbwrow->book_id,
  813. 'para' => $wbwrow->paragraph,
  814. 'sn' => array_slice($wbwId, 2),
  815. 'word' => ['value' => $word->pali->__toString(), 'status' => 0],
  816. 'real' => ['value' => $word->real->__toString(), 'status' => 0],
  817. 'meaning' => ['value' => $word->mean->__toString(), 'status' => 0],
  818. 'type' => ['value' => $word->type->__toString(), 'status' => 0],
  819. 'grammar' => ['value' => $word->gramma->__toString(), 'status' => 0],
  820. 'case' => ['value' => $word->case->__toString(), 'status' => 0],
  821. 'parent' => ['value' => $word->parent->__toString(), 'status' => 0],
  822. 'style' => ['value' => $word->style->__toString(), 'status' => 0],
  823. 'factors' => ['value' => $word->org->__toString(), 'status' => 0],
  824. 'factorMeaning' => ['value' => $word->om->__toString(), 'status' => 0],
  825. 'confidence' => $word->cf->__toString(),
  826. 'created_at' => $wbwrow->created_at,
  827. 'updated_at' => $wbwrow->updated_at,
  828. 'hasComment' => Discussion::where('res_id', $wbwrow->uid)->exists(),
  829. ];
  830. if (isset($word->parent2)) {
  831. $wbwData['parent2']['value'] = $word->parent2->__toString();
  832. if (isset($word->parent2['status'])) {
  833. $wbwData['parent2']['status'] = (int)$word->parent2['status'];
  834. } else {
  835. $wbwData['parent2']['status'] = 0;
  836. }
  837. }
  838. if (isset($word->pg)) {
  839. $wbwData['grammar2']['value'] = $word->pg->__toString();
  840. if (isset($word->pg['status'])) {
  841. $wbwData['grammar2']['status'] = (int)$word->pg['status'];
  842. } else {
  843. $wbwData['grammar2']['status'] = 0;
  844. }
  845. }
  846. if (isset($word->rela)) {
  847. $wbwData['relation']['value'] = $word->rela->__toString();
  848. if (isset($word->rela['status'])) {
  849. $wbwData['relation']['status'] = (int)$word->rela['status'];
  850. } else {
  851. $wbwData['relation']['status'] = 7;
  852. }
  853. }
  854. if (isset($word->bmt)) {
  855. $wbwData['bookMarkText']['value'] = $word->bmt->__toString();
  856. if (isset($word->bmt['status'])) {
  857. $wbwData['bookMarkText']['status'] = (int)$word->bmt['status'];
  858. } else {
  859. $wbwData['bookMarkText']['status'] = 7;
  860. }
  861. }
  862. if (isset($word->bmc)) {
  863. $wbwData['bookMarkColor']['value'] = $word->bmc->__toString();
  864. if (isset($word->bmc['status'])) {
  865. $wbwData['bookMarkColor']['status'] = (int)$word->bmc['status'];
  866. } else {
  867. $wbwData['bookMarkColor']['status'] = 7;
  868. }
  869. }
  870. if (isset($word->note)) {
  871. $wbwData['note']['value'] = $word->note->__toString();
  872. if (isset($word->note['status'])) {
  873. $wbwData['note']['status'] = (int)$word->note['status'];
  874. } else {
  875. $wbwData['note']['status'] = 7;
  876. }
  877. }
  878. if (isset($word->cf)) {
  879. $wbwData['confidence'] = (float)$word->cf->__toString();
  880. }
  881. if (isset($word->attachments)) {
  882. $wbwData['attachments'] = json_decode($word->attachments->__toString());
  883. }
  884. if (isset($word->pali['status'])) {
  885. $wbwData['word']['status'] = (int)$word->pali['status'];
  886. }
  887. if (isset($word->real['status'])) {
  888. $wbwData['real']['status'] = (int)$word->real['status'];
  889. }
  890. if (isset($word->mean['status'])) {
  891. $wbwData['meaning']['status'] = (int)$word->mean['status'];
  892. }
  893. if (isset($word->type['status'])) {
  894. $wbwData['type']['status'] = (int)$word->type['status'];
  895. }
  896. if (isset($word->gramma['status'])) {
  897. $wbwData['grammar']['status'] = (int)$word->gramma['status'];
  898. }
  899. if (isset($word->case['status'])) {
  900. $wbwData['case']['status'] = (int)$word->case['status'];
  901. }
  902. if (isset($word->parent['status'])) {
  903. $wbwData['parent']['status'] = (int)$word->parent['status'];
  904. }
  905. if (isset($word->org['status'])) {
  906. $wbwData['factors']['status'] = (int)$word->org['status'];
  907. }
  908. if (isset($word->om['status'])) {
  909. $wbwData['factorMeaning']['status'] = (int)$word->om['status'];
  910. }
  911. $wbwContent[] = $wbwData;
  912. }
  913. }
  914. if (count($wbwContent) === 0) {
  915. return false;
  916. }
  917. return \json_encode($wbwContent, JSON_UNESCAPED_UNICODE);
  918. }
  919. /**
  920. * 将句子放进结果列表
  921. */
  922. private function pushSent($result, $sent, $level = 0, $mode = 'read')
  923. {
  924. $sentProps = base64_encode(\json_encode($sent));
  925. if ($mode === 'read') {
  926. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' ></MdTpl>";
  927. } else {
  928. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' ></MdTpl>";
  929. }
  930. //增加标题的html标记
  931. if ($level > 0) {
  932. $sentWidget = "<h{$level}>" . $sentWidget . "</h{$level}>";
  933. }
  934. array_push($result, $sentWidget);
  935. return $result;
  936. }
  937. private function newSent($book, $para, $word_start, $word_end)
  938. {
  939. $sent = [
  940. "id" => "{$book}-{$para}-{$word_start}-{$word_end}",
  941. "book" => $book,
  942. "para" => $para,
  943. "wordStart" => $word_start,
  944. "wordEnd" => $word_end,
  945. "origin" => [],
  946. "translation" => [],
  947. "commentaries" => [],
  948. ];
  949. if ($book < 1000) {
  950. #生成channel 数量列表
  951. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  952. $channelCount = CorpusController::_sentCanReadCount($book, $para, $word_start, $word_end, $this->userUuid);
  953. $path = json_decode(PaliText::where('book', $book)->where('paragraph', $para)->value("path"), true);
  954. $sent["path"] = [];
  955. foreach ($path as $key => $value) {
  956. # code...
  957. $value['paliTitle'] = $value['title'];
  958. $sent["path"][] = $value;
  959. }
  960. $sent["tranNum"] = $channelCount['tranNum'];
  961. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  962. $sent["commNum"] = $channelCount['commNum'];
  963. $sent["originNum"] = $channelCount['originNum'];
  964. $sent["simNum"] = $channelCount['simNum'];
  965. }
  966. return $sent;
  967. }
  968. public static function _sentCanReadCount($book, $para, $start, $end, $userUuid = null)
  969. {
  970. $keyCanRead = "/channel/can-read/";
  971. if ($userUuid) {
  972. $keyCanRead .= $userUuid;
  973. } else {
  974. $keyCanRead .= 'guest';
  975. }
  976. $channelCanRead = Cache::remember(
  977. $keyCanRead,
  978. config('mint.cache.expire'),
  979. function () use ($userUuid) {
  980. return ChannelApi::getCanReadByUser($userUuid);
  981. }
  982. );
  983. $channels = Sentence::where('book_id', $book)
  984. ->where('paragraph', $para)
  985. ->where('word_start', $start)
  986. ->where('word_end', $end)
  987. ->where('strlen', '<>', 0)
  988. ->whereIn('channel_uid', $channelCanRead)
  989. ->select('channel_uid')
  990. ->groupBy('channel_uid')
  991. ->get();
  992. $channelList = [];
  993. foreach ($channels as $key => $value) {
  994. # code...
  995. if (Str::isUuid($value->channel_uid)) {
  996. $channelList[] = $value->channel_uid;
  997. }
  998. }
  999. $simId = PaliSentence::where('book', $book)
  1000. ->where('paragraph', $para)
  1001. ->where('word_begin', $start)
  1002. ->where('word_end', $end)
  1003. ->value('id');
  1004. if ($simId) {
  1005. $output["simNum"] = SentSimIndex::where('sent_id', $simId)->value('count');
  1006. } else {
  1007. $output["simNum"] = 0;
  1008. }
  1009. $channelInfo = Channel::whereIn("uid", $channelList)->select('type')->get();
  1010. $output["tranNum"] = 0;
  1011. $output["nissayaNum"] = 0;
  1012. $output["commNum"] = 0;
  1013. $output["originNum"] = 0;
  1014. foreach ($channelInfo as $key => $value) {
  1015. # code...
  1016. switch ($value->type) {
  1017. case "translation":
  1018. $output["tranNum"]++;
  1019. break;
  1020. case "nissaya":
  1021. $output["nissayaNum"]++;
  1022. break;
  1023. case "commentary":
  1024. $output["commNum"]++;
  1025. break;
  1026. case "original":
  1027. $output["originNum"]++;
  1028. break;
  1029. }
  1030. }
  1031. return $output;
  1032. }
  1033. /**
  1034. * 获取某个句子的相关资源的句子数量
  1035. */
  1036. public static function sentCanReadCount($book, $para, $start, $end, $userUuid = null)
  1037. {
  1038. $sentId = "{$book}-{$para}-{$start}-{$end}";
  1039. $hKey = "/sentence/res-count/{$sentId}/";
  1040. if ($userUuid) {
  1041. $key = $userUuid;
  1042. } else {
  1043. $key = 'guest';
  1044. }
  1045. if (Redis::hExists($hKey, $key)) {
  1046. return json_decode(Redis::hGet($hKey, $key), true);
  1047. } else {
  1048. $channelCount = CorpusController::_sentCanReadCount($book, $para, $start, $end, $userUuid);
  1049. Redis::hSet($hKey, $key, json_encode($channelCount));
  1050. return $channelCount;
  1051. }
  1052. }
  1053. private function markdownRender($input) {}
  1054. /**
  1055. * Update the specified resource in storage.
  1056. *
  1057. * @param \Illuminate\Http\Request $request
  1058. * @param \App\Models\Sentence $sentence
  1059. * @return \Illuminate\Http\Response
  1060. */
  1061. public function update(Request $request, Sentence $sentence)
  1062. {
  1063. //
  1064. }
  1065. /**
  1066. * Remove the specified resource from storage.
  1067. *
  1068. * @param \App\Models\Sentence $sentence
  1069. * @return \Illuminate\Http\Response
  1070. */
  1071. public function destroy(Sentence $sentence)
  1072. {
  1073. //
  1074. }
  1075. }