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 |
||
| 25 | class MediawikiApi implements MediawikiApiInterface, LoggerAwareInterface { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var ClientInterface|null Should be accessed through getClient |
||
| 29 | */ |
||
| 30 | private $client = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool|string |
||
| 34 | */ |
||
| 35 | private $isLoggedIn; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var MediawikiSession |
||
| 39 | */ |
||
| 40 | private $session; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $version; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var LoggerInterface |
||
| 49 | */ |
||
| 50 | private $logger; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $apiUrl; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @since 2.0.0 |
||
| 59 | * |
||
| 60 | * @param string $apiEndpoint e.g. https://en.wikipedia.org/w/api.php |
||
| 61 | * |
||
| 62 | * @return self returns a MediawikiApi instance using $apiEndpoint |
||
| 63 | */ |
||
| 64 | public static function newFromApiEndpoint( $apiEndpoint ) { |
||
| 65 | return new self( $apiEndpoint ); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @since 2.0.0 |
||
| 70 | * |
||
| 71 | * @param string $url e.g. https://en.wikipedia.org OR https://de.wikipedia.org/wiki/Berlin |
||
| 72 | * |
||
| 73 | * @return self returns a MediawikiApi instance using the apiEndpoint provided by the RSD |
||
| 74 | * file accessible on all Mediawiki pages |
||
| 75 | * |
||
| 76 | * @see https://en.wikipedia.org/wiki/Really_Simple_Discovery |
||
| 77 | */ |
||
| 78 | 1 | public static function newFromPage( $url ) { |
|
| 79 | 1 | $tempClient = new Client( array( 'headers' => array( 'User-Agent' => 'addwiki-mediawiki-client' ) ) ); |
|
| 80 | 1 | $pageXml = new SimpleXMLElement( $tempClient->get( $url )->getBody() ); |
|
| 81 | 1 | $rsdElement = $pageXml->xpath( 'head/link[@type="application/rsd+xml"][@href]' ); |
|
| 82 | 1 | $rsdXml = new SimpleXMLElement( $tempClient->get( (string) $rsdElement[0]->attributes()['href'] )->getBody() ); |
|
| 83 | 1 | return self::newFromApiEndpoint( (string) $rsdXml->service->apis->api->attributes()->apiLink ); |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $apiUrl The API Url |
||
| 88 | * @param ClientInterface|null $client Guzzle Client |
||
| 89 | * @param MediawikiSession|null $session Inject a custom session here |
||
| 90 | */ |
||
| 91 | 24 | public function __construct( $apiUrl, ClientInterface $client = null, MediawikiSession $session = null ) { |
|
| 92 | 24 | if( !is_string( $apiUrl ) ) { |
|
| 93 | 4 | throw new InvalidArgumentException( '$apiUrl must be a string' ); |
|
| 94 | } |
||
| 95 | 20 | if( $session === null ) { |
|
| 96 | 20 | $session = new MediawikiSession( $this ); |
|
| 97 | } |
||
| 98 | |||
| 99 | 20 | $this->apiUrl = $apiUrl; |
|
| 100 | 20 | $this->client = $client; |
|
| 101 | 20 | $this->session = $session; |
|
| 102 | |||
| 103 | 20 | $this->logger = new NullLogger(); |
|
| 104 | 20 | } |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Get the API URL (the URL to which API requests are sent, usually ending in api.php). |
||
| 108 | * This is useful if you've created this object via MediawikiApi::newFromPage(). |
||
| 109 | * @return string The API URL. |
||
| 110 | */ |
||
| 111 | public function getApiUrl() { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return ClientInterface |
||
| 117 | */ |
||
| 118 | 21 | private function getClient() { |
|
| 119 | 21 | if( $this->client === null ) { |
|
| 120 | 4 | $clientFactory = new ClientFactory(); |
|
| 121 | 4 | $clientFactory->setLogger( $this->logger ); |
|
| 122 | 4 | $this->client = $clientFactory->getClient(); |
|
| 123 | } |
||
| 124 | 21 | return $this->client; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Sets a logger instance on the object |
||
| 129 | * |
||
| 130 | * @since 1.1 |
||
| 131 | * |
||
| 132 | * @param LoggerInterface $logger |
||
| 133 | * |
||
| 134 | * @return null |
||
| 135 | */ |
||
| 136 | public function setLogger( LoggerInterface $logger ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @since 2.0 |
||
| 143 | * |
||
| 144 | * @param Request $request |
||
| 145 | * |
||
| 146 | * @return PromiseInterface |
||
| 147 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 148 | * Can throw UsageExceptions or RejectionExceptions |
||
| 149 | */ |
||
| 150 | 1 | public function getRequestAsync( Request $request ) { |
|
| 151 | 1 | $promise = $this->getClient()->requestAsync( |
|
| 152 | 1 | 'GET', |
|
| 153 | 1 | $this->apiUrl, |
|
| 154 | 1 | $this->getClientRequestOptions( $request, 'query' ) |
|
| 155 | ); |
||
| 156 | |||
| 157 | return $promise->then( function( ResponseInterface $response ) { |
||
| 158 | 1 | return call_user_func( array( $this, 'decodeResponse' ), $response ); |
|
| 159 | 1 | } ); |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @since 2.0 |
||
| 164 | * |
||
| 165 | * @param Request $request |
||
| 166 | * |
||
| 167 | * @return PromiseInterface |
||
| 168 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 169 | * Can throw UsageExceptions or RejectionExceptions |
||
| 170 | */ |
||
| 171 | 1 | public function postRequestAsync( Request $request ) { |
|
| 172 | 1 | $promise = $this->getClient()->requestAsync( |
|
| 173 | 1 | 'POST', |
|
| 174 | 1 | $this->apiUrl, |
|
| 175 | 1 | $this->getClientRequestOptions( $request, $this->getPostRequestEncoding( $request ) ) |
|
| 176 | ); |
||
| 177 | |||
| 178 | return $promise->then( function( ResponseInterface $response ) { |
||
| 179 | 1 | return call_user_func( array( $this, 'decodeResponse' ), $response ); |
|
| 180 | 1 | } ); |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @since 0.2 |
||
| 185 | * |
||
| 186 | * @param Request $request |
||
| 187 | * |
||
| 188 | * @return mixed Normally an array |
||
| 189 | */ |
||
| 190 | 9 | public function getRequest( Request $request ) { |
|
| 191 | 9 | $response = $this->getClient()->request( |
|
| 192 | 9 | 'GET', |
|
| 193 | 9 | $this->apiUrl, |
|
| 194 | 9 | $this->getClientRequestOptions( $request, 'query' ) |
|
| 195 | ); |
||
| 196 | |||
| 197 | 9 | return $this->decodeResponse( $response ); |
|
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @since 0.2 |
||
| 202 | * |
||
| 203 | * @param Request $request |
||
| 204 | * |
||
| 205 | * @return mixed Normally an array |
||
| 206 | */ |
||
| 207 | 10 | public function postRequest( Request $request ) { |
|
| 208 | 10 | $response = $this->getClient()->request( |
|
| 209 | 10 | 'POST', |
|
| 210 | 10 | $this->apiUrl, |
|
| 211 | 10 | $this->getClientRequestOptions( $request, $this->getPostRequestEncoding( $request ) ) |
|
| 212 | ); |
||
| 213 | |||
| 214 | 10 | return $this->decodeResponse( $response ); |
|
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param ResponseInterface $response |
||
| 219 | * |
||
| 220 | * @return mixed |
||
| 221 | * @throws UsageException |
||
| 222 | */ |
||
| 223 | 21 | private function decodeResponse( ResponseInterface $response ) { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param Request $request |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | 9 | private function getPostRequestEncoding( Request $request ) { |
|
| 238 | 9 | foreach ( $request->getParams() as $value ) { |
|
| 239 | 9 | if ( is_resource( $value ) ) { |
|
| 240 | 9 | return 'multipart'; |
|
| 241 | } |
||
| 242 | } |
||
| 243 | 8 | return 'form_params'; |
|
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param Request $request |
||
| 248 | * @param string $paramsKey either 'query' or 'multipart' |
||
| 249 | * |
||
| 250 | * @throws RequestException |
||
| 251 | * |
||
| 252 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
| 253 | */ |
||
| 254 | 21 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
| 255 | 21 | $params = array_merge( $request->getParams(), array( 'format' => 'json' ) ); |
|
| 256 | 21 | if ( $paramsKey === 'multipart' ) { |
|
| 257 | 1 | $params = $this->encodeMultipartParams( $params ); |
|
| 258 | } |
||
| 259 | |||
| 260 | return array( |
||
| 261 | 21 | $paramsKey => $params, |
|
| 262 | 21 | 'headers' => array_merge( $this->getDefaultHeaders(), $request->getHeaders() ), |
|
| 263 | ); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param array $params |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | private function encodeMultipartParams( $params ) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | 17 | private function getDefaultHeaders() { |
|
| 283 | return array( |
||
| 284 | 17 | 'User-Agent' => $this->getUserAgent(), |
|
| 285 | ); |
||
| 286 | } |
||
| 287 | |||
| 288 | 17 | private function getUserAgent() { |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param $result |
||
| 298 | */ |
||
| 299 | 17 | private function logWarnings( $result ) { |
|
| 300 | 17 | if( is_array( $result ) && array_key_exists( 'warnings', $result ) ) { |
|
| 301 | foreach( $result['warnings'] as $module => $warningData ) { |
||
| 302 | $this->logger->log( LogLevel::WARNING, $module . ': ' . $warningData['*'], array( 'data' => $warningData ) ); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | 17 | } |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @param array $result |
||
| 309 | * |
||
| 310 | * @throws UsageException |
||
| 311 | */ |
||
| 312 | 17 | private function throwUsageExceptions( $result ) { |
|
| 313 | 17 | if( is_array( $result ) && array_key_exists( 'error', $result ) ) { |
|
| 314 | 2 | throw new UsageException( |
|
| 315 | 2 | $result['error']['code'], |
|
| 316 | 2 | $result['error']['info'], |
|
| 317 | $result |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | 15 | } |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @since 0.1 |
||
| 324 | * |
||
| 325 | * @return bool|string false or the name of the current user |
||
| 326 | */ |
||
| 327 | 17 | public function isLoggedin() { |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @since 0.1 |
||
| 333 | * |
||
| 334 | * @param ApiUser $apiUser |
||
| 335 | * |
||
| 336 | * @throws UsageException |
||
| 337 | * @return bool success |
||
| 338 | */ |
||
| 339 | 2 | public function login( ApiUser $apiUser ) { |
|
| 340 | 2 | $this->logger->log( LogLevel::DEBUG, 'Logging in' ); |
|
| 341 | 2 | $credentials = $this->getLoginParams( $apiUser ); |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @param ApiUser $apiUser |
||
| 359 | * |
||
| 360 | * @return string[] |
||
| 361 | */ |
||
| 362 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @param array $result |
||
| 376 | * |
||
| 377 | * @throws UsageException |
||
| 378 | */ |
||
| 379 | 1 | private function throwLoginUsageException( $result ) { |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $loginResult |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | 1 | private function getLoginExceptionMessage( $loginResult ) { |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @since 0.1 |
||
| 419 | * |
||
| 420 | * @return bool success |
||
| 421 | */ |
||
| 422 | 2 | public function logout() { |
|
| 432 | |||
| 433 | /** |
||
| 434 | * @since 0.1 |
||
| 435 | * |
||
| 436 | * @param string $type |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | 2 | public function getToken( $type = 'csrf' ) { |
|
| 443 | |||
| 444 | /** |
||
| 445 | * @since 0.1 |
||
| 446 | * |
||
| 447 | * Clears all tokens stored by the api |
||
| 448 | */ |
||
| 449 | 1 | public function clearTokens() { |
|
| 452 | |||
| 453 | /** |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | 4 | public function getVersion(){ |
|
| 471 | |||
| 472 | } |
||
| 473 |