ImportArticle.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * @return int
  33. */
  34. public function handle()
  35. {
  36. if (!$this->confirm('Do you wish to continue?')) {
  37. return 0;
  38. }
  39. $token = $this->option('token');
  40. $studioName = $this->option('studio');
  41. $anthologyId = $this->option('anthology');
  42. //先获取文章列表,建立全部目录
  43. $url = config('app.url').'/api/v2/article-map';
  44. $response = Http::get($url,[
  45. 'view'=>'anthology',
  46. 'id'=>$anthologyId,
  47. ]);
  48. if($response->failed()){
  49. $this->error('获取文章列表 fail');
  50. return 0;
  51. }else{
  52. $this->info('获取文章列表 ok');
  53. }
  54. if(!$response->json('ok')){
  55. $this->error('http request error'.$response->json('message'));
  56. return 0;
  57. }
  58. $articles = $response->json('data.rows');
  59. $this->info('打开csv文件并读取数据');
  60. $head = array();
  61. $strFileName = __DIR__."/tipitaka-sarupa.csv";
  62. if(!file_exists($strFileName)){
  63. $this->error($strFileName.'文件不存在');
  64. return 1;
  65. }
  66. if (($fp = fopen($strFileName, "r")) === false) {
  67. $this->error("can not open csv $strFileName");
  68. return 0;
  69. }
  70. $inputRow = 0;
  71. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  72. if($inputRow>0){
  73. if(!isset($head[$data[1]])){
  74. $head[$data[1]] = 1;
  75. }
  76. }
  77. $inputRow++;
  78. }
  79. $this->info("csv head=" .count($head));
  80. $titles = array();
  81. $allTitles = array();
  82. foreach ($articles as $key => $article) {
  83. $allTitles[$article['title']] = $article['article_id'];
  84. if($article['level']===1 && isset($head[$article['title']])){
  85. $titles[$article['title']] = $article['article_id'];
  86. $this->info('已有='.$article['title']);
  87. }
  88. }
  89. $this->info("article map head=" .count($titles));
  90. //新建目录
  91. foreach ($head as $key => $value) {
  92. if(!isset($titles[$key])){
  93. $this->info('没有key'.$key.'新建');
  94. $url = config('app.url').'/api/v2/article';
  95. $response = Http::withToken($token)->post($url,
  96. [
  97. 'title'=> $key,
  98. 'lang'=> 'my',
  99. 'studio'=> $studioName,
  100. 'anthologyId'=> $anthologyId,
  101. ]);
  102. if($response->ok()){
  103. $this->info('append ok title='.$key);
  104. $articleId = $response->json('data')['uid'];
  105. $titles[$key] = $articleId;
  106. }else{
  107. $this->error('append fail');
  108. Log::error('append fail');
  109. }
  110. }
  111. }
  112. print_r($titles);
  113. //导入文章
  114. $url = config('app.url').'/api/v2/article';
  115. $inputRow = 0;
  116. fseek($fp, 0);
  117. $count = 0;
  118. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  119. if($inputRow>0){
  120. $id = $data[0];
  121. $dir = $data[1];
  122. $title = $data[2];
  123. $realTitle = "[{$id}]{$title}";
  124. $content = str_replace('\n',"\n",$data[4]) ;
  125. $reference = str_replace(['(',')'],['({{ql|title=','}})'],$data[5]);
  126. $contentCombine = "{$title}\n\n{$content}\n\n{$reference}";
  127. $percent = (int)($inputRow*100/7000);
  128. $this->info("[{$percent}%] do ".$title);
  129. //先查是否有
  130. if(!isset($allTitles[$realTitle])){
  131. $count++;
  132. $this->info('没有,新建 title='.$realTitle);
  133. $response = Http::withToken($token)->post($url,
  134. [
  135. 'title'=> $realTitle,
  136. 'lang'=> 'my',
  137. 'studio'=> $studioName,
  138. 'anthologyId'=> $anthologyId,
  139. 'parentNode'=>$titles[$dir],
  140. ]);
  141. sleep(1);
  142. if($response->ok()){
  143. $this->info('create ok');
  144. $articleId = $response->json('data')['uid'];
  145. }else{
  146. $this->error('create fail');
  147. Log::error('create fail title='.$title);
  148. continue;
  149. }
  150. $this->info('修改 id='.$articleId);
  151. $response = Http::withToken($token)->put($url.'/'.$articleId,
  152. [
  153. 'title'=> $realTitle,
  154. 'subtitle'=> $realTitle,
  155. 'lang'=> 'my',
  156. 'content'=> $contentCombine,
  157. 'anthology_id'=>$anthologyId,
  158. 'to_tpl'=>true,
  159. 'status'=>30,
  160. ]);
  161. if($response->ok()){
  162. $this->info('edit ok');
  163. }else{
  164. $this->error('edit fail');
  165. Log::error('edit fail id='.$articleId);
  166. continue;
  167. }
  168. }else{
  169. $this->error('已经存在 title='.$realTitle);
  170. $articleId = $allTitles[$realTitle];
  171. }
  172. }
  173. $inputRow++;
  174. }
  175. fclose($fp);
  176. return 0;
  177. }
  178. }