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, |
|
120 | MediawikiSession $session = null ) { |
||
121 | 24 | if ( !is_string( $apiUrl ) ) { |
|
122 | 4 | throw new InvalidArgumentException( '$apiUrl must be a string' ); |
|
123 | } |
||
124 | 20 | if ( $session === null ) { |
|
125 | 20 | $session = new MediawikiSession( $this ); |
|
126 | } |
||
127 | |||
128 | 20 | $this->apiUrl = $apiUrl; |
|
129 | 20 | $this->client = $client; |
|
130 | 20 | $this->session = $session; |
|
131 | |||
132 | 20 | $this->logger = new NullLogger(); |
|
133 | 20 | } |
|
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() { |
|
151 | 21 | if ( $this->client === null ) { |
|
152 | 4 | $clientFactory = new ClientFactory(); |
|
153 | 4 | $clientFactory->setLogger( $this->logger ); |
|
154 | 4 | $this->client = $clientFactory->getClient(); |
|
155 | } |
||
156 | 21 | return $this->client; |
|
157 | } |
||
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 ) { |
|
183 | 1 | $promise = $this->getClient()->requestAsync( |
|
184 | 1 | 'GET', |
|
185 | 1 | $this->apiUrl, |
|
186 | 1 | $this->getClientRequestOptions( $request, 'query' ) |
|
187 | ); |
||
188 | |||
189 | return $promise->then( function( ResponseInterface $response ) { |
||
190 | 1 | return call_user_func( [ $this, 'decodeResponse' ], $response ); |
|
191 | 1 | } ); |
|
192 | } |
||
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 ) { |
|
204 | 1 | $promise = $this->getClient()->requestAsync( |
|
205 | 1 | 'POST', |
|
206 | 1 | $this->apiUrl, |
|
207 | 1 | $this->getClientRequestOptions( $request, $this->getPostRequestEncoding( $request ) ) |
|
208 | ); |
||
209 | |||
210 | return $promise->then( function( ResponseInterface $response ) { |
||
211 | 1 | return call_user_func( [ $this, 'decodeResponse' ], $response ); |
|
212 | 1 | } ); |
|
213 | } |
||
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 ) { |
|
223 | 9 | $response = $this->getClient()->request( |
|
224 | 9 | 'GET', |
|
225 | 9 | $this->apiUrl, |
|
226 | 9 | $this->getClientRequestOptions( $request, 'query' ) |
|
227 | ); |
||
228 | |||
229 | 9 | return $this->decodeResponse( $response ); |
|
230 | } |
||
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 ) { |
|
240 | 10 | $response = $this->getClient()->request( |
|
241 | 10 | 'POST', |
|
242 | 10 | $this->apiUrl, |
|
243 | 10 | $this->getClientRequestOptions( $request, $this->getPostRequestEncoding( $request ) ) |
|
244 | ); |
||
245 | |||
246 | 10 | return $this->decodeResponse( $response ); |
|
247 | } |
||
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 | foreach ( $request->getParams() as $value ) { |
|
271 | 9 | if ( is_resource( $value ) ) { |
|
272 | 9 | return 'multipart'; |
|
273 | } |
||
274 | } |
||
275 | 8 | return 'form_params'; |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * @param Request $request |
||
280 | * @param string $paramsKey either 'query' or 'multipart' |
||
281 | * |
||
282 | * @throws RequestException |
||
283 | * |
||
284 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
285 | */ |
||
286 | 21 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
287 | |||
288 | 21 | $params = array_merge( $request->getParams(), [ 'format' => 'json' ] ); |
|
289 | 21 | if ( $paramsKey === 'multipart' ) { |
|
290 | 1 | $params = $this->encodeMultipartParams( $params ); |
|
291 | } |
||
292 | |||
293 | return [ |
||
294 | 21 | $paramsKey => $params, |
|
295 | 21 | 'headers' => array_merge( $this->getDefaultHeaders(), $request->getHeaders() ), |
|
296 | ]; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @param array $params |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | 1 | private function encodeMultipartParams( $params ) { |
|
305 | |||
306 | 1 | return array_map( |
|
307 | 1 | function ( $name, $value ) { |
|
308 | |||
309 | return [ |
||
310 | 1 | 'name' => $name, |
|
311 | 1 | 'contents' => $value, |
|
312 | ]; |
||
313 | 1 | }, |
|
314 | array_keys( $params ), |
||
315 | $params |
||
316 | ); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @return array |
||
321 | */ |
||
322 | 17 | private function getDefaultHeaders() { |
|
323 | return [ |
||
324 | 17 | 'User-Agent' => $this->getUserAgent(), |
|
325 | ]; |
||
326 | } |
||
327 | |||
328 | 17 | private function getUserAgent() { |
|
335 | |||
336 | /** |
||
337 | * @param $result |
||
338 | */ |
||
339 | 17 | private function logWarnings( $result ) { |
|
340 | 17 | if ( is_array( $result ) && array_key_exists( 'warnings', $result ) ) { |
|
341 | foreach ( $result['warnings'] as $module => $warningData ) { |
||
342 | // Accomodate both formatversion=2 and old-style API results |
||
343 | $logPrefix = $module . ': '; |
||
344 | if ( isset( $warningData['*'] ) ) { |
||
345 | $this->logger->warning( $logPrefix . $warningData['*'], [ 'data' => $warningData ] ); |
||
346 | } else { |
||
347 | $this->logger->warning( $logPrefix . $warningData['warnings'], [ 'data' => $warningData ] ); |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | 17 | } |
|
352 | |||
353 | /** |
||
354 | * @param array $result |
||
355 | * |
||
356 | * @throws UsageException |
||
357 | */ |
||
358 | 17 | private function throwUsageExceptions( $result ) { |
|
359 | 17 | if ( is_array( $result ) && array_key_exists( 'error', $result ) ) { |
|
360 | 2 | throw new UsageException( |
|
361 | 2 | $result['error']['code'], |
|
362 | 2 | $result['error']['info'], |
|
363 | $result |
||
364 | ); |
||
365 | } |
||
366 | 15 | } |
|
367 | |||
368 | /** |
||
369 | * @since 0.1 |
||
370 | * |
||
371 | * @return bool|string false or the name of the current user |
||
372 | */ |
||
373 | 17 | public function isLoggedin() { |
|
376 | |||
377 | /** |
||
378 | * @since 0.1 |
||
379 | * |
||
380 | * @param ApiUser $apiUser |
||
381 | * |
||
382 | * @throws UsageException |
||
383 | * @return bool success |
||
384 | */ |
||
385 | 2 | public function login( ApiUser $apiUser ) { |
|
386 | 2 | $this->logger->log( LogLevel::DEBUG, 'Logging in' ); |
|
387 | 2 | $credentials = $this->getLoginParams( $apiUser ); |
|
388 | 2 | $result = $this->postRequest( new SimpleRequest( 'login', $credentials ) ); |
|
389 | 2 | if ( $result['login']['result'] == "NeedToken" ) { |
|
390 | 2 | $params = array_merge( [ 'lgtoken' => $result['login']['token'] ], $credentials ); |
|
391 | 2 | $result = $this->postRequest( new SimpleRequest( 'login', $params ) ); |
|
392 | } |
||
393 | 2 | if ( $result['login']['result'] == "Success" ) { |
|
394 | 1 | $this->isLoggedIn = $apiUser->getUsername(); |
|
395 | 1 | return true; |
|
396 | } |
||
397 | |||
398 | 1 | $this->isLoggedIn = false; |
|
399 | 1 | $this->logger->log( LogLevel::DEBUG, 'Login failed.', $result ); |
|
400 | 1 | $this->throwLoginUsageException( $result ); |
|
401 | return false; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @param ApiUser $apiUser |
||
406 | * |
||
407 | * @return string[] |
||
408 | */ |
||
409 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
410 | $params = [ |
||
411 | 2 | 'lgname' => $apiUser->getUsername(), |
|
412 | 2 | 'lgpassword' => $apiUser->getPassword(), |
|
413 | ]; |
||
414 | |||
415 | 2 | if ( !is_null( $apiUser->getDomain() ) ) { |
|
416 | $params['lgdomain'] = $apiUser->getDomain(); |
||
417 | } |
||
418 | 2 | return $params; |
|
419 | } |
||
420 | |||
421 | /** |
||
422 | * @param array $result |
||
423 | * |
||
424 | * @throws UsageException |
||
425 | */ |
||
426 | 1 | private function throwLoginUsageException( $result ) { |
|
427 | 1 | $loginResult = $result['login']['result']; |
|
428 | |||
429 | 1 | throw new UsageException( |
|
430 | 1 | 'login-' . $loginResult, |
|
431 | 1 | array_key_exists( 'reason', $result['login'] ) |
|
432 | ? $result['login']['reason'] |
||
433 | 1 | : 'No Reason given', |
|
434 | $result |
||
435 | ); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * @since 0.1 |
||
440 | * |
||
441 | * @return bool success |
||
442 | */ |
||
443 | 2 | public function logout() { |
|
453 | |||
454 | /** |
||
455 | * @since 0.1 |
||
456 | * |
||
457 | * @param string $type |
||
458 | * |
||
459 | * @return string |
||
460 | */ |
||
461 | 2 | public function getToken( $type = 'csrf' ) { |
|
464 | |||
465 | /** |
||
466 | * @since 0.1 |
||
467 | * |
||
468 | * Clears all tokens stored by the api |
||
469 | */ |
||
470 | 1 | public function clearTokens() { |
|
473 | |||
474 | /** |
||
475 | * @return string |
||
476 | */ |
||
477 | 4 | public function getVersion() { |
|
478 | 4 | if ( !isset( $this->version ) ) { |
|
479 | 4 | $result = $this->getRequest( new SimpleRequest( 'query', [ |
|
480 | 4 | 'meta' => 'siteinfo', |
|
481 | 'continue' => '', |
||
482 | ] ) ); |
||
483 | 4 | preg_match( |
|
484 | 4 | '/\d+(?:\.\d+)+/', |
|
485 | 4 | $result['query']['general']['generator'], |
|
486 | $versionParts |
||
487 | ); |
||
488 | 4 | $this->version = $versionParts[0]; |
|
492 | |||
493 | } |
||
494 |