Completed
Push — rmmvmsg ( 4a74e1 )
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 32 and the first side effect is on line 28.

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\GeoCoordinateParser;
15
use FileFetcher\SimpleFileFetcher;
16
use Maps\CircleParser;
17
use Maps\DistanceParser;
18
use Maps\ImageOverlayParser;
19
use Maps\LineParser;
20
use Maps\LocationParser;
21
use Maps\PolygonParser;
22
use Maps\RectangleParser;
23
use Maps\ServiceParam;
24
use Maps\WmsOverlayParser;
25
26
if ( defined( 'Maps_COORDS_FLOAT' ) ) {
27
	// Do not initialize more than once.
28
	return 1;
29
}
30
31
// The different coordinate notations.
32
define( 'Maps_COORDS_FLOAT' , 'float' );
33
define( 'Maps_COORDS_DMS' , 'dms' );
34
define( 'Maps_COORDS_DM' , 'dm' );
35
define( 'Maps_COORDS_DD' , 'dd' );
36
37
require_once __DIR__ . '/Maps_Settings.php';
38
39
// Include the composer autoloader if it is present.
40
if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
41
	include_once( __DIR__ . '/vendor/autoload.php' );
42
}
43
44
// Internationalization
45
$GLOBALS['wgMessagesDirs']['Maps'] = __DIR__ . '/i18n';
46
$GLOBALS['wgExtensionMessagesFiles']['MapsMagic'] = __DIR__ . '/Maps.i18n.magic.php';
47
$GLOBALS['wgExtensionMessagesFiles']['MapsAlias'] = __DIR__ . '/Maps.i18n.alias.php';
48
49
50
$GLOBALS['wgExtensionFunctions'][] = function () {
51
	if ( $GLOBALS['egMapsDisableExtension'] ) {
52
		return true;
53
	}
54
55
	if ( defined( 'Maps_VERSION' ) ) {
56
		// Do not initialize more than once.
57
		return true;
58
	}
59
60
	// Only initialize the extension when all dependencies are present.
61
	if ( !defined( 'Validator_VERSION' ) ) {
62
		throw new Exception( 'You need to have Validator installed in order to use Maps' );
63
	}
64
65
	if ( version_compare( $GLOBALS['wgVersion'], '1.23c' , '<' ) ) {
66
		throw new Exception(
67
			'This version of Maps requires MediaWiki 1.23 or above; use Maps 3.5.x for older versions.'
68
			. ' More information at https://github.com/JeroenDeDauw/Maps/blob/master/INSTALL.md'
69
		);
70
	}
71
72
	define( 'Maps_VERSION' , '4.0-alpha' );
73
	define( 'SM_VERSION', Maps_VERSION );
74
75
	if ( $GLOBALS['egMapsGMaps3Language'] === '' ) {
76
		$GLOBALS['egMapsGMaps3Language'] = $GLOBALS['wgLang'];
77
	}
78
79
	if ( in_array( 'googlemaps3', $GLOBALS['egMapsAvailableServices'] ) ) {
80
		$GLOBALS['wgSpecialPages']['MapEditor'] = 'SpecialMapEditor';
81
		$GLOBALS['wgSpecialPageGroups']['MapEditor'] = 'maps';
82
	}
83
84
	$GLOBALS['wgExtensionCredits']['parserhook'][] = [
85
		'path' => __FILE__ ,
86
		'name' => 'Maps' ,
87
		'version' => Maps_VERSION ,
88
		'author' => [
89
			'[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]',
90
			'...'
91
		] ,
92
		'url' => 'https://github.com/JeroenDeDauw/Maps/blob/master/README.md#maps' ,
93
		'descriptionmsg' => 'maps-desc',
94
		'license-name' => 'GPL-2.0+'
95
	];
96
97
	$GLOBALS['egMapsStyleVersion'] = $GLOBALS['wgStyleVersion'] . '-' . Maps_VERSION;
98
99
	$GLOBALS['wgResourceModules'] = array_merge( $GLOBALS['wgResourceModules'], include 'Maps.resources.php' );
100
101
	$GLOBALS['wgAPIModules']['geocode'] = 'Maps\Api\Geocode';
102
103
104
105
	$GLOBALS['wgHooks']['AdminLinks'][]                = 'MapsHooks::addToAdminLinks';
106
	$GLOBALS['wgHooks']['MakeGlobalVariablesScript'][] = 'MapsHooks::onMakeGlobalVariablesScript';
107
108
	// Parser hooks
109
110
	// Required for #coordinates.
111
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
112
		$instance = new MapsCoordinates();
113
		return $instance->init( $parser );
114
	};
115
116
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
117
		$instance = new MapsDisplayMap();
118
		return $instance->init( $parser );
119
	};
120
121
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
122
		$instance = new MapsDistance();
123
		return $instance->init( $parser );
124
	};
125
126
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
127
		$instance = new MapsFinddestination();
128
		return $instance->init( $parser );
129
	};
130
131
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
132
		$instance = new MapsGeocode();
133
		return $instance->init( $parser );
134
	};
135
136
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
137
		$instance = new MapsGeodistance();
138
		return $instance->init( $parser );
139
	};
140
141
	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser ) {
142
		$instance = new MapsMapsDoc();
143
		return $instance->init( $parser );
144
	};
145
146
	// Geocoders
147
148
	// Registration of the GeoNames service geocoder.
149
	$GLOBALS['wgHooks']['GeocoderFirstCallInit'][] = 'MapsGeonamesGeocoder::register';
150
151
	// Registration of the Google Geocoding (v2) service geocoder.
152
	$GLOBALS['wgHooks']['GeocoderFirstCallInit'][] = 'MapsGoogleGeocoder::register';
153
154
	// Registration of the geocoder.us service geocoder.
155
	$GLOBALS['wgHooks']['GeocoderFirstCallInit'][] = 'MapsGeocoderusGeocoder::register';
156
157
	// Registration of the OSM Nominatim service geocoder.
158
	$GLOBALS['wgHooks']['GeocoderFirstCallInit'][] = function() {
159
		\Maps\Geocoders::registerGeocoder(
160
			'nominatim',
161
			new \Maps\Geocoders\NominatimGeocoder( new SimpleFileFetcher() )
162
		);
163
		return true;
164
	};
165
166
167
168
	// Google Maps API v3
169
	include_once __DIR__ . '/includes/services/GoogleMaps3/GoogleMaps3.php';
170
171
	MapsMappingServices::registerService( 'googlemaps3', MapsGoogleMaps3::class );
172
173
	$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
174
	$googleMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
175
176
177
178
	// OpenLayers API
179
	include_once __DIR__ . '/includes/services/OpenLayers/OpenLayers.php';
180
181
	MapsMappingServices::registerService(
182
		'openlayers',
183
		MapsOpenLayers::class,
184
		[ 'display_map' => MapsDisplayMapRenderer::class ]
185
	);
186
187
188
189
	// Leaflet API
190
	include_once __DIR__ . '/includes/services/Leaflet/Leaflet.php';
191
192
	MapsMappingServices::registerService( 'leaflet', MapsLeaflet::class );
193
	$leafletMaps = MapsMappingServices::getServiceInstance( 'leaflet' );
194
	$leafletMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
195
196
197
198
199
	$GLOBALS['wgAvailableRights'][] = 'geocode';
200
201
	// Users that can geocode. By default the same as those that can edit.
202
	foreach ( $GLOBALS['wgGroupPermissions'] as $group => $rights ) {
203
		if ( array_key_exists( 'edit' , $rights ) ) {
204
			$GLOBALS['wgGroupPermissions'][$group]['geocode'] = $GLOBALS['wgGroupPermissions'][$group]['edit'];
205
		}
206
	}
207
208
	$GLOBALS['wgParamDefinitions']['coordinate'] = [
209
		'string-parser' => GeoCoordinateParser::class,
210
	];
211
212
	$GLOBALS['wgParamDefinitions']['mappingservice'] = [
213
		'definition'=> ServiceParam::class,
214
	];
215
216
	$GLOBALS['wgParamDefinitions']['mapslocation'] = [
217
		'string-parser' => LocationParser::class,
218
	];
219
220
	$GLOBALS['wgParamDefinitions']['mapsline'] = [
221
		'string-parser' => LineParser::class,
222
	];
223
224
	$GLOBALS['wgParamDefinitions']['mapscircle'] = [
225
		'string-parser' => CircleParser::class,
226
	];
227
228
	$GLOBALS['wgParamDefinitions']['mapsrectangle'] = [
229
		'string-parser' => RectangleParser::class,
230
	];
231
232
	$GLOBALS['wgParamDefinitions']['mapspolygon'] = [
233
		'string-parser' => PolygonParser::class,
234
	];
235
236
	$GLOBALS['wgParamDefinitions']['distance'] = [
237
		'string-parser' => DistanceParser::class,
238
	];
239
240
	$GLOBALS['wgParamDefinitions']['wmsoverlay'] = [
241
		'string-parser' => WmsOverlayParser::class,
242
	];
243
244
	$GLOBALS['wgParamDefinitions']['mapsimageoverlay'] = [
245
		'string-parser' => ImageOverlayParser::class,
246
	];
247
248
	if ( !$GLOBALS['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ) ) {
249
		SemanticMaps::newFromMediaWikiGlobals( $GLOBALS )->initExtension();
250
	}
251
252
	return true;
253
};
254
255
class SemanticMaps {
256
257
	private $mwGlobals;
258
259
	public static function newFromMediaWikiGlobals( array &$mwGlobals ) {
260
		return new self( $mwGlobals );
261
	}
262
263
	private function __construct( array &$mwGlobals ) {
264
		$this->mwGlobals =& $mwGlobals;
265
	}
266
267
	/**
268
	 * @since 3.4
269
	 */
270
	public function initExtension() {
271
		// Hook for initializing the Geographical Data types.
272
		$this->mwGlobals['wgHooks']['SMW::DataType::initTypes'][] = 'SemanticMapsHooks::initGeoDataTypes';
273
274
		// Hook for defining the default query printer for queries that ask for geographical coordinates.
275
		$this->mwGlobals['wgHooks']['SMWResultFormat'][] = 'SemanticMapsHooks::addGeoCoordsDefaultFormat';
276
277
		// Hook for adding a Semantic Maps links to the Admin Links extension.
278
		$this->mwGlobals['wgHooks']['AdminLinks'][] = 'SemanticMapsHooks::addToAdminLinks';
279
280
		$this->mwGlobals['wgHooks']['sfFormPrinterSetup'][] = 'SemanticMaps\FormInputsSetup::run';
281
282
		$this->registerResourceModules();
283
284
		$this->registerGoogleMaps();
285
		$this->registerLeaflet();
286
		$this->registerOpenLayers();
287
288
		$this->mwGlobals['smwgResultFormats']['kml'] = SMKMLPrinter::class;
289
290
		$this->mwGlobals['smwgResultAliases'][$this->mwGlobals['egMapsDefaultServices']['qp']][] = 'map';
291
		SMMapPrinter::registerDefaultService( $this->mwGlobals['egMapsDefaultServices']['qp'] );
292
293
		// Internationalization
294
		$this->mwGlobals['wgMessagesDirs']['SemanticMaps'] = __DIR__ . '/i18n';
295
	}
296
297
	private function registerResourceModules() {
298
		$moduleTemplate = [
299
			'position' => 'bottom',
300
			'group' => 'ext.semanticmaps',
301
		];
302
303
		$this->mwGlobals['wgResourceModules']['ext.sm.forminputs'] = $moduleTemplate + [
304
				'dependencies' => [ 'ext.maps.coord' ],
305
				'localBasePath' => __DIR__ . '/SemanticMaps/src/forminputs',
306
				'remoteExtPath' => 'Maps/SemanticMaps/src/forminputs',
307
				'scripts' => [
308
					'jquery.mapforminput.js'
309
				],
310
				'messages' => [
311
					'semanticmaps_enteraddresshere',
312
					'semanticmaps-updatemap',
313
					'semanticmaps_lookupcoordinates',
314
					'semanticmaps-forminput-remove',
315
					'semanticmaps-forminput-add',
316
					'semanticmaps-forminput-locations'
317
				]
318
			];
319
320
		$this->mwGlobals['wgResourceModules']['ext.sm.common'] = $moduleTemplate + [
321
				'localBasePath' => __DIR__ . '/SemanticMaps/src',
322
				'remoteExtPath' => 'Maps/SemanticMaps/src',
323
				'scripts' => [
324
					'ext.sm.common.js'
325
				]
326
			];
327
	}
328
329
	private function registerGoogleMaps() {
330
		$moduleTemplate = [
331
			'localBasePath' => __DIR__ . '/SemanticMaps/src/services/GoogleMaps3',
332
			'remoteExtPath' => 'Maps/SemanticMaps/src/services/GoogleMaps3',
333
			'group' => 'ext.semanticmaps',
334
		];
335
336
		$this->mwGlobals['wgResourceModules']['ext.sm.fi.googlemaps3ajax'] = $moduleTemplate + [
337
				'dependencies' => [
338
					'ext.maps.googlemaps3',
339
					'ext.sm.common'
340
				],
341
				'scripts' => [
342
					'ext.sm.googlemaps3ajax.js'
343
				]
344
			];
345
346
		$this->mwGlobals['wgResourceModules']['ext.sm.fi.googlemaps3'] = $moduleTemplate + [
347
				'dependencies' => [
348
					'ext.sm.fi.googlemaps3.single',
349
				],
350
				'scripts' => [
351
					'ext.sm.googlemapsinput.js',
352
				],
353
			];
354
355
		$this->mwGlobals['wgResourceModules']['ext.sm.fi.googlemaps3.single'] = $moduleTemplate + [
356
				'dependencies' => [
357
					'ext.maps.googlemaps3',
358
					'ext.sm.forminputs',
359
				],
360
				'scripts' => [
361
					'jquery.googlemapsinput.js',
362
				],
363
				'messages' => [
364
				]
365
			];
366
367
		/* @var MapsMappingService $googleMaps */
368
		$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
369
		$googleMaps->addResourceModules( array( 'ext.sm.fi.googlemaps3ajax' ) );
370
371
		$googleMaps->addFeature( 'fi', SMGoogleMaps3FormInput::class );
372
373
		SMMapPrinter::registerService( $googleMaps );
374
375
		$this->mwGlobals['smwgResultFormats'][$googleMaps->getName()] = SMMapPrinter::class;
376
		$this->mwGlobals['smwgResultAliases'][$googleMaps->getName()] = $googleMaps->getAliases();
377
	}
378
379
	private function registerLeaflet() {
380
		$this->mwGlobals['wgResourceModules']['ext.sm.fi.leafletajax'] = [
381
			'localBasePath' => __DIR__ . '/SemanticMaps/src/services/Leaflet',
382
			'remoteExtPath' => 'Maps/SemanticMaps/src/services/Leaflet',
383
			'group' => 'ext.semanticmaps',
384
			'dependencies' => [
385
				'ext.maps.leaflet',
386
				'ext.sm.common'
387
			],
388
			'scripts' => [
389
				'ext.sm.leafletajax.js'
390
			]
391
		];
392
393
		/* @var MapsMappingService $leaflet */
394
		$leaflet = MapsMappingServices::getServiceInstance( 'leaflet' );
395
		$leaflet->addResourceModules( array( 'ext.sm.fi.leafletajax' ) );
396
397
		SMMapPrinter::registerService( $leaflet );
398
399
		$this->mwGlobals['smwgResultFormats'][$leaflet->getName()] = SMMapPrinter::class;
400
		$this->mwGlobals['smwgResultAliases'][$leaflet->getName()] = $leaflet->getAliases();
401
	}
402
403
	private function registerOpenLayers() {
404
		$this->mwGlobals['wgResourceModules']['ext.sm.fi.openlayersajax'] = [
405
			'localBasePath' => __DIR__ . '/SemanticMaps/src/services/OpenLayers',
406
			'remoteExtPath' => 'Maps/SemanticMaps/src/services/OpenLayers',
407
			'group' => 'ext.semanticmaps',
408
			'dependencies' => [
409
				'ext.maps.openlayers',
410
				'ext.sm.common'
411
			],
412
			'scripts' => [
413
				'ext.sm.openlayersajax.js'
414
			]
415
		];
416
417
		/* @var MapsMappingService $openLayers */
418
		$openLayers = MapsMappingServices::getServiceInstance( 'openlayers' );
419
		$openLayers->addResourceModules( array( 'ext.sm.fi.openlayersajax' ) );
420
421
		SMMapPrinter::registerService( $openLayers );
422
423
		$this->mwGlobals['smwgResultFormats'][$openLayers->getName()] = SMMapPrinter::class;
424
		$this->mwGlobals['smwgResultAliases'][$openLayers->getName()] = $openLayers->getAliases();
425
	}
426
427
	/**
428
	 * @since 3.4
429
	 *
430
	 * @return string|null
431
	 */
432
	public static function getVersion() {
433
		return SM_VERSION;
434
	}
435
436
}
437
438