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