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