SemanticMediaWiki /
SemanticMaps
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Initialization file for the Semantic Maps extension. |
||
| 5 | * |
||
| 6 | * @licence GNU GPL v2+ |
||
| 7 | * @author Jeroen De Dauw < [email protected] > |
||
| 8 | */ |
||
| 9 | |||
| 10 | if ( !defined( 'MEDIAWIKI' ) ) { |
||
| 11 | die( 'Not an entry point.' ); |
||
| 12 | } |
||
| 13 | |||
| 14 | if ( defined( 'SM_VERSION' ) ) { |
||
| 15 | // Do not initialize more than once. |
||
| 16 | return 1; |
||
| 17 | } |
||
| 18 | |||
| 19 | if ( version_compare( $GLOBALS['wgVersion'], '1.23c', '<' ) ) { |
||
| 20 | throw new Exception( |
||
| 21 | 'This version of Semantic Maps requires MediaWiki 1.23 or above; use Semantic Maps 3.3.x for older versions.' |
||
| 22 | . ' See https://github.com/SemanticMediaWiki/SemanticMaps/blob/master/INSTALL.md for more info.' |
||
| 23 | ); |
||
| 24 | } |
||
| 25 | |||
| 26 | if ( !defined( 'Maps_VERSION' ) && is_readable( __DIR__ . '/vendor/autoload.php' ) ) { |
||
| 27 | include_once( __DIR__ . '/vendor/autoload.php' ); |
||
| 28 | } |
||
| 29 | |||
| 30 | if ( !defined( 'Maps_VERSION' ) ) { |
||
| 31 | throw new Exception( 'You need to have Maps installed in order to use Semantic Maps' ); |
||
| 32 | } |
||
| 33 | |||
| 34 | define( 'SM_VERSION', '3.4.0' ); |
||
| 35 | |||
| 36 | require_once __DIR__ . '/SM_Settings.php'; |
||
| 37 | |||
| 38 | SemanticMaps::newFromMediaWikiGlobals( $GLOBALS )->initExtension(); |
||
| 39 | |||
| 40 | $GLOBALS['wgExtensionFunctions'][] = function() { |
||
| 41 | // Hook for initializing the Geographical Data types. |
||
| 42 | $GLOBALS['wgHooks']['SMW::DataType::initTypes'][] = 'SemanticMapsHooks::initGeoDataTypes'; |
||
| 43 | |||
| 44 | // Hook for defining the default query printer for queries that ask for geographical coordinates. |
||
| 45 | $GLOBALS['wgHooks']['SMWResultFormat'][] = 'SemanticMapsHooks::addGeoCoordsDefaultFormat'; |
||
| 46 | |||
| 47 | // Hook for adding a Semantic Maps links to the Admin Links extension. |
||
| 48 | $GLOBALS['wgHooks']['AdminLinks'][] = 'SemanticMapsHooks::addToAdminLinks'; |
||
| 49 | |||
| 50 | $GLOBALS['wgHooks']['sfFormPrinterSetup'][] = 'SemanticMaps\FormInputsSetup::run'; |
||
| 51 | }; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @codeCoverageIgnore |
||
| 55 | */ |
||
| 56 | class SemanticMaps { |
||
| 57 | |||
| 58 | private $mwGlobals; |
||
| 59 | |||
| 60 | public static function newFromMediaWikiGlobals( array &$mwGlobals ) { |
||
| 61 | return new self( $mwGlobals ); |
||
| 62 | } |
||
| 63 | |||
| 64 | private function __construct( array &$mwGlobals ) { |
||
| 65 | $this->mwGlobals =& $mwGlobals; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @since 3.4 |
||
| 70 | */ |
||
| 71 | public function initExtension() { |
||
| 72 | $this->mwGlobals['wgExtensionCredits']['semantic'][] = [ |
||
| 73 | 'path' => __FILE__, |
||
| 74 | 'name' => 'Semantic Maps', |
||
| 75 | 'version' => SM_VERSION, |
||
| 76 | 'author' => [ |
||
| 77 | '[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]' |
||
| 78 | ], |
||
| 79 | 'url' => 'https://github.com/SemanticMediaWiki/SemanticMaps/blob/master/README.md#semantic-maps', |
||
| 80 | 'descriptionmsg' => 'semanticmaps-desc', |
||
| 81 | 'license-name' => 'GPL-2.0+' |
||
| 82 | ]; |
||
| 83 | |||
| 84 | $this->registerResourceModules(); |
||
| 85 | |||
| 86 | $this->registerGoogleMaps(); |
||
| 87 | $this->registerLeaflet(); |
||
| 88 | $this->registerOpenLayers(); |
||
| 89 | |||
| 90 | $this->initializeQueryPrinters(); |
||
| 91 | |||
| 92 | // Internationalization |
||
| 93 | $this->mwGlobals['wgMessagesDirs']['SemanticMaps'] = __DIR__ . '/i18n'; |
||
| 94 | } |
||
| 95 | |||
| 96 | private function registerResourceModules() { |
||
| 97 | $moduleTemplate = [ |
||
| 98 | 'position' => 'bottom', |
||
| 99 | 'group' => 'ext.semanticmaps', |
||
| 100 | ]; |
||
| 101 | |||
| 102 | $this->mwGlobals['wgResourceModules']['ext.sm.forminputs'] = $moduleTemplate + [ |
||
| 103 | 'dependencies' => [ 'ext.maps.coord' ], |
||
| 104 | 'localBasePath' => __DIR__ . '/src/forminputs', |
||
| 105 | 'remoteExtPath' => 'SemanticMaps/src/forminputs', |
||
| 106 | 'scripts' => [ |
||
| 107 | 'jquery.mapforminput.js' |
||
| 108 | ], |
||
| 109 | 'messages' => [ |
||
| 110 | 'semanticmaps_enteraddresshere', |
||
| 111 | 'semanticmaps-updatemap', |
||
| 112 | 'semanticmaps_lookupcoordinates', |
||
| 113 | 'semanticmaps-forminput-remove', |
||
| 114 | 'semanticmaps-forminput-add', |
||
| 115 | 'semanticmaps-forminput-locations' |
||
| 116 | ] |
||
| 117 | ]; |
||
| 118 | |||
| 119 | $this->mwGlobals['wgResourceModules']['ext.sm.common'] = $moduleTemplate + [ |
||
| 120 | 'localBasePath' => __DIR__ . '/src', |
||
| 121 | 'remoteExtPath' => 'SemanticMaps/src', |
||
| 122 | 'scripts' => [ |
||
| 123 | 'ext.sm.common.js' |
||
| 124 | ] |
||
| 125 | ]; |
||
| 126 | } |
||
| 127 | |||
| 128 | private function registerGoogleMaps() { |
||
| 129 | $moduleTemplate = [ |
||
| 130 | 'localBasePath' => __DIR__ . '/src/services/GoogleMaps3', |
||
| 131 | 'remoteExtPath' => 'SemanticMaps/src/services/GoogleMaps3', |
||
| 132 | 'group' => 'ext.semanticmaps', |
||
| 133 | ]; |
||
| 134 | |||
| 135 | $this->mwGlobals['wgResourceModules']['ext.sm.fi.googlemaps3ajax'] = $moduleTemplate + [ |
||
| 136 | 'dependencies' => [ |
||
| 137 | 'ext.maps.googlemaps3', |
||
| 138 | 'ext.sm.common' |
||
| 139 | ], |
||
| 140 | 'scripts' => [ |
||
| 141 | 'ext.sm.googlemaps3ajax.js' |
||
| 142 | ] |
||
| 143 | ]; |
||
| 144 | |||
| 145 | $this->mwGlobals['wgResourceModules']['ext.sm.fi.googlemaps3'] = $moduleTemplate + [ |
||
| 146 | 'dependencies' => [ |
||
| 147 | 'ext.sm.fi.googlemaps3.single', |
||
| 148 | ], |
||
| 149 | 'scripts' => [ |
||
| 150 | 'ext.sm.googlemapsinput.js', |
||
| 151 | ], |
||
| 152 | ]; |
||
| 153 | |||
| 154 | $this->mwGlobals['wgResourceModules']['ext.sm.fi.googlemaps3.single'] = $moduleTemplate + [ |
||
| 155 | 'dependencies' => [ |
||
| 156 | 'ext.maps.googlemaps3', |
||
| 157 | 'ext.sm.forminputs', |
||
| 158 | ], |
||
| 159 | 'scripts' => [ |
||
| 160 | 'jquery.googlemapsinput.js', |
||
| 161 | ], |
||
| 162 | 'messages' => [ |
||
| 163 | ] |
||
| 164 | ]; |
||
| 165 | |||
| 166 | $this->mwGlobals['wgHooks']['MappingServiceLoad'][] = function() { |
||
| 167 | MapsMappingServices::registerServiceFeature( 'googlemaps3', 'qp', 'SMMapPrinter' ); |
||
| 168 | MapsMappingServices::registerServiceFeature( 'googlemaps3', 'fi', 'SMGoogleMaps3FormInput' ); |
||
| 169 | |||
| 170 | /* @var MapsMappingService $googleMaps */ |
||
| 171 | $googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' ); |
||
| 172 | $googleMaps->addResourceModules( array( 'ext.sm.fi.googlemaps3ajax' ) ); |
||
| 173 | |||
| 174 | return true; |
||
| 175 | }; |
||
| 176 | } |
||
| 177 | |||
| 178 | private function registerLeaflet() { |
||
| 179 | $this->mwGlobals['wgResourceModules']['ext.sm.fi.leafletajax'] = [ |
||
| 180 | 'localBasePath' => __DIR__ . '/src/services/Leaflet', |
||
| 181 | 'remoteExtPath' => 'SemanticMaps/src/services/Leaflet', |
||
| 182 | 'group' => 'ext.semanticmaps', |
||
| 183 | 'dependencies' => [ |
||
| 184 | 'ext.maps.leaflet', |
||
| 185 | 'ext.sm.common' |
||
| 186 | ], |
||
| 187 | 'scripts' => [ |
||
| 188 | 'ext.sm.leafletajax.js' |
||
| 189 | ] |
||
| 190 | ]; |
||
| 191 | |||
| 192 | $this->mwGlobals['wgHooks']['MappingServiceLoad'][] = function() { |
||
| 193 | MapsMappingServices::registerServiceFeature( 'leaflet', 'qp', 'SMMapPrinter' ); |
||
| 194 | |||
| 195 | /* @var MapsMappingService $leaflet */ |
||
| 196 | $leaflet = MapsMappingServices::getServiceInstance( 'leaflet' ); |
||
| 197 | $leaflet->addResourceModules( array( 'ext.sm.fi.leafletajax' ) ); |
||
| 198 | |||
| 199 | return true; |
||
| 200 | }; |
||
| 201 | } |
||
| 202 | |||
| 203 | private function registerOpenLayers() { |
||
| 204 | $this->mwGlobals['wgResourceModules']['ext.sm.fi.openlayersajax'] = [ |
||
| 205 | 'localBasePath' => __DIR__ . '/src/services/OpenLayers', |
||
| 206 | 'remoteExtPath' => 'SemanticMaps/src/services/OpenLayers', |
||
| 207 | 'group' => 'ext.semanticmaps', |
||
| 208 | 'dependencies' => [ |
||
| 209 | 'ext.maps.openlayers', |
||
| 210 | 'ext.sm.common' |
||
| 211 | ], |
||
| 212 | 'scripts' => [ |
||
| 213 | 'ext.sm.openlayersajax.js' |
||
| 214 | ] |
||
| 215 | ]; |
||
| 216 | |||
| 217 | $this->mwGlobals['wgHooks']['MappingServiceLoad'][] = function() { |
||
| 218 | MapsMappingServices::registerServiceFeature( 'openlayers', 'qp', SMMapPrinter::class ); |
||
| 219 | |||
| 220 | /* @var MapsMappingService $openlayers */ |
||
| 221 | $openlayers = MapsMappingServices::getServiceInstance( 'openlayers' ); |
||
| 222 | $openlayers->addResourceModules( array( 'ext.sm.fi.openlayersajax' ) ); |
||
| 223 | |||
| 224 | return true; |
||
| 225 | }; |
||
| 226 | } |
||
| 227 | |||
| 228 | private function initializeQueryPrinters() { |
||
|
0 ignored issues
–
show
|
|||
| 229 | $GLOBALS['smwgResultFormats']['kml'] = SMKMLPrinter::class; |
||
| 230 | |||
| 231 | foreach ( MapsMappingServices::getServiceIdentifiers() as $serviceIdentifier ) { |
||
| 232 | $service = MapsMappingServices::getServiceInstance( $serviceIdentifier ); |
||
| 233 | |||
| 234 | // Check if the service has a query printer. |
||
| 235 | $QPClass = $service->getFeature( 'qp' ); |
||
| 236 | |||
| 237 | // If the service has no QP, skip it and continue with the next one. |
||
| 238 | if ( $QPClass === false ) { |
||
| 239 | continue; |
||
| 240 | } |
||
| 241 | |||
| 242 | // Initiate the format. |
||
| 243 | $aliases = $service->getAliases(); |
||
| 244 | |||
| 245 | // Add the 'map' result format if there are mapping services that have QP's loaded. |
||
| 246 | if ( $GLOBALS['egMapsDefaultServices']['qp'] == $serviceIdentifier ) { |
||
| 247 | $aliases[] = 'map'; |
||
| 248 | } |
||
| 249 | |||
| 250 | // Add the QP to SMW. |
||
| 251 | $GLOBALS['smwgResultFormats'][$service->getName()] = SMMapPrinter::class; |
||
| 252 | |||
| 253 | $GLOBALS['smwgResultAliases'][$service->getName()] = $aliases; |
||
| 254 | } |
||
| 255 | |||
| 256 | return true; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @since 3.4 |
||
| 261 | * |
||
| 262 | * @return string|null |
||
| 263 | */ |
||
| 264 | public static function getVersion() { |
||
| 265 | return SM_VERSION; |
||
| 266 | } |
||
| 267 | |||
| 268 | } |
||
| 269 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: