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