CorpusController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Sentence;
  4. use App\Models\Channel;
  5. use App\Models\PaliText;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Http\Api\MdRender;
  9. use Illuminate\Support\Facades\Log;
  10. class CorpusController extends Controller
  11. {
  12. protected $result = [
  13. "uid"=> '',
  14. "title"=> '',
  15. "path"=>[],
  16. "sub_title"=> '',
  17. "summary"=> '',
  18. "content"=> '',
  19. "content_type"=> "html",
  20. "status"=>30,
  21. "lang"=> "",
  22. "created_at"=> "",
  23. "updated_at"=> "",
  24. ];
  25. protected $selectCol = ['book_id','paragraph','word_start',"word_end",'channel_uid','content','updated_at'];
  26. public function __construct()
  27. {
  28. }
  29. /**
  30. * Display a listing of the resource.
  31. *
  32. * @return \Illuminate\Http\Response
  33. */
  34. public function index()
  35. {
  36. //
  37. }
  38. /**
  39. * Store a newly created resource in storage.
  40. *
  41. * @param \Illuminate\Http\Request $request
  42. * @return \Illuminate\Http\Response
  43. */
  44. public function store(Request $request)
  45. {
  46. //
  47. }
  48. /**
  49. * Display the specified resource.
  50. *
  51. * @param \App\Models\Sentence $sentence
  52. * @return \Illuminate\Http\Response
  53. */
  54. public function show(Sentence $sentence)
  55. {
  56. //
  57. }
  58. public function getSentTpl($id,$channels){
  59. $sent = [];
  60. $sentId = \explode('-',$id);
  61. array_push($channels,config("app.admin.cs_channel"));
  62. $record = Sentence::select($this->selectCol)
  63. ->where('book_id',$sentId[0])
  64. ->where('paragraph',$sentId[1])
  65. ->where('word_start',$sentId[2])
  66. ->where('word_end',$sentId[3])
  67. ->whereIn('channel_uid',$channels)
  68. ->get();
  69. Log::info("sent count:".count($record));
  70. $channelIndex = $this->getChannelIndex($channels);
  71. $content = $this->makeContent($record,"edit",$channelIndex);
  72. return $content;
  73. }
  74. /**
  75. * Display the specified resource.
  76. *
  77. * @param string $id
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function showSent($id)
  81. {
  82. //
  83. $param = \explode('_',$id);
  84. if(count($param)>1){
  85. $channels = array_slice($param,1);
  86. }else{
  87. $channels = [];
  88. }
  89. $this->result['content'] = getSentTpl($param[0],$channels);
  90. return $this->ok($this->result);
  91. }
  92. public function showSentences($type,$id,$mode='read'){
  93. $param = \explode('_',$id);
  94. $sentId = \explode('-',$param[0]);
  95. $channels = [];
  96. #获取channel类型
  97. $sentChannel = Sentence::select('channel_uid')
  98. ->where('book_id',$sentId[0])
  99. ->where('paragraph',$sentId[1])
  100. ->where('word_start',$sentId[2])
  101. ->where('word_end',$sentId[3])
  102. ->get();
  103. foreach ($sentChannel as $key => $value) {
  104. # code...
  105. $channels[] = $value->channel_uid;
  106. }
  107. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  108. $indexChannel = [];
  109. $channels = [];
  110. foreach ($channelInfo as $key => $value) {
  111. # code...
  112. if($value->type === $type){
  113. $indexChannel[$value->uid] = $value;
  114. $channels[] = $value->uid;
  115. }
  116. }
  117. //获取句子数据
  118. $record = Sentence::select($this->selectCol)
  119. ->where('book_id',$sentId[0])
  120. ->where('paragraph',$sentId[1])
  121. ->where('word_start',$sentId[2])
  122. ->where('word_end',$sentId[3])
  123. ->whereIn('channel_uid',$channels)
  124. ->orderBy('paragraph')
  125. ->orderBy('word_start')
  126. ->get();
  127. if(count($record) ===0){
  128. return $this->error("no data");
  129. }
  130. $this->result['content'] = $this->makeContent($record,$mode,$indexChannel);
  131. return $this->ok($this->result);
  132. }
  133. public function showChapter($id,$mode='read')
  134. {
  135. //
  136. $param = \explode('_',$id);
  137. $sentId = \explode('-',$param[0]);
  138. $channels = [];
  139. if(count($param)>1){
  140. $channels = array_slice($param,1);
  141. }
  142. $channels[] = config("app.admin.cs_channel");
  143. $chapter = PaliText::where('book',$sentId[0])->where('paragraph',$sentId[1])->first();
  144. if(!$chapter){
  145. return $this->error("no data");
  146. }
  147. if(empty($chapter->toc)){
  148. $this->result['title'] = "unknown";
  149. }else{
  150. $this->result['title'] = $chapter->toc;
  151. $this->result['sub_title'] = $chapter->toc;
  152. $this->result['path'] = json_decode($chapter->path);
  153. }
  154. $paraFrom = $sentId[1];
  155. $paraTo = $sentId[1]+$chapter->chapter_len-1;
  156. //获取标题
  157. $heading = PaliText::select(["book","paragraph","level"])
  158. ->where('book',$sentId[0])
  159. ->whereBetween('paragraph',[$paraFrom,$paraTo])
  160. ->where('level','<',8)
  161. ->get();
  162. //将标题段落转成索引数组 以便输出标题层级
  163. $indexedHeading = [];
  164. foreach ($heading as $key => $value) {
  165. # code...
  166. $indexedHeading["{$value->book}-{$value->paragraph}"] = $value->level;
  167. }
  168. #获取channel索引表
  169. $tranChannels = [];
  170. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  171. $indexChannel = [];
  172. foreach ($channelInfo as $key => $value) {
  173. # code...
  174. $indexChannel[$value->uid] = $value;
  175. if($value->type==="translation" ){
  176. $tranChannels[] = $value->uid;
  177. }
  178. }
  179. $title = Sentence::select($this->selectCol)
  180. ->where('book_id',$sentId[0])
  181. ->where('paragraph',$sentId[1])
  182. ->whereIn('channel_uid',$tranChannels)
  183. ->first();
  184. if($title){
  185. $this->result['title'] = MdRender::render($title->content,$title->channel_uid);
  186. }
  187. //获取句子数据
  188. $record = Sentence::select($this->selectCol)
  189. ->where('book_id',$sentId[0])
  190. ->whereBetween('paragraph',[$paraFrom,$paraTo])
  191. ->whereIn('channel_uid',$channels)
  192. ->orderBy('paragraph')
  193. ->orderBy('word_start')
  194. ->get();
  195. if(count($record) ===0){
  196. return $this->error("no data");
  197. }
  198. $this->result['content'] = $this->makeContent($record,$mode,$indexChannel,$indexedHeading);
  199. return $this->ok($this->result);
  200. }
  201. private function getChannelIndex($channels){
  202. #获取channel索引表
  203. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  204. $indexChannel = [];
  205. foreach ($channelInfo as $key => $value) {
  206. # code...
  207. $indexChannel[$value->uid] = $value;
  208. }
  209. return $indexChannel;
  210. }
  211. private function makeContent($record,$mode,$indexChannel,$indexedHeading=[]){
  212. $content = [];
  213. $lastSent = "0-0";
  214. $sentCount = 0;
  215. foreach ($record as $key => $value) {
  216. # 遍历结果生成html文件
  217. $currSentId = $value->book_id.'-'.$value->paragraph.'-'.$value->word_start.'-'.$value->word_end;
  218. if($currSentId !== $lastSent){
  219. if($sentCount > 0){
  220. //保存上一个句子
  221. //增加标题的html标记
  222. $level = 0;
  223. if(isset($indexedHeading["{$value->book_id}-{$value->paragraph}"])){
  224. $level = $indexedHeading["{$value->book_id}-{$value->paragraph}"];
  225. }
  226. $content = $this->pushSent($content,$sent,$level,$mode);
  227. }
  228. //新建句子
  229. $sent = $this->newSent($value->book_id,$value->paragraph,$value->word_start,$value->word_end);
  230. $lastSent = $currSentId;
  231. }
  232. $newSent = [
  233. "content"=>$value->content,
  234. "html"=> Cache::remember("/sent/{$value->channel_uid}/{$currSentId}",1,
  235. function() use($value){
  236. return MdRender::render($value->content,$value->channel_uid);
  237. }),
  238. "book"=> $value->book_id,
  239. "para"=> $value->paragraph,
  240. "wordStart"=> $value->word_start,
  241. "wordEnd"=> $value->word_end,
  242. "editor"=> [
  243. 'id'=>$value->editor_uid,
  244. 'nickName'=>'nickname',
  245. 'realName'=>'realName',
  246. 'avatar'=>'',
  247. ],
  248. "channel"=> [
  249. "name"=>$indexChannel[$value->channel_uid]->name,
  250. "id"=> $value->channel_uid,
  251. ],
  252. "updateAt"=> $value->updated_at,
  253. ];
  254. switch ($indexChannel[$value->channel_uid]->type) {
  255. case 'original';
  256. array_push($sent["origin"],$newSent);
  257. break;
  258. default:
  259. array_push($sent["translation"],$newSent);
  260. break;
  261. }
  262. $sentCount++;
  263. }
  264. $content = $this->pushSent($content,$sent,0,$mode);
  265. return \implode("",$content);
  266. }
  267. private function pushSent($result,$sent,$level=0,$mode='read'){
  268. $sentProps = base64_encode(\json_encode($sent)) ;
  269. $sentWidget = "<MdTpl tpl='sent{$mode}' props='{$sentProps}' />";
  270. //增加标题的html标记
  271. if($level>0){
  272. $sentWidget = "<h{$level}>".$sentWidget."</h{$level}>";
  273. }
  274. array_push($result,$sentWidget);
  275. return $result;
  276. }
  277. private function newSent($book,$para,$word_start,$word_end){
  278. $sent = [
  279. "id"=>"{$book}-{$para}-{$word_start}-{$word_end}",
  280. "origin"=>[],
  281. "translation"=>[],
  282. ];
  283. #生成channel 数量列表
  284. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  285. $channelCount = Cache::remember("/sent1/{$sentId}/channels/count",
  286. 60,
  287. function() use($book,$para,$word_start,$word_end){
  288. $channels = Sentence::where('book_id',$book)
  289. ->where('paragraph',$para)
  290. ->where('word_start',$word_start)
  291. ->where('word_end',$word_end)
  292. ->select('channel_uid')
  293. ->groupBy('channel_uid')
  294. ->get();
  295. $channelList = [];
  296. foreach ($channels as $key => $value) {
  297. # code...
  298. $channelList[] = $value->channel_uid;
  299. }
  300. $channelInfo = Channel::whereIn("uid",$channelList)->select('type')->get();
  301. $output["tranNum"]=0;
  302. $output["nissayaNum"]=0;
  303. $output["commNum"]=0;
  304. $output["originNum"]=0;
  305. foreach ($channelInfo as $key => $value) {
  306. # code...
  307. switch($value->type){
  308. case "translation":
  309. $output["tranNum"]++;
  310. break;
  311. case "nissaya":
  312. $output["nissayaNum"]++;
  313. break;
  314. case "commentary":
  315. $output["commNum"]++;
  316. break;
  317. case "original":
  318. $output["originNum"]++;
  319. break;
  320. }
  321. }
  322. return $output;
  323. });
  324. $sent["tranNum"] = $channelCount['tranNum'];
  325. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  326. $sent["commNum"] = $channelCount['commNum'];
  327. $sent["originNum"] = $channelCount['originNum'];
  328. return $sent;
  329. }
  330. private function markdownRender($input){
  331. }
  332. /**
  333. * Update the specified resource in storage.
  334. *
  335. * @param \Illuminate\Http\Request $request
  336. * @param \App\Models\Sentence $sentence
  337. * @return \Illuminate\Http\Response
  338. */
  339. public function update(Request $request, Sentence $sentence)
  340. {
  341. //
  342. }
  343. /**
  344. * Remove the specified resource from storage.
  345. *
  346. * @param \App\Models\Sentence $sentence
  347. * @return \Illuminate\Http\Response
  348. */
  349. public function destroy(Sentence $sentence)
  350. {
  351. //
  352. }
  353. }