ExportArticle.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Str;
  7. use Illuminate\Support\Facades\Storage;
  8. use App\Tools\RedisClusters;
  9. use App\Tools\ExportDownload;
  10. use App\Http\Api\MdRender;
  11. class ExportArticle extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. * php artisan export:article 78c22ad3-58e2-4cf0-b979-67783ca3a375 123 --channel=7fea264d-7a26-40f8-bef7-bc95102760fb --format=html
  16. * php artisan export:article 4732bcae-fb9d-4db4-b6b7-e8d0aa882f30 1234 --channel=7fea264d-7a26-40f8-bef7-bc95102760fb --anthology=eb9e3f7f-b942-4ca4-bd6f-b7876b59a523 --format=html
  17. * @var string
  18. */
  19. protected $signature = 'export:article {id} {filename} {--anthology=} {--channel=} {--origin=false} {--translation=true} {--format=tex} {--debug}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Command description';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. $this->info('task export chapter start');
  43. Log::debug('task export chapter start');
  44. if(\App\Tools\Tools::isStop()){
  45. return 0;
  46. }
  47. $upload = new ExportDownload([
  48. 'filename'=>$this->argument('filename').'.zip',
  49. 'format'=>$this->option('format'),
  50. 'debug'=>$this->option('debug'),
  51. 'real_filename'=>'article',
  52. ]);
  53. MdRender::init();
  54. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
  55. 'delimiters' => '[[ ]]',
  56. 'escape'=>function ($value){
  57. return $value;
  58. }));
  59. $api = 'http://127.0.0.1:8000/api';
  60. $basicUrl = $api . '/v2/article/';
  61. $sections = array();
  62. $articles = array();
  63. $url = $basicUrl . $this->argument('id');
  64. $this->info('http request url='.$url);
  65. $response = Http::get($url,[
  66. 'mode' => 'read',
  67. 'format' => 'html',
  68. 'anthology'=> $this->option('anthology'),
  69. 'channel' => $this->option('channel'),
  70. ]);
  71. if($response->failed()){
  72. $this->error('http request error');
  73. }
  74. if(!$response->json('ok')){
  75. $this->error('http request error'.$response->json('message'));
  76. }
  77. $article = $response->json('data');
  78. $bookMeta = array();
  79. $bookMeta['book_author'] = "";
  80. $bookMeta['book_title'] = $article['title_text'];
  81. $articles[] = [
  82. 'level'=>1,
  83. 'title'=>$article['title_text'],
  84. 'content'=>isset($article['html'])?$article['html']:'',
  85. ];
  86. if(isset($article['toc'])){
  87. $this->info('has sub article '. count($article['toc']));
  88. }
  89. foreach ($article['toc'] as $key => $value) {
  90. $url = $basicUrl . $value['key'];
  91. $this->info('http request url='.$url);
  92. $response = Http::get($url,[
  93. 'mode' => 'read',
  94. 'format' => 'html',
  95. 'anthology'=> $this->option('anthology'),
  96. 'channel' => $this->option('channel'),
  97. ]);
  98. if($response->failed()){
  99. $this->error('http request error');
  100. $this->error('http request error'.$response->json('message'));
  101. continue;
  102. }
  103. if(!$response->json('ok')){
  104. $this->error('http request error'.$response->json('message'));
  105. continue;
  106. }
  107. $article = $response->json('data');
  108. $this->info('sub article title='.$article['title_text']);
  109. $articles[] = [
  110. 'level'=>$value['level'],
  111. 'title'=>$article['title_text'],
  112. 'content'=>isset($article['html'])?$article['html']:'',
  113. ];
  114. }
  115. $sections[] = [
  116. 'name'=>'first',
  117. 'body'=>['articles'=>$articles],
  118. ];
  119. $this->info($upload->setStatus(0.9,'export content done'));
  120. Log::debug('导出结束');
  121. $upload->upload('article',$sections,$bookMeta);
  122. return 0;
  123. }
  124. }