ImportArticle.php 5.2 KB

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