Skip to content

Commit 9c268e3

Browse files
committed
feat: #66 ignore page not found
1 parent bbfb879 commit 9c268e3

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

app/Commands/WikiImportCommand.php

+31-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Confluence\Content;
88
use DOMDocument;
99
use Exception;
10+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
1011
use Illuminate\Support\Str;
1112
use LaravelFans\Confluence\Facades\Confluence;
1213
use LaravelZero\Framework\Commands\Command;
@@ -192,26 +193,13 @@ private function uploadConfluencePages(string $dataPath, array $tree, array $tit
192193
foreach ($tree as $page => $subPages) {
193194
$title = $titles[$page];
194195
$this->info('标题:' . $title);
195-
$markdown = $this->confluence->htmlFile2Markdown($dataPath . $page);
196-
$attachments = $this->confluence->parseAttachments($dataPath . $page, $markdown);
197-
$codingAttachments = $this->codingDisk->uploadAttachments(
198-
$this->codingToken,
199-
$this->codingProjectUri,
200-
$dataPath,
201-
$attachments
202-
);
203-
foreach ($codingAttachments as $attachmentPath => $codingAttachment) {
204-
if (empty($codingAttachment)) {
205-
$message = '错误:文件上传失败 ' . $attachmentPath;
206-
$this->error($message);
207-
$this->errors[] = $message;
208-
}
209-
}
210-
$markdown = $this->codingWiki->replaceAttachments($markdown, $codingAttachments);
211-
$mdFilename = substr($page, 0, -5) . '.md';
212-
if ($this->option('save-markdown')) {
213-
file_put_contents($dataPath . $mdFilename, $markdown . "\n");
196+
try {
197+
$markdown = $this->confluence->htmlFile2Markdown($dataPath . $page);
198+
} catch (FileNotFoundException $e) {
199+
$this->error('页面不存在:' . $dataPath . $page);
200+
continue;
214201
}
202+
$mdFilename = $this->dealAttachments($dataPath, $page, $markdown);
215203
$zipFilePath = $this->codingWiki->createMarkdownZip($markdown, $dataPath, $mdFilename, $title);
216204
$result = $this->codingWiki->createWikiByUploadZip(
217205
$this->codingToken,
@@ -277,4 +265,28 @@ private function unzipConfluenceHtml(): string
277265
}
278266
return str_ends_with($dataPath, '/index.html') ? substr($dataPath, 0, -10) : Str::finish($dataPath, '/');
279267
}
268+
269+
private function dealAttachments(string $dataPath, string $page, string $markdown): string
270+
{
271+
$attachments = $this->confluence->parseAttachments($dataPath . $page, $markdown);
272+
$codingAttachments = $this->codingDisk->uploadAttachments(
273+
$this->codingToken,
274+
$this->codingProjectUri,
275+
$dataPath,
276+
$attachments
277+
);
278+
foreach ($codingAttachments as $attachmentPath => $codingAttachment) {
279+
if (empty($codingAttachment)) {
280+
$message = '错误:文件上传失败 ' . $attachmentPath;
281+
$this->error($message);
282+
$this->errors[] = $message;
283+
}
284+
}
285+
$markdown = $this->codingWiki->replaceAttachments($markdown, $codingAttachments);
286+
$mdFilename = substr($page, 0, -5) . '.md';
287+
if ($this->option('save-markdown')) {
288+
file_put_contents($dataPath . $mdFilename, $markdown . "\n");
289+
}
290+
return $mdFilename;
291+
}
280292
}

app/Confluence.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DOMDocument;
66
use DOMXPath;
7+
use Illuminate\Support\Facades\File;
78
use League\HTMLToMarkdown\Converter\TableConverter;
89
use League\HTMLToMarkdown\HtmlConverter;
910

@@ -46,7 +47,7 @@ public function htmlFile2Markdown(string $filename): string
4647
'|<img .* src="data:.*/>|',
4748
],
4849
'',
49-
file_get_contents($filename)
50+
File::get($filename)
5051
);
5152
libxml_use_internal_errors(true);
5253
$this->document->loadHTML($html);

tests/Feature/WikiImportCommandTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ public function testHandleConfluenceHtmlSuccess()
186186
->expectsQuestion('空间导出的 HTML zip 文件路径', $this->dataDir . 'confluence/space1/')
187187
->expectsOutput('空间名称:空间 1')
188188
->expectsOutput('空间标识:space1')
189-
->expectsOutput('发现 2 个一级页面')
189+
->expectsOutput('发现 3 个一级页面')
190190
->expectsOutput("开始导入 CODING:")
191+
->expectsOutput('标题:Not Found')
192+
->expectsOutput('页面不存在:' . $this->dataDir . 'confluence/space1/not-found.html')
191193
->expectsOutput('标题:Image Demo')
192194
->expectsOutput('上传成功,正在处理,任务 ID:a12353fa-f45b-4af2-83db-666bf9f66615')
193195
->expectsOutput('标题:你好世界')

tests/data/confluence/space1/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ <h1 id="title-heading" class="pagetitle">
4444
<h2 class="pageSectionTitle">Available Pages:</h2>
4545
</div>
4646
<ul>
47+
<li>
48+
<a href="not-found.html">Not Found</a>
49+
4750
<li>
4851
<a href="image-demo_65619.html">Image Demo</a>
4952

0 commit comments

Comments
 (0)