CorpusController.php 41 KB

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