CorpusController.php 41 KB

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