Completed
Pull Request — master (#130)
by None
07:05
created

Geocode::getExamples()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 7
ccs 0
cts 1
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Maps\Api;
4
5
use ApiBase;
6
7
/**
8
 * API module for geocoding.
9
 *
10
 * @since 1.0.3
11
 *
12
 * @ingroup API
13
 *
14
 * @licence GNU GPL v2++
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class Geocode extends ApiBase {
18
19
	public function __construct( $main, $action ) {
20
		parent::__construct( $main, $action );
21
	}
22
23
	public function execute() {
24
		global $wgUser;
25
26
		if ( !$wgUser->isAllowed( 'geocode' ) || $wgUser->isBlocked() ) {
27
			$this->dieUsageMsg( array( 'badaccess-groups' ) );
28
		}
29
30
		$params = $this->extractRequestParams();
31
32
		$results = array();
33
34
		foreach ( array_unique( $params['locations'] ) as $location ) {
35
			$result = \Maps\Geocoders::geocode( $location, $params['service'] );
36
37
			$results[$location] = array(
38
				'count' => $result === false ? 0 : 1,
39
				'locations' => array()
40
			);
41
42
			if ( $result !== false ) {
43
				$results[$location]['locations'][] = $result;
44
			}
45
46
			$this->getResult()->setIndexedTagName( $results[$location]['locations'], 'location' );
47
		}
48
49
		$this->getResult()->addValue(
50
			null,
51
			'results',
52
			$results
53
		);
54
	}
55
56
	public function getAllowedParams() {
57
		return array(
58
			'locations' => array(
59
				ApiBase::PARAM_TYPE => 'string',
60
				ApiBase::PARAM_REQUIRED => true,
61
				ApiBase::PARAM_ISMULTI => true,
62
			),
63
			'service' => array(
64
				ApiBase::PARAM_TYPE => \Maps\Geocoders::getAvailableGeocoders(),
65
			),
66
			'props' => array(
67
				ApiBase::PARAM_ISMULTI => true,
68
				ApiBase::PARAM_TYPE => array( 'lat', 'lon', 'alt' ),
69
				ApiBase::PARAM_DFLT => 'lat|lon',
70
			),
71
		);
72
	}
73
74
	public function getParamDescription() {
75
		return array(
76
			'locations' => 'The locations to geocode',
77
			'service' => 'The geocoding service to use',
78
		);
79
	}
80
81
	public function getDescription() {
82
		return array(
83
			'API module for geocoding.'
84
		);
85
	}
86
87
	public function getPossibleErrors() {
88
		return array_merge( parent::getPossibleErrors(), array(
89
			array( 'missingparam', 'locations' ),
90
		) );
91
	}
92
93
	protected function getExamples() {
94
		return array(
95
			'api.php?action=geocode&locations=new york',
96
			'api.php?action=geocode&locations=new york|brussels|london',
97
			'api.php?action=geocode&locations=new york&service=geonames',
98
		);
99
	}
100
101
	public function getVersion() {
102
		return __CLASS__ . '-' . Maps_VERSION;
103
	}
104
105
}
106