Completed
Push — newparam ( 72433c...c5fad3 )
by Jeroen De
01:21
created

MapsSetup   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 36.54%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 10
dl 0
loc 201
ccs 38
cts 104
cp 0.3654
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 registerPermissions() 0 10 3
A registerParameterTypes() 0 41 1
A defaultSettings() 0 17 5
A registerAllTheThings() 0 8 1
B registerParserHooks() 0 70 5
A registerHooks() 0 10 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\JsonFileParser;
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 50
	private function registerParserHooks() {
78
		if ( $this->mwGlobals['egMapsEnableCoordinateFunction'] ) {
79 50
			$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
80 50
				return ( new CoordinatesFunction() )->init( $parser );
81
			};
82
		}
83
84 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
85 50
			foreach ( [ 'display_map', 'display_point', 'display_points', 'display_line' ] as $hookName ) {
86 50
				$parser->setFunctionHook(
87 50
					$hookName,
88 50
					function ( Parser $parser, PPFrame $frame, array $arguments ) {
89 19
						$mapHtml = MapsFactory::newDefault()->getDisplayMapFunction()->getMapHtmlForKeyValueStrings(
90 19
							$parser,
91 19
							array_map(
92 19
								function ( $argument ) use ( $frame ) {
93 19
									return $frame->expand( $argument );
94 19
								},
95
								$arguments
96
							)
97
						);
98
99
						return [
100 19
							$mapHtml,
101
							'noparse' => true,
102
							'isHTML' => true,
103
						];
104 50
					},
105 50
					Parser::SFH_OBJECT_ARGS
106
				);
107
108 50
				$parser->setHook(
109 50
					$hookName,
110 50
					function ( $text, array $arguments, Parser $parser ) {
111 2
						if ( $text !== null ) {
112 2
							$defaultParameters = DisplayMapFunction::getHookDefinition( "\n" )->getDefaultParameters();
113 2
							$defaultParam = array_shift( $defaultParameters );
114
115
							// If there is a first default parameter, set the tag contents as its value.
116 2
							if ( $defaultParam !== null ) {
117 2
								$arguments[$defaultParam] = $text;
118
							}
119
						}
120
121 2
						return MapsFactory::newDefault()->getDisplayMapFunction()->getMapHtmlForParameterList( $parser, $arguments );
122 50
					}
123
				);
124
			}
125 50
		};
126
127 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
128 50
			return ( new DistanceFunction() )->init( $parser );
129
		};
130
131 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
132 50
			return ( new FindDestinationFunction() )->init( $parser );
133
		};
134
135 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
136 50
			return ( new GeocodeFunction() )->init( $parser );
137
		};
138
139 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
140 50
			return ( new GeoDistanceFunction() )->init( $parser );
141
		};
142
143 50
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
144 50
			return ( new MapsDocFunction() )->init( $parser );
145
		};
146
	}
147
148
	private function registerPermissions() {
149
		$this->mwGlobals['wgAvailableRights'][] = 'geocode';
150
151
		// Users that can geocode. By default the same as those that can edit.
152
		foreach ( $this->mwGlobals['wgGroupPermissions'] as $group => $rights ) {
153
			if ( array_key_exists( 'edit', $rights ) ) {
154
				$this->mwGlobals['wgGroupPermissions'][$group]['geocode'] = $this->mwGlobals['wgGroupPermissions'][$group]['edit'];
155
			}
156
		}
157
	}
158
159
	private function registerParameterTypes() {
160
		$this->mwGlobals['wgParamDefinitions']['coordinate'] = [
161
			'string-parser' => LatLongParser::class,
162
		];
163
164
		$this->mwGlobals['wgParamDefinitions']['mapslocation'] = [
165
			'string-parser' => LocationParser::class,
166
		];
167
168
		$this->mwGlobals['wgParamDefinitions']['mapsline'] = [
169
			'string-parser' => LineParser::class,
170
		];
171
172
		$this->mwGlobals['wgParamDefinitions']['mapscircle'] = [
173
			'string-parser' => CircleParser::class,
174
		];
175
176
		$this->mwGlobals['wgParamDefinitions']['mapsrectangle'] = [
177
			'string-parser' => RectangleParser::class,
178
		];
179
180
		$this->mwGlobals['wgParamDefinitions']['mapspolygon'] = [
181
			'string-parser' => PolygonParser::class,
182
		];
183
184
		$this->mwGlobals['wgParamDefinitions']['distance'] = [
185
			'string-parser' => DistanceParser::class,
186
		];
187
188
		$this->mwGlobals['wgParamDefinitions']['wmsoverlay'] = [
189
			'string-parser' => WmsOverlayParser::class,
190
		];
191
192
		$this->mwGlobals['wgParamDefinitions']['mapsimageoverlay'] = [
193
			'string-parser' => ImageOverlayParser::class,
194
		];
195
196
		$this->mwGlobals['wgParamDefinitions']['jsonfile'] = [
197
			'string-parser' => JsonFileParser::class,
198
		];
199
	}
200
201
	private function registerHooks() {
202
		$this->mwGlobals['wgHooks']['AdminLinks'][] = 'Maps\MediaWiki\MapsHooks::addToAdminLinks';
203
		$this->mwGlobals['wgHooks']['MakeGlobalVariablesScript'][] = 'Maps\MediaWiki\MapsHooks::onMakeGlobalVariablesScript';
204
		$this->mwGlobals['wgHooks']['SkinTemplateNavigation'][] = 'Maps\MediaWiki\MapsHooks::onSkinTemplateNavigation';
205
		$this->mwGlobals['wgHooks']['BeforeDisplayNoArticleText'][] = 'Maps\MediaWiki\MapsHooks::onBeforeDisplayNoArticleText';
206
		$this->mwGlobals['wgHooks']['ShowMissingArticle'][] = 'Maps\MediaWiki\MapsHooks::onShowMissingArticle';
207
		$this->mwGlobals['wgHooks']['ListDefinedTags'][] = 'Maps\MediaWiki\MapsHooks::onRegisterTags';
208
		$this->mwGlobals['wgHooks']['ChangeTagsListActive'][] = 'Maps\MediaWiki\MapsHooks::onRegisterTags';
209
		$this->mwGlobals['wgHooks']['ChangeTagsAllowedAdd'][] = 'Maps\MediaWiki\MapsHooks::onChangeTagsAllowedAdd';
210
	}
211
212
	private function registerGeoJsonContentModel() {
213
		$this->mwGlobals['wgContentHandlers'][GeoJsonContent::CONTENT_MODEL_ID] = GeoJsonContentHandler::class;
214
	}
215
216
	/**
217
	 * mediawiki.api.edit is present in 1.31 but not 1.33
218
	 * Once Maps requires MW 1.33+, this can be removed after replacing usage of mediawiki.api.edit
219
	 */
220
	private function registerEditApiModuleFallback() {
221
		if ( version_compare( MW_VERSION, '1.32', '<' ) ) {
222
			return;
223
		}
224
225
		$this->mwGlobals['wgResourceModules']['mediawiki.api.edit'] = [
226
			'dependencies' => [
227
				'mediawiki.api'
228
			],
229
			'targets' => [ 'desktop', 'mobile' ]
230
		];
231
	}
232
233
}
234