CorpusController.php 37 KB

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