1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Gerrie package. |
4
|
|
|
* |
5
|
|
|
* (c) Andreas Grunwald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Gerrie\API\DataService; |
12
|
|
|
|
13
|
|
|
class HTTPDataService extends BaseDataService |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor |
18
|
|
|
* |
19
|
|
|
* @param \Buzz\Browser $connector |
20
|
|
|
* @param array $config |
21
|
|
|
* @return \Gerrie\API\DataService\HTTPDataService |
|
|
|
|
22
|
|
|
*/ |
23
|
9 |
|
public function __construct(\Buzz\Browser $connector, array $config) |
24
|
|
|
{ |
25
|
9 |
|
$this->setConnector($connector); |
26
|
9 |
|
$this->setConfig($config); |
27
|
9 |
|
$this->setName('HTTP'); |
28
|
9 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets the base url for all HTTP requests. |
32
|
|
|
* |
33
|
|
|
* @param bool $withAuthentication If true, the authentification string will be appended. False otherwise |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
protected function getBaseUrl($withAuthentication = false) |
37
|
|
|
{ |
38
|
|
|
$config = $this->getConfig(); |
39
|
|
|
$baseUrl = $config['scheme'] . '://' . rtrim($config['host'], '/') . '/'; |
40
|
|
|
|
41
|
|
|
if (isset($config['path'])) { |
42
|
|
|
$baseUrl .= trim($config['path'], '/') . '/'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($withAuthentication === true |
46
|
|
|
&& $this->getConnector()->getListener() instanceof \Buzz\Listener\BasicAuthListener) { |
47
|
|
|
|
48
|
|
|
$baseUrl .= 'a/'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $baseUrl; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Verifies the last request. |
56
|
|
|
* If the last request was not successful, it will be throw an exception. |
57
|
|
|
* |
58
|
|
|
* @param \Buzz\Message\Response $response The response object from the last reques |
59
|
|
|
* @param string $url The url which was requested |
60
|
|
|
* @return \Buzz\Message\Response |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
|
|
protected function verifyResult(\Buzz\Message\Response $response, $url) |
64
|
|
|
{ |
65
|
|
|
if ($response->getStatusCode() !== 200) { |
66
|
|
|
throw new \Exception('Request to "' . $url . '" failed', 1364061673); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Transforms a JSON string into an array. |
74
|
|
|
* Regular, the json is the content from the response. |
75
|
|
|
* |
76
|
|
|
* @param string $json The json string |
77
|
|
|
* @return array|null |
78
|
|
|
*/ |
79
|
2 |
|
public function transformJsonResponse($json) |
80
|
|
|
{ |
81
|
|
|
// In a REST-API call, the first five chars are )]}'\n |
82
|
|
|
// to decode it, we have to strip it |
83
|
|
|
// See https://review.typo3.org/Documentation/rest-api.html#output |
84
|
2 |
|
if (substr($json, 0, 4) === ')]}\'') { |
85
|
|
|
$json = substr($json, 5); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return json_decode($json, true); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Initiales the query limit |
93
|
|
|
* |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
protected function initQueryLimit() |
97
|
|
|
{ |
98
|
|
|
$url = $this->getBaseUrl(true) . 'accounts/self/capabilities?format=JSON'; |
99
|
|
|
$response = $this->getConnector()->get($url); |
100
|
|
|
$response = $this->verifyResult($response, $url); |
101
|
|
|
|
102
|
|
|
$content = $this->transformJsonResponse($response->getContent()); |
103
|
|
|
|
104
|
|
|
return $content['queryLimit']['max']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Requests projects at the Gerrit server |
109
|
|
|
* |
110
|
|
|
* @return array|null |
111
|
|
|
*/ |
112
|
|
|
public function getProjects() |
113
|
|
|
{ |
114
|
|
|
$urlParts = array( |
115
|
|
|
'format' => 'JSON', |
116
|
|
|
'description' => '', |
117
|
|
|
'type' => 'all', |
118
|
|
|
'all' => '', |
119
|
|
|
'tree' => '', |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$url = $this->getBaseUrl() . 'projects/?' . http_build_query($urlParts); |
123
|
|
|
|
124
|
|
|
$response = $this->getConnector()->get($url); |
125
|
|
|
$response = $this->verifyResult($response, $url); |
126
|
|
|
|
127
|
|
|
$content = $this->transformJsonResponse($response->getContent()); |
128
|
|
|
|
129
|
|
|
return $content; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Requests changesets at the Gerrit server. |
134
|
|
|
* |
135
|
|
|
* This method is not implemented yet, because at the moment (2013-03-24) Gerrit 2.6.* is not released. |
136
|
|
|
* Many Gerrit systems (e.g. TYPO3, WikiMedia, OpenStack, etc.) are running at 2.5.*. |
137
|
|
|
* In 2.5.* the SSH API delivers more information than the REST API. |
138
|
|
|
* |
139
|
|
|
* If Gerrit 2.6 is released, the HTTP DataService will be extended and fully implemented. |
140
|
|
|
* Maybe, you want to help me? |
141
|
|
|
* |
142
|
|
|
* SSH commands: |
143
|
|
|
* /usr/bin/ssh -p 29418 review.typo3.org gerrit query --format 'JSON' --current-patch-set |
144
|
|
|
* --all-approvals --files --comments |
145
|
|
|
* --commit-message --dependencies --submit-records |
146
|
|
|
* 'project:Documentation/ApiTypo3Org' limit:'500' 2>&1 |
147
|
|
|
* /usr/bin/ssh -p 29418 review.typo3.org gerrit query --format 'JSON' --current-patch-set |
148
|
|
|
* --all-approvals --files --comments |
149
|
|
|
* --commit-message --dependencies --submit-records |
150
|
|
|
* 'project:Documentation/ApiTypo3Org' limit:'500' |
151
|
|
|
* resume_sortkey:'00215ec7000041b3' 2>&1 |
152
|
|
|
* |
153
|
|
|
* @param string $projectName The project name |
154
|
|
|
* @param string $resumeKey The key where the request will be resumed |
155
|
|
|
* @param integer $start Number of changesets which will be skipped |
156
|
|
|
* @return array|null |
157
|
|
|
* @throws \Exception |
158
|
|
|
*/ |
159
|
|
|
public function getChangesets($projectName, $resumeKey = null, $start = 0) |
160
|
|
|
{ |
161
|
|
|
throw new \Exception(__METHOD__ . ' not implemented yet. Will you help me?', 1374257295); |
162
|
|
|
|
163
|
|
|
$urlParts = array( |
|
|
|
|
164
|
|
|
'q' => sprintf('project:%s', $projectName), |
165
|
|
|
'n' => $this->getQueryLimit() |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
// The "o" parameter can be applied more than one time |
169
|
|
|
// This parameter defines how detailed the answer will be |
170
|
|
|
$oOptions = array( |
171
|
|
|
'LABELS', |
172
|
|
|
'DETAILED_LABELS', |
173
|
|
|
'CURRENT_REVISION', |
174
|
|
|
'ALL_REVISIONS', |
175
|
|
|
'CURRENT_COMMIT', |
176
|
|
|
'ALL_COMMITS', |
177
|
|
|
'CURRENT_FILES', |
178
|
|
|
'ALL_FILES', |
179
|
|
|
'DETAILED_ACCOUNTS', |
180
|
|
|
'MESSAGES' |
181
|
|
|
); |
182
|
|
|
$additionalFields = $this->buildQueryStringWithSameParameterName('o', $oOptions); |
183
|
|
|
$url = $this->getBaseUrl() . 'changes/?' . http_build_query($urlParts) . '&' . $additionalFields; |
184
|
|
|
|
185
|
|
|
$response = $this->getConnector()->get($url); |
186
|
|
|
$response = $this->verifyResult($response, $url); |
187
|
|
|
|
188
|
|
|
$content = $this->transformJsonResponse($response->getContent()); |
189
|
|
|
|
190
|
|
|
return $content; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* This function build a url query string with a parameter which can be applied more than one time. |
195
|
|
|
* E.g. http://www.google.de/?q=5&q=6&q=7&q=8&q=9... |
196
|
|
|
* |
197
|
|
|
* This method is used to apply the parameter "o" in GET /changes/ command for REST-API. |
198
|
|
|
* |
199
|
|
|
* @see https://review.typo3.org/Documentation/rest-api-changes.html#list-changes |
200
|
|
|
* |
201
|
|
|
* @param string $parameterName Parametername which should be used more than one time |
202
|
|
|
* @param array $values Various of values |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
private function buildQueryStringWithSameParameterName($parameterName, array $values) |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
$queryString = ''; |
208
|
|
|
|
209
|
|
|
foreach ($values as $value) { |
210
|
|
|
if ($queryString) { |
211
|
|
|
$queryString .= '&'; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$queryString .= http_build_query(array($parameterName => $value)); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $queryString; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Returns the version of the Gerrit server |
222
|
|
|
* |
223
|
|
|
* @link https://review.typo3.org/Documentation/rest-api-config.html |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function getVersion() |
228
|
|
|
{ |
229
|
|
|
$url = $this->getBaseUrl() . 'config/server/version'; |
230
|
|
|
$response = $this->getConnector()->get($url); |
231
|
|
|
$response = $this->verifyResult($response, $url); |
232
|
|
|
|
233
|
|
|
$content = $this->transformJsonResponse($response->getContent()); |
|
|
|
|
234
|
|
|
|
235
|
|
|
return $content; |
236
|
|
|
} |
237
|
|
|
} |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.