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