CorpusController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. $getChannel = explode(",",$request->get('channels'));
  220. $channels = array_merge($channels,$getChannel );
  221. }
  222. $para = explode(",",$request->get('par'));
  223. //段落所在章节
  224. $parent = PaliText::where('book',$request->get('book'))
  225. ->where('paragraph',$para[0])->first();
  226. $chapter = PaliText::where('book',$request->get('book'))
  227. ->where('paragraph',$parent->parent)->first();
  228. if($chapter){
  229. if(empty($chapter->toc)){
  230. $this->result['title'] = "unknown";
  231. }else{
  232. $this->result['title'] = $chapter->toc;
  233. $this->result['sub_title'] = $chapter->toc;
  234. $this->result['path'] = json_decode($chapter->path);
  235. }
  236. }
  237. $paraFrom = $para[0];
  238. $paraTo = end($para);
  239. $indexedHeading = [];
  240. #获取channel索引表
  241. $tranChannels = [];
  242. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  243. foreach ($channelInfo as $key => $value) {
  244. # code...
  245. if($value->type==="translation" ){
  246. $tranChannels[] = $value->uid;
  247. }
  248. }
  249. $indexChannel = [];
  250. $indexChannel = $this->getChannelIndex($channels);
  251. //获取wbw channel
  252. //目前默认的 wbw channel 是第一个translation channel
  253. foreach ($channels as $key => $value) {
  254. # code...
  255. if($indexChannel[$value]->type==='translation'){
  256. $this->wbwChannels[] = $value;
  257. break;
  258. }
  259. }
  260. //章节译文标题
  261. $title = Sentence::select($this->selectCol)
  262. ->where('book_id',$parent->book)
  263. ->where('paragraph',$parent->paragraph)
  264. ->whereIn('channel_uid',$tranChannels)
  265. ->first();
  266. if($title){
  267. $this->result['title'] = MdRender::render($title->content,[$title->channel_uid]);
  268. }
  269. /**
  270. * 获取句子数据
  271. */
  272. $record = Sentence::select($this->selectCol)
  273. ->where('book_id',$request->get('book'))
  274. ->whereIn('paragraph',$para)
  275. ->whereIn('channel_uid',$channels)
  276. ->orderBy('paragraph')
  277. ->orderBy('word_start')
  278. ->get();
  279. if(count($record) ===0){
  280. $this->result['content'] = "<span>No Data</span>";
  281. }else{
  282. $this->result['content'] = $this->makeContent($record,$request->get('mode','read'),$indexChannel,$indexedHeading);
  283. }
  284. return $this->ok($this->result);
  285. }
  286. /**
  287. * Store a newly created resource in storage.
  288. * @param \Illuminate\Http\Request $request
  289. * @param string $id
  290. * @return \Illuminate\Http\Response
  291. */
  292. public function showChapter(Request $request, string $id)
  293. {
  294. //
  295. $sentId = \explode('-',$id);
  296. $channels = [];
  297. if($request->has('channels')){
  298. $channels = explode('_',$request->get('channels'));
  299. }
  300. $mode = $request->get('mode','read');
  301. if($mode === 'read'){
  302. //阅读模式加载html格式原文
  303. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  304. }else{
  305. //翻译模式加载json格式原文
  306. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  307. }
  308. if($channelId !== false){
  309. $channels[] = $channelId;
  310. }
  311. $chapter = PaliText::where('book',$sentId[0])->where('paragraph',$sentId[1])->first();
  312. if(!$chapter){
  313. return $this->error("no data");
  314. }
  315. if(empty($chapter->toc)){
  316. $this->result['title'] = "unknown";
  317. }else{
  318. $this->result['title'] = $chapter->toc;
  319. $this->result['sub_title'] = $chapter->toc;
  320. $this->result['path'] = json_decode($chapter->path);
  321. }
  322. $paraFrom = $sentId[1];
  323. $paraTo = $sentId[1]+$chapter->chapter_len-1;
  324. //获取标题
  325. $heading = PaliText::select(["book","paragraph","level"])
  326. ->where('book',$sentId[0])
  327. ->whereBetween('paragraph',[$paraFrom,$paraTo])
  328. ->where('level','<',8)
  329. ->get();
  330. //将标题段落转成索引数组 以便输出标题层级
  331. $indexedHeading = [];
  332. foreach ($heading as $key => $value) {
  333. # code...
  334. $indexedHeading["{$value->book}-{$value->paragraph}"] = $value->level;
  335. }
  336. #获取channel索引表
  337. $tranChannels = [];
  338. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  339. foreach ($channelInfo as $key => $value) {
  340. # code...
  341. if($value->type==="translation" ){
  342. $tranChannels[] = $value->uid;
  343. }
  344. }
  345. $indexChannel = [];
  346. $indexChannel = $this->getChannelIndex($channels);
  347. //获取wbw channel
  348. //目前默认的 wbw channel 是第一个translation channel
  349. foreach ($channels as $key => $value) {
  350. # code...
  351. if($indexChannel[$value]->type==='translation'){
  352. $this->wbwChannels[] = $value;
  353. break;
  354. }
  355. }
  356. $title = Sentence::select($this->selectCol)
  357. ->where('book_id',$sentId[0])
  358. ->where('paragraph',$sentId[1])
  359. ->whereIn('channel_uid',$tranChannels)
  360. ->first();
  361. if($title){
  362. $this->result['title'] = MdRender::render($title->content,[$title->channel_uid]);
  363. }
  364. /**
  365. * 获取句子数据
  366. * 算法:
  367. * 1. 如果标题和下一级第一个标题之间有段落。只输出这些段落和子目录
  368. * 2. 如果标题和下一级第一个标题之间没有间隔 且 chapter 长度大于10000个字符 且有子目录,只输出子目录
  369. * 3. 如果二者都不是,lazy load
  370. */
  371. //1. 计算 标题和下一级第一个标题之间 是否有间隔
  372. $nextChapter = PaliText::where('book',$sentId[0])
  373. ->where('paragraph',">",$sentId[1])
  374. ->where('level','<',8)
  375. ->orderBy('paragraph')
  376. ->value('paragraph');
  377. $between = $nextChapter - $sentId[1];
  378. //输出子目录
  379. $chapterLen = $chapter->chapter_len;
  380. $toc = PaliText::where('book',$sentId[0])
  381. ->whereBetween('paragraph',[$paraFrom+1,$paraFrom+$chapterLen-1])
  382. ->where('level','<',8)
  383. ->orderBy('paragraph')
  384. ->select(['book','paragraph','level','toc'])
  385. ->get();
  386. if($between > 1){
  387. //有间隔
  388. $paraTo = $nextChapter - 1;
  389. }else{
  390. if($chapter->chapter_strlen>2000){
  391. if(count($toc)>0){
  392. //有子目录只输出标题和目录
  393. $paraTo = $paraFrom;
  394. }else{
  395. //没有子目录 全部输出
  396. }
  397. }else{
  398. //章节小。全部输出 不输出章节
  399. $toc = [];
  400. }
  401. }
  402. $pFrom = $request->get('from',$paraFrom);
  403. $pTo = $request->get('to',$paraTo);
  404. //根据句子的长度找到这次应该加载的句子
  405. $maxLen = 3000;
  406. $paliText = PaliText::select(['paragraph','lenght'])
  407. ->where('book',$sentId[0])
  408. ->whereBetween('paragraph',[$pFrom,$pTo])
  409. ->get();
  410. $sumLen = 0;
  411. $currTo = $pTo;
  412. foreach ($paliText as $para) {
  413. $sumLen += $para->lenght;
  414. if($sumLen > $maxLen){
  415. $currTo = $para->paragraph;
  416. break;
  417. }
  418. }
  419. $record = Sentence::select($this->selectCol)
  420. ->where('book_id',$sentId[0])
  421. ->whereBetween('paragraph',[$pFrom,$currTo])
  422. ->whereIn('channel_uid',$channels)
  423. ->orderBy('paragraph')
  424. ->orderBy('word_start')
  425. ->get();
  426. if(count($record) ===0){
  427. return $this->error("no data");
  428. }
  429. $this->result['content'] = $this->makeContent($record,$mode,$indexChannel,$indexedHeading);
  430. if(!$request->has('from')){
  431. //第一次才显示toc
  432. $this->result['toc'] = TocResource::collection($toc);
  433. }
  434. if($currTo < $pTo){
  435. $this->result['from'] = $currTo+1;
  436. $this->result['to'] = $pTo;
  437. $this->result['paraId'] = $id;
  438. $this->result['channels'] = $request->get('channels');
  439. $this->result['mode'] = $request->get('mode');
  440. }
  441. return $this->ok($this->result);
  442. }
  443. private function getChannelIndex($channels,$type=null){
  444. #获取channel索引表
  445. $channelInfo = Channel::whereIn("uid",$channels)
  446. ->select(['uid','type','name','owner_uid'])->get();
  447. $indexChannel = [];
  448. foreach ($channelInfo as $key => $value) {
  449. # code...
  450. if($type !== null && $value->type !== $type){
  451. continue;
  452. }
  453. $indexChannel[$value->uid] = $value;
  454. }
  455. foreach ($indexChannel as $uid => $value) {
  456. # 查询studio
  457. $indexChannel[$uid]['studio'] = StudioApi::getById($value->owner_uid);
  458. }
  459. return $indexChannel;
  460. }
  461. /**
  462. * 根据句子库数据生成文章内容
  463. * $record 句子数据
  464. * $mode read | edit | wbw
  465. * $indexChannel channel索引
  466. * $indexedHeading 标题索引 用于给段落加标题标签 <h1> ect.
  467. */
  468. private function makeContent($record,$mode,$indexChannel,$indexedHeading=[],$onlyProps=false,$paraMark=false){
  469. $content = [];
  470. $lastSent = "0-0";
  471. $sentCount = 0;
  472. $sent = [];
  473. $sent["origin"] = [];
  474. $sent["translation"] = [];
  475. //获取句子编号列表
  476. $sentList = [];
  477. foreach ($record as $key => $value) {
  478. $currSentId = "{$value->book_id}-{$value->paragraph}-{$value->word_start}-{$value->word_end}";
  479. $sentList[$currSentId]=[$value->book_id ,$value->paragraph,$value->word_start,$value->word_end];
  480. $value['sid'] = "{$currSentId}_{$value->channel_uid}";
  481. }
  482. //遍历列表查找每个句子的所有channel的数据,并填充
  483. $currPara = "";
  484. foreach ($sentList as $currSentId => $arrSentId) {
  485. if($currPara === ""){
  486. $currPara = $arrSentId[0]."-".$arrSentId[1];
  487. }
  488. $sent = $this->newSent($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3]);
  489. foreach ($indexChannel as $channelId => $info) {
  490. # code...
  491. $sid = "{$currSentId}_{$channelId}";
  492. $newSent = [
  493. "content"=>"",
  494. "html"=> "",
  495. "book"=> $arrSentId[0],
  496. "para"=> $arrSentId[1],
  497. "wordStart"=> $arrSentId[2],
  498. "wordEnd"=> $arrSentId[3],
  499. "channel"=> [
  500. "name"=>$info->name,
  501. "type"=>$info->type,
  502. "id"=> $info->uid,
  503. ],
  504. "studio" => $info['studio'],
  505. "updateAt"=> "",
  506. "suggestionCount" => SuggestionApi::getCountBySent($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3],$channelId),
  507. ];
  508. $row = Arr::first($record,function($value,$key) use($sid){
  509. return $value['sid']===$sid;
  510. });
  511. if($row){
  512. $newSent['id'] = $row->uid;
  513. $newSent['content'] = $row->content;
  514. $newSent['contentType'] = $row->content_type;
  515. $newSent['html'] = "";
  516. $newSent["editor"]=UserApi::getByUuid($row->editor_uid);
  517. $newSent['updateAt'] = $row->updated_at;
  518. if($mode !== "read"){
  519. if(isset($row->acceptor_uid) && !empty($row->acceptor_uid)){
  520. $newSent["acceptor"]=UserApi::getByUuid($row->acceptor_uid);
  521. $newSent["prEditAt"]=$row->pr_edit_at;
  522. }
  523. }
  524. switch ($info->type) {
  525. case 'wbw':
  526. case 'original':
  527. //
  528. // 在编辑模式下。
  529. // 如果是原文,查看是否有逐词解析数据,
  530. // 有的话优先显示。
  531. // 阅读模式直接显示html原文
  532. // 传过来的数据一定有一个原文channel
  533. //
  534. if($mode !== "read"){
  535. $newSent['channel']['type'] = "wbw";
  536. if(isset($this->wbwChannels[0])){
  537. $newSent['channel']['name'] = $indexChannel[$this->wbwChannels[0]]->name;
  538. $newSent['channel']['id'] = $this->wbwChannels[0];
  539. //存在一个translation channel
  540. //尝试查找逐词解析数据。找到,替换现有数据
  541. $wbwData = $this->getWbw($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3],$this->wbwChannels[0]);
  542. if($wbwData){
  543. $newSent['content'] = $wbwData;
  544. $newSent['html'] = "";
  545. }
  546. }
  547. }else{
  548. $newSent['content'] = "";
  549. $newSent['html'] = $row->content;
  550. }
  551. break;
  552. case 'nissaya':
  553. $newSent['html'] = Cache::remember("/sent/{$channelId}/{$currSentId}",
  554. env('CACHE_EXPIRE',3600*24),
  555. function() use($row,$mode){
  556. return MdRender::render($row->content,[$row->channel_uid],null,$mode,"nissaya",$row->content_type);
  557. });
  558. break;
  559. default:
  560. //译文需要markdown渲染
  561. $newSent['html'] = Cache::remember("/sent/{$channelId}/{$currSentId}",
  562. env('CACHE_EXPIRE',3600*24),
  563. function() use($row){
  564. return MdRender::render($row->content,[$row->channel_uid]);
  565. });
  566. break;
  567. }
  568. }
  569. switch ($info->type) {
  570. case 'wbw':
  571. case 'original':
  572. array_push($sent["origin"],$newSent);
  573. break;
  574. default:
  575. array_push($sent["translation"],$newSent);
  576. break;
  577. }
  578. }
  579. if($onlyProps){
  580. return $sent;
  581. }
  582. $content = $this->pushSent($content,$sent,0,$mode);
  583. }
  584. $output = \implode("",$content);
  585. return "<div>{$output}</div>";
  586. }
  587. public function getWbw($book,$para,$start,$end,$channel){
  588. /**
  589. * 非阅读模式下。原文使用逐词解析数据。
  590. * 优先加载第一个translation channel 如果没有。加载默认逐词解析。
  591. */
  592. //获取逐词解析数据
  593. $wbwBlock = WbwBlock::where('channel_uid',$channel)
  594. ->where('book_id',$book)
  595. ->where('paragraph',$para)
  596. ->select('uid')
  597. ->first();
  598. if(!$wbwBlock){
  599. return false;
  600. }
  601. //找到逐词解析数据
  602. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  603. ->whereBetween('wid',[$start,$end])
  604. ->select(['book_id','paragraph','wid','data','uid'])
  605. ->orderBy('wid')
  606. ->get();
  607. $wbwContent = [];
  608. foreach ($wbwData as $wbwrow) {
  609. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  610. $wbw = str_replace("<br>",' ',$wbw);
  611. $xmlString = "<root>" . $wbw . "</root>";
  612. try{
  613. $xmlWord = simplexml_load_string($xmlString);
  614. }catch(Exception $e){
  615. continue;
  616. }
  617. $wordsList = $xmlWord->xpath('//word');
  618. foreach ($wordsList as $word) {
  619. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  620. $case = \str_replace('$$','$',$case);
  621. $case = trim($case);
  622. $case = trim($case,"$");
  623. $wbwId = explode('-',$word->id->__toString());
  624. $wbwData = [
  625. 'uid'=>$wbwrow->uid,
  626. 'book'=>$wbwrow->book_id,
  627. 'para'=>$wbwrow->paragraph,
  628. 'sn'=> array_slice($wbwId,2),
  629. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  630. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  631. 'meaning'=> ['value'=>$word->mean->__toString() ,'status'=>0],
  632. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  633. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  634. 'case'=> ['value'=>$word->case->__toString(),'status'=>0],
  635. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  636. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  637. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  638. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  639. 'confidence'=> $word->cf->__toString(),
  640. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  641. ];
  642. if(isset($word->parent2)){
  643. $wbwData['parent2']['value'] = $word->parent2->__toString();
  644. if(isset($word->parent2['status'])){
  645. $wbwData['parent2']['status'] = (int)$word->parent2['status'];
  646. }else{
  647. $wbwData['parent2']['status'] = 0;
  648. }
  649. }
  650. if(isset($word->pg)){
  651. $wbwData['grammar2']['value'] = $word->pg->__toString();
  652. if(isset($word->pg['status'])){
  653. $wbwData['grammar2']['status'] = (int)$word->pg['status'];
  654. }else{
  655. $wbwData['grammar2']['status'] = 0;
  656. }
  657. }
  658. if(isset($word->rela)){
  659. $wbwData['relation']['value'] = $word->rela->__toString();
  660. if(isset($word->rela['status'])){
  661. $wbwData['relation']['status'] = (int)$word->rela['status'];
  662. }else{
  663. $wbwData['relation']['status'] = 7;
  664. }
  665. }
  666. if(isset($word->bmt)){
  667. $wbwData['bookMarkText']['value'] = $word->bmt->__toString();
  668. if(isset($word->bmt['status'])){
  669. $wbwData['bookMarkText']['status'] = (int)$word->bmt['status'];
  670. }else{
  671. $wbwData['bookMarkText']['status'] = 7;
  672. }
  673. }
  674. if(isset($word->bmc)){
  675. $wbwData['bookMarkColor']['value'] = $word->bmc->__toString();
  676. if(isset($word->bmc['status'])){
  677. $wbwData['bookMarkColor']['status'] = (int)$word->bmc['status'];
  678. }else{
  679. $wbwData['bookMarkColor']['status'] = 7;
  680. }
  681. }
  682. if(isset($word->note)){
  683. $wbwData['note']['value'] = $word->note->__toString();
  684. if(isset($word->note['status'])){
  685. $wbwData['note']['status'] = (int)$word->note['status'];
  686. }else{
  687. $wbwData['note']['status'] = 7;
  688. }
  689. }
  690. if(isset($word->cf)){
  691. $wbwData['confidence'] = (float)$word->cf->__toString();
  692. }
  693. if(isset($word->attachments)){
  694. $wbwData['attachments'] = json_decode($word->attachments->__toString());
  695. }
  696. if(isset($word->pali['status'])){
  697. $wbwData['word']['status'] = (int)$word->pali['status'];
  698. }
  699. if(isset($word->real['status'])){
  700. $wbwData['real']['status'] = (int)$word->real['status'];
  701. }
  702. if(isset($word->mean['status'])){
  703. $wbwData['meaning']['status'] = (int)$word->mean['status'];
  704. }
  705. if(isset($word->type['status'])){
  706. $wbwData['type']['status'] = (int)$word->type['status'];
  707. }
  708. if(isset($word->gramma['status'])){
  709. $wbwData['grammar']['status'] = (int)$word->gramma['status'];
  710. }
  711. if(isset($word->case['status'])){
  712. $wbwData['case']['status'] = (int)$word->case['status'];
  713. }
  714. if(isset($word->parent['status'])){
  715. $wbwData['parent']['status'] = (int)$word->parent['status'];
  716. }
  717. if(isset($word->org['status'])){
  718. $wbwData['factors']['status'] = (int)$word->org['status'];
  719. }
  720. if(isset($word->om['status'])){
  721. $wbwData['factorMeaning']['status'] = (int)$word->om['status'];
  722. }
  723. $wbwContent[] = $wbwData;
  724. }
  725. }
  726. if(count($wbwContent)===0){
  727. return false;
  728. }
  729. return \json_encode($wbwContent,JSON_UNESCAPED_UNICODE);
  730. }
  731. /**
  732. * 将句子放进结果列表
  733. */
  734. private function pushSent($result,$sent,$level=0,$mode='read'){
  735. $sentProps = base64_encode(\json_encode($sent)) ;
  736. if($mode === 'read'){
  737. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' />";
  738. }else{
  739. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' />";
  740. }
  741. //增加标题的html标记
  742. if($level>0){
  743. $sentWidget = "<h{$level}>".$sentWidget."</h{$level}>";
  744. }
  745. array_push($result,$sentWidget);
  746. return $result;
  747. }
  748. private function newSent($book,$para,$word_start,$word_end){
  749. $sent = [
  750. "id"=>"{$book}-{$para}-{$word_start}-{$word_end}",
  751. "book"=>$book,
  752. "para"=>$para,
  753. "wordStart"=>$word_start,
  754. "wordEnd"=>$word_end,
  755. "origin"=>[],
  756. "translation"=>[],
  757. ];
  758. #生成channel 数量列表
  759. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  760. $channelCount = CorpusController::sentResCount($book,$para,$word_start,$word_end);
  761. $path = json_decode(PaliText::where('book',$book)->where('paragraph',$para)->value("path"),true);
  762. $sent["path"] = [];
  763. foreach ($path as $key => $value) {
  764. # code...
  765. $value['paliTitle'] = $value['title'];
  766. $sent["path"][] = $value;
  767. }
  768. $sent["tranNum"] = $channelCount['tranNum'];
  769. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  770. $sent["commNum"] = $channelCount['commNum'];
  771. $sent["originNum"] = $channelCount['originNum'];
  772. $sent["simNum"] = $channelCount['simNum'];
  773. return $sent;
  774. }
  775. /**
  776. * 获取某个句子的相关资源的句子数量
  777. */
  778. public static function sentResCount($book,$para,$start,$end){
  779. $sentId = "{$book}-{$para}-{$start}-{$end}";
  780. $channelCount = Cache::remember("/sentence/{$sentId}/channels/count",
  781. env('CACHE_EXPIRE',3600*24),
  782. function() use($book,$para,$start,$end){
  783. $channels = Sentence::where('book_id',$book)
  784. ->where('paragraph',$para)
  785. ->where('word_start',$start)
  786. ->where('word_end',$end)
  787. ->select('channel_uid')
  788. ->groupBy('channel_uid')
  789. ->get();
  790. $channelList = [];
  791. foreach ($channels as $key => $value) {
  792. # code...
  793. if(Str::isUuid($value->channel_uid)){
  794. $channelList[] = $value->channel_uid;
  795. }
  796. }
  797. $simId = PaliSentence::where('book',$book)
  798. ->where('paragraph',$para)
  799. ->where('word_begin',$start)
  800. ->where('word_end',$end)
  801. ->value('id');
  802. if($simId){
  803. $output["simNum"]=SentSimIndex::where('sent_id',$simId)->value('count');
  804. }else{
  805. $output["simNum"]=0;
  806. }
  807. $channelInfo = Channel::whereIn("uid",$channelList)->select('type')->get();
  808. $output["tranNum"]=0;
  809. $output["nissayaNum"]=0;
  810. $output["commNum"]=0;
  811. $output["originNum"]=0;
  812. foreach ($channelInfo as $key => $value) {
  813. # code...
  814. switch($value->type){
  815. case "translation":
  816. $output["tranNum"]++;
  817. break;
  818. case "nissaya":
  819. $output["nissayaNum"]++;
  820. break;
  821. case "commentary":
  822. $output["commNum"]++;
  823. break;
  824. case "original":
  825. $output["originNum"]++;
  826. break;
  827. }
  828. }
  829. return $output;
  830. });
  831. return $channelCount;
  832. }
  833. private function markdownRender($input){
  834. }
  835. /**
  836. * Update the specified resource in storage.
  837. *
  838. * @param \Illuminate\Http\Request $request
  839. * @param \App\Models\Sentence $sentence
  840. * @return \Illuminate\Http\Response
  841. */
  842. public function update(Request $request, Sentence $sentence)
  843. {
  844. //
  845. }
  846. /**
  847. * Remove the specified resource from storage.
  848. *
  849. * @param \App\Models\Sentence $sentence
  850. * @return \Illuminate\Http\Response
  851. */
  852. public function destroy(Sentence $sentence)
  853. {
  854. //
  855. }
  856. }