CorpusController.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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','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','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','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. }
  420. /**
  421. * 获取句子数据
  422. * 算法:
  423. * 1. 如果标题和下一级第一个标题之间有段落。只输出这些段落和子目录
  424. * 2. 如果标题和下一级第一个标题之间没有间隔 且 chapter 长度大于10000个字符 且有子目录,只输出子目录
  425. * 3. 如果二者都不是,lazy load
  426. */
  427. //1. 计算 标题和下一级第一个标题之间 是否有间隔
  428. $nextChapter = PaliText::where('book',$sentId[0])
  429. ->where('paragraph',">",$sentId[1])
  430. ->where('level','<',8)
  431. ->orderBy('paragraph')
  432. ->value('paragraph');
  433. $between = $nextChapter - $sentId[1];
  434. //查找子目录
  435. $chapterLen = $chapter->chapter_len;
  436. $toc = PaliText::where('book',$sentId[0])
  437. ->whereBetween('paragraph',[$paraFrom+1,$paraFrom+$chapterLen-1])
  438. ->where('level','<',8)
  439. ->orderBy('paragraph')
  440. ->select(['book','paragraph','level','toc'])
  441. ->get();
  442. $maxLen = 3000;
  443. if($between > 1){
  444. //有间隔
  445. $paraTo = $nextChapter - 1;
  446. }else{
  447. if($chapter->chapter_strlen > $maxLen){
  448. if(count($toc)>0){
  449. //有子目录只输出标题和目录
  450. $paraTo = $paraFrom;
  451. }else{
  452. //没有子目录 全部输出
  453. }
  454. }else{
  455. //章节小。全部输出 不输出子目录
  456. $toc = [];
  457. }
  458. }
  459. $pFrom = $request->get('from',$paraFrom);
  460. $pTo = $request->get('to',$paraTo);
  461. //根据句子的长度找到这次应该加载的段落
  462. $paliText = PaliText::select(['paragraph','lenght'])
  463. ->where('book',$sentId[0])
  464. ->whereBetween('paragraph',[$pFrom,$pTo])
  465. ->orderBy('paragraph')
  466. ->get();
  467. $sumLen = 0;
  468. $currTo = $pTo;
  469. foreach ($paliText as $para) {
  470. $sumLen += $para->lenght;
  471. if($sumLen > $maxLen){
  472. $currTo = $para->paragraph;
  473. break;
  474. }
  475. }
  476. $record = Sentence::select($this->selectCol)
  477. ->where('book_id',$sentId[0])
  478. ->whereBetween('paragraph',[$pFrom,$currTo])
  479. ->whereIn('channel_uid',$channels)
  480. ->orderBy('paragraph')
  481. ->orderBy('word_start')
  482. ->get();
  483. if(count($record) ===0){
  484. return $this->error("no data");
  485. }
  486. $this->result['content'] = $this->makeContent($record,$mode,$indexChannel,$indexedHeading,false,true);
  487. if(!$request->has('from')){
  488. //第一次才显示toc
  489. $this->result['toc'] = TocResource::collection($toc);
  490. }
  491. if($currTo < $pTo){
  492. $this->result['from'] = $currTo+1;
  493. $this->result['to'] = $pTo;
  494. $this->result['paraId'] = $id;
  495. $this->result['channels'] = $request->get('channels');
  496. $this->result['mode'] = $request->get('mode');
  497. }
  498. return $this->ok($this->result);
  499. }
  500. private function getChannelIndex($channels,$type=null){
  501. #获取channel索引表
  502. $channelInfo = Channel::whereIn("uid",$channels)
  503. ->select(['uid','type','name','owner_uid'])->get();
  504. $indexChannel = [];
  505. foreach ($channels as $key => $channelId) {
  506. $channelInfo = Channel::where("uid",$channelId)
  507. ->select(['uid','type','name','owner_uid'])->first();
  508. if(!$channelInfo){
  509. Log::error('no channel id'.$channelId);
  510. continue;
  511. }
  512. if($type !== null && $channelInfo->type !== $type){
  513. continue;
  514. }
  515. $indexChannel[$channelId] = $channelInfo;
  516. $indexChannel[$channelId]->studio = StudioApi::getById($channelInfo->owner_uid);
  517. }
  518. return $indexChannel;
  519. }
  520. /**
  521. * 根据句子库数据生成文章内容
  522. * $record 句子数据
  523. * $mode read | edit | wbw
  524. * $indexChannel channel索引
  525. * $indexedHeading 标题索引 用于给段落加标题标签 <h1> ect.
  526. */
  527. private function makeContent($record,$mode,$indexChannel,$indexedHeading=[],$onlyProps=false,$paraMark=false,$format='react'){
  528. $content = [];
  529. $lastSent = "0-0";
  530. $sentCount = 0;
  531. $sent = [];
  532. $sent["origin"] = [];
  533. $sent["translation"] = [];
  534. //获取句子编号列表
  535. $sentList = [];
  536. foreach ($record as $key => $value) {
  537. $currSentId = "{$value->book_id}-{$value->paragraph}-{$value->word_start}-{$value->word_end}";
  538. $sentList[$currSentId]=[$value->book_id ,$value->paragraph,$value->word_start,$value->word_end];
  539. $value->sid = "{$currSentId}_{$value->channel_uid}";
  540. }
  541. $channelsId = array();
  542. $count = 0;
  543. foreach ($indexChannel as $channelId => $info){
  544. if($count>0){
  545. $channelsId[] = $channelId;
  546. }
  547. $count++;
  548. }
  549. //遍历列表查找每个句子的所有channel的数据,并填充
  550. $currPara = "";
  551. foreach ($sentList as $currSentId => $arrSentId) {
  552. $para = $arrSentId[0]."-".$arrSentId[1];
  553. if($currPara !== $para){
  554. $currPara = $para;
  555. //输出段落标记
  556. if($paraMark){
  557. $sentInPara = array();
  558. foreach ($sentList as $sentId => $sentParam) {
  559. if($sentParam[0]===$arrSentId[0] &&
  560. $sentParam[1]===$arrSentId[1]){
  561. $sentInPara[] = $sentId;
  562. }
  563. }
  564. //输出段落起始
  565. if(!empty($currPara)){
  566. $content[] = '</MdTpl>';
  567. }
  568. $markProps = base64_encode(\json_encode([
  569. 'book'=>$arrSentId[0],
  570. 'para'=>$arrSentId[1],
  571. 'channels'=>$channelsId,
  572. 'sentences'=>$sentInPara,
  573. ])) ;
  574. $content[] = "<MdTpl tpl='para-shell' props='{$markProps}' >";
  575. }
  576. }
  577. $sent = $this->newSent($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3]);
  578. foreach ($indexChannel as $channelId => $info) {
  579. # code...
  580. $sid = "{$currSentId}_{$channelId}";
  581. if(isset($info->studio)){
  582. $studioInfo = $info->studio;
  583. }else{
  584. $studioInfo = null;
  585. }
  586. $newSent = [
  587. "content"=>"",
  588. "html"=> "",
  589. "book"=> $arrSentId[0],
  590. "para"=> $arrSentId[1],
  591. "wordStart"=> $arrSentId[2],
  592. "wordEnd"=> $arrSentId[3],
  593. "channel"=> [
  594. "name"=>$info->name,
  595. "type"=>$info->type,
  596. "id"=> $info->uid,
  597. ],
  598. "studio" => $studioInfo,
  599. "updateAt"=> "",
  600. "suggestionCount" => SuggestionApi::getCountBySent($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3],$channelId),
  601. ];
  602. $row = Arr::first($record,function($value,$key) use($sid){
  603. return $value->sid===$sid;
  604. });
  605. if($row){
  606. $newSent['id'] = $row->uid;
  607. $newSent['content'] = $row->content;
  608. $newSent['contentType'] = $row->content_type;
  609. $newSent['html'] = '';
  610. $newSent["editor"]=UserApi::getByUuid($row->editor_uid);
  611. /**
  612. * TODO 刷库改数据
  613. * 旧版api没有更新updated_at所以造成旧版的数据updated_at数据比modify_time 要晚
  614. */
  615. $newSent['forkAt'] = $row->fork_at; //
  616. $newSent['updateAt'] = $row->updated_at; //
  617. $newSent['updateAt'] = date("Y-m-d H:i:s.",$row->modify_time/1000).($row->modify_time%1000)." UTC";
  618. $newSent['createdAt'] = $row->created_at;
  619. if($mode !== "read"){
  620. if(isset($row->acceptor_uid) && !empty($row->acceptor_uid)){
  621. $newSent["acceptor"]=UserApi::getByUuid($row->acceptor_uid);
  622. $newSent["prEditAt"]=$row->pr_edit_at;
  623. }
  624. }
  625. switch ($info->type) {
  626. case 'wbw':
  627. case 'original':
  628. //
  629. // 在编辑模式下。
  630. // 如果是原文,查看是否有逐词解析数据,
  631. // 有的话优先显示。
  632. // 阅读模式直接显示html原文
  633. // 传过来的数据一定有一个原文channel
  634. //
  635. if($mode === "read"){
  636. $newSent['content'] = "";
  637. $newSent['html'] = MdRender::render($row->content,[$row->channel_uid],
  638. null,$mode,"translation",
  639. $row->content_type,$format);
  640. }else{
  641. if($row->content_type==='json'){
  642. $newSent['channel']['type'] = "wbw";
  643. if(isset($this->wbwChannels[0])){
  644. $newSent['channel']['name'] = $indexChannel[$this->wbwChannels[0]]->name;
  645. $newSent['channel']['id'] = $this->wbwChannels[0];
  646. //存在一个translation channel
  647. //尝试查找逐词解析数据。找到,替换现有数据
  648. $wbwData = $this->getWbw($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3],
  649. $this->wbwChannels[0]);
  650. if($wbwData){
  651. $newSent['content'] = $wbwData;
  652. $newSent['contentType'] = 'json';
  653. $newSent['html'] = "";
  654. }
  655. }
  656. }else{
  657. $newSent['content'] = $row->content;
  658. $newSent['html'] = MdRender::render($row->content,[$row->channel_uid],
  659. null,$mode,"translation",
  660. $row->content_type,$format);
  661. }
  662. }
  663. break;
  664. case 'nissaya':
  665. $newSent['html'] = RedisClusters::remember("/sent/{$channelId}/{$currSentId}/{$format}",
  666. config('mint.cache.expire'),
  667. function() use($row,$mode,$format){
  668. return MdRender::render($row->content,[$row->channel_uid],
  669. null,$mode,"nissaya",
  670. $row->content_type,$format);
  671. });
  672. break;
  673. default:
  674. /**
  675. * 译文需要markdown渲染
  676. * 包涵术语的不用cache
  677. */
  678. if(strpos($row->content,'[[')===false){
  679. $newSent['html'] = MdRender::render($row->content,[$row->channel_uid],
  680. null,$mode,'translation',
  681. $row->content_type,$format);
  682. /*
  683. RedisClusters::remember("/sent/{$channelId}/{$currSentId}/{$format}",
  684. config('mint.cache.expire'),
  685. function() use($row,$mode,$format){
  686. return MdRender::render($row->content,[$row->channel_uid],
  687. null,$mode,'translation',
  688. $row->content_type,$format);
  689. });
  690. */
  691. }else{
  692. $mdRender = new MdRender(
  693. [
  694. 'debug'=>$this->debug,
  695. 'format'=>$format,
  696. 'mode'=>$mode,
  697. 'channelType'=>'translation',
  698. 'contentType'=>$row->content_type,
  699. ]);
  700. $newSent['html'] = $mdRender->convert($row->content,[$row->channel_uid]);
  701. }
  702. break;
  703. }
  704. }
  705. switch ($info->type) {
  706. case 'wbw':
  707. case 'original':
  708. array_push($sent["origin"],$newSent);
  709. break;
  710. default:
  711. array_push($sent["translation"],$newSent);
  712. break;
  713. }
  714. }
  715. if($onlyProps){
  716. return $sent;
  717. }
  718. $content = $this->pushSent($content,$sent,0,$mode);
  719. }
  720. if($paraMark){
  721. $content[] = '</MdTpl>';
  722. }
  723. $output = \implode("",$content);
  724. return "<div>{$output}</div>";
  725. }
  726. public function getWbw($book,$para,$start,$end,$channel){
  727. /**
  728. * 非阅读模式下。原文使用逐词解析数据。
  729. * 优先加载第一个translation channel 如果没有。加载默认逐词解析。
  730. */
  731. //获取逐词解析数据
  732. $wbwBlock = WbwBlock::where('channel_uid',$channel)
  733. ->where('book_id',$book)
  734. ->where('paragraph',$para)
  735. ->select('uid')
  736. ->first();
  737. if(!$wbwBlock){
  738. return false;
  739. }
  740. //找到逐词解析数据
  741. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  742. ->whereBetween('wid',[$start,$end])
  743. ->select(['book_id','paragraph','wid','data','uid'])
  744. ->orderBy('wid')
  745. ->get();
  746. $wbwContent = [];
  747. foreach ($wbwData as $wbwrow) {
  748. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  749. $wbw = str_replace("<br>",' ',$wbw);
  750. $xmlString = "<root>" . $wbw . "</root>";
  751. try{
  752. $xmlWord = simplexml_load_string($xmlString);
  753. }catch(Exception $e){
  754. continue;
  755. }
  756. $wordsList = $xmlWord->xpath('//word');
  757. foreach ($wordsList as $word) {
  758. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  759. $case = \str_replace('$$','$',$case);
  760. $case = trim($case);
  761. $case = trim($case,"$");
  762. $wbwId = explode('-',$word->id->__toString());
  763. $wbwData = [
  764. 'uid'=>$wbwrow->uid,
  765. 'book'=>$wbwrow->book_id,
  766. 'para'=>$wbwrow->paragraph,
  767. 'sn'=> array_slice($wbwId,2),
  768. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  769. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  770. 'meaning'=> ['value'=>$word->mean->__toString() ,'status'=>0],
  771. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  772. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  773. 'case'=> ['value'=>$word->case->__toString(),'status'=>0],
  774. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  775. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  776. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  777. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  778. 'confidence'=> $word->cf->__toString(),
  779. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  780. ];
  781. if(isset($word->parent2)){
  782. $wbwData['parent2']['value'] = $word->parent2->__toString();
  783. if(isset($word->parent2['status'])){
  784. $wbwData['parent2']['status'] = (int)$word->parent2['status'];
  785. }else{
  786. $wbwData['parent2']['status'] = 0;
  787. }
  788. }
  789. if(isset($word->pg)){
  790. $wbwData['grammar2']['value'] = $word->pg->__toString();
  791. if(isset($word->pg['status'])){
  792. $wbwData['grammar2']['status'] = (int)$word->pg['status'];
  793. }else{
  794. $wbwData['grammar2']['status'] = 0;
  795. }
  796. }
  797. if(isset($word->rela)){
  798. $wbwData['relation']['value'] = $word->rela->__toString();
  799. if(isset($word->rela['status'])){
  800. $wbwData['relation']['status'] = (int)$word->rela['status'];
  801. }else{
  802. $wbwData['relation']['status'] = 7;
  803. }
  804. }
  805. if(isset($word->bmt)){
  806. $wbwData['bookMarkText']['value'] = $word->bmt->__toString();
  807. if(isset($word->bmt['status'])){
  808. $wbwData['bookMarkText']['status'] = (int)$word->bmt['status'];
  809. }else{
  810. $wbwData['bookMarkText']['status'] = 7;
  811. }
  812. }
  813. if(isset($word->bmc)){
  814. $wbwData['bookMarkColor']['value'] = $word->bmc->__toString();
  815. if(isset($word->bmc['status'])){
  816. $wbwData['bookMarkColor']['status'] = (int)$word->bmc['status'];
  817. }else{
  818. $wbwData['bookMarkColor']['status'] = 7;
  819. }
  820. }
  821. if(isset($word->note)){
  822. $wbwData['note']['value'] = $word->note->__toString();
  823. if(isset($word->note['status'])){
  824. $wbwData['note']['status'] = (int)$word->note['status'];
  825. }else{
  826. $wbwData['note']['status'] = 7;
  827. }
  828. }
  829. if(isset($word->cf)){
  830. $wbwData['confidence'] = (float)$word->cf->__toString();
  831. }
  832. if(isset($word->attachments)){
  833. $wbwData['attachments'] = json_decode($word->attachments->__toString());
  834. }
  835. if(isset($word->pali['status'])){
  836. $wbwData['word']['status'] = (int)$word->pali['status'];
  837. }
  838. if(isset($word->real['status'])){
  839. $wbwData['real']['status'] = (int)$word->real['status'];
  840. }
  841. if(isset($word->mean['status'])){
  842. $wbwData['meaning']['status'] = (int)$word->mean['status'];
  843. }
  844. if(isset($word->type['status'])){
  845. $wbwData['type']['status'] = (int)$word->type['status'];
  846. }
  847. if(isset($word->gramma['status'])){
  848. $wbwData['grammar']['status'] = (int)$word->gramma['status'];
  849. }
  850. if(isset($word->case['status'])){
  851. $wbwData['case']['status'] = (int)$word->case['status'];
  852. }
  853. if(isset($word->parent['status'])){
  854. $wbwData['parent']['status'] = (int)$word->parent['status'];
  855. }
  856. if(isset($word->org['status'])){
  857. $wbwData['factors']['status'] = (int)$word->org['status'];
  858. }
  859. if(isset($word->om['status'])){
  860. $wbwData['factorMeaning']['status'] = (int)$word->om['status'];
  861. }
  862. $wbwContent[] = $wbwData;
  863. }
  864. }
  865. if(count($wbwContent)===0){
  866. return false;
  867. }
  868. return \json_encode($wbwContent,JSON_UNESCAPED_UNICODE);
  869. }
  870. /**
  871. * 将句子放进结果列表
  872. */
  873. private function pushSent($result,$sent,$level=0,$mode='read'){
  874. $sentProps = base64_encode(\json_encode($sent)) ;
  875. if($mode === 'read'){
  876. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' ></MdTpl>";
  877. }else{
  878. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' ></MdTpl>";
  879. }
  880. //增加标题的html标记
  881. if($level>0){
  882. $sentWidget = "<h{$level}>".$sentWidget."</h{$level}>";
  883. }
  884. array_push($result,$sentWidget);
  885. return $result;
  886. }
  887. private function newSent($book,$para,$word_start,$word_end){
  888. $sent = [
  889. "id"=>"{$book}-{$para}-{$word_start}-{$word_end}",
  890. "book"=>$book,
  891. "para"=>$para,
  892. "wordStart"=>$word_start,
  893. "wordEnd"=>$word_end,
  894. "origin"=>[],
  895. "translation"=>[],
  896. ];
  897. if($book<1000){
  898. #生成channel 数量列表
  899. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  900. $channelCount = CorpusController::_sentCanReadCount($book,$para,$word_start,$word_end,$this->userUuid);
  901. $path = json_decode(PaliText::where('book',$book)->where('paragraph',$para)->value("path"),true);
  902. $sent["path"] = [];
  903. foreach ($path as $key => $value) {
  904. # code...
  905. $value['paliTitle'] = $value['title'];
  906. $sent["path"][] = $value;
  907. }
  908. $sent["tranNum"] = $channelCount['tranNum'];
  909. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  910. $sent["commNum"] = $channelCount['commNum'];
  911. $sent["originNum"] = $channelCount['originNum'];
  912. $sent["simNum"] = $channelCount['simNum'];
  913. }
  914. return $sent;
  915. }
  916. public static function _sentCanReadCount($book,$para,$start,$end,$userUuid=null){
  917. $keyCanRead="/channel/can-read/";
  918. if($userUuid){
  919. $keyCanRead .= $userUuid;
  920. }else{
  921. $keyCanRead .= 'guest';
  922. }
  923. $channelCanRead = RedisClusters::remember($keyCanRead,
  924. config('mint.cache.expire'),
  925. function() use($userUuid){
  926. return ChannelApi::getCanReadByUser($userUuid);
  927. });
  928. $channels = Sentence::where('book_id',$book)
  929. ->where('paragraph',$para)
  930. ->where('word_start',$start)
  931. ->where('word_end',$end)
  932. ->where('strlen','<>',0)
  933. ->whereIn('channel_uid',$channelCanRead)
  934. ->select('channel_uid')
  935. ->groupBy('channel_uid')
  936. ->get();
  937. $channelList = [];
  938. foreach ($channels as $key => $value) {
  939. # code...
  940. if(Str::isUuid($value->channel_uid)){
  941. $channelList[] = $value->channel_uid;
  942. }
  943. }
  944. $simId = PaliSentence::where('book',$book)
  945. ->where('paragraph',$para)
  946. ->where('word_begin',$start)
  947. ->where('word_end',$end)
  948. ->value('id');
  949. if($simId){
  950. $output["simNum"]=SentSimIndex::where('sent_id',$simId)->value('count');
  951. }else{
  952. $output["simNum"]=0;
  953. }
  954. $channelInfo = Channel::whereIn("uid",$channelList)->select('type')->get();
  955. $output["tranNum"]=0;
  956. $output["nissayaNum"]=0;
  957. $output["commNum"]=0;
  958. $output["originNum"]=0;
  959. foreach ($channelInfo as $key => $value) {
  960. # code...
  961. switch($value->type){
  962. case "translation":
  963. $output["tranNum"]++;
  964. break;
  965. case "nissaya":
  966. $output["nissayaNum"]++;
  967. break;
  968. case "commentary":
  969. $output["commNum"]++;
  970. break;
  971. case "original":
  972. $output["originNum"]++;
  973. break;
  974. }
  975. }
  976. return $output;
  977. }
  978. /**
  979. * 获取某个句子的相关资源的句子数量
  980. */
  981. public static function sentCanReadCount($book,$para,$start,$end,$userUuid=null){
  982. $sentId = "{$book}-{$para}-{$start}-{$end}";
  983. $hKey = "/sentence/res-count/{$sentId}/";
  984. if($userUuid){
  985. $key = $userUuid;
  986. }else{
  987. $key = 'guest';
  988. }
  989. if(Redis::hExists($hKey,$key)){
  990. return json_decode(Redis::hGet($hKey,$key),true);
  991. }else{
  992. $channelCount = CorpusController::_sentCanReadCount($book,$para,$start,$end,$userUuid);
  993. Redis::hSet($hKey,$key,json_encode($channelCount));
  994. return $channelCount;
  995. }
  996. }
  997. private function markdownRender($input){
  998. }
  999. /**
  1000. * Update the specified resource in storage.
  1001. *
  1002. * @param \Illuminate\Http\Request $request
  1003. * @param \App\Models\Sentence $sentence
  1004. * @return \Illuminate\Http\Response
  1005. */
  1006. public function update(Request $request, Sentence $sentence)
  1007. {
  1008. //
  1009. }
  1010. /**
  1011. * Remove the specified resource from storage.
  1012. *
  1013. * @param \App\Models\Sentence $sentence
  1014. * @return \Illuminate\Http\Response
  1015. */
  1016. public function destroy(Sentence $sentence)
  1017. {
  1018. //
  1019. }
  1020. }