Completed
Push — clean ( fb88a2...60e622 )
by Jeroen De
03:29
created

Maps.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Initialization file for the Maps extension.
5
 *
6
 * @links https://github.com/JeroenDeDauw/Maps/blob/master/README.md#maps Documentation
7
 * @links https://github.com/JeroenDeDauw/Maps/issues Support
8
 * @links https://github.com/JeroenDeDauw/Maps Source code
9
 *
10
 * @license https://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
14
use DataValues\Geo\Parsers\LatLongParser;
15
use Maps\CircleParser;
16
use Maps\DistanceParser;
17
use Maps\ImageOverlayParser;
18
use Maps\LineParser;
19
use Maps\LocationParser;
20
use Maps\PolygonParser;
21
use Maps\RectangleParser;
22
use Maps\SemanticMaps;
23
use Maps\WmsOverlayParser;
24
use ParserHooks\FunctionRunner;
25
use ParserHooks\HookRegistrant;
26
use ParserHooks\HookRunner;
27
28
if ( defined( 'Maps_COORDS_FLOAT' ) ) {
29
	// Do not initialize more than once.
30
	return 1;
31
}
32
33
// The different coordinate notations.
34
define( 'Maps_COORDS_FLOAT', 'float' );
35
define( 'Maps_COORDS_DMS', 'dms' );
36
define( 'Maps_COORDS_DM', 'dm' );
37
define( 'Maps_COORDS_DD', 'dd' );
38
39
require_once __DIR__ . '/Maps_Settings.php';
40
41
// Include the composer autoloader if it is present.
42
if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
43
	include_once( __DIR__ . '/vendor/autoload.php' );
44
}
45
46
// Internationalization
47
$GLOBALS['wgMessagesDirs']['Maps'] = __DIR__ . '/i18n';
48
$GLOBALS['wgExtensionMessagesFiles']['MapsMagic'] = __DIR__ . '/Maps.i18n.magic.php';
49
$GLOBALS['wgExtensionMessagesFiles']['MapsAlias'] = __DIR__ . '/Maps.i18n.alias.php';
50
51
$GLOBALS['wgExtensionFunctions'][] = function() {
52
	if ( $GLOBALS['egMapsDisableExtension'] ) {
53
		return true;
54
	}
55
56
	if ( defined( 'Maps_VERSION' ) ) {
57
		// Do not initialize more than once.
58
		return true;
59
	}
60
61
	// Only initialize the extension when all dependencies are present.
62
	if ( !defined( 'Validator_VERSION' ) ) {
63
		throw new Exception( 'You need to have Validator installed in order to use Maps' );
64
	}
65
66
	if ( version_compare( $GLOBALS['wgVersion'], '1.27c', '<' ) ) {
67
		throw new Exception(
68
			'This version of Maps requires MediaWiki 1.27 or above; use Maps 4.2.x for older versions.'
69
			. ' More information at https://github.com/JeroenDeDauw/Maps/blob/master/INSTALL.md'
70
		);
71
	}
72
73
	define( 'Maps_VERSION', '5.5.3 alpha' );
74
	define( 'SM_VERSION', Maps_VERSION );
75
76
	if ( $GLOBALS['egMapsGMaps3Language'] === '' ) {
77
		$GLOBALS['egMapsGMaps3Language'] = $GLOBALS['wgLang'];
78
	}
79
80
	if ( in_array( 'googlemaps3', $GLOBALS['egMapsAvailableServices'] ) ) {
81
		$GLOBALS['wgSpecialPages']['MapEditor'] = 'SpecialMapEditor';
82
		$GLOBALS['wgSpecialPageGroups']['MapEditor'] = 'maps';
83
	}
84
85
	$GLOBALS['wgExtensionCredits']['parserhook'][] = [
86
		'path' => __FILE__,
87
		'name' => 'Maps',
88
		'version' => Maps_VERSION,
89
		'author' => [
90
			'[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]',
91
			'...'
92
		],
93
		'url' => 'https://github.com/JeroenDeDauw/Maps/blob/master/README.md#maps',
94
		'descriptionmsg' => 'maps-desc',
95
		'license-name' => 'GPL-2.0-or-later'
96
	];
97
98
	$GLOBALS['wgResourceModules'] = array_merge( $GLOBALS['wgResourceModules'], include 'Maps.resources.php' );
99
100
	$GLOBALS['wgAPIModules']['geocode'] = 'Maps\Api\Geocode';
101
102
	$GLOBALS['wgHooks']['AdminLinks'][] = 'MapsHooks::addToAdminLinks';
103
	$GLOBALS['wgHooks']['MakeGlobalVariablesScript'][] = 'MapsHooks::onMakeGlobalVariablesScript';
104
105
	// Parser hooks
106
107
	// Required for #coordinates.
108
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
109
		$instance = new MapsCoordinates();
110
		return $instance->init( $parser );
111
	};
112
113
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
114
		foreach ( [ 'display_map', 'display_point', 'display_points', 'display_line' ] as $hookName ) {
115
			$parser->setFunctionHook(
116
				$hookName,
117
				function( Parser $parser, PPFrame $frame, array $arguments ) {
118
					$hook = new MapsDisplayMap();
119
120
					$mapHtml = $hook->getMapHtmlForKeyValueStrings(
121
						$parser,
122
						array_map(
123
							function( $argument ) use ( $frame ) {
124
								return $frame->expand( $argument );
125
							},
126
							$arguments
127
						)
128
					);
129
130
					return [
131
						$mapHtml,
132
						'noparse' => true,
133
						'isHTML' => true,
134
					];
135
				},
136
				Parser::SFH_OBJECT_ARGS
137
			);
138
139
			$parser->setHook(
140
				$hookName,
141
				function( $text, array $arguments, Parser $parser, PPFrame $frame ) {
0 ignored issues
show
The parameter $frame is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
142
					if ( $text !== null ) {
143
						$defaultParameters = MapsDisplayMap::getHookDefinition( "\n" )->getDefaultParameters();
144
						$defaultParam = array_shift( $defaultParameters );
145
146
						// If there is a first default parameter, set the tag contents as its value.
147
						if ( $defaultParam !== null ) {
148
							$arguments[$defaultParam] = $text;
149
						}
150
					}
151
152
					return ( new MapsDisplayMap() )->getMapHtmlForParameterList( $parser, $arguments );
153
				}
154
			);
155
		}
156
	};
157
158
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
159
		$instance = new MapsDistance();
160
		return $instance->init( $parser );
161
	};
162
163
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
164
		$instance = new MapsFinddestination();
165
		return $instance->init( $parser );
166
	};
167
168
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
169
		$instance = new MapsGeocode();
170
		return $instance->init( $parser );
171
	};
172
173
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
174
		$instance = new MapsGeodistance();
175
		return $instance->init( $parser );
176
	};
177
178
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
179
		$instance = new MapsMapsDoc();
180
		return $instance->init( $parser );
181
	};
182
183
	// Google Maps API v3
184
	if ( $GLOBALS['egMapsGMaps3ApiKey'] === '' && array_key_exists( 'egGoogleJsApiKey', $GLOBALS ) ) {
185
		$GLOBALS['egMapsGMaps3ApiKey'] = $GLOBALS['egGoogleJsApiKey'];
186
	}
187
188
	include_once __DIR__ . '/includes/services/GoogleMaps3/GoogleMaps3.php';
189
190
	MapsMappingServices::registerService( 'googlemaps3', MapsGoogleMaps3::class );
191
192
	$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
193
	$googleMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
194
195
196
	// OpenLayers API
197
	include_once __DIR__ . '/includes/services/OpenLayers/OpenLayers.php';
198
199
	MapsMappingServices::registerService(
200
		'openlayers',
201
		MapsOpenLayers::class
202
	);
203
204
	$openLayers = MapsMappingServices::getServiceInstance( 'openlayers' );
205
	$openLayers->addFeature( 'display_map', MapsDisplayMapRenderer::class );
206
207
208
	// Leaflet API
209
	include_once __DIR__ . '/includes/services/Leaflet/Leaflet.php';
210
211
	MapsMappingServices::registerService( 'leaflet', MapsLeaflet::class );
212
	$leafletMaps = MapsMappingServices::getServiceInstance( 'leaflet' );
213
	$leafletMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
214
215
	$GLOBALS['wgAvailableRights'][] = 'geocode';
216
217
	// Users that can geocode. By default the same as those that can edit.
218
	foreach ( $GLOBALS['wgGroupPermissions'] as $group => $rights ) {
219
		if ( array_key_exists( 'edit', $rights ) ) {
220
			$GLOBALS['wgGroupPermissions'][$group]['geocode'] = $GLOBALS['wgGroupPermissions'][$group]['edit'];
221
		}
222
	}
223
224
	$GLOBALS['wgParamDefinitions']['coordinate'] = [
225
		'string-parser' => LatLongParser::class,
226
	];
227
228
	$GLOBALS['wgParamDefinitions']['mapslocation'] = [
229
		'string-parser' => LocationParser::class,
230
	];
231
232
	$GLOBALS['wgParamDefinitions']['mapsline'] = [
233
		'string-parser' => LineParser::class,
234
	];
235
236
	$GLOBALS['wgParamDefinitions']['mapscircle'] = [
237
		'string-parser' => CircleParser::class,
238
	];
239
240
	$GLOBALS['wgParamDefinitions']['mapsrectangle'] = [
241
		'string-parser' => RectangleParser::class,
242
	];
243
244
	$GLOBALS['wgParamDefinitions']['mapspolygon'] = [
245
		'string-parser' => PolygonParser::class,
246
	];
247
248
	$GLOBALS['wgParamDefinitions']['distance'] = [
249
		'string-parser' => DistanceParser::class,
250
	];
251
252
	$GLOBALS['wgParamDefinitions']['wmsoverlay'] = [
253
		'string-parser' => WmsOverlayParser::class,
254
	];
255
256
	$GLOBALS['wgParamDefinitions']['mapsimageoverlay'] = [
257
		'string-parser' => ImageOverlayParser::class,
258
	];
259
260
	if ( !$GLOBALS['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ) ) {
261
		SemanticMaps::newFromMediaWikiGlobals( $GLOBALS )->initExtension();
262
	}
263
264
	return true;
265
};
266
267