Completed
Push — vishual ( da6cc4...124318 )
by Jeroen De
07:54
created

MapsSetup::foo()   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
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 0
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 static function foo(  ) {
32
		return true;
33
	}
34
35
	public function __construct( array &$mwGlobals ) {
36
		$this->mwGlobals = $mwGlobals;
37
	}
38
39
	public function setup() {
40
		$this->defaultSettings();
41
		$this->registerAllTheThings();
42
43
		if ( !$this->mwGlobals['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ) ) {
44
			SemanticMaps::newFromMediaWikiGlobals( $this->mwGlobals )->initExtension();
45
		}
46
	}
47
48
	private function registerAllTheThings() {
49
		$this->registerWebResources();
50
		$this->registerApiModules();
51
		$this->registerParserHooks();
52
		$this->registerMappingServices();
53
		$this->registerPermissions();
54
		$this->registerParameterTypes();
55
		$this->registerHooks();
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.class';
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
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
82
			$instance = new MapsCoordinates();
83
			return $instance->init( $parser );
84
		};
85
86
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
87
			foreach ( [ 'display_map', 'display_point', 'display_points', 'display_line' ] as $hookName ) {
88
				$parser->setFunctionHook(
89
					$hookName,
90
					function( Parser $parser, PPFrame $frame, array $arguments ) {
91
						$hook = new MapsDisplayMap();
92
93
						$mapHtml = $hook->getMapHtmlForKeyValueStrings(
94 26
							$parser,
95 26
							array_map(
96 26
								function( $argument ) use ( $frame ) {
97
									return $frame->expand( $argument );
98
								},
99 26
								$arguments
100 26
							)
101 26
						);
102 26
103 26
						return [
104 17
							$mapHtml,
105
							'noparse' => true,
106 17
							'isHTML' => true,
107 17
						];
108 17
					},
109 17
					Parser::SFH_OBJECT_ARGS
110 17
				);
111 17
112 17
				$parser->setHook(
113
					$hookName,
114
					function( $text, array $arguments, Parser $parser ) {
115
						if ( $text !== null ) {
116
							$defaultParameters = MapsDisplayMap::getHookDefinition( "\n" )->getDefaultParameters();
117 17
							$defaultParam = array_shift( $defaultParameters );
118
119
							// If there is a first default parameter, set the tag contents as its value.
120
							if ( $defaultParam !== null ) {
121 26
								$arguments[$defaultParam] = $text;
122 26
							}
123
						}
124
125 26
						return ( new MapsDisplayMap() )->getMapHtmlForParameterList( $parser, $arguments );
126 26
					}
127 26
				);
128 2
			}
129 2
		};
130 2
131
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
132
			return ( new MapsDistance() )->init( $parser );
133 2
		};
134 2
135
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
136
			return ( new MapsFinddestination() )->init( $parser );
137
		};
138 2
139 26
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
140
			return ( new MapsGeocode() )->init( $parser );
141
		};
142 26
143
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
144 26
			return ( new MapsGeodistance() )->init( $parser );
145 26
		};
146
147
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
148 26
			return ( new MapsMapsDoc() )->init( $parser );
149 26
		};
150
	}
151
152 26
	private function registerMappingServices() {
153 26
		include_once __DIR__ . '/../includes/services/GoogleMaps3/GoogleMaps3.php';
154
155
		MapsMappingServices::registerService( 'googlemaps3', MapsGoogleMaps3::class );
156 26
157 26
		$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
158
		$googleMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
159
160 26
161 26
		// 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