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