Skip to content

Commit

Permalink
feat: #66 ignore page not found
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkcup committed Aug 14, 2021
1 parent bbfb879 commit da7bb25
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
50 changes: 31 additions & 19 deletions app/Commands/WikiImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Confluence\Content;
use DOMDocument;
use Exception;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Str;
use LaravelFans\Confluence\Facades\Confluence;
use LaravelZero\Framework\Commands\Command;
Expand Down Expand Up @@ -192,26 +193,13 @@ private function uploadConfluencePages(string $dataPath, array $tree, array $tit
foreach ($tree as $page => $subPages) {
$title = $titles[$page];
$this->info('标题:' . $title);
$markdown = $this->confluence->htmlFile2Markdown($dataPath . $page);
$attachments = $this->confluence->parseAttachments($dataPath . $page, $markdown);
$codingAttachments = $this->codingDisk->uploadAttachments(
$this->codingToken,
$this->codingProjectUri,
$dataPath,
$attachments
);
foreach ($codingAttachments as $attachmentPath => $codingAttachment) {
if (empty($codingAttachment)) {
$message = '错误:文件上传失败 ' . $attachmentPath;
$this->error($message);
$this->errors[] = $message;
}
}
$markdown = $this->codingWiki->replaceAttachments($markdown, $codingAttachments);
$mdFilename = substr($page, 0, -5) . '.md';
if ($this->option('save-markdown')) {
file_put_contents($dataPath . $mdFilename, $markdown . "\n");
try {
$markdown = $this->confluence->htmlFile2Markdown($dataPath . $page);
} catch (FileNotFoundException $e) {
$this->error('页面不存在:' . $dataPath . $page);
continue;
}
$mdFilename = $this->dealAttachments($dataPath, $page, $markdown);
$zipFilePath = $this->codingWiki->createMarkdownZip($markdown, $dataPath, $mdFilename, $title);
$result = $this->codingWiki->createWikiByUploadZip(
$this->codingToken,
Expand Down Expand Up @@ -277,4 +265,28 @@ private function unzipConfluenceHtml(): string
}
return str_ends_with($dataPath, '/index.html') ? substr($dataPath, 0, -10) : Str::finish($dataPath, '/');
}

private function dealAttachments(string $dataPath, string $page, string $markdown): string
{
$attachments = $this->confluence->parseAttachments($dataPath . $page, $markdown);
$codingAttachments = $this->codingDisk->uploadAttachments(
$this->codingToken,
$this->codingProjectUri,
$dataPath,
$attachments
);
foreach ($codingAttachments as $attachmentPath => $codingAttachment) {
if (empty($codingAttachment)) {
$message = '错误:文件上传失败 ' . $attachmentPath;
$this->error($message);
$this->errors[] = $message;
}
}
$markdown = $this->codingWiki->replaceAttachments($markdown, $codingAttachments);
$mdFilename = substr($page, 0, -5) . '.md';
if ($this->option('save-markdown')) {
file_put_contents($dataPath . $mdFilename, $markdown . "\n");
}
return $mdFilename;
}
}
3 changes: 2 additions & 1 deletion app/Confluence.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DOMDocument;
use DOMXPath;
use Illuminate\Support\Facades\File;
use League\HTMLToMarkdown\Converter\TableConverter;
use League\HTMLToMarkdown\HtmlConverter;

Expand Down Expand Up @@ -46,7 +47,7 @@ public function htmlFile2Markdown(string $filename): string
'|<img .* src="data:.*/>|',
],
'',
file_get_contents($filename)
File::get($filename)
);
libxml_use_internal_errors(true);
$this->document->loadHTML($html);
Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/WikiImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ public function testHandleConfluenceHtmlSuccess()
->expectsQuestion('空间导出的 HTML zip 文件路径', $this->dataDir . 'confluence/space1/')
->expectsOutput('空间名称:空间 1')
->expectsOutput('空间标识:space1')
->expectsOutput('发现 2 个一级页面')
->expectsOutput('发现 3 个一级页面')
->expectsOutput("开始导入 CODING:")
->expectsOutput('标题:Not Found')
->expectsOutput('页面不存在:' . $this->dataDir . 'confluence/space1/not-found.html')
->expectsOutput('标题:Image Demo')
->expectsOutput('上传成功,正在处理,任务 ID:a12353fa-f45b-4af2-83db-666bf9f66615')
->expectsOutput('标题:你好世界')
Expand Down
3 changes: 3 additions & 0 deletions tests/data/confluence/space1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ <h1 id="title-heading" class="pagetitle">
<h2 class="pageSectionTitle">Available Pages:</h2>
</div>
<ul>
<li>
<a href="not-found.html">Not Found</a>

<li>
<a href="image-demo_65619.html">Image Demo</a>

Expand Down

0 comments on commit da7bb25

Please sign in to comment.