Completed
Push — master ( af4e66...ea2b76 )
by Jeroen De
05:38
created

MapsSetup   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 8
dl 0
loc 219
ccs 35
cts 105
cp 0.3333
rs 10
c 0
b 0
f 0

10 Methods

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