CorpusController.php 36 KB

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