ImportArticle.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. class ImportArticle extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. * php artisan import:article --token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJuYmYiOjE2OTc3Mjg2ODUsImV4cCI6MTcyOTI2NDY4NSwidWlkIjoiYmE1NDYzZjMtNzJkMS00NDEwLTg1OGUtZWFkZDEwODg0NzEzIiwiaWQiOjR9.fiXhnY2LczZ9kKVHV0FfD3AJPZt-uqM5wrDe4EhToVexdd007ebPFYssZefmchfL0mx9nF0rgHSqjNhx4P0yDA --studio=visuddhinanda --anthology=eb9e3f7f-b942-4ca4-bd6f-b7876b59a523
  11. * @var string
  12. */
  13. protected $signature = 'import:article {--token=} {--studio=} {--anthology=}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '导入缅文tipitaka sarupa文章';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. * 分两个步骤导入
  32. * 1. 导入文章到文集
  33. * 2. 重新生成目录结构
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. if (!$this->confirm('Do you wish to continue?')) {
  39. return 0;
  40. }
  41. $token = $this->option('token');
  42. $studioName = $this->option('studio');
  43. $anthologyId = $this->option('anthology');
  44. //先获取文章列表,建立全部目录
  45. $head = array();
  46. $strFileName = __DIR__."/tipitaka-sarupa.csv";
  47. if(!file_exists($strFileName)){
  48. $this->error($strFileName.'文件不存在');
  49. return 1;
  50. }
  51. if (($fp = fopen($strFileName, "r")) === false) {
  52. $this->error("can not open csv {$strFileName}");
  53. return 0;
  54. }
  55. $this->info('打开csv文件成功');
  56. $studioId = App\Http\Api\StudioApi::getIdByName($studioName);
  57. if(!$studioId){
  58. $this->error("can not found studio name {$studioName}");
  59. return 0;
  60. }
  61. //导入文章
  62. $url = config('app.url').'/api/v2/article';
  63. $inputRow = 0;
  64. fseek($fp, 0);
  65. $count = 0;
  66. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  67. if($inputRow>0){
  68. $id = $data[0];
  69. $dir = $data[1];
  70. $title = $data[2];
  71. $realTitle = "[{$id}]{$title}";
  72. $content = str_replace('\n',"\n",$data[4]) ;
  73. $reference = str_replace(['(',')'],['({{ql|title=','}})'],$data[5]);
  74. $contentCombine = "{$title}\n\n{$content}\n\n{$reference}";
  75. $percent = (int)($inputRow*100/7000);
  76. $this->info("[{$percent}%] doing ".$realTitle);
  77. //先查是否有
  78. $hasArticle = Article::where('owner',$studioId)
  79. ->where('title',$realTitle)
  80. ->exists();
  81. if($hasArticle){
  82. $this->error('文章已经存在 title='.$realTitle);
  83. continue;
  84. }
  85. $count++;
  86. $this->info('新建 title='.$realTitle);
  87. $response = Http::withToken($token)->post($url,
  88. [
  89. 'title'=> $realTitle,
  90. 'lang'=> 'my',
  91. 'studio'=> $studioName,
  92. 'anthologyId'=> $anthologyId,
  93. ]);
  94. sleep(1);
  95. if($response->ok()){
  96. $this->info('create ok');
  97. $articleId = $response->json('data')['uid'];
  98. }else{
  99. $this->error('create article fail.'.$realTitle);
  100. Log::error('create article fail title='.$realTitle);
  101. continue;
  102. }
  103. $this->info('修改 id='.$articleId);
  104. $response = Http::withToken($token)->put($url.'/'.$articleId,
  105. [
  106. 'title'=> $realTitle,
  107. 'subtitle'=> $realTitle,
  108. 'lang'=> 'my',
  109. 'content'=> $contentCombine,
  110. 'anthology_id'=>$anthologyId,
  111. 'to_tpl'=>true,
  112. 'status'=>30,
  113. ]);
  114. if($response->ok()){
  115. $this->info('edit ok');
  116. }else{
  117. $this->error('edit article fail');
  118. Log::error('edit article fail ',['id'=>$articleId,'title'=>$realTitle]);
  119. }
  120. }
  121. $inputRow++;
  122. }
  123. fclose($fp);
  124. return 0;
  125. }
  126. }