CorpusController.php 12 KB

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