Completed
Push — styling ( 65b228 )
by Jeroen De
04:06
created

MapsSetup   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 27.47%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 9
dl 0
loc 192
ccs 25
cts 91
cp 0.2747
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setup() 0 8 3
A defaultSettings() 0 17 5
A registerAllTheThings() 0 8 1
A registerPermissions() 0 10 3
B registerParserHooks() 0 64 4
A registerParameterTypes() 0 37 1
A registerHooks() 0 11 1
A registerGeoJsonContentModel() 0 3 1
A registerEditApiModuleFallback() 0 12 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps;
6
7
use DataValues\Geo\Parsers\LatLongParser;
8
use Maps\DataAccess\GeoJsonFetcher;
9
use Maps\MediaWiki\Content\GeoJsonContent;
10
use Maps\MediaWiki\Content\GeoJsonContentHandler;
11
use Maps\MediaWiki\ParserHooks\CoordinatesFunction;
12
use Maps\MediaWiki\ParserHooks\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 ( !$this->mwGlobals['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ) ) {
46
			SemanticMaps::newFromMediaWikiGlobals( $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->registerEditApiModuleFallback();
75
	}
76
77 52
	private function registerParserHooks() {
78
		if ( $this->mwGlobals['egMapsEnableCoordinateFunction'] ) {
79
			$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
80 52
				return ( new CoordinatesFunction() )->init( $parser );
81
			};
82
		}
83
84
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
85 52
			foreach ( [ 'display_map', 'display_point', 'display_points', 'display_line' ] as $hookName ) {
86 52
				$parser->setFunctionHook(
87 52
					$hookName,
88
					function ( Parser $parser, PPFrame $frame, array $arguments ) {
89 21
						$mapHtml = MapsFactory::newDefault()->getDisplayMapFunction()->getMapHtmlForKeyValueStrings(
90 21
							$parser,
91 21
							array_map(
92
								function ( $argument ) use ( $frame ) {
93 21
									return $frame->expand( $argument );
94 21
								},
95
								$arguments
96
							)
97
						);
98
99
						return [
100 21
							$mapHtml,
101
							'noparse' => true,
102
							'isHTML' => true,
103
						];
104 52
					},
105 52
					Parser::SFH_OBJECT_ARGS
106
				);
107
108 52
				$parser->setHook(
109 52
					$hookName,
110
					function ( $text, array $arguments, Parser $parser ) {
111 2
						if ( $text !== null ) {
112 2
							$arguments[DisplayMapFunction::getDefaultParameters()[0]] = $text;
113
						}
114
115 2
						return MapsFactory::newDefault()->getDisplayMapFunction()->getMapHtmlForParameterList( $parser, $arguments );
116 52
					}
117
				);
118
			}
119 52
		};
120
121
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
122 52
			return ( new DistanceFunction() )->init( $parser );
123
		};
124
125
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
126 52
			return ( new FindDestinationFunction() )->init( $parser );
127
		};
128
129
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
130 52
			return ( new GeocodeFunction() )->init( $parser );
131
		};
132
133
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
134 52
			return ( new GeoDistanceFunction() )->init( $parser );
135
		};
136
137
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
138 52
			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
203
	private function registerGeoJsonContentModel() {
204
		$this->mwGlobals['wgContentHandlers'][GeoJsonContent::CONTENT_MODEL_ID] = GeoJsonContentHandler::class;
205
	}
206
207
	/**
208
	 * mediawiki.api.edit is present in 1.31 but not 1.33
209
	 * Once Maps requires MW 1.33+, this can be removed after replacing usage of mediawiki.api.edit
210
	 */
211
	private function registerEditApiModuleFallback() {
212
		if ( version_compare( $this->mwGlobals['wgVersion'], '1.32', '<' ) ) {
213
			return;
214
		}
215
216
		$this->mwGlobals['wgResourceModules']['mediawiki.api.edit'] = [
217
			'dependencies' => [
218
				'mediawiki.api'
219
			],
220
			'targets' => [ 'desktop', 'mobile' ]
221
		];
222
	}
223
224
}
225