|
|
@@ -3,15 +3,21 @@
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
+use Illuminate\Support\Facades\Http;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+use App\Http\Api\StudioApi;
|
|
|
+use App\Models\Article;
|
|
|
+use App\Models\Collection;
|
|
|
|
|
|
class ImportArticleMap extends Command
|
|
|
{
|
|
|
/**
|
|
|
* The name and signature of the console command.
|
|
|
+ * php artisan import:article.map visuddhinanda --studio=visuddhinanda --size=30000 --anthology=4c6b661b-fd68-44c5-8918-2e327c870b9a --token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJuYmYiOjE2OTc3Mjg2ODUsImV4cCI6MTcyOTI2NDY4NSwidWlkIjoiYmE1NDYzZjMtNzJkMS00NDEwLTg1OGUtZWFkZDEwODg0NzEzIiwiaWQiOjR9.fiXhnY2LczZ9kKVHV0FfD3AJPZt-uqM5wrDe4EhToVexdd007ebPFYssZefmchfL0mx9nF0rgHSqjNhx4P0yDA
|
|
|
*
|
|
|
* @var string
|
|
|
*/
|
|
|
- protected $signature = 'import:article.map {--token=} {--studio=} {--anthology=}';
|
|
|
+ protected $signature = 'import:article.map {src_studio} {--token=} {--studio=} {--anthology=} {--size=}';
|
|
|
|
|
|
/**
|
|
|
* The console command description.
|
|
|
@@ -37,30 +43,27 @@ class ImportArticleMap extends Command
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
- if (!$this->confirm('Do you wish to continue?')) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
$token = $this->option('token');
|
|
|
$studioName = $this->option('studio');
|
|
|
$anthologyId = $this->option('anthology');
|
|
|
-
|
|
|
- //先获取文章列表,建立全部目录
|
|
|
- $url = config('app.url').'/api/v2/article-map';
|
|
|
- $response = Http::get($url,[
|
|
|
- 'view'=>'anthology',
|
|
|
- 'id'=>$anthologyId,
|
|
|
- ]);
|
|
|
- if($response->failed()){
|
|
|
- $this->error('获取文章列表 fail');
|
|
|
+ $srcStudio = $this->argument('src_studio');
|
|
|
+ if (!$this->confirm('Do you wish to continue?')) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ $studioId = StudioApi::getIdByName($studioName);
|
|
|
+ if(!$studioId){
|
|
|
+ $this->error("can not found studio name {$studioName}");
|
|
|
return 0;
|
|
|
- }else{
|
|
|
- $this->info('获取文章列表 ok');
|
|
|
}
|
|
|
- if(!$response->json('ok')){
|
|
|
- $this->error('http request error'.$response->json('message'));
|
|
|
+ $srcStudioId = StudioApi::getIdByName($srcStudio);
|
|
|
+ if(!$srcStudioId){
|
|
|
+ $this->error("can not found src studio name {$srcStudio}");
|
|
|
return 0;
|
|
|
}
|
|
|
- $articles = $response->json('data.rows');
|
|
|
+
|
|
|
+ //先获取文章列表,建立全部目录
|
|
|
+ $url = config('app.url').'/api/v2/article-map';
|
|
|
+
|
|
|
$this->info('打开csv文件并读取数据');
|
|
|
$head = array();
|
|
|
$strFileName = __DIR__."/tipitaka-sarupa.csv";
|
|
|
@@ -70,54 +73,131 @@ class ImportArticleMap extends Command
|
|
|
}
|
|
|
|
|
|
if (($fp = fopen($strFileName, "r")) === false) {
|
|
|
- $this->error("can not open csv $strFileName");
|
|
|
+ $this->error("can not open csv {$strFileName}");
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ //查询文集语言
|
|
|
+ $lang = Collection::where('uid',$anthologyId)->value('lang');
|
|
|
+ if(empty($lang)){
|
|
|
+ $this->error("文集语言不能为空 anthologyId=".$anthologyId);
|
|
|
return 0;
|
|
|
}
|
|
|
$inputRow = 0;
|
|
|
+ $currSize=0;
|
|
|
+ $currBlock=1;
|
|
|
+ $currDir='';
|
|
|
+ $success = 0;
|
|
|
+ $fail = 0;
|
|
|
+ $articleMap = array();
|
|
|
while (($data = fgetcsv($fp, 0, ',')) !== false) {
|
|
|
if($inputRow>0){
|
|
|
- if(!isset($head[$data[1]])){
|
|
|
- $head[$data[1]] = 1;
|
|
|
+ $id = $data[0];
|
|
|
+ $dir = $data[1];
|
|
|
+ $title = $data[2];
|
|
|
+ $realTitle = "[{$id}]{$title}";
|
|
|
+ $reference = $data[5];
|
|
|
+ if($this->option('size')){
|
|
|
+ $currDir = 'tipitaka-sarupa-'.$currBlock;
|
|
|
+ if($currSize > $this->option('size')){
|
|
|
+ $currBlock++;
|
|
|
+ $currSize=0;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $currDir = $dir;
|
|
|
+ }
|
|
|
+ //查找目录文章是否存在
|
|
|
+ $dirArticle = Article::where('owner',$studioId)
|
|
|
+ ->where('title',$currDir)
|
|
|
+ ->first();
|
|
|
+ if($dirArticle){
|
|
|
+ $dirId = $dirArticle->uid;
|
|
|
+ }else{
|
|
|
+ $this->info('不存在目录'.$currDir.'新建');
|
|
|
+ $url = config('app.url').'/api/v2/article';
|
|
|
+ sleep(2);
|
|
|
+ $response = Http::withToken($token)->post($url,
|
|
|
+ [
|
|
|
+ 'title'=> $currDir,
|
|
|
+ 'lang'=> $lang,
|
|
|
+ 'studio'=> $studioName,
|
|
|
+ 'anthologyId'=> $anthologyId,
|
|
|
+ ]);
|
|
|
+ if($response->ok()){
|
|
|
+ $this->info('dir create ok title='.$currDir);
|
|
|
+ $dirId = $response->json('data.uid');
|
|
|
+ }else{
|
|
|
+ $this->error('create dir fail.'.$currDir);
|
|
|
+ Log::error('create dir fail title='.$currDir);
|
|
|
+ $fail++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //创建目录结束
|
|
|
+ if(!isset($articleMap[$dirId])){
|
|
|
+ $articleMap[$dirId] = ['name'=>$currDir,'children'=>[]];
|
|
|
+ }
|
|
|
+ //查找文章
|
|
|
+ $article = Article::where('owner',$srcStudioId)
|
|
|
+ ->where('title',$realTitle)
|
|
|
+ ->first();
|
|
|
+ if(!$article){
|
|
|
+ $this->error('文章没找到.'.$realTitle);
|
|
|
+ Log::error('文章没找到 title='.$realTitle);
|
|
|
+ $fail++;
|
|
|
+ continue;
|
|
|
}
|
|
|
+ $articleMap[$dirId]['children'][] = [
|
|
|
+ 'id'=>$article->uid,
|
|
|
+ 'title'=>$article->title,
|
|
|
+ ];
|
|
|
+ if($this->option('size')){
|
|
|
+ $currSize += mb_strlen($title,'UTF-8') +
|
|
|
+ mb_strlen($data[4],'UTF-8') +
|
|
|
+ mb_strlen($reference,'UTF-8');
|
|
|
+ }
|
|
|
+ $success++;
|
|
|
}
|
|
|
$inputRow++;
|
|
|
}
|
|
|
+ $this->info("找到文章=" .$success);
|
|
|
+ $this->info("目录=" .count($articleMap));
|
|
|
|
|
|
- $this->info("csv head=" .count($head));
|
|
|
+ $this->info('正在准备map数据');
|
|
|
|
|
|
- $titles = array();
|
|
|
- $allTitles = array();
|
|
|
- foreach ($articles as $key => $article) {
|
|
|
- $allTitles[$article['title']] = $article['article_id'];
|
|
|
- if($article['level']===1 && isset($head[$article['title']])){
|
|
|
- $titles[$article['title']] = $article['article_id'];
|
|
|
- $this->info('已有='.$article['title']);
|
|
|
+ $data = array();
|
|
|
+ foreach ($articleMap as $dirId => $dir) {
|
|
|
+ $data[] = [
|
|
|
+ 'article_id'=> $dirId,
|
|
|
+ 'level'=> 1,
|
|
|
+ 'title'=> $dir['name'],
|
|
|
+ 'children'=> count($dir['children']),
|
|
|
+ 'deleted_at'=> null,
|
|
|
+ ];
|
|
|
+ foreach ($dir['children'] as $key => $child) {
|
|
|
+ $data[] = [
|
|
|
+ 'article_id'=> $child['id'],
|
|
|
+ 'level'=> 2,
|
|
|
+ 'title'=> $child['title'],
|
|
|
+ 'children'=> 0,
|
|
|
+ 'deleted_at'=> null,
|
|
|
+ ];
|
|
|
}
|
|
|
}
|
|
|
- $this->info("article map head=" .count($titles));
|
|
|
- //新建目录
|
|
|
- foreach ($head as $key => $value) {
|
|
|
- if(!isset($titles[$key])){
|
|
|
- $this->info('没有key'.$key.'新建');
|
|
|
- $url = config('app.url').'/api/v2/article';
|
|
|
- $response = Http::withToken($token)->post($url,
|
|
|
- [
|
|
|
- 'title'=> $key,
|
|
|
- 'lang'=> 'my',
|
|
|
- 'studio'=> $studioName,
|
|
|
- 'anthologyId'=> $anthologyId,
|
|
|
- ]);
|
|
|
- if($response->ok()){
|
|
|
- $this->info('append ok title='.$key);
|
|
|
- $articleId = $response->json('data')['uid'];
|
|
|
- $titles[$key] = $articleId;
|
|
|
- }else{
|
|
|
- $this->error('append fail');
|
|
|
- Log::error('append fail');
|
|
|
- }
|
|
|
- }
|
|
|
+ $this->info('map data='.count($data));
|
|
|
+
|
|
|
+ //目录写入db
|
|
|
+ $url = config('app.url').'/api/v2/article-map/'.$anthologyId;
|
|
|
+ $response = Http::withToken($token)->put($url,
|
|
|
+ [
|
|
|
+ 'data'=> $data,
|
|
|
+ 'operation' => "anthology",
|
|
|
+ ]);
|
|
|
+ if($response->ok()){
|
|
|
+ $this->info('map update ok ');
|
|
|
+ }else{
|
|
|
+ $this->error('map update fail.');
|
|
|
+ Log::error('map update fail ');
|
|
|
}
|
|
|
- print_r($titles);
|
|
|
return 0;
|
|
|
}
|
|
|
}
|