11
11
12
12
use CloudPlayDev \ConfluenceClient \Entity \ConfluencePage ;
13
13
use InvalidArgumentException ;
14
+ use CloudPlayDev \ConfluenceClient \Exception \Exception ;
15
+ use function count ;
14
16
use function in_array ;
15
17
use function is_array ;
18
+ use function is_string ;
16
19
17
20
class Client
18
21
{
@@ -52,7 +55,7 @@ public function createPage(ConfluencePage $page)
52
55
],
53
56
];
54
57
55
- return $ this ->request ('GET ' , $ this ->curl ->getHost () . '/content ' , $ data );
58
+ return $ this ->request ('GET ' , $ this ->curl ->getHost () . '/rest/api/ content ' , $ data );
56
59
}
57
60
58
61
@@ -76,10 +79,10 @@ public function updatePage(ConfluencePage $page)
76
79
'representation ' => 'storage ' ,
77
80
],
78
81
],
79
- 'version ' => ['number ' => $ page ->getVersion ()]
82
+ 'version ' => ['number ' => $ page ->getVersion ()+ 1 ]
80
83
];
81
84
82
- return $ this ->request ('POST ' , $ this ->curl ->getHost () . "/content/ {$ page ->getId ()}" , $ data );
85
+ return $ this ->request ('PUT ' , $ this ->curl ->getHost () . "/rest/api /content/ {$ page ->getId ()}" , $ data );
83
86
}
84
87
85
88
/**
@@ -91,18 +94,18 @@ public function updatePage(ConfluencePage $page)
91
94
*/
92
95
public function deletePage (string $ id )
93
96
{
94
- return $ this ->request ('DELETE ' , $ this ->curl ->getHost () . "/content/ $ id " );
97
+ return $ this ->request ('DELETE ' , $ this ->curl ->getHost () . "/rest/api/ content/ $ id " );
95
98
}
96
99
97
100
/**
98
101
* Search page by title, space key, type or id
99
102
* @param array<string, string> $parameters
100
- * @return mixed
103
+ * @return ConfluencePage|null
101
104
* @throws Exception
102
105
*/
103
- public function selectPageBy (array $ parameters = [])
106
+ public function selectPageBy (array $ parameters = []): ? ConfluencePage
104
107
{
105
- $ url = $ this ->curl ->getHost () . '/content? ' ;
108
+ $ url = $ this ->curl ->getHost () . '/rest/api/ content? ' ;
106
109
if (isset ($ parameters ['title ' ])) {
107
110
$ url .= "title= {$ parameters ['title ' ]}& " ;
108
111
}
@@ -113,13 +116,46 @@ public function selectPageBy(array $parameters = [])
113
116
$ url .= "type= {$ parameters ['type ' ]}& " ;
114
117
}
115
118
if (isset ($ parameters ['id ' ])) {
116
- $ url = $ this ->curl -> getHost () . ' /content/ ' . $ parameters ['id ' ] . ' ? ' ;
119
+ return $ this ->getPageById (( int ) $ parameters ['id ' ]) ;
117
120
}
118
121
if (isset ($ parameters ['expand ' ])) {
119
122
$ url .= 'expand= ' . $ parameters ['expand ' ];
120
123
}
121
124
122
- return $ this ->request ('GET ' , $ url );
125
+ $ searchResponse = $ this ->request ('GET ' , $ url );
126
+
127
+ if (is_array ($ searchResponse ) && isset ($ searchResponse ['results ' ], $ searchResponse ['size ' ]) && is_array ($ searchResponse ['results ' ]) && $ searchResponse ['size ' ] >= 1 && count ($ searchResponse ['results ' ]) >= 1 ) {
128
+ $ firstPage = (array )reset ($ searchResponse ['results ' ]);
129
+ if (isset ($ firstPage ['id ' ])) {
130
+ return $ this ->getPageById ((int )$ firstPage ['id ' ]);
131
+ }
132
+ }
133
+ return null ;
134
+ }
135
+
136
+ public function getPageById (int $ pageId ): ?ConfluencePage
137
+ {
138
+ $ url = $ this ->curl ->getHost () . '/rest/api/content/ ' .$ pageId ;
139
+ $ firstPage = $ this ->request ('GET ' , $ url );
140
+
141
+ if (!isset ($ firstPage ['id ' ],
142
+ $ firstPage ['type ' ],
143
+ $ firstPage ['title ' ],
144
+ $ firstPage ['_links ' ]['self ' ],
145
+ $ firstPage ['space ' ]['key ' ],
146
+ $ firstPage ['version ' ]['number ' ])
147
+ ) {
148
+ return null ;
149
+ }
150
+
151
+ $ page = new ConfluencePage ();
152
+ $ page ->setId ((int )$ firstPage ['id ' ]);
153
+ $ page ->setType ((string )$ firstPage ['type ' ]);
154
+ $ page ->setTitle ((string )$ firstPage ['title ' ]);
155
+ $ page ->setUrl ((string )$ firstPage ['_links ' ]['self ' ]);
156
+ $ page ->setSpace (str_replace ('/rest/api/space/ ' , '' , (string )$ firstPage ['space ' ]['key ' ]));
157
+ $ page ->setVersion ((int )$ firstPage ['version ' ]['number ' ]);
158
+ return $ page ;
123
159
}
124
160
125
161
/**
@@ -140,7 +176,7 @@ public function uploadAttachment(string $path, string $parentPageId)
140
176
];
141
177
return $ this ->request (
142
178
'POST ' ,
143
- $ this ->curl ->getHost () . "/content/ $ parentPageId/child/attachment " ,
179
+ $ this ->curl ->getHost () . "/rest/api/ content/ $ parentPageId/child/attachment " ,
144
180
$ data ,
145
181
$ headers
146
182
);
@@ -155,7 +191,7 @@ public function uploadAttachment(string $path, string $parentPageId)
155
191
*/
156
192
public function selectAttachments (string $ pageId )
157
193
{
158
- return $ this ->request ('GET ' , $ this ->curl ->getHost () . "/content/ $ pageId/child/attachment " );
194
+ return $ this ->request ('GET ' , $ this ->curl ->getHost () . "/rest/api/ content/ $ pageId/child/attachment " );
159
195
}
160
196
161
197
/**
@@ -166,7 +202,7 @@ public function selectAttachments(string $pageId)
166
202
*/
167
203
public function addLabel (string $ pageId , array $ labels )
168
204
{
169
- return $ this ->request ('POST ' , $ this ->curl ->getHost () . "/content/ $ pageId/label " , $ labels );
205
+ return $ this ->request ('POST ' , $ this ->curl ->getHost () . "/rest/api/ content/ $ pageId/label " , $ labels );
170
206
}
171
207
172
208
/**
@@ -177,11 +213,11 @@ public function addLabel(string $pageId, array $labels)
177
213
* @param mixed[] $data
178
214
* @param array<string, string> $headers
179
215
*
180
- * @return string|false
216
+ * @return mixed[]|null
181
217
*
182
218
* @throws Exception
183
219
*/
184
- public function request (string $ method , string $ url , array $ data = [], array $ headers = ['Content-Type ' => 'application/json ' ])
220
+ public function request (string $ method , string $ url , array $ data = [], array $ headers = ['Content-Type ' => 'application/json ' ]): ? array
185
221
{
186
222
//Detect invalid method
187
223
$ method = strtoupper ($ method );
@@ -196,16 +232,25 @@ public function request(string $method, string $url, array $data = [], array $he
196
232
])->setHeaders ($ headers );
197
233
198
234
if ($ data !== []) {
199
- $ this ->curl ->setOption (CURLOPT_POSTFIELDS , $ data );
235
+ $ this ->curl ->setOption (CURLOPT_POSTFIELDS , json_encode ( $ data, JSON_THROW_ON_ERROR ) );
200
236
}
201
237
202
238
$ serverOutput = $ this ->curl ->execute ();
203
- $ this ->curl ->close ();
204
239
205
- if (!is_scalar ( $ serverOutput ) && ! is_array ($ serverOutput )) {
240
+ if (!is_string ($ serverOutput )) {
206
241
throw new InvalidArgumentException ('Unexpected return value ' );
207
242
}
208
243
209
- return json_encode ($ serverOutput , JSON_THROW_ON_ERROR );
244
+ if (empty ($ serverOutput )) {
245
+ return null ;
246
+ }
247
+
248
+ $ decodedData = json_decode ($ serverOutput , true , 512 , JSON_THROW_ON_ERROR );
249
+
250
+ if (!is_array ($ decodedData )) {
251
+ throw new InvalidArgumentException ('Return value could not be decoded. ' );
252
+ }
253
+
254
+ return $ decodedData ;
210
255
}
211
256
}
0 commit comments