Completed
Push — master ( 4427e6...cf958e )
by Jeroen De
03:32 queued 11s
created

MapsSetup::registerParserHooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1.0156
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps;
6
7
use DataValues\Geo\Parsers\LatLongParser;
8
use Maps\GeoJsonPages\GeoJsonContent;
9
use Maps\GeoJsonPages\GeoJsonContentHandler;
10
use Maps\LegacyMapEditor\SpecialMapEditor;
11
use Maps\Map\CargoFormat\CargoFormat;
12
use Maps\Map\DisplayMap\DisplayMapFunction;
13
use Maps\ParserHooks\CoordinatesFunction;
14
use Maps\ParserHooks\DistanceFunction;
15
use Maps\ParserHooks\FindDestinationFunction;
16
use Maps\ParserHooks\GeocodeFunction;
17
use Maps\ParserHooks\GeoDistanceFunction;
18
use Maps\ParserHooks\MapsDocFunction;
19
use Maps\WikitextParsers\CircleParser;
20
use Maps\WikitextParsers\DistanceParser;
21
use Maps\WikitextParsers\ImageOverlayParser;
22
use Maps\WikitextParsers\LineParser;
23
use Maps\WikitextParsers\LocationParser;
24
use Maps\WikitextParsers\PolygonParser;
25
use Maps\WikitextParsers\RectangleParser;
26
use Maps\WikitextParsers\WmsOverlayParser;
27
use Parser;
28
use PPFrame;
29
30
/**
31
 * @licence GNU GPL v2+
32
 * @author Jeroen De Dauw < [email protected] >
33
 */
34
class MapsSetup {
35
36
	private $mwGlobals;
37
38
	public function __construct( array &$mwGlobals ) {
39
		$this->mwGlobals = $mwGlobals;
40
	}
41
42
	public function setup() {
43
		$this->defaultSettings();
44
		$this->registerAllTheThings();
45
46
		if ( MapsFactory::globalInstance()->smwIntegrationIsEnabled() ) {
47
			MapsFactory::globalInstance()->newSemanticMapsSetup( $this->mwGlobals )->initExtension();
48
		}
49
	}
50
51
	private function defaultSettings() {
52
		if ( $this->mwGlobals['egMapsGMaps3Language'] === '' ) {
53
			$this->mwGlobals['egMapsGMaps3Language'] = $this->mwGlobals['wgLang'];
54
		}
55
56
		if ( in_array( 'googlemaps3', $this->mwGlobals['egMapsAvailableServices'] ) ) {
57
			$this->mwGlobals['wgSpecialPages']['MapEditor'] = SpecialMapEditor::class;
58
			$this->mwGlobals['wgSpecialPageGroups']['MapEditor'] = 'maps';
59
		}
60
61
		if ( $this->mwGlobals['egMapsGMaps3ApiKey'] === '' && array_key_exists(
62
				'egGoogleJsApiKey',
63
				$this->mwGlobals
64
			) ) {
65
			$this->mwGlobals['egMapsGMaps3ApiKey'] = $this->mwGlobals['egGoogleJsApiKey'];
66
		}
67
	}
68
69
	private function registerAllTheThings() {
70
		$this->registerParserHooks();
71
		$this->registerPermissions();
72
		$this->registerParameterTypes();
73
		$this->registerHooks();
74
		$this->registerGeoJsonContentModel();
75
		$this->registerEditApiModuleFallbacks();
76
	}
77
78
	private function registerParserHooks() {
79 1
		$this->mwGlobals['wgHooks']['ParserFirstCallInit'][] = function ( Parser $parser ) {
80 1
			MapsFactory::globalInstance()->newParserHookSetup( $parser )->registerParserHooks();
81 1
		};
82
	}
83
84
	private function registerPermissions() {
85
		$this->mwGlobals['wgAvailableRights'][] = 'geocode';
86
87
		// Users that can geocode. By default the same as those that can edit.
88
		foreach ( $this->mwGlobals['wgGroupPermissions'] as $group => $rights ) {
89
			if ( array_key_exists( 'edit', $rights ) ) {
90
				$this->mwGlobals['wgGroupPermissions'][$group]['geocode'] = $this->mwGlobals['wgGroupPermissions'][$group]['edit'];
91
			}
92
		}
93
	}
94
95
	private function registerParameterTypes() {
96
		$this->mwGlobals['wgParamDefinitions']['coordinate'] = [
97
			'string-parser' => LatLongParser::class,
98
		];
99
100
		$this->mwGlobals['wgParamDefinitions']['mapslocation'] = [
101
			'string-parser' => LocationParser::class,
102
		];
103
104
		$this->mwGlobals['wgParamDefinitions']['mapsline'] = [
105
			'string-parser' => LineParser::class,
106
		];
107
108
		$this->mwGlobals['wgParamDefinitions']['mapscircle'] = [
109
			'string-parser' => CircleParser::class,
110
		];
111
112
		$this->mwGlobals['wgParamDefinitions']['mapsrectangle'] = [
113
			'string-parser' => RectangleParser::class,
114
		];
115
116
		$this->mwGlobals['wgParamDefinitions']['mapspolygon'] = [
117
			'string-parser' => PolygonParser::class,
118
		];
119
120
		$this->mwGlobals['wgParamDefinitions']['distance'] = [
121
			'string-parser' => DistanceParser::class,
122
		];
123
124
		$this->mwGlobals['wgParamDefinitions']['wmsoverlay'] = [
125
			'string-parser' => WmsOverlayParser::class,
126
		];
127
128
		$this->mwGlobals['wgParamDefinitions']['mapsimageoverlay'] = [
129
			'string-parser' => ImageOverlayParser::class,
130
		];
131
	}
132
133
	private function registerHooks() {
134
		$this->mwGlobals['wgHooks']['AdminLinks'][] = 'Maps\MapsHooks::addToAdminLinks';
135
		$this->mwGlobals['wgHooks']['MakeGlobalVariablesScript'][] = 'Maps\MapsHooks::onMakeGlobalVariablesScript';
136
		$this->mwGlobals['wgHooks']['SkinTemplateNavigation'][] = 'Maps\MapsHooks::onSkinTemplateNavigation';
137
		$this->mwGlobals['wgHooks']['BeforeDisplayNoArticleText'][] = 'Maps\MapsHooks::onBeforeDisplayNoArticleText';
138
		$this->mwGlobals['wgHooks']['ShowMissingArticle'][] = 'Maps\MapsHooks::onShowMissingArticle';
139
		$this->mwGlobals['wgHooks']['ListDefinedTags'][] = 'Maps\MapsHooks::onRegisterTags';
140
		$this->mwGlobals['wgHooks']['ChangeTagsListActive'][] = 'Maps\MapsHooks::onRegisterTags';
141
		$this->mwGlobals['wgHooks']['ChangeTagsAllowedAdd'][] = 'Maps\MapsHooks::onChangeTagsAllowedAdd';
142
		$this->mwGlobals['wgHooks']['ResourceLoaderTestModules'][] = 'Maps\MapsHooks::onResourceLoaderTestModules';
143
144
		$this->mwGlobals['wgHooks']['CargoSetFormatClasses'][] = function( array &$formatClasses ) {
145
			$formatClasses['map'] = CargoFormat::class;
146
		};
147
	}
148
149
	private function registerGeoJsonContentModel() {
150
		$this->mwGlobals['wgContentHandlers'][GeoJsonContent::CONTENT_MODEL_ID] = GeoJsonContentHandler::class;
151
	}
152
153
	private function registerEditApiModuleFallbacks() {
154
		// mediawiki.api.edit is present in 1.31 but not 1.32
155
		// Once Maps requires MW 1.32+, this can be removed after replacing usage of mediawiki.api.edit
156
		if ( version_compare( $this->mwGlobals['wgVersion'], '1.32', '>=' ) ) {
157
			$this->mwGlobals['wgResourceModules']['mediawiki.api.edit'] = [
158
				'dependencies' => [
159
					'mediawiki.api'
160
				],
161
				'targets' => [ 'desktop', 'mobile' ]
162
			];
163
		}
164
165
		// 1.35 combines the jquery.ui modules into one
166
		if ( version_compare( $this->mwGlobals['wgVersion'], '1.35', '>=' ) ) {
167
			$this->mwGlobals['wgResourceModules']['jquery.ui.resizable'] = [
168
				'dependencies' => [
169
					'jquery.ui'
170
				],
171
				'targets' => [ 'desktop', 'mobile' ]
172
			];
173
174
			$this->mwGlobals['wgResourceModules']['jquery.ui.autocomplete'] = [
175
				'dependencies' => [
176
					'jquery.ui'
177
				],
178
				'targets' => [ 'desktop', 'mobile' ]
179
			];
180
181
			$this->mwGlobals['wgResourceModules']['jquery.ui.slider'] = [
182
				'dependencies' => [
183
					'jquery.ui'
184
				],
185
				'targets' => [ 'desktop', 'mobile' ]
186
			];
187
188
			$this->mwGlobals['wgResourceModules']['jquery.ui.dialog'] = [
189
				'dependencies' => [
190
					'jquery.ui'
191
				],
192
				'targets' => [ 'desktop', 'mobile' ]
193
			];
194
		}
195
	}
196
197
}
198