Completed
Push — master ( 36b27e...57e5b8 )
by Jeroen De
28s queued 10s
created

MapsSetup   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 430
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 22.16%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 10
dl 0
loc 430
ccs 39
cts 176
cp 0.2216
rs 10
c 0
b 0
f 0

12 Methods

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