ExportChapter.php 11 KB

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