CorpusController.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. return MdRender::render(
  707. $row->content,
  708. [$row->channel_uid],
  709. null,
  710. $mode,
  711. "nissaya",
  712. $row->content_type,
  713. $format
  714. );
  715. }
  716. );
  717. break;
  718. case 'commentary':
  719. $options = [
  720. 'debug' => $this->debug,
  721. 'format' => $format,
  722. 'mode' => $mode,
  723. 'channelType' => 'translation',
  724. 'contentType' => $row->content_type,
  725. ];
  726. $mdRender = new MdRender($options);
  727. $newSent['html'] = $mdRender->convert($row->content, $channelsId);
  728. break;
  729. default:
  730. $options = [
  731. 'debug' => $this->debug,
  732. 'format' => $format,
  733. 'mode' => $mode,
  734. 'channelType' => 'translation',
  735. 'contentType' => $row->content_type,
  736. ];
  737. $mdRender = new MdRender($options);
  738. $newSent['html'] = $mdRender->convert($row->content, [$row->channel_uid]);
  739. //Log::debug('md render', ['content' => $row->content, 'options' => $options, 'render' => $newSent['html']]);
  740. break;
  741. }
  742. }
  743. switch ($info->type) {
  744. case 'wbw':
  745. case 'original':
  746. array_push($sent["origin"], $newSent);
  747. break;
  748. case 'commentary':
  749. array_push($sent["commentaries"], $newSent);
  750. break;
  751. default:
  752. array_push($sent["translation"], $newSent);
  753. break;
  754. }
  755. }
  756. if ($onlyProps) {
  757. return $sent;
  758. }
  759. $content = $this->pushSent($content, $sent, 0, $mode);
  760. }
  761. if ($paraMark) {
  762. $content[] = '</MdTpl>';
  763. }
  764. $output = \implode("", $content);
  765. return "<div>{$output}</div>";
  766. }
  767. public function getWbw($book, $para, $start, $end, $channel)
  768. {
  769. /**
  770. * 非阅读模式下。原文使用逐词解析数据。
  771. * 优先加载第一个translation channel 如果没有。加载默认逐词解析。
  772. */
  773. //获取逐词解析数据
  774. $wbwBlock = WbwBlock::where('channel_uid', $channel)
  775. ->where('book_id', $book)
  776. ->where('paragraph', $para)
  777. ->select('uid')
  778. ->first();
  779. if (!$wbwBlock) {
  780. return false;
  781. }
  782. //找到逐词解析数据
  783. $wbwData = Wbw::where('block_uid', $wbwBlock->uid)
  784. ->whereBetween('wid', [$start, $end])
  785. ->select(['book_id', 'paragraph', 'wid', 'data', 'uid', 'editor_id', 'created_at', 'updated_at'])
  786. ->orderBy('wid')
  787. ->get();
  788. $wbwContent = [];
  789. foreach ($wbwData as $wbwrow) {
  790. $wbw = str_replace("&nbsp;", ' ', $wbwrow->data);
  791. $wbw = str_replace("<br>", ' ', $wbw);
  792. $xmlString = "<root>" . $wbw . "</root>";
  793. try {
  794. $xmlWord = simplexml_load_string($xmlString);
  795. } catch (\Exception $e) {
  796. Log::error('corpus', ['error' => $e]);
  797. continue;
  798. }
  799. $wordsList = $xmlWord->xpath('//word');
  800. foreach ($wordsList as $word) {
  801. $case = \str_replace(['#', '.'], ['$', ''], $word->case->__toString());
  802. $case = \str_replace('$$', '$', $case);
  803. $case = trim($case);
  804. $case = trim($case, "$");
  805. $wbwId = explode('-', $word->id->__toString());
  806. $wbwData = [
  807. 'uid' => $wbwrow->uid,
  808. 'book' => $wbwrow->book_id,
  809. 'para' => $wbwrow->paragraph,
  810. 'sn' => array_slice($wbwId, 2),
  811. 'word' => ['value' => $word->pali->__toString(), 'status' => 0],
  812. 'real' => ['value' => $word->real->__toString(), 'status' => 0],
  813. 'meaning' => ['value' => $word->mean->__toString(), 'status' => 0],
  814. 'type' => ['value' => $word->type->__toString(), 'status' => 0],
  815. 'grammar' => ['value' => $word->gramma->__toString(), 'status' => 0],
  816. 'case' => ['value' => $word->case->__toString(), 'status' => 0],
  817. 'parent' => ['value' => $word->parent->__toString(), 'status' => 0],
  818. 'style' => ['value' => $word->style->__toString(), 'status' => 0],
  819. 'factors' => ['value' => $word->org->__toString(), 'status' => 0],
  820. 'factorMeaning' => ['value' => $word->om->__toString(), 'status' => 0],
  821. 'confidence' => $word->cf->__toString(),
  822. 'created_at' => $wbwrow->created_at,
  823. 'updated_at' => $wbwrow->updated_at,
  824. 'hasComment' => Discussion::where('res_id', $wbwrow->uid)->exists(),
  825. ];
  826. if (isset($word->parent2)) {
  827. $wbwData['parent2']['value'] = $word->parent2->__toString();
  828. if (isset($word->parent2['status'])) {
  829. $wbwData['parent2']['status'] = (int)$word->parent2['status'];
  830. } else {
  831. $wbwData['parent2']['status'] = 0;
  832. }
  833. }
  834. if (isset($word->pg)) {
  835. $wbwData['grammar2']['value'] = $word->pg->__toString();
  836. if (isset($word->pg['status'])) {
  837. $wbwData['grammar2']['status'] = (int)$word->pg['status'];
  838. } else {
  839. $wbwData['grammar2']['status'] = 0;
  840. }
  841. }
  842. if (isset($word->rela)) {
  843. $wbwData['relation']['value'] = $word->rela->__toString();
  844. if (isset($word->rela['status'])) {
  845. $wbwData['relation']['status'] = (int)$word->rela['status'];
  846. } else {
  847. $wbwData['relation']['status'] = 7;
  848. }
  849. }
  850. if (isset($word->bmt)) {
  851. $wbwData['bookMarkText']['value'] = $word->bmt->__toString();
  852. if (isset($word->bmt['status'])) {
  853. $wbwData['bookMarkText']['status'] = (int)$word->bmt['status'];
  854. } else {
  855. $wbwData['bookMarkText']['status'] = 7;
  856. }
  857. }
  858. if (isset($word->bmc)) {
  859. $wbwData['bookMarkColor']['value'] = $word->bmc->__toString();
  860. if (isset($word->bmc['status'])) {
  861. $wbwData['bookMarkColor']['status'] = (int)$word->bmc['status'];
  862. } else {
  863. $wbwData['bookMarkColor']['status'] = 7;
  864. }
  865. }
  866. if (isset($word->note)) {
  867. $wbwData['note']['value'] = $word->note->__toString();
  868. if (isset($word->note['status'])) {
  869. $wbwData['note']['status'] = (int)$word->note['status'];
  870. } else {
  871. $wbwData['note']['status'] = 7;
  872. }
  873. }
  874. if (isset($word->cf)) {
  875. $wbwData['confidence'] = (float)$word->cf->__toString();
  876. }
  877. if (isset($word->attachments)) {
  878. $wbwData['attachments'] = json_decode($word->attachments->__toString());
  879. }
  880. if (isset($word->pali['status'])) {
  881. $wbwData['word']['status'] = (int)$word->pali['status'];
  882. }
  883. if (isset($word->real['status'])) {
  884. $wbwData['real']['status'] = (int)$word->real['status'];
  885. }
  886. if (isset($word->mean['status'])) {
  887. $wbwData['meaning']['status'] = (int)$word->mean['status'];
  888. }
  889. if (isset($word->type['status'])) {
  890. $wbwData['type']['status'] = (int)$word->type['status'];
  891. }
  892. if (isset($word->gramma['status'])) {
  893. $wbwData['grammar']['status'] = (int)$word->gramma['status'];
  894. }
  895. if (isset($word->case['status'])) {
  896. $wbwData['case']['status'] = (int)$word->case['status'];
  897. }
  898. if (isset($word->parent['status'])) {
  899. $wbwData['parent']['status'] = (int)$word->parent['status'];
  900. }
  901. if (isset($word->org['status'])) {
  902. $wbwData['factors']['status'] = (int)$word->org['status'];
  903. }
  904. if (isset($word->om['status'])) {
  905. $wbwData['factorMeaning']['status'] = (int)$word->om['status'];
  906. }
  907. $wbwContent[] = $wbwData;
  908. }
  909. }
  910. if (count($wbwContent) === 0) {
  911. return false;
  912. }
  913. return \json_encode($wbwContent, JSON_UNESCAPED_UNICODE);
  914. }
  915. /**
  916. * 将句子放进结果列表
  917. */
  918. private function pushSent($result, $sent, $level = 0, $mode = 'read')
  919. {
  920. $sentProps = base64_encode(\json_encode($sent));
  921. if ($mode === 'read') {
  922. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' ></MdTpl>";
  923. } else {
  924. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' ></MdTpl>";
  925. }
  926. //增加标题的html标记
  927. if ($level > 0) {
  928. $sentWidget = "<h{$level}>" . $sentWidget . "</h{$level}>";
  929. }
  930. array_push($result, $sentWidget);
  931. return $result;
  932. }
  933. private function newSent($book, $para, $word_start, $word_end)
  934. {
  935. $sent = [
  936. "id" => "{$book}-{$para}-{$word_start}-{$word_end}",
  937. "book" => $book,
  938. "para" => $para,
  939. "wordStart" => $word_start,
  940. "wordEnd" => $word_end,
  941. "origin" => [],
  942. "translation" => [],
  943. "commentaries" => [],
  944. ];
  945. if ($book < 1000) {
  946. #生成channel 数量列表
  947. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  948. $channelCount = CorpusController::_sentCanReadCount($book, $para, $word_start, $word_end, $this->userUuid);
  949. $path = json_decode(PaliText::where('book', $book)->where('paragraph', $para)->value("path"), true);
  950. $sent["path"] = [];
  951. foreach ($path as $key => $value) {
  952. # code...
  953. $value['paliTitle'] = $value['title'];
  954. $sent["path"][] = $value;
  955. }
  956. $sent["tranNum"] = $channelCount['tranNum'];
  957. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  958. $sent["commNum"] = $channelCount['commNum'];
  959. $sent["originNum"] = $channelCount['originNum'];
  960. $sent["simNum"] = $channelCount['simNum'];
  961. }
  962. return $sent;
  963. }
  964. public static function _sentCanReadCount($book, $para, $start, $end, $userUuid = null)
  965. {
  966. $keyCanRead = "/channel/can-read/";
  967. if ($userUuid) {
  968. $keyCanRead .= $userUuid;
  969. } else {
  970. $keyCanRead .= 'guest';
  971. }
  972. $channelCanRead = Cache::remember(
  973. $keyCanRead,
  974. config('mint.cache.expire'),
  975. function () use ($userUuid) {
  976. return ChannelApi::getCanReadByUser($userUuid);
  977. }
  978. );
  979. $channels = Sentence::where('book_id', $book)
  980. ->where('paragraph', $para)
  981. ->where('word_start', $start)
  982. ->where('word_end', $end)
  983. ->where('strlen', '<>', 0)
  984. ->whereIn('channel_uid', $channelCanRead)
  985. ->select('channel_uid')
  986. ->groupBy('channel_uid')
  987. ->get();
  988. $channelList = [];
  989. foreach ($channels as $key => $value) {
  990. # code...
  991. if (Str::isUuid($value->channel_uid)) {
  992. $channelList[] = $value->channel_uid;
  993. }
  994. }
  995. $simId = PaliSentence::where('book', $book)
  996. ->where('paragraph', $para)
  997. ->where('word_begin', $start)
  998. ->where('word_end', $end)
  999. ->value('id');
  1000. if ($simId) {
  1001. $output["simNum"] = SentSimIndex::where('sent_id', $simId)->value('count');
  1002. } else {
  1003. $output["simNum"] = 0;
  1004. }
  1005. $channelInfo = Channel::whereIn("uid", $channelList)->select('type')->get();
  1006. $output["tranNum"] = 0;
  1007. $output["nissayaNum"] = 0;
  1008. $output["commNum"] = 0;
  1009. $output["originNum"] = 0;
  1010. foreach ($channelInfo as $key => $value) {
  1011. # code...
  1012. switch ($value->type) {
  1013. case "translation":
  1014. $output["tranNum"]++;
  1015. break;
  1016. case "nissaya":
  1017. $output["nissayaNum"]++;
  1018. break;
  1019. case "commentary":
  1020. $output["commNum"]++;
  1021. break;
  1022. case "original":
  1023. $output["originNum"]++;
  1024. break;
  1025. }
  1026. }
  1027. return $output;
  1028. }
  1029. /**
  1030. * 获取某个句子的相关资源的句子数量
  1031. */
  1032. public static function sentCanReadCount($book, $para, $start, $end, $userUuid = null)
  1033. {
  1034. $sentId = "{$book}-{$para}-{$start}-{$end}";
  1035. $hKey = "/sentence/res-count/{$sentId}/";
  1036. if ($userUuid) {
  1037. $key = $userUuid;
  1038. } else {
  1039. $key = 'guest';
  1040. }
  1041. if (Redis::hExists($hKey, $key)) {
  1042. return json_decode(Redis::hGet($hKey, $key), true);
  1043. } else {
  1044. $channelCount = CorpusController::_sentCanReadCount($book, $para, $start, $end, $userUuid);
  1045. Redis::hSet($hKey, $key, json_encode($channelCount));
  1046. return $channelCount;
  1047. }
  1048. }
  1049. private function markdownRender($input) {}
  1050. /**
  1051. * Update the specified resource in storage.
  1052. *
  1053. * @param \Illuminate\Http\Request $request
  1054. * @param \App\Models\Sentence $sentence
  1055. * @return \Illuminate\Http\Response
  1056. */
  1057. public function update(Request $request, Sentence $sentence)
  1058. {
  1059. //
  1060. }
  1061. /**
  1062. * Remove the specified resource from storage.
  1063. *
  1064. * @param \App\Models\Sentence $sentence
  1065. * @return \Illuminate\Http\Response
  1066. */
  1067. public function destroy(Sentence $sentence)
  1068. {
  1069. //
  1070. }
  1071. }