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