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