|
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 Geocode extends ApiBase { |
|
13
|
|
|
|
|
14
|
|
|
public function execute() { |
|
15
|
|
|
global $wgUser; |
|
16
|
|
|
|
|
17
|
|
|
if ( !$wgUser->isAllowed( 'geocode' ) || $wgUser->isBlocked() ) { |
|
18
|
|
|
$this->dieUsageMsg( [ 'badaccess-groups' ] ); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
$geocoder = MapsFactory::newDefault()->newGeocoder(); |
|
22
|
|
|
|
|
23
|
|
|
$params = $this->extractRequestParams(); |
|
24
|
|
|
|
|
25
|
|
|
$results = []; |
|
26
|
|
|
|
|
27
|
|
|
foreach ( array_unique( $params['locations'] ) as $location ) { |
|
28
|
|
|
$result = $geocoder->geocode( $location ); |
|
29
|
|
|
|
|
30
|
|
|
$results[$location] = [ |
|
31
|
|
|
'count' => $result === null ? 0 : 1, |
|
32
|
|
|
'locations' => [] |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
if ( $result !== null ) { |
|
36
|
|
|
// FIXME: this makes the API use private var names in its output! |
|
37
|
|
|
$results[$location]['locations'][] = $result; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->getResult()->setIndexedTagName( $results[$location]['locations'], 'location' ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->getResult()->addValue( |
|
44
|
|
|
null, |
|
45
|
|
|
'results', |
|
46
|
|
|
$results |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getAllowedParams() { |
|
51
|
|
|
return [ |
|
52
|
|
|
'locations' => [ |
|
53
|
|
|
ApiBase::PARAM_TYPE => 'string', |
|
54
|
|
|
ApiBase::PARAM_REQUIRED => true, |
|
55
|
|
|
ApiBase::PARAM_ISMULTI => true, |
|
56
|
|
|
], |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getParamDescription() { |
|
61
|
|
|
return [ |
|
62
|
|
|
'locations' => 'The locations to geocode', |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getDescription() { |
|
67
|
|
|
return [ |
|
68
|
|
|
'API module for geocoding.' |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function getExamples() { |
|
73
|
|
|
return [ |
|
74
|
|
|
'api.php?action=geocode&locations=new york', |
|
75
|
|
|
'api.php?action=geocode&locations=new york|brussels|london', |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|