Completed
Push — move ( 04f5b5...ed2c91 )
by Jeroen De
17:58 queued 15:41
created

MapsSetup   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 34.78%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 11
dl 0
loc 215
ccs 40
cts 115
cp 0.3478
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMappingServices() 0 28 1
A __construct() 0 3 1
A setup() 0 8 3
A registerAllTheThings() 0 11 1
A defaultSettings() 0 14 5
A registerWebResources() 0 6 1
B registerParserHooks() 0 71 4
A registerPermissions() 0 10 3
A registerParameterTypes() 0 41 1
A registerHooks() 0 4 1
A registerApiModules() 0 3 1
1
<?php
2
3
namespace Maps;
4
5
use DataValues\Geo\Parsers\LatLongParser;
6
use Maps\GeoJson\GeoJsonContent;
7
use Maps\GeoJson\GeoJsonContentHandler;
8
use Maps\MediaWiki\Api\Geocode;
9
use Maps\MediaWiki\ParserHooks\CoordinatesFunction;
10
use Maps\MediaWiki\ParserHooks\DisplayMapFunction;
11
use Maps\MediaWiki\ParserHooks\DistanceFunction;
12
use Maps\MediaWiki\ParserHooks\FindDestinationFunction;
13
use Maps\MediaWiki\ParserHooks\GeocodeFunction;
14
use Maps\MediaWiki\ParserHooks\GeoDistanceFunction;
15
use Maps\MediaWiki\ParserHooks\MapsDocFunction;
16
use Maps\Parsers\JsonFileParser;
17
use Maps\Presentation\WikitextParsers\CircleParser;
18
use Maps\Presentation\WikitextParsers\DistanceParser;
19
use Maps\Presentation\WikitextParsers\ImageOverlayParser;
20
use Maps\Presentation\WikitextParsers\LineParser;
21
use Maps\Presentation\WikitextParsers\LocationParser;
22
use Maps\Presentation\WikitextParsers\PolygonParser;
23
use Maps\Presentation\WikitextParsers\RectangleParser;
24
use Maps\Presentation\WikitextParsers\WmsOverlayParser;
25
use MapsDisplayMapRenderer;
26
use MapsGoogleMaps3;
27
use MapsLeaflet;
28
use MapsMappingServices;
29
use MapsOpenLayers;
30
use Parser;
31
use PPFrame;
32
33
/**
34
 * @licence GNU GPL v2+
35
 * @author Jeroen De Dauw < [email protected] >
36
 */
37
class MapsSetup {
38
39
	private $mwGlobals;
40
41
	public function __construct( array &$mwGlobals ) {
42
		$this->mwGlobals = $mwGlobals;
43
	}
44
45
	public function setup() {
46
		$this->defaultSettings();
47
		$this->registerAllTheThings();
48
49
		if ( !$this->mwGlobals['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ) ) {
50
			SemanticMaps::newFromMediaWikiGlobals( $this->mwGlobals )->initExtension();
51
		}
52
	}
53
54
	private function registerAllTheThings() {
55
		$this->registerWebResources();
56
		$this->registerApiModules();
57
		$this->registerParserHooks();
58
		$this->registerMappingServices();
59
		$this->registerPermissions();
60
		$this->registerParameterTypes();
61
		$this->registerHooks();
62
63
		$this->mwGlobals['wgContentHandlers'][GeoJsonContent::CONTENT_MODEL_ID] = GeoJsonContentHandler::class;
64
	}
65
66
	private function defaultSettings() {
67
		if ( $this->mwGlobals['egMapsGMaps3Language'] === '' ) {
68
			$this->mwGlobals['egMapsGMaps3Language'] = $this->mwGlobals['wgLang'];
69
		}
70
71
		if ( in_array( 'googlemaps3', $this->mwGlobals['egMapsAvailableServices'] ) ) {
72
			$this->mwGlobals['wgSpecialPages']['MapEditor'] = 'Maps\MediaWiki\Specials\SpecialMapEditor';
73
			$this->mwGlobals['wgSpecialPageGroups']['MapEditor'] = 'Maps';
74
		}
75
76
		if ( $this->mwGlobals['egMapsGMaps3ApiKey'] === '' && array_key_exists( 'egGoogleJsApiKey', $this->mwGlobals ) ) {
77
			$this->mwGlobals['egMapsGMaps3ApiKey'] = $this->mwGlobals['egGoogleJsApiKey'];
78
		}
79
	}
80
81
	private function registerWebResources() {
82
		$this->mwGlobals['wgResourceModules'] = array_merge(
83
			$this->mwGlobals['wgResourceModules'],
84
			include __DIR__ . '/../Maps.resources.php'
85
		);
86
	}
87
88
	private function registerParserHooks() {
89 48
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
90 48
			$instance = new CoordinatesFunction();
91 48
			return $instance->init( $parser );
92
		};
93
94 48
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
95 48
			foreach ( [ 'display_map', 'display_point', 'display_points', 'display_line' ] as $hookName ) {
96 48
				$parser->setFunctionHook(
97 48
					$hookName,
98 48
					function( Parser $parser, PPFrame $frame, array $arguments ) {
99 17
						$hook = new DisplayMapFunction();
100
101 17
						$mapHtml = $hook->getMapHtmlForKeyValueStrings(
102 17
							$parser,
103 17
							array_map(
104 17
								function( $argument ) use ( $frame ) {
105 17
									return $frame->expand( $argument );
106 17
								},
107 17
								$arguments
108
							)
109
						);
110
111
						return [
112 17
							$mapHtml,
113
							'noparse' => true,
114
							'isHTML' => true,
115
						];
116 48
					},
117 48
					Parser::SFH_OBJECT_ARGS
118
				);
119
120 48
				$parser->setHook(
121 48
					$hookName,
122 48
					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 ( new DisplayMapFunction() )->getMapHtmlForParameterList( $parser, $arguments );
134 48
					}
135
				);
136
			}
137 48
		};
138
139 48
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
140 48
			return ( new DistanceFunction() )->init( $parser );
141
		};
142
143 48
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
144 48
			return ( new FindDestinationFunction() )->init( $parser );
145
		};
146
147 48
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
148 48
			return ( new GeocodeFunction() )->init( $parser );
149
		};
150
151 48
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
152 48
			return ( new GeoDistanceFunction() )->init( $parser );
153
		};
154
155 48
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
156 48
			return ( new MapsDocFunction() )->init( $parser );
157
		};
158
	}
159
160
	private function registerMappingServices() {
161
		include_once __DIR__ . '/../includes/services/GoogleMaps3/GoogleMaps3.php';
162
163
		MapsMappingServices::registerService( 'googlemaps3', MapsGoogleMaps3::class );
164
165
		$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
166
		$googleMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
167
168
169
		// OpenLayers API
170
		include_once __DIR__ . '/../includes/services/OpenLayers/OpenLayers.php';
171
172
		MapsMappingServices::registerService(
173
			'openlayers',
174
			MapsOpenLayers::class
175
		);
176
177
		$openLayers = MapsMappingServices::getServiceInstance( 'openlayers' );
178
		$openLayers->addFeature( 'display_map', MapsDisplayMapRenderer::class );
179
180
181
		// Leaflet API
182
		include_once __DIR__ . '/../includes/services/Leaflet/Leaflet.php';
183
184
		MapsMappingServices::registerService( 'leaflet', MapsLeaflet::class );
185
		$leafletMaps = MapsMappingServices::getServiceInstance( 'leaflet' );
186
		$leafletMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
187
	}
188
189
	private function registerPermissions() {
190
		$this->mwGlobals['wgAvailableRights'][] = 'geocode';
191
192
		// Users that can geocode. By default the same as those that can edit.
193
		foreach ( $this->mwGlobals['wgGroupPermissions'] as $group => $rights ) {
194
			if ( array_key_exists( 'edit', $rights ) ) {
195
				$this->mwGlobals['wgGroupPermissions'][$group]['geocode'] = $this->mwGlobals['wgGroupPermissions'][$group]['edit'];
196
			}
197
		}
198
	}
199
200
	private function registerParameterTypes() {
201
		$this->mwGlobals['wgParamDefinitions']['coordinate'] = [
202
			'string-parser' => LatLongParser::class,
203
		];
204
205
		$this->mwGlobals['wgParamDefinitions']['mapslocation'] = [
206
			'string-parser' => LocationParser::class,
207
		];
208
209
		$this->mwGlobals['wgParamDefinitions']['mapsline'] = [
210
			'string-parser' => LineParser::class,
211
		];
212
213
		$this->mwGlobals['wgParamDefinitions']['mapscircle'] = [
214
			'string-parser' => CircleParser::class,
215
		];
216
217
		$this->mwGlobals['wgParamDefinitions']['mapsrectangle'] = [
218
			'string-parser' => RectangleParser::class,
219
		];
220
221
		$this->mwGlobals['wgParamDefinitions']['mapspolygon'] = [
222
			'string-parser' => PolygonParser::class,
223
		];
224
225
		$this->mwGlobals['wgParamDefinitions']['distance'] = [
226
			'string-parser' => DistanceParser::class,
227
		];
228
229
		$this->mwGlobals['wgParamDefinitions']['wmsoverlay'] = [
230
			'string-parser' => WmsOverlayParser::class,
231
		];
232
233
		$this->mwGlobals['wgParamDefinitions']['mapsimageoverlay'] = [
234
			'string-parser' => ImageOverlayParser::class,
235
		];
236
237
		$this->mwGlobals['wgParamDefinitions']['jsonfile'] = [
238
			'string-parser' => JsonFileParser::class,
239
		];
240
	}
241
242
	private function registerHooks() {
243
		$this->mwGlobals['wgHooks']['AdminLinks'][] = 'Maps\MediaWiki\MapsHooks::addToAdminLinks';
244
		$this->mwGlobals['wgHooks']['MakeGlobalVariablesScript'][] = 'Maps\MediaWiki\MapsHooks::onMakeGlobalVariablesScript';
245
	}
246
247
	private function registerApiModules() {
248
		$this->mwGlobals['wgAPIModules']['geocode'] = Geocode::class;
249
	}
250
251
}
252