@@ -148,8 +148,12 @@ private function handleConfluenceApi(): int
148148
149149 private function handleConfluenceHtml (): int
150150 {
151- $ htmlDir = $ this ->unzipConfluenceHtml ();
152- $ filePath = $ htmlDir . 'index.html ' ;
151+ $ path = $ this ->unzipConfluenceHtml ();
152+ if (str_ends_with ($ path , '.html ' )) {
153+ return $ this ->uploadConfluencePage ($ path );
154+ }
155+ $ htmlDir = $ path ;
156+ $ filePath = $ htmlDir . DIRECTORY_SEPARATOR . 'index.html ' ;
153157 if (!file_exists ($ filePath )) {
154158 $ message = "文件不存在: $ filePath " ;
155159 $ this ->error ($ message );
@@ -188,55 +192,75 @@ private function handleConfluenceHtml(): int
188192 return 0 ;
189193 }
190194
191- private function uploadConfluencePages (string $ dataPath , array $ tree , array $ titles , int $ parentId = 0 ): void
195+ private function uploadConfluencePages (string $ htmlDir , array $ tree , array $ titles , int $ parentId = 0 ): void
192196 {
193197 foreach ($ tree as $ page => $ subPages ) {
194198 $ title = $ titles [$ page ];
195- $ this ->info ('标题: ' . $ title );
196- try {
197- $ markdown = $ this ->confluence ->htmlFile2Markdown ($ dataPath . $ page );
198- } catch (FileNotFoundException $ e ) {
199- $ this ->error ('页面不存在: ' . $ dataPath . $ page );
200- continue ;
199+ $ wikiId = $ this ->uploadConfluencePage ($ htmlDir . DIRECTORY_SEPARATOR . $ page , $ title , $ parentId );
200+ if ($ wikiId && !empty ($ subPages )) {
201+ $ this ->info ('发现 ' . count ($ subPages ) . ' 个子页面 ' );
202+ // TODO tests
203+ $ this ->uploadConfluencePages ($ htmlDir , $ subPages , $ titles , $ wikiId );
201204 }
202- $ mdFilename = $ this ->dealAttachments ($ dataPath , $ page , $ markdown );
203- $ zipFilePath = $ this ->codingWiki ->createMarkdownZip ($ markdown , $ dataPath , $ mdFilename , $ title );
204- $ result = $ this ->codingWiki ->createWikiByUploadZip (
205+ }
206+ }
207+
208+ private function uploadConfluencePage (string $ filePath , string $ title = '' , int $ parentId = 0 ): int
209+ {
210+ try {
211+ $ markdown = $ this ->confluence ->htmlFile2Markdown ($ filePath );
212+ } catch (FileNotFoundException $ e ) {
213+ $ message = '页面不存在: ' . $ filePath ;
214+ $ this ->error ($ message );
215+ $ this ->errors [] = $ message ;
216+ return false ;
217+ }
218+ libxml_use_internal_errors (true );
219+ $ this ->document ->loadHTMLFile ($ filePath );
220+ if (empty ($ title )) {
221+ $ title = $ this ->document ->getElementsByTagName ('title ' )[0 ]->nodeValue ;
222+ }
223+ $ this ->info ('标题: ' . $ title );
224+
225+ $ htmlDir = dirname ($ filePath );
226+ $ page = basename ($ filePath );
227+ $ markdown = $ this ->dealAttachments ($ filePath , $ markdown );
228+ $ mdFilename = substr ($ page , 0 , -5 ) . '.md ' ;
229+ if ($ this ->option ('save-markdown ' )) {
230+ file_put_contents ($ htmlDir . DIRECTORY_SEPARATOR . $ mdFilename , $ markdown . "\n" );
231+ }
232+ $ zipFilePath = $ this ->codingWiki ->createMarkdownZip ($ markdown , $ htmlDir , $ mdFilename , $ title );
233+ $ result = $ this ->codingWiki ->createWikiByUploadZip (
234+ $ this ->codingToken ,
235+ $ this ->codingProjectUri ,
236+ $ zipFilePath ,
237+ $ parentId ,
238+ );
239+ $ this ->info ('上传成功,正在处理,任务 ID: ' . $ result ['JobId ' ]);
240+ $ wikiId = null ;
241+ try {
242+ $ jobStatus = $ this ->codingWiki ->getImportJobStatusWithRetry (
205243 $ this ->codingToken ,
206244 $ this ->codingProjectUri ,
207- $ zipFilePath ,
208- $ parentId ,
245+ $ result ['JobId ' ]
209246 );
210- $ this ->info ('上传成功,正在处理,任务 ID: ' . $ result ['JobId ' ]);
211- $ wikiId = null ;
212- try {
213- $ jobStatus = $ this ->codingWiki ->getImportJobStatusWithRetry (
214- $ this ->codingToken ,
215- $ this ->codingProjectUri ,
216- $ result ['JobId ' ]
217- );
218- } catch (Exception $ e ) {
219- $ message = '错误:导入失败,跳过 ' . $ title . ' ' . $ page ;
220- $ this ->error ($ message );
221- $ this ->errors [] = $ message ;
222- continue ;
223- }
224- if ($ jobStatus ['Status ' ] == 'success ' ) {
225- $ wikiId = intval ($ jobStatus ['Iids ' ][0 ]);
226- }
227- if (empty ($ wikiId )) {
228- $ message = '错误:导入失败,跳过 ' . $ title . ' ' . $ page ;
229- $ this ->error ($ message );
230- $ this ->errors [] = $ message ;
231- continue ;
232- }
233- $ this ->codingWiki ->updateTitle ($ this ->codingToken , $ this ->codingProjectUri , $ wikiId , $ title );
234- if (!empty ($ subPages )) {
235- $ this ->info ('发现 ' . count ($ subPages ) . ' 个子页面 ' );
236- // TODO tests
237- $ this ->uploadConfluencePages ($ dataPath , $ subPages , $ titles , $ wikiId );
238- }
247+ } catch (Exception $ e ) {
248+ $ message = '错误:导入失败,跳过 ' . $ title . ' ' . $ page ;
249+ $ this ->error ($ message );
250+ $ this ->errors [] = $ message ;
251+ return false ;
252+ }
253+ if ($ jobStatus ['Status ' ] == 'success ' ) {
254+ $ wikiId = intval ($ jobStatus ['Iids ' ][0 ]);
239255 }
256+ if (empty ($ wikiId )) {
257+ $ message = '错误:导入失败,跳过 ' . $ title . ' ' . $ page ;
258+ $ this ->error ($ message );
259+ $ this ->errors [] = $ message ;
260+ return false ;
261+ }
262+ $ this ->codingWiki ->updateTitle ($ this ->codingToken , $ this ->codingProjectUri , $ wikiId , $ title );
263+ return $ wikiId ;
240264 }
241265
242266 private function unzipConfluenceHtml (): string
@@ -263,16 +287,16 @@ private function unzipConfluenceHtml(): string
263287 $ zip ->close ();
264288 return $ tmpDir . '/ ' . scandir ($ tmpDir , 1 )[0 ] . '/ ' ;
265289 }
266- return str_ends_with ( $ dataPath , ' /index.html ' ) ? substr ( $ dataPath , 0 , - 10 ) : Str:: finish ($ dataPath , '/ ' );
290+ return rtrim ($ dataPath , '/ ' );
267291 }
268292
269- private function dealAttachments (string $ dataPath , string $ page , string $ markdown ): string
293+ private function dealAttachments (string $ filePath , string $ markdown ): string
270294 {
271- $ attachments = $ this ->confluence ->parseAttachments ($ dataPath . $ page , $ markdown );
295+ $ attachments = $ this ->confluence ->parseAttachments ($ filePath , $ markdown );
272296 $ codingAttachments = $ this ->codingDisk ->uploadAttachments (
273297 $ this ->codingToken ,
274298 $ this ->codingProjectUri ,
275- $ dataPath ,
299+ dirname ( $ filePath ) ,
276300 $ attachments
277301 );
278302 foreach ($ codingAttachments as $ attachmentPath => $ codingAttachment ) {
@@ -282,11 +306,6 @@ private function dealAttachments(string $dataPath, string $page, string $markdow
282306 $ this ->errors [] = $ message ;
283307 }
284308 }
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 ;
309+ return $ this ->codingWiki ->replaceAttachments ($ markdown , $ codingAttachments );
291310 }
292311}
0 commit comments