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 if($perm < AUTH_READ) continue; 207 array_push($this->hasparts, $page['id']); 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 if($perm < AUTH_READ) continue; 229 array_push($this->hasparts, $page); 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 = p_get_metadata($id, 'plugin_include'); 277 $meta[$include_key] = array_unique($this->hasparts); 278 p_set_metadata($id, array('plugin_include' => $meta), true, true); 279 } 280 281 /** 282 * Returns the converted instructions of a give page/section 283 * 284 * @author Michael Klier <chi@chimeric.de> 285 */ 286 function _get_instructions($page, $sect, $mode, $lvl, $flags) { 287 global $ID; 288 289 $key = ($sect) ? $page . '#' . $sect : $page; 290 291 // prevent recursion 292 if(!$this->includes[$key]) { 293 $ins = p_cached_instructions(wikiFN($page)); 294 $this->includes[$key] = true; 295 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags); 296 return $ins; 297 } 298 } 299 300 /** 301 * Converts instructions of the included page 302 * 303 * The funcion iterates over the given list of instructions and generates 304 * an index of header and section indicies. It also removes document 305 * start/end instructions, converts links, and removes unwanted 306 * instructions like tags, comments, linkbacks. 307 * 308 * Later all header/section levels are convertet to match the current 309 * inclusion level. 310 * 311 * @author Michael Klier <chi@chimeric.de> 312 */ 313 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags) { 314 315 // filter instructions if needed 316 if(!empty($sect)) { 317 $this->_get_section($ins, $sect); // section required 318 } 319 320 if($flags['firstsec']) { 321 $this->_get_firstsec($ins, $page); // only first section 322 } 323 324 $ns = getNS($page); 325 $num = count($ins); 326 327 $conv_idx = array(); // conversion index 328 $lvl_max = false; // max level 329 $first_header = -1; 330 $no_header = false; 331 $sect_title = false; 332 333 for($i=0; $i<$num; $i++) { 334 switch($ins[$i][0]) { 335 case 'document_start': 336 case 'document_end': 337 case 'section_edit': 338 unset($ins[$i]); 339 break; 340 case 'header': 341 // get section title of first section 342 if($sect && !$sect_title) { 343 $sect_title = $ins[$i][1][0]; 344 } 345 // check if we need to skip the first header 346 if((!$no_header) && $flags['noheader']) { 347 $no_header = true; 348 } 349 350 $conv_idx[] = $i; 351 // get index of first header 352 if($first_header == -1) $first_header = $i; 353 // get max level of this instructions set 354 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 355 $lvl_max = $ins[$i][1][1]; 356 } 357 break; 358 case 'section_open': 359 $conv_idx[] = $i; 360 break; 361 case 'internallink': 362 case 'internalmedia': 363 if($ins[$i][1][0]{0} == '.') { 364 if($ins[$i][1][0]{1} == '.') { 365 $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace 366 } else { 367 $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace 368 } 369 } elseif (strpos($ins[$i][1][0], ':') === false) { 370 $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links 371 } 372 break; 373 case 'plugin': 374 // FIXME skip other plugins? 375 switch($ins[$i][1][0]) { 376 case 'tag_tag': // skip tags 377 case 'discussion_comments': // skip comments 378 case 'linkback': // skip linkbacks 379 case 'data_entry': // skip data plugin 380 case 'meta': // skip meta plugin 381 unset($ins[$i]); 382 break; 383 } 384 break; 385 default: 386 break; 387 } 388 } 389 390 // calculate difference between header/section level and include level 391 $diff = 0; 392 if (!$lvl_max) $lvl_max = 0; // if no level found in target, set to 0 393 $diff = $lvl - $lvl_max + 1; 394 if ($no_header) $diff -= 1; // push up one level if "noheader" 395 396 // convert headers and set footer/permalink 397 $hdr_deleted = false; 398 $has_permalink = false; 399 $footer_lvl = false; 400 foreach($conv_idx as $idx) { 401 if($ins[$idx][0] == 'header') { 402 if($no_header && !$hdr_deleted) { 403 unset ($ins[$idx]); 404 $hdr_deleted = true; 405 continue; 406 } 407 408 if($flags['indent']) { 409 $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 410 $ins[$idx][1][1] = $lvl_new; 411 } 412 413 // set permalink 414 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 415 $this->_permalink($ins[$idx], $page, $sect, $flags); 416 $has_permalink = true; 417 } 418 419 // set footer level 420 if(!$footer_lvl && ($idx == $first_header) && !$no_header) { 421 if($flags['indent']) { 422 $footer_lvl = $lvl_new; 423 } else { 424 $footer_lvl = $lvl_max; 425 } 426 } 427 } else { 428 // it's a section 429 if($flags['indent']) { 430 $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 431 $ins[$idx][1][0] = $lvl_new; 432 } 433 434 // check if noheader is used and set the footer level to the first section 435 if($no_header && !$footer_lvl) { 436 if($flags['indent']) { 437 $footer_lvl = $lvl_new; 438 } else { 439 $footer_lvl = $lvl_max; 440 } 441 } 442 } 443 } 444 445 // add edit button 446 if($flags['editbtn'] && (auth_quickaclcheck($page) >= AUTH_EDIT)) { 447 $this->_editbtn($ins, $page, $sect, $sect_title); 448 } 449 450 // add footer 451 if($flags['footer']) { 452 $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl); 453 } 454 455 // add instructions entry divs 456 array_unshift($ins, array('plugin', array('include_div', array('open', $page)))); 457 array_push($ins, array('plugin', array('include_div', array('close')))); 458 459 // close previous section if any and re-open after inclusion 460 if($lvl != 0) { 461 array_unshift($ins, array('section_close')); 462 $ins[] = array('section_open', array($lvl)); 463 } 464 } 465 466 /** 467 * Appends instruction item for the include plugin footer 468 * 469 * @author Michael Klier <chi@chimeric.de> 470 */ 471 function _footer($page, $sect, $sect_title, $flags, $footer_lvl) { 472 $footer = array(); 473 $footer[0] = 'plugin'; 474 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $this->toplevel_id, $footer_lvl)); 475 return $footer; 476 } 477 478 /** 479 * Appends instruction item for an edit button 480 * 481 * @author Michael Klier <chi@chimeric.de> 482 */ 483 function _editbtn(&$ins, $page, $sect, $sect_title) { 484 $editbtn = array(); 485 $editbtn[0] = 'plugin'; 486 $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $this->toplevel_id)); 487 $ins[] = $editbtn; 488 } 489 490 /** 491 * Convert instruction item for a permalink header 492 * 493 * @author Michael Klier <chi@chimeric.de> 494 */ 495 function _permalink(&$ins, $page, $sect, $flags) { 496 $ins[0] = 'plugin'; 497 $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags)); 498 } 499 500 /** 501 * Get a section including its subsections 502 * 503 * @author Michael Klier <chi@chimeric.de> 504 */ 505 function _get_section(&$ins, $sect) { 506 $num = count($ins); 507 $offset = false; 508 $lvl = false; 509 $end = false; 510 511 for($i=0; $i<$num; $i++) { 512 if ($ins[$i][0] == 'header') { 513 514 // found the right header 515 if (cleanID($ins[$i][1][0]) == $sect) { 516 $offset = $i; 517 $lvl = $ins[$i][1][1]; 518 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 519 $end = $i - $offset; 520 break; 521 } 522 } 523 } 524 $offset = $offset ? $offset : 0; 525 $end = $end ? $end : ($num - 1); 526 if(is_array($ins)) { 527 $ins = array_slice($ins, $offset, $end); 528 } 529 } 530 531 /** 532 * Only display the first section of a page and a readmore link 533 * 534 * @author Michael Klier <chi@chimeric.de> 535 */ 536 function _get_firstsec(&$ins, $page) { 537 $num = count($ins); 538 $first_sect = false; 539 for($i=0; $i<$num; $i++) { 540 if($ins[$i][0] == 'section_close') { 541 $first_sect = $i; 542 } 543 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 544 $ins = array_slice($ins, 0, $first_sect); 545 $ins[] = array('p_open', array()); 546 $ins[] = array('internallink',array($page, $this->getLang('readmore'))); 547 $ins[] = array('p_close', array()); 548 $ins[] = array('section_close'); 549 return; 550 } 551 } 552 } 553 554 /** 555 * Makes user or date dependent includes possible 556 */ 557 function _apply_macro($id) { 558 global $INFO; 559 global $auth; 560 561 // if we don't have an auth object, do nothing 562 if (!$auth) return $id; 563 564 $user = $_SERVER['REMOTE_USER']; 565 $group = $INFO['userinfo']['grps'][0]; 566 567 $replace = array( 568 '@USER@' => cleanID($user), 569 '@NAME@' => cleanID($INFO['userinfo']['name']), 570 '@GROUP@' => cleanID($group), 571 '@YEAR@' => date('Y'), 572 '@MONTH@' => date('m'), 573 '@DAY@' => date('d'), 574 ); 575 return str_replace(array_keys($replace), array_values($replace), $id); 576 } 577} 578//vim:ts=4:sw=4:et:enc=utf-8: 579