3 // gets coordinates and returns an array of lon/lat coordinate pairs, e.g.
7 // array(array(11.87, 47.12), array(11.70, 47.13))
8 function geo_to_coordinates($input) {
10 $num_matches = preg_match_all('/\s*(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*/', $input, $matches);
12 for ($i=0; $i!=$num_matches; ++$i) {
13 $result[] = array(floatval($matches[2][$i]), floatval($matches[1][$i]));
15 if (implode($matches[0]) != $input) throw new Exception('Falsches Koordinatenformat: ' . $input);
20 // convert sledruns to geojson (http://www.geojson.org/geojson-spec.html)
21 // Returns an array of features
22 function sledruns_to_json_features() {
23 $json_features = array(); // result
24 $dbr = wfGetDB(DB_SLAVE);
25 $res = $dbr->select(array('wrsledruncache', 'wrreportcache'), array('wrsledruncache.page_title', 'position_latitude', 'position_longitude', 'date_report', '`condition`'), array('show_in_overview', 'not under_construction'), __METHOD__, array(), array('wrreportcache' => array('left outer join', 'wrsledruncache.page_id=wrreportcache.page_id')));
26 while ($sledrun = $dbr->fetchRow($res)) {
27 $lat = $sledrun['position_latitude'];
28 $lon = $sledrun['position_longitude'];
29 if (is_null($lat) || is_null($lon)) continue;
30 $lat = floatval($lat);
31 $lon = floatval($lon);
32 $title = Title::newFromText($sledrun['page_title']);
33 $properties = array('type' => 'sledrun', 'name' => $title->getText(), 'wiki' => $title->getLocalUrl());
34 if (!is_null($sledrun['date_report'])) $properties[] = $sledrun['date_report'];
35 if (!is_null($sledrun['condition'])) $properties[] = intval($sledrun['condition']);
36 $json_feature = array(
40 'coordinates' => array($lon, $lat)
42 'properties' => $properties
44 $json_features[] = $json_feature;
46 $dbr->freeResult($res);
47 return $json_features;
51 // convert XML to geojson (http://www.geojson.org/geojson-spec.html)
52 // Returns an array of features
53 function xml_to_json_features($input) {
54 libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
55 $xml = new SimpleXMLElement($input); // input
56 $whitespace = (string) $xml; // everything between <wrmap> and </wrmap> that's not a sub-element
57 if (strlen($whitespace) > 0 && !ctype_space($whitespace)) { // there must not be anythin except sub-elements or whitespace
58 throw new Exception('Die Landkarte enthält folgenden ungültigen Text: "' . trim($xml) . '".');
60 $json_features = array(); // output
61 $point_types = array('gasthaus', 'haltestelle', 'parkplatz', 'achtung', 'punkt');
62 $line_types = array('rodelbahn', 'gehweg', 'alternative', 'lift', 'anfahrt', 'linie');
63 foreach ($xml as $feature) {
64 $given_properties = array();
65 foreach ($feature->attributes() as $key => $value) $given_properties[] = $key;
67 // determine feature type
68 $is_point = in_array($feature->getName(), $point_types);
69 $is_line = in_array($feature->getName(), $line_types);
70 if (!$is_point && !$is_line) {
71 throw new Exception('Unbekanntes Element <' . $feature->getName() . '>. Erlaubt sind: <' . implode('>, <', array_merge($point_types, $line_types)) . '>.');
76 $properties = array('type' => $feature->getName());
77 $allowed_properties = array('name', 'wiki');
78 $wrong_properties = array_diff($given_properties, $allowed_properties);
79 if (count($wrong_properties) > 0) throw new Exception("Das Attribut '" . reset($wrong_properties) . "' ist nicht erlaubt bei <" . $feature->getName() . ">. Erlaubt sind: '" . implode("', '", $allowed_properties) . "'.");
80 foreach ($given_properties as $property) {
81 $propval = (string) $feature[$property];
82 if ($property == 'wiki') {
83 $title = Title::newFromText($propval);
84 $propval = $title->getLocalUrl();
86 $properties[$property] = $propval;
88 $coordinates = geo_to_coordinates($feature);
89 if (count($coordinates) != 1) throw new Exception('Das Element <' . $feature->getName() . '> muss genau ein Koordinatenpaar haben.');
90 $json_feature = array(
94 'coordinates' => reset($coordinates)
96 'properties' => $properties
98 $json_features[] = $json_feature;
102 $properties = array('type' => $feature->getName());
103 $allowed_properties = array('farbe', 'dicke');
104 $wrong_properties = array_diff($given_properties, $allowed_properties);
105 if (count($wrong_properties) > 0) throw new Exception("Das Attribut '" . reset($wrong_properties) . "' ist nicht erlaubt bei <" . $feature->getName() . ">. Erlaubt sind: '" . implode("', '", $allowed_properties) . "'.");
106 if (isset($feature['farbe'])) {
107 $color = (string) $feature['farbe']; // e.g. #a200b7
108 if (preg_match('/^#[0-9a-f]{6}$/i', $color) != 1)
109 throw new Exception('Die Farbangabe der Linie hat ein falsches Format. Sie muss z.B. so aussehen: #a200b7.');
110 $properties['strokeColor'] = $color;
112 if (isset($feature['dicke'])) {
113 $stroke_width = (int) $feature['dicke']; // e.g. 6
114 if (((string) $stroke_width) !== (string) $feature['dicke'])
115 throw new Exception('Die Angabe der Liniendicke hat ein falsches Format. Sie muss eine ganze Zahl wie z.B. 6 sein.');
116 $properties['strokeWidth'] = $stroke_width;
118 $json_feature = array(
121 'type' => 'LineString',
122 'coordinates' => geo_to_coordinates($feature)
124 'properties' => $properties
126 $json_features[] = $json_feature;
129 return $json_features;
135 /// Renders the <wrgmap> tag and the <wrmap> tag.
136 /// This class would be the only class needed but as the function render() toes not provide an argument
137 /// telling which tag name called the function, a trick with two inherited classes has to be used.
138 /// @param $content string - the content of the <wrgmap> tag
139 /// @param $args array - the array of attribute name/value pairs for the tag
140 /// @param $parser Parser - the MW Parser object for the current page
142 /// @return string - the html for rendering the map
143 public static function render($content, $args, $parser, $frame) {
144 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
145 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
146 assert(in_array($tagname, array('wrmap', 'wrgmap')));
148 $parserOutput = $parser->getOutput();
149 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&sensor=false"></script>', 'googlemaps');
150 $parserOutput->addModules('ext.wrmap');
152 // append all sledruns as icon
153 $json_features = array();
154 $show_sledruns = ($tagname == 'wrgmap');
155 if ($show_sledruns) {
156 $json_features = array_merge($json_features, sledruns_to_json_features());
161 $properties = array();
162 if (isset($args['lat'])) $properties['lat'] = (float) $args['lat']; // latitude as float value
163 if (isset($args['lon'])) $properties['lon'] = (float) $args['lon']; // longitude as float value
164 if (isset($args['zoom'])) $properties['zoom'] = (int) $args['zoom']; // zoom as int value
165 if (isset($args['width'])) $properties['width'] = (int) $args['width']; // width as int value
166 if (isset($args['height'])) $properties['height'] = (int) $args['height']; // height as int value
168 // append all elements in the XML
169 $json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
170 } catch (Exception $e) {
171 return '<div class="error">' . htmlspecialchars("Fehler beim Parsen der Landkarte: " . $e->getMessage()) . '</div>';
174 // create final geojson
176 'type' => 'FeatureCollection',
177 'features' => $json_features,
178 'properties' => $properties
180 $json_string = json_encode($json);
182 // Create <div/> element where the map is placed in
183 global $wgExtensionAssetsPath;
184 $output = "<div class=\"wrmap\" style=\"border-style:none;\" data-img-path=\"$wgExtensionAssetsPath/wrmap/openlayers/img/\">";
185 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
186 $output .= "</div>\n";
188 return array($output, 'markerType' => 'nowiki');
194 class WrMap extends WrBaseMap {
199 class WrGMap extends WrBaseMap {