1<?php
2/**
3 * Random Include Plugin:
4 * Randomly select a page from a namespace and include it the current page.
5 *
6 * Action plugin component, for cache validity determination
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     Christopher Smith <chris@jalakai.co.uk>
10 */
11if(!defined('DOKU_INC')) die();  // no Dokuwiki, no go
12
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
14require_once(DOKU_PLUGIN.'action.php');
15
16/**
17 * All DokuWiki plugins to extend the parser/rendering mechanism
18 * need to inherit from this class
19 */
20class action_plugin_randominc extends DokuWiki_Action_Plugin {
21
22    protected $supportedModes = array('xhtml');
23
24    /**
25     * plugin should use this method to register its handlers with the dokuwiki's event controller
26     */
27    function register(Doku_Event_Handler $controller) {
28        $controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare');
29        // debugging only
30        //$controller->register_hook('PARSER_CACHE_USE','AFTER', $this, '_cache_result');
31    }
32
33    /**
34     * prepare the cache object for default _useCache action
35     */
36    function _cache_prepare(&$event, $param) {
37        $cache =& $event->data;
38
39        // we're only interested in wiki pages and supported render modes
40        if (!isset($cache->page)) return;
41        if (!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
42
43        $key = '';
44        $depends = array();
45        $expire = $this->_inclusion_check($cache->page, $key, $depends);
46
47        //global $debug;
48        //$debug[] = compact('key','expire','depends','cache');
49
50        // empty $key implies no includes, so nothing to do
51        if (empty($key)) return;
52
53        // mark the cache as being modified by the include plugin
54        $cache->include = true;
55
56        // set new cache key & cache name - now also dependent on included page ids and their ACL_READ status
57        $cache->key .= $key;
58        $cache->cache = getCacheName($cache->key, $cache->ext);
59
60        // inclusion check was able to determine the cache must be invalid
61        if ($expire) {
62            $event->preventDefault();
63            $event->stopPropagation();
64            $event->result = false;
65            return;
66        }
67
68        // update depends['files'] array to include all included files
69        $cache->depends['files'] = !empty($cache->depends['files']) ? array_merge($cache->depends['files'], $depends) : $depends;
70    }
71
72    /**
73     * carry out included page checks:
74     * - to establish proper cache name, its dependent on the read status of included pages
75     * - to establish file dependencies, the included raw wiki pages
76     *
77     * @param   string    $id         wiki page name
78     * @param   string    $key        (reference) cache key
79     * @param   array     $depends    array of include file dependencies
80     *
81     * @return  bool                  expire the cache
82     */
83    function _inclusion_check($id, &$key, &$depends) {
84        $hasPart = p_get_metadata($id, 'relation haspart');
85        if (empty($hasPart)) return false;
86
87        $expire = false;
88        foreach ($hasPart as $page => $exists) {
89            // ensure its a wiki page
90            if (strpos($page,'/') ||  cleanID($page) != $page) continue;
91
92            $file = wikiFN($page);
93
94            // file existence state is different from state recorded in metadata
95            if (@file_exists($file) != $exists) {
96                if (($acl = $this->_acl_read_check($page)) != 'NONE') { $expire = true;  }
97            } else if ($exists) {
98                // carry out an inclusion check on the included page, that will update $key & $depends
99                if ($this->_inclusion_check($page, $key, $depends)) { $expire = true; }
100                if (($acl = $this->_acl_read_check($page)) != 'NONE') { $depends[] = $file;  }
101            } else {
102                $acl = 'NONE';
103            }
104
105            // add this page and acl status to the key
106            $key .= '#'.$page.'|'.$acl;
107        }
108
109        return $expire;
110    }
111
112    function _acl_read_check($id) {
113        return (AUTH_READ <= auth_quickaclcheck($id)) ? 'READ' : 'NONE';
114    }
115
116    function _cache_result(&$event, $param) {
117        $cache =& $event->data;
118        if (empty($cache->include)) return;
119
120        //global $debug;
121        //$debug['cache_result'][] = $event->result ? 'true' : 'false';
122    }
123}
124
125//Setup VIM: ex: et ts=4 enc=utf-8 :
126