1<?php
2/*
3 * Twitter plugin action component.
4 *
5 * @license GPL 2 (http://opensource.org/licenses/gpl-2.0.php)
6 * @author Christopher Smith <chris@jalakai.co.uk>
7 * @author Björn Kalkbrenner <terminar@cyberphoria.org> (rewritten for twitter plugin)
8 * @author Mark C. Prins <mprins@users.sf.net>
9 */
10
11/**
12 * Twitter Plugin Action plugin component, for cache validity determination; based on the Source Plugin/Action scripts.
13 */
14class action_plugin_twitter extends DokuWiki_Action_Plugin {
15
16	/**
17	 * plugin should use this method to register its handlers with the dokuwiki's event controller.
18	 * @see DokuWiki_Action_Plugin::register()
19	 */
20	function register(Doku_Event_Handler $controller) {
21		$controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, '_cache_prepare');
22	}
23
24	/**
25	 * prepare the cache object for default _useCache action
26	 */
27	function _cache_prepare($event, $param) {
28		$cache =& $event->data;
29
30		// we're only interested in wiki pages and supported render modes
31		if (!isset($cache->page)) {
32			return;
33		}
34		if (!isset($cache->mode) || !in_array($cache->mode, array('i', 'metadata'))) {
35			return;
36		}
37
38		$max_age = $this->_cache_maxage($cache->page);
39
40		if (is_null($max_age)) {
41			return;
42		}
43
44		if ($max_age <= 0) {
45			// expire the cache
46			//no cache for twitter!
47			$event->preventDefault();
48			$event->stopPropagation();
49			$event->result = false;
50			return;
51		}
52		$cache->depends['age'] = !empty($cache->depends['age']) ? min($cache->depends['age'], $max_age) : $max_age;
53	}
54
55	/**
56	 * determine the max allowable age of the cache
57	 *
58	 * @param   string    $id wiki page name
59	 *
60	 * @return  int max allowable age of the cache null means not applicable
61	 */
62	function _cache_maxage($id) {
63		$hasPart = p_get_metadata($id, 'relation haspart');
64		if (empty($hasPart) || !is_array($hasPart)) {
65			return null;
66		}
67		$age = 0;
68		foreach ($hasPart as $file => $data) {
69			if ($file == "_plugin_twitter") {
70				//this is us, outdate the cache if older than the configured seconds
71				return $this->getConf('timeout');
72			}
73		}
74		return $age ? time() - $age : null;
75	}
76
77}
78