ExportChapter.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\ProgressChapter;
  6. use App\Models\Channel;
  7. use App\Models\PaliText;
  8. use App\Models\Sentence;
  9. use App\Http\Api\ChannelApi;
  10. use App\Http\Api\MdRender;
  11. use App\Tools\Export;
  12. use Illuminate\Support\Facades\Log;
  13. use App\Tools\RedisClusters;
  14. class ExportChapter extends Command
  15. {
  16. protected $exportStatusKey = 'export/status';
  17. protected $exportStatusExpiry = 3600;
  18. protected $currExportStatusKey = '';
  19. /**
  20. * The name and signature of the console command.
  21. * php artisan export:chapter 213 1913 a19eaf75-c63f-4b84-8125-1bce18311e23 213-1913 --format=html
  22. * php artisan export:chapter 168 915 19f53a65-81db-4b7d-8144-ac33f1217d34 168-915 --format=html
  23. * @var string
  24. */
  25. protected $signature = 'export:chapter {book} {para} {channel} {filename} {--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. * progress: 0-1, error -1
  43. * message: string
  44. */
  45. protected function setStatus($progress,$message=''){
  46. RedisClusters::put($this->currExportStatusKey,
  47. [
  48. 'progress'=>$progress,
  49. 'message'=>$message,
  50. ],
  51. $this->exportStatusExpiry);
  52. }
  53. public function getStatus($filename){
  54. return RedisClusters::get($this->exportStatusKey.'/'.$filename);
  55. }
  56. /**
  57. * Execute the console command.
  58. *
  59. * @return int
  60. */
  61. public function handle()
  62. {
  63. $this->info('task export chapter start');
  64. Log::debug('task export chapter start');
  65. if(\App\Tools\Tools::isStop()){
  66. return 0;
  67. }
  68. MdRender::init();
  69. $this->currExportStatusKey = $this->exportStatusKey . '/' . $this->argument('filename');
  70. switch ($this->option('format')) {
  71. case 'md':
  72. $renderFormat='markdown';
  73. break;
  74. case 'html':
  75. $renderFormat='html';
  76. break;
  77. default:
  78. $renderFormat=$this->option('format');
  79. break;
  80. }
  81. $book = $this->argument('book');
  82. $para = $this->argument('para');
  83. $channelId = $this->argument('channel');
  84. $channel = ChannelApi::getById($channelId);
  85. $chapter = PaliText::where('book',$book)->where('paragraph',$para)->first();
  86. if(!$chapter){
  87. return $this->error("no data");
  88. }
  89. $currProgress = 0;
  90. $this->setStatus($currProgress,'start');
  91. $bookMeta = array();
  92. if(empty($chapter->toc)){
  93. $bookMeta['title'] = "unknown";
  94. }else{
  95. $title = ProgressChapter::where('book',$book)->where('para',$para)
  96. ->where('channel_id',$channelId)
  97. ->value('title');
  98. $bookMeta['book_title'] = $title?$title:$chapter->toc;
  99. $bookMeta['sub_title'] = $chapter->toc;
  100. }
  101. if($channel){
  102. $bookMeta['book_author'] = $channel['name'];
  103. }
  104. $subChapter = PaliText::where('book',$book)->where('parent',$para)
  105. ->where('level','<',8)
  106. ->orderBy('paragraph')
  107. ->get();
  108. if(count($subChapter) === 0){
  109. //没有子章节
  110. $subChapter = PaliText::where('book',$book)->where('paragraph',$para)
  111. ->where('level','<',8)
  112. ->orderBy('paragraph')
  113. ->get();
  114. }
  115. $step = 1 / PaliText::where('book',$book)->where('paragraph',$para)->value('chapter_len');
  116. $sections = array();
  117. foreach ($subChapter as $key => $sub) {
  118. # code...
  119. $chapter = ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
  120. ->where('channel_id',$channelId)
  121. ->first();
  122. if($chapter){
  123. $filename = "{$sub->paragraph}.".$this->option('format');
  124. $bookMeta['sections'][] = ['filename'=>$filename];
  125. $paliTitle = PaliText::where('book',$book)->where('paragraph',$sub->paragraph)->value('toc');
  126. $title = $chapter->title?$chapter->title:$paliTitle;
  127. $content = array();
  128. $chapterStart = $sub->paragraph+1;
  129. $chapterEnd = $sub->paragraph + $sub->chapter_len;
  130. $chapterBody = PaliText::where('book',$book)
  131. ->whereBetween('paragraph',[$chapterStart,$chapterEnd])
  132. ->orderBy('paragraph')->get();
  133. foreach ($chapterBody as $body) {
  134. $currProgress += $step;
  135. $this->setStatus($currProgress,'export chapter '.$body->paragraph);
  136. # code...
  137. $translationData = Sentence::where('book_id',$book)
  138. ->where('paragraph',$body->paragraph)
  139. ->where('channel_uid',$channelId)
  140. ->orderBy('word_start')->get();
  141. $sentContent = array();
  142. foreach ($translationData as $sent) {
  143. $texText = MdRender::render($sent->content,
  144. [$sent->channel_uid],
  145. null,
  146. 'read',
  147. $channel['type'],
  148. $sent->content_type,
  149. $renderFormat
  150. );
  151. $sentContent[] = trim($texText);
  152. }
  153. $paraContent = implode(' ',$sentContent);
  154. if($body->level > 7){
  155. switch ($this->option('format')) {
  156. case 'tex':
  157. $content[] = '\par '.$paraContent;
  158. break;
  159. case 'html':
  160. $content[] = '<p>'.$paraContent.'</p>';
  161. break;
  162. case 'md':
  163. $content[] = "\n\n".$paraContent;
  164. break;
  165. }
  166. }else{
  167. $currLevel = $body->level - $sub->level;
  168. if($currLevel>0){
  169. if(empty($paraContent)){
  170. $subSessionTitle = PaliText::where('book',$book)
  171. ->where('paragraph',$body->paragraph)
  172. ->value('toc');
  173. }else{
  174. $subSessionTitle = $paraContent;
  175. }
  176. switch ($this->option('format')) {
  177. case 'tex':
  178. $subStr = array_fill(0,$currLevel,'sub');
  179. $content[] = '\\'. implode('',$subStr) . "section{".$subSessionTitle.'}';
  180. break;
  181. case 'md':
  182. $subStr = array_fill(0,$currLevel,'#');
  183. $content[] = implode('',$subStr) . " ".$subSessionTitle;
  184. break;
  185. case 'html':
  186. $level = $currLevel+2;
  187. $content[] = "<h{$currLevel}".$subSessionTitle."</h{$currLevel}";
  188. break;
  189. }
  190. }else{
  191. $content[] = '\par '.$paraContent;
  192. }
  193. }
  194. $content[] = "\n\n";
  195. }
  196. $sections[] = [
  197. 'name'=>$filename,
  198. 'body'=>[
  199. 'title'=>$title,
  200. 'content'=>implode('',$content)
  201. ]
  202. ];
  203. }
  204. }
  205. $this->setStatus(0.9,'export content done');
  206. Log::debug('导出结束');
  207. $tex = array();
  208. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
  209. 'delimiters' => '[[ ]]',
  210. 'escape'=>function ($value){
  211. return $value;
  212. }));
  213. $tpl = file_get_contents(resource_path("mustache/".$this->option('format')."/main.".$this->option('format')));
  214. $texContent = $m->render($tpl,$bookMeta);
  215. $tex[] = ['name'=>'main.'.$this->option('format'),
  216. 'content'=>$texContent
  217. ];
  218. foreach ($sections as $key => $section) {
  219. $tpl = file_get_contents(resource_path("mustache/".$this->option('format')."/section.".$this->option('format')));
  220. $texContent = $m->render($tpl,$section['body']);
  221. $tex[] = ['name'=>$section['name'],
  222. 'content'=>$texContent
  223. ];
  224. }
  225. Log::debug('footnote start');
  226. //footnote
  227. $tplFile = resource_path("mustache/".$this->option('format')."/footnote.".$this->option('format'));
  228. if(isset($GLOBALS['note']) &&
  229. is_array($GLOBALS['note']) &&
  230. count($GLOBALS['note'])>0 &&
  231. file_exists($tplFile)){
  232. $tpl = file_get_contents($tplFile);
  233. $texContent = $m->render($tpl,['footnote'=>$GLOBALS['note']]);
  234. $tex[] = ['name'=>'footnote.'.$this->option('format'),
  235. 'content'=>$texContent
  236. ];
  237. }
  238. if($this->option('debug')){
  239. $dir = "export/".$this->option('format')."/{$book}-{$para}-{$channelId}/";
  240. foreach ($tex as $key => $section) {
  241. Storage::disk('local')->put($dir.$section['name'], $section['content']);
  242. }
  243. }
  244. Log::debug('footnote finished');
  245. Log::debug('upload start');
  246. $this->setStatus(0.95,'export content done');
  247. //upload
  248. switch ($this->option('format')) {
  249. case 'tex':
  250. $data = Export::ToPdf($tex);
  251. if($data['ok']){
  252. $filename = "export/".$this->argument('filename');
  253. $this->info($data['content-type']);
  254. Storage::put($filename, $data['data']);
  255. }else{
  256. $this->error($data['code'].'-'.$data['message']);
  257. }
  258. break;
  259. case 'html':
  260. $filename = "export/".$this->argument('filename');
  261. $file = array();
  262. foreach ($tex as $key => $section) {
  263. $file[] = $section['content'];
  264. }
  265. Storage::put($filename, implode('',$file));
  266. break;
  267. }
  268. $this->setStatus(1,'export done');
  269. Log::debug('task export offline chapter-table finished');
  270. return 0;
  271. }
  272. }