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 | 2 | public static function newFromPage( $url ) { |
|
98 | |||
99 | /** |
||
100 | * @param string $apiUrl The API Url |
||
101 | * @param ClientInterface|null $client Guzzle Client |
||
102 | * @param MediawikiSession|null $session Inject a custom session here |
||
103 | */ |
||
104 | 24 | public function __construct( $apiUrl, ClientInterface $client = null, |
|
105 | MediawikiSession $session = null ) { |
||
106 | 24 | if ( !is_string( $apiUrl ) ) { |
|
107 | 4 | throw new InvalidArgumentException( '$apiUrl must be a string' ); |
|
108 | } |
||
109 | 20 | if ( $session === null ) { |
|
110 | 20 | $session = new MediawikiSession( $this ); |
|
111 | } |
||
112 | |||
113 | 20 | $this->apiUrl = $apiUrl; |
|
114 | 20 | $this->client = $client; |
|
115 | 20 | $this->session = $session; |
|
116 | |||
117 | 20 | $this->logger = new NullLogger(); |
|
118 | 20 | } |
|
119 | |||
120 | /** |
||
121 | * Get the API URL (the URL to which API requests are sent, usually ending in api.php). |
||
122 | * This is useful if you've created this object via MediawikiApi::newFromPage(). |
||
123 | * |
||
124 | * @since 2.3 |
||
125 | * |
||
126 | * @return string The API URL. |
||
127 | */ |
||
128 | public function getApiUrl() { |
||
129 | return $this->apiUrl; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return ClientInterface |
||
134 | */ |
||
135 | 21 | private function getClient() { |
|
143 | |||
144 | /** |
||
145 | * Sets a logger instance on the object |
||
146 | * |
||
147 | * @since 1.1 |
||
148 | * |
||
149 | * @param LoggerInterface $logger |
||
150 | * |
||
151 | * @return null |
||
152 | */ |
||
153 | public function setLogger( LoggerInterface $logger ) { |
||
154 | $this->logger = $logger; |
||
155 | $this->session->setLogger( $logger ); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @since 2.0 |
||
160 | * |
||
161 | * @param Request $request |
||
162 | * |
||
163 | * @return PromiseInterface |
||
164 | * Normally promising an array, though can be mixed (json_decode result) |
||
165 | * Can throw UsageExceptions or RejectionExceptions |
||
166 | */ |
||
167 | 1 | public function getRequestAsync( Request $request ) { |
|
178 | |||
179 | /** |
||
180 | * @since 2.0 |
||
181 | * |
||
182 | * @param Request $request |
||
183 | * |
||
184 | * @return PromiseInterface |
||
185 | * Normally promising an array, though can be mixed (json_decode result) |
||
186 | * Can throw UsageExceptions or RejectionExceptions |
||
187 | */ |
||
188 | 1 | public function postRequestAsync( Request $request ) { |
|
199 | |||
200 | /** |
||
201 | * @since 0.2 |
||
202 | * |
||
203 | * @param Request $request |
||
204 | * |
||
205 | * @return mixed Normally an array |
||
206 | */ |
||
207 | 9 | public function getRequest( Request $request ) { |
|
216 | |||
217 | /** |
||
218 | * @since 0.2 |
||
219 | * |
||
220 | * @param Request $request |
||
221 | * |
||
222 | * @return mixed Normally an array |
||
223 | */ |
||
224 | 10 | public function postRequest( Request $request ) { |
|
233 | |||
234 | /** |
||
235 | * @param ResponseInterface $response |
||
236 | * |
||
237 | * @return mixed |
||
238 | * @throws UsageException |
||
239 | */ |
||
240 | 21 | private function decodeResponse( ResponseInterface $response ) { |
|
241 | 21 | $resultArray = json_decode( $response->getBody(), true ); |
|
242 | |||
243 | 21 | $this->logWarnings( $resultArray ); |
|
244 | 21 | $this->throwUsageExceptions( $resultArray ); |
|
245 | |||
246 | 19 | return $resultArray; |
|
247 | } |
||
248 | |||
249 | /** |
||
250 | * @param Request $request |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | 9 | private function getPostRequestEncoding( Request $request ) { |
|
262 | |||
263 | /** |
||
264 | * @param Request $request |
||
265 | * @param string $paramsKey either 'query' or 'multipart' |
||
266 | * |
||
267 | * @throws RequestException |
||
268 | * |
||
269 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
270 | */ |
||
271 | 21 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
272 | |||
273 | 21 | $params = array_merge( $request->getParams(), [ 'format' => 'json' ] ); |
|
274 | 21 | if ( $paramsKey === 'multipart' ) { |
|
275 | 1 | $params = $this->encodeMultipartParams( $params ); |
|
276 | } |
||
277 | |||
278 | return [ |
||
279 | 21 | $paramsKey => $params, |
|
280 | 21 | 'headers' => array_merge( $this->getDefaultHeaders(), $request->getHeaders() ), |
|
281 | ]; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param array $params |
||
286 | * |
||
287 | * @return array |
||
288 | */ |
||
289 | 1 | private function encodeMultipartParams( $params ) { |
|
290 | |||
291 | 1 | return array_map( |
|
292 | 1 | function ( $name, $value ) { |
|
293 | |||
294 | return [ |
||
295 | 1 | 'name' => $name, |
|
296 | 1 | 'contents' => $value, |
|
297 | ]; |
||
298 | 1 | }, |
|
299 | array_keys( $params ), |
||
300 | $params |
||
301 | ); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @return array |
||
306 | */ |
||
307 | 17 | private function getDefaultHeaders() { |
|
312 | |||
313 | 17 | private function getUserAgent() { |
|
314 | 17 | $loggedIn = $this->isLoggedin(); |
|
315 | 17 | if ( $loggedIn ) { |
|
320 | |||
321 | /** |
||
322 | * @param $result |
||
323 | */ |
||
324 | 17 | private function logWarnings( $result ) { |
|
325 | 17 | if ( is_array( $result ) && array_key_exists( 'warnings', $result ) ) { |
|
326 | foreach ( $result['warnings'] as $module => $warningData ) { |
||
327 | // Accomodate both formatversion=2 and old-style API results |
||
328 | $logPrefix = $module . ': '; |
||
329 | if ( isset( $warningData['*'] ) ) { |
||
330 | $this->logger->warning( $logPrefix . $warningData['*'], [ 'data' => $warningData ] ); |
||
331 | } else { |
||
332 | $this->logger->warning( $logPrefix . $warningData['warnings'], [ 'data' => $warningData ] ); |
||
333 | } |
||
334 | } |
||
335 | } |
||
336 | 17 | } |
|
337 | |||
338 | /** |
||
339 | * @param array $result |
||
340 | * |
||
341 | * @throws UsageException |
||
342 | */ |
||
343 | 17 | private function throwUsageExceptions( $result ) { |
|
344 | 17 | if ( is_array( $result ) && array_key_exists( 'error', $result ) ) { |
|
345 | 2 | throw new UsageException( |
|
346 | 2 | $result['error']['code'], |
|
347 | 2 | $result['error']['info'], |
|
348 | $result |
||
349 | ); |
||
350 | } |
||
351 | 15 | } |
|
352 | |||
353 | /** |
||
354 | * @since 0.1 |
||
355 | * |
||
356 | * @return bool|string false or the name of the current user |
||
357 | */ |
||
358 | 17 | public function isLoggedin() { |
|
361 | |||
362 | /** |
||
363 | * @since 0.1 |
||
364 | * |
||
365 | * @param ApiUser $apiUser |
||
366 | * |
||
367 | * @throws UsageException |
||
368 | * @return bool success |
||
369 | */ |
||
370 | 2 | public function login( ApiUser $apiUser ) { |
|
371 | 2 | $this->logger->log( LogLevel::DEBUG, 'Logging in' ); |
|
372 | 2 | $credentials = $this->getLoginParams( $apiUser ); |
|
373 | 2 | $result = $this->postRequest( new SimpleRequest( 'login', $credentials ) ); |
|
374 | 2 | if ( $result['login']['result'] == "NeedToken" ) { |
|
375 | 2 | $params = array_merge( [ 'lgtoken' => $result['login']['token'] ], $credentials ); |
|
376 | 2 | $result = $this->postRequest( new SimpleRequest( 'login', $params ) ); |
|
377 | } |
||
378 | 2 | if ( $result['login']['result'] == "Success" ) { |
|
379 | 1 | $this->isLoggedIn = $apiUser->getUsername(); |
|
380 | 1 | return true; |
|
381 | } |
||
382 | |||
383 | 1 | $this->isLoggedIn = false; |
|
384 | 1 | $this->logger->log( LogLevel::DEBUG, 'Login failed.', $result ); |
|
385 | 1 | $this->throwLoginUsageException( $result ); |
|
386 | return false; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param ApiUser $apiUser |
||
391 | * |
||
392 | * @return string[] |
||
393 | */ |
||
394 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
395 | $params = [ |
||
396 | 2 | 'lgname' => $apiUser->getUsername(), |
|
397 | 2 | 'lgpassword' => $apiUser->getPassword(), |
|
398 | ]; |
||
399 | |||
400 | 2 | if ( !is_null( $apiUser->getDomain() ) ) { |
|
401 | $params['lgdomain'] = $apiUser->getDomain(); |
||
402 | } |
||
403 | 2 | return $params; |
|
404 | } |
||
405 | |||
406 | /** |
||
407 | * @param array $result |
||
408 | * |
||
409 | * @throws UsageException |
||
410 | */ |
||
411 | 1 | private function throwLoginUsageException( $result ) { |
|
412 | 1 | $loginResult = $result['login']['result']; |
|
413 | |||
414 | 1 | throw new UsageException( |
|
415 | 1 | 'login-' . $loginResult, |
|
416 | 1 | array_key_exists( 'reason', $result['login'] ) |
|
417 | ? $result['login']['reason'] |
||
418 | 1 | : 'No Reason given', |
|
419 | $result |
||
420 | ); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * @since 0.1 |
||
425 | * |
||
426 | * @return bool success |
||
427 | */ |
||
428 | 2 | public function logout() { |
|
438 | |||
439 | /** |
||
440 | * @since 0.1 |
||
441 | * |
||
442 | * @param string $type |
||
443 | * |
||
444 | * @return string |
||
445 | */ |
||
446 | 2 | public function getToken( $type = 'csrf' ) { |
|
449 | |||
450 | /** |
||
451 | * @since 0.1 |
||
452 | * |
||
453 | * Clears all tokens stored by the api |
||
454 | */ |
||
455 | 1 | public function clearTokens() { |
|
458 | |||
459 | /** |
||
460 | * @return string |
||
461 | */ |
||
462 | 4 | public function getVersion() { |
|
463 | 4 | if ( !isset( $this->version ) ) { |
|
464 | 4 | $result = $this->getRequest( new SimpleRequest( 'query', [ |
|
465 | 4 | 'meta' => 'siteinfo', |
|
466 | 'continue' => '', |
||
477 | |||
478 | } |
||
479 |