ImportArticleMap.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. class ImportArticleMap extends Command
  5. {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. protected $signature = 'import:article.map {--token=} {--studio=} {--anthology=}';
  12. /**
  13. * The console command description.
  14. *
  15. * @var string
  16. */
  17. protected $description = '重置缅文tipitaka sarupa文章目录';
  18. /**
  19. * Create a new command instance.
  20. *
  21. * @return void
  22. */
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. }
  27. /**
  28. * Execute the console command.
  29. *
  30. * @return int
  31. */
  32. public function handle()
  33. {
  34. if (!$this->confirm('Do you wish to continue?')) {
  35. return 0;
  36. }
  37. $token = $this->option('token');
  38. $studioName = $this->option('studio');
  39. $anthologyId = $this->option('anthology');
  40. //先获取文章列表,建立全部目录
  41. $url = config('app.url').'/api/v2/article-map';
  42. $response = Http::get($url,[
  43. 'view'=>'anthology',
  44. 'id'=>$anthologyId,
  45. ]);
  46. if($response->failed()){
  47. $this->error('获取文章列表 fail');
  48. return 0;
  49. }else{
  50. $this->info('获取文章列表 ok');
  51. }
  52. if(!$response->json('ok')){
  53. $this->error('http request error'.$response->json('message'));
  54. return 0;
  55. }
  56. $articles = $response->json('data.rows');
  57. $this->info('打开csv文件并读取数据');
  58. $head = array();
  59. $strFileName = __DIR__."/tipitaka-sarupa.csv";
  60. if(!file_exists($strFileName)){
  61. $this->error($strFileName.'文件不存在');
  62. return 1;
  63. }
  64. if (($fp = fopen($strFileName, "r")) === false) {
  65. $this->error("can not open csv $strFileName");
  66. return 0;
  67. }
  68. $inputRow = 0;
  69. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  70. if($inputRow>0){
  71. if(!isset($head[$data[1]])){
  72. $head[$data[1]] = 1;
  73. }
  74. }
  75. $inputRow++;
  76. }
  77. $this->info("csv head=" .count($head));
  78. $titles = array();
  79. $allTitles = array();
  80. foreach ($articles as $key => $article) {
  81. $allTitles[$article['title']] = $article['article_id'];
  82. if($article['level']===1 && isset($head[$article['title']])){
  83. $titles[$article['title']] = $article['article_id'];
  84. $this->info('已有='.$article['title']);
  85. }
  86. }
  87. $this->info("article map head=" .count($titles));
  88. //新建目录
  89. foreach ($head as $key => $value) {
  90. if(!isset($titles[$key])){
  91. $this->info('没有key'.$key.'新建');
  92. $url = config('app.url').'/api/v2/article';
  93. $response = Http::withToken($token)->post($url,
  94. [
  95. 'title'=> $key,
  96. 'lang'=> 'my',
  97. 'studio'=> $studioName,
  98. 'anthologyId'=> $anthologyId,
  99. ]);
  100. if($response->ok()){
  101. $this->info('append ok title='.$key);
  102. $articleId = $response->json('data')['uid'];
  103. $titles[$key] = $articleId;
  104. }else{
  105. $this->error('append fail');
  106. Log::error('append fail');
  107. }
  108. }
  109. }
  110. print_r($titles);
  111. return 0;
  112. }
  113. }