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