Skip to content

Commit 5721858

Browse files
committed
refactor: #11 parse html
1 parent bdaca4f commit 5721858

File tree

11 files changed

+90
-83
lines changed

11 files changed

+90
-83
lines changed

app/Coding.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public function createWiki($token, $projectName, $data)
2626
'ProjectName' => $projectName,
2727
], $data),
2828
]);
29-
return json_decode($response->getBody(), true);
29+
return json_decode($response->getBody(), true)['Response']['Data'];
3030
}
3131
}

app/Commands/WikiImportCommand.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ class WikiImportCommand extends Command
3838
private string $codingTeamDomain;
3939
private string $codingToken;
4040
private Coding $coding;
41+
private \App\Confluence $confluence;
4142
private \DOMDocument $document;
4243

4344
/**
4445
* Execute the console command.
4546
*
4647
*/
47-
public function handle(Coding $coding, \DOMDocument $document): int
48+
public function handle(Coding $coding, \App\Confluence $confluence, \DOMDocument $document): int
4849
{
4950
$this->coding = $coding;
51+
$this->confluence = $confluence;
5052
$this->document = $document;
5153
$this->setCodingApi();
5254

@@ -86,7 +88,7 @@ public function handle(Coding $coding, \DOMDocument $document): int
8688
private function createWiki($data)
8789
{
8890
$result = $this->coding->createWiki($this->codingToken, $this->codingProjectUri, $data);
89-
$path = $result['Response']['Data']['Path'];
91+
$path = $result['Path'];
9092
$this->info("https://{$this->codingTeamDomain}.coding.net/p/{$this->codingProjectUri}/wiki/${path}");
9193
}
9294

@@ -189,14 +191,12 @@ private function handleConfluenceHtml(): int
189191
$pages[] = $aElement->getAttribute('href');
190192
}
191193
foreach ($pages as $page) {
192-
$this->document->loadHTMLFile($dataPath . $page);
193-
$title = trim($this->document->getElementById('title-text')->nodeValue);
194-
$title = str_replace($space['name'] . ' : ', '', $title);
195-
$this->info("标题:${title}");
194+
$data = $this->confluence->parsePageHtml($dataPath . $page, $space['name']);
195+
$this->info("标题:${data['title']}");
196196

197197
$this->createWiki([
198-
'Title' => $title,
199-
'Content' => trim($this->document->getElementById('main-content')->nodeValue),
198+
'Title' => $data['title'],
199+
'Content' => $data['content'],
200200
'ParentIid' => 0,
201201
]);
202202
}

app/Confluence.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App;
4+
5+
class Confluence
6+
{
7+
private \DOMDocument $document;
8+
9+
public function __construct(\DOMDocument $document = null)
10+
{
11+
$this->document = $document ?? new \DOMDocument();
12+
}
13+
14+
public function parsePageHtml(string $filename, string $spaceName): array
15+
{
16+
libxml_use_internal_errors(true);
17+
$this->document->loadHTMLFile($filename);
18+
$title = trim($this->document->getElementById('title-text')->nodeValue);
19+
$title = str_replace($spaceName . ' : ', '', $title);
20+
21+
$content = trim($this->document->getElementById('main-content')->nodeValue);
22+
return [
23+
'title' => $title,
24+
'content' => $content,
25+
];
26+
}
27+
}

tests/Feature/WikiImportCommandTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class WikiImportCommandTest extends TestCase
1515
protected function setUp(): void
1616
{
1717
parent::setUp();
18-
$this->codingResponse = json_decode(file_get_contents($this->dataDir . 'coding/createWikiResponse.json'), true);
18+
$this->codingResponse = json_decode(file_get_contents($this->dataDir . 'coding/createWikiResponse.json'), true)
19+
['Response']['Data'];
1920
}
2021

2122
public function testHandleIndex()
@@ -158,16 +159,7 @@ public function testHandleConfluenceHtml()
158159
$codingProjectUri,
159160
$codingResponse
160161
) {
161-
$mock->shouldReceive('createWiki')->once()->withArgs([
162-
$codingToken,
163-
$codingProjectUri,
164-
[
165-
'Title' => 'Text Demo',
166-
'Content' => '你好',
167-
'ParentIid' => 0,
168-
]
169-
])->andReturn($codingResponse);
170-
$mock->shouldReceive('createWiki')->times(1)->andReturn($codingResponse);
162+
$mock->shouldReceive('createWiki')->times(2)->andReturn($codingResponse);
171163
});
172164

173165
$this->artisan('wiki:import')
@@ -178,7 +170,7 @@ public function testHandleConfluenceHtml()
178170
->expectsOutput('空间标识:demo')
179171
->expectsOutput('发现 2 个一级页面')
180172
->expectsOutput("开始导入 CODING:")
181-
->expectsOutput('标题:Text Demo')
173+
->expectsOutput('标题:Image Demo')
182174
->expectsOutput("https://${codingTeamDomain}.coding.net/p/$codingProjectUri/wiki/27")
183175
->expectsOutput('标题:Demo')
184176
->expectsOutput("https://${codingTeamDomain}.coding.net/p/$codingProjectUri/wiki/27")

tests/TestCase.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,5 @@ class TestCase extends BaseTestCase
1010
use CreatesApplication;
1111
use WithFaker;
1212

13-
protected string $dataDir;
14-
15-
protected function setUp(): void
16-
{
17-
parent::setUp();
18-
$this->dataDir = __DIR__ . '/data/';
19-
}
13+
protected string $dataDir = __DIR__ . '/data/';
2014
}

tests/Unit/CodingTest.php

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,7 @@ class CodingTest extends TestCase
1111
{
1212
public function testCreateWiki()
1313
{
14-
$responseBody = '{
15-
"Response" : {
16-
"Data" : {
17-
"CanMaintain" : true,
18-
"CanRead" : true,
19-
"Content" : "foo foo",
20-
"CreatedAt" : 1625214079010,
21-
"Creator" : {
22-
"Avatar" : "https://coding-net-production-static-ci.codehub.cn/2cb665a3-aa00-2b6df3e33edc.jpg",
23-
"Email" : "",
24-
"GlobalKey" : "KMRnIKgzbV",
25-
"Id" : 183478,
26-
"Name" : "sinkcup",
27-
"Phone" : "",
28-
"RequestId" : "",
29-
"Status" : "ACTIVE",
30-
"TeamId" : 0
31-
},
32-
"CreatorId" : 0,
33-
"CurrentUserRoleId" : 0,
34-
"CurrentVersion" : 1,
35-
"Editor" : {
36-
"Avatar" : "https://coding-net-production-static-ci.codehub.cn/2cb665a3--aa00-2b6df3e33edc.jpg",
37-
"Email" : "",
38-
"GlobalKey" : "KMRnIKgzbV",
39-
"Id" : 183478,
40-
"Name" : "sinkcup",
41-
"Phone" : "",
42-
"RequestId" : "",
43-
"Status" : "ACTIVE",
44-
"TeamId" : 0
45-
},
46-
"EditorId" : 0,
47-
"HistoriesCount" : 1,
48-
"HistoryId" : 2707176,
49-
"Html" : "<p>foo foo</p>",
50-
"Id" : 1325288,
51-
"Iid" : 27,
52-
"LastVersion" : 1,
53-
"Msg" : "",
54-
"Order" : 2,
55-
"ParentIid" : 0,
56-
"ParentShared" : false,
57-
"ParentVisibleRange" : "PUBLIC",
58-
"Path" : "27",
59-
"Title" : "foo by curl",
60-
"UpdatedAt" : 1625214079014,
61-
"VisibleRange" : "INHERIT"
62-
},
63-
"RequestId" : "a50c8805-8e1f-fc4d-f965-855e5a3cf709"
64-
}
65-
}';
14+
$responseBody = file_get_contents($this->dataDir . 'coding/createWikiResponse.json');
6615
$codingToken = $this->faker->md5;
6716
$codingProjectUri = $this->faker->slug;
6817
$article = [
@@ -92,6 +41,6 @@ public function testCreateWiki()
9241
->willReturn(new Response(200, [], $responseBody));
9342
$coding = new Coding($clientMock);
9443
$result = $coding->createWiki($codingToken, $codingProjectUri, $article);
95-
$this->assertEquals(json_decode($responseBody, true), $result);
44+
$this->assertEquals(json_decode($responseBody, true)['Response']['Data'], $result);
9645
}
9746
}

tests/Unit/ConfluenceTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use App\Confluence;
6+
use Tests\TestCase;
7+
8+
class ConfluenceTest extends TestCase
9+
{
10+
public function testParsePageHtml()
11+
{
12+
$confluence = new Confluence();
13+
$result = $confluence->parsePageHtml($this->dataDir . 'confluence/space-1/text-demo_65601.html', 'Demo');
14+
$this->assertEquals([
15+
'title' => 'Text Demo',
16+
'content' => '你好',
17+
], $result);
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Response": {
3+
"RequestId": "45a02fd8-95aa-f3c3-84fa-f2752e0edb8a",
4+
"Token": {
5+
"AuthToken": "65e5968b5e17d5aaa3f5d33200aca2d1911fe2ad2948b47d899d46e6da1e4",
6+
"Provide": "TENCENT",
7+
"SecretId": "AKIDU-VqQm39vRar-ZrHj1UIE5u2gYJ7gWFcG2ThwFNO9eU1HbyQlZp8vVcQ99",
8+
"SecretKey": "clUYSNeg2es16EILsrF6RyCD3ss6uFLX3Xgc=",
9+
"UploadLink": "https://coding-net-dev-file-123456.cos.ap-shanghai.myqcloud.com",
10+
"UpToken": "EOlMEc2x0xbrFoL9CMy7OqDl5413654938410a360a63207e30dab4655pMKmNJ3t5M-Z8bGt",
11+
"Time": 1625579588693
12+
}
13+
}
14+
}
Loading

tests/data/confluence/space-1/image-demo_65619.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ <h3 style="text-align: left;" id="helloworld-1.2Whydoweuseit?">1.2 Why do we use
5353
Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites
5454
still in their infancy. Various versions have evolved over the years, sometimes by accident,
5555
sometimes on purpose (injected humour and the like).</p>
56+
<p><span class="confluence-embedded-file-wrapper confluence-embedded-manual-size"><img
57+
class="confluence-embedded-image" height="250" src="attachments/65619/65624.png"
58+
data-image-src="attachments/65619/65624.png" data-unresolved-comment-count="0"
59+
data-linked-resource-id="65624" data-linked-resource-version="1"
60+
data-linked-resource-type="attachment"
61+
data-linked-resource-default-alias="coding-logo.png"
62+
data-base-url="http://9.134.190.26:8090" data-linked-resource-content-type="image/png"
63+
data-linked-resource-container-id="65619" data-linked-resource-container-version="4"></span></p>
64+
<p>
5665
<h2 id="helloworld-第二章">第二章</h2>
5766
<p>world</p>
5867
<p><span class="confluence-embedded-file-wrapper confluence-embedded-manual-size"><img
@@ -75,6 +84,9 @@ <h2 id="attachments" class="pageSectionTitle">Attachments:</h2>
7584
<img src="images/icons/bullet_blue.gif" height="8" width="8" alt=""/>
7685
<a href="attachments/65619/65623.png">github-ubuntu-16.04.png</a> (image/png)
7786
<br/>
87+
<img src="images/icons/bullet_blue.gif" height="8" width="8" alt=""/>
88+
<a href="attachments/65619/65624.png">coding-logo.png</a> (image/png)
89+
<br/>
7890
</div>
7991
</div>
8092

tests/data/confluence/space-1/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ <h2 class="pageSectionTitle">Available Pages:</h2>
4545
</div>
4646
<ul>
4747
<li>
48-
<a href="text-demo_65601.html">Text Demo</a>
48+
<a href="image-demo_65619.html">Image Demo</a>
4949

5050
</li>
5151
<li>
@@ -61,7 +61,7 @@ <h2 class="pageSectionTitle">Available Pages:</h2>
6161
</ul>
6262
<ul>
6363
<li>
64-
<a href="image-demo_65619.html">Image Demo</a>
64+
<a href="text-demo_65601.html">Text Demo</a>
6565

6666
</li>
6767
</ul>

0 commit comments

Comments
 (0)