Update extension.json to version 2 with "updateExtensionJsonSchema.php".
[philipp/winterrodeln/mediawiki_extensions/wrsemantic.git] / wrsemantic_body.php
1 <?php
2 /*
3 Example:
4
5 <script type="application/ld+json">
6 {
7   "@context": "http://schema.org",
8   "@type": "Place",
9   "name": "Melchsee Frutt",
10   "url": "http://www.winterrodeln.org/wiki/Melchsee_Frutt",
11   "description": "Rodelbahn",
12   "image": "http://www.winterrodeln.org/mediawiki/images/4/47/Rodelbahn_Melchsee_Frutt_2014-02-15.jpg",
13   "telephone": "+41-41-6697070",
14   "geo": {
15      "@type": "GeoCoordinates",
16      "longitude": 8.262508,
17      "latitude": 46.782709
18   }
19 }
20 </script>
21
22 */
23
24
25 class WrSemantic {
26         /// Splits a Winterrodeln phone number string like "+41-41-6697070 (Info)" to array("+41-41-6697070", "Info").
27         static function split_wr_phone($phone_str) {
28                 $machtes = array();
29                 if (preg_match('/([-+\d]+) +\((.+)\)/', $phone_str, $matches)) return array_slice($matches, 1);
30                 return array(NULL, NULL);
31         }
32
33
34         /// Returns an array representing the JSON-LD information
35         static function json_ld_sledrun_compose($title, $row, $file_image) {
36                 $json = array(
37                         "@context" => "http://schema.org",
38                         "@type" => "Place",
39                         "name" => $title->getBaseText(),
40                         "url" => $title->getCanonicalURL(),
41                         "description" => "Rodelbahn"
42                 );
43                 if (!is_null($file_image)) $json["image"] = $file_image->getFullUrl();
44                 if (!is_null($row->information_phone)) $json["telephone"] = self::split_wr_phone($row->information_phone)[0];
45                 $lon = $row->position_longitude;
46                 $lat = $row->position_latitude;
47                 if (!is_null($lon) && !is_null($lat)) $json["geo"] = array("@type" => "GeoCoordinates", "longitude" => $lon, "latitude" => $lat);
48                 return $json;
49         }
50
51
52         /// Adds a JSON representation of JSON-LD to the page header
53         static function json_ld_add(OutputPage &$out, ParserOutput $parseroutput, $json) {
54                 $script = "<script type=\"application/ld+json\">\n" . htmlspecialchars(json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), ENT_NOQUOTES) . "\n</script>";
55                 $out->addHeadItem('wrsemantic-ld-json', $script);
56         }
57
58
59         /// Adds a Twitter Summary Card to the page header (https://dev.twitter.com/cards/types/summary)
60         static function twitter_summary_card_sledrun_add(OutputPage &$out, ParserOutput $parseroutput, $title, $row, $file_image) {
61                 $out->addMeta('twitter:card', 'summary');
62                 // $out->addMeta('twitter:site', '@flickr'); // we don't have a twitter name
63                 $out->addMeta('twitter:title', $title->getBaseText());
64                 $out->addMeta('twitter:description', 'Beschreibungseite der Rodelbahn mit Schneelage-Bewertung.');
65                 if (!is_null($file_image)) {
66                         $thumb_image = $file_image->transform(array('width' => 800));
67                         $url = wfExpandUrl($thumb_image->getUrl(), PROTO_CANONICAL);
68                         $out->addMeta('twitter:image', $url);
69                 }
70         }
71
72
73         /// Adds Facebook Open Graph META Tags
74         static function facebook_open_graph_sledrun_add(OutputPage &$out, ParserOutput $parseroutput, $title, $row, $file_image) {
75                 $out->addHeadItem('wrsemantic-fb-site', '<meta property="og:site_name" content="Winterrodeln" />');
76                 $out->addHeadItem('wrsemantic-fb-type', '<meta property="og:type" content="place" />');
77                 $title_base_text = $title->getBaseText();
78                 $out->addHeadItem('wrsemantic-fb-title', '<meta property="og:title" content="' . htmlspecialchars($title_base_text) . '" />');
79                 if (!is_null($file_image)) {
80                         $thumb_image = $file_image->transform(array('width' => 800));
81                         $thumb_image_url = wfExpandUrl($thumb_image->getUrl(), PROTO_CANONICAL);
82                         $out->addHeadItem('wrsemantic-fb-image', '<meta property="og:image" content="' . htmlspecialchars($thumb_image_url) . '" />');
83                 }
84                 $lat = $row->position_latitude;
85                 if (!is_null($lat)) $out->addHeadItem('wrsemantic-fb-lat', "<meta property=\"place:location:latitude\"  content=\"$lat\" />");
86                 $lon = $row->position_longitude;
87                 if (!is_null($lon)) $out->addHeadItem('wrsemantic-fb-lon', "<meta property=\"place:location:longitude\" content=\"$lon\" />");
88         }
89
90
91         public static function onOutputPageParserOutput(OutputPage &$out, ParserOutput $parseroutput) {
92                 if (!$out->isArticle()) return true;
93                 $categories = $out->getCategories();
94                 $title = $out->getTitle();
95
96                 if (!in_array('Rodelbahn', $categories)) return true;
97                 // select sledrun cache for page
98                 $dbr = wfGetDB(DB_REPLICA);
99                 // SELECT position_longitude, position_latitude, operator, information_phone, image, under_construction FROM wrsledruncache WHERE page_id=226;
100                 $res = $dbr->select('wrsledruncache', array('position_longitude', 'position_latitude', 'operator', 'information_phone', 'image', 'under_construction'), array('page_id' => $title->getArticleID()), __METHOD__);
101                 $row = $dbr->fetchObject($res);
102                 if ($row === false) return true;
103                 if (!$row->under_construction) {
104                         // get image information if an image is available
105                         $file_image = NULL;
106                         $title_image = NULL;
107                         if (!is_null($row->image)) {
108                                 $title_image = $title->newFromText($row->image, $defaultNamespace=NS_FILE);
109                                 if (!is_null($title_image)) $file_image = wfFindFile($title_image);
110                         }
111
112                         // JSON-LD + schema.org (https://developers.google.com/search/docs/guides/intro-structured-data)
113                         $json = self::json_ld_sledrun_compose($title, $row, $file_image);
114                         self::json_ld_add($out, $parseroutput, $json);
115
116                         // Twitter Summary Card (https://dev.twitter.com/cards/types/summary)
117                         self::twitter_summary_card_sledrun_add($out, $parseroutput, $title, $row, $file_image);
118
119                         // Facebook Open Graph META Tags
120                         self::facebook_open_graph_sledrun_add($out, $parseroutput, $title, $row, $file_image);
121
122                 }
123                 return true;
124         }
125 }
126
127 ?>