CorpusController.php 37 KB

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