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