1<?php
2/**
3 * $Id: action.php 18 2017-06-19 15:22:35Z denis $
4 *
5 * Source Plugin: includes a source file using the geshi highlighter
6 * @author    DenisVS <deniswebcomm@gmail.com>
7 * Action plugin component, for cache validity determination
8 * @link    https://www.dokuwiki.org/plugin:src
9 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 * @based on Source Plugin by  Chris Smith <chris@jalakai.co.uk>
11 */
12if(!defined('DOKU_INC')) die();  // no Dokuwiki, no go
13
14if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15require_once(DOKU_PLUGIN.'action.php');
16
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21class action_plugin_source extends DokuWiki_Action_Plugin {
22
23    /**
24     * return some info
25     */
26    function getInfo(){
27      return array(
28        'author' => 'Christopher Smith',
29        'email'  => 'chris@jalakai.co.uk',
30        'date'   => '2008-08-13',
31        'name'   => 'Source Plugin',
32        'desc'   => 'Include a remote source file',
33        'url'    => 'http://www.dokuwiki.org/plugin:source',
34      );
35    }
36
37    /**
38     * plugin should use this method to register its handlers with the dokuwiki's event controller
39     */
40    function register(&$controller) {
41      $controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare');
42    }
43
44    /**
45     * prepare the cache object for default _useCache action
46     */
47    function _cache_prepare(&$event, $param) {
48      $cache =& $event->data;
49
50      // we're only interested in wiki pages and supported render modes
51      if (!isset($cache->page)) return;
52      if (!isset($cache->mode) || in_array($cache->mode,array('i','metadata'))) return;
53
54      $max_age = $this->_cache_maxage($cache->page);
55      if (is_null($max_age)) return;
56
57      if ($max_age <= 0) {
58        // expire the cache
59        $event->preventDefault();
60        $event->stopPropagation();
61        $event->result = false;
62        return;
63      }
64
65      $cache->depends['age'] = !empty($cache->depends['age']) ?  min($cache->depends['age'],$max_age): $max_age;
66    }
67
68    /**
69     * determine the max allowable age of the cache
70     *
71     * @param   string    $id         wiki page name
72     *
73     * @return  int                   max allowable age of the cache
74     *                                null means not applicable
75     */
76    function _cache_maxage($id) {
77      $hasPart = p_get_metadata($id, 'relation haspart');
78      if (empty($hasPart) || !is_array($hasPart)) return null;
79
80      $location = $this->getConf('location');
81
82      $age = 0;
83      foreach ($hasPart as $file => $data) {
84        // ensure the metadata entry was created by or for this plugin
85        if (empty($data['owner']) || $data['owner'] != $this->getPluginName()) continue;
86
87        $file = $this->getConf['location'].$file;
88
89        // determine max allowable age for the cache
90        // if filemtime can't be determined, either for a non-existent $file or for a $file using
91        // an unsupported scheme, expire the cache immediately/always
92        $mtime = @filemtime($file);
93        if (!$mtime) { return 0; }
94
95        $age = max($age,$mtime);
96      }
97
98      return $age ? time()-$age : null;
99    }
100
101}
102
103//Setup VIM: ex: et ts=4 enc=utf-8 :
104