CorpusController.php 42 KB

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