Skip to content

Commit

Permalink
Merge pull request #260 from JeroenDeDauw/initialization
Browse files Browse the repository at this point in the history
Reordered initialization code
  • Loading branch information
JeroenDeDauw committed Oct 9, 2016
2 parents 13befa6 + f5068a8 commit 3854624
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 157 deletions.
164 changes: 86 additions & 78 deletions Maps.php
@@ -1,4 +1,5 @@
<?php

/**
* Initialization file for the Maps extension.
*
Expand All @@ -22,36 +23,58 @@
use Maps\ServiceParam;
use Maps\WmsOverlayParser;

if ( !defined( 'MEDIAWIKI' ) ) {
die( 'Not an entry point.' );
}

if ( defined( 'Maps_VERSION' ) ) {
if ( defined( 'Maps_COORDS_FLOAT' ) ) {
// Do not initialize more than once.
return 1;
}

define( 'Maps_VERSION' , '4.0-alpha' );
define( 'SM_VERSION', Maps_VERSION );
// The different coordinate notations.
define( 'Maps_COORDS_FLOAT' , 'float' );
define( 'Maps_COORDS_DMS' , 'dms' );
define( 'Maps_COORDS_DM' , 'dm' );
define( 'Maps_COORDS_DD' , 'dd' );

require_once __DIR__ . '/Maps_Settings.php';

// Include the composer autoloader if it is present.
if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
include_once( __DIR__ . '/vendor/autoload.php' );
}

// Only initialize the extension when all dependencies are present.
if ( !defined( 'Validator_VERSION' ) ) {
throw new Exception( 'You need to have Validator installed in order to use Maps' );
}
$GLOBALS['wgExtensionFunctions'][] = function () {
if ( $GLOBALS['egMapsDisableExtension'] ) {
return true;
}

if ( version_compare( $GLOBALS['wgVersion'], '1.23c' , '<' ) ) {
throw new Exception(
'This version of Maps requires MediaWiki 1.23 or above; use Maps 3.5.x for older versions.'
. ' More information at https://github.com/JeroenDeDauw/Maps/blob/master/INSTALL.md'
);
}
if ( defined( 'Maps_VERSION' ) ) {
// Do not initialize more than once.
return true;
}

// Only initialize the extension when all dependencies are present.
if ( !defined( 'Validator_VERSION' ) ) {
throw new Exception( 'You need to have Validator installed in order to use Maps' );
}

if ( version_compare( $GLOBALS['wgVersion'], '1.23c' , '<' ) ) {
throw new Exception(
'This version of Maps requires MediaWiki 1.23 or above; use Maps 3.5.x for older versions.'
. ' More information at https://github.com/JeroenDeDauw/Maps/blob/master/INSTALL.md'
);
}

define( 'Maps_VERSION' , '4.0-alpha' );
define( 'SM_VERSION', Maps_VERSION );

if ( $GLOBALS['egMapsGMaps3Language'] === '' ) {
$GLOBALS['egMapsGMaps3Language'] = $GLOBALS['wgLang'];
}

if ( in_array( 'googlemaps3', $GLOBALS['egMapsAvailableServices'] ) ) {
$GLOBALS['wgSpecialPages']['MapEditor'] = 'SpecialMapEditor';
$GLOBALS['wgSpecialPageGroups']['MapEditor'] = 'maps';
}

call_user_func( function() {
$GLOBALS['wgExtensionCredits']['parserhook'][] = [
'path' => __FILE__ ,
'name' => 'Maps' ,
Expand All @@ -65,12 +88,6 @@
'license-name' => 'GPL-2.0+'
];

// The different coordinate notations.
define( 'Maps_COORDS_FLOAT' , 'float' );
define( 'Maps_COORDS_DMS' , 'dms' );
define( 'Maps_COORDS_DM' , 'dm' );
define( 'Maps_COORDS_DD' , 'dd' );

$GLOBALS['egMapsStyleVersion'] = $GLOBALS['wgStyleVersion'] . '-' . Maps_VERSION;

// Internationalization
Expand All @@ -82,23 +99,7 @@

$GLOBALS['wgAPIModules']['geocode'] = 'Maps\Api\Geocode';

// Register the initialization function of Maps.
$GLOBALS['wgExtensionFunctions'][] = function () {

if ( $GLOBALS['egMapsGMaps3Language'] === '' ) {
$GLOBALS['egMapsGMaps3Language'] = $GLOBALS['wgLang'];
}

Hooks::run( 'MappingServiceLoad' );
Hooks::run( 'MappingFeatureLoad' );

if ( in_array( 'googlemaps3', $GLOBALS['egMapsAvailableServices'] ) ) {
$GLOBALS['wgSpecialPages']['MapEditor'] = 'SpecialMapEditor';
$GLOBALS['wgSpecialPageGroups']['MapEditor'] = 'maps';
}

return true;
};

$GLOBALS['wgHooks']['AdminLinks'][] = 'MapsHooks::addToAdminLinks';
$GLOBALS['wgHooks']['MakeGlobalVariablesScript'][] = 'MapsHooks::onMakeGlobalVariablesScript';
Expand Down Expand Up @@ -161,25 +162,38 @@
return true;
};

// Mapping services

// Include the mapping services that should be loaded into Maps.
// Commenting or removing a mapping service will make Maps completely ignore it, and so improve performance.

// Google Maps API v3
// TODO: improve loading mechanism
include_once __DIR__ . '/includes/services/GoogleMaps3/GoogleMaps3.php';

MapsMappingServices::registerService( 'googlemaps3', MapsGoogleMaps3::class );

$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
$googleMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );



// OpenLayers API
// TODO: improve loading mechanism
include_once __DIR__ . '/includes/services/OpenLayers/OpenLayers.php';

MapsMappingServices::registerService(
'openlayers',
MapsOpenLayers::class,
[ 'display_map' => MapsDisplayMapRenderer::class ]
);



// Leaflet API
// TODO: improve loading mechanism
include_once __DIR__ . '/includes/services/Leaflet/Leaflet.php';

MapsMappingServices::registerService( 'leaflet', MapsLeaflet::class );
$leafletMaps = MapsMappingServices::getServiceInstance( 'leaflet' );
$leafletMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );



require_once __DIR__ . '/Maps_Settings.php';

$GLOBALS['wgAvailableRights'][] = 'geocode';

Expand Down Expand Up @@ -230,10 +244,12 @@
'string-parser' => ImageOverlayParser::class,
];

if ( defined( 'SMW_VERSION' ) ) { // TODO: add setting to disable auto-enabling of SM features
if ( !$GLOBALS['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ) ) {
SemanticMaps::newFromMediaWikiGlobals( $GLOBALS )->initExtension();
}
} );

return true;
};

class SemanticMaps {

Expand Down Expand Up @@ -270,10 +286,8 @@ public function initExtension() {

$this->mwGlobals['smwgResultFormats']['kml'] = SMKMLPrinter::class;

$this->mwGlobals['wgHooks']['MappingServiceLoad'][] = function() {
$this->mwGlobals['smwgResultAliases'][$this->mwGlobals['egMapsDefaultServices']['qp']][] = 'map';
SMMapPrinter::registerDefaultService( $this->mwGlobals['egMapsDefaultServices']['qp'] );
};
$this->mwGlobals['smwgResultAliases'][$this->mwGlobals['egMapsDefaultServices']['qp']][] = 'map';
SMMapPrinter::registerDefaultService( $this->mwGlobals['egMapsDefaultServices']['qp'] );

// Internationalization
$this->mwGlobals['wgMessagesDirs']['SemanticMaps'] = __DIR__ . '/i18n';
Expand Down Expand Up @@ -349,18 +363,16 @@ private function registerGoogleMaps() {
]
];

$this->mwGlobals['wgHooks']['MappingServiceLoad'][] = function() {
/* @var MapsMappingService $googleMaps */
$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
$googleMaps->addResourceModules( array( 'ext.sm.fi.googlemaps3ajax' ) );
/* @var MapsMappingService $googleMaps */
$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
$googleMaps->addResourceModules( array( 'ext.sm.fi.googlemaps3ajax' ) );

$googleMaps->addFeature( 'fi', SMGoogleMaps3FormInput::class );
$googleMaps->addFeature( 'fi', SMGoogleMaps3FormInput::class );

SMMapPrinter::registerService( $googleMaps );
SMMapPrinter::registerService( $googleMaps );

$this->mwGlobals['smwgResultFormats'][$googleMaps->getName()] = SMMapPrinter::class;
$this->mwGlobals['smwgResultAliases'][$googleMaps->getName()] = $googleMaps->getAliases();
};
$this->mwGlobals['smwgResultFormats'][$googleMaps->getName()] = SMMapPrinter::class;
$this->mwGlobals['smwgResultAliases'][$googleMaps->getName()] = $googleMaps->getAliases();
}

private function registerLeaflet() {
Expand All @@ -377,16 +389,14 @@ private function registerLeaflet() {
]
];

$this->mwGlobals['wgHooks']['MappingServiceLoad'][] = function() {
/* @var MapsMappingService $leaflet */
$leaflet = MapsMappingServices::getServiceInstance( 'leaflet' );
$leaflet->addResourceModules( array( 'ext.sm.fi.leafletajax' ) );
/* @var MapsMappingService $leaflet */
$leaflet = MapsMappingServices::getServiceInstance( 'leaflet' );
$leaflet->addResourceModules( array( 'ext.sm.fi.leafletajax' ) );

SMMapPrinter::registerService( $leaflet );
SMMapPrinter::registerService( $leaflet );

$this->mwGlobals['smwgResultFormats'][$leaflet->getName()] = SMMapPrinter::class;
$this->mwGlobals['smwgResultAliases'][$leaflet->getName()] = $leaflet->getAliases();
};
$this->mwGlobals['smwgResultFormats'][$leaflet->getName()] = SMMapPrinter::class;
$this->mwGlobals['smwgResultAliases'][$leaflet->getName()] = $leaflet->getAliases();
}

private function registerOpenLayers() {
Expand All @@ -403,16 +413,14 @@ private function registerOpenLayers() {
]
];

$this->mwGlobals['wgHooks']['MappingServiceLoad'][] = function() {
/* @var MapsMappingService $openLayers */
$openLayers = MapsMappingServices::getServiceInstance( 'openlayers' );
$openLayers->addResourceModules( array( 'ext.sm.fi.openlayersajax' ) );
/* @var MapsMappingService $openLayers */
$openLayers = MapsMappingServices::getServiceInstance( 'openlayers' );
$openLayers->addResourceModules( array( 'ext.sm.fi.openlayersajax' ) );

SMMapPrinter::registerService( $openLayers );
SMMapPrinter::registerService( $openLayers );

$this->mwGlobals['smwgResultFormats'][$openLayers->getName()] = SMMapPrinter::class;
$this->mwGlobals['smwgResultAliases'][$openLayers->getName()] = $openLayers->getAliases();
};
$this->mwGlobals['smwgResultFormats'][$openLayers->getName()] = SMMapPrinter::class;
$this->mwGlobals['smwgResultAliases'][$openLayers->getName()] = $openLayers->getAliases();
}

/**
Expand Down
11 changes: 8 additions & 3 deletions Maps_Settings.php
Expand Up @@ -10,9 +10,14 @@
* @author Jeroen De Dauw
*/

if ( !defined( 'MEDIAWIKI' ) ) {
die( 'Not an entry point.' );
}

// This allows disabling the extension even when it is installed. Can be useful on wiki farms.
// CAUTION: extensions that depend on Maps will likely either break of disable themselves.
$GLOBALS['egMapsDisableExtension'] = false;

// This allows disabling the Semantic MediaWiki integration.
$GLOBALS['egMapsDisableSmwIntegration'] = false;


// Mapping services configuration

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -33,7 +33,7 @@
"require": {
"php": ">=5.5",
"composer/installers": "^1.0.1",
"mediawiki/validator": "^2.0.2",
"mediawiki/validator": "~2.2",
"data-values/geo": "~1.0",
"jeroen/file-fetcher": "~3.1",
"jeroen/simple-cache": "~2.0"
Expand Down
22 changes: 1 addition & 21 deletions includes/services/GoogleMaps3/GoogleMaps3.php
Expand Up @@ -18,7 +18,7 @@
}

call_user_func( function() {
global $wgResourceModules, $wgHooks;
global $wgResourceModules;

$pathParts = ( explode( DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR, __DIR__, 2 ) );

Expand Down Expand Up @@ -100,24 +100,4 @@
'googleearth-compiled.js',
],
];

$wgHooks['MappingServiceLoad'][] = 'efMapsInitGoogleMaps3';
} );

/**
* Initialization function for the Google Maps v3 service.
*
* @since 0.6.3
* @ingroup MapsGoogleMaps3
*
* @return boolean true
*/
function efMapsInitGoogleMaps3() {
MapsMappingServices::registerService( 'googlemaps3', MapsGoogleMaps3::class );

// TODO: kill below code
$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
$googleMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );

return true;
}
20 changes: 1 addition & 19 deletions includes/services/Leaflet/Leaflet.php
Expand Up @@ -19,10 +19,7 @@
}

call_user_func( function() {
global $wgHooks, $wgResourceModules;

// Specify the function that will initialize the parser function.
$wgHooks['MappingServiceLoad'][] = 'efMapsInitLeaflet';
global $wgResourceModules;

$pathParts = ( explode( DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR, __DIR__, 2 ) );

Expand Down Expand Up @@ -91,18 +88,3 @@
],
];
} );

/**
* Initialization function for the Leaflet service.
*
* @ingroup Leaflet
*
* @return boolean true
*/
function efMapsInitLeaflet() {
MapsMappingServices::registerService( 'leaflet', MapsLeaflet::class );
$leafletMaps = MapsMappingServices::getServiceInstance( 'leaflet' );
$leafletMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );

return true;
}
14 changes: 1 addition & 13 deletions includes/services/OpenLayers/OpenLayers.php
Expand Up @@ -18,7 +18,7 @@
}

call_user_func( function() {
global $wgHooks, $wgResourceModules;
global $wgResourceModules;

$pathParts = ( explode( DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR, __DIR__, 2 ) );

Expand Down Expand Up @@ -46,16 +46,4 @@
'maps-searchmarkers-text',
]
];

$wgHooks['MappingServiceLoad'][] = 'efMapsInitOpenLayers';
} );

function efMapsInitOpenLayers() {
MapsMappingServices::registerService(
'openlayers',
MapsOpenLayers::class,
[ 'display_map' => MapsDisplayMapRenderer::class ]
);

return true;
}

0 comments on commit 3854624

Please sign in to comment.