Completed
Push — master ( cf7c26...f36585 )
by Jeroen De
02:58
created

MapsSetup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps;
6
7
use DataValues\Geo\Parsers\LatLongParser;
8
use Maps\DataAccess\JsonFileParser;
9
use Maps\MediaWiki\Content\GeoJsonContent;
10
use Maps\MediaWiki\Content\GeoJsonContentHandler;
11
use Maps\MediaWiki\ParserHooks\CoordinatesFunction;
12
use Maps\MediaWiki\ParserHooks\DisplayMapFunction;
13
use Maps\MediaWiki\ParserHooks\DistanceFunction;
14
use Maps\MediaWiki\ParserHooks\FindDestinationFunction;
15
use Maps\MediaWiki\ParserHooks\GeocodeFunction;
16
use Maps\MediaWiki\ParserHooks\GeoDistanceFunction;
17
use Maps\MediaWiki\ParserHooks\MapsDocFunction;
18
use Maps\Presentation\WikitextParsers\CircleParser;
19
use Maps\Presentation\WikitextParsers\DistanceParser;
20
use Maps\Presentation\WikitextParsers\ImageOverlayParser;
21
use Maps\Presentation\WikitextParsers\LineParser;
22
use Maps\Presentation\WikitextParsers\LocationParser;
23
use Maps\Presentation\WikitextParsers\PolygonParser;
24
use Maps\Presentation\WikitextParsers\RectangleParser;
25
use Maps\Presentation\WikitextParsers\WmsOverlayParser;
26
use Parser;
27
use PPFrame;
28
29
/**
30
 * @licence GNU GPL v2+
31
 * @author Jeroen De Dauw < [email protected] >
32
 */
33
class MapsSetup {
34
35
	private $mwGlobals;
36
37
	public function __construct( array &$mwGlobals ) {
38
		$this->mwGlobals = $mwGlobals;
39
	}
40
41
	public function setup() {
42
		$this->defaultSettings();
43
		$this->registerAllTheThings();
44
45
		if ( !$this->mwGlobals['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ) ) {
46
			SemanticMaps::newFromMediaWikiGlobals( $this->mwGlobals )->initExtension();
47
		}
48
	}
49
50
	private function registerAllTheThings() {
51
		$this->registerParserHooks();
52
		$this->registerMappingServices();
53
		$this->registerPermissions();
54
		$this->registerParameterTypes();
55
		$this->registerHooks();
56
57
		$this->mwGlobals['wgContentHandlers'][GeoJsonContent::CONTENT_MODEL_ID] = GeoJsonContentHandler::class;
58
	}
59
60
	private function defaultSettings() {
61
		if ( $this->mwGlobals['egMapsGMaps3Language'] === '' ) {
62
			$this->mwGlobals['egMapsGMaps3Language'] = $this->mwGlobals['wgLang'];
63
		}
64
65
		if ( in_array( 'googlemaps3', $this->mwGlobals['egMapsAvailableServices'] ) ) {
66
			$this->mwGlobals['wgSpecialPages']['MapEditor'] = 'Maps\MediaWiki\Specials\SpecialMapEditor';
67
			$this->mwGlobals['wgSpecialPageGroups']['MapEditor'] = 'maps';
68
		}
69
70
		if ( $this->mwGlobals['egMapsGMaps3ApiKey'] === '' && array_key_exists(
71
				'egGoogleJsApiKey',
72
				$this->mwGlobals
73
			) ) {
74
			$this->mwGlobals['egMapsGMaps3ApiKey'] = $this->mwGlobals['egGoogleJsApiKey'];
75
		}
76
	}
77
78 50
	private function registerParserHooks() {
79
		if ( $this->mwGlobals['egMapsEnableCoordinateFunction'] ) {
80 50
			$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
81 50
				return ( new CoordinatesFunction() )->init( $parser );
82
			};
83
		}
84
85 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
86 50
			foreach ( [ 'display_map', 'display_point', 'display_points', 'display_line' ] as $hookName ) {
87 50
				$parser->setFunctionHook(
88 50
					$hookName,
89 50
					function ( Parser $parser, PPFrame $frame, array $arguments ) {
90 19
						$mapHtml = MapsFactory::newDefault()->getDisplayMapFunction()->getMapHtmlForKeyValueStrings(
91 19
							$parser,
92 19
							array_map(
93 19
								function ( $argument ) use ( $frame ) {
94 19
									return $frame->expand( $argument );
95 19
								},
96 19
								$arguments
97
							)
98
						);
99
100
						return [
101 19
							$mapHtml,
102
							'noparse' => true,
103
							'isHTML' => true,
104
						];
105 50
					},
106 50
					Parser::SFH_OBJECT_ARGS
107
				);
108
109 50
				$parser->setHook(
110 50
					$hookName,
111 50
					function ( $text, array $arguments, Parser $parser ) {
112 2
						if ( $text !== null ) {
113 2
							$defaultParameters = DisplayMapFunction::getHookDefinition( "\n" )->getDefaultParameters();
114 2
							$defaultParam = array_shift( $defaultParameters );
115
116
							// If there is a first default parameter, set the tag contents as its value.
117 2
							if ( $defaultParam !== null ) {
118 2
								$arguments[$defaultParam] = $text;
119
							}
120
						}
121
122 2
						return MapsFactory::newDefault()->getDisplayMapFunction()->getMapHtmlForParameterList( $parser, $arguments );
123 50
					}
124
				);
125
			}
126 50
		};
127
128 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
129 50
			return ( new DistanceFunction() )->init( $parser );
130
		};
131
132 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
133 50
			return ( new FindDestinationFunction() )->init( $parser );
134
		};
135
136 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
137 50
			return ( new GeocodeFunction() )->init( $parser );
138
		};
139
140 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
141 50
			return ( new GeoDistanceFunction() )->init( $parser );
142
		};
143
144 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
145 50
			return ( new MapsDocFunction() )->init( $parser );
146
		};
147
	}
148
149
	private function registerMappingServices() {
150
		$localBasePath = __DIR__ . '/../resources';
151
		$remoteExtPath = 'Maps/resources';
152
153
		$this->registerGoogleMapsModules( $localBasePath, $remoteExtPath );
154
155
		$this->registerLeafletModules( $localBasePath, $remoteExtPath );
156
	}
157
158
	private function registerGoogleMapsModules( string $localBasePath, string $remoteExtPath ) {
159
		global $wgResourceModules;
160
161
		$localBasePath = $localBasePath . '/GoogleMaps';
162
		$remoteExtPath = $remoteExtPath . '/GoogleMaps';
163
164
		$wgResourceModules['ext.maps.googlemaps3'] = [
165
			'dependencies' => [ 'ext.maps.common' ],
166
			'localBasePath' => $localBasePath,
167
			'remoteExtPath' => $remoteExtPath,
168
			'group' => 'ext.maps',
169
			'targets' => [
170
				'mobile',
171
				'desktop'
172
			],
173
			'scripts' => [
174
				'jquery.googlemap.js',
175
				'ext.maps.googlemaps3.js'
176
			],
177
			'messages' => [
178
				'maps-googlemaps3-incompatbrowser',
179
				'maps-copycoords-prompt',
180
				'maps-searchmarkers-text',
181
				'maps-fullscreen-button',
182
				'maps-fullscreen-button-tooltip',
183
			]
184
		];
185
186
		$wgResourceModules['ext.maps.gm3.markercluster'] = [
187
			'localBasePath' => $localBasePath . '/gm3-util-library',
188
			'remoteExtPath' => $remoteExtPath . '/gm3-util-library',
189
			'group' => 'ext.maps',
190
			'targets' => [
191
				'mobile',
192
				'desktop'
193
			],
194
			'scripts' => [
195
				'markerclusterer.js',
196
			],
197
		];
198
199
		$wgResourceModules['ext.maps.gm3.markerwithlabel'] = [
200
			'localBasePath' => $localBasePath . '/gm3-util-library',
201
			'remoteExtPath' => $remoteExtPath  . '/gm3-util-library',
202
			'group' => 'ext.maps',
203
			'targets' => [
204
				'mobile',
205
				'desktop'
206
			],
207
			'scripts' => [
208
				'markerwithlabel.js',
209
			],
210
			'styles' => [
211
				'markerwithlabel.css',
212
			],
213
		];
214
215
		$wgResourceModules['ext.maps.gm3.geoxml'] = [
216
			'localBasePath' => $localBasePath . '/geoxml3',
217
			'remoteExtPath' => $remoteExtPath . '/geoxml3',
218
			'group' => 'ext.maps',
219
			'targets' => [
220
				'mobile',
221
				'desktop'
222
			],
223
			'scripts' => [
224
				'geoxml3.js',
225
				'ZipFile.complete.js', //kmz handling
226
				'ProjectedOverlay.js', //Overlay handling
227
			],
228
		];
229
230
		$wgResourceModules['ext.maps.gm3.earth'] = [
231
			'localBasePath' => $localBasePath . '/gm3-util-library',
232
			'remoteExtPath' => $remoteExtPath  . '/gm3-util-library',
233
			'group' => 'ext.maps',
234
			'targets' => [
235
				'mobile',
236
				'desktop'
237
			],
238
			'scripts' => [
239
				'googleearth-compiled.js',
240
			],
241
		];
242
243
		$wgResourceModules['ext.sm.googlemaps3ajax'] = [
244
			'localBasePath' => $localBasePath,
245
			'remoteExtPath' => $remoteExtPath,
246
			'group' => 'ext.maps',
247
			'dependencies' => [
248
				'ext.maps.googlemaps3',
249
				'ext.sm.common'
250
			],
251
			'scripts' => [
252
				'ext.sm.googlemaps3ajax.js'
253
			]
254
		];
255
	}
256
257
	private function registerLeafletModules( string $localBasePath, string $remoteExtPath ) {
258
		global $wgResourceModules;
259
260
		$localBasePath = $localBasePath . '/leaflet';
261
		$remoteExtPath = $remoteExtPath . '/leaflet';
262
263
		$wgResourceModules['ext.maps.leaflet.base'] = [
264
			'localBasePath' => $localBasePath . '/leaflet',
265
			'remoteExtPath' => $remoteExtPath . '/leaflet',
266
			'group' => 'ext.maps',
267
			'targets' => [
268
				'mobile',
269
				'desktop'
270
			],
271
			'scripts' => [
272
				'leaflet.js',
273
			],
274
			'styles' => [
275
				'leaflet.css',
276
			],
277
		];
278
279
		$wgResourceModules['ext.maps.leaflet'] = [
280
			'dependencies' => [
281
				'ext.maps.common',
282
				'ext.maps.services',
283
				'ext.maps.leaflet.base'
284
			],
285
			'localBasePath' => $localBasePath,
286
			'remoteExtPath' => $remoteExtPath,
287
			'group' => 'ext.maps',
288
			'targets' => [
289
				'mobile',
290
				'desktop'
291
			],
292
			'scripts' => [
293
				'jquery.leaflet.js',
294
				'ext.maps.leaflet.js',
295
			],
296
			'messages' => [
297
				'maps-markers',
298
				'maps-copycoords-prompt',
299
				'maps-searchmarkers-text',
300
			],
301
		];
302
303
		$wgResourceModules['ext.maps.leaflet.fullscreen'] = [
304
			'dependencies' => [ 'ext.maps.leaflet' ],
305
			'localBasePath' => $localBasePath . '/leaflet.fullscreen',
306
			'remoteExtPath' => $remoteExtPath . '/leaflet.fullscreen',
307
			'group' => 'ext.maps',
308
			'targets' => [
309
				'mobile',
310
				'desktop'
311
			],
312
			'scripts' => [
313
				'Control.FullScreen.js',
314
			],
315
			'styles' => [
316
				'Control.FullScreen.css',
317
			],
318
		];
319
320
		$wgResourceModules['ext.maps.leaflet.markercluster'] = [
321
			'dependencies' => [ 'ext.maps.leaflet' ],
322
			'localBasePath' => $localBasePath . '/leaflet.markercluster',
323
			'remoteExtPath' => $remoteExtPath . '/leaflet.markercluster',
324
			'group' => 'ext.maps',
325
			'targets' => [
326
				'mobile',
327
				'desktop'
328
			],
329
			'scripts' => [
330
				'leaflet.markercluster.js',
331
			],
332
			'styles' => [
333
				'MarkerCluster.css',
334
			],
335
		];
336
337
		$wgResourceModules['ext.maps.leaflet.providers'] = [
338
			'dependencies' => [ 'ext.maps.leaflet' ],
339
			'localBasePath' => $localBasePath . '/leaflet-providers',
340
			'remoteExtPath' => $remoteExtPath . '/leaflet-providers',
341
			'group' => 'ext.maps',
342
			'targets' => [
343
				'mobile',
344
				'desktop'
345
			],
346
			'scripts' => [
347
				'leaflet-providers.js',
348
			],
349
		];
350
351
		$wgResourceModules['ext.maps.leaflet.editable'] = [
352
			'dependencies' => [ 'ext.maps.leaflet.base' ],
353
			'localBasePath' => $localBasePath . '/leaflet.editable',
354
			'remoteExtPath' => $remoteExtPath . '/leaflet.editable',
355
			'group' => 'ext.maps',
356
			'targets' => [
357
				'mobile',
358
				'desktop'
359
			],
360
			'scripts' => [
361
				'Leaflet.Editable.js',
362
			],
363
		];
364
365
		$wgResourceModules['ext.maps.leaflet.editor'] = [
366
			'dependencies' => [
367
				'ext.maps.leaflet.base',
368
				//'ext.maps.leaflet.editable'
369
			],
370
			'localBasePath' => $localBasePath,
371
			'remoteExtPath' => $remoteExtPath,
372
			'group' => 'ext.maps',
373
			'targets' => [
374
				'mobile',
375
				'desktop'
376
			],
377
			'scripts' => [
378
				'leaflet.editor.js',
379
			],
380
		];
381
382
		$wgResourceModules['ext.sm.leafletajax'] = [
383
			'localBasePath' => $localBasePath,
384
			'remoteExtPath' => $remoteExtPath,
385
			'group' => 'ext.maps',
386
			'dependencies' => [
387
				'ext.maps.leaflet',
388
				'ext.sm.common'
389
			],
390
			'scripts' => [
391
				'ext.sm.leafletajax.js'
392
			]
393
		];
394
	}
395
396
	private function registerPermissions() {
397
		$this->mwGlobals['wgAvailableRights'][] = 'geocode';
398
399
		// Users that can geocode. By default the same as those that can edit.
400
		foreach ( $this->mwGlobals['wgGroupPermissions'] as $group => $rights ) {
401
			if ( array_key_exists( 'edit', $rights ) ) {
402
				$this->mwGlobals['wgGroupPermissions'][$group]['geocode'] = $this->mwGlobals['wgGroupPermissions'][$group]['edit'];
403
			}
404
		}
405
	}
406
407
	private function registerParameterTypes() {
408
		$this->mwGlobals['wgParamDefinitions']['coordinate'] = [
409
			'string-parser' => LatLongParser::class,
410
		];
411
412
		$this->mwGlobals['wgParamDefinitions']['mapslocation'] = [
413
			'string-parser' => LocationParser::class,
414
		];
415
416
		$this->mwGlobals['wgParamDefinitions']['mapsline'] = [
417
			'string-parser' => LineParser::class,
418
		];
419
420
		$this->mwGlobals['wgParamDefinitions']['mapscircle'] = [
421
			'string-parser' => CircleParser::class,
422
		];
423
424
		$this->mwGlobals['wgParamDefinitions']['mapsrectangle'] = [
425
			'string-parser' => RectangleParser::class,
426
		];
427
428
		$this->mwGlobals['wgParamDefinitions']['mapspolygon'] = [
429
			'string-parser' => PolygonParser::class,
430
		];
431
432
		$this->mwGlobals['wgParamDefinitions']['distance'] = [
433
			'string-parser' => DistanceParser::class,
434
		];
435
436
		$this->mwGlobals['wgParamDefinitions']['wmsoverlay'] = [
437
			'string-parser' => WmsOverlayParser::class,
438
		];
439
440
		$this->mwGlobals['wgParamDefinitions']['mapsimageoverlay'] = [
441
			'string-parser' => ImageOverlayParser::class,
442
		];
443
444
		$this->mwGlobals['wgParamDefinitions']['jsonfile'] = [
445
			'string-parser' => JsonFileParser::class,
446
		];
447
	}
448
449
	private function registerHooks() {
450
		$this->mwGlobals['wgHooks']['AdminLinks'][] = 'Maps\MediaWiki\MapsHooks::addToAdminLinks';
451
		$this->mwGlobals['wgHooks']['MakeGlobalVariablesScript'][] = 'Maps\MediaWiki\MapsHooks::onMakeGlobalVariablesScript';
452
	}
453
454
}
455