Completed
Push — master ( 1a1684...348046 )
by adam
04:21
created

MediawikiApi::postRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Mediawiki\Api;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Exception\RequestException;
8
use GuzzleHttp\Promise\PromiseInterface;
9
use InvalidArgumentException;
10
use Mediawiki\Api\Guzzle\ClientFactory;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LoggerInterface;
14
use Psr\Log\LogLevel;
15
use Psr\Log\NullLogger;
16
use SimpleXMLElement;
17
18
/**
19
 * Main class for this library
20
 *
21
 * @since 0.1
22
 *
23
 * @author Addshore
24
 */
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
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
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 20
		}
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
	 *
110
	 * @since 2.3
111
	 *
112
	 * @return string The API URL.
113
	 */
114
	public function getApiUrl() {
115
		return $this->apiUrl;
116
	}
117
118
	/**
119
	 * @return ClientInterface
120
	 */
121 21
	private function getClient() {
122 21
		if( $this->client === null ) {
123 4
			$clientFactory = new ClientFactory();
124 4
			$clientFactory->setLogger( $this->logger );
125 4
			$this->client = $clientFactory->getClient();
126 4
		}
127 21
		return $this->client;
128
	}
129
130
	/**
131
	 * Sets a logger instance on the object
132
	 *
133
	 * @since 1.1
134
	 *
135
	 * @param LoggerInterface $logger
136
	 *
137
	 * @return null
138
	 */
139
	public function setLogger( LoggerInterface $logger ) {
140
		$this->logger = $logger;
141
		$this->session->setLogger( $logger );
142
	}
143
144
	/**
145
	 * @since 2.0
146
	 *
147
	 * @param Request $request
148
	 *
149
	 * @return PromiseInterface
150
	 *         Normally promising an array, though can be mixed (json_decode result)
151
	 *         Can throw UsageExceptions or RejectionExceptions
152
	 */
153 1
	public function getRequestAsync( Request $request ) {
154 1
		$promise = $this->getClient()->requestAsync(
155 1
			'GET',
156 1
			$this->apiUrl,
157 1
			$this->getClientRequestOptions( $request, 'query' )
158 1
		);
159
160
		return $promise->then( function( ResponseInterface $response ) {
161 1
			return call_user_func( array( $this, 'decodeResponse' ), $response );
162 1
		} );
163
	}
164
165
	/**
166
	 * @since 2.0
167
	 *
168
	 * @param Request $request
169
	 *
170
	 * @return PromiseInterface
171
	 *         Normally promising an array, though can be mixed (json_decode result)
172
	 *         Can throw UsageExceptions or RejectionExceptions
173
	 */
174 1
	public function postRequestAsync( Request $request ) {
175 1
		$promise = $this->getClient()->requestAsync(
176 1
			'POST',
177 1
			$this->apiUrl,
178 1
			$this->getClientRequestOptions( $request, $this->getPostRequestEncoding( $request ) )
179 1
		);
180
181
		return $promise->then( function( ResponseInterface $response ) {
182 1
			return call_user_func( array( $this, 'decodeResponse' ), $response );
183 1
		} );
184
	}
185
186
	/**
187
	 * @since 0.2
188
	 *
189
	 * @param Request $request
190
	 *
191
	 * @return mixed Normally an array
192
	 */
193 9
	public function getRequest( Request $request ) {
194 9
		$response = $this->getClient()->request(
195 9
			'GET',
196 9
			$this->apiUrl,
197 9
			$this->getClientRequestOptions( $request, 'query' )
198 9
		);
199
200 9
		return $this->decodeResponse( $response );
201
	}
202
203
	/**
204
	 * @since 0.2
205
	 *
206
	 * @param Request $request
207
	 *
208
	 * @return mixed Normally an array
209
	 */
210 10
	public function postRequest( Request $request ) {
211 10
		$response = $this->getClient()->request(
212 10
			'POST',
213 10
			$this->apiUrl,
214 10
			$this->getClientRequestOptions( $request, $this->getPostRequestEncoding( $request ) )
215 10
		);
216
217 10
		return $this->decodeResponse( $response );
218
	}
219
220
	/**
221
	 * @param ResponseInterface $response
222
	 *
223
	 * @return mixed
224
	 * @throws UsageException
225
	 */
226 21
	private function decodeResponse( ResponseInterface $response ) {
227 21
		$resultArray = json_decode( $response->getBody(), true );
228
229 21
		$this->logWarnings( $resultArray );
230 21
		$this->throwUsageExceptions( $resultArray );
231
232 19
		return $resultArray;
233
	}
234
235
    /**
236
     * @param Request $request
237
     *
238
     * @return string
239
     */
240 9
	private function getPostRequestEncoding( Request $request ) {
241 9
	    foreach ( $request->getParams() as $value ) {
242 9
            if ( is_resource( $value ) ) {
243 1
                return 'multipart';
244
            }
245 9
        }
246 8
        return 'form_params';
247
    }
248
249
	/**
250
	 * @param Request $request
251
	 * @param string $paramsKey either 'query' or 'multipart'
252
	 *
253
	 * @throws RequestException
254
	 *
255
	 * @return array as needed by ClientInterface::get and ClientInterface::post
256
	 */
257 21
	private function getClientRequestOptions( Request $request, $paramsKey ) {
258
259 21
		$params = array_merge( $request->getParams(), array( 'format' => 'json' ) );
260 21
		if ( $paramsKey === 'multipart' ) {
261 1
			$params = $this->encodeMultipartParams( $params );
262 1
		}
263
264
		return array(
265 21
			$paramsKey => $params,
266 21
			'headers' => array_merge( $this->getDefaultHeaders(), $request->getHeaders() ),
267 21
		);
268
	}
269
270
	/**
271
	 * @param array $params
272
	 *
273
	 * @return array
274
	 */
275 1
	private function encodeMultipartParams( $params ) {
276
277 1
		return array_map(
278 1
			function ( $name, $value ) {
279
280
				return array(
281 1
					'name' => $name,
282 1
					'contents' => $value,
283 1
				);
284 1
			},
285 1
			array_keys( $params ),
286
			$params
287 1
		);
288
	}
289
290
	/**
291
	 * @return array
292
	 */
293 17
	private function getDefaultHeaders() {
294
		return array(
295 17
			'User-Agent' => $this->getUserAgent(),
296 17
		);
297
	}
298
299 17
	private function getUserAgent() {
300 17
		$loggedIn = $this->isLoggedin();
301 17
		if( $loggedIn ) {
302
			return 'addwiki-mediawiki-client/' . $loggedIn;
303
		}
304 17
		return 'addwiki-mediawiki-client';
305
	}
306
307
	/**
308
	 * @param $result
309
	 */
310 17
	private function logWarnings( $result ) {
311 17
		if( is_array( $result ) && array_key_exists( 'warnings', $result ) ) {
312
			foreach( $result['warnings'] as $module => $warningData ) {
313
				$this->logger->log( LogLevel::WARNING, $module . ': ' . $warningData['*'], array( 'data' => $warningData ) );
314
			}
315
		}
316 17
	}
317
318
	/**
319
	 * @param array $result
320
	 *
321
	 * @throws UsageException
322
	 */
323 17
	private function throwUsageExceptions( $result ) {
324 17
		if( is_array( $result ) && array_key_exists( 'error', $result ) ) {
325 2
			throw new UsageException(
326 2
				$result['error']['code'],
327 2
				$result['error']['info'],
328
				$result
329 2
			);
330
		}
331 15
	}
332
333
	/**
334
	 * @since 0.1
335
	 *
336
	 * @return bool|string false or the name of the current user
337
	 */
338 17
	public function isLoggedin() {
339 17
		return $this->isLoggedIn;
340
	}
341
342
	/**
343
	 * @since 0.1
344
	 *
345
	 * @param ApiUser $apiUser
346
	 *
347
	 * @throws UsageException
348
	 * @return bool success
349
	 */
350 2
	public function login( ApiUser $apiUser ) {
351 2
		$this->logger->log( LogLevel::DEBUG, 'Logging in' );
352 2
		$credentials = $this->getLoginParams( $apiUser );
353 2
		$result = $this->postRequest( new SimpleRequest( 'login', $credentials ) );
354 2
		if ( $result['login']['result'] == "NeedToken" ) {
355 2
			$result = $this->postRequest( new SimpleRequest( 'login', array_merge( array( 'lgtoken' => $result['login']['token'] ), $credentials) ) );
356 2
		}
357 2
		if ( $result['login']['result'] == "Success" ) {
358 1
			$this->isLoggedIn = $apiUser->getUsername();
359 1
			return true;
360
		}
361
362 1
		$this->isLoggedIn = false;
363 1
		$this->logger->log( LogLevel::DEBUG, 'Login failed.', $result );
364 1
		$this->throwLoginUsageException( $result );
365
		return false;
366
	}
367
368
	/**
369
	 * @param ApiUser $apiUser
370
	 *
371
	 * @return string[]
372
	 */
373 2
	private function getLoginParams( ApiUser $apiUser ) {
374
		$params = array(
375 2
			'lgname' => $apiUser->getUsername(),
376 2
			'lgpassword' => $apiUser->getPassword(),
377 2
		);
378
379 2
		if( !is_null( $apiUser->getDomain() ) ) {
380
			$params['lgdomain'] = $apiUser->getDomain();
381
		}
382 2
		return $params;
383
	}
384
385
	/**
386
	 * @param array $result
387
	 *
388
	 * @throws UsageException
389
	 */
390 1
	private function throwLoginUsageException( $result ) {
391 1
		$loginResult = $result['login']['result'];
392
393 1
		throw new UsageException(
394 1
			'login-' . $loginResult,
395 1
			$this->getLoginExceptionMessage( $loginResult ),
396
			$result
397 1
		);
398
	}
399
400
	/**
401
	 * @param string $loginResult
402
	 *
403
	 * @return string
404
	 */
405 1
	private function getLoginExceptionMessage( $loginResult ) {
406
		switch( $loginResult ) {
407 1
			case 'Illegal';
408
				return 'You provided an illegal username';
409 1
			case 'NotExists';
410
				return 'The username you provided doesn\'t exist';
411 1
			case 'WrongPass';
412
				return 'The password you provided is incorrect';
413 1
			case 'WrongPluginPass';
414
				return 'An authentication plugin rather than MediaWiki itself rejected the password';
415 1
			case 'CreateBlocked';
416
				return 'The wiki tried to automatically create a new account for you, but your IP address has been blocked from account creation';
417 1
			case 'Throttled';
418
				return 'You\'ve logged in too many times in a short time.';
419 1
			case 'Blocked';
420
				return 'User is blocked';
421 1
			case 'NeedToken';
422
				return 'Either you did not provide the login token or the sessionid cookie.';
423 1
			default:
424 1
				return $loginResult;
425 1
		}
426
	}
427
428
	/**
429
	 * @since 0.1
430
	 *
431
	 * @return bool success
432
	 */
433 2
	public function logout() {
434 2
		$this->logger->log( LogLevel::DEBUG, 'Logging out' );
435 2
		$result = $this->postRequest( new SimpleRequest( 'logout' ) );
436 2
		if( $result === array() ) {
437 1
			$this->isLoggedIn = false;
438 1
			$this->clearTokens();
439 1
			return true;
440
		}
441 1
		return false;
442
	}
443
444
	/**
445
	 * @since 0.1
446
	 *
447
	 * @param string $type
448
	 *
449
	 * @return string
450
	 */
451 2
	public function getToken( $type = 'csrf' ) {
452 2
		return $this->session->getToken( $type );
453
	}
454
455
	/**
456
	 * @since 0.1
457
	 *
458
	 * Clears all tokens stored by the api
459
	 */
460 1
	public function clearTokens() {
461 1
		$this->session->clearTokens();
462 1
	}
463
464
	/**
465
	 * @return string
466
	 */
467 4
	public function getVersion(){
468 4
		if( !isset( $this->version ) ) {
469 4
			$result = $this->getRequest( new SimpleRequest( 'query', array(
470 4
				'meta' => 'siteinfo',
471 4
				'continue' => '',
472 4
			) ) );
473 4
			preg_match(
474 4
				'/\d+(?:\.\d+)+/',
475 4
				$result['query']['general']['generator'],
476
				$versionParts
477 4
			);
478 4
			$this->version = $versionParts[0];
479 4
		}
480 4
		return $this->version;
481
	}
482
483
}
484