ExportChapter.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Str;
  7. use App\Models\ProgressChapter;
  8. use App\Models\Channel;
  9. use App\Models\PaliText;
  10. use App\Models\Sentence;
  11. use App\Http\Api\ChannelApi;
  12. use App\Http\Api\MdRender;
  13. use App\Tools\Export;
  14. use App\Tools\RedisClusters;
  15. use App\Tools\ExportDownload;
  16. class ExportChapter extends Command
  17. {
  18. /**
  19. * The name and signature of the console command.
  20. * php artisan export:chapter 213 3 a19eaf75-c63f-4b84-8125-1bce18311e23 213-3.html --format=html --origin=true
  21. * php artisan export:chapter 168 915 7fea264d-7a26-40f8-bef7-bc95102760fb 168-915.html --format=html --debug
  22. * php artisan export:chapter 168 915 7fea264d-7a26-40f8-bef7-bc95102760fb 168-915.html --format=html --origin=true
  23. * @var string
  24. */
  25. protected $signature = 'export:chapter {book} {para} {channel} {query_id} {--token=} {--origin=false} {--translation=true} {--debug} {--format=tex} ';
  26. /**
  27. * The console command description.
  28. *
  29. * @var string
  30. */
  31. protected $description = 'Command description';
  32. /**
  33. * Create a new command instance.
  34. *
  35. * @return void
  36. */
  37. public function __construct()
  38. {
  39. parent::__construct();
  40. }
  41. /**
  42. * Execute the console command.
  43. *
  44. * @return int
  45. */
  46. public function handle()
  47. {
  48. $this->info('task export chapter start');
  49. Log::debug('task export chapter start');
  50. if(\App\Tools\Tools::isStop()){
  51. return 0;
  52. }
  53. $book = $this->argument('book');
  54. $para = $this->argument('para');
  55. $upload = new ExportDownload([
  56. 'queryId'=>$this->argument('query_id'),
  57. 'format'=>$this->option('format'),
  58. 'debug'=>$this->option('debug'),
  59. 'filename'=>$book.'-'.$para,
  60. ]);
  61. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
  62. 'delimiters' => '[[ ]]',
  63. 'escape'=>function ($value){
  64. return $value;
  65. }));
  66. $tplFile = resource_path("mustache/chapter/".$this->option('format')."/paragraph.".$this->option('format'));
  67. $tplParagraph = file_get_contents($tplFile);
  68. MdRender::init();
  69. switch ($this->option('format')) {
  70. case 'md':
  71. $renderFormat='markdown';
  72. break;
  73. case 'html':
  74. $renderFormat='html';
  75. break;
  76. default:
  77. $renderFormat=$this->option('format');
  78. break;
  79. }
  80. //获取原文channel
  81. $orgChannelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  82. $tranChannelsId = explode('_',$this->argument('channel'));
  83. $channelsId = array_merge([$orgChannelId],$tranChannelsId);
  84. $channels = array();
  85. $channelsIndex = array();
  86. foreach ($channelsId as $key => $id) {
  87. $channels[] = ChannelApi::getById($id);
  88. $channelsIndex[$id] = ChannelApi::getById($id);
  89. }
  90. $bookMeta = array();
  91. $bookMeta['book_author'] = "";
  92. foreach ($channels as $key => $channel) {
  93. $bookMeta['book_author'] .= $channel['name'] . ' ';
  94. }
  95. $chapter = PaliText::where('book',$book)
  96. ->where('paragraph',$para)->first();
  97. if(!$chapter){
  98. return $this->error("no data");
  99. }
  100. $currProgress = 0;
  101. $this->info($upload->setStatus($currProgress,'start'));
  102. if(empty($chapter->toc)){
  103. $bookMeta['title'] = "unknown";
  104. }else{
  105. $bookMeta['book_title'] = '';
  106. foreach ($channelsId as $key => $id) {
  107. $title = ProgressChapter::where('book',$book)->where('para',$para)
  108. ->where('channel_id',$id)
  109. ->value('title');
  110. $bookMeta['book_title'] .= $title;
  111. }
  112. $bookMeta['sub_title'] = $chapter->toc;
  113. }
  114. $subChapter = PaliText::where('book',$book)->where('parent',$para)
  115. ->where('level','<',8)
  116. ->orderBy('paragraph')
  117. ->get();
  118. if(count($subChapter) === 0){
  119. //没有子章节
  120. $subChapter = PaliText::where('book',$book)->where('paragraph',$para)
  121. ->where('level','<',8)
  122. ->orderBy('paragraph')
  123. ->get();
  124. }
  125. $chapterParagraph = PaliText::where('book',$book)->where('paragraph',$para)->value('chapter_len');
  126. if($chapterParagraph >0 ){
  127. $step = 0.9 / $chapterParagraph;
  128. }else{
  129. $step = 0.9;
  130. Log::error('段落长度不能为0',['book'=>$book,'para'=>$para]);
  131. }
  132. $outputChannelsId = [];
  133. if($this->option('origin') === 'true'){
  134. $outputChannelsId[] = $orgChannelId;
  135. }
  136. if($this->option('translation') === 'true'){
  137. $outputChannelsId = array_merge($outputChannelsId,$tranChannelsId);
  138. }
  139. $sections = array();
  140. foreach ($subChapter as $key => $sub) {
  141. # 看这个章节是否存在译文
  142. $hasChapter = false;
  143. if($this->option('origin') === 'true'){
  144. $hasChapter = true;
  145. }
  146. if($this->option('translation') === 'true'){
  147. foreach ($tranChannelsId as $id) {
  148. if(ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
  149. ->where('channel_id',$id)
  150. ->exists()){
  151. $hasChapter = true;
  152. }
  153. }
  154. }
  155. if(!$hasChapter){
  156. //不存在需要导出的数据
  157. continue;
  158. }
  159. $filename = "{$sub->paragraph}.".$this->option('format');
  160. $bookMeta['sections'][] = ['filename'=>$filename];
  161. $paliTitle = PaliText::where('book',$book)
  162. ->where('paragraph',$sub->paragraph)
  163. ->value('toc');
  164. $sectionTitle = $paliTitle;
  165. if($this->option('translation') === 'true'){
  166. $chapter = ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
  167. ->where('channel_id',$tranChannelsId[0])
  168. ->first();
  169. if($chapter && !empty($chapter->title)){
  170. $sectionTitle = $chapter->title;
  171. }
  172. }
  173. $content = array();
  174. $chapterStart = $sub->paragraph+1;
  175. $chapterEnd = $sub->paragraph + $sub->chapter_len;
  176. $chapterBody = PaliText::where('book',$book)
  177. ->whereBetween('paragraph',[$chapterStart,$chapterEnd])
  178. ->orderBy('paragraph')->get();
  179. foreach ($chapterBody as $body) {
  180. $currProgress += $step;
  181. $this->info($upload->setStatus($currProgress,'export chapter '.$body->paragraph));
  182. $paraData = array();
  183. $paraData['translations'] = array();
  184. foreach ($outputChannelsId as $key => $channelId) {
  185. $translationData = Sentence::where('book_id',$book)
  186. ->where('paragraph',$body->paragraph)
  187. ->where('channel_uid',$channelId)
  188. ->orderBy('word_start')->get();
  189. $sentContent = array();
  190. foreach ($translationData as $sent) {
  191. $texText = MdRender::render($sent->content,
  192. [$sent->channel_uid],
  193. null,
  194. 'read',
  195. $channelsIndex[$channelId]['type'],
  196. $sent->content_type,
  197. $renderFormat
  198. );
  199. $sentContent[] = trim($texText);
  200. }
  201. $paraContent = implode(' ',$sentContent);
  202. if($channelsIndex[$channelId]['type'] === 'original'){
  203. $paraData['origin'] = $paraContent;
  204. }else{
  205. $paraData['translations'][] = ['content'=>$paraContent];
  206. }
  207. }
  208. if($body->level > 7){
  209. $content[] = $m->render($tplParagraph,$paraData);
  210. }else{
  211. $currLevel = $body->level - $sub->level;
  212. if($currLevel<=0){
  213. $currLevel = 1;
  214. }
  215. if(count($paraData['translations'])===0){
  216. $subSessionTitle = PaliText::where('book',$book)
  217. ->where('paragraph',$body->paragraph)
  218. ->value('toc');
  219. }else{
  220. $subSessionTitle = $paraData['translations'][0]['content'];
  221. }
  222. switch ($this->option('format')) {
  223. case 'tex':
  224. $subStr = array_fill(0,$currLevel,'sub');
  225. $content[] = '\\'. implode('',$subStr) . "section{".$subSessionTitle.'}';
  226. break;
  227. case 'md':
  228. $subStr = array_fill(0,$currLevel,'#');
  229. $content[] = implode('',$subStr) . " ".$subSessionTitle;
  230. break;
  231. case 'html':
  232. $level = $currLevel+2;
  233. $content[] = "<h{$currLevel}>".$subSessionTitle."</h{$currLevel}>";
  234. break;
  235. }
  236. }
  237. $content[] = "\n\n";
  238. }
  239. $sections[] = [
  240. 'name'=>$filename,
  241. 'body'=>[
  242. 'title'=>$sectionTitle,
  243. 'content'=>implode('',$content)
  244. ]
  245. ];
  246. }
  247. //导出术语表
  248. $keyPali = array();
  249. $keyMeaning = array();
  250. if(isset($GLOBALS['glossary'])){
  251. $glossary = $GLOBALS['glossary'];
  252. foreach ($glossary as $word => $meaning) {
  253. $keyMeaning[$meaning] = $word;
  254. $keyPali[$word] = $meaning;
  255. }
  256. }
  257. ksort($keyPali);
  258. krsort($keyMeaning);
  259. $glossaryData = [];
  260. $glossaryData['pali'] = [];
  261. $glossaryData['meaning'] = [];
  262. foreach ($keyPali as $word => $meaning) {
  263. $glossaryData['pali'][] = ['pali'=>$word,'meaning'=>$meaning];
  264. }
  265. foreach ($keyMeaning as $meaning => $word) {
  266. $glossaryData['meaning'][] = ['pali' => $word,'meaning'=>$meaning];
  267. }
  268. Log::debug('glossary',['data' => $glossaryData]);
  269. $tplFile = resource_path("mustache/chapter/".$this->option('format')."/glossary.".$this->option('format'));
  270. $tplGlossary = file_get_contents($tplFile);
  271. $glossaryContent = $m->render($tplGlossary,$glossaryData);
  272. $sections[] = [
  273. 'name'=>'glossary.'.$this->option('format'),
  274. 'body'=>[
  275. 'title' => 'glossary',
  276. 'content' => $glossaryContent
  277. ]
  278. ];
  279. $this->info($upload->setStatus(0.9,'export content done'));
  280. Log::debug('导出结束',['sections'=>count($sections)]);
  281. $upload->upload('chapter',$sections,$bookMeta);
  282. $this->info($upload->setStatus(1,'export chapter done'));
  283. return 0;
  284. }
  285. }