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