|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maps; |
|
4
|
|
|
|
|
5
|
|
|
use Html; |
|
6
|
|
|
use ParamProcessor\ParameterTypes; |
|
7
|
|
|
use ParamProcessor\ProcessingResult; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @licence GNU GPL v2+ |
|
11
|
|
|
*/ |
|
12
|
|
|
class LeafletService implements MappingService { |
|
13
|
|
|
|
|
14
|
|
|
private $addedDependencies = []; |
|
15
|
|
|
|
|
16
|
24 |
|
public function getName(): string { |
|
17
|
24 |
|
return 'leaflet'; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
23 |
|
public function getAliases(): array { |
|
21
|
23 |
|
return [ 'leafletmaps', 'leaflet' ]; // TODO: main name should not be in here? |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function hasAlias( string $alias ): bool { |
|
25
|
|
|
return in_array( $alias, [ 'leafletmaps', 'leaflet' ] ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
17 |
|
public function getParameterInfo(): array { |
|
29
|
17 |
|
$params = MapsFunctions::getCommonParameters(); |
|
30
|
|
|
|
|
31
|
17 |
|
$params['zoom'] = [ |
|
32
|
|
|
'type' => ParameterTypes::INTEGER, |
|
33
|
|
|
'range' => [ 0, 20 ], |
|
34
|
|
|
'default' => false, |
|
35
|
|
|
'message' => 'maps-par-zoom' |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
17 |
|
$params['defzoom'] = [ |
|
39
|
17 |
|
'type' => ParameterTypes::INTEGER, |
|
40
|
|
|
'range' => [ 0, 20 ], |
|
41
|
17 |
|
'default' => self::getDefaultZoom(), |
|
42
|
17 |
|
'message' => 'maps-leaflet-par-defzoom' |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
17 |
|
$params['layers'] = [ |
|
46
|
17 |
|
'aliases' => 'layer', |
|
47
|
17 |
|
'type' => 'string', |
|
48
|
17 |
|
'values' => array_keys( $GLOBALS['egMapsLeafletAvailableLayers'], true, true ), |
|
49
|
17 |
|
'default' => $GLOBALS['egMapsLeafletLayers'], |
|
50
|
17 |
|
'message' => 'maps-leaflet-par-layers', |
|
51
|
|
|
'islist' => true, |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
17 |
|
$params['overlays'] = [ |
|
55
|
17 |
|
'aliases' => [ 'overlaylayers' ], |
|
56
|
|
|
'type' => ParameterTypes::STRING, |
|
57
|
17 |
|
'values' => array_keys( $GLOBALS['egMapsLeafletAvailableOverlayLayers'], true, true ), |
|
58
|
17 |
|
'default' => $GLOBALS['egMapsLeafletOverlayLayers'], |
|
59
|
17 |
|
'message' => 'maps-leaflet-par-overlaylayers', |
|
60
|
|
|
'islist' => true, |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
17 |
|
$params['resizable'] = [ |
|
64
|
17 |
|
'type' => ParameterTypes::BOOLEAN, |
|
65
|
17 |
|
'default' => $GLOBALS['egMapsResizableByDefault'], |
|
66
|
17 |
|
'message' => 'maps-par-resizable' |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
17 |
|
$params['fullscreen'] = [ |
|
70
|
|
|
'aliases' => [ 'enablefullscreen' ], |
|
71
|
|
|
'type' => ParameterTypes::BOOLEAN, |
|
72
|
|
|
'default' => false, |
|
73
|
|
|
'message' => 'maps-par-enable-fullscreen', |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
17 |
|
$params['scrollwheelzoom'] = [ |
|
77
|
|
|
'aliases' => [ 'scrollzoom' ], |
|
78
|
|
|
'type' => ParameterTypes::BOOLEAN, |
|
79
|
|
|
'default' => true, |
|
80
|
|
|
'message' => 'maps-par-scrollwheelzoom', |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
17 |
|
$params['cluster'] = [ |
|
84
|
|
|
'aliases' => [ 'markercluster' ], |
|
85
|
|
|
'type' => ParameterTypes::BOOLEAN, |
|
86
|
|
|
'default' => false, |
|
87
|
|
|
'message' => 'maps-par-markercluster', |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
17 |
|
$params['clustermaxzoom'] = [ |
|
91
|
|
|
'type' => ParameterTypes::INTEGER, |
|
92
|
|
|
'default' => 20, |
|
93
|
|
|
'message' => 'maps-par-clustermaxzoom', |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
17 |
|
$params['clusterzoomonclick'] = [ |
|
97
|
|
|
'type' => ParameterTypes::BOOLEAN, |
|
98
|
|
|
'default' => true, |
|
99
|
|
|
'message' => 'maps-par-clusterzoomonclick', |
|
100
|
|
|
]; |
|
101
|
|
|
|
|
102
|
17 |
|
$params['clustermaxradius'] = [ |
|
103
|
|
|
'type' => ParameterTypes::INTEGER, |
|
104
|
|
|
'default' => 80, |
|
105
|
|
|
'message' => 'maps-par-maxclusterradius', |
|
106
|
|
|
]; |
|
107
|
|
|
|
|
108
|
17 |
|
$params['clusterspiderfy'] = [ |
|
109
|
|
|
'type' => ParameterTypes::BOOLEAN, |
|
110
|
|
|
'default' => true, |
|
111
|
|
|
'message' => 'maps-leaflet-par-clusterspiderfy', |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
17 |
|
$params['geojson'] = [ |
|
115
|
|
|
'type' => ParameterTypes::STRING, |
|
116
|
|
|
'default' => '', |
|
117
|
|
|
'message' => 'maps-displaymap-par-geojson', |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
17 |
|
$params['clicktarget'] = [ |
|
121
|
|
|
'type' => ParameterTypes::STRING, |
|
122
|
|
|
'default' => '', |
|
123
|
|
|
'message' => 'maps-leaflet-par-clicktarget', |
|
124
|
|
|
]; |
|
125
|
|
|
|
|
126
|
17 |
|
return $params; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @since 3.0 |
|
131
|
|
|
*/ |
|
132
|
17 |
|
public function getDefaultZoom() { |
|
133
|
17 |
|
return $GLOBALS['egMapsLeafletZoom']; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
17 |
|
public function newMapId(): string { |
|
137
|
17 |
|
static $mapsOnThisPage = 0; |
|
138
|
|
|
|
|
139
|
17 |
|
$mapsOnThisPage++; |
|
140
|
|
|
|
|
141
|
17 |
|
return 'map_leaflet_' . $mapsOnThisPage; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
17 |
|
public function getResourceModules(): array { |
|
145
|
17 |
|
return [ 'ext.maps.leaflet.loader', 'ext.maps.leaflet.leafletajax' ]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
17 |
|
public function getDependencyHtml( array $params ): string { |
|
149
|
17 |
|
$dependencies = []; |
|
150
|
|
|
|
|
151
|
|
|
// Only add dependencies that have not yet been added. |
|
152
|
17 |
|
foreach ( $this->getDependencies( $params ) as $dependency ) { |
|
153
|
17 |
|
if ( !in_array( $dependency, $this->addedDependencies ) ) { |
|
154
|
17 |
|
$dependencies[] = $dependency; |
|
155
|
17 |
|
$this->addedDependencies[] = $dependency; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// If there are dependencies, put them all together in a string, otherwise return false. |
|
160
|
17 |
|
return $dependencies !== [] ? implode( '', $dependencies ) : false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
17 |
|
private function getDependencies( array $params ): array { |
|
164
|
17 |
|
$leafletPath = $GLOBALS['wgScriptPath'] . '/extensions/Maps/resources/lib/leaflet'; |
|
165
|
|
|
|
|
166
|
17 |
|
return array_merge( |
|
167
|
|
|
[ |
|
168
|
17 |
|
Html::linkedStyle( "$leafletPath/leaflet.css" ), |
|
169
|
17 |
|
Html::linkedScript( "$leafletPath/leaflet.js" ), |
|
170
|
|
|
], |
|
171
|
17 |
|
$this->getLayerDependencies( $params ) |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
17 |
|
private function getLayerDependencies( array $params ) { |
|
176
|
17 |
|
global $egMapsLeafletLayerDependencies, $egMapsLeafletAvailableLayers, |
|
177
|
17 |
|
$egMapsLeafletLayersApiKeys; |
|
178
|
|
|
|
|
179
|
17 |
|
$layerDependencies = []; |
|
180
|
|
|
|
|
181
|
17 |
|
foreach ( $params['layers'] as $layerName ) { |
|
182
|
17 |
|
if ( array_key_exists( $layerName, $egMapsLeafletAvailableLayers ) |
|
183
|
17 |
|
&& $egMapsLeafletAvailableLayers[$layerName] |
|
184
|
17 |
|
&& array_key_exists( $layerName, $egMapsLeafletLayersApiKeys ) |
|
185
|
17 |
|
&& array_key_exists( $layerName, $egMapsLeafletLayerDependencies ) ) { |
|
186
|
|
|
$layerDependencies[] = '<script src="' . $egMapsLeafletLayerDependencies[$layerName] . |
|
187
|
|
|
$egMapsLeafletLayersApiKeys[$layerName] . '"></script>'; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
17 |
|
return array_unique( $layerDependencies ); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
16 |
|
public function processingResultToMapParams( ProcessingResult $processingResult ): array { |
|
195
|
16 |
|
$mapParams = $processingResult->getParameterArray(); |
|
196
|
|
|
|
|
197
|
16 |
|
if ( $mapParams['geojson'] !== '' ) { |
|
198
|
2 |
|
$fetcher = MapsFactory::newDefault()->newGeoJsonFetcher(); |
|
199
|
|
|
|
|
200
|
2 |
|
$result = $fetcher->fetch( $mapParams['geojson'] ); |
|
201
|
|
|
|
|
202
|
2 |
|
$mapParams['geojson'] = $result->getContent(); |
|
203
|
2 |
|
$mapParams['GeoJsonSource'] = $result->getTitleValue() === null ? null : $result->getTitleValue()->getText(); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
16 |
|
return $mapParams; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|