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 $defaults = array(); 21 var $sec_close = true; 22 var $taghelper = null; 23 var $includes = array(); // deprecated - compatibility code for the blog plugin 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 $this->defaults['linkonly'] = $this->getConf('linkonly'); 43 $this->defaults['title'] = $this->getConf('title'); 44 $this->defaults['pageexists'] = $this->getConf('pageexists'); 45 $this->defaults['parlink'] = $this->getConf('parlink'); 46 $this->defaults['inline'] = false; 47 } 48 49 /** 50 * Available methods for other plugins 51 */ 52 function getMethods() { 53 $result = array(); 54 $result[] = array( 55 'name' => 'get_flags', 56 'desc' => 'overrides standard values for showfooter and firstseconly settings', 57 'params' => array('flags' => 'array'), 58 ); 59 return $result; 60 } 61 62 /** 63 * Overrides standard values for showfooter and firstseconly settings 64 */ 65 function get_flags($setflags) { 66 // load defaults 67 $flags = array(); 68 $flags = $this->defaults; 69 foreach ($setflags as $flag) { 70 switch ($flag) { 71 case 'footer': 72 $flags['footer'] = 1; 73 break; 74 case 'nofooter': 75 $flags['footer'] = 0; 76 break; 77 case 'firstseconly': 78 case 'firstsectiononly': 79 $flags['firstsec'] = 1; 80 break; 81 case 'fullpage': 82 $flags['firstsec'] = 0; 83 break; 84 case 'noheader': 85 $flags['noheader'] = 1; 86 break; 87 case 'editbtn': 88 case 'editbutton': 89 $flags['editbtn'] = 1; 90 break; 91 case 'noeditbtn': 92 case 'noeditbutton': 93 $flags['editbtn'] = 0; 94 break; 95 case 'permalink': 96 $flags['permalink'] = 1; 97 break; 98 case 'nopermalink': 99 $flags['permalink'] = 0; 100 break; 101 case 'redirect': 102 $flags['redirect'] = 1; 103 break; 104 case 'noredirect': 105 $flags['redirect'] = 0; 106 break; 107 case 'link': 108 $flags['link'] = 1; 109 break; 110 case 'nolink': 111 $flags['link'] = 0; 112 break; 113 case 'user': 114 $flags['user'] = 1; 115 break; 116 case 'nouser': 117 $flags['user'] = 0; 118 break; 119 case 'comments': 120 $flags['comments'] = 1; 121 break; 122 case 'nocomments': 123 $flags['comments'] = 0; 124 break; 125 case 'linkbacks': 126 $flags['linkbacks'] = 1; 127 break; 128 case 'nolinkbacks': 129 $flags['linkbacks'] = 0; 130 break; 131 case 'tags': 132 $flags['tags'] = 1; 133 break; 134 case 'notags': 135 $flags['tags'] = 0; 136 break; 137 case 'date': 138 $flags['date'] = 1; 139 break; 140 case 'nodate': 141 $flags['date'] = 0; 142 break; 143 case 'indent': 144 $flags['indent'] = 1; 145 break; 146 case 'noindent': 147 $flags['indent'] = 0; 148 break; 149 case 'linkonly': 150 $flags['linkonly'] = 1; 151 break; 152 case 'nolinkonly': 153 case 'include_content': 154 $flags['linkonly'] = 0; 155 break; 156 case 'inline': 157 $flags['inline'] = 1; 158 break; 159 case 'title': 160 $flags['title'] = 1; 161 break; 162 case 'pageexists': 163 $flags['pageexists'] = 1; 164 break; 165 case 'existlink': 166 $flags['pageexists'] = 1; 167 $flags['linkonly'] = 1; 168 break; 169 case 'noparlink': 170 $flags['parlink'] = 0; 171 break; 172 } 173 } 174 // the include_content URL parameter overrides flags 175 if (isset($_REQUEST['include_content'])) 176 $flags['linkonly'] = 0; 177 return $flags; 178 } 179 180 /** 181 * Returns the converted instructions of a give page/section 182 * 183 * @author Michael Klier <chi@chimeric.de> 184 * @author Michael Hamann <michael@content-space.de> 185 */ 186 function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null) { 187 $key = ($sect) ? $page . '#' . $sect : $page; 188 $this->includes[$key] = true; // legacy code for keeping compatibility with other plugins 189 190 // keep compatibility with other plugins that don't know the $root_id parameter 191 if (is_null($root_id)) { 192 global $ID; 193 $root_id = $ID; 194 } 195 196 if ($flags['linkonly']) { 197 if (page_exists($page) || $flags['pageexists'] == 0) { 198 $title = ''; 199 if ($flags['title']) 200 $title = p_get_first_heading($page); 201 if($flags['parlink']) { 202 $ins = array( 203 array('p_open', array()), 204 array('internallink', array(':'.$key, $title)), 205 array('p_close', array()), 206 ); 207 } else { 208 $ins = array(array('internallink', array(':'.$key,$title))); 209 } 210 }else { 211 $ins = array(); 212 } 213 } else { 214 if (page_exists($page)) { 215 global $ID; 216 $backupID = $ID; 217 $ID = $page; // Change the global $ID as otherwise plugins like the discussion plugin will save data for the wrong page 218 $ins = p_cached_instructions(wikiFN($page)); 219 $ID = $backupID; 220 } else { 221 $ins = array(); 222 } 223 224 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id); 225 } 226 return $ins; 227 } 228 229 /** 230 * Converts instructions of the included page 231 * 232 * The funcion iterates over the given list of instructions and generates 233 * an index of header and section indicies. It also removes document 234 * start/end instructions, converts links, and removes unwanted 235 * instructions like tags, comments, linkbacks. 236 * 237 * Later all header/section levels are convertet to match the current 238 * inclusion level. 239 * 240 * @author Michael Klier <chi@chimeric.de> 241 */ 242 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id) { 243 global $conf; 244 245 // filter instructions if needed 246 if(!empty($sect)) { 247 $this->_get_section($ins, $sect); // section required 248 } 249 250 if($flags['firstsec']) { 251 $this->_get_firstsec($ins, $page); // only first section 252 } 253 254 $ns = getNS($page); 255 $num = count($ins); 256 257 $conv_idx = array(); // conversion index 258 $lvl_max = false; // max level 259 $first_header = -1; 260 $no_header = false; 261 $sect_title = false; 262 263 for($i=0; $i<$num; $i++) { 264 switch($ins[$i][0]) { 265 case 'document_start': 266 case 'document_end': 267 case 'section_edit': 268 unset($ins[$i]); 269 break; 270 case 'header': 271 // get section title of first section 272 if($sect && !$sect_title) { 273 $sect_title = $ins[$i][1][0]; 274 } 275 // check if we need to skip the first header 276 if((!$no_header) && $flags['noheader']) { 277 $no_header = true; 278 } 279 280 $conv_idx[] = $i; 281 // get index of first header 282 if($first_header == -1) $first_header = $i; 283 // get max level of this instructions set 284 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 285 $lvl_max = $ins[$i][1][1]; 286 } 287 break; 288 case 'section_open': 289 if ($flags['inline']) 290 unset($ins[$i]); 291 else 292 $conv_idx[] = $i; 293 break; 294 case 'section_close': 295 if ($flags['inline']) 296 unset($ins[$i]); 297 break; 298 case 'internallink': 299 case 'internalmedia': 300 // make sure parameters aren't touched 301 $link_params = ''; 302 $link_id = $ins[$i][1][0]; 303 $link_parts = explode('?', $link_id, 2); 304 if (count($link_parts) === 2) { 305 $link_id = $link_parts[0]; 306 $link_params = $link_parts[1]; 307 } 308 // resolve the id without cleaning it 309 $link_id = resolve_id($ns, $link_id, false); 310 // this id is internal (i.e. absolute) now, add ':' to make resolve_id work again 311 if ($link_id{0} != ':') $link_id = ':'.$link_id; 312 // restore parameters 313 $ins[$i][1][0] = ($link_params != '') ? $link_id.'?'.$link_params : $link_id; 314 break; 315 case 'plugin': 316 // FIXME skip other plugins? 317 switch($ins[$i][1][0]) { 318 case 'tag_tag': // skip tags 319 case 'discussion_comments': // skip comments 320 case 'linkback': // skip linkbacks 321 case 'data_entry': // skip data plugin 322 case 'meta': // skip meta plugin 323 unset($ins[$i]); 324 break; 325 // adapt indentation level of nested includes 326 case 'include_include': 327 if (!$flags['inline'] && $flags['indent']) 328 $ins[$i][1][1][4] += $lvl; 329 break; 330 } 331 break; 332 default: 333 break; 334 } 335 } 336 337 // calculate difference between header/section level and include level 338 $diff = 0; 339 if (!isset($lvl_max)) $lvl_max = 0; // if no level found in target, set to 0 340 $diff = $lvl - $lvl_max + 1; 341 if ($no_header) $diff -= 1; // push up one level if "noheader" 342 343 // convert headers and set footer/permalink 344 $hdr_deleted = false; 345 $has_permalink = false; 346 $footer_lvl = false; 347 $contains_secedit = false; 348 $section_close_at = false; 349 foreach($conv_idx as $idx) { 350 if($ins[$idx][0] == 'header') { 351 if ($section_close_at === false) { 352 // store the index of the first heading (the begin of the first section) 353 $section_close_at = $idx; 354 } 355 356 if($no_header && !$hdr_deleted) { 357 unset ($ins[$idx]); 358 $hdr_deleted = true; 359 continue; 360 } 361 362 if($flags['indent']) { 363 $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 364 $ins[$idx][1][1] = $lvl_new; 365 } 366 367 if($ins[$idx][1][1] <= $conf['maxseclevel']) 368 $contains_secedit = true; 369 370 // set permalink 371 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 372 $this->_permalink($ins[$idx], $page, $sect, $flags); 373 $has_permalink = true; 374 } 375 376 // set footer level 377 if(!$footer_lvl && ($idx == $first_header) && !$no_header) { 378 if($flags['indent']) { 379 $footer_lvl = $lvl_new; 380 } else { 381 $footer_lvl = $lvl_max; 382 } 383 } 384 } else { 385 // it's a section 386 if($flags['indent']) { 387 $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 388 $ins[$idx][1][0] = $lvl_new; 389 } 390 391 // check if noheader is used and set the footer level to the first section 392 if($no_header && !$footer_lvl) { 393 if($flags['indent']) { 394 $footer_lvl = $lvl_new; 395 } else { 396 $footer_lvl = $lvl_max; 397 } 398 } 399 } 400 } 401 402 // close last open section of the included page if there is any 403 if ($contains_secedit) { 404 array_push($ins, array('plugin', array('include_closelastsecedit', array()))); 405 } 406 407 // add edit button 408 if($flags['editbtn']) { 409 $this->_editbtn($ins, $page, $sect, $sect_title, ($flags['redirect'] ? $root_id : false)); 410 } 411 412 // add footer 413 if($flags['footer']) { 414 $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id); 415 } 416 417 // wrap content at the beginning of the include that is not in a section in a section 418 if ($lvl > 0 && $section_close_at !== 0 && $flags['indent'] && !$flags['inline']) { 419 if ($section_close_at === false) { 420 $ins[] = array('section_close', array()); 421 array_unshift($ins, array('section_open', array($lvl))); 422 } else { 423 $section_close_idx = array_search($section_close_at, array_keys($ins)); 424 if ($section_close_idx > 0) { 425 $before_ins = array_slice($ins, 0, $section_close_idx); 426 $after_ins = array_slice($ins, $section_close_idx); 427 $ins = array_merge($before_ins, array(array('section_close', array())), $after_ins); 428 array_unshift($ins, array('section_open', array($lvl))); 429 } 430 } 431 } 432 433 // add instructions entry wrapper 434 array_unshift($ins, array('plugin', array('include_wrap', array('open', $page, $flags['redirect'])))); 435 array_push($ins, array('plugin', array('include_wrap', array('close')))); 436 437 // close previous section if any and re-open after inclusion 438 if($lvl != 0 && $this->sec_close && !$flags['inline']) { 439 array_unshift($ins, array('section_close', array())); 440 $ins[] = array('section_open', array($lvl)); 441 } 442 } 443 444 /** 445 * Appends instruction item for the include plugin footer 446 * 447 * @author Michael Klier <chi@chimeric.de> 448 */ 449 function _footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id) { 450 $footer = array(); 451 $footer[0] = 'plugin'; 452 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $root_id, $footer_lvl)); 453 return $footer; 454 } 455 456 /** 457 * Appends instruction item for an edit button 458 * 459 * @author Michael Klier <chi@chimeric.de> 460 */ 461 function _editbtn(&$ins, $page, $sect, $sect_title, $root_id) { 462 $title = ($sect) ? $sect_title : $page; 463 $editbtn = array(); 464 $editbtn[0] = 'plugin'; 465 $editbtn[1] = array('include_editbtn', array($title)); 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 $check = array(); // used for sectionID() in order to get the same ids as the xhtml renderer 491 492 for($i=0; $i<$num; $i++) { 493 if ($ins[$i][0] == 'header') { 494 495 // found the right header 496 if (sectionID($ins[$i][1][0], $check) == $sect) { 497 $offset = $i; 498 $lvl = $ins[$i][1][1]; 499 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 500 $end = $i - $offset; 501 break; 502 } 503 } 504 } 505 $offset = $offset ? $offset : 0; 506 $end = $end ? $end : ($num - 1); 507 if(is_array($ins)) { 508 $ins = array_slice($ins, $offset, $end); 509 } 510 } 511 512 /** 513 * Only display the first section of a page and a readmore link 514 * 515 * @author Michael Klier <chi@chimeric.de> 516 */ 517 function _get_firstsec(&$ins, $page) { 518 $num = count($ins); 519 $first_sect = false; 520 for($i=0; $i<$num; $i++) { 521 if($ins[$i][0] == 'section_close') { 522 $first_sect = $i; 523 } 524 // only truncate the content and add the read more link when there is really 525 // more than that first section 526 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 527 $ins = array_slice($ins, 0, $first_sect); 528 $ins[] = array('plugin', array('include_readmore', array($page))); 529 $ins[] = array('section_close', array()); 530 return; 531 } 532 } 533 } 534 535 /** 536 * Gives a list of pages for a given include statement 537 * 538 * @author Michael Hamann <michael@content-space.de> 539 */ 540 function _get_included_pages($mode, $page, $sect, $parent_id) { 541 global $conf; 542 $pages = array(); 543 switch($mode) { 544 case 'namespace': 545 $ns = str_replace(':', '/', cleanID($page)); 546 search($pagearrays, $conf['datadir'], 'search_list', '', $ns); 547 if (is_array($pagearrays)) { 548 foreach ($pagearrays as $pagearray) { 549 $pages[] = $pagearray['id']; 550 } 551 } 552 break; 553 case 'tagtopic': 554 if (!$this->taghelper) 555 $this->taghelper =& plugin_load('helper', 'tag'); 556 if(!$this->taghelper) { 557 msg('You have to install the tag plugin to use this functionality!', -1); 558 return array(); 559 } 560 $tag = $page; 561 $sect = ''; 562 $pagearrays = $this->taghelper->getTopic('', null, $tag); 563 foreach ($pagearrays as $pagearray) { 564 $pages[] = $pagearray['id']; 565 } 566 break; 567 default: 568 $page = $this->_apply_macro($page); 569 resolve_pageid(getNS($parent_id), $page, $exists); // resolve shortcuts and clean ID 570 if (auth_quickaclcheck($page) >= AUTH_READ) 571 $pages[] = $page; 572 } 573 574 sort($pages); 575 576 $result = array(); 577 foreach ($pages as $page) { 578 $exists = page_exists($page); 579 $result[] = array('id' => $page, 'exists' => $exists, 'parent_id' => $parent_id); 580 } 581 return $result; 582 } 583 584 /** 585 * This function generates the list of all included pages from a list of metadata 586 * instructions. 587 */ 588 function _get_included_pages_from_meta_instructions($instructions) { 589 $pages = array(); 590 foreach ($instructions as $instruction) { 591 extract($instruction); 592 $pages = array_merge($pages, $this->_get_included_pages($mode, $page, $sect, $parent_id)); 593 } 594 return $pages; 595 } 596 597 /** 598 * Makes user or date dependent includes possible 599 */ 600 function _apply_macro($id) { 601 global $INFO; 602 global $auth; 603 604 // if we don't have an auth object, do nothing 605 if (!$auth) return $id; 606 607 $user = $_SERVER['REMOTE_USER']; 608 $group = $INFO['userinfo']['grps'][0]; 609 610 $time_stamp = time(); 611 if(preg_match('/@DATE(\w+)@/',$id,$matches)) { 612 switch($matches[1]) { 613 case 'PMONTH': 614 $time_stamp = strtotime("-1 month"); 615 break; 616 case 'NMONTH': 617 $time_stamp = strtotime("+1 month"); 618 break; 619 case 'NWEEK': 620 $time_stamp = strtotime("+1 week"); 621 break; 622 case 'PWEEK': 623 $time_stamp = strtotime("-1 week"); 624 break; 625 case 'TOMORROW': 626 $time_stamp = strtotime("+1 day"); 627 break; 628 case 'YESTERDAY': 629 $time_stamp = strtotime("-1 day"); 630 break; 631 case 'NYEAR': 632 $time_stamp = strtotime("+1 year"); 633 break; 634 case 'PYEAR': 635 $time_stamp = strtotime("-1 year"); 636 break; 637 } 638 $id = preg_replace('/@DATE(\w+)@/','', $id); 639 } 640 641 $replace = array( 642 '@USER@' => cleanID($user), 643 '@NAME@' => cleanID($INFO['userinfo']['name']), 644 '@GROUP@' => cleanID($group), 645 '@YEAR@' => date('Y',$time_stamp), 646 '@MONTH@' => date('m',$time_stamp), 647 '@WEEK@' => date('W',$time_stamp), 648 '@DAY@' => date('d',$time_stamp), 649 '@YEARPMONTH@' => date('Ym',strtotime("-1 month")), 650 '@PMONTH@' => date('m',strtotime("-1 month")), 651 '@NMONTH@' => date('m',strtotime("+1 month")), 652 '@YEARNMONTH@' => date('Ym',strtotime("+1 month")), 653 '@YEARPWEEK@' => date('YW',strtotime("-1 week")), 654 '@PWEEK@' => date('W',strtotime("-1 week")), 655 '@NWEEK@' => date('W',strtotime("+1 week")), 656 '@YEARNWEEK@' => date('YW',strtotime("+1 week")), 657 ); 658 return str_replace(array_keys($replace), array_values($replace), $id); 659 } 660} 661// vim:ts=4:sw=4:et: 662