Completed
Push — master ( d813d3...42a1ca )
by Jeroen De
03:09
created

MapsSetup   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 268
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 33.91%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 10
dl 0
loc 268
ccs 39
cts 115
cp 0.3391
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setup() 0 8 3
A registerAllTheThings() 0 9 1
A defaultSettings() 0 17 5
B registerParserHooks() 0 70 5
B registerGoogleMapsModules() 0 92 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->registerParserHooks();
52
		$this->registerGoogleMapsModules();
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 registerGoogleMapsModules() {
150
		global $wgResourceModules;
151
152
		$localBasePath = __DIR__ . '/../resources/GoogleMaps';
153
		$remoteExtPath = 'Maps/resources/GoogleMaps';
154
155
		$wgResourceModules['ext.maps.googlemaps3'] = [
156
			'dependencies' => [ 'ext.maps.common' ],
157
			'localBasePath' => $localBasePath,
158
			'remoteExtPath' => $remoteExtPath,
159
			'targets' => [
160
				'mobile',
161
				'desktop'
162
			],
163
			'scripts' => [
164
				'jquery.googlemap.js',
165
				'ext.maps.googlemaps3.js'
166
			],
167
			'messages' => [
168
				'maps-googlemaps3-incompatbrowser',
169
				'maps-copycoords-prompt',
170
				'maps-searchmarkers-text',
171
				'maps-fullscreen-button',
172
				'maps-fullscreen-button-tooltip',
173
			]
174
		];
175
176
		$wgResourceModules['ext.maps.gm3.markercluster'] = [
177
			'localBasePath' => $localBasePath . '/gm3-util-library',
178
			'remoteExtPath' => $remoteExtPath . '/gm3-util-library',
179
			'targets' => [
180
				'mobile',
181
				'desktop'
182
			],
183
			'scripts' => [
184
				'markerclusterer.js',
185
			],
186
		];
187
188
		$wgResourceModules['ext.maps.gm3.markerwithlabel'] = [
189
			'localBasePath' => $localBasePath . '/gm3-util-library',
190
			'remoteExtPath' => $remoteExtPath  . '/gm3-util-library',
191
			'targets' => [
192
				'mobile',
193
				'desktop'
194
			],
195
			'scripts' => [
196
				'markerwithlabel.js',
197
			],
198
			'styles' => [
199
				'markerwithlabel.css',
200
			],
201
		];
202
203
		$wgResourceModules['ext.maps.gm3.geoxml'] = [
204
			'localBasePath' => $localBasePath . '/geoxml3',
205
			'remoteExtPath' => $remoteExtPath . '/geoxml3',
206
			'targets' => [
207
				'mobile',
208
				'desktop'
209
			],
210
			'scripts' => [
211
				'geoxml3.js',
212
				'ZipFile.complete.js', //kmz handling
213
				'ProjectedOverlay.js', //Overlay handling
214
			],
215
		];
216
217
		$wgResourceModules['ext.maps.gm3.earth'] = [
218
			'localBasePath' => $localBasePath . '/gm3-util-library',
219
			'remoteExtPath' => $remoteExtPath  . '/gm3-util-library',
220
			'targets' => [
221
				'mobile',
222
				'desktop'
223
			],
224
			'scripts' => [
225
				'googleearth-compiled.js',
226
			],
227
		];
228
229
		$wgResourceModules['ext.sm.googlemaps3ajax'] = [
230
			'localBasePath' => $localBasePath,
231
			'remoteExtPath' => $remoteExtPath,
232
			'dependencies' => [
233
				'ext.maps.googlemaps3',
234
				'ext.sm.common'
235
			],
236
			'scripts' => [
237
				'ext.sm.googlemaps3ajax.js'
238
			]
239
		];
240
	}
241
242
	private function registerPermissions() {
243
		$this->mwGlobals['wgAvailableRights'][] = 'geocode';
244
245
		// Users that can geocode. By default the same as those that can edit.
246
		foreach ( $this->mwGlobals['wgGroupPermissions'] as $group => $rights ) {
247
			if ( array_key_exists( 'edit', $rights ) ) {
248
				$this->mwGlobals['wgGroupPermissions'][$group]['geocode'] = $this->mwGlobals['wgGroupPermissions'][$group]['edit'];
249
			}
250
		}
251
	}
252
253
	private function registerParameterTypes() {
254
		$this->mwGlobals['wgParamDefinitions']['coordinate'] = [
255
			'string-parser' => LatLongParser::class,
256
		];
257
258
		$this->mwGlobals['wgParamDefinitions']['mapslocation'] = [
259
			'string-parser' => LocationParser::class,
260
		];
261
262
		$this->mwGlobals['wgParamDefinitions']['mapsline'] = [
263
			'string-parser' => LineParser::class,
264
		];
265
266
		$this->mwGlobals['wgParamDefinitions']['mapscircle'] = [
267
			'string-parser' => CircleParser::class,
268
		];
269
270
		$this->mwGlobals['wgParamDefinitions']['mapsrectangle'] = [
271
			'string-parser' => RectangleParser::class,
272
		];
273
274
		$this->mwGlobals['wgParamDefinitions']['mapspolygon'] = [
275
			'string-parser' => PolygonParser::class,
276
		];
277
278
		$this->mwGlobals['wgParamDefinitions']['distance'] = [
279
			'string-parser' => DistanceParser::class,
280
		];
281
282
		$this->mwGlobals['wgParamDefinitions']['wmsoverlay'] = [
283
			'string-parser' => WmsOverlayParser::class,
284
		];
285
286
		$this->mwGlobals['wgParamDefinitions']['mapsimageoverlay'] = [
287
			'string-parser' => ImageOverlayParser::class,
288
		];
289
290
		$this->mwGlobals['wgParamDefinitions']['jsonfile'] = [
291
			'string-parser' => JsonFileParser::class,
292
		];
293
	}
294
295
	private function registerHooks() {
296
		$this->mwGlobals['wgHooks']['AdminLinks'][] = 'Maps\MediaWiki\MapsHooks::addToAdminLinks';
297
		$this->mwGlobals['wgHooks']['MakeGlobalVariablesScript'][] = 'Maps\MediaWiki\MapsHooks::onMakeGlobalVariablesScript';
298
	}
299
300
}
301