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->renderer =& $renderer; 180 181 // exchange page ID for included one 182 $backupID = $ID; // store the current ID 183 $ID = $this->page['id']; // change ID to the included page 184 185 // get instructions and render them on the fly 186 $this->ins = p_cached_instructions($this->page['file']); 187 188 // show only a given section? 189 if ($this->page['section'] && $this->page['exists']) $this->_getSection(); 190 191 // convert relative links 192 $this->_convertInstructions(); 193 194 // render the included page 195 $content = '<div class="entry-content">'.DOKU_LF. 196 $this->_cleanXHTML(p_render('xhtml', $this->ins, $info)).DOKU_LF. 197 '</div>'.DOKU_LF; 198 199 // restore ID 200 $ID = $backupID; 201 202 // embed the included page 203 $class = ($this->page['draft'] ? 'include draft' : 'include'); 204 $this->doc = '<div class="'.$class.' hentry"'.$this->_showTagLogos().'>'.DOKU_LF; 205 if (!$this->header && $this->clevel && ($this->mode == 'section')) 206 $this->doc .= '<div class="level'.$this->clevel.'">'.DOKU_LF; 207 if ((@file_exists(DOKU_PLUGIN.'editsections/action.php')) 208 && (!plugin_isdisabled('editsections'))){ // for Edit Section Reorganizer Plugin 209 $this->doc .= $this->_editButton().$content; 210 } else { 211 $this->doc .= $content.$this->_editButton(); 212 } 213 214 // output meta line (if wanted) and remove page from filechain 215 $this->doc .= $this->_footer(array_pop($this->pages)); 216 217 if (!$this->header && $this->clevel && ($this->mode == 'section')) 218 $this->doc .= '</div>'.DOKU_LF; // class="level?" 219 $this->doc .= '</div>'.DOKU_LF; // class="include hentry" 220 221 // reset defaults 222 $this->helper_plugin_include(); 223 224 // return XHTML 225 $renderer->doc .= $this->doc; 226 return $this->doc; 227 } 228 229/* ---------- Private Methods ---------- */ 230 231 /** 232 * Get a section including its subsections 233 */ 234 function _getSection(){ 235 foreach ($this->ins as $ins){ 236 if ($ins[0] == 'header'){ 237 238 // found the right header 239 if (cleanID($ins[1][0]) == $this->page['section']){ 240 $level = $ins[1][1]; 241 $i[] = $ins; 242 243 // next header of the same or higher level -> exit 244 } elseif ($ins[1][1] <= $level){ 245 $this->ins = $i; 246 return true; 247 } elseif (isset($level)){ 248 $i[] = $ins; 249 } 250 251 // add instructions from our section 252 } elseif (isset($level)){ 253 $i[] = $ins; 254 } 255 } 256 $this->ins = $i; 257 return true; 258 } 259 260 /** 261 * Corrects relative internal links and media and 262 * converts headers of included pages to subheaders of the current page 263 */ 264 function _convertInstructions(){ 265 global $ID; 266 267 if (!$this->page['exists']) return false; 268 269 // check if included page is in same namespace 270 $ns = getNS($this->page['id']); 271 $convert = (getNS($ID) == $ns ? false : true); 272 273 $n = count($this->ins); 274 for ($i = 0; $i < $n; $i++){ 275 $current = $this->ins[$i][0]; 276 277 // convert internal links and media from relative to absolute 278 if ($convert && (substr($current, 0, 8) == 'internal')){ 279 $this->ins[$i][1][0] = $this->_convertInternalLink($this->ins[$i][1][0], $ns); 280 281 // set header level to current section level + header level 282 } elseif ($current == 'header'){ 283 $this->_convertHeader($i); 284 285 // the same for sections 286 } elseif (($current == 'section_open') && ($this->mode == 'section')){ 287 $this->ins[$i][1][0] = $this->_convertSectionLevel($this->ins[$i][1][0]); 288 289 // show only the first section? 290 } elseif ($this->firstsec && ($current == 'section_close') 291 && ($this->ins[$i-1][0] != 'section_open')){ 292 $this->_readMore($i); 293 return true; 294 } 295 } 296 $this->_finishConvert(); 297 return true; 298 } 299 300 /** 301 * Convert relative internal links and media 302 * 303 * @param integer $i: counter for current instruction 304 * @param string $ns: namespace of included page 305 * @return string $link: converted, now absolute link 306 */ 307 function _convertInternalLink($link, $ns){ 308 309 // relative subnamespace 310 if ($link{0} == '.'){ 311 if ($link{1} == '.') return getNS($ns).':'.substr($link, 2); // parent namespace 312 else return $ns.':'.substr($link, 1); // current namespace 313 314 // relative link 315 } elseif (strpos($link, ':') === false){ 316 return $ns.':'.$link; 317 318 // absolute link - don't change 319 } else { 320 return $link; 321 } 322 } 323 324 /** 325 * Convert header level and add header to TOC 326 * 327 * @param integer $i: counter for current instruction 328 * @return boolean true 329 */ 330 function _convertHeader($i){ 331 global $conf; 332 333 $text = $this->ins[$i][1][0]; 334 $hid = $this->renderer->_headerToLink($text, 'true'); 335 if (empty($this->header)){ 336 $this->_offset = $this->clevel - $this->ins[$i][1][1] + 1; 337 $level = $this->_convertSectionLevel(1); 338 $this->header = array('hid' => $hid, 'title' => hsc($text), 'level' => $level); 339 if ($this->noheader){ 340 unset($this->ins[$i]); 341 return true; 342 } 343 } else { 344 $level = $this->_convertSectionLevel($this->ins[$i][1][1]); 345 } 346 if ($this->mode == 'section') $this->ins[$i][1][1] = $level; 347 348 // add TOC item 349 if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])){ 350 $this->renderer->toc[] = array( 351 'hid' => $hid, 352 'title' => $text, 353 'type' => 'ul', 354 'level' => $level - $conf['toptoclevel'] + 1 355 ); 356 } 357 return true; 358 } 359 360 /** 361 * Convert the level of headers and sections 362 * 363 * @param integer $in: current level 364 * @return integer $out: converted level 365 */ 366 function _convertSectionLevel($in){ 367 $out = $in + $this->_offset; 368 if ($out >= 5) return 5; 369 if ($out <= $this->clevel + 1) return $this->clevel + 1; 370 return $out; 371 } 372 373 /** 374 * Adds a read more... link at the bottom of the first section 375 * 376 * @param integer $i: counter for current instruction 377 * @return boolean true 378 */ 379 function _readMore($i){ 380 $more = ((is_array($this->ins[$i+1])) && ($this->ins[$i+1][0] != 'document_end')); 381 382 if ($this->ins[0][0] == 'document_start') $this->ins = array_slice($this->ins, 1, $i); 383 else $this->ins = array_slice($this->ins, 0, $i); 384 385 if ($more){ 386 array_unshift($this->ins, array('document_start', array(), 0)); 387 $last = array_pop($this->ins); 388 $this->ins[] = array('p_open', array(), $last[2]); 389 $this->ins[] = array('internallink',array($this->page['id'], $this->getLang('readmore')),$last[2]); 390 $this->ins[] = array('p_close', array(), $last[2]); 391 $this->ins[] = $last; 392 $this->ins[] = array('document_end', array(), $last[2]); 393 } else { 394 $this->_finishConvert(); 395 } 396 return true; 397 } 398 399 /** 400 * Adds 'document_start' and 'document_end' instructions if not already there 401 */ 402 function _finishConvert(){ 403 if ($this->ins[0][0] != 'document_start') 404 array_unshift($this->ins, array('document_start', array(), 0)); 405 $c = count($this->ins) - 1; 406 if ($this->ins[$c][0] != 'document_end') 407 $this->ins[] = array('document_end', array(), 0); 408 } 409 410 /** 411 * Remove TOC, section edit buttons and tags 412 */ 413 function _cleanXHTML($xhtml){ 414 $replace = array( 415 '!<div class="toc">.*?(</div>\n</div>)!s' => '', // remove toc 416 '#<!-- SECTION "(.*?)" \[(\d+-\d*)\] -->#e' => '', // remove section edit buttons 417 '!<div class="tags">.*?(</div>)!s' => '', // remove category tags 418 ); 419 if ($this->clevel) 420 $replace['#<div class="footnotes">#s'] = '<div class="footnotes level'.$this->clevel.'">'; 421 $xhtml = preg_replace(array_keys($replace), array_values($replace), $xhtml); 422 return $xhtml; 423 } 424 425 /** 426 * Optionally display logo for the first tag found in the included page 427 */ 428 function _showTagLogos(){ 429 if ((!$this->getConf('showtaglogos')) 430 || (plugin_isdisabled('tag')) 431 || (!$taghelper =& plugin_load('helper', 'tag'))) 432 return ''; 433 434 $subject = p_get_metadata($this->page['id'], 'subject'); 435 if (is_array($subject)) $tag = $subject[0]; 436 else list($tag, $rest) = explode(' ', $subject, 2); 437 $title = str_replace('_', ' ', noNS($tag)); 438 resolve_pageid($taghelper->namespace, $tag, $exists); // resolve shortcuts 439 440 $logosrc = mediaFN($logoID); 441 $types = array('.png', '.jpg', '.gif'); // auto-detect filetype 442 foreach ($types as $type){ 443 if (!@file_exists($logosrc.$type)) continue; 444 $logoID = $tag.$type; 445 $logosrc .= $type; 446 list($w, $h, $t, $a) = getimagesize($logosrc); 447 return ' style="min-height: '.$h.'px">'. 448 '<img class="mediaright" src="'.ml($logoID).'" alt="'.$title.'"/'; 449 } 450 return ''; 451 } 452 453 /** 454 * Display an edit button for the included page 455 */ 456 function _editButton(){ 457 if ($this->page['exists']){ 458 if (($this->page['perm'] >= AUTH_EDIT) && (is_writable($this->page['file']))) 459 $action = 'edit'; 460 else return ''; 461 } elseif ($this->page['perm'] >= AUTH_CREATE){ 462 $action = 'create'; 463 } 464 if ($this->editbtn){ 465 return '<div class="secedit">'.DOKU_LF.DOKU_TAB. 466 html_btn($action, $this->page['id'], '', array('do' => 'edit'), 'post').DOKU_LF. 467 '</div>'.DOKU_LF; 468 } else { 469 return ''; 470 } 471 } 472 473 /** 474 * Returns the meta line below the included page 475 */ 476 function _footer($page){ 477 global $conf, $ID; 478 479 if (!$this->footer) return ''; // '<div class="inclmeta"> </div>'.DOKU_LF; 480 481 $id = $page['id']; 482 $meta = p_get_metadata($id); 483 $ret = array(); 484 485 // permalink 486 if ($this->getConf('showlink')){ 487 $title = ($page['title'] ? $page['title'] : $meta['title']); 488 if (!$title) $title = str_replace('_', ' ', noNS($id)); 489 $class = ($page['exists'] ? 'wikilink1' : 'wikilink2'); 490 $link = array( 491 'url' => wl($id), 492 'title' => $id, 493 'name' => hsc($title), 494 'target' => $conf['target']['wiki'], 495 'class' => $class.' permalink', 496 'more' => 'rel="bookmark"', 497 ); 498 $ret[] = $this->renderer->_formatLink($link); 499 } 500 501 // date 502 if ($this->getConf('showdate')){ 503 $date = ($page['date'] ? $page['date'] : $meta['date']['created']); 504 if ($date) 505 $ret[] = '<abbr class="published" title="'.strftime('%Y-%m-%dT%H:%M:%SZ', $date).'">'. 506 strftime($conf['dformat'], $date). 507 '</abbr>'; 508 } 509 510 // author 511 if ($this->getConf('showuser')){ 512 $author = ($page['user'] ? $page['user'] : $meta['creator']); 513 if ($author){ 514 $userpage = cleanID($this->getConf('usernamespace').':'.$author); 515 resolve_pageid(getNS($ID), $userpage, $exists); 516 $class = ($exists ? 'wikilink1' : 'wikilink2'); 517 $link = array( 518 'url' => wl($userpage), 519 'title' => $userpage, 520 'name' => hsc($author), 521 'target' => $conf['target']['wiki'], 522 'class' => $class.' url fn', 523 'pre' => '<span class="vcard author">', 524 'suf' => '</span>', 525 ); 526 $ret[] = $this->renderer->_formatLink($link); 527 } 528 } 529 530 // comments - let Discussion Plugin do the work for us 531 if (!$page['section'] && $this->getConf('showcomments') 532 && (!plugin_isdisabled('discussion')) 533 && ($discussion =& plugin_load('helper', 'discussion'))){ 534 $disc = $discussion->td($id); 535 if ($disc) $ret[] = '<span class="comment">'.$disc.'</span>'; 536 } 537 538 // linkbacks - let Linkback Plugin do the work for us 539 if (!$page['section'] && $this->getConf('showlinkbacks') 540 && (!plugin_isdisabled('linkback')) 541 && ($linkback =& plugin_load('helper', 'linkback'))){ 542 $link = $linkback->td($id); 543 if ($link) $ret[] = '<span class="linkback">'.$link.'</span>'; 544 } 545 546 $ret = implode(DOKU_LF.DOKU_TAB.'· ', $ret); 547 548 // tags - let Tag Plugin do the work for us 549 if (!$page['section'] && $this->getConf('showtags') 550 && (!plugin_isdisabled('tag')) 551 && ($tag =& plugin_load('helper', 'tag'))){ 552 $page['tags'] = '<div class="tags"><span>'.DOKU_LF. 553 DOKU_TAB.$tag->td($id).DOKU_LF. 554 DOKU_TAB.'</span></div>'.DOKU_LF; 555 $ret = $page['tags'].DOKU_TAB.$ret; 556 } 557 558 if (!$ret) $ret = ' '; 559 $class = 'inclmeta'; 560 if ($this->header && $this->clevel && ($this->mode == 'section')) 561 $class .= ' level'.$this->clevel; 562 return '<div class="'.$class.'">'.DOKU_LF.DOKU_TAB.$ret.DOKU_LF.'</div>'.DOKU_LF; 563 } 564 565 /** 566 * Builds the ODT to embed the page to include 567 */ 568 function renderODT(&$renderer){ 569 global $ID; 570 571 if (!$this->page['id']) return ''; // page must be set first 572 if (!$this->page['exists'] && ($this->page['perm'] < AUTH_CREATE)) return ''; 573 574 // prepare variable 575 $this->renderer =& $renderer; 576 577 // get instructions and render them on the fly 578 $this->ins = p_cached_instructions($this->page['file']); 579 580 // show only a given section? 581 if ($this->page['section'] && $this->page['exists']) $this->_getSection(); 582 583 // convert relative links 584 $this->_convertInstructions(); 585 586 // render the included page 587 $backupID = $ID; // store the current ID 588 $ID = $this->page['id']; // change ID to the included page 589 // remove document_start and document_end to avoid zipping 590 $this->ins = array_slice($this->ins, 1, -1); 591 p_render('odt', $this->ins, $info); 592 $ID = $backupID; // restore ID 593 // reset defaults 594 $this->helper_plugin_include(); 595 } 596} 597 598//Setup VIM: ex: et ts=4 enc=utf-8 : 599