CorpusController.php 43 KB

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