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\ClientException; |
9
|
|
|
use GuzzleHttp\Psr7\Request; |
10
|
|
|
use GuzzleHttp\Psr7\Stream; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Confluence |
14
|
|
|
* |
15
|
|
|
* @package CodeMine\ConfluenceImporter\Service |
16
|
|
|
*/ |
17
|
|
|
class Confluence |
18
|
|
|
{ |
19
|
|
|
const ADD_PAGE = 'rest/api/content'; |
|
|
|
|
20
|
|
|
const SEARCH_PAGE = 'rest/api/content/search'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \GuzzleHttp\ClientInterface |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
/** |
27
|
|
|
* @var \CodeMine\ConfluenceImporter\Service\Confluence\InstanceInterface |
28
|
|
|
*/ |
29
|
|
|
private $confluenceInstance; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Confluence constructor. |
33
|
|
|
* |
34
|
|
|
* blabdlabsdaldbalsbdals |
35
|
|
|
* aldnalsdlasdn |
36
|
|
|
* alndlasndsdijfpgaijfawl |
37
|
|
|
* |
38
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
39
|
|
|
* @param \CodeMine\ConfluenceImporter\Service\Confluence\InstanceInterface $confluenceInstance |
40
|
|
|
*/ |
41
|
|
|
public function __construct(ClientInterface $client, InstanceInterface $confluenceInstance) |
42
|
|
|
{ |
43
|
|
|
$this->client = $client; |
|
|
|
|
44
|
|
|
$this->confluenceInstance = $confluenceInstance; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $key |
49
|
|
|
* @param PageInterface $page |
50
|
|
|
* @param PageInterface|NULL $parentPage |
51
|
|
|
* @param null $id |
52
|
|
|
* |
53
|
|
|
* @docblock bla bla bla bla bla bla bla |
54
|
|
|
*/ |
55
|
|
|
public function createNewPage($key, PageInterface $page, PageInterface $parentPage = NULL, $id = NULL) |
56
|
|
|
{ |
57
|
|
|
$body = $this->getBody($key, $page, $parentPage, $id); |
|
|
|
|
58
|
|
|
$headers = $this->getHeaders(); |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$request = $this->getRequest($headers, $body); |
|
|
|
|
62
|
|
|
$response = $this->client->send($request); //TODO: find solution for exception on same page name |
|
|
|
|
63
|
|
|
$pageChildren = $page->children(); |
64
|
|
|
|
65
|
|
|
$newPageId = json_decode($response->getBody()->getContents())->id; |
66
|
|
|
|
67
|
|
|
if (null !== $pageChildren) { |
68
|
|
|
foreach ($pageChildren as $pageChild) { |
69
|
|
|
$this->createNewPage($key, $pageChild, $page, $newPageId); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} catch (ClientException $e) { |
73
|
|
|
//silent |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getPageId($key, PageInterface $page) //TODO::Change for private method |
78
|
|
|
{ |
79
|
|
|
$headers = [ |
80
|
|
|
'Authorization' => 'Basic ' . $this->getBase64Credentials(), |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
|
85
|
|
|
$response = $this->client->request('GET', self::SEARCH_PAGE, [ |
86
|
|
|
'headers' => $headers, |
87
|
|
|
'query' => [ |
88
|
|
|
'cql' => sprintf('title="%s" and space="%s"', $page->title(), $key) |
89
|
|
|
|
90
|
|
|
], |
91
|
|
|
'debug' => TRUE |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
} catch (\Exception $e) { |
95
|
|
|
//silent |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** @var Stream $returnBody */ |
99
|
|
|
$returnBody = $response->getBody(); |
100
|
|
|
|
101
|
|
|
$stdBody = json_decode($returnBody->getContents()); |
102
|
|
|
if ($response->getStatusCode() == 200 && count($stdBody->results) == 1) { |
103
|
|
|
$id = (string)$stdBody->results[0]->id; |
104
|
|
|
|
105
|
|
|
return $id; |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
throw new \Exception('ERROR!!!!'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
private function getBase64Credentials() |
115
|
|
|
{ |
116
|
|
|
$base64Credentials = base64_encode( |
117
|
|
|
$this->confluenceInstance->username() . |
118
|
|
|
':' . |
119
|
|
|
$this->confluenceInstance->password() |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
return $base64Credentials; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return array |
|
|
|
|
127
|
|
|
*/ |
128
|
|
|
private function getHeaders() |
129
|
|
|
{ |
130
|
|
|
$headers = [ |
131
|
|
|
'Authorization' => 'Basic ' . $this->getBase64Credentials(), |
132
|
|
|
'Content-Type' => 'application/json' |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
return $headers; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $key |
140
|
|
|
* @param PageInterface $page |
141
|
|
|
* @param PageInterface|NULL $parentPage |
142
|
|
|
* @return string<json> |
|
|
|
|
143
|
|
|
* @throws \Exception |
144
|
|
|
*/ |
145
|
|
|
private function getBody($key, PageInterface $page, PageInterface $parentPage = NULL, $id = NULL) |
146
|
|
|
{ |
147
|
|
|
$bodyArray = [ |
148
|
|
|
'type' => 'page', |
149
|
|
|
'title' => $page->title(), |
150
|
|
|
'space' => [ |
151
|
|
|
'key' => $key |
152
|
|
|
], |
153
|
|
|
'body' => [ |
154
|
|
|
'storage' => [ |
155
|
|
|
'value' => $page->content(), |
156
|
|
|
'representation' => 'storage' |
157
|
|
|
] |
158
|
|
|
] |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
if (isset($parentPage)) { |
162
|
|
|
if (isset($id)){ |
163
|
|
|
$bodyArray['ancestors'] = [ |
164
|
|
|
['id' => $id] |
165
|
|
|
]; |
166
|
|
|
}else { |
167
|
|
|
$pageId = $this->getPageId($key, $parentPage); |
|
|
|
|
168
|
|
|
$bodyArray['ancestors'] = [ |
169
|
|
|
['id' => $pageId] |
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$body = json_encode($bodyArray); |
175
|
|
|
|
176
|
|
|
return $body; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $headers |
181
|
|
|
* @param $body |
182
|
|
|
* @return Request |
183
|
|
|
*/ |
184
|
|
|
private function getRequest($headers, $body) |
185
|
|
|
{ |
186
|
|
|
|
187
|
|
|
$request = new Request('POST', Confluence::ADD_PAGE, $headers, $body); |
|
|
|
|
188
|
|
|
|
189
|
|
|
return $request; |
190
|
|
|
} |
191
|
|
|
} |
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.