Completed
Push — master ( 70ab19...31530e )
by Jeroen De
15s
created

SemanticMaps::registerResourceModules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
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
	if ( $GLOBALS['egMapsGMaps3ApiKey'] === '' && array_key_exists( 'egGoogleJsApiKey', $GLOBALS ) ) {
170
		$GLOBALS['egMapsGMaps3ApiKey'] = $GLOBALS['egGoogleJsApiKey'];
171
	}
172
173
	include_once __DIR__ . '/includes/services/GoogleMaps3/GoogleMaps3.php';
174
175
	MapsMappingServices::registerService( 'googlemaps3', MapsGoogleMaps3::class );
176
177
	$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
178
	$googleMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
179
180
181
182
	// OpenLayers API
183
	include_once __DIR__ . '/includes/services/OpenLayers/OpenLayers.php';
184
185
	MapsMappingServices::registerService(
186
		'openlayers',
187
		MapsOpenLayers::class,
188
		[ 'display_map' => MapsDisplayMapRenderer::class ]
189
	);
190
191
192
193
	// Leaflet API
194
	include_once __DIR__ . '/includes/services/Leaflet/Leaflet.php';
195
196
	MapsMappingServices::registerService( 'leaflet', MapsLeaflet::class );
197
	$leafletMaps = MapsMappingServices::getServiceInstance( 'leaflet' );
198
	$leafletMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
199
200
201
202
203
	$GLOBALS['wgAvailableRights'][] = 'geocode';
204
205
	// Users that can geocode. By default the same as those that can edit.
206
	foreach ( $GLOBALS['wgGroupPermissions'] as $group => $rights ) {
207
		if ( array_key_exists( 'edit' , $rights ) ) {
208
			$GLOBALS['wgGroupPermissions'][$group]['geocode'] = $GLOBALS['wgGroupPermissions'][$group]['edit'];
209
		}
210
	}
211
212
	$GLOBALS['wgParamDefinitions']['coordinate'] = [
213
		'string-parser' => GeoCoordinateParser::class,
214
	];
215
216
	$GLOBALS['wgParamDefinitions']['mappingservice'] = [
217
		'definition'=> ServiceParam::class,
218
	];
219
220
	$GLOBALS['wgParamDefinitions']['mapslocation'] = [
221
		'string-parser' => LocationParser::class,
222
	];
223
224
	$GLOBALS['wgParamDefinitions']['mapsline'] = [
225
		'string-parser' => LineParser::class,
226
	];
227
228
	$GLOBALS['wgParamDefinitions']['mapscircle'] = [
229
		'string-parser' => CircleParser::class,
230
	];
231
232
	$GLOBALS['wgParamDefinitions']['mapsrectangle'] = [
233
		'string-parser' => RectangleParser::class,
234
	];
235
236
	$GLOBALS['wgParamDefinitions']['mapspolygon'] = [
237
		'string-parser' => PolygonParser::class,
238
	];
239
240
	$GLOBALS['wgParamDefinitions']['distance'] = [
241
		'string-parser' => DistanceParser::class,
242
	];
243
244
	$GLOBALS['wgParamDefinitions']['wmsoverlay'] = [
245
		'string-parser' => WmsOverlayParser::class,
246
	];
247
248
	$GLOBALS['wgParamDefinitions']['mapsimageoverlay'] = [
249
		'string-parser' => ImageOverlayParser::class,
250
	];
251
252
	if ( !$GLOBALS['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ) ) {
253
		SemanticMaps::newFromMediaWikiGlobals( $GLOBALS )->initExtension();
254
	}
255
256
	return true;
257
};
258
259
class SemanticMaps {
260
261
	private $mwGlobals;
262
263
	public static function newFromMediaWikiGlobals( array &$mwGlobals ) {
264
		return new self( $mwGlobals );
265
	}
266
267
	private function __construct( array &$mwGlobals ) {
268
		$this->mwGlobals =& $mwGlobals;
269
	}
270
271
	/**
272
	 * @since 3.4
273
	 */
274
	public function initExtension() {
275
		// Hook for initializing the Geographical Data types.
276
		$this->mwGlobals['wgHooks']['SMW::DataType::initTypes'][] = 'SemanticMapsHooks::initGeoDataTypes';
277
278
		// Hook for defining the default query printer for queries that ask for geographical coordinates.
279
		$this->mwGlobals['wgHooks']['SMWResultFormat'][] = 'SemanticMapsHooks::addGeoCoordsDefaultFormat';
280
281
		// Hook for adding a Semantic Maps links to the Admin Links extension.
282
		$this->mwGlobals['wgHooks']['AdminLinks'][] = 'SemanticMapsHooks::addToAdminLinks';
283
284
		$this->registerResourceModules();
285
286
		$this->registerGoogleMaps();
287
		$this->registerLeaflet();
288
		$this->registerOpenLayers();
289
290
		$this->mwGlobals['smwgResultFormats']['kml'] = SMKMLPrinter::class;
291
292
		$this->mwGlobals['smwgResultAliases'][$this->mwGlobals['egMapsDefaultServices']['qp']][] = 'map';
293
		SMMapPrinter::registerDefaultService( $this->mwGlobals['egMapsDefaultServices']['qp'] );
294
295
		// Internationalization
296
		$this->mwGlobals['wgMessagesDirs']['SemanticMaps'] = __DIR__ . '/i18n';
297
	}
298
299
	private function registerResourceModules() {
300
		$moduleTemplate = [
301
			'position' => 'bottom',
302
			'group' => 'ext.semanticmaps',
303
		];
304
305
		$this->mwGlobals['wgResourceModules']['ext.sm.common'] = $moduleTemplate + [
306
				'localBasePath' => __DIR__ . '/SemanticMaps/src',
307
				'remoteExtPath' => 'Maps/SemanticMaps/src',
308
				'scripts' => [
309
					'ext.sm.common.js'
310
				]
311
			];
312
	}
313
314
	private function registerGoogleMaps() {
315
		$moduleTemplate = [
316
			'localBasePath' => __DIR__ . '/SemanticMaps/src/services/GoogleMaps3',
317
			'remoteExtPath' => 'Maps/SemanticMaps/src/services/GoogleMaps3',
318
			'group' => 'ext.semanticmaps',
319
		];
320
321
		$this->mwGlobals['wgResourceModules']['ext.sm.fi.googlemaps3ajax'] = $moduleTemplate + [
322
				'dependencies' => [
323
					'ext.maps.googlemaps3',
324
					'ext.sm.common'
325
				],
326
				'scripts' => [
327
					'ext.sm.googlemaps3ajax.js'
328
				]
329
			];
330
331
		$this->mwGlobals['wgResourceModules']['ext.sm.fi.googlemaps3'] = $moduleTemplate + [
332
				'dependencies' => [
333
					'ext.sm.fi.googlemaps3.single',
334
				],
335
				'scripts' => [
336
					'ext.sm.googlemapsinput.js',
337
				],
338
			];
339
340
		$this->mwGlobals['wgResourceModules']['ext.sm.fi.googlemaps3.single'] = $moduleTemplate + [
341
				'dependencies' => [
342
					'ext.maps.googlemaps3',
343
				],
344
				'scripts' => [
345
					'jquery.googlemapsinput.js',
346
				],
347
				'messages' => [
348
				]
349
			];
350
351
		/* @var MapsMappingService $googleMaps */
352
		$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
353
		$googleMaps->addResourceModules( array( 'ext.sm.fi.googlemaps3ajax' ) );
354
355
		SMMapPrinter::registerService( $googleMaps );
356
357
		$this->mwGlobals['smwgResultFormats'][$googleMaps->getName()] = SMMapPrinter::class;
358
		$this->mwGlobals['smwgResultAliases'][$googleMaps->getName()] = $googleMaps->getAliases();
359
	}
360
361
	private function registerLeaflet() {
362
		$this->mwGlobals['wgResourceModules']['ext.sm.fi.leafletajax'] = [
363
			'localBasePath' => __DIR__ . '/SemanticMaps/src/services/Leaflet',
364
			'remoteExtPath' => 'Maps/SemanticMaps/src/services/Leaflet',
365
			'group' => 'ext.semanticmaps',
366
			'dependencies' => [
367
				'ext.maps.leaflet',
368
				'ext.sm.common'
369
			],
370
			'scripts' => [
371
				'ext.sm.leafletajax.js'
372
			]
373
		];
374
375
		/* @var MapsMappingService $leaflet */
376
		$leaflet = MapsMappingServices::getServiceInstance( 'leaflet' );
377
		$leaflet->addResourceModules( array( 'ext.sm.fi.leafletajax' ) );
378
379
		SMMapPrinter::registerService( $leaflet );
380
381
		$this->mwGlobals['smwgResultFormats'][$leaflet->getName()] = SMMapPrinter::class;
382
		$this->mwGlobals['smwgResultAliases'][$leaflet->getName()] = $leaflet->getAliases();
383
	}
384
385
	private function registerOpenLayers() {
386
		$this->mwGlobals['wgResourceModules']['ext.sm.fi.openlayersajax'] = [
387
			'localBasePath' => __DIR__ . '/SemanticMaps/src/services/OpenLayers',
388
			'remoteExtPath' => 'Maps/SemanticMaps/src/services/OpenLayers',
389
			'group' => 'ext.semanticmaps',
390
			'dependencies' => [
391
				'ext.maps.openlayers',
392
				'ext.sm.common'
393
			],
394
			'scripts' => [
395
				'ext.sm.openlayersajax.js'
396
			]
397
		];
398
399
		/* @var MapsMappingService $openLayers */
400
		$openLayers = MapsMappingServices::getServiceInstance( 'openlayers' );
401
		$openLayers->addResourceModules( array( 'ext.sm.fi.openlayersajax' ) );
402
403
		SMMapPrinter::registerService( $openLayers );
404
405
		$this->mwGlobals['smwgResultFormats'][$openLayers->getName()] = SMMapPrinter::class;
406
		$this->mwGlobals['smwgResultAliases'][$openLayers->getName()] = $openLayers->getAliases();
407
	}
408
409
	/**
410
	 * @since 3.4
411
	 *
412
	 * @return string|null
413
	 */
414
	public static function getVersion() {
415
		return SM_VERSION;
416
	}
417
418
}
419
420