ImportArticleMap.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. use App\Models\Collection;
  9. class ImportArticleMap extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. * php artisan import:article.map visuddhinanda --studio=visuddhinanda --size=30000 --anthology=4c6b661b-fd68-44c5-8918-2e327c870b9a --token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJuYmYiOjE2OTc3Mjg2ODUsImV4cCI6MTcyOTI2NDY4NSwidWlkIjoiYmE1NDYzZjMtNzJkMS00NDEwLTg1OGUtZWFkZDEwODg0NzEzIiwiaWQiOjR9.fiXhnY2LczZ9kKVHV0FfD3AJPZt-uqM5wrDe4EhToVexdd007ebPFYssZefmchfL0mx9nF0rgHSqjNhx4P0yDA
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'import:article.map {src_studio} {--token=} {--studio=} {--anthology=} {--size=}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '重置缅文tipitaka sarupa文章目录';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. $token = $this->option('token');
  41. $studioName = $this->option('studio');
  42. $anthologyId = $this->option('anthology');
  43. $srcStudio = $this->argument('src_studio');
  44. if (!$this->confirm('Do you wish to continue?')) {
  45. return 0;
  46. }
  47. $studioId = StudioApi::getIdByName($studioName);
  48. if(!$studioId){
  49. $this->error("can not found studio name {$studioName}");
  50. return 0;
  51. }
  52. $srcStudioId = StudioApi::getIdByName($srcStudio);
  53. if(!$srcStudioId){
  54. $this->error("can not found src studio name {$srcStudio}");
  55. return 0;
  56. }
  57. //先获取文章列表,建立全部目录
  58. $url = config('app.url').'/api/v2/article-map';
  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. //查询文集语言
  71. $lang = Collection::where('uid',$anthologyId)->value('lang');
  72. if(empty($lang)){
  73. $this->error("文集语言不能为空 anthologyId=".$anthologyId);
  74. return 0;
  75. }
  76. $inputRow = 0;
  77. $currSize=0;
  78. $currBlock=1;
  79. $currDir='';
  80. $success = 0;
  81. $fail = 0;
  82. $articleMap = array();
  83. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  84. if($inputRow>0){
  85. $id = $data[0];
  86. $dir = $data[1];
  87. $title = $data[2];
  88. $realTitle = "[{$id}]{$title}";
  89. $reference = $data[5];
  90. if($this->option('size')){
  91. $currDir = 'tipitaka-sarupa-'.$currBlock;
  92. if($currSize > $this->option('size')){
  93. $currBlock++;
  94. $currSize=0;
  95. }
  96. }else{
  97. $currDir = $dir;
  98. }
  99. //查找目录文章是否存在
  100. $dirArticle = Article::where('owner',$studioId)
  101. ->where('title',$currDir)
  102. ->first();
  103. if($dirArticle){
  104. $dirId = $dirArticle->uid;
  105. }else{
  106. $this->info('不存在目录'.$currDir.'新建');
  107. $url = config('app.url').'/api/v2/article';
  108. sleep(2);
  109. $response = Http::withToken($token)->post($url,
  110. [
  111. 'title'=> $currDir,
  112. 'lang'=> $lang,
  113. 'studio'=> $studioName,
  114. 'anthologyId'=> $anthologyId,
  115. ]);
  116. if($response->ok()){
  117. $this->info('dir create ok title='.$currDir);
  118. $dirId = $response->json('data.uid');
  119. }else{
  120. $this->error('create dir fail.'.$currDir);
  121. Log::error('create dir fail title='.$currDir);
  122. $fail++;
  123. continue;
  124. }
  125. }
  126. //创建目录结束
  127. if(!isset($articleMap[$dirId])){
  128. $articleMap[$dirId] = ['name'=>$currDir,'children'=>[]];
  129. }
  130. //查找文章
  131. $article = Article::where('owner',$srcStudioId)
  132. ->where('title',$realTitle)
  133. ->first();
  134. if(!$article){
  135. $this->error('文章没找到.'.$realTitle);
  136. Log::error('文章没找到 title='.$realTitle);
  137. $fail++;
  138. continue;
  139. }
  140. $articleMap[$dirId]['children'][] = [
  141. 'id'=>$article->uid,
  142. 'title'=>$article->title,
  143. ];
  144. if($this->option('size')){
  145. $currSize += mb_strlen($title,'UTF-8') +
  146. mb_strlen($data[4],'UTF-8') +
  147. mb_strlen($reference,'UTF-8');
  148. }
  149. $success++;
  150. }
  151. $inputRow++;
  152. }
  153. $this->info("找到文章=" .$success);
  154. $this->info("目录=" .count($articleMap));
  155. $this->info('正在准备map数据');
  156. $data = array();
  157. foreach ($articleMap as $dirId => $dir) {
  158. $data[] = [
  159. 'article_id'=> $dirId,
  160. 'level'=> 1,
  161. 'title'=> $dir['name'],
  162. 'children'=> count($dir['children']),
  163. 'deleted_at'=> null,
  164. ];
  165. foreach ($dir['children'] as $key => $child) {
  166. $data[] = [
  167. 'article_id'=> $child['id'],
  168. 'level'=> 2,
  169. 'title'=> $child['title'],
  170. 'children'=> 0,
  171. 'deleted_at'=> null,
  172. ];
  173. }
  174. }
  175. $this->info('map data='.count($data));
  176. //目录写入db
  177. $url = config('app.url').'/api/v2/article-map/'.$anthologyId;
  178. $response = Http::withToken($token)->put($url,
  179. [
  180. 'data'=> $data,
  181. 'operation' => "anthology",
  182. ]);
  183. if($response->ok()){
  184. $this->info('map update ok ');
  185. }else{
  186. $this->error('map update fail.');
  187. Log::error('map update fail ');
  188. }
  189. return 0;
  190. }
  191. }