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