1<?php 2/** 3 * Include Plugin: Display a wiki page within another wiki page 4 * 5 * Action plugin component, for cache validity determination 6 * 7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 8 * @author Christopher Smith <chris@jalakai.co.uk> 9 * @author Michael Klier <chi@chimeric.de> 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_include extends DokuWiki_Action_Plugin { 21 22 var $supportedModes = array('xhtml', 'metadata'); 23 /* @var helper_plugin_include $helper */ 24 var $helper = null; 25 26 function action_plugin_include() { 27 $this->helper = plugin_load('helper', 'include'); 28 } 29 30 /** 31 * plugin should use this method to register its handlers with the dokuwiki's event controller 32 */ 33 function register(&$controller) { 34 /* @var Doku_event_handler $controller */ 35 $controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare'); 36 $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_form'); 37 $controller->register_hook('HTML_CONFLICTFORM_OUTPUT', 'BEFORE', $this, 'handle_form'); 38 $controller->register_hook('HTML_DRAFTFORM_OUTPUT', 'BEFORE', $this, 'handle_form'); 39 $controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'handle_redirect'); 40 $controller->register_hook('PARSER_HANDLER_DONE', 'BEFORE', $this, 'handle_parser'); 41 $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handle_metadata'); 42 $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'handle_secedit_button'); 43 } 44 45 /** 46 * Used for debugging purposes only 47 */ 48 function handle_metadata(&$event, $param) { 49 global $conf; 50 if($conf['allowdebug']) { 51 dbglog('---- PLUGIN INCLUDE META DATA START ----'); 52 dbglog($event->data); 53 dbglog('---- PLUGIN INCLUDE META DATA END ----'); 54 } 55 } 56 57 /** 58 * Supplies the current section level to the include syntax plugin 59 * 60 * @author Michael Klier <chi@chimeric.de> 61 * @author Michael Hamann <michael@content-space.de> 62 */ 63 function handle_parser(Doku_Event &$event, $param) { 64 global $ID; 65 66 $level = 0; 67 $ins =& $event->data->calls; 68 $num = count($ins); 69 for($i=0; $i<$num; $i++) { 70 switch($ins[$i][0]) { 71 case 'plugin': 72 switch($ins[$i][1][0]) { 73 case 'include_include': 74 $ins[$i][1][1][] = $level; 75 break; 76 /* FIXME: this doesn't work anymore that way with the new structure 77 // some plugins already close open sections 78 // so we need to make sure we don't close them twice 79 case 'box': 80 $this->helper->sec_close = false; 81 break; 82 */ 83 } 84 break; 85 case 'section_open': 86 $level = $ins[$i][1][0]; 87 break; 88 } 89 } 90 } 91 92 /** 93 * Add a hidden input to the form to preserve the redirect_id 94 */ 95 function handle_form(Doku_Event &$event, $param) { 96 if (array_key_exists('redirect_id', $_REQUEST)) { 97 $event->data->addHidden('redirect_id', cleanID($_REQUEST['redirect_id'])); 98 } 99 } 100 101 /** 102 * Modify the data for the redirect when there is a redirect_id set 103 */ 104 function handle_redirect(Doku_Event &$event, $param) { 105 if (array_key_exists('redirect_id', $_REQUEST)) { 106 // Render metadata when this is an older DokuWiki version where 107 // metadata is not automatically re-rendered as the page has probably 108 // been changed but is not directly displayed 109 $versionData = getVersionData(); 110 if ($versionData['date'] < '2010-11-23') { 111 p_set_metadata($event->data['id'], array(), true); 112 } 113 $event->data['id'] = cleanID($_REQUEST['redirect_id']); 114 $event->data['title'] = ''; 115 } 116 } 117 118 /** 119 * prepare the cache object for default _useCache action 120 */ 121 function _cache_prepare(Doku_Event &$event, $param) { 122 global $conf; 123 124 /* @var cache_renderer $cache */ 125 $cache =& $event->data; 126 127 if(!isset($cache->page)) return; 128 if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return; 129 130 $depends = p_get_metadata($cache->page, 'plugin_include'); 131 132 if($conf['allowdebug']) { 133 dbglog('---- PLUGIN INCLUDE CACHE DEPENDS START ----'); 134 dbglog($depends); 135 dbglog('---- PLUGIN INCLUDE CACHE DEPENDS END ----'); 136 } 137 138 if (!is_array($depends)) return; // nothing to do for us 139 140 if (!is_array($depends['pages']) || 141 !is_array($depends['instructions']) || 142 $depends['pages'] != $this->helper->_get_included_pages_from_meta_instructions($depends['instructions']) || 143 // the include_content url parameter may change the behavior for included pages 144 $depends['include_content'] != isset($_REQUEST['include_content'])) { 145 146 $cache->depends['purge'] = true; // included pages changed or old metadata - request purge. 147 if($conf['allowdebug']) { 148 dbglog('---- PLUGIN INCLUDE: REQUESTING CACHE PURGE ----'); 149 dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META START ----'); 150 dbglog($depends['pages']); 151 dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META END ----'); 152 dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS START ----'); 153 dbglog($this->helper->_get_included_pages_from_meta_instructions($depends['instructions'])); 154 dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS END ----'); 155 156 } 157 } else { 158 // add plugin.info.txt to depends for nicer upgrades 159 $cache->depends['files'][] = dirname(__FILE__) . '/plugin.info.txt'; 160 foreach ($depends['pages'] as $page) { 161 if (!$page['exists']) continue; 162 $file = wikiFN($page['id']); 163 if (!in_array($file, $cache->depends['files'])) { 164 $cache->depends['files'][] = $file; 165 } 166 } 167 } 168 } 169 170 /** 171 * Handle special section edit buttons for the include plugin to get the current page 172 * and replace normal section edit buttons when the current page is different from the 173 * global $ID. 174 */ 175 function handle_secedit_button(Doku_Event &$event, $params) { 176 // stack of included pages in the form ('id' => page, 'rev' => modification time, 'writable' => bool) 177 static $page_stack = array(); 178 179 global $ID, $lang; 180 181 $data = $event->data; 182 183 if ($data['target'] == 'plugin_include_start' || $data['target'] == 'plugin_include_start_noredirect') { 184 // handle the "section edits" added by the include plugin 185 $fn = wikiFN($data['name']); 186 $perm = auth_quickaclcheck($data['name']); 187 array_unshift($page_stack, array( 188 'id' => $data['name'], 189 'rev' => @filemtime($fn), 190 'writable' => (page_exists($data['name']) ? (is_writable($fn) && $perm >= AUTH_EDIT) : $perm >= AUTH_CREATE), 191 'redirect' => ($data['target'] == 'plugin_include_start'), 192 )); 193 } elseif ($data['target'] == 'plugin_include_end') { 194 array_shift($page_stack); 195 } elseif ($data['target'] == 'plugin_include_editbtn') { 196 if ($page_stack[0]['writable']) { 197 $params = array('do' => 'edit', 198 'id' => $page_stack[0]['id']); 199 if ($page_stack[0]['redirect']) 200 $params['redirect_id'] = $ID; 201 $event->result = '<div class="secedit">' . DOKU_LF . 202 html_btn('incledit', $page_stack[0]['id'], '', 203 $params, 'post', 204 $data['name'], 205 $lang['btn_secedit'].' ('.$page_stack[0]['id'].')') . 206 '</div>' . DOKU_LF; 207 } 208 } elseif (!empty($page_stack)) { 209 210 // Special handling for the edittable plugin 211 if ($data['target'] == 'table' && !plugin_isdisabled('edittable')) { 212 /* @var action_plugin_edittable $edittable */ 213 $edittable =& plugin_load('action', 'edittable'); 214 $data['name'] = $edittable->getLang('secedit_name'); 215 } 216 217 if ($page_stack[0]['writable'] && isset($data['name']) && $data['name'] !== '') { 218 $name = $data['name']; 219 unset($data['name']); 220 221 $secid = $data['secid']; 222 unset($data['secid']); 223 224 if ($page_stack[0]['redirect']) 225 $data['redirect_id'] = $ID; 226 227 $event->result = "<div class='secedit editbutton_" . $data['target'] . 228 " editbutton_" . $secid . "'>" . 229 html_btn('secedit', $page_stack[0]['id'], '', 230 array_merge(array('do' => 'edit', 231 'rev' => $page_stack[0]['rev'], 232 'summary' => '['.$name.'] '), $data), 233 'post', $name) . '</div>'; 234 } else { 235 $event->result = ''; 236 } 237 } else { 238 return; // return so the event won't be stopped 239 } 240 241 $event->preventDefault(); 242 $event->stopPropagation(); 243 } 244} 245// vim:ts=4:sw=4:et: 246