<?php
require_once( dirname( __FILE__ ) . '/GoogleMapAPI.class.php' );
$wgExtensionFunctions[] = 'wfGoogleMaps';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'YouTube',
'description' => 'Display Google Maps'
);
function wfGoogleMaps() {
global $wgParser, $wgGoogleMapsKeys, $wgGoogleMapsKey;
if ( !empty( $wgGoogleMapsKeys ) ) {
preg_match(
"!^(?P<host>(?:(?P<subdomain>[\w\.]+)\.)?" . "(?P<domain>\w+\.(?P<extension>\w+)))!",
$_SERVER['SERVER_NAME'],
$matches
);
if ( !empty( $matches ) ) {
if ( !empty( $wgGoogleMapsKeys[$matches['domain']] ) ) {
$wgGoogleMapsKey = $wgGoogleMapsKeys[$matches['domain']];
}
} else {
if ( !empty( $wgGoogleMapsKeys[$_SERVER['SERVER_NAME']] ) ) {
$wgGoogleMapsKey = $wgGoogleMapsKeys[$_SERVER['SERVER_NAME']];
}
}
}
$wgParser->setHook( 'googlemaps', 'renderGoogleMaps' );
}
function renderGoogleMaps( $input, $params ) {
global $wgGoogleMapsKey, $wgOut, $wgJsMimeType, $wgDBuser, $wgDBpassword, $wgDBserver, $wgDBprefix;
switch ( $params['type'] ) {
case '0':
$params['type'] = 'map'; break;
case '1':
$params['type'] = 'satellite'; break;
case '2':
$params['type'] = 'hybrid'; break;
default:
$params['type'] = 'map';
}
$hash = md5( serialize( $params ) );
$map = new BusinesswikiGoogleMapAPI( $hash );
$map->setAPIKey( $wgGoogleMapsKey );
$map->disableDirections();
$map->disableOverviewControl();
$map->disableScaleControl();
$map->disableSidebar();
// $map->disableZoomEncompass();
$map->setControlSize( 'small' );
$map->setWidth( $params['width'] );
$map->setHeight( $params['height'] );
$map->setCenterCoords( $params['lon'], $params['lat'] );
$map->setZoomLevel( $params['zoom'] );
$map->setMapType( $params['type'] );
$markers = json_decode( str_replace( "'", '"', $params['markers'] ), true );
foreach ( $markers as $m ) {
if ( empty( $m['lon'] ) || empty( $m['lat'] ) || empty( $m['text'] ) ) {
continue;
}
$map->addMarkerByCoords( $m['lon'], $m['lat'], $m['text'] );
}
$wgOut->addHeadItem( 'googleMapsHeader', $map->getHeaderJS() );
$wgOut->addHeadItem( "googleMapsJs_{$hash}", $map->getMapJS() );
$wgOut->addHeadItem( 'googleMapsCss', '<style type="text/css"> v\:* { behavior:url(#default#VML); } </style>' );
$wgOut->addHeadItem( "googleMapsOnLoad_{$hash}", '<script type="'. $wgJsMimeType .'">addOnloadHook(onLoad_' . $hash . ');</script>' );
return $map->getMap();
}
$wgHooks['MakeGlobalVariablesScript'][] = 'keyGoogleMaps';
function keyGoogleMaps( &$vars ) {
global $wgGoogleMapsKey;
if ( !empty( $wgGoogleMapsKey ) ) {
$vars['wgGoogleMapsKey'] = $wgGoogleMapsKey;
} else {
$vars['wgGoogleMapsKey'] = '';
}
return true;
}
class BusinesswikiGoogleMapAPI extends GoogleMapAPI {
/**
* get the geocode lat/lon points from cache for given address
*
* @param string $address
*/
public function getCache( $address ) {
$dbr =& wfGetDB( DB_SLAVE );
if ( !$dbr->tableExists( 'geocodes' ) ) {
return false;
}
$row = $dbr->selectRow(
'geocodes',
array( 'lon', 'lat' ),
array( 'address' => $address ),
__METHOD__
);
return $row;
}
/**
* put the geocode lat/lon points into cache for given address
*
* @param string $address
* @param string $lon the map latitude (horizontal)
* @param string $lat the map latitude (vertical)
*/
public function putCache( $address, $lon, $lat ) {
$dbw =& wfGetDB( DB_MASTER );
if (
empty( $address ) || empty( $lon )
|| empty( $lat ) || !$dbr->tableExists( 'geocodes' )
) {
return false;
}
$dbw->insert(
'geocodes',
array( 'address' => $address, 'lon' => $lon, 'lat' => $lat ),
__METHOD__
);
return true;
}
}