Completed
Pull Request — master (#554)
by Jeroen De
03:35
created

ApiGeocode::getAllowedParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 2
cts 2
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Maps\MediaWiki\Api;
4
5
use ApiBase;
6
use Maps\MapsFactory;
7
8
/**
9
 * @licence GNU GPL v2++
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class ApiGeocode extends ApiBase {
13
14 2
	public function execute() {
15 2
		if ( !$this->getUser()->isAllowed( 'geocode' ) || $this->getUser()->isBlocked() ) {
16 1
			$this->dieWithError( 'badaccess-groups' );
17
		}
18
19 1
		$geocoder = MapsFactory::newDefault()->getGeocoder();
20
21 1
		$params = $this->extractRequestParams();
22
23 1
		$results = [];
24
25 1
		foreach ( array_unique( $params['locations'] ) as $location ) {
26
			$result = $geocoder->geocode( $location );
27
28
			$results[$location] = [
29
				'count' => $result === null ? 0 : 1,
30
				'locations' => []
31
			];
32
33
			if ( $result !== null ) {
34
				// FIXME: this makes the API use private var names in its output!
35
				$results[$location]['locations'][] = $result;
36
			}
37
38
			$this->getResult()->setIndexedTagName( $results[$location]['locations'], 'location' );
39
		}
40
41 1
		$this->getResult()->addValue(
42 1
			null,
43 1
			'results',
44 1
			$results
45
		);
46 1
	}
47
48 2
	public function getAllowedParams() {
49
		return [
50 2
			'locations' => [
51
				ApiBase::PARAM_TYPE => 'string',
52
				ApiBase::PARAM_REQUIRED => true,
53
				ApiBase::PARAM_ISMULTI => true,
54
			],
55
		];
56
	}
57
58
	public function getParamDescription() {
59
		return [
60
			'locations' => 'The locations to geocode',
61
		];
62
	}
63
64
	public function getDescription() {
65
		return [
66
			'API module for geocoding.'
67
		];
68
	}
69
70
	protected function getExamples() {
71
		return [
72
			'api.php?action=geocode&locations=new york',
73
			'api.php?action=geocode&locations=new york|brussels|london',
74
		];
75
	}
76
77
}
78