Completed
Push — master ( a7a34c...1e3be1 )
by Jeroen De
03:21
created

MapsSetup::setup()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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