-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWiki.php
More file actions
204 lines (194 loc) · 7.47 KB
/
Wiki.php
File metadata and controls
204 lines (194 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace App\Coding;
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use ZipArchive;
class Wiki extends Base
{
public function createWiki($token, $projectName, $data)
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => array_merge([
'Action' => 'CreateWiki',
'ProjectName' => $projectName,
], $data),
]);
return json_decode($response->getBody(), true)['Response']['Data'];
}
public function createMarkdownZip($markdown, $path, $markdownFilename, $title): bool|string
{
$zipFileFullPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $markdownFilename . '-' . Str::uuid() . '.zip';
if ($this->zipArchive->open($zipFileFullPath, ZipArchive::CREATE) !== true) {
Log::error("cannot open <$zipFileFullPath>");
return false;
}
$this->zipArchive->addFromString($markdownFilename, $markdown);
preg_match_all('/!\[\]\(([a-z0-9\/\._\-]+)\)/', $markdown, $matches);
if (!empty($matches)) {
foreach ($matches[1] as $attachment) {
// markdown image title: 
$tmp = explode(' ', $attachment);
$filename = $tmp[0];
$filepath = $path . DIRECTORY_SEPARATOR . $filename;
if (!file_exists($filepath)) {
Log::error("文件不存在", ['filename' => $filename, 'title' => $title]);
continue;
}
$this->zipArchive->addFile($filepath, $filename);
}
}
$this->zipArchive->close();
return $zipFileFullPath;
}
public function createWikiByZip(string $token, string $projectName, array $uploadToken, array $data)
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => [
'Action' => 'CreateWikiByZip',
'ProjectName' => $projectName,
'ParentIid' => $data['ParentIid'],
'FileName' => $data['FileName'],
'Key' => $uploadToken['StorageKey'],
'Time' => $uploadToken['Time'],
'AuthToken' => $uploadToken['AuthToken'],
],
]);
$result = json_decode($response->getBody(), true);
if (!isset($result['Response']['JobId'])) {
return new Exception('failed');
}
return $result['Response'];
}
/**
* 获取 Wiki 导入任务的进度(API 文档未展示,其实此接口已上线)
*
* @param string $token
* @param string $projectName
* @param string $jobId
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws Exception
*/
public function getImportJobStatus(string $token, string $projectName, string $jobId)
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => [
'Action' => 'DescribeImportJobStatus',
'ProjectName' => $projectName,
'JobId' => $jobId,
],
]);
$result = json_decode($response->getBody(), true);
if (isset($result['Response']['Error']['Message'])) {
throw new Exception($result['Response']['Error']['Message']);
}
return $result['Response']['Data'];
}
public function getImportJobStatusWithRetry(string $token, string $projectName, string $jobId, int $retry = 10)
{
$waitingTimes = 0;
while (true) {
// HACK 如果上传成功立即查询,会报错:invoke function
sleep(1);
try {
$jobStatus = $this->getImportJobStatus($token, $projectName, $jobId);
if (in_array($jobStatus['Status'], ['wait_process', 'processing']) && $waitingTimes < $retry) {
$waitingTimes++;
continue;
}
return $jobStatus;
} catch (Exception $e) {
if ($waitingTimes < 10) {
$waitingTimes++;
continue;
}
throw $e;
}
break;
}
}
public function createWikiByUploadZip(string $token, string $projectName, string $zipFileFullPath, int $parentId)
{
$zipFilename = basename($zipFileFullPath);
$uploadToken = $this->createUploadToken(
$token,
$projectName,
$zipFilename
);
$this->upload($uploadToken, $zipFileFullPath);
return $this->createWikiByZip($token, $projectName, $uploadToken, [
'ParentIid' => $parentId,
'FileName' => $zipFilename,
]);
}
public function getWiki(string $token, string $projectName, int $id, int $version = 1)
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => [
'Action' => 'DescribeWiki',
'ProjectName' => $projectName,
'Iid' => $id,
'VersionId' => $version,
],
]);
$result = json_decode($response->getBody(), true);
return $result['Response']['Data'];
}
public function updateTitle(string $token, string $projectName, int $id, string $title): bool
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => [
'Action' => 'ModifyWikiTitle',
'ProjectName' => $projectName,
'Iid' => $id,
'Title' => $title,
],
]);
$result = json_decode($response->getBody(), true);
return $result['Response']['Data']['Title'] == $title;
}
public function replaceAttachments(string $markdown, array $codingAttachments): string
{
if (empty($codingAttachments)) {
return $markdown;
}
$markdown .= "\n\nAttachments\n---\n\n";
foreach ($codingAttachments as $attachmentPath => $codingAttachment) {
$resourceCode = $codingAttachment['ResourceCode'] ?? 0;
$filename = $codingAttachment['FileName'] ?? '此文件迁移失败';
$markdown .= "- #${resourceCode} ${filename}\n";
$markdown = preg_replace(
"|\[.*\]\(${attachmentPath}\)|",
" #${resourceCode} `${filename}`",
$markdown
);
}
return $markdown;
}
}