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

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
25
if ( defined( 'Maps_COORDS_FLOAT' ) ) {
26
	// Do not initialize more than once.
27
	return 1;
28
}
29
30
// The different coordinate notations.
31
define( 'Maps_COORDS_FLOAT', 'float' );
32
define( 'Maps_COORDS_DMS', 'dms' );
33
define( 'Maps_COORDS_DM', 'dm' );
34
define( 'Maps_COORDS_DD', 'dd' );
35
36
require_once __DIR__ . '/Maps_Settings.php';
37
38
// Include the composer autoloader if it is present.
39
if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
40
	include_once( __DIR__ . '/vendor/autoload.php' );
41
}
42
43
// Internationalization
44
$GLOBALS['wgMessagesDirs']['Maps'] = __DIR__ . '/i18n';
45
$GLOBALS['wgExtensionMessagesFiles']['MapsMagic'] = __DIR__ . '/Maps.i18n.magic.php';
46
$GLOBALS['wgExtensionMessagesFiles']['MapsAlias'] = __DIR__ . '/Maps.i18n.alias.php';
47
48
$GLOBALS['wgExtensionFunctions'][] = function() {
49
	if ( $GLOBALS['egMapsDisableExtension'] ) {
50
		return true;
51
	}
52
53
	if ( defined( 'Maps_VERSION' ) ) {
54
		// Do not initialize more than once.
55
		return true;
56
	}
57
58
	// Only initialize the extension when all dependencies are present.
59
	if ( !defined( 'Validator_VERSION' ) ) {
60
		throw new Exception( 'You need to have Validator installed in order to use Maps' );
61
	}
62
63
	if ( version_compare( $GLOBALS['wgVersion'], '1.27c', '<' ) ) {
64
		throw new Exception(
65
			'This version of Maps requires MediaWiki 1.27 or above; use Maps 4.2.x for older versions.'
66
			. ' More information at https://github.com/JeroenDeDauw/Maps/blob/master/INSTALL.md'
67
		);
68
	}
69
70
	define( 'Maps_VERSION', '5.5.3 alpha' );
71
	define( 'SM_VERSION', Maps_VERSION );
72
73
	if ( $GLOBALS['egMapsGMaps3Language'] === '' ) {
74
		$GLOBALS['egMapsGMaps3Language'] = $GLOBALS['wgLang'];
75
	}
76
77
	if ( in_array( 'googlemaps3', $GLOBALS['egMapsAvailableServices'] ) ) {
78
		$GLOBALS['wgSpecialPages']['MapEditor'] = 'SpecialMapEditor';
79
		$GLOBALS['wgSpecialPageGroups']['MapEditor'] = 'maps';
80
	}
81
82
	$GLOBALS['wgExtensionCredits']['parserhook'][] = [
83
		'path' => __FILE__,
84
		'name' => 'Maps',
85
		'version' => Maps_VERSION,
86
		'author' => [
87
			'[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]',
88
			'...'
89
		],
90
		'url' => 'https://github.com/JeroenDeDauw/Maps/blob/master/README.md#maps',
91
		'descriptionmsg' => 'maps-desc',
92
		'license-name' => 'GPL-2.0-or-later'
93
	];
94
95
	$GLOBALS['wgResourceModules'] = array_merge( $GLOBALS['wgResourceModules'], include 'Maps.resources.php' );
96
97
	$GLOBALS['wgAPIModules']['geocode'] = 'Maps\Api\Geocode';
98
99
	$GLOBALS['wgHooks']['AdminLinks'][] = 'MapsHooks::addToAdminLinks';
100
	$GLOBALS['wgHooks']['MakeGlobalVariablesScript'][] = 'MapsHooks::onMakeGlobalVariablesScript';
101
102
	// Parser hooks
103
104
	// Required for #coordinates.
105
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
106
		$instance = new MapsCoordinates();
107
		return $instance->init( $parser );
108
	};
109
110
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
111
		foreach ( [ 'display_map', 'display_point', 'display_points', 'display_line' ] as $hookName ) {
112
			$parser->setFunctionHook(
113
				$hookName,
114
				function( Parser $parser, PPFrame $frame, array $arguments ) {
115
					$hook = new MapsDisplayMap();
116
117
					$mapHtml = $hook->getMapHtmlForKeyValueStrings(
118
						$parser,
119
						array_map(
120
							function( $argument ) use ( $frame ) {
121
								return $frame->expand( $argument );
122
							},
123
							$arguments
124
						)
125
					);
126
127
					return [
128
						$mapHtml,
129
						'noparse' => true,
130
						'isHTML' => true,
131
					];
132
				},
133
				Parser::SFH_OBJECT_ARGS
134
			);
135
136
			$parser->setHook(
137
				$hookName,
138
				function( $text, array $arguments, Parser $parser ) {
139
					if ( $text !== null ) {
140
						$defaultParameters = MapsDisplayMap::getHookDefinition( "\n" )->getDefaultParameters();
141
						$defaultParam = array_shift( $defaultParameters );
142
143
						// If there is a first default parameter, set the tag contents as its value.
144
						if ( $defaultParam !== null ) {
145
							$arguments[$defaultParam] = $text;
146
						}
147
					}
148
149
					return ( new MapsDisplayMap() )->getMapHtmlForParameterList( $parser, $arguments );
150
				}
151
			);
152
		}
153
	};
154
155
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
156
		$instance = new MapsDistance();
157
		return $instance->init( $parser );
158
	};
159
160
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
161
		$instance = new MapsFinddestination();
162
		return $instance->init( $parser );
163
	};
164
165
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
166
		$instance = new MapsGeocode();
167
		return $instance->init( $parser );
168
	};
169
170
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
171
		$instance = new MapsGeodistance();
172
		return $instance->init( $parser );
173
	};
174
175
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
176
		$instance = new MapsMapsDoc();
177
		return $instance->init( $parser );
178
	};
179
180
	// Google Maps API v3
181
	if ( $GLOBALS['egMapsGMaps3ApiKey'] === '' && array_key_exists( 'egGoogleJsApiKey', $GLOBALS ) ) {
182
		$GLOBALS['egMapsGMaps3ApiKey'] = $GLOBALS['egGoogleJsApiKey'];
183
	}
184
185
	include_once __DIR__ . '/includes/services/GoogleMaps3/GoogleMaps3.php';
186
187
	MapsMappingServices::registerService( 'googlemaps3', MapsGoogleMaps3::class );
188
189
	$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
190
	$googleMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
191
192
193
	// OpenLayers API
194
	include_once __DIR__ . '/includes/services/OpenLayers/OpenLayers.php';
195
196
	MapsMappingServices::registerService(
197
		'openlayers',
198
		MapsOpenLayers::class
199
	);
200
201
	$openLayers = MapsMappingServices::getServiceInstance( 'openlayers' );
202
	$openLayers->addFeature( 'display_map', MapsDisplayMapRenderer::class );
203
204
205
	// Leaflet API
206
	include_once __DIR__ . '/includes/services/Leaflet/Leaflet.php';
207
208
	MapsMappingServices::registerService( 'leaflet', MapsLeaflet::class );
209
	$leafletMaps = MapsMappingServices::getServiceInstance( 'leaflet' );
210
	$leafletMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
211
212
	$GLOBALS['wgAvailableRights'][] = 'geocode';
213
214
	// Users that can geocode. By default the same as those that can edit.
215
	foreach ( $GLOBALS['wgGroupPermissions'] as $group => $rights ) {
216
		if ( array_key_exists( 'edit', $rights ) ) {
217
			$GLOBALS['wgGroupPermissions'][$group]['geocode'] = $GLOBALS['wgGroupPermissions'][$group]['edit'];
218
		}
219
	}
220
221
	$GLOBALS['wgParamDefinitions']['coordinate'] = [
222
		'string-parser' => LatLongParser::class,
223
	];
224
225
	$GLOBALS['wgParamDefinitions']['mapslocation'] = [
226
		'string-parser' => LocationParser::class,
227
	];
228
229
	$GLOBALS['wgParamDefinitions']['mapsline'] = [
230
		'string-parser' => LineParser::class,
231
	];
232
233
	$GLOBALS['wgParamDefinitions']['mapscircle'] = [
234
		'string-parser' => CircleParser::class,
235
	];
236
237
	$GLOBALS['wgParamDefinitions']['mapsrectangle'] = [
238
		'string-parser' => RectangleParser::class,
239
	];
240
241
	$GLOBALS['wgParamDefinitions']['mapspolygon'] = [
242
		'string-parser' => PolygonParser::class,
243
	];
244
245
	$GLOBALS['wgParamDefinitions']['distance'] = [
246
		'string-parser' => DistanceParser::class,
247
	];
248
249
	$GLOBALS['wgParamDefinitions']['wmsoverlay'] = [
250
		'string-parser' => WmsOverlayParser::class,
251
	];
252
253
	$GLOBALS['wgParamDefinitions']['mapsimageoverlay'] = [
254
		'string-parser' => ImageOverlayParser::class,
255
	];
256
257
	if ( !$GLOBALS['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ) ) {
258
		SemanticMaps::newFromMediaWikiGlobals( $GLOBALS )->initExtension();
259
	}
260
261
	return true;
262
};
263
264