1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodeMine\ConfluenceImporter\Service; |
4
|
|
|
|
5
|
|
|
use CodeMine\ConfluenceImporter\Documentation\PageInterface; |
6
|
|
|
use CodeMine\ConfluenceImporter\Service\Confluence\InstanceInterface; |
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
9
|
|
|
use GuzzleHttp\Exception\ClientException; |
10
|
|
|
use GuzzleHttp\Psr7\Request; |
11
|
|
|
use GuzzleHttp\Psr7\Stream; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Confluence |
15
|
|
|
* |
16
|
|
|
* @package CodeMine\ConfluenceImporter\Service |
17
|
|
|
*/ |
18
|
|
|
class Confluence |
19
|
|
|
{ |
20
|
|
|
const ADD_PAGE = 'rest/api/content'; |
|
|
|
|
21
|
|
|
const SEARCH_PAGE = 'rest/api/content/search?cql=title="%s" and space="%s"'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \GuzzleHttp\ClientInterface |
25
|
|
|
*/ |
26
|
|
|
private $client; |
27
|
|
|
/** |
28
|
|
|
* @var \CodeMine\ConfluenceImporter\Service\InstanceInterface |
29
|
|
|
*/ |
30
|
|
|
private $confluenceInstance; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Confluence constructor. |
34
|
|
|
* |
35
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
36
|
|
|
* @param \CodeMine\ConfluenceImporter\Service\Confluence\InstanceInterface $confluenceInstance |
37
|
|
|
*/ |
38
|
|
|
public function __construct(ClientInterface $client, InstanceInterface $confluenceInstance) |
39
|
|
|
{ |
40
|
|
|
$this->client = $client; |
|
|
|
|
41
|
|
|
$this->confluenceInstance = $confluenceInstance; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function createNewPage(string $key, PageInterface $page, PageInterface $parentPage = NULL) |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
$body = $this->getBody($key, $page, $parentPage); |
|
|
|
|
48
|
|
|
$headers = $this->getHeaders(); |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$request = $this->getRequest($headers, $body); |
|
|
|
|
54
|
|
|
$response = $this->client->send($request); //TODO: find solution for exception on same page name |
|
|
|
|
55
|
|
|
$pageChildren = $page->children(); |
56
|
|
|
if (null !== $pageChildren){ |
57
|
|
|
var_dump('1'); |
|
|
|
|
58
|
|
|
var_dump(urlencode('"sda sd"'));die; |
|
|
|
|
59
|
|
|
foreach($pageChildren as $pageChild){ |
|
|
|
|
60
|
|
|
var_dump('2'); |
61
|
|
|
|
62
|
|
|
$childBody = $this->getBody($key, $pageChild, $page); |
|
|
|
|
63
|
|
|
var_dump('child body'); |
64
|
|
|
$childRequest = $this->getRequest($headers, $pageChild); |
|
|
|
|
65
|
|
|
try { |
66
|
|
|
var_dump('3'); |
67
|
|
|
|
68
|
|
|
$this->client->send($request); |
69
|
|
|
}catch(\Exception $e){ |
70
|
|
|
var_dump($e->getMessage()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
var_dump(urlencode('"sda sd"'));die; |
|
|
|
|
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
// var_dump($response->); |
|
|
|
|
78
|
|
|
}catch(ClientException $e){ |
79
|
|
|
|
80
|
|
|
echo 'ERROR!'; |
81
|
|
|
var_dump($e->getMessage()); |
82
|
|
|
echo 'ERROR!!!'; |
83
|
|
|
|
84
|
|
|
return FALSE; |
85
|
|
|
} |
86
|
|
|
$returnBody = $response->getBody(); |
87
|
|
|
$stdBody = json_decode($returnBody->getContents()); |
|
|
|
|
88
|
|
|
$pageId = (string)$stdBody->id; |
|
|
|
|
89
|
|
|
|
90
|
|
|
return TRUE; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getPageId(string $key, PageInterface $page) //TODO::Change for private method |
94
|
|
|
{ |
95
|
|
|
$headers = [ |
96
|
|
|
'Authorization' => 'Basic ' . $this->getBase64Credentials(), |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
$url = sprintf(Confluence::SEARCH_PAGE, $page->title(), $key); |
|
|
|
|
100
|
|
|
var_dump($url); |
|
|
|
|
101
|
|
|
|
102
|
|
|
|
103
|
|
|
var_dump('ID'); |
104
|
|
|
$request = new Request('GET', $url, $headers); |
105
|
|
|
|
106
|
|
|
try { |
107
|
|
|
var_dump('try'); |
108
|
|
|
//var_dump($request);die; |
|
|
|
|
109
|
|
|
$response = $this->client->send($request); |
110
|
|
|
}catch(\Exception $e){ |
111
|
|
|
var_dump('EEEEEEEEEEEEEEEERRRROOOOOOOOOOOOR'); |
112
|
|
|
var_dump($e->getMessage()); |
113
|
|
|
die; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
var_dump($response->getBody());die; |
|
|
|
|
116
|
|
|
/** @var Stream $returnBody */ |
117
|
|
|
$returnBody = $response->getBody(); |
|
|
|
|
118
|
|
|
$returnBody-> |
119
|
|
|
$e = $response->getHeaders(); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$stdBody = json_decode($returnBody->getContents()); |
122
|
|
|
|
123
|
|
|
// var_dump($stdBody); |
|
|
|
|
124
|
|
|
// var_dump($response->getStatusCode()); |
125
|
|
|
// var_dump(count($stdBody->results)); |
126
|
|
|
// var_dump($stdBody->results); |
127
|
|
|
|
128
|
|
|
if ($response->getStatusCode() == 200 && count($stdBody->results) == 1){ |
|
|
|
|
129
|
|
|
|
130
|
|
|
$id = (string)$stdBody->results[0]->id; |
131
|
|
|
|
132
|
|
|
return $id; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
throw new \Exception('ERROR!!!!'); |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
private function getBase64Credentials() |
144
|
|
|
{ |
145
|
|
|
$base64Credentials = base64_encode( |
146
|
|
|
$this->confluenceInstance->username() . |
147
|
|
|
':' . |
148
|
|
|
$this->confluenceInstance->password() |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
return $base64Credentials; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return array |
|
|
|
|
156
|
|
|
*/ |
157
|
|
|
private function getHeaders() |
158
|
|
|
{ |
159
|
|
|
$headers = [ |
160
|
|
|
'Authorization' => 'Basic ' . $this->getBase64Credentials(), |
161
|
|
|
'Content-Type' => 'application/json' |
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
return $headers; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $key |
169
|
|
|
* @param PageInterface $page |
170
|
|
|
* @param PageInterface|NULL $parentPage |
171
|
|
|
* @return string<json> |
|
|
|
|
172
|
|
|
* @throws \Exception |
173
|
|
|
*/ |
174
|
|
|
private function getBody(string $key, PageInterface $page, PageInterface $parentPage = NULL) |
175
|
|
|
{ |
176
|
|
|
$bodyArray = [ |
177
|
|
|
'type' => 'page', |
178
|
|
|
'title' => $page->title(), |
179
|
|
|
'space' => [ |
180
|
|
|
'key' => $key |
181
|
|
|
], |
182
|
|
|
'body' => [ |
183
|
|
|
'storage' => [ |
184
|
|
|
'value' => $page->content(), |
185
|
|
|
'representation' => 'storage' |
186
|
|
|
] |
187
|
|
|
] |
188
|
|
|
]; |
189
|
|
|
|
190
|
|
|
if (isset($parentPage)) { |
191
|
|
|
$parentPageId = $this->getPageId($key, $parentPage); |
192
|
|
|
|
193
|
|
|
$bodyArray['ancestors'] = [ |
194
|
|
|
['id' => $parentPageId] |
195
|
|
|
]; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$body = json_encode($bodyArray); |
199
|
|
|
|
200
|
|
|
return $body; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $headers |
205
|
|
|
* @param $body |
206
|
|
|
* @return Request |
207
|
|
|
*/ |
208
|
|
|
private function getRequest($headers, $body) |
209
|
|
|
{ |
210
|
|
|
$request = new Request('POST', Confluence::ADD_PAGE, $headers, $body); |
|
|
|
|
211
|
|
|
return $request; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
/* |
215
|
|
|
curl -u admin:admin -X POST -H 'Content-Type: application/json' -d' |
216
|
|
|
{"type":"page", |
217
|
|
|
"title":"new page", |
218
|
|
|
ancestors:[{"id":456}], "space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page</p>", |
219
|
|
|
"representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/ | python -mjson.tool |
220
|
|
|
*/ |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.