<?php
-require_once dirname(__FILE__) . '/../wrgeo/wrgeo.body.php'; # for the function wrGeoGeoStringToGeo
-function wrMapParserFirstCallInit() {
- global $wgParser;
- $wgParser->setHook('wrmap', 'wrmapParserHook');
- return true;
-}
-
-
-/// Format inside <wrmap>...</wrmap> has to be like this:
-///
-/// <wrmap>
-/// Rodelbahn|47.143241 N 11.208959 E|Birgitzer Alm
-/// ...
-/// </wrmap>
-///
-/// The extension produces a format like this:
-/// <googlemap version="0.9" lat="47.241016" lon="11.56517" zoom="11">
-/// 47.241731, 11.358994, Birgitzer Alm
-/// Die Birgitzer Alm ist nett
-/// 47.17607, 11.542763, Naviser Hütte
-/// Die Naviser Hütte
-/// </googlemap>
-function wrmapParserHook($input, $args, $parser) {
- $debug = (isset($args['debug']));
-
- $output = '';
- $lines = explode("\n", $input);
- $latitudes = array();
- $longitudes = array();
- foreach ($lines as $line) {
- try {
- $l = trim($line);
- if (strlen($l) == 0) continue;
- $columns = explode('|', $line);
- if (count($columns) != 3) throw new Exception(sprintf(utf8_encode('Die Anzahl der Spalten ist nicht 3 sondern %d'), count($columns)));
- $columns = list($type, $geo, $name) = $columns;
- list($latitude, $longitude) = wrGeoStringToGeo($geo);
- $latitudes[] = $latitude;
- $longitudes[] = $longitude;
- $output .= sprintf("%F, %F, %s\n", $latitude, $longitude, htmlspecialchars($name));
- } catch (Exception $e) {
- return sprintf(utf8_encode('Ungültige Zeile in der Koordinatenliste: <em>%s</em>. %s'), htmlspecialchars($line), htmlspecialchars($e->getMessage()));
+class WrMap {
+ /// Renders the <wrgmap> tag
+ /// @param $content string - the content of the <wrgmap> tag
+ /// @param $args array - the array of attribute name/value pairs for the tag
+ /// @param $parser Parser - the MW Parser object for the current page
+ ///
+ /// @return string - the html for rendering the map
+ public static function render($content, $args, $parser, $frame) {
+ // Get center and zoom level from $args
+ if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648; // latitude as float value
+ if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655; // longitude as float value
+ if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
+ $latitude_s = sprintf('%.6F', $latitude);
+ $longitude_s = sprintf('%.6F', $longitude);
+
+ // Query database
+ $dbr = wfGetDB(DB_SLAVE);
+ $res = $dbr->select('wrsledruncache', array('page_title', 'position_latitude', 'position_longitude'), array('show_in_overview', 'not under_construction'));
+ $sledruns = array();
+ while ($sledrun = $dbr->fetchRow($res)) $sledruns[] = $sledrun;
+ $dbr->freeResult($res);
+
+ $parserOutput = $parser->getOutput();
+ $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
+ $parserOutput->addModules('ext.wrmap');
+
+ // Create <div/> element where the map is placed in
+ $output = "<div class=\"wrmap\" style=\"width: 100%; height: 450px; border-style:none;\" data-center-lon=\"$longitude_s\" data-center-lat=\"$latitude_s\" data-zoom=\"$zoom\">\n";
+ foreach ($sledruns as $sledrun) {
+ $lat = $sledrun['position_latitude'];
+ $lon = $sledrun['position_longitude'];
+ if (!$lat || !$lon) continue;
+ $output .= "<p data-lon=\"$lon\" data-lat=\"$lat\" data-title=\"{$sledrun['page_title']}\" />\n";
}
+ $output .= "</div>\n";
+
+ return $output;
}
- if (count($latitudes) == 0) return utf8_encode('Keine Koordinaten eingetragen');
- $minLatitude = min($latitudes);
- $centerLat = $minLatitude + (max($latitudes) - $minLatitude) / 2;
- $minLongitude = min($longitudes);
- $centerLon = $minLongitude + (max($longitudes) - $minLongitude) / 2;
-
- // TODO: Varable zoom level
- $output = '<googlemap version="0.9" lat="' . $centerLat . '" lon="' . $centerLon . '" type="terrain" zoom="9">'. "\n" . $output . "</googlemap>\n";
-
- if ($debug) return $output = "<pre><nowiki>$output</nowiki></pre>";
-
- /*
- global $wgTitle, $wgUser;
- $parser = new Parser();
- $parserOptions = new ParserOptions();
- $parserOptions->initialiseFromUser($wgUser);
- $result = $parser->parse($output, $wgTitle, $parserOptions); // TODO: Make this call less complicated
- return $result->getText();
- */
-
- $output = $parser->recursiveTagParse($output); // TODO: Maybe this is already the solution?
- return $output;
}