1<?php 2/** 3 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 4 * @author Esther Brunner <wikidesign@gmail.com> 5 */ 6 7// must be run within Dokuwiki 8if (!defined('DOKU_INC')) die(); 9 10if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 11if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/'); 13 14class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin 15 16 var $pages = array(); // filechain of included pages 17 var $page = array(); // associative array with data about the page to include 18 var $ins = array(); // instructions array 19 var $doc = ''; // the final output XHTML string 20 var $mode = 'section'; // inclusion mode: 'page' or 'section' 21 var $clevel = 0; // current section level 22 var $firstsec = 0; // show first section only 23 var $editbtn = 1; // show edit button 24 var $footer = 1; // show metaline below page 25 var $noheader = 0; // omit header 26 var $header = array(); // included page / section header 27 var $renderer = NULL; // DokuWiki renderer object 28 29 // private variables 30 var $_offset = NULL; 31 32 /** 33 * Constructor loads some config settings 34 */ 35 function helper_plugin_include(){ 36 $this->firstsec = $this->getConf('firstseconly'); 37 $this->editbtn = $this->getConf('showeditbtn'); 38 $this->footer = $this->getConf('showfooter'); 39 $this->noheader = 0; 40 $this->header = array(); 41 } 42 43 function getInfo(){ 44 return array( 45 'author' => 'Gina Häussge, Michael Klier, Esther Brunner', 46 'email' => 'dokuwiki@freelists.org', 47 'date' => '2008-04-07', 48 'name' => 'Include Plugin (helper class)', 49 'desc' => 'Functions to include another page in a wiki page', 50 'url' => 'http://wiki.splitbrain.org/plugin:include', 51 ); 52 } 53 54 function getMethods(){ 55 $result = array(); 56 $result[] = array( 57 'name' => 'setPage', 58 'desc' => 'sets the page to include', 59 'params' => array("page attributes, 'id' required, 'section' for filtering" => 'array'), 60 'return' => array('success' => 'boolean'), 61 ); 62 $result[] = array( 63 'name' => 'setMode', 64 'desc' => 'sets inclusion mode: should indention be merged?', 65 'params' => array("'page' (original) or 'section' (merged indention)" => 'string'), 66 ); 67 $result[] = array( 68 'name' => 'setLevel', 69 'desc' => 'sets the indention for the current section level', 70 'params' => array('level: 0 to 5' => 'integer'), 71 'return' => array('success' => 'boolean'), 72 ); 73 $result[] = array( 74 'name' => 'setFlags', 75 'desc' => 'overrides standard values for showfooter and firstseconly settings', 76 'params' => array('flags' => 'array'), 77 ); 78 $result[] = array( 79 'name' => 'renderXHTML', 80 'desc' => 'renders the XHTML output of the included page', 81 'params' => array('DokuWiki renderer' => 'object'), 82 'return' => array('XHTML' => 'string'), 83 ); 84 return $result; 85 } 86 87 /** 88 * Sets the page to include if it is not already included (prevent recursion) 89 * and the current user is allowed to read it 90 */ 91 function setPage($page){ 92 global $ID; 93 94 $id = $page['id']; 95 $fullid = $id.'#'.$page['section']; 96 97 if (!$id) return false; // no page id given 98 if ($id == $ID) return false; // page can't include itself 99 100 // prevent include recursion 101 if ((isset($this->pages[$id.'#'])) || (isset($this->pages[$fullid]))) return false; 102 103 // we need to make sure 'perm', 'file' and 'exists' are set 104 if (!isset($page['perm'])) $page['perm'] = auth_quickaclcheck($page['id']); 105 if (!isset($page['file'])) $page['file'] = wikiFN($page['id']); 106 if (!isset($page['exists'])) $page['exists'] = @file_exists($page['file']); 107 108 // check permission 109 if ($page['perm'] < AUTH_READ) return false; 110 111 // add the page to the filechain 112 $this->pages[$fullid] = $page; 113 $this->page =& $this->pages[$fullid]; 114 return true; 115 } 116 117 /** 118 * Sets the inclusion mode: 'page' or 'section' 119 */ 120 function setMode($mode){ 121 $this->mode = $mode; 122 } 123 124 /** 125 * Sets the right indention for a given section level 126 */ 127 function setLevel($level){ 128 if ((is_numeric($level)) && ($level >= 0) && ($level <= 5)){ 129 $this->clevel = $level; 130 return true; 131 } 132 return false; 133 } 134 135 /** 136 * Overrides standard values for showfooter and firstseconly settings 137 */ 138 function setFlags($flags){ 139 foreach ($flags as $flag){ 140 switch ($flag){ 141 case 'footer': 142 $this->footer = 1; 143 break; 144 case 'nofooter': 145 $this->footer = 0; 146 break; 147 case 'firstseconly': 148 case 'firstsectiononly': 149 $this->firstsec = 1; 150 break; 151 case 'fullpage': 152 $this->firstsec = 0; 153 break; 154 case 'noheader': 155 $this->noheader = 1; 156 break; 157 case 'editbtn': 158 case 'editbutton': 159 $this->editbtn = 1; 160 break; 161 case 'noeditbtn': 162 case 'noeditbutton': 163 $this->editbtn = 0; 164 break; 165 } 166 } 167 } 168 169 /** 170 * Builds the XHTML to embed the page to include 171 */ 172 function renderXHTML(&$renderer){ 173 global $ID; 174 175 if (!$this->page['id']) return ''; // page must be set first 176 if (!$this->page['exists'] && ($this->page['perm'] < AUTH_CREATE)) return ''; 177 178 // prepare variables 179 $this->doc = ''; 180 $this->renderer =& $renderer; 181 182 // exchange page ID for included one 183 $backupID = $ID; // store the current ID 184 $ID = $this->page['id']; // change ID to the included page 185 186 // get instructions and render them on the fly 187 $this->ins = p_cached_instructions($this->page['file']); 188 189 // show only a given section? 190 if ($this->page['section'] && $this->page['exists']) $this->_getSection(); 191 192 // convert relative links 193 $this->_convertInstructions(); 194 195 // render the included page 196 $content = '<div class="entry-content">'.DOKU_LF. 197 $this->_cleanXHTML(p_render('xhtml', $this->ins, $info)).DOKU_LF. 198 '</div>'.DOKU_LF; 199 200 // restore ID 201 $ID = $backupID; 202 203 // embed the included page 204 $class = ($this->page['draft'] ? 'include draft' : 'include'); 205 $this->doc .= '<div class="'.$class.' hentry"'.$this->_showTagLogos().'>'.DOKU_LF; 206 if (!$this->header && $this->clevel && ($this->mode == 'section')) 207 $this->doc .= '<div class="level'.$this->clevel.'">'.DOKU_LF; 208 if ((@file_exists(DOKU_PLUGIN.'editsections/action.php')) 209 && (!plugin_isdisabled('editsections'))){ // for Edit Section Reorganizer Plugin 210 $this->doc .= $this->_editButton().$content; 211 } else { 212 $this->doc .= $content.$this->_editButton(); 213 } 214 215 // output meta line (if wanted) and remove page from filechain 216 $this->doc .= $this->_footer(array_pop($this->pages)); 217 218 if (!$this->header && $this->clevel && ($this->mode == 'section')) 219 $this->doc .= '</div>'.DOKU_LF; // class="level?" 220 $this->doc .= '</div>'.DOKU_LF; // class="include hentry" 221 222 // reset defaults 223 $this->helper_plugin_include(); 224 225 // return XHTML 226 $renderer->doc .= $this->doc; 227 return $this->doc; 228 } 229 230/* ---------- Private Methods ---------- */ 231 232 /** 233 * Get a section including its subsections 234 */ 235 function _getSection(){ 236 foreach ($this->ins as $ins){ 237 if ($ins[0] == 'header'){ 238 239 // found the right header 240 if (cleanID($ins[1][0]) == $this->page['section']){ 241 $level = $ins[1][1]; 242 $i[] = $ins; 243 244 // next header of the same or higher level -> exit 245 } elseif ($ins[1][1] <= $level){ 246 $this->ins = $i; 247 return true; 248 } elseif (isset($level)){ 249 $i[] = $ins; 250 } 251 252 // add instructions from our section 253 } elseif (isset($level)){ 254 $i[] = $ins; 255 } 256 } 257 $this->ins = $i; 258 return true; 259 } 260 261 /** 262 * Corrects relative internal links and media and 263 * converts headers of included pages to subheaders of the current page 264 */ 265 function _convertInstructions(){ 266 global $ID; 267 268 if (!$this->page['exists']) return false; 269 270 // check if included page is in same namespace 271 $ns = getNS($this->page['id']); 272 $convert = (getNS($ID) == $ns ? false : true); 273 274 $n = count($this->ins); 275 for ($i = 0; $i < $n; $i++){ 276 $current = $this->ins[$i][0]; 277 278 // convert internal links and media from relative to absolute 279 if ($convert && (substr($current, 0, 8) == 'internal')){ 280 $this->ins[$i][1][0] = $this->_convertInternalLink($this->ins[$i][1][0], $ns); 281 282 // set header level to current section level + header level 283 } elseif ($current == 'header'){ 284 $this->_convertHeader($i); 285 286 // the same for sections 287 } elseif (($current == 'section_open') && ($this->mode == 'section')){ 288 $this->ins[$i][1][0] = $this->_convertSectionLevel($this->ins[$i][1][0]); 289 290 // show only the first section? 291 } elseif ($this->firstsec && ($current == 'section_close') 292 && ($this->ins[$i-1][0] != 'section_open')){ 293 $this->_readMore($i); 294 return true; 295 } 296 } 297 $this->_finishConvert(); 298 return true; 299 } 300 301 /** 302 * Convert relative internal links and media 303 * 304 * @param integer $i: counter for current instruction 305 * @param string $ns: namespace of included page 306 * @return string $link: converted, now absolute link 307 */ 308 function _convertInternalLink($link, $ns){ 309 310 // relative subnamespace 311 if ($link{0} == '.'){ 312 if ($link{1} == '.') return getNS($ns).':'.substr($link, 2); // parent namespace 313 else return $ns.':'.substr($link, 1); // current namespace 314 315 // relative link 316 } elseif (strpos($link, ':') === false){ 317 return $ns.':'.$link; 318 319 // absolute link - don't change 320 } else { 321 return $link; 322 } 323 } 324 325 /** 326 * Convert header level and add header to TOC 327 * 328 * @param integer $i: counter for current instruction 329 * @return boolean true 330 */ 331 function _convertHeader($i){ 332 global $conf; 333 334 $text = $this->ins[$i][1][0]; 335 $hid = $this->renderer->_headerToLink($text, 'true'); 336 if (empty($this->header)){ 337 $this->_offset = $this->clevel - $this->ins[$i][1][1] + 1; 338 $level = $this->_convertSectionLevel(1); 339 $this->header = array('hid' => $hid, 'title' => hsc($text), 'level' => $level); 340 if ($this->noheader){ 341 unset($this->ins[$i]); 342 return true; 343 } 344 } else { 345 $level = $this->_convertSectionLevel($this->ins[$i][1][1]); 346 } 347 if ($this->mode == 'section') $this->ins[$i][1][1] = $level; 348 349 // add TOC item 350 if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])){ 351 $this->renderer->toc[] = array( 352 'hid' => $hid, 353 'title' => $text, 354 'type' => 'ul', 355 'level' => $level - $conf['toptoclevel'] + 1 356 ); 357 } 358 return true; 359 } 360 361 /** 362 * Convert the level of headers and sections 363 * 364 * @param integer $in: current level 365 * @return integer $out: converted level 366 */ 367 function _convertSectionLevel($in){ 368 $out = $in + $this->_offset; 369 if ($out >= 5) return 5; 370 if ($out <= $this->clevel + 1) return $this->clevel + 1; 371 return $out; 372 } 373 374 /** 375 * Adds a read more... link at the bottom of the first section 376 * 377 * @param integer $i: counter for current instruction 378 * @return boolean true 379 */ 380 function _readMore($i){ 381 $more = ((is_array($this->ins[$i+1])) && ($this->ins[$i+1][0] != 'document_end')); 382 383 if ($this->ins[0][0] == 'document_start') $this->ins = array_slice($this->ins, 1, $i); 384 else $this->ins = array_slice($this->ins, 0, $i); 385 386 if ($more){ 387 array_unshift($this->ins, array('document_start', array(), 0)); 388 $last = array_pop($this->ins); 389 $this->ins[] = array('p_open', array(), $last[2]); 390 $this->ins[] = array('internallink',array($this->page['id'], $this->getLang('readmore')),$last[2]); 391 $this->ins[] = array('p_close', array(), $last[2]); 392 $this->ins[] = $last; 393 $this->ins[] = array('document_end', array(), $last[2]); 394 } else { 395 $this->_finishConvert(); 396 } 397 return true; 398 } 399 400 /** 401 * Adds 'document_start' and 'document_end' instructions if not already there 402 */ 403 function _finishConvert(){ 404 if ($this->ins[0][0] != 'document_start') 405 array_unshift($this->ins, array('document_start', array(), 0)); 406 $c = count($this->ins) - 1; 407 if ($this->ins[$c][0] != 'document_end') 408 $this->ins[] = array('document_end', array(), 0); 409 } 410 411 /** 412 * Remove TOC, section edit buttons and tags 413 */ 414 function _cleanXHTML($xhtml){ 415 $replace = array( 416 '!<div class="toc">.*?(</div>\n</div>)!s' => '', // remove toc 417 '#<!-- SECTION "(.*?)" \[(\d+-\d*)\] -->#e' => '', // remove section edit buttons 418 '!<div class="tags">.*?(</div>)!s' => '', // remove category tags 419 ); 420 if ($this->clevel) 421 $replace['#<div class="footnotes">#s'] = '<div class="footnotes level'.$this->clevel.'">'; 422 $xhtml = preg_replace(array_keys($replace), array_values($replace), $xhtml); 423 return $xhtml; 424 } 425 426 /** 427 * Optionally display logo for the first tag found in the included page 428 */ 429 function _showTagLogos(){ 430 if ((!$this->getConf('showtaglogos')) 431 || (plugin_isdisabled('tag')) 432 || (!$taghelper =& plugin_load('helper', 'tag'))) 433 return ''; 434 435 $subject = p_get_metadata($this->page['id'], 'subject'); 436 if (is_array($subject)) $tag = $subject[0]; 437 else list($tag, $rest) = explode(' ', $subject, 2); 438 $title = str_replace('_', ' ', noNS($tag)); 439 resolve_pageid($taghelper->namespace, $tag, $exists); // resolve shortcuts 440 441 $logosrc = mediaFN($logoID); 442 $types = array('.png', '.jpg', '.gif'); // auto-detect filetype 443 foreach ($types as $type){ 444 if (!@file_exists($logosrc.$type)) continue; 445 $logoID = $tag.$type; 446 $logosrc .= $type; 447 list($w, $h, $t, $a) = getimagesize($logosrc); 448 return ' style="min-height: '.$h.'px">'. 449 '<img class="mediaright" src="'.ml($logoID).'" alt="'.$title.'"/'; 450 } 451 return ''; 452 } 453 454 /** 455 * Display an edit button for the included page 456 */ 457 function _editButton(){ 458 if ($this->page['exists']){ 459 if (($this->page['perm'] >= AUTH_EDIT) && (is_writable($this->page['file']))) 460 $action = 'edit'; 461 else return ''; 462 } elseif ($this->page['perm'] >= AUTH_CREATE){ 463 $action = 'create'; 464 } 465 if ($this->editbtn){ 466 return '<div class="secedit">'.DOKU_LF.DOKU_TAB. 467 html_btn($action, $this->page['id'], '', array('do' => 'edit'), 'post').DOKU_LF. 468 '</div>'.DOKU_LF; 469 } else { 470 return ''; 471 } 472 } 473 474 /** 475 * Returns the meta line below the included page 476 */ 477 function _footer($page){ 478 global $conf, $ID; 479 480 if (!$this->footer) return ''; // '<div class="inclmeta"> </div>'.DOKU_LF; 481 482 $id = $page['id']; 483 $meta = p_get_metadata($id); 484 $ret = array(); 485 486 // permalink 487 if ($this->getConf('showlink')){ 488 $title = ($page['title'] ? $page['title'] : $meta['title']); 489 if (!$title) $title = str_replace('_', ' ', noNS($id)); 490 $class = ($page['exists'] ? 'wikilink1' : 'wikilink2'); 491 $link = array( 492 'url' => wl($id), 493 'title' => $id, 494 'name' => hsc($title), 495 'target' => $conf['target']['wiki'], 496 'class' => $class.' permalink', 497 'more' => 'rel="bookmark"', 498 ); 499 $ret[] = $this->renderer->_formatLink($link); 500 } 501 502 // date 503 if ($this->getConf('showdate')){ 504 $date = ($page['date'] ? $page['date'] : $meta['date']['created']); 505 if ($date) 506 $ret[] = '<abbr class="published" title="'.strftime('%Y-%m-%dT%H:%M:%SZ', $date).'">'. 507 strftime($conf['dformat'], $date). 508 '</abbr>'; 509 } 510 511 // author 512 if ($this->getConf('showuser')){ 513 $author = ($page['user'] ? $page['user'] : $meta['creator']); 514 if ($author){ 515 $userpage = cleanID($this->getConf('usernamespace').':'.$author); 516 resolve_pageid(getNS($ID), $userpage, $exists); 517 $class = ($exists ? 'wikilink1' : 'wikilink2'); 518 $link = array( 519 'url' => wl($userpage), 520 'title' => $userpage, 521 'name' => hsc($author), 522 'target' => $conf['target']['wiki'], 523 'class' => $class.' url fn', 524 'pre' => '<span class="vcard author">', 525 'suf' => '</span>', 526 ); 527 $ret[] = $this->renderer->_formatLink($link); 528 } 529 } 530 531 // comments - let Discussion Plugin do the work for us 532 if (!$page['section'] && $this->getConf('showcomments') 533 && (!plugin_isdisabled('discussion')) 534 && ($discussion =& plugin_load('helper', 'discussion'))){ 535 $disc = $discussion->td($id); 536 if ($disc) $ret[] = '<span class="comment">'.$disc.'</span>'; 537 } 538 539 // linkbacks - let Linkback Plugin do the work for us 540 if (!$page['section'] && $this->getConf('showlinkbacks') 541 && (!plugin_isdisabled('linkback')) 542 && ($linkback =& plugin_load('helper', 'linkback'))){ 543 $link = $linkback->td($id); 544 if ($link) $ret[] = '<span class="linkback">'.$link.'</span>'; 545 } 546 547 $ret = implode(DOKU_LF.DOKU_TAB.'· ', $ret); 548 549 // tags - let Tag Plugin do the work for us 550 if (!$page['section'] && $this->getConf('showtags') 551 && (!plugin_isdisabled('tag')) 552 && ($tag =& plugin_load('helper', 'tag'))){ 553 $page['tags'] = '<div class="tags"><span>'.DOKU_LF. 554 DOKU_TAB.$tag->td($id).DOKU_LF. 555 DOKU_TAB.'</span></div>'.DOKU_LF; 556 $ret = $page['tags'].DOKU_TAB.$ret; 557 } 558 559 if (!$ret) $ret = ' '; 560 $class = 'inclmeta'; 561 if ($this->header && $this->clevel && ($this->mode == 'section')) 562 $class .= ' level'.$this->clevel; 563 return '<div class="'.$class.'">'.DOKU_LF.DOKU_TAB.$ret.DOKU_LF.'</div>'.DOKU_LF; 564 } 565 566 /** 567 * Builds the ODT to embed the page to include 568 */ 569 function renderODT(&$renderer){ 570 global $ID; 571 572 if (!$this->page['id']) return ''; // page must be set first 573 if (!$this->page['exists'] && ($this->page['perm'] < AUTH_CREATE)) return ''; 574 575 // prepare variable 576 $this->renderer =& $renderer; 577 578 // get instructions and render them on the fly 579 $this->ins = p_cached_instructions($this->page['file']); 580 581 // show only a given section? 582 if ($this->page['section'] && $this->page['exists']) $this->_getSection(); 583 584 // convert relative links 585 $this->_convertInstructions(); 586 587 // render the included page 588 $backupID = $ID; // store the current ID 589 $ID = $this->page['id']; // change ID to the included page 590 // remove document_start and document_end to avoid zipping 591 $this->ins = array_slice($this->ins, 1, -1); 592 p_render('odt', $this->ins, $info); 593 $ID = $backupID; // restore ID 594 // reset defaults 595 $this->helper_plugin_include(); 596 } 597} 598 599//Setup VIM: ex: et ts=4 enc=utf-8 : 600