|
1
|
|
|
<?php declare( strict_types=1 ); |
|
2
|
|
|
|
|
3
|
|
|
namespace BotRiconferme\Request; |
|
4
|
|
|
|
|
5
|
|
|
use BadMethodCallException; |
|
6
|
|
|
use BotRiconferme\Exception\APIRequestException; |
|
7
|
|
|
use BotRiconferme\Exception\BlockedException; |
|
8
|
|
|
use BotRiconferme\Exception\MissingPageException; |
|
9
|
|
|
use BotRiconferme\Exception\PermissionDeniedException; |
|
10
|
|
|
use BotRiconferme\Exception\ProtectedPageException; |
|
11
|
|
|
use BotRiconferme\Exception\TimeoutException; |
|
12
|
|
|
use Generator; |
|
13
|
|
|
use Psr\Log\LoggerInterface; |
|
14
|
|
|
use stdClass; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Core wrapper for an API request. Current implementations use either cURL or file_get_contents |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class RequestBase { |
|
20
|
|
|
protected const USER_AGENT = 'Daimona - BotRiconferme ' . BOT_VERSION . |
|
21
|
|
|
' (https://github.com/Daimona/BotRiconferme)'; |
|
22
|
|
|
protected const HEADERS = [ |
|
23
|
|
|
'Content-Type: application/x-www-form-urlencoded', |
|
24
|
|
|
'User-Agent: ' . self::USER_AGENT |
|
25
|
|
|
]; |
|
26
|
|
|
// In seconds |
|
27
|
|
|
protected const MAXLAG = 5; |
|
28
|
|
|
|
|
29
|
|
|
protected const METHOD_GET = 'GET'; |
|
30
|
|
|
protected const METHOD_POST = 'POST'; |
|
31
|
|
|
|
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
protected $url; |
|
34
|
|
|
/** @var string[] */ |
|
35
|
|
|
protected $cookiesToSet; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
* @phan-var array<int|string|bool> |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $params; |
|
41
|
|
|
/** @var string */ |
|
42
|
|
|
protected $method = self::METHOD_GET; |
|
43
|
|
|
/** @var string[] */ |
|
44
|
|
|
protected $newCookies = []; |
|
45
|
|
|
/** @var callable|null */ |
|
46
|
|
|
private $cookiesHandlerCallback; |
|
47
|
|
|
|
|
48
|
|
|
/** @var LoggerInterface */ |
|
49
|
|
|
protected $logger; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @private Use RequestFactory |
|
53
|
|
|
* |
|
54
|
|
|
* @param LoggerInterface $logger |
|
55
|
|
|
* @param array $params |
|
56
|
|
|
* @phan-param array<int|string|bool> $params |
|
57
|
|
|
* @param string $domain |
|
58
|
|
|
* @param callable $cookiesHandlerCallback |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct( |
|
61
|
|
|
LoggerInterface $logger, |
|
62
|
|
|
array $params, |
|
63
|
|
|
string $domain, |
|
64
|
|
|
callable $cookiesHandlerCallback |
|
65
|
|
|
) { |
|
66
|
|
|
$this->logger = $logger; |
|
67
|
|
|
$this->params = [ 'format' => 'json' ] + $params; |
|
68
|
|
|
$this->url = $domain; |
|
69
|
|
|
$this->cookiesHandlerCallback = $cookiesHandlerCallback; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set the method to POST |
|
74
|
|
|
* |
|
75
|
|
|
* @return self For chaining |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setPost(): self { |
|
78
|
|
|
$this->method = self::METHOD_POST; |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param array $cookies |
|
84
|
|
|
* @return self For chaining |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setCookies( array $cookies ): self { |
|
87
|
|
|
$this->cookiesToSet = $cookies; |
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Execute a query request |
|
93
|
|
|
* @return Generator |
|
94
|
|
|
*/ |
|
95
|
|
|
public function executeAsQuery(): Generator { |
|
96
|
|
|
if ( ( $this->params['action'] ?? false ) !== 'query' ) { |
|
97
|
|
|
throw new BadMethodCallException( 'Not an ApiQuery!' ); |
|
98
|
|
|
} |
|
99
|
|
|
// TODO Is this always correct? |
|
100
|
|
|
$key = $this->params['list'] ?? 'pages'; |
|
101
|
|
|
$curParams = $this->params; |
|
102
|
|
|
$lim = $this->parseLimit(); |
|
103
|
|
|
do { |
|
104
|
|
|
$res = $this->makeRequestInternal( $curParams ); |
|
105
|
|
|
$this->handleErrorAndWarnings( $res ); |
|
106
|
|
|
yield from $key === 'pages' ? get_object_vars( $res->query->pages ) : $res->query->$key; |
|
107
|
|
|
|
|
108
|
|
|
// Assume that we have finished |
|
109
|
|
|
$finished = true; |
|
110
|
|
|
if ( isset( $res->continue ) ) { |
|
111
|
|
|
// This may indicate that we're not done... |
|
112
|
|
|
$curParams = get_object_vars( $res->continue ) + $curParams; |
|
113
|
|
|
$finished = false; |
|
114
|
|
|
} |
|
115
|
|
|
if ( $lim !== -1 ) { |
|
116
|
|
|
$count = $this->countQueryResults( $res, $key ); |
|
117
|
|
|
if ( $count !== null && $count >= $lim ) { |
|
118
|
|
|
// Unless we're able to use a limit, and that limit was passed. |
|
119
|
|
|
$finished = true; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} while ( !$finished ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Execute a request that doesn't need any continuation. |
|
127
|
|
|
* @return stdClass |
|
128
|
|
|
*/ |
|
129
|
|
|
public function executeSingle(): stdClass { |
|
130
|
|
|
$curParams = $this->params; |
|
131
|
|
|
$res = $this->makeRequestInternal( $curParams ); |
|
132
|
|
|
$this->handleErrorAndWarnings( $res ); |
|
133
|
|
|
return $res; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return int |
|
138
|
|
|
*/ |
|
139
|
|
|
private function parseLimit(): int { |
|
140
|
|
|
foreach ( $this->params as $name => $val ) { |
|
141
|
|
|
if ( substr( $name, -strlen( 'limit' ) ) === 'limit' ) { |
|
142
|
|
|
return $val === 'max' ? -1 : (int)$val; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
// Assume no limit |
|
146
|
|
|
return -1; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Try to count the amount of entries in a result. |
|
151
|
|
|
* |
|
152
|
|
|
* @param stdClass $res |
|
153
|
|
|
* @param string $resKey |
|
154
|
|
|
* @return int|null |
|
155
|
|
|
*/ |
|
156
|
|
|
private function countQueryResults( stdClass $res, string $resKey ): ?int { |
|
157
|
|
|
if ( !isset( $res->query->$resKey ) ) { |
|
158
|
|
|
return null; |
|
159
|
|
|
} |
|
160
|
|
|
if ( $resKey === 'pages' ) { |
|
161
|
|
|
if ( count( get_object_vars( $res->query->pages ) ) !== 1 ) { |
|
162
|
|
|
return null; |
|
163
|
|
|
} |
|
164
|
|
|
$pages = $res->query->pages; |
|
165
|
|
|
$firstPage = reset( $pages ); |
|
166
|
|
|
// TODO Avoid special-casing this. |
|
167
|
|
|
if ( !isset( $firstPage->revisions ) ) { |
|
168
|
|
|
return null; |
|
169
|
|
|
} |
|
170
|
|
|
$actualList = $firstPage->revisions; |
|
171
|
|
|
} else { |
|
172
|
|
|
$actualList = $res->query->$resKey; |
|
173
|
|
|
} |
|
174
|
|
|
return count( $actualList ); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Process parameters and call the actual request method |
|
179
|
|
|
* |
|
180
|
|
|
* @param array $params |
|
181
|
|
|
* @phan-param array<int|string|bool> $params |
|
182
|
|
|
* @return stdClass |
|
183
|
|
|
*/ |
|
184
|
|
|
private function makeRequestInternal( array $params ): stdClass { |
|
185
|
|
|
if ( $this->method === self::METHOD_POST ) { |
|
186
|
|
|
$params['maxlag'] = self::MAXLAG; |
|
187
|
|
|
} |
|
188
|
|
|
if ( !isset( $params['assert'] ) ) { |
|
189
|
|
|
$params['assert'] = 'user'; |
|
190
|
|
|
} |
|
191
|
|
|
$query = http_build_query( $params ); |
|
192
|
|
|
|
|
193
|
|
|
try { |
|
194
|
|
|
$body = $this->reallyMakeRequest( $query ); |
|
195
|
|
|
} catch ( TimeoutException $_ ) { |
|
196
|
|
|
$this->logger->warning( 'Retrying request after timeout' ); |
|
197
|
|
|
$body = $this->reallyMakeRequest( $query ); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
( $this->cookiesHandlerCallback )( $this->newCookies ); |
|
201
|
|
|
return json_decode( $body ); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Parses the new cookies and saves them for later retrieval. |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $cookie "{key}={value}" |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function saveNewCookie( string $cookie ): void { |
|
210
|
|
|
$bits = explode( ';', $cookie ); |
|
211
|
|
|
[ $name, $value ] = explode( '=', $bits[0] ); |
|
212
|
|
|
$this->newCookies[$name] = $value; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Actual method which will make the request |
|
217
|
|
|
* |
|
218
|
|
|
* @param string $params |
|
219
|
|
|
* @return string |
|
220
|
|
|
*/ |
|
221
|
|
|
abstract protected function reallyMakeRequest( string $params ): string; |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get a specific exception class depending on the error code |
|
225
|
|
|
* |
|
226
|
|
|
* @param stdClass $res |
|
227
|
|
|
* @return APIRequestException |
|
228
|
|
|
*/ |
|
229
|
|
|
private function getException( stdClass $res ): APIRequestException { |
|
230
|
|
|
switch ( $res->error->code ) { |
|
231
|
|
|
case 'missingtitle': |
|
232
|
|
|
$ex = new MissingPageException; |
|
233
|
|
|
break; |
|
234
|
|
|
case 'protectedpage': |
|
235
|
|
|
$ex = new ProtectedPageException; |
|
236
|
|
|
break; |
|
237
|
|
|
case 'permissiondenied': |
|
238
|
|
|
$ex = new PermissionDeniedException( $res->error->info ); |
|
239
|
|
|
break; |
|
240
|
|
|
case 'blocked': |
|
241
|
|
|
$ex = new BlockedException( $res->error->info ); |
|
242
|
|
|
break; |
|
243
|
|
|
default: |
|
244
|
|
|
$ex = new APIRequestException( $res->error->code . ' - ' . $res->error->info ); |
|
245
|
|
|
} |
|
246
|
|
|
return $ex; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Handle known warning and errors from an API request |
|
251
|
|
|
* |
|
252
|
|
|
* @param stdClass $res |
|
253
|
|
|
* @throws APIRequestException |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function handleErrorAndWarnings( stdClass $res ): void { |
|
256
|
|
|
if ( isset( $res->error ) ) { |
|
257
|
|
|
throw $this->getException( $res ); |
|
258
|
|
|
} |
|
259
|
|
|
if ( isset( $res->warnings ) ) { |
|
260
|
|
|
$act = $this->params[ 'action' ]; |
|
261
|
|
|
$warning = $res->warnings->$act ?? $res->warnings->main; |
|
262
|
|
|
throw new APIRequestException( reset( $warning ) ); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Get the headers to use for a new request |
|
268
|
|
|
* |
|
269
|
|
|
* @return string[] |
|
270
|
|
|
*/ |
|
271
|
|
|
protected function getHeaders(): array { |
|
272
|
|
|
$ret = self::HEADERS; |
|
273
|
|
|
if ( $this->cookiesToSet ) { |
|
274
|
|
|
$cookies = []; |
|
275
|
|
|
foreach ( $this->cookiesToSet as $cname => $cval ) { |
|
276
|
|
|
$cookies[] = trim( "$cname=$cval" ); |
|
277
|
|
|
} |
|
278
|
|
|
$ret[] = 'Cookie: ' . implode( '; ', $cookies ); |
|
279
|
|
|
} |
|
280
|
|
|
return $ret; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Utility function to implode headers |
|
285
|
|
|
* |
|
286
|
|
|
* @param string[] $headers |
|
287
|
|
|
* @return string |
|
288
|
|
|
*/ |
|
289
|
|
|
protected function buildHeadersString( array $headers ): string { |
|
290
|
|
|
$ret = ''; |
|
291
|
|
|
foreach ( $headers as $header ) { |
|
292
|
|
|
$ret .= "$header\r\n"; |
|
293
|
|
|
} |
|
294
|
|
|
return $ret; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @param string $actualParams |
|
299
|
|
|
* @return string |
|
300
|
|
|
*/ |
|
301
|
|
|
protected function getDebugURL( string $actualParams ): string { |
|
302
|
|
|
return strpos( $this->url, 'login' ) !== false |
|
303
|
|
|
? '[Login request]' |
|
304
|
|
|
: "{$this->url}?$actualParams"; |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|