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 = NULL; 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 // check if toplevel is set already 144 if(!isset($this->toplevel)) $this->toplevel = $lvl; 145 146 $mode = $ins[$i][1][1][0]; 147 $page = $ins[$i][1][1][1]; 148 $sect = $ins[$i][1][1][2]; 149 $flags = $ins[$i][1][1][3]; 150 151 // check if we left the range of possible sub includes and reset lvl to toplevel 152 if($range && ($i > $range)) { 153 if($prev_lvl) { 154 $lvl = $prev_lvl; 155 $prev_lvl = false; 156 } else { 157 $lvl = $this->toplevel; 158 } 159 } 160 161 $page = $this->_apply_macro($page); 162 resolve_pageid(getNS($id), $page, $exists); // resolve shortcuts 163 $flags = $this->get_flags($flags); 164 165 $ins_inc = $this->_get_instructions($page, $sect, $mode, $lvl, $flags); 166 167 if(!empty($ins_inc)) { 168 // combine instructions and reset counter 169 $ins_start = array_slice($ins, 0, $i+1); 170 $ins_end = array_slice($ins, $i+1); 171 $range = $i + count($ins_inc); 172 $ins = array_merge($ins_start, $ins_inc, $ins_end); 173 $num = count($ins); 174 } 175 } 176 } 177 } 178 179 /** 180 * Returns the converted instructions of a give page/section 181 * 182 * @author Michael Klier <chi@chimeric.de> 183 */ 184 function _get_instructions($page, $sect, $mode, $lvl, $flags) { 185 global $ID; 186 187 if($ID == $page || !page_exists($page) || (page_exists($page) && auth_quickaclcheck($page) < AUTH_READ)) return array(); 188 $key = ($sect) ? $page . '#' . $sect : $page; 189 190 // prevent recursion 191 if(!$this->includes[$key]) { 192 $ins = p_cached_instructions(wikiFN($page)); 193 $this->includes[$key] = true; 194 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags); 195 return $ins; 196 } 197 } 198 199 /** 200 * Converts instructions of the included page 201 * 202 * The funcion iterates over the given list of instructions and generates 203 * an index of header and section indicies. It also removes document 204 * start/end instructions, converts links, and removes unwanted 205 * instructions like tags, comments, linkbacks. 206 * 207 * Later all header/section levels are convertet to match the current 208 * inclusion level. 209 * 210 * @author Michael Klier <chi@chimeric.de> 211 */ 212 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags) { 213 214 // filter instructions if needed 215 if(!empty($sect)) { 216 $this->_get_section($ins, $sect); // section required 217 } 218 219 if($flags['firstsec']) { 220 $this->_get_firstsec($ins, $page); // only first section 221 } 222 223 $ns = getNS($page); 224 $num = count($ins); 225 226 $conv_idx = array(); // conversion index 227 $lvl_max = false; // max level 228 $first_header = -1; 229 $no_header = false; 230 $sect_title = false; 231 232 for($i=0; $i<$num; $i++) { 233 switch($ins[$i][0]) { 234 case 'document_start': 235 case 'document_end': 236 case 'section_edit': 237 unset($ins[$i]); 238 break; 239 case 'header': 240 // get section title of first section 241 if($sect && !$sect_title) { 242 $sect_title = $ins[$i][1][0]; 243 } 244 // check if we need to skip the first header 245 if((!$no_header) && $flags['noheader']) { 246 unset($ins[$i]); 247 $no_header = true; 248 continue; 249 } 250 $conv_idx[] = $i; 251 // get index of first header 252 if($first_header == -1) $first_header = $i; 253 // get max level if this instructions set 254 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 255 $lvl_max = $ins[$i][1][1]; 256 } 257 break; 258 case 'section_open': 259 $conv_idx[] = $i; 260 break; 261 case 'internallink': 262 case 'internalmedia': 263 if($ins[$i][1][0]{0} == '.') { 264 if($ins[$i][1][0]{1} == '.') { 265 $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace 266 } else { 267 $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace 268 } 269 } elseif (strpos($ins[$i][1][0], ':') === false) { 270 $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links 271 } 272 break; 273 case 'plugin': 274 // FIXME skip other plugins? 275 switch($ins[$i][1][0]) { 276 case 'tag_tag': // skip tags 277 case 'discussion_comments': // skip comments 278 case 'linkback': // skip linkbacks 279 case 'data_entry': // skip data plugin 280 unset($ins[$i]); 281 break; 282 } 283 break; 284 default: 285 break; 286 } 287 } 288 289 // calculate difference between header/section level and include level 290 $diff = 0; 291 if($lvl_max) { 292 // max level equals inclusion level diff is 1 293 if($lvl_max == $lvl) { 294 $diff = 1; 295 } 296 // max level is les than inclusion level, we have to convert downwards 297 if($lvl_max < $lvl) { 298 $diff = (($lvl - $lvl_max) + 1); 299 } 300 // max level is greate inclusion level, we have to convert upwards 301 if($lvl_max > $lvl) { 302 if($lvl == 0) { 303 // we had no previous section pretend it was 1 304 $diff = (1 - $lvl_max); 305 } elseif(($lvl - $lvl_max) == -1) { 306 // we don't need to convert anything up diff is 0 307 $diff = 0; 308 } else { 309 // convert everything up 310 $diff = ($lvl - $lvl_max); 311 } 312 } 313 } 314 315 // convert headers and set footer/permalink 316 $has_permalink = false; 317 $footer_lvl = false; 318 foreach($conv_idx as $idx) { 319 if($ins[$idx][0] == 'header') { 320 $new_lvl = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 321 $ins[$idx][1][1] = $new_lvl; 322 323 // set permalink 324 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 325 $this->_permalink($ins[$idx], $page, $sect, $flags); 326 $has_permalink = true; 327 } 328 } else { 329 // it's a section 330 $new_lvl = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 331 $ins[$idx][1][0] = $new_lvl; 332 // check if noheader is used and set the footer level to the first section 333 if($no_header && !$footer_lvl) $footer_lvl = $new_lvl; 334 } 335 336 // set footer level 337 if(!$footer_lvl && ($idx == $first_header)) $footer_lvl = $new_lvl; 338 } 339 340 // add edit button 341 if($flags['editbtn']) $this->_editbtn($ins, $page, $sect, $sect_title); 342 343 // add footer 344 if($flags['footer']) $this->_footer($ins, $page, $sect, $sect_title, $flags, $footer_lvl); 345 346 // add instructions entry divs 347 array_unshift($ins, array('plugin', array('include_div', array('open', $page)))); 348 array_push($ins, array('plugin', array('include_div', array('close')))); 349 350 // close previous section if any and re-open after inclusion 351 if($lvl != 0) { 352 array_unshift($ins, array('section_close')); 353 $ins[] = array('section_open', array($lvl)); 354 } 355 } 356 357 /** 358 * Appends instruction item for the include plugin footer 359 * 360 * @author Michael Klier <chi@chimeric.de> 361 */ 362 function _footer(&$ins, $page, $sect, $sect_title, $flags, $footer_lvl) { 363 $footer = array(); 364 $footer[0] = 'plugin'; 365 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $this->toplevel_id, $footer_lvl)); 366 $ins[] = $footer; 367 } 368 369 /** 370 * Appends instruction item for an edit button 371 * 372 * @author Michael Klier <chi@chimeric.de> 373 */ 374 function _editbtn(&$ins, $page, $sect, $sect_title) { 375 $editbtn = array(); 376 $editbtn[0] = 'plugin'; 377 $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $this->toplevel_id)); 378 $ins[] = $editbtn; 379 } 380 381 /** 382 * Convert instruction item for a permalink header 383 * 384 * @author Michael Klier <chi@chimeric.de> 385 */ 386 function _permalink(&$ins, $page, $sect, $flags) { 387 $ins[0] = 'plugin'; 388 $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags)); 389 } 390 391 /** 392 * Get a section including its subsections 393 * 394 * @author Michael Klier <chi@chimeric.de> 395 */ 396 function _get_section(&$ins, $sect) { 397 $num = count($ins); 398 $offset = false; 399 $lvl = false; 400 401 for($i=0; $i<$num; $i++) { 402 if ($ins[$i][0] == 'header') { 403 404 // found the right header 405 if (cleanID($ins[$i][1][0]) == $sect) { 406 $offset = $i; 407 $lvl = $ins[$i][1][1]; 408 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 409 $ins = array_slice($ins, $offset, ($i - $offset)); 410 } 411 } 412 } 413 } 414 415 /** 416 * Only display the first section of a page and a readmore link 417 * 418 * @author Michael Klier <chi@chimeric.de> 419 */ 420 function _get_firstsec(&$ins, $page) { 421 $num = count($ins); 422 $first_sect = false; 423 for($i=0; $i<$num; $i++) { 424 if($ins[$i][0] == 'section_close') { 425 $first_sect = $i; 426 } 427 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 428 $ins = array_slice($ins, 0, $first_sect); 429 $ins[] = array('p_open', array()); 430 $ins[] = array('internallink',array($page, $this->getLang('readmore'))); 431 $ins[] = array('p_close', array()); 432 $ins[] = array('section_close'); 433 return; 434 } 435 } 436 } 437 438 /** 439 * Makes user or date dependent includes possible 440 */ 441 function _apply_macro($id) { 442 global $INFO; 443 global $auth; 444 445 // if we don't have an auth object, do nothing 446 if (!$auth) return $id; 447 448 $user = $_SERVER['REMOTE_USER']; 449 $userdata = $auth->getUserData($user); 450 $group = $userdata['grps'][0]; 451 452 $replace = array( 453 '@USER@' => cleanID($user), 454 '@NAME@' => cleanID($INFO['userinfo']['name']), 455 '@GROUP@' => cleanID($group), 456 '@YEAR@' => date('Y'), 457 '@MONTH@' => date('m'), 458 '@DAY@' => date('d'), 459 ); 460 return str_replace(array_keys($replace), array_values($replace), $id); 461 } 462} 463//vim:ts=4:sw=4:et:enc=utf-8: 464