Completed
Push — master ( 4d0076...81538e )
by Jeroen De
26s queued 11s
created

MapsSetup   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 32.41%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 8
dl 0
loc 223
ccs 35
cts 108
cp 0.3241
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A defaultSettings() 0 17 5
A registerAllTheThings() 0 8 1
A registerGeoJsonContentModel() 0 3 1
B registerParserHooks() 0 64 4
A registerPermissions() 0 10 3
A registerParameterTypes() 0 37 1
A setup() 0 8 2
A registerHooks() 0 15 1
A registerEditApiModuleFallbacks() 0 43 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps;
6
7
use DataValues\Geo\Parsers\LatLongParser;
8
use Maps\Map\CargoFormat\CargoFormat;
9
use Maps\MediaWiki\Content\GeoJsonContent;
10
use Maps\MediaWiki\Content\GeoJsonContentHandler;
11
use Maps\MediaWiki\ParserHooks\CoordinatesFunction;
12
use Maps\Map\DisplayMap\DisplayMapFunction;
13
use Maps\MediaWiki\ParserHooks\DistanceFunction;
14
use Maps\MediaWiki\ParserHooks\FindDestinationFunction;
15
use Maps\MediaWiki\ParserHooks\GeocodeFunction;
16
use Maps\MediaWiki\ParserHooks\GeoDistanceFunction;
17
use Maps\MediaWiki\ParserHooks\MapsDocFunction;
18
use Maps\Presentation\WikitextParsers\CircleParser;
19
use Maps\Presentation\WikitextParsers\DistanceParser;
20
use Maps\Presentation\WikitextParsers\ImageOverlayParser;
21
use Maps\Presentation\WikitextParsers\LineParser;
22
use Maps\Presentation\WikitextParsers\LocationParser;
23
use Maps\Presentation\WikitextParsers\PolygonParser;
24
use Maps\Presentation\WikitextParsers\RectangleParser;
25
use Maps\Presentation\WikitextParsers\WmsOverlayParser;
26
use Parser;
27
use PPFrame;
28
29
/**
30
 * @licence GNU GPL v2+
31
 * @author Jeroen De Dauw < [email protected] >
32
 */
33
class MapsSetup {
34
35
	private $mwGlobals;
36
37
	public function __construct( array &$mwGlobals ) {
38
		$this->mwGlobals = $mwGlobals;
39
	}
40
41
	public function setup() {
42
		$this->defaultSettings();
43
		$this->registerAllTheThings();
44
45
		if ( MapsFactory::globalInstance()->smwIntegrationIsEnabled() ) {
46
			MapsFactory::globalInstance()->newSemanticMapsSetup( $this->mwGlobals )->initExtension();
47
		}
48
	}
49
50
	private function defaultSettings() {
51
		if ( $this->mwGlobals['egMapsGMaps3Language'] === '' ) {
52
			$this->mwGlobals['egMapsGMaps3Language'] = $this->mwGlobals['wgLang'];
53
		}
54
55
		if ( in_array( 'googlemaps3', $this->mwGlobals['egMapsAvailableServices'] ) ) {
56
			$this->mwGlobals['wgSpecialPages']['MapEditor'] = 'Maps\MediaWiki\Specials\SpecialMapEditor';
57
			$this->mwGlobals['wgSpecialPageGroups']['MapEditor'] = 'maps';
58
		}
59
60
		if ( $this->mwGlobals['egMapsGMaps3ApiKey'] === '' && array_key_exists(
61
				'egGoogleJsApiKey',
62
				$this->mwGlobals
63
			) ) {
64
			$this->mwGlobals['egMapsGMaps3ApiKey'] = $this->mwGlobals['egGoogleJsApiKey'];
65
		}
66
	}
67
68
	private function registerAllTheThings() {
69
		$this->registerParserHooks();
70
		$this->registerPermissions();
71
		$this->registerParameterTypes();
72
		$this->registerHooks();
73
		$this->registerGeoJsonContentModel();
74
		$this->registerEditApiModuleFallbacks();
75
	}
76
77 56
	private function registerParserHooks() {
78
		if ( $this->mwGlobals['egMapsEnableCoordinateFunction'] ) {
79 56
			$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
80 56
				return ( new CoordinatesFunction() )->init( $parser );
81
			};
82
		}
83
84 56
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
85 56
			foreach ( [ 'display_map', 'display_point', 'display_points', 'display_line' ] as $hookName ) {
86 56
				$parser->setFunctionHook(
87 56
					$hookName,
88 56
					function ( Parser $parser, PPFrame $frame, array $arguments ) {
89 25
						$mapHtml = MapsFactory::globalInstance()->getDisplayMapFunction()->getMapHtmlForKeyValueStrings(
90 25
							$parser,
91 25
							array_map(
92 25
								function ( $argument ) use ( $frame ) {
93 25
									return $frame->expand( $argument );
94 25
								},
95
								$arguments
96
							)
97
						);
98
99
						return [
100 25
							$mapHtml,
101
							'noparse' => true,
102
							'isHTML' => true,
103
						];
104 56
					},
105 56
					Parser::SFH_OBJECT_ARGS
106
				);
107
108 56
				$parser->setHook(
109 56
					$hookName,
110 56
					function ( $text, array $arguments, Parser $parser ) {
111 2
						if ( $text !== null ) {
112 2
							$arguments[DisplayMapFunction::getDefaultParameters()[0]] = $text;
113
						}
114
115 2
						return MapsFactory::globalInstance()->getDisplayMapFunction()->getMapHtmlForParameterList( $parser, $arguments );
116 56
					}
117
				);
118
			}
119 56
		};
120
121 56
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
122 56
			return ( new DistanceFunction() )->init( $parser );
123
		};
124
125 56
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
126 56
			return ( new FindDestinationFunction() )->init( $parser );
127
		};
128
129 56
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
130 56
			return ( new GeocodeFunction() )->init( $parser );
131
		};
132
133 56
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
134 56
			return ( new GeoDistanceFunction() )->init( $parser );
135
		};
136
137 56
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
138 56
			return ( new MapsDocFunction() )->init( $parser );
139
		};
140
	}
141
142
	private function registerPermissions() {
143
		$this->mwGlobals['wgAvailableRights'][] = 'geocode';
144
145
		// Users that can geocode. By default the same as those that can edit.
146
		foreach ( $this->mwGlobals['wgGroupPermissions'] as $group => $rights ) {
147
			if ( array_key_exists( 'edit', $rights ) ) {
148
				$this->mwGlobals['wgGroupPermissions'][$group]['geocode'] = $this->mwGlobals['wgGroupPermissions'][$group]['edit'];
149
			}
150
		}
151
	}
152
153
	private function registerParameterTypes() {
154
		$this->mwGlobals['wgParamDefinitions']['coordinate'] = [
155
			'string-parser' => LatLongParser::class,
156
		];
157
158
		$this->mwGlobals['wgParamDefinitions']['mapslocation'] = [
159
			'string-parser' => LocationParser::class,
160
		];
161
162
		$this->mwGlobals['wgParamDefinitions']['mapsline'] = [
163
			'string-parser' => LineParser::class,
164
		];
165
166
		$this->mwGlobals['wgParamDefinitions']['mapscircle'] = [
167
			'string-parser' => CircleParser::class,
168
		];
169
170
		$this->mwGlobals['wgParamDefinitions']['mapsrectangle'] = [
171
			'string-parser' => RectangleParser::class,
172
		];
173
174
		$this->mwGlobals['wgParamDefinitions']['mapspolygon'] = [
175
			'string-parser' => PolygonParser::class,
176
		];
177
178
		$this->mwGlobals['wgParamDefinitions']['distance'] = [
179
			'string-parser' => DistanceParser::class,
180
		];
181
182
		$this->mwGlobals['wgParamDefinitions']['wmsoverlay'] = [
183
			'string-parser' => WmsOverlayParser::class,
184
		];
185
186
		$this->mwGlobals['wgParamDefinitions']['mapsimageoverlay'] = [
187
			'string-parser' => ImageOverlayParser::class,
188
		];
189
	}
190
191
	private function registerHooks() {
192
		$this->mwGlobals['wgHooks']['AdminLinks'][] = 'Maps\MediaWiki\MapsHooks::addToAdminLinks';
193
		$this->mwGlobals['wgHooks']['MakeGlobalVariablesScript'][] = 'Maps\MediaWiki\MapsHooks::onMakeGlobalVariablesScript';
194
		$this->mwGlobals['wgHooks']['SkinTemplateNavigation'][] = 'Maps\MediaWiki\MapsHooks::onSkinTemplateNavigation';
195
		$this->mwGlobals['wgHooks']['BeforeDisplayNoArticleText'][] = 'Maps\MediaWiki\MapsHooks::onBeforeDisplayNoArticleText';
196
		$this->mwGlobals['wgHooks']['ShowMissingArticle'][] = 'Maps\MediaWiki\MapsHooks::onShowMissingArticle';
197
		$this->mwGlobals['wgHooks']['ListDefinedTags'][] = 'Maps\MediaWiki\MapsHooks::onRegisterTags';
198
		$this->mwGlobals['wgHooks']['ChangeTagsListActive'][] = 'Maps\MediaWiki\MapsHooks::onRegisterTags';
199
		$this->mwGlobals['wgHooks']['ChangeTagsAllowedAdd'][] = 'Maps\MediaWiki\MapsHooks::onChangeTagsAllowedAdd';
200
		$this->mwGlobals['wgHooks']['ResourceLoaderTestModules'][] = 'Maps\MediaWiki\MapsHooks::onResourceLoaderTestModules';
201
202
		$this->mwGlobals['wgHooks']['CargoSetFormatClasses'][] = function( array &$formatClasses ) {
203
			$formatClasses['map'] = CargoFormat::class;
204
		};
205
	}
206
207
	private function registerGeoJsonContentModel() {
208
		$this->mwGlobals['wgContentHandlers'][GeoJsonContent::CONTENT_MODEL_ID] = GeoJsonContentHandler::class;
209
	}
210
211
	private function registerEditApiModuleFallbacks() {
212
		// mediawiki.api.edit is present in 1.31 but not 1.32
213
		// Once Maps requires MW 1.32+, this can be removed after replacing usage of mediawiki.api.edit
214
		if ( version_compare( $this->mwGlobals['wgVersion'], '1.32', '>=' ) ) {
215
			$this->mwGlobals['wgResourceModules']['mediawiki.api.edit'] = [
216
				'dependencies' => [
217
					'mediawiki.api'
218
				],
219
				'targets' => [ 'desktop', 'mobile' ]
220
			];
221
		}
222
223
		// 1.35 combines the jquery.ui modules into one
224
		if ( version_compare( $this->mwGlobals['wgVersion'], '1.35', '>=' ) ) {
225
			$this->mwGlobals['wgResourceModules']['jquery.ui.resizable'] = [
226
				'dependencies' => [
227
					'jquery.ui'
228
				],
229
				'targets' => [ 'desktop', 'mobile' ]
230
			];
231
232
			$this->mwGlobals['wgResourceModules']['jquery.ui.autocomplete'] = [
233
				'dependencies' => [
234
					'jquery.ui'
235
				],
236
				'targets' => [ 'desktop', 'mobile' ]
237
			];
238
239
			$this->mwGlobals['wgResourceModules']['jquery.ui.slider'] = [
240
				'dependencies' => [
241
					'jquery.ui'
242
				],
243
				'targets' => [ 'desktop', 'mobile' ]
244
			];
245
246
			$this->mwGlobals['wgResourceModules']['jquery.ui.dialog'] = [
247
				'dependencies' => [
248
					'jquery.ui'
249
				],
250
				'targets' => [ 'desktop', 'mobile' ]
251
			];
252
		}
253
	}
254
255
}
256