dokuwiki-source-plugin – Diff between revs 2 and 3

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