xref: /plugin/geotag/syntax/geotag.php (revision a42972ef71a14df1dc4a19857b1709c48e59f1f5)
1<?php
2/*
3 * Copyright (c) 2011-2013 Mark C. Prins <mprins@users.sf.net>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/**
19 * DokuWiki Plugin geotag (Syntax Component)
20 *
21 * @license BSD license
22 * @author  Mark C. Prins <mprins@users.sf.net>
23 */
24if (!defined('DOKU_INC')) die();
25if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
26if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
27
28require_once(DOKU_PLUGIN.'syntax.php');
29/**
30 * Handles the rendering part of the geotag plugin.
31 *
32 * @author Mark
33 *
34 */
35class syntax_plugin_geotag_geotag extends DokuWiki_Syntax_Plugin {
36	public function getType() { return 'substition'; }
37
38	public function getPType() { return 'block'; }
39
40	public function getSort() { return 305; }
41
42	public function connectTo($mode) {
43		$this->Lexer->addSpecialPattern('\{\{geotag>.*?\}\}',$mode,'plugin_geotag_geotag');
44	}
45
46	public function handle($match, $state, $pos, &$handler){
47		$tags = trim(substr($match, 9, -2));
48		// parse geotag content
49		preg_match("(lat[:|=]\d*\.\d*)",$tags,$lat);
50		preg_match("(lon[:|=]\d*\.\d*)",$tags,$lon);
51		preg_match("(region[:|=][a-zA-Z\s\w'-]*)",$tags,$region);
52		preg_match("(placename[:|=][a-zA-Z\s\w'-]*)",$tags,$placename);
53		preg_match("(country[:|=][a-zA-Z\s\w'-]*)",$tags,$country);
54		preg_match("(hide|unhide)",$tags,$hide);
55
56		$showlocation=$this->getConf('geotag_location_prefix');
57		if ($this->getConf('geotag_showlocation')) {
58			$showlocation=trim(substr($placename[0],10));
59			if (strlen($showlocation)>0) {
60				$showlocation .=': ';
61			}
62		}
63		// read config for system setting
64		$style='';
65		if ($this->getConf('geotag_hide')) {
66			$style=' style="display: none;"';
67		}
68		// override config for the current tag
69		if (trim($hide[0])=='hide'){
70			$style=' style="display: none;"';
71		} elseif(trim($hide[0])=='unhide'){
72			$style='';
73		}
74
75		$data = array(
76			trim(substr($lat[0],4)),
77			trim(substr($lon[0],4)),
78			$this->_geohash(substr($lat[0],4), substr($lon[0],4)),
79			trim(substr($region[0],7)),
80			trim(substr($placename[0],10)),
81			trim(substr($country[0],8)),
82			$showlocation,
83			$style,
84		);
85		return $data;
86	}
87
88	public function render($mode, &$renderer, $data) {
89		if ($data === false) return false;
90		list ($lat, $lon, $geohash, $region, $placename, $country, $showlocation, $style) = $data;
91		if ($mode == 'xhtml') {
92			if ($this->getConf('geotag_prevent_microformat_render')) {
93				// config says no microformat rendering
94				return true;
95			}
96			// render geotag microformat
97			$renderer->doc .= '<span class="geotagPrint">'.$this->getLang('geotag_desc').'</span><div class="geo"'.$style.
98					' title="'.$this->getLang('geotag_desc').$placename.'">'.
99					$showlocation.'<span class="latitude">'.
100					$lat.'</span>;<span class="longitude">'.
101					$lon.'</span></div>'.DOKU_LF;
102			return true;
103		} elseif ($mode == 'metadata') {
104			// render metadata (action plugin will put it in the page head)
105			$renderer->meta['geo']['lat'] = $lat;
106			$renderer->meta['geo']['lon'] = $lon;
107			$renderer->meta['geo']['placename'] = $placename;
108			$renderer->meta['geo']['region'] = $region;
109			$renderer->meta['geo']['country'] = $country;
110			$renderer->meta['geo']['geohash'] = $geohash;
111			return true;
112		} elseif ($mode=='odt'){
113			// TODO
114			return false;
115		}
116		return false;
117	}
118
119	/**
120	 * Calculate the geohash for this lat/lon pair.
121	 *
122	 * @param float $lat
123	 * @param float $lon
124	 */
125	private function _geohash($lat, $lon){
126		if (!$geophp = &plugin_load('helper', 'geophp')){
127			dbglog($geophp,'syntax_plugin_geotag_geotag::_geohash: geophp plugin is not available.');
128			return "";
129		}
130		$_lat = floatval($lat);
131		$_lon = floatval($lon);
132		$geometry = new Point($_lon,$_lat);
133		dbglog($geometry, 'geometry to calculate geohash from..');
134		return $geometry->out('geohash');
135	}
136}
137