2
0

ExportChapter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\MdRender;
  5. use App\Models\PaliText;
  6. use App\Models\ProgressChapter;
  7. use App\Models\Sentence;
  8. use App\Tools\Export;
  9. use App\Tools\ExportDownload;
  10. use App\Tools\Tools;
  11. use Illuminate\Console\Command;
  12. use Illuminate\Support\Facades\Log;
  13. class ExportChapter extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. * php artisan export:chapter 213 3 a19eaf75-c63f-4b84-8125-1bce18311e23 213-3.html --format=html --origin=true
  18. * php artisan export:chapter 168 915 7fea264d-7a26-40f8-bef7-bc95102760fb 168-915.html --format=html --debug
  19. * php artisan export:chapter 168 915 7fea264d-7a26-40f8-bef7-bc95102760fb 168-915.html --format=html --origin=true
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'export:chapter {book} {para} {channel} {query_id} {--token=} {--origin=false} {--translation=true} {--debug} {--format=markdown} ';
  24. /**
  25. * The console command description.
  26. *
  27. * @var string
  28. */
  29. protected $description = 'Command description';
  30. /**
  31. * Create a new command instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct()
  36. {
  37. parent::__construct();
  38. }
  39. /**
  40. * Execute the console command.
  41. *
  42. * @return int
  43. */
  44. public function handle()
  45. {
  46. $this->info('task export chapter start');
  47. Log::debug('task export chapter start');
  48. if (Tools::isStop()) {
  49. return 0;
  50. }
  51. $book = $this->argument('book');
  52. $para = $this->argument('para');
  53. $upload = new ExportDownload([
  54. 'queryId' => $this->argument('query_id'),
  55. 'format' => $this->option('format'),
  56. 'debug' => $this->option('debug'),
  57. 'filename' => $book.'-'.$para,
  58. ]);
  59. $m = new \Mustache_Engine([
  60. 'entity_flags' => ENT_QUOTES,
  61. 'delimiters' => '[[ ]]',
  62. 'escape' => function ($value) {
  63. return $value;
  64. },
  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 = [];
  75. $channelsIndex = [];
  76. foreach ($channelsId as $key => $id) {
  77. $channels[] = ChannelApi::getById($id);
  78. $channelsIndex[$id] = ChannelApi::getById($id);
  79. }
  80. $bookMeta = [];
  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 = [];
  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. ) {
  142. $hasChapter = true;
  143. }
  144. }
  145. }
  146. if (! $hasChapter) {
  147. // 不存在需要导出的数据
  148. continue;
  149. }
  150. $filename = "{$sub->paragraph}.".$this->option('format');
  151. $bookMeta['sections'][] = ['filename' => $filename];
  152. $paliTitle = PaliText::where('book', $book)
  153. ->where('paragraph', $sub->paragraph)
  154. ->value('toc');
  155. $sectionTitle = $paliTitle;
  156. if ($this->option('translation') === 'true') {
  157. $chapter = ProgressChapter::where('book', $book)->where('para', $sub->paragraph)
  158. ->where('channel_id', $tranChannelsId[0])
  159. ->first();
  160. if ($chapter && ! empty($chapter->title)) {
  161. $sectionTitle = $chapter->title;
  162. }
  163. }
  164. $content = [];
  165. $chapterStart = $sub->paragraph + 1;
  166. $chapterEnd = $sub->paragraph + $sub->chapter_len;
  167. $chapterBody = PaliText::where('book', $book)
  168. ->whereBetween('paragraph', [$chapterStart, $chapterEnd])
  169. ->orderBy('paragraph')->get();
  170. foreach ($chapterBody as $body) {
  171. $currProgress += $step;
  172. $this->info($upload->setStatus($currProgress, 'export chapter '.$body->paragraph));
  173. $paraData = [];
  174. $paraData['translations'] = [];
  175. foreach ($outputChannelsId as $key => $channelId) {
  176. $translationData = Sentence::where('book_id', $book)
  177. ->where('paragraph', $body->paragraph)
  178. ->where('channel_uid', $channelId)
  179. ->orderBy('word_start')->get();
  180. $sentContent = [];
  181. foreach ($translationData as $sent) {
  182. $texText = MdRender::render(
  183. $sent->content,
  184. [$sent->channel_uid],
  185. null,
  186. 'read',
  187. $channelsIndex[$channelId]['type'],
  188. $sent->content_type,
  189. $renderFormat
  190. );
  191. $sentContent[] = trim($texText);
  192. }
  193. $paraContent = implode(' ', $sentContent);
  194. if ($channelsIndex[$channelId]['type'] === 'original') {
  195. $paraData['origin'] = $paraContent;
  196. } else {
  197. $paraData['translations'][] = ['content' => $paraContent];
  198. }
  199. }
  200. if ($body->level > 7) {
  201. $content[] = $m->render($tplParagraph, $paraData);
  202. } else {
  203. $currLevel = $body->level - $sub->level;
  204. if ($currLevel <= 0) {
  205. $currLevel = 1;
  206. }
  207. if (count($paraData['translations']) === 0) {
  208. $subSessionTitle = PaliText::where('book', $book)
  209. ->where('paragraph', $body->paragraph)
  210. ->value('toc');
  211. } else {
  212. $subSessionTitle = $paraData['translations'][0]['content'];
  213. }
  214. // 标题
  215. $subStr = array_fill(0, $currLevel, '#');
  216. $content[] = implode('', $subStr).' '.$subSessionTitle;
  217. }
  218. $content[] = "\n\n";
  219. }
  220. $sections[] = [
  221. 'name' => $filename,
  222. 'body' => [
  223. 'title' => $sectionTitle,
  224. 'content' => implode('', $content),
  225. ],
  226. ];
  227. }
  228. // 导出术语表
  229. $keyPali = [];
  230. $keyMeaning = [];
  231. if (isset($GLOBALS['glossary'])) {
  232. $glossary = $GLOBALS['glossary'];
  233. foreach ($glossary as $word => $meaning) {
  234. $keyMeaning[$meaning] = $word;
  235. $keyPali[$word] = $meaning;
  236. }
  237. }
  238. ksort($keyPali);
  239. krsort($keyMeaning);
  240. $glossaryData = [];
  241. $glossaryData['pali'] = [];
  242. $glossaryData['meaning'] = [];
  243. foreach ($keyPali as $word => $meaning) {
  244. $glossaryData['pali'][] = ['pali' => $word, 'meaning' => $meaning];
  245. }
  246. foreach ($keyMeaning as $meaning => $word) {
  247. $glossaryData['meaning'][] = ['pali' => $word, 'meaning' => $meaning];
  248. }
  249. Log::debug('glossary', ['data' => $glossaryData]);
  250. $tplFile = resource_path('mustache/chapter/'.$this->option('format').'/glossary.'.$this->option('format'));
  251. $tplGlossary = file_get_contents($tplFile);
  252. $glossaryContent = $m->render($tplGlossary, $glossaryData);
  253. $sections[] = [
  254. 'name' => 'glossary.'.$this->option('format'),
  255. 'body' => [
  256. 'title' => 'glossary',
  257. 'content' => $glossaryContent,
  258. ],
  259. ];
  260. $this->info($upload->setStatus($currProgress, 'export glossary '.count($keyPali)));
  261. $this->info($upload->setStatus(0.9, 'export content done sections='.count($sections)));
  262. Log::debug('导出结束', ['sections' => count($sections)]);
  263. $upload->upload('chapter', $sections, $bookMeta);
  264. $this->info($upload->setStatus(1, 'export chapter done'));
  265. return 0;
  266. }
  267. }