xref: /plugin/geotag/action.php (revision 04c57205e840343fb53b51f62babaca1af8cc187) !
1<?php
2/*
3 * Copyright (c) 2011 Mark C. Prins <mc.prins@gmail.com>
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 */
17if (!defined('DOKU_INC')) die();
18
19if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
20require_once(DOKU_PLUGIN.'action.php');
21
22/**
23 * DokuWiki Plugin geotag (Action Component)
24 *
25 * @license BSD license
26 * @author  Mark C. Prins <mc.prins@gmail.com>
27 */
28class action_plugin_geotag extends DokuWiki_Action_Plugin {
29
30	/**
31	 * Register for events.
32	 *
33	 * @param $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER
34	 */
35	public function register(Doku_Event_Handler &$controller) {
36		$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_metaheader_output');
37		$controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'ping_geourl', array());
38		if($this->getConf('toolbar_icon')) {
39			$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());
40		}
41	}
42
43	/**
44	 * retrieve metadata and add to the head of the page using appropriate meta tags.
45	 * @param Doku_Event $event the DokuWiki event. $event->data is a two-dimensional
46	 * 	array of all meta headers. The keys are meta, link and script.
47	 * @param unknown_type $param
48	 */
49	public function handle_metaheader_output(Doku_Event &$event, $param) {
50		/*
51		 * see: http://www.dokuwiki.org/devel:event:tpl_metaheader_output
52		 * $data is a two-dimensional array of all meta headers. The keys are meta, link and script.
53		 */
54		global $ID;
55		$title = p_get_metadata($ID,'title',true);
56		$geotags = p_get_metadata($ID,'geo',true);
57		$region=$geotags['region'];
58		$lat=$geotags['lat'];
59		$lon=$geotags['lon'];
60		$country=$geotags['country'];
61		$placename=$geotags['placename'];
62
63		if (!empty($region)) {$event->data['meta'][] = array('name' => 'geo.region','content' => $region,);}
64		if (!empty($placename)) {$event->data['meta'][] = array('name' => 'geo.placename','content' => $placename,);}
65		if (!(empty($lat)&&empty($lon))) {$event->data['meta'][] = array('name' => 'geo.position','content' => $lat.';'.$lon,);}
66		if (!empty($country)) {$event->data['meta'][] = array('name' => 'geo.country','content' => $country,);}
67		if (!(empty($lat)&&empty($lon))) {
68			$event->data['meta'][] = array('name' => "ICBM",'content' => $lat.', '.$lon,);
69			// icbm is generally useless without a dc.title, so we copy that from title unless empty
70			if (!(empty($title))) {$event->data['meta'][] = array('name' => "DC.title",'content' => $title);}
71		}
72	}
73
74	/**
75	 * Ping the geourl webservice with the url of the for indexing, only if the page is new.
76	 * @param Doku_Event $event the DokuWiki event
77	 * @param mixed $param not used
78	 */
79	function ping_geourl(Doku_Event &$event, $param) {
80		global $ID;
81		/*
82		 * see: http://www.dokuwiki.org/devel:event:io_wikipage_write
83		 * event data:
84		 * $data[0] – The raw arguments for io_saveFile as an array. Do not change file path.
85		 * $data[0][0] – the file path.
86		 * $data[0][1] – the content to be saved, and may be modified.
87		 * $data[1] – ns: The colon separated namespace path minus the trailing page name. (false if root ns)
88		 * $data[2] – page_name: The wiki page name.
89		 * $data[3] – rev: The page revision, false for current wiki pages.
90		 */
91		if (!$this->getConf('geotag_pinggeourl')) return false; // config says don't ping
92		if ($event->data[3]) return false;                   // old revision saved
93		if (@file_exists($event->data[0][0])) return false;  // file not new
94		if (!$event->data[0][1]) return false;               // file is empty
95		if (p_get_metadata($ID,'geo',true)) return false; // no geo metadata available, ping is useless
96
97		$url = 'http://geourl.org/ping/?p='.wl($ID,'',true);
98		$http = new DokuHTTPClient();
99		$result = $http->get($url);
100		dbglog($result,"GeoURL Ping response for $url");
101		return $result;
102	}
103
104	/**
105	 * Inserts the toolbar button.
106	 * @param Doku_Event $event the DokuWiki event
107	 */
108	function insert_button(Doku_Event &$event, $param) {
109		$event->data[] = array (
110	        'type' => 'format',
111	        'title' => $this->getLang('toolbar_desc'),
112	        'icon' => '../../plugins/geotag/images/geotag.png',
113	        'open' => '{{geotag>lat:',
114			'sample' => '52.2345',
115	    	'close' => ', lon: , placename: , country: , region: }}'
116	    	);
117	}
118}
119