Complex classes like MediawikiApi often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MediawikiApi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class MediawikiApi implements MediawikiApiInterface, LoggerAwareInterface { |
||
28 | |||
29 | /** |
||
30 | * @var ClientInterface|null Should be accessed through getClient |
||
31 | */ |
||
32 | private $client = null; |
||
33 | |||
34 | /** |
||
35 | * @var bool|string |
||
36 | */ |
||
37 | private $isLoggedIn; |
||
38 | |||
39 | /** |
||
40 | * @var MediawikiSession |
||
41 | */ |
||
42 | private $session; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $version; |
||
48 | |||
49 | /** |
||
50 | * @var LoggerInterface |
||
51 | */ |
||
52 | private $logger; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private $apiUrl; |
||
58 | |||
59 | /** |
||
60 | * @since 2.0 |
||
61 | * |
||
62 | * @param string $apiEndpoint e.g. https://en.wikipedia.org/w/api.php |
||
63 | * |
||
64 | * @return self returns a MediawikiApi instance using $apiEndpoint |
||
65 | */ |
||
66 | public static function newFromApiEndpoint( $apiEndpoint ) { |
||
69 | |||
70 | /** |
||
71 | * Create a new MediawikiApi object from a URL to any page in a MediaWiki website. |
||
72 | * |
||
73 | * @since 2.0 |
||
74 | * @see https://en.wikipedia.org/wiki/Really_Simple_Discovery |
||
75 | * |
||
76 | * @param string $url e.g. https://en.wikipedia.org OR https://de.wikipedia.org/wiki/Berlin |
||
77 | * @return self returns a MediawikiApi instance using the apiEndpoint provided by the RSD |
||
78 | * file accessible on all Mediawiki pages |
||
79 | * @throws RsdException If the RSD URL could not be found in the page's HTML. |
||
80 | */ |
||
81 | 3 | public static function newFromPage( $url ) { |
|
113 | |||
114 | /** |
||
115 | * @param string $apiUrl The API Url |
||
116 | * @param ClientInterface|null $client Guzzle Client |
||
117 | * @param MediawikiSession|null $session Inject a custom session here |
||
118 | */ |
||
119 | 24 | public function __construct( $apiUrl, ClientInterface $client = null, |
|
134 | |||
135 | /** |
||
136 | * Get the API URL (the URL to which API requests are sent, usually ending in api.php). |
||
137 | * This is useful if you've created this object via MediawikiApi::newFromPage(). |
||
138 | * |
||
139 | * @since 2.3 |
||
140 | * |
||
141 | * @return string The API URL. |
||
142 | */ |
||
143 | public function getApiUrl() { |
||
146 | |||
147 | /** |
||
148 | * @return ClientInterface |
||
149 | */ |
||
150 | 21 | private function getClient() { |
|
158 | |||
159 | /** |
||
160 | * Sets a logger instance on the object |
||
161 | * |
||
162 | * @since 1.1 |
||
163 | * |
||
164 | * @param LoggerInterface $logger |
||
165 | * |
||
166 | * @return null |
||
167 | */ |
||
168 | public function setLogger( LoggerInterface $logger ) { |
||
172 | |||
173 | /** |
||
174 | * @since 2.0 |
||
175 | * |
||
176 | * @param Request $request |
||
177 | * |
||
178 | * @return PromiseInterface |
||
179 | * Normally promising an array, though can be mixed (json_decode result) |
||
180 | * Can throw UsageExceptions or RejectionExceptions |
||
181 | */ |
||
182 | 1 | public function getRequestAsync( Request $request ) { |
|
193 | |||
194 | /** |
||
195 | * @since 2.0 |
||
196 | * |
||
197 | * @param Request $request |
||
198 | * |
||
199 | * @return PromiseInterface |
||
200 | * Normally promising an array, though can be mixed (json_decode result) |
||
201 | * Can throw UsageExceptions or RejectionExceptions |
||
202 | */ |
||
203 | 1 | public function postRequestAsync( Request $request ) { |
|
214 | |||
215 | /** |
||
216 | * @since 0.2 |
||
217 | * |
||
218 | * @param Request $request |
||
219 | * |
||
220 | * @return mixed Normally an array |
||
221 | */ |
||
222 | 9 | public function getRequest( Request $request ) { |
|
231 | |||
232 | /** |
||
233 | * @since 0.2 |
||
234 | * |
||
235 | * @param Request $request |
||
236 | * |
||
237 | * @return mixed Normally an array |
||
238 | */ |
||
239 | 10 | public function postRequest( Request $request ) { |
|
248 | |||
249 | /** |
||
250 | * @param ResponseInterface $response |
||
251 | * |
||
252 | * @return mixed |
||
253 | * @throws UsageException |
||
254 | */ |
||
255 | 21 | private function decodeResponse( ResponseInterface $response ) { |
|
263 | |||
264 | /** |
||
265 | * @param Request $request |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | 9 | private function getPostRequestEncoding( Request $request ) { |
|
270 | 9 | if ( $request instanceof MultipartRequest ) { |
|
271 | return 'multipart'; |
||
272 | } |
||
273 | 9 | foreach ( $request->getParams() as $value ) { |
|
274 | 9 | if ( is_resource( $value ) ) { |
|
275 | 1 | return 'multipart'; |
|
276 | } |
||
277 | 9 | } |
|
278 | 8 | return 'form_params'; |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * @param Request $request |
||
283 | * @param string $paramsKey either 'query' or 'multipart' |
||
284 | * |
||
285 | * @throws RequestException |
||
286 | * |
||
287 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
288 | */ |
||
289 | 21 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
290 | |||
291 | 21 | $params = array_merge( $request->getParams(), [ 'format' => 'json' ] ); |
|
292 | 21 | if ( $paramsKey === 'multipart' ) { |
|
293 | 1 | $params = $this->encodeMultipartParams( $request, $params ); |
|
294 | 1 | } |
|
295 | |||
296 | return [ |
||
297 | 21 | $paramsKey => $params, |
|
298 | 21 | 'headers' => array_merge( $this->getDefaultHeaders(), $request->getHeaders() ), |
|
299 | 21 | ]; |
|
300 | } |
||
301 | |||
302 | /** |
||
303 | * Turn the normal key-value array of request parameters into a multipart array where each |
||
304 | * parameter is a new array with a 'name' and 'contents' elements (and optionally more, if the |
||
305 | * request is a MultipartRequest). |
||
306 | * |
||
307 | * @param Request $request The request to which the parameters belong. |
||
308 | * @param string[] $params The existing parameters. Not the same as $request->getParams(). |
||
309 | * |
||
310 | * @return array |
||
311 | */ |
||
312 | 1 | private function encodeMultipartParams( Request $request, $params ) { |
|
313 | // See if there are any multipart parameters in this request. |
||
314 | 1 | $multipartParams = ( $request instanceof MultipartRequest ) |
|
315 | 1 | ? $request->getMultipartParams() |
|
316 | 1 | : []; |
|
317 | 1 | return array_map( |
|
318 | 1 | function ( $name, $value ) use ( $multipartParams ) { |
|
319 | $partParams = [ |
||
320 | 1 | 'name' => $name, |
|
321 | 1 | 'contents' => $value, |
|
322 | 1 | ]; |
|
323 | 1 | if ( isset( $multipartParams[ $name ] ) ) { |
|
324 | // If extra parameters have been set for this part, use them. |
||
325 | $partParams = array_merge( $multipartParams[ $name ], $partParams ); |
||
326 | } |
||
327 | 1 | return $partParams; |
|
328 | 1 | }, |
|
329 | 1 | array_keys( $params ), |
|
330 | $params |
||
331 | 1 | ); |
|
332 | } |
||
333 | |||
334 | /** |
||
335 | * @return array |
||
336 | */ |
||
337 | 17 | private function getDefaultHeaders() { |
|
338 | return [ |
||
339 | 17 | 'User-Agent' => $this->getUserAgent(), |
|
340 | 17 | ]; |
|
341 | } |
||
342 | |||
343 | 17 | private function getUserAgent() { |
|
350 | |||
351 | /** |
||
352 | * @param $result |
||
353 | */ |
||
354 | 17 | private function logWarnings( $result ) { |
|
355 | 17 | if ( is_array( $result ) && array_key_exists( 'warnings', $result ) ) { |
|
356 | foreach ( $result['warnings'] as $module => $warningData ) { |
||
357 | // Accomodate both formatversion=2 and old-style API results |
||
358 | $logPrefix = $module . ': '; |
||
359 | if ( isset( $warningData['*'] ) ) { |
||
360 | $this->logger->warning( $logPrefix . $warningData['*'], [ 'data' => $warningData ] ); |
||
361 | } else { |
||
362 | $this->logger->warning( $logPrefix . $warningData['warnings'], [ 'data' => $warningData ] ); |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | 17 | } |
|
367 | |||
368 | /** |
||
369 | * @param array $result |
||
370 | * |
||
371 | * @throws UsageException |
||
372 | */ |
||
373 | 17 | private function throwUsageExceptions( $result ) { |
|
382 | |||
383 | /** |
||
384 | * @since 0.1 |
||
385 | * |
||
386 | * @return bool|string false or the name of the current user |
||
387 | */ |
||
388 | 17 | public function isLoggedin() { |
|
391 | |||
392 | /** |
||
393 | * @since 0.1 |
||
394 | * |
||
395 | * @param ApiUser $apiUser |
||
396 | * |
||
397 | * @throws UsageException |
||
398 | * @return bool success |
||
399 | */ |
||
400 | 2 | public function login( ApiUser $apiUser ) { |
|
401 | 2 | $this->logger->log( LogLevel::DEBUG, 'Logging in' ); |
|
402 | 2 | $credentials = $this->getLoginParams( $apiUser ); |
|
403 | 2 | $result = $this->postRequest( new SimpleRequest( 'login', $credentials ) ); |
|
404 | 2 | if ( $result['login']['result'] == "NeedToken" ) { |
|
405 | 2 | $params = array_merge( [ 'lgtoken' => $result['login']['token'] ], $credentials ); |
|
406 | 2 | $result = $this->postRequest( new SimpleRequest( 'login', $params ) ); |
|
407 | 2 | } |
|
408 | 2 | if ( $result['login']['result'] == "Success" ) { |
|
409 | 1 | $this->isLoggedIn = $apiUser->getUsername(); |
|
410 | 1 | return true; |
|
411 | } |
||
412 | |||
413 | 1 | $this->isLoggedIn = false; |
|
414 | 1 | $this->logger->log( LogLevel::DEBUG, 'Login failed.', $result ); |
|
415 | 1 | $this->throwLoginUsageException( $result ); |
|
416 | return false; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * @param ApiUser $apiUser |
||
421 | * |
||
422 | * @return string[] |
||
423 | */ |
||
424 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
425 | $params = [ |
||
426 | 2 | 'lgname' => $apiUser->getUsername(), |
|
427 | 2 | 'lgpassword' => $apiUser->getPassword(), |
|
428 | 2 | ]; |
|
429 | |||
430 | 2 | if ( !is_null( $apiUser->getDomain() ) ) { |
|
431 | $params['lgdomain'] = $apiUser->getDomain(); |
||
432 | } |
||
433 | 2 | return $params; |
|
434 | } |
||
435 | |||
436 | /** |
||
437 | * @param array $result |
||
438 | * |
||
439 | * @throws UsageException |
||
440 | */ |
||
441 | 1 | private function throwLoginUsageException( $result ) { |
|
442 | 1 | $loginResult = $result['login']['result']; |
|
443 | |||
444 | 1 | throw new UsageException( |
|
445 | 1 | 'login-' . $loginResult, |
|
446 | 1 | array_key_exists( 'reason', $result['login'] ) |
|
447 | 1 | ? $result['login']['reason'] |
|
448 | 1 | : 'No Reason given', |
|
449 | $result |
||
450 | 1 | ); |
|
451 | } |
||
452 | |||
453 | /** |
||
454 | * @since 0.1 |
||
455 | * |
||
456 | * @return bool success |
||
457 | */ |
||
458 | 2 | public function logout() { |
|
468 | |||
469 | /** |
||
470 | * @since 0.1 |
||
471 | * |
||
472 | * @param string $type |
||
473 | * |
||
474 | * @return string |
||
475 | */ |
||
476 | 2 | public function getToken( $type = 'csrf' ) { |
|
479 | |||
480 | /** |
||
481 | * @since 0.1 |
||
482 | * |
||
483 | * Clears all tokens stored by the api |
||
484 | */ |
||
485 | 1 | public function clearTokens() { |
|
488 | |||
489 | /** |
||
490 | * @return string |
||
491 | */ |
||
492 | 4 | public function getVersion() { |
|
493 | 4 | if ( !isset( $this->version ) ) { |
|
494 | 4 | $result = $this->getRequest( new SimpleRequest( 'query', [ |
|
495 | 4 | 'meta' => 'siteinfo', |
|
496 | 4 | 'continue' => '', |
|
507 | |||
508 | } |
||
509 |