Completed
Push — vishual ( da6cc4 )
by Jeroen De
03:55
created

MapsSetup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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