A very basic functionality is back after the restructuring.
[philipp/winterrodeln/mediawiki_extensions/wrmap.git] / wrmap.body.php
1 <?php
2
3 // get value of key or default value if key does not exist
4 function array_get($key, $array, $default) {
5         if (array_key_exists($key, $array)) return $array[$key];
6         return $default;
7 }
8
9
10 // gets coordinates and returns an array of lon/lat coordinate pairs, e.g.
11 // 47.12 N 11.87 E
12 // 47.13 N 11.70 E
13 // ->
14 // array(array(11.87, 47.12), array(11.70, 47.13))
15 function geo_to_coordinates($input) {
16         $matches = array();
17         $num_matches = preg_match_all('/(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*/', $input, $matches);
18         $result = array();
19         for ($i=0; $i!=$num_matches; ++$i) {
20                 $result[] = array(floatval($matches[2][$i]), floatval($matches[1][$i]));
21         }
22         return $result;
23 }
24
25
26 // convert sledruns to geojson (http://www.geojson.org/geojson-spec.html)
27 // Returns an array of features
28 function sledruns_to_json_features() {
29         $json_features = array(); // result
30         $dbr = wfGetDB(DB_SLAVE);
31         $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')));
32         while ($sledrun = $dbr->fetchRow($res)) {
33                 $lat = $sledrun['position_latitude'];
34                 $lon = $sledrun['position_longitude'];
35                 if (is_null($lat) || is_null($lon)) continue;
36                 $lat = floatval($lat);
37                 $lon = floatval($lon);
38                 $title = Title::newFromText($sledrun['page_title']);
39                 $title_text = $title->getText();
40                 $title_url = $title->getLocalUrl();
41                 $condition = $sledrun['condition'];
42                 if (!is_null($condition)) $condition = intval($condition);
43                 $json_feature = array(
44                         'type' => 'feature',
45                         'geometry' => array(
46                                 'type' => 'Point',
47                                 'coordinates' => array($lon, $lat)
48                         ),
49                         'properties' => array(
50                                 'type' => 'sledrun',
51                                 'name' => $title_text,
52                                 'wiki' => $title_url,
53                                 'date_report' => $sledrun['date_report'],
54                                 'condition' => $condition
55                         )
56                 );
57                 $json_features[] = $json_feature;
58         }
59         $dbr->freeResult($res);
60         return $json_features;
61 }
62
63
64 // convert XML to geojson (http://www.geojson.org/geojson-spec.html)
65 // Returns an array of features
66 function xml_to_json_features($input) {
67         libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
68         $xml = new SimpleXMLElement($input); // input
69         $json_features = array(); // output
70         foreach ($xml as $feature) {
71                 // point
72                 if (in_array($feature->getName(), array('gasthaus', 'haltestelle', 'parkplatz', 'punkt'))) {
73                         $wiki = array_get('wiki', $feature, null);
74                         $name = array_get('name', $feature, null);
75                         $json_feature = array(
76                                 'type' => 'feature',
77                                 'geometry' => array(
78                                         'type' => 'Point',
79                                         'coordinates' => geo_to_coordinates($feature)[0]
80                                 ),
81                                 'properties' => array(
82                                         'type' => $feature->getName(),
83                                         'name' => $name,
84                                         'wiki' => $wiki
85                                 )
86                         );
87                         $json_features[] = $json_feature;
88                 }
89                 // line
90                 if (in_array($feature->getName(), array('rodelbahn', 'gehweg', 'alternative', 'lift', 'linie'))) {
91                         $color = array_get('farbe', $feature, null);
92                         $thickness = array_get('dicke', $feature, null);
93                         $json_feature = array(
94                                 'type' => 'feature',
95                                 'geometry' => array(
96                                         'type' => 'LineString',
97                                         'coordinates' => geo_to_coordinates($feature)
98                                 ),
99                                 'properties' => array(
100                                         'type' => $feature->getName(),
101                                         'color' => $color,
102                                         'thickness' => $thickness
103                                 )
104                         );
105                         $json_features[] = $json_feature;
106                 }
107         }
108         return $json_features;
109 }
110
111
112
113 class WrBaseMap {
114         /// Renders the <wrgmap> tag and the <wrmap> tag.
115         /// This class would be the only class needed but as the function render() toes not provide an argument
116         /// telling which tag name called the function, a trick with two inherited classes has to be used.
117         /// @param $content string - the content of the <wrgmap> tag
118         /// @param $args array - the array of attribute name/value pairs for the tag
119         /// @param $parser Parser - the MW Parser object for the current page
120         ///
121         /// @return string - the html for rendering the map
122         public static function render($content, $args, $parser, $frame) {
123                 // Unfortunately, $tagname is no argument of this function, therefore we have to use a trick with derived classes.
124                 $tagname = strtolower(get_called_class()); // either wrmap or wrgmap
125                 assert(in_array($tagname, array('wrmap', 'wrgmap')));
126
127                 // Get center and zoom level from $args
128                 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648;   // latitude as float value
129                 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655; // longitude as float value
130                 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
131                 if (isset($args['width'])) $width = intval($args['width']); else $width = null; // null corresponds to 100%
132                 if (isset($args['height'])) $height = intval($args['height']); else $height = 450;
133                 $latitude_s = sprintf('%.6F', $latitude);
134                 $longitude_s = sprintf('%.6F', $longitude);
135                 $width_s = is_null($width) ? '100%' : $width . 'px';
136                 $height_s = $height . 'px';
137                 $show_sledruns = ($tagname == 'wrgmap');
138
139                 $parserOutput = $parser->getOutput();
140                 $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&amp;sensor=false"></script>', 'googlemaps');
141                 $parserOutput->addModules('ext.wrmap');
142                 
143                 $json_features = array();
144
145                 // append all sledruns as icon
146                 if ($show_sledruns) {
147                         $json_features = array_merge($json_features, sledruns_to_json_features());
148                 }
149
150                 // append all elements in the XML
151                 try {
152                         $json_features = array_merge($json_features, xml_to_json_features('<wrmap>' . $content . '</wrmap>'));
153                 } catch (Exception $e) {
154                         return htmlspecialchars($e);
155                 }
156
157                 // create final geojson
158                 $json = array(
159                         'type' => 'FeatureCollection',
160                         'features' => $json_features
161                 );
162                 $json_string = json_encode($json);
163
164                 // Create <div/> element where the map is placed in
165                 global $wgExtensionAssetsPath;
166                 $output = "<div class=\"wrmap\" style=\"width: $width_s; height: $height_s; border-style:none;\" data-center-lon=\"$longitude_s\" data-center-lat=\"$latitude_s\" data-zoom=\"$zoom\" data-img-path=\"$wgExtensionAssetsPath/wrmap/openlayers/img/\">";
167                 $output .= "<script type=\"application/json\">";
168                 $output .= htmlspecialchars($json_string, ENT_NOQUOTES);
169                 $output .= "</script>";
170                 $output .= "</div>\n";
171                 
172                 return $output;
173         }
174 }
175
176
177 // <wrmap> tag
178 class WrMap extends WrBaseMap {
179 }
180
181
182 // <wrgmap> tag
183 class WrGMap extends WrBaseMap {
184 }
185
186 ?>