1<?php 2/** 3 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 4 * @author Esther Brunner <wikidesign@gmail.com> 5 * @author Christopher Smith <chris@jalakai.co.uk> 6 * @author Gina Häußge, Michael Klier <dokuwiki@chimeric.de> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) die(); 11 12if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/'); 15 16class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin 17 18 var $includes = array(); 19 var $toplevel_id = NULL; 20 var $toplevel = 0; 21 var $defaults = array(); 22 23 /** 24 * Constructor loads default config settings once 25 */ 26 function helper_plugin_include() { 27 $this->defaults['firstsec'] = $this->getConf('firstseconly'); 28 $this->defaults['editbtn'] = $this->getConf('showeditbtn'); 29 $this->defaults['taglogos'] = $this->getConf('showtaglogos'); 30 $this->defaults['footer'] = $this->getConf('showfooter'); 31 $this->defaults['redirect'] = $this->getConf('doredirect'); 32 $this->defaults['date'] = $this->getConf('showdate'); 33 $this->defaults['user'] = $this->getConf('showuser'); 34 $this->defaults['comments'] = $this->getConf('showcomments'); 35 $this->defaults['linkbacks'] = $this->getConf('showlinkbacks'); 36 $this->defaults['tags'] = $this->getConf('showtags'); 37 $this->defaults['link'] = $this->getConf('showlink'); 38 $this->defaults['permalink'] = $this->getConf('showpermalink'); 39 } 40 41 function getInfo() { 42 return array( 43 'author' => 'Gina Häußge, Michael Klier, Esther Brunner', 44 'email' => 'dokuwiki@chimeric.de', 45 'date' => @file_get_contents(DOKU_PLUGIN . 'blog/VERSION'), 46 'name' => 'Include Plugin (helper class)', 47 'desc' => 'Functions to include another page in a wiki page', 48 'url' => 'http://dokuwiki.org/plugin:include', 49 ); 50 } 51 52 /** 53 * Available methods for other plugins 54 */ 55 function getMethods() { 56 $result = array(); 57 $result[] = array( 58 'name' => 'get_flags', 59 'desc' => 'overrides standard values for showfooter and firstseconly settings', 60 'params' => array('flags' => 'array'), 61 ); 62 return $result; 63 } 64 65 /** 66 * Overrides standard values for showfooter and firstseconly settings 67 */ 68 function get_flags($setflags) { 69 // load defaults 70 $flags = array(); 71 $flags = $this->defaults; 72 foreach ($setflags as $flag) { 73 switch ($flag) { 74 case 'footer': 75 $flags['footer'] = 1; 76 break; 77 case 'nofooter': 78 $flags['footer'] = 0; 79 break; 80 case 'firstseconly': 81 case 'firstsectiononly': 82 $flags['firstsec'] = 1; 83 break; 84 case 'fullpage': 85 $flags['firstsec'] = 0; 86 break; 87 case 'noheader': 88 $flags['noheader'] = 1; 89 break; 90 case 'editbtn': 91 case 'editbutton': 92 $flags['editbtn'] = 1; 93 break; 94 case 'noeditbtn': 95 case 'noeditbutton': 96 $flags['editbtn'] = 0; 97 break; 98 case 'permalink': 99 $flags['permalink'] = 1; 100 break; 101 case 'nopermalink': 102 $flags['permalink'] = 0; 103 break; 104 case 'redirect': 105 $flags['redirect'] = 1; 106 break; 107 case 'noredirect': 108 $flags['redirect'] = 0; 109 break; 110 case 'link': 111 $flags['link'] = 1; 112 break; 113 } 114 } 115 return $flags; 116 } 117 118 /** 119 * Parses the instructions list of the page which contains the includes 120 * 121 * @author Michael Klier <chi@chimeric.de> 122 */ 123 function parse_instructions($id, &$ins) { 124 $num = count($ins); 125 126 $lvl = false; 127 $prev_lvl = false; 128 $mode = ''; 129 $page = ''; 130 $flags = array(); 131 $range = false; 132 133 for($i=0; $i<$num; $i++) { 134 // set current level 135 if($ins[$i][0] == 'section_open') { 136 $lvl = $ins[$i][1][0]; 137 if($i > $range) $prev_lvl = $lvl; 138 } 139 if($ins[$i][0] == 'plugin' && $ins[$i][1][0] == 'include_include' ) { 140 // found no previous section set lvl to 0 141 if(!$lvl) $lvl = 0; 142 143 $mode = $ins[$i][1][1][0]; 144 $page = $ins[$i][1][1][1]; 145 $sect = $ins[$i][1][1][2]; 146 $flags = $ins[$i][1][1][3]; 147 148 // check if we left the range of possible sub includes and reset lvl to toplevel 149 if($range && ($i > $range)) { 150 if(isset($prev_lvl)) { 151 $lvl = ($prev_lvl == 0) ? 1 : $prev_lvl; 152 $prev_lvl = false; 153 } else { 154 $lvl = $this->toplevel; 155 } 156 } 157 158 $page = $this->_apply_macro($page); 159 resolve_pageid(getNS($id), $page, $exists); // resolve shortcuts 160 $flags = $this->get_flags($flags); 161 162 $ins_inc = $this->_get_instructions($page, $sect, $mode, $lvl, $flags); 163 164 if(!empty($ins_inc)) { 165 // combine instructions and reset counter 166 $ins_start = array_slice($ins, 0, $i+1); 167 $ins_end = array_slice($ins, $i+1); 168 $range = $i + count($ins_inc); 169 $ins = array_merge($ins_start, $ins_inc, $ins_end); 170 $num = count($ins); 171 } 172 } 173 } 174 } 175 176 /** 177 * Returns the converted instructions of a give page/section 178 * 179 * @author Michael Klier <chi@chimeric.de> 180 */ 181 function _get_instructions($page, $sect, $mode, $lvl, $flags) { 182 global $ID; 183 184 if(($ID == $page) || (auth_quickaclcheck($page) < AUTH_READ) || (!page_exists($page)) && (auth_quickaclcheck($page) < AUTH_CREATE)) return array(); 185 $key = ($sect) ? $page . '#' . $sect : $page; 186 dbglog($this->includes); 187 188 // prevent recursion 189 if(!$this->includes[$key]) { 190 $ins = p_cached_instructions(wikiFN($page)); 191 $this->includes[$key] = true; 192 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags); 193 return $ins; 194 } 195 } 196 197 /** 198 * Converts instructions of the included page 199 * 200 * The funcion iterates over the given list of instructions and generates 201 * an index of header and section indicies. It also removes document 202 * start/end instructions, converts links, and removes unwanted 203 * instructions like tags, comments, linkbacks. 204 * 205 * Later all header/section levels are convertet to match the current 206 * inclusion level. 207 * 208 * @author Michael Klier <chi@chimeric.de> 209 */ 210 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags) { 211 212 // filter instructions if needed 213 if(!empty($sect)) { 214 $this->_get_section($ins, $sect); // section required 215 } 216 217 if($flags['firstsec']) { 218 $this->_get_firstsec($ins, $page); // only first section 219 } 220 221 $ns = getNS($page); 222 $num = count($ins); 223 224 $conv_idx = array(); // conversion index 225 $lvl_max = false; // max level 226 $first_header = -1; 227 $no_header = false; 228 $sect_title = false; 229 230 for($i=0; $i<$num; $i++) { 231 switch($ins[$i][0]) { 232 case 'document_start': 233 case 'document_end': 234 case 'section_edit': 235 unset($ins[$i]); 236 break; 237 case 'header': 238 // get section title of first section 239 if($sect && !$sect_title) { 240 $sect_title = $ins[$i][1][0]; 241 } 242 // check if we need to skip the first header 243 if((!$no_header) && $flags['noheader']) { 244 $no_header = true; 245 } 246 $conv_idx[] = $i; 247 // get index of first header 248 if($first_header == -1) $first_header = $i; 249 // get max level if this instructions set 250 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 251 $lvl_max = $ins[$i][1][1]; 252 } 253 break; 254 case 'section_open': 255 $conv_idx[] = $i; 256 break; 257 case 'internallink': 258 case 'internalmedia': 259 if($ins[$i][1][0]{0} == '.') { 260 if($ins[$i][1][0]{1} == '.') { 261 $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace 262 } else { 263 $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace 264 } 265 } elseif (strpos($ins[$i][1][0], ':') === false) { 266 $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links 267 } 268 break; 269 case 'plugin': 270 // FIXME skip other plugins? 271 switch($ins[$i][1][0]) { 272 case 'tag_tag': // skip tags 273 case 'discussion_comments': // skip comments 274 case 'linkback': // skip linkbacks 275 case 'data_entry': // skip data plugin 276 case 'meta': // skip meta plugin 277 unset($ins[$i]); 278 break; 279 } 280 break; 281 default: 282 break; 283 } 284 } 285 286 // calculate difference between header/section level and include level 287 $diff = 0; 288 if (!$lvl_max) $lvl_max = 0; // if no level found in target, set to 0 289 $diff = $lvl - $lvl_max + 1; 290 if ($no_header) $diff -= 1; // push up one level if "noheader" 291 292 // convert headers and set footer/permalink 293 $hdr_deleted = false; 294 $has_permalink = false; 295 $footer_lvl = false; 296 foreach($conv_idx as $idx) { 297 if($ins[$idx][0] == 'header') { 298 if($no_header && !$hdr_deleted) { 299 unset ($ins[$idx]); 300 $hdr_deleted = true; 301 continue; 302 } 303 $new_lvl = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 304 $ins[$idx][1][1] = $new_lvl; 305 306 // set permalink 307 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 308 $this->_permalink($ins[$idx], $page, $sect, $flags); 309 $has_permalink = true; 310 } 311 } else { 312 // it's a section 313 $new_lvl = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 314 $ins[$idx][1][0] = $new_lvl; 315 // check if noheader is used and set the footer level to the first section 316 if($no_header && !$footer_lvl) $footer_lvl = $new_lvl; 317 } 318 319 // set footer level 320 if(!$footer_lvl && ($idx == $first_header)) $footer_lvl = $new_lvl; 321 } 322 323 // add edit button 324 if($flags['editbtn'] && (auth_quickaclcheck($page) >= AUTH_EDIT)) $this->_editbtn($ins, $page, $sect, $sect_title); 325 326 // add footer 327 if($flags['footer']) $this->_footer($ins, $page, $sect, $sect_title, $flags, $footer_lvl); 328 329 // add instructions entry divs 330 array_unshift($ins, array('plugin', array('include_div', array('open', $page)))); 331 array_push($ins, array('plugin', array('include_div', array('close')))); 332 333 // close previous section if any and re-open after inclusion 334 if($lvl != 0) { 335 array_unshift($ins, array('section_close')); 336 $ins[] = array('section_open', array($lvl)); 337 } 338 } 339 340 /** 341 * Appends instruction item for the include plugin footer 342 * 343 * @author Michael Klier <chi@chimeric.de> 344 */ 345 function _footer(&$ins, $page, $sect, $sect_title, $flags, $footer_lvl) { 346 $footer = array(); 347 $footer[0] = 'plugin'; 348 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $this->toplevel_id, $footer_lvl)); 349 $ins[] = $footer; 350 } 351 352 /** 353 * Appends instruction item for an edit button 354 * 355 * @author Michael Klier <chi@chimeric.de> 356 */ 357 function _editbtn(&$ins, $page, $sect, $sect_title) { 358 $editbtn = array(); 359 $editbtn[0] = 'plugin'; 360 $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $this->toplevel_id)); 361 $ins[] = $editbtn; 362 } 363 364 /** 365 * Convert instruction item for a permalink header 366 * 367 * @author Michael Klier <chi@chimeric.de> 368 */ 369 function _permalink(&$ins, $page, $sect, $flags) { 370 $ins[0] = 'plugin'; 371 $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags)); 372 } 373 374 /** 375 * Get a section including its subsections 376 * 377 * @author Michael Klier <chi@chimeric.de> 378 */ 379 function _get_section(&$ins, $sect) { 380 $num = count($ins); 381 $offset = false; 382 $lvl = false; 383 $end = false; 384 385 for($i=0; $i<$num; $i++) { 386 if ($ins[$i][0] == 'header') { 387 388 // found the right header 389 if (cleanID($ins[$i][1][0]) == $sect) { 390 $offset = $i; 391 $lvl = $ins[$i][1][1]; 392 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 393 $end = $i - $offset; 394 break; 395 } 396 } 397 } 398 $offset = $offset ? $offset : 0; 399 $end = $end ? $end : ($num - 1); 400 $ins = array_slice($ins, $offset, $end); 401 } 402 403 /** 404 * Only display the first section of a page and a readmore link 405 * 406 * @author Michael Klier <chi@chimeric.de> 407 */ 408 function _get_firstsec(&$ins, $page) { 409 $num = count($ins); 410 $first_sect = false; 411 for($i=0; $i<$num; $i++) { 412 if($ins[$i][0] == 'section_close') { 413 $first_sect = $i; 414 } 415 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 416 $ins = array_slice($ins, 0, $first_sect); 417 $ins[] = array('p_open', array()); 418 $ins[] = array('internallink',array($page, $this->getLang('readmore'))); 419 $ins[] = array('p_close', array()); 420 $ins[] = array('section_close'); 421 return; 422 } 423 } 424 } 425 426 /** 427 * Makes user or date dependent includes possible 428 */ 429 function _apply_macro($id) { 430 global $INFO; 431 global $auth; 432 433 // if we don't have an auth object, do nothing 434 if (!$auth) return $id; 435 436 $user = $_SERVER['REMOTE_USER']; 437 $group = $INFO['userinfo']['grps'][0]; 438 439 $replace = array( 440 '@USER@' => cleanID($user), 441 '@NAME@' => cleanID($INFO['userinfo']['name']), 442 '@GROUP@' => cleanID($group), 443 '@YEAR@' => date('Y'), 444 '@MONTH@' => date('m'), 445 '@DAY@' => date('d'), 446 ); 447 return str_replace(array_keys($replace), array_values($replace), $id); 448 } 449} 450//vim:ts=4:sw=4:et:enc=utf-8: 451