ExportChapter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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=markdown} ';
  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/md/paragraph.md");
  67. $tplParagraph = file_get_contents($tplFile);
  68. MdRender::init();
  69. $renderFormat='markdown';
  70. //获取原文channel
  71. $orgChannelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  72. $tranChannelsId = explode('_',$this->argument('channel'));
  73. $channelsId = array_merge([$orgChannelId],$tranChannelsId);
  74. $channels = array();
  75. $channelsIndex = array();
  76. foreach ($channelsId as $key => $id) {
  77. $channels[] = ChannelApi::getById($id);
  78. $channelsIndex[$id] = ChannelApi::getById($id);
  79. }
  80. $bookMeta = array();
  81. $bookMeta['book_author'] = "";
  82. foreach ($channels as $key => $channel) {
  83. $bookMeta['book_author'] .= $channel['name'] . ' ';
  84. }
  85. $chapter = PaliText::where('book',$book)
  86. ->where('paragraph',$para)->first();
  87. if(!$chapter){
  88. return $this->error("no data");
  89. }
  90. $currProgress = 0;
  91. $this->info($upload->setStatus($currProgress,'start'));
  92. if(empty($chapter->toc)){
  93. $bookMeta['title'] = "unknown";
  94. }else{
  95. $bookMeta['book_title'] = '';
  96. foreach ($channelsId as $key => $id) {
  97. $title = ProgressChapter::where('book',$book)->where('para',$para)
  98. ->where('channel_id',$id)
  99. ->value('title');
  100. $bookMeta['book_title'] .= $title;
  101. }
  102. $bookMeta['sub_title'] = $chapter->toc;
  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. $chapterParagraph = PaliText::where('book',$book)->where('paragraph',$para)->value('chapter_len');
  116. if($chapterParagraph >0 ){
  117. $step = 0.9 / $chapterParagraph;
  118. }else{
  119. $step = 0.9;
  120. Log::error('段落长度不能为0',['book'=>$book,'para'=>$para]);
  121. }
  122. $outputChannelsId = [];
  123. if($this->option('origin') === 'true'){
  124. $outputChannelsId[] = $orgChannelId;
  125. }
  126. if($this->option('translation') === 'true'){
  127. $outputChannelsId = array_merge($outputChannelsId,$tranChannelsId);
  128. }
  129. $sections = array();
  130. foreach ($subChapter as $key => $sub) {
  131. # 看这个章节是否存在译文
  132. $hasChapter = false;
  133. if($this->option('origin') === 'true'){
  134. $hasChapter = true;
  135. }
  136. if($this->option('translation') === 'true'){
  137. foreach ($tranChannelsId as $id) {
  138. if(ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
  139. ->where('channel_id',$id)
  140. ->exists()){
  141. $hasChapter = true;
  142. }
  143. }
  144. }
  145. if(!$hasChapter){
  146. //不存在需要导出的数据
  147. continue;
  148. }
  149. $filename = "{$sub->paragraph}.".$this->option('format');
  150. $bookMeta['sections'][] = ['filename'=>$filename];
  151. $paliTitle = PaliText::where('book',$book)
  152. ->where('paragraph',$sub->paragraph)
  153. ->value('toc');
  154. $sectionTitle = $paliTitle;
  155. if($this->option('translation') === 'true'){
  156. $chapter = ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
  157. ->where('channel_id',$tranChannelsId[0])
  158. ->first();
  159. if($chapter && !empty($chapter->title)){
  160. $sectionTitle = $chapter->title;
  161. }
  162. }
  163. $content = array();
  164. $chapterStart = $sub->paragraph+1;
  165. $chapterEnd = $sub->paragraph + $sub->chapter_len;
  166. $chapterBody = PaliText::where('book',$book)
  167. ->whereBetween('paragraph',[$chapterStart,$chapterEnd])
  168. ->orderBy('paragraph')->get();
  169. foreach ($chapterBody as $body) {
  170. $currProgress += $step;
  171. $this->info($upload->setStatus($currProgress,'export chapter '.$body->paragraph));
  172. $paraData = array();
  173. $paraData['translations'] = array();
  174. foreach ($outputChannelsId as $key => $channelId) {
  175. $translationData = Sentence::where('book_id',$book)
  176. ->where('paragraph',$body->paragraph)
  177. ->where('channel_uid',$channelId)
  178. ->orderBy('word_start')->get();
  179. $sentContent = array();
  180. foreach ($translationData as $sent) {
  181. $texText = MdRender::render($sent->content,
  182. [$sent->channel_uid],
  183. null,
  184. 'read',
  185. $channelsIndex[$channelId]['type'],
  186. $sent->content_type,
  187. $renderFormat
  188. );
  189. $sentContent[] = trim($texText);
  190. }
  191. $paraContent = implode(' ',$sentContent);
  192. if($channelsIndex[$channelId]['type'] === 'original'){
  193. $paraData['origin'] = $paraContent;
  194. }else{
  195. $paraData['translations'][] = ['content'=>$paraContent];
  196. }
  197. }
  198. if($body->level > 7){
  199. $content[] = $m->render($tplParagraph,$paraData);
  200. }else{
  201. $currLevel = $body->level - $sub->level;
  202. if($currLevel<=0){
  203. $currLevel = 1;
  204. }
  205. if(count($paraData['translations'])===0){
  206. $subSessionTitle = PaliText::where('book',$book)
  207. ->where('paragraph',$body->paragraph)
  208. ->value('toc');
  209. }else{
  210. $subSessionTitle = $paraData['translations'][0]['content'];
  211. }
  212. //标题
  213. $subStr = array_fill(0,$currLevel,'#');
  214. $content[] = implode('',$subStr) . " ".$subSessionTitle;
  215. }
  216. $content[] = "\n\n";
  217. }
  218. $sections[] = [
  219. 'name'=>$filename,
  220. 'body'=>[
  221. 'title'=>$sectionTitle,
  222. 'content'=>implode('',$content)
  223. ]
  224. ];
  225. }
  226. //导出术语表
  227. $keyPali = array();
  228. $keyMeaning = array();
  229. if(isset($GLOBALS['glossary'])){
  230. $glossary = $GLOBALS['glossary'];
  231. foreach ($glossary as $word => $meaning) {
  232. $keyMeaning[$meaning] = $word;
  233. $keyPali[$word] = $meaning;
  234. }
  235. }
  236. ksort($keyPali);
  237. krsort($keyMeaning);
  238. $glossaryData = [];
  239. $glossaryData['pali'] = [];
  240. $glossaryData['meaning'] = [];
  241. foreach ($keyPali as $word => $meaning) {
  242. $glossaryData['pali'][] = ['pali'=>$word,'meaning'=>$meaning];
  243. }
  244. foreach ($keyMeaning as $meaning => $word) {
  245. $glossaryData['meaning'][] = ['pali' => $word,'meaning'=>$meaning];
  246. }
  247. Log::debug('glossary',['data' => $glossaryData]);
  248. $tplFile = resource_path("mustache/chapter/".$this->option('format')."/glossary.".$this->option('format'));
  249. $tplGlossary = file_get_contents($tplFile);
  250. $glossaryContent = $m->render($tplGlossary,$glossaryData);
  251. $sections[] = [
  252. 'name'=>'glossary.'.$this->option('format'),
  253. 'body'=>[
  254. 'title' => 'glossary',
  255. 'content' => $glossaryContent
  256. ]
  257. ];
  258. $this->info($upload->setStatus($currProgress,'export glossary '. count($keyPali)));
  259. $this->info($upload->setStatus(0.9,'export content done sections='.count($sections)));
  260. Log::debug('导出结束',['sections'=>count($sections)]);
  261. $upload->upload('chapter',$sections,$bookMeta);
  262. $this->info($upload->setStatus(1,'export chapter done'));
  263. return 0;
  264. }
  265. }