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\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
|
|
|
$this->mwGlobals['wgResourceModules']['ext.sm.googlemaps3ajax'] = [ |
316
|
|
|
'localBasePath' => __DIR__ . '/SemanticMaps/src/services/GoogleMaps3', |
317
|
|
|
'remoteExtPath' => 'Maps/SemanticMaps/src/services/GoogleMaps3', |
318
|
|
|
'group' => 'ext.semanticmaps', |
319
|
|
|
'dependencies' => [ |
320
|
|
|
'ext.maps.googlemaps3', |
321
|
|
|
'ext.sm.common' |
322
|
|
|
], |
323
|
|
|
'scripts' => [ |
324
|
|
|
'ext.sm.googlemaps3ajax.js' |
325
|
|
|
] |
326
|
|
|
]; |
327
|
|
|
|
328
|
|
|
/* @var MapsMappingService $googleMaps */ |
329
|
|
|
$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' ); |
330
|
|
|
$googleMaps->addResourceModules( array( 'ext.sm.googlemaps3ajax' ) ); |
331
|
|
|
|
332
|
|
|
SMMapPrinter::registerService( $googleMaps ); |
333
|
|
|
|
334
|
|
|
$this->mwGlobals['smwgResultFormats'][$googleMaps->getName()] = SMMapPrinter::class; |
335
|
|
|
$this->mwGlobals['smwgResultAliases'][$googleMaps->getName()] = $googleMaps->getAliases(); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
private function registerLeaflet() { |
339
|
|
|
$this->mwGlobals['wgResourceModules']['ext.sm.fi.leafletajax'] = [ |
340
|
|
|
'localBasePath' => __DIR__ . '/SemanticMaps/src/services/Leaflet', |
341
|
|
|
'remoteExtPath' => 'Maps/SemanticMaps/src/services/Leaflet', |
342
|
|
|
'group' => 'ext.semanticmaps', |
343
|
|
|
'dependencies' => [ |
344
|
|
|
'ext.maps.leaflet', |
345
|
|
|
'ext.sm.common' |
346
|
|
|
], |
347
|
|
|
'scripts' => [ |
348
|
|
|
'ext.sm.leafletajax.js' |
349
|
|
|
] |
350
|
|
|
]; |
351
|
|
|
|
352
|
|
|
/* @var MapsMappingService $leaflet */ |
353
|
|
|
$leaflet = MapsMappingServices::getServiceInstance( 'leaflet' ); |
354
|
|
|
$leaflet->addResourceModules( array( 'ext.sm.fi.leafletajax' ) ); |
355
|
|
|
|
356
|
|
|
SMMapPrinter::registerService( $leaflet ); |
357
|
|
|
|
358
|
|
|
$this->mwGlobals['smwgResultFormats'][$leaflet->getName()] = SMMapPrinter::class; |
359
|
|
|
$this->mwGlobals['smwgResultAliases'][$leaflet->getName()] = $leaflet->getAliases(); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
private function registerOpenLayers() { |
363
|
|
|
$this->mwGlobals['wgResourceModules']['ext.sm.fi.openlayersajax'] = [ |
364
|
|
|
'localBasePath' => __DIR__ . '/SemanticMaps/src/services/OpenLayers', |
365
|
|
|
'remoteExtPath' => 'Maps/SemanticMaps/src/services/OpenLayers', |
366
|
|
|
'group' => 'ext.semanticmaps', |
367
|
|
|
'dependencies' => [ |
368
|
|
|
'ext.maps.openlayers', |
369
|
|
|
'ext.sm.common' |
370
|
|
|
], |
371
|
|
|
'scripts' => [ |
372
|
|
|
'ext.sm.openlayersajax.js' |
373
|
|
|
] |
374
|
|
|
]; |
375
|
|
|
|
376
|
|
|
/* @var MapsMappingService $openLayers */ |
377
|
|
|
$openLayers = MapsMappingServices::getServiceInstance( 'openlayers' ); |
378
|
|
|
$openLayers->addResourceModules( array( 'ext.sm.fi.openlayersajax' ) ); |
379
|
|
|
|
380
|
|
|
SMMapPrinter::registerService( $openLayers ); |
381
|
|
|
|
382
|
|
|
$this->mwGlobals['smwgResultFormats'][$openLayers->getName()] = SMMapPrinter::class; |
383
|
|
|
$this->mwGlobals['smwgResultAliases'][$openLayers->getName()] = $openLayers->getAliases(); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @since 3.4 |
388
|
|
|
* |
389
|
|
|
* @return string|null |
390
|
|
|
*/ |
391
|
|
|
public static function getVersion() { |
392
|
|
|
return SM_VERSION; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
|
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.