|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Commands; |
| 4 | + |
| 5 | +use App\Coding; |
| 6 | +use Confluence\Content; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use LaravelFans\Confluence\Facades\Confluence; |
| 9 | +use LaravelZero\Framework\Commands\Command; |
| 10 | + |
| 11 | +class WikiImportCommand extends Command |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The signature of the command. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $signature = 'wiki:import |
| 19 | + {--coding_import_provider= : 数据来源,如 Confluence、MediaWiki} |
| 20 | + {--confluence_base_uri= : Confluence API URL,如 http://localhost:8090/confluence/rest/api/} |
| 21 | + {--confluence_username=} |
| 22 | + {--confluence_password=} |
| 23 | + {--coding_token= : CODING 令牌} |
| 24 | + {--coding_team_domain= : CODING 团队域名,如 xxx.coding.net 即填写 xxx} |
| 25 | + {--coding_project_uri= : CODING 项目标识,如 xxx.coding.net/p/yyy 即填写 yyy} |
| 26 | + '; |
| 27 | + |
| 28 | + /** |
| 29 | + * The description of the command. |
| 30 | + * |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + protected $description = 'import wiki from confluence and so on'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Execute the console command. |
| 37 | + * |
| 38 | + * @return mixed |
| 39 | + */ |
| 40 | + public function handle(Coding $coding) |
| 41 | + { |
| 42 | + if ($this->option('coding_import_provider')) { |
| 43 | + $provider = $this->option('coding_import_provider'); |
| 44 | + } else { |
| 45 | + $provider = config('coding.import.provider') ?? $this->choice( |
| 46 | + '数据来源?', |
| 47 | + ['Confluence', 'MediaWiki'], |
| 48 | + 0 |
| 49 | + ); |
| 50 | + } |
| 51 | + if ($provider != 'Confluence') { |
| 52 | + $this->info('TODO'); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if ($this->option('confluence_base_uri')) { |
| 57 | + $baseUri = $this->option('confluence_base_uri'); |
| 58 | + } else { |
| 59 | + $baseUri = config('confluence.base_uri') ?? $this->ask( |
| 60 | + $provider . ' API 链接:', |
| 61 | + 'http://9.134.190.26:8090/rest/api/' |
| 62 | + ); |
| 63 | + } |
| 64 | + config(['confluence.base_uri' => $baseUri]); |
| 65 | + |
| 66 | + if ($this->option('confluence_username')) { |
| 67 | + $username = $this->option('confluence_username'); |
| 68 | + } else { |
| 69 | + $username = config('confluence.username') ?? $this->ask($provider . ' 账号:', 'admin'); |
| 70 | + } |
| 71 | + if ($this->option('confluence_password')) { |
| 72 | + $password = $this->option('confluence_password'); |
| 73 | + } else { |
| 74 | + $password = config('confluence.password') ?? $this->ask($provider . ' 密码:', 'Demo.2021'); |
| 75 | + } |
| 76 | + config(['confluence.auth' => [$username, $password]]); |
| 77 | + |
| 78 | + if ($this->option('coding_token')) { |
| 79 | + $codingToken = $this->option('coding_token'); |
| 80 | + } else { |
| 81 | + $codingToken = config('coding.token') ?? $this->ask( |
| 82 | + 'CODING Token:', |
| 83 | + '793fe7845effd2596f2da38562336523b58585fb' |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + if ($this->option('coding_team_domain')) { |
| 88 | + $codingTeamDomain = $this->option('coding_team_domain'); |
| 89 | + } else { |
| 90 | + $codingTeamDomain = config('coding.team_domain') ?? $this->ask('CODING 团队域名:', 'codes-farm'); |
| 91 | + } |
| 92 | + $codingTeamDomain = str_replace('.coding.net', '', $codingTeamDomain); |
| 93 | + |
| 94 | + if ($this->option('coding_project_uri')) { |
| 95 | + $codingProjectUri = $this->option('coding_project_uri'); |
| 96 | + } else { |
| 97 | + $codingProjectUri = config('coding.project_uri') ?? $this->ask('CODING 项目标识:', 'laravel-demo'); |
| 98 | + } |
| 99 | + |
| 100 | + $data = Confluence::resource(Content::class)->index(); |
| 101 | + $this->info("已获得 ${data['size']} 条数据"); |
| 102 | + if ($data['size'] == 0) { |
| 103 | + return; |
| 104 | + } |
| 105 | + $this->info("开始导入 CODING:"); |
| 106 | + foreach ($data['results'] as $result) { |
| 107 | + $content = Confluence::resource(Content::class)->show($result['id'], ['expand' => 'body.storage']); |
| 108 | + $result = $coding->createWiki($codingToken, $codingProjectUri, [ |
| 109 | + 'Title' => $content['title'], |
| 110 | + 'Content' => $content['body']['storage']['value'], |
| 111 | + 'ParentIid' => 0, |
| 112 | + ]); |
| 113 | + $path = $result['Response']['Data']['Path']; |
| 114 | + $this->info("https://${codingTeamDomain}.coding.net/p/${codingProjectUri}/wiki/${path}"); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments