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 16require_once(DOKU_INC.'inc/search.php'); 17 18class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin 19 20 var $includes = array(); 21 var $hasparts = array(); 22 var $toplevel_id = NULL; 23 var $toplevel = 0; 24 var $defaults = array(); 25 var $include_key = ''; 26 27 /** 28 * Constructor loads default config settings once 29 */ 30 function helper_plugin_include() { 31 $this->defaults['firstsec'] = $this->getConf('firstseconly'); 32 $this->defaults['editbtn'] = $this->getConf('showeditbtn'); 33 $this->defaults['taglogos'] = $this->getConf('showtaglogos'); 34 $this->defaults['footer'] = $this->getConf('showfooter'); 35 $this->defaults['redirect'] = $this->getConf('doredirect'); 36 $this->defaults['date'] = $this->getConf('showdate'); 37 $this->defaults['user'] = $this->getConf('showuser'); 38 $this->defaults['comments'] = $this->getConf('showcomments'); 39 $this->defaults['linkbacks'] = $this->getConf('showlinkbacks'); 40 $this->defaults['tags'] = $this->getConf('showtags'); 41 $this->defaults['link'] = $this->getConf('showlink'); 42 $this->defaults['permalink'] = $this->getConf('showpermalink'); 43 $this->defaults['indent'] = $this->getConf('doindent'); 44 } 45 46 function getInfo() { 47 return array( 48 'author' => 'Gina Häußge, Michael Klier, Esther Brunner', 49 'email' => 'dokuwiki@chimeric.de', 50 'date' => @file_get_contents(DOKU_PLUGIN . 'blog/VERSION'), 51 'name' => 'Include Plugin (helper class)', 52 'desc' => 'Functions to include another page in a wiki page', 53 'url' => 'http://dokuwiki.org/plugin:include', 54 ); 55 } 56 57 /** 58 * Available methods for other plugins 59 */ 60 function getMethods() { 61 $result = array(); 62 $result[] = array( 63 'name' => 'get_flags', 64 'desc' => 'overrides standard values for showfooter and firstseconly settings', 65 'params' => array('flags' => 'array'), 66 ); 67 return $result; 68 } 69 70 /** 71 * Overrides standard values for showfooter and firstseconly settings 72 */ 73 function get_flags($setflags) { 74 // load defaults 75 $flags = array(); 76 $flags = $this->defaults; 77 foreach ($setflags as $flag) { 78 switch ($flag) { 79 case 'footer': 80 $flags['footer'] = 1; 81 break; 82 case 'nofooter': 83 $flags['footer'] = 0; 84 break; 85 case 'firstseconly': 86 case 'firstsectiononly': 87 $flags['firstsec'] = 1; 88 break; 89 case 'fullpage': 90 $flags['firstsec'] = 0; 91 break; 92 case 'noheader': 93 $flags['noheader'] = 1; 94 break; 95 case 'editbtn': 96 case 'editbutton': 97 $flags['editbtn'] = 1; 98 break; 99 case 'noeditbtn': 100 case 'noeditbutton': 101 $flags['editbtn'] = 0; 102 break; 103 case 'permalink': 104 $flags['permalink'] = 1; 105 break; 106 case 'nopermalink': 107 $flags['permalink'] = 0; 108 break; 109 case 'redirect': 110 $flags['redirect'] = 1; 111 break; 112 case 'noredirect': 113 $flags['redirect'] = 0; 114 break; 115 case 'link': 116 $flags['link'] = 1; 117 break; 118 case 'nolink': 119 $flags['link'] = 0; 120 break; 121 case 'user': 122 $flags['user'] = 1; 123 break; 124 case 'nouser': 125 $flags['user'] = 0; 126 break; 127 case 'comments': 128 $flags['comments'] = 1; 129 break; 130 case 'nocomments': 131 $flags['comments'] = 0; 132 break; 133 case 'linkbacks': 134 $flags['linkbacks'] = 1; 135 break; 136 case 'nolinkbacks': 137 $flags['linkbacks'] = 0; 138 break; 139 case 'tags': 140 $flags['tags'] = 1; 141 break; 142 case 'notags': 143 $flags['tags'] = 0; 144 break; 145 case 'date': 146 $flags['date'] = 1; 147 break; 148 case 'nodate': 149 $flags['date'] = 0; 150 break; 151 case 'indent': 152 $flags['indent'] = 1; 153 break; 154 case 'noindent': 155 $flags['indent'] = 0; 156 break; 157 } 158 } 159 return $flags; 160 } 161 162 /** 163 * Parses the instructions list of the page which contains the includes 164 * 165 * @author Michael Klier <chi@chimeric.de> 166 */ 167 function parse_instructions($id, &$ins) { 168 global $conf; 169 global $INFO; 170 171 $num = count($ins); 172 173 $lvl = false; 174 $prev_lvl = false; 175 $mode = ''; 176 $page = ''; 177 $flags = array(); 178 $range = false; 179 $scope = $id; 180 181 for($i=0; $i<$num; $i++) { 182 // set current level 183 if($ins[$i][0] == 'section_open') { 184 $lvl = $ins[$i][1][0]; 185 if($i > $range) $prev_lvl = $lvl; 186 } 187 188 if($ins[$i][0] == 'plugin' && $ins[$i][1][0] == 'include_include' ) { 189 // found no previous section set lvl to 0 190 if(!$lvl) $lvl = 0; 191 192 $mode = $ins[$i][1][1][0]; 193 194 if($mode == 'namespace') { 195 $ns = str_replace(':', '/', cleanID($ins[$i][1][1][1])); 196 $sect = ''; 197 $flags = $ins[$i][1][1][3]; 198 199 $pages = array(); 200 search($pages, $conf['datadir'], 'search_list', '', $ns); 201 202 if(!empty($pages)) { 203 $ins_inc = array(); 204 foreach($pages as $page) { 205 $perm = auth_quickaclcheck($page['id']); 206 array_push($this->hasparts, $page['id']); 207 if($perm < AUTH_READ) continue; 208 $ins_tmp[0] = 'plugin'; 209 $ins_tmp[1][0] = 'include_include'; 210 $ins_tmp[1][1][0] = 'page'; 211 $ins_tmp[1][1][1] = $page['id']; 212 $ins_tmp[1][1][2] = ''; 213 $ins_tmp[1][1][3] = $flags; 214 $ins_inc = array_merge($ins_inc, array($ins_tmp)); 215 } 216 $ins_start = array_slice($ins, 0, $i+1); 217 $ins_end = array_slice($ins, $i+1); 218 $ins = array_merge($ins_start, $ins_inc, $ins_end); 219 } 220 unset($ins[$i]); 221 $i--; 222 } 223 224 if($mode == 'page' || $mode == 'section') { 225 $page = $ins[$i][1][1][1]; 226 $perm = auth_quickaclcheck($page); 227 228 array_push($this->hasparts, $page); 229 if($perm < AUTH_READ) continue; 230 231 $sect = $ins[$i][1][1][2]; 232 $flags = $ins[$i][1][1][3]; 233 234 $page = $this->_apply_macro($page); 235 resolve_pageid(getNS($scope), $page, $exists); // resolve shortcuts 236 $ins[$i][1][1][4] = $scope; 237 $scope = $page; 238 $flags = $this->get_flags($flags); 239 240 if(!page_exists($page)) { 241 if($flags['footer']) { 242 $ins[$i] = $this->_footer($page, $sect, '', $flags, 0); 243 } else { 244 unset($ins[$i]); 245 } 246 } else { 247 $ins_inc = $this->_get_instructions($page, $sect, $mode, $lvl, $flags); 248 if(!empty($ins_inc)) { 249 // combine instructions and reset counter 250 $ins_start = array_slice($ins, 0, $i+1); 251 $ins_end = array_slice($ins, $i+1); 252 $range = $i + count($ins_inc); 253 $ins = array_merge($ins_start, $ins_inc, $ins_end); 254 $num = count($ins); 255 } 256 } 257 } 258 } 259 260 // check if we left the range of possible sub includes and reset lvl and scope to toplevel_id 261 if($range && ($i >= $range)) { 262 $lvl = ($prev_lvl == 0) ? 1 : $prev_lvl; 263 $prev_lvl = false; 264 $range = false; 265 // reset scope to toplevel_id 266 $scope = $this->toplevel_id; 267 } 268 } 269 270 if(!empty($INFO['userinfo'])) { 271 $include_key = $INFO['userinfo']['name'] . '|' . implode('|', $INFO['userinfo']['grps']); 272 } else { 273 $include_key = '@ALL'; 274 } 275 276 $meta = array(); 277 $meta = p_get_metadata($id, 'plugin_include'); 278 $meta['pages'] = array_unique($this->hasparts); 279 $meta['keys'][$include_key] = true; 280 p_set_metadata($id, array('plugin_include' => $meta), true, true); 281 } 282 283 /** 284 * Returns the converted instructions of a give page/section 285 * 286 * @author Michael Klier <chi@chimeric.de> 287 */ 288 function _get_instructions($page, $sect, $mode, $lvl, $flags) { 289 global $ID; 290 291 $key = ($sect) ? $page . '#' . $sect : $page; 292 293 // prevent recursion 294 if(!$this->includes[$key]) { 295 $ins = p_cached_instructions(wikiFN($page)); 296 $this->includes[$key] = true; 297 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags); 298 return $ins; 299 } 300 } 301 302 /** 303 * Converts instructions of the included page 304 * 305 * The funcion iterates over the given list of instructions and generates 306 * an index of header and section indicies. It also removes document 307 * start/end instructions, converts links, and removes unwanted 308 * instructions like tags, comments, linkbacks. 309 * 310 * Later all header/section levels are convertet to match the current 311 * inclusion level. 312 * 313 * @author Michael Klier <chi@chimeric.de> 314 */ 315 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags) { 316 317 // filter instructions if needed 318 if(!empty($sect)) { 319 $this->_get_section($ins, $sect); // section required 320 } 321 322 if($flags['firstsec']) { 323 $this->_get_firstsec($ins, $page); // only first section 324 } 325 326 $ns = getNS($page); 327 $num = count($ins); 328 329 $conv_idx = array(); // conversion index 330 $lvl_max = false; // max level 331 $first_header = -1; 332 $no_header = false; 333 $sect_title = false; 334 335 for($i=0; $i<$num; $i++) { 336 switch($ins[$i][0]) { 337 case 'document_start': 338 case 'document_end': 339 case 'section_edit': 340 unset($ins[$i]); 341 break; 342 case 'header': 343 // get section title of first section 344 if($sect && !$sect_title) { 345 $sect_title = $ins[$i][1][0]; 346 } 347 // check if we need to skip the first header 348 if((!$no_header) && $flags['noheader']) { 349 $no_header = true; 350 } 351 352 $conv_idx[] = $i; 353 // get index of first header 354 if($first_header == -1) $first_header = $i; 355 // get max level of this instructions set 356 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 357 $lvl_max = $ins[$i][1][1]; 358 } 359 break; 360 case 'section_open': 361 $conv_idx[] = $i; 362 break; 363 case 'internallink': 364 case 'internalmedia': 365 if($ins[$i][1][0]{0} == '.') { 366 if($ins[$i][1][0]{1} == '.') { 367 $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace 368 } else { 369 $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace 370 } 371 } elseif (strpos($ins[$i][1][0], ':') === false) { 372 $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links 373 } 374 break; 375 case 'plugin': 376 // FIXME skip other plugins? 377 switch($ins[$i][1][0]) { 378 case 'tag_tag': // skip tags 379 case 'discussion_comments': // skip comments 380 case 'linkback': // skip linkbacks 381 case 'data_entry': // skip data plugin 382 case 'meta': // skip meta plugin 383 unset($ins[$i]); 384 break; 385 } 386 break; 387 default: 388 break; 389 } 390 } 391 392 // calculate difference between header/section level and include level 393 $diff = 0; 394 if (!$lvl_max) $lvl_max = 0; // if no level found in target, set to 0 395 $diff = $lvl - $lvl_max + 1; 396 if ($no_header) $diff -= 1; // push up one level if "noheader" 397 398 // convert headers and set footer/permalink 399 $hdr_deleted = false; 400 $has_permalink = false; 401 $footer_lvl = false; 402 foreach($conv_idx as $idx) { 403 if($ins[$idx][0] == 'header') { 404 if($no_header && !$hdr_deleted) { 405 unset ($ins[$idx]); 406 $hdr_deleted = true; 407 continue; 408 } 409 410 if($flags['indent']) { 411 $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 412 $ins[$idx][1][1] = $lvl_new; 413 } 414 415 // set permalink 416 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 417 $this->_permalink($ins[$idx], $page, $sect, $flags); 418 $has_permalink = true; 419 } 420 421 // set footer level 422 if(!$footer_lvl && ($idx == $first_header) && !$no_header) { 423 if($flags['indent']) { 424 $footer_lvl = $lvl_new; 425 } else { 426 $footer_lvl = $lvl_max; 427 } 428 } 429 } else { 430 // it's a section 431 if($flags['indent']) { 432 $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 433 $ins[$idx][1][0] = $lvl_new; 434 } 435 436 // check if noheader is used and set the footer level to the first section 437 if($no_header && !$footer_lvl) { 438 if($flags['indent']) { 439 $footer_lvl = $lvl_new; 440 } else { 441 $footer_lvl = $lvl_max; 442 } 443 } 444 } 445 } 446 447 // add edit button 448 if($flags['editbtn'] && (auth_quickaclcheck($page) >= AUTH_EDIT)) { 449 $this->_editbtn($ins, $page, $sect, $sect_title); 450 } 451 452 // add footer 453 if($flags['footer']) { 454 $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl); 455 } 456 457 // add instructions entry divs 458 array_unshift($ins, array('plugin', array('include_div', array('open', $page)))); 459 array_push($ins, array('plugin', array('include_div', array('close')))); 460 461 // close previous section if any and re-open after inclusion 462 if($lvl != 0) { 463 array_unshift($ins, array('section_close')); 464 $ins[] = array('section_open', array($lvl)); 465 } 466 } 467 468 /** 469 * Appends instruction item for the include plugin footer 470 * 471 * @author Michael Klier <chi@chimeric.de> 472 */ 473 function _footer($page, $sect, $sect_title, $flags, $footer_lvl) { 474 $footer = array(); 475 $footer[0] = 'plugin'; 476 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $this->toplevel_id, $footer_lvl)); 477 return $footer; 478 } 479 480 /** 481 * Appends instruction item for an edit button 482 * 483 * @author Michael Klier <chi@chimeric.de> 484 */ 485 function _editbtn(&$ins, $page, $sect, $sect_title) { 486 $editbtn = array(); 487 $editbtn[0] = 'plugin'; 488 $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $this->toplevel_id)); 489 $ins[] = $editbtn; 490 } 491 492 /** 493 * Convert instruction item for a permalink header 494 * 495 * @author Michael Klier <chi@chimeric.de> 496 */ 497 function _permalink(&$ins, $page, $sect, $flags) { 498 $ins[0] = 'plugin'; 499 $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags)); 500 } 501 502 /** 503 * Get a section including its subsections 504 * 505 * @author Michael Klier <chi@chimeric.de> 506 */ 507 function _get_section(&$ins, $sect) { 508 $num = count($ins); 509 $offset = false; 510 $lvl = false; 511 $end = false; 512 513 for($i=0; $i<$num; $i++) { 514 if ($ins[$i][0] == 'header') { 515 516 // found the right header 517 if (cleanID($ins[$i][1][0]) == $sect) { 518 $offset = $i; 519 $lvl = $ins[$i][1][1]; 520 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 521 $end = $i - $offset; 522 break; 523 } 524 } 525 } 526 $offset = $offset ? $offset : 0; 527 $end = $end ? $end : ($num - 1); 528 if(is_array($ins)) { 529 $ins = array_slice($ins, $offset, $end); 530 } 531 } 532 533 /** 534 * Only display the first section of a page and a readmore link 535 * 536 * @author Michael Klier <chi@chimeric.de> 537 */ 538 function _get_firstsec(&$ins, $page) { 539 $num = count($ins); 540 $first_sect = false; 541 for($i=0; $i<$num; $i++) { 542 if($ins[$i][0] == 'section_close') { 543 $first_sect = $i; 544 } 545 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 546 $ins = array_slice($ins, 0, $first_sect); 547 $ins[] = array('p_open', array()); 548 $ins[] = array('internallink',array($page, $this->getLang('readmore'))); 549 $ins[] = array('p_close', array()); 550 $ins[] = array('section_close'); 551 return; 552 } 553 } 554 } 555 556 /** 557 * Makes user or date dependent includes possible 558 */ 559 function _apply_macro($id) { 560 global $INFO; 561 global $auth; 562 563 // if we don't have an auth object, do nothing 564 if (!$auth) return $id; 565 566 $user = $_SERVER['REMOTE_USER']; 567 $group = $INFO['userinfo']['grps'][0]; 568 569 $replace = array( 570 '@USER@' => cleanID($user), 571 '@NAME@' => cleanID($INFO['userinfo']['name']), 572 '@GROUP@' => cleanID($group), 573 '@YEAR@' => date('Y'), 574 '@MONTH@' => date('m'), 575 '@DAY@' => date('d'), 576 ); 577 return str_replace(array_keys($replace), array_values($replace), $id); 578 } 579} 580//vim:ts=4:sw=4:et:enc=utf-8: 581