Completed
Push — geojsonsmw ( 4cff82 )
by Jeroen De
03:44
created

MapsSetup::setup()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 12
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->registerEditApiModuleFallbacks();
75
	}
76
77 53
	private function registerParserHooks() {
78
		if ( $this->mwGlobals['egMapsEnableCoordinateFunction'] ) {
79
			$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
80 53
				return ( new CoordinatesFunction() )->init( $parser );
81
			};
82
		}
83
84
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
85 53
			foreach ( [ 'display_map', 'display_point', 'display_points', 'display_line' ] as $hookName ) {
86 53
				$parser->setFunctionHook(
87 53
					$hookName,
88
					function ( Parser $parser, PPFrame $frame, array $arguments ) {
89 22
						$mapHtml = MapsFactory::newDefault()->getDisplayMapFunction()->getMapHtmlForKeyValueStrings(
90 22
							$parser,
91 22
							array_map(
92
								function ( $argument ) use ( $frame ) {
93 22
									return $frame->expand( $argument );
94 22
								},
95
								$arguments
96
							)
97
						);
98
99
						return [
100 22
							$mapHtml,
101
							'noparse' => true,
102
							'isHTML' => true,
103
						];
104 53
					},
105 53
					Parser::SFH_OBJECT_ARGS
106
				);
107
108 53
				$parser->setHook(
109 53
					$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 53
					}
117
				);
118
			}
119 53
		};
120
121
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
122 53
			return ( new DistanceFunction() )->init( $parser );
123
		};
124
125
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
126 53
			return ( new FindDestinationFunction() )->init( $parser );
127
		};
128
129
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
130 53
			return ( new GeocodeFunction() )->init( $parser );
131
		};
132
133
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
134 53
			return ( new GeoDistanceFunction() )->init( $parser );
135
		};
136
137
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
138 53
			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
		$this->mwGlobals['wgHooks']['NewRevisionFromEditComplete'][] = 'Maps\MediaWiki\MapsHooks::onNewRevisionFromEditComplete';
202
	}
203
204
	private function registerGeoJsonContentModel() {
205
		$this->mwGlobals['wgContentHandlers'][GeoJsonContent::CONTENT_MODEL_ID] = GeoJsonContentHandler::class;
206
	}
207
208
	private function registerEditApiModuleFallbacks() {
209
		// mediawiki.api.edit is present in 1.31 but not 1.32
210
		// Once Maps requires MW 1.32+, this can be removed after replacing usage of mediawiki.api.edit
211
		if ( version_compare( $this->mwGlobals['wgVersion'], '1.32', '>=' ) ) {
212
			$this->mwGlobals['wgResourceModules']['mediawiki.api.edit'] = [
213
				'dependencies' => [
214
					'mediawiki.api'
215
				],
216
				'targets' => [ 'desktop', 'mobile' ]
217
			];
218
		}
219
220
		// 1.35 combines the jquery.ui modules into one
221
		if ( version_compare( $this->mwGlobals['wgVersion'], '1.35', '>=' ) ) {
222
			$this->mwGlobals['wgResourceModules']['jquery.ui.resizable'] = [
223
				'dependencies' => [
224
					'jquery.ui'
225
				],
226
				'targets' => [ 'desktop', 'mobile' ]
227
			];
228
229
			$this->mwGlobals['wgResourceModules']['jquery.ui.autocomplete'] = [
230
				'dependencies' => [
231
					'jquery.ui'
232
				],
233
				'targets' => [ 'desktop', 'mobile' ]
234
			];
235
236
			$this->mwGlobals['wgResourceModules']['jquery.ui.slider'] = [
237
				'dependencies' => [
238
					'jquery.ui'
239
				],
240
				'targets' => [ 'desktop', 'mobile' ]
241
			];
242
243
			$this->mwGlobals['wgResourceModules']['jquery.ui.dialog'] = [
244
				'dependencies' => [
245
					'jquery.ui'
246
				],
247
				'targets' => [ 'desktop', 'mobile' ]
248
			];
249
		}
250
	}
251
252
}
253