Benutzer:Erik/Extension:Häfen in der Nähe: Unterschied zwischen den Versionen
Erik (Diskussion | Beiträge) →ToDo: Testumgebungen spezifiziert. |
Erik (Diskussion | Beiträge) →Installation: Neue Version kompatibel zu php 4 |
||
| Zeile 38: | Zeile 38: | ||
Das folgende Script (NearbyPlacesExtension.php) muss im Extensions-Verzeichnis abgelegt und die Zeile <code>require_once("./extensions/NearbyPlacesExtension.php");</code> muss zu der LocalSettings.php hinzugefügt werden. | Das folgende Script (NearbyPlacesExtension.php) muss im Extensions-Verzeichnis abgelegt und die Zeile <code>require_once("./extensions/NearbyPlacesExtension.php");</code> muss zu der LocalSettings.php hinzugefügt werden. | ||
<?php | <?php | ||
// HISTORY | |||
// 2007-03-04 : Fix for PHP 4 | |||
// Instead of DOM or DOM XML, the xml parser functions are used | |||
// 2007-03-02 : Initial implemtation | |||
// TODO | |||
// - debug output | |||
// - testing | |||
// - lat and lon in degrees and minutes | |||
// - improved retrival of places | |||
// - error handling | |||
// - config file | |||
// - documentation | |||
// - output data to be selected by attributes | |||
// register the extension | // register the extension | ||
| Zeile 48: | Zeile 63: | ||
'name' => 'Nearby Places', | 'name' => 'Nearby Places', | ||
'author' => 'Erik Hansen', | 'author' => 'Erik Hansen', | ||
'url' => 'http://www.skipperguide.de/wiki/Benutzer:Erik', | 'url' => 'http://www.skipperguide.de/wiki/Benutzer:Erik/Extension:H%C3%A4fen_in_der_N%C3%A4he', | ||
'description' => 'Shows a list of places near a given place', | 'description' => 'Shows a list of places near a given place', | ||
); | ); | ||
| Zeile 55: | Zeile 70: | ||
// initialize this extension | // initialize this extension | ||
// register nearbyplaces as parser hook. | // register nearbyplaces as parser hook. | ||
function wfNearbyPlacesExtension() { | function wfNearbyPlacesExtension() { | ||
global $wgParser; | global $wgParser; | ||
$wgParser->setHook( "nearbyplaces", "npeShowNearbyPlaces" ); | $wgParser->setHook( "nearbyplaces", "npeShowNearbyPlaces" ); | ||
} | } | ||
| Zeile 72: | Zeile 87: | ||
} | } | ||
// XML Parser Class | |||
class npeCParser { | |||
var $places; | |||
var $tag; | |||
var $description; | |||
var $placename; | |||
var $coordinates; | |||
var $parser; | |||
// constructor | |||
function npeCParser() { | |||
$this->places = array(); | |||
$this->tag = ""; | |||
$this->description = ""; | |||
$this->placename = ""; | |||
$this->coordinates = ""; | |||
} | |||
// initialize variables for every new placemark | |||
// store the current tag for use within die characterData function | |||
function startElement($parser, $name, $attrs) { | |||
if ($name == "PLACEMARK") { | |||
$this->description = ""; | |||
$this->placename = ""; | |||
$this->coordinates = ""; | |||
} | |||
$this->tag = $name; | |||
} | |||
// store the data from the closed placemark | |||
function endElement($parser, $name) { | |||
if ($name == "PLACEMARK") { | |||
$this->places[] = array ("name" => trim($this->placename), | |||
"description" => trim($this->description), | |||
"coordinates" => trim($this->coordinates)); | |||
} | |||
} | |||
// store the data of the tags | |||
function characterData($parser, $data) { | |||
switch ($this->tag) { | |||
case "NAME": | |||
$this->placename .= $data; | |||
break; | |||
case "DESCRIPTION": | |||
$this->description .= $data; | |||
break; | |||
case "COORDINATES": | |||
$this->coordinates .= $data; | |||
break; | |||
} | |||
} | |||
// parse the xml data | |||
function parse($xml) { | |||
$this->parser = xml_parser_create(); | |||
xml_set_object($this->parser, $this); | |||
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); | |||
xml_set_element_handler($this->parser, "startElement", "endElement"); | |||
xml_set_character_data_handler($this->parser, "characterData"); | |||
xml_parse($this->parser, $xml); | |||
xml_parser_free($this->parser); | |||
return $this->places; | |||
} | |||
} | |||
// this function | // this function | ||
| Zeile 109: | Zeile 201: | ||
// create a new dom instance and load the kml file into it | // create a new dom instance and load the kml file into it | ||
$xml = file_get_contents("http://www.skipperguide.de/extension/GoogleEarthExport.php"); | $xml = file_get_contents("http://www.skipperguide.de/extension/GoogleEarthExport.php"); | ||
$ | |||
$ | // create a new Parser and parse the retrieved kml file | ||
$parser = new npeCParser(); | |||
$places = $parser->parse($xml); | |||
// pick all placemarks from the kml-File | // pick all placemarks from the kml-File | ||
// In case none is found we are done already. | // In case none is found we are done already. | ||
if (count($places) == 0) | |||
return ("Es wurden leider keine Häfen gefunden (0)."); | |||
return ("Es wurden leider keine Häfen gefunden."); | |||
// parse through all palces from the kml file | // parse through all palces from the kml file | ||
foreach ($places as $place) { | foreach ($places as $place) { | ||
list($lon, $lat) = sscanf($place["coordinates"], "%f,%f"); | |||
// in case the difference in nm between the latitudes is | // in case the difference in nm between the latitudes is | ||
// lager than the distance, we do not hat to proceed any | // lager than the distance, we do not hat to proceed any | ||
// further with this place | // further with this place | ||
if (abs($lat - $req_lat) >> ($max_dist / 3600.0)) { | if (abs($lat - $req_lat) >> ($max_dist / 3600.0)) { | ||
continue; | continue; | ||
| Zeile 136: | Zeile 229: | ||
// In case the distance is larger than max_dist, | // In case the distance is larger than max_dist, | ||
// this place will be discarded. | // this place will be discarded. | ||
$b = $lat - $req_lat; | $b = $lat - $req_lat; | ||
$l = $lon - $req_lon; | $l = $lon - $req_lon; | ||
| Zeile 149: | Zeile 242: | ||
// in case we have come that far, the place will be added | // in case we have come that far, the place will be added | ||
// to the list of places within the requested range | // to the list of places within the requested range | ||
list($link) = sscanf($place["description"], "http://www.skipperguide.de/wiki/%s"); | |||
$nearby_places[] = array ("lat" => $lat, | |||
list($link) = sscanf($description, "http://www.skipperguide.de/wiki/%s"); | "lon" => $lon, | ||
"dist" => $dist, | |||
"name" => $place["name"], | |||
"link" => $link); | |||
} | } | ||
| Zeile 158: | Zeile 254: | ||
// check wether any places have been found. | // check wether any places have been found. | ||
if (count($nearby_places) == 0) | if (count($nearby_places) == 0) | ||
return ("Es wurden leider keine Häfen gefunden."); | return ("Es wurden leider keine Häfen gefunden (1)."); | ||
// sort all places using npeCmpPlacesByDist for comparation | // sort all places using npeCmpPlacesByDist for comparation | ||