1<?php
2/**
3 * Datatemplate plugin.
4 *
5 * Action plugin component, for cache validity determination
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     Christoph Clausen <christoph.clausen@gmail.com>
9 */
10if(!defined('DOKU_INC')) die();  // no Dokuwiki, no go
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'action.php');
14
15/**
16 * All DokuWiki plugins to extend the parser/rendering mechanism
17 * need to inherit from this class
18 */
19class action_plugin_datatemplate extends DokuWiki_Action_Plugin {
20
21    /**
22     * return some info
23     */
24    function getInfo(){
25        return array(
26            'author' => 'Cyrille Giquello, Christoph Clausen',
27            'email'  => '',
28            'date'   => '2016-08-02',
29            'name'   => 'Datatemplate Plugin',
30            'desc'   => 'A template extension for the data plugin',
31            'url'    => 'https://github.com/Cyrille37/dokuwiki-plugin-datatemplate',
32            );
33    }
34
35    /**
36     * plugin should use this method to register its handlers with the dokuwiki's event controller
37     */
38    function register(Doku_Event_Handler $controller) {
39        $controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare');
40    }
41
42    /**
43     * prepare the cache object for default _useCache action
44     */
45    function _cache_prepare(&$event, $param) {
46        $cache =& $event->data;
47
48        // we're only interested in wiki pages and supported render modes
49        if (!isset($cache->page)) return;
50        if (!isset($cache->mode) || $cache->mode != 'metadata') return;
51
52        $files = $this->_get_dependencies($cache->page);
53        $cache->depends['files'] = array_merge($cache->depends['files'], $files);
54    }
55
56    /**
57     * Get list of files that this page depends on. Usually, this would just be
58     * a single template file.
59     */
60    function _get_dependencies($id) {
61        $hasPart = p_get_metadata($id, 'relation haspart');
62        if(empty($hasPart) || !is_array($hasPart)) return array();
63
64        $files = array();
65        foreach($hasPart as $file => $data) {
66            if(empty($data['owner']) || $data['owner'] != $this->getPluginName()) continue;
67            $files[] = $file;
68        }
69        return $files;
70    }
71}
72