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-01-12', 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 $renderer->doc .= '<div class="include hentry"'.$this->_showTagLogos().'>'.DOKU_LF; 175 if (!$this->header && $this->clevel && ($this->mode == 'section')) 176 $renderer->doc .= '<div class="level'.$this->clevel.'">'.DOKU_LF; 177 if ((@file_exists(DOKU_PLUGIN.'editsections/action.php')) 178 && (!plugin_isdisabled('editsections'))){ // for Edit Section Reorganizer Plugin 179 $renderer->doc .= $this->_editButton().$content; 180 } else { 181 $renderer->doc .= $content.$this->_editButton(); 182 } 183 184 if (!$this->header && $this->clevel && ($this->mode == 'section')) 185 $renderer->doc .= '</div>'.DOKU_LF; // class="level?" 186 $renderer->doc .= '</div>'.DOKU_LF; // class="include hentry" 187 188 // output meta line (if wanted) and remove page from filechain 189 $renderer->doc .= $this->_footer(array_pop($this->pages)); 190 $this->helper_plugin_include(); 191 192 return $this->doc; 193 } 194 195/* ---------- Private Methods ---------- */ 196 197 /** 198 * Get a section including its subsections 199 */ 200 function _getSection(){ 201 foreach ($this->ins as $ins){ 202 if ($ins[0] == 'header'){ 203 204 // found the right header 205 if (cleanID($ins[1][0]) == $this->page['section']){ 206 $level = $ins[1][1]; 207 $i[] = $ins; 208 209 // next header of the same or higher level -> exit 210 } elseif ($ins[1][1] <= $level){ 211 $this->ins = $i; 212 return true; 213 } elseif (isset($level)){ 214 $i[] = $ins; 215 } 216 217 // add instructions from our section 218 } elseif (isset($level)){ 219 $i[] = $ins; 220 } 221 } 222 $this->ins = $i; 223 return true; 224 } 225 226 /** 227 * Corrects relative internal links and media and 228 * converts headers of included pages to subheaders of the current page 229 */ 230 function _convertInstructions(){ 231 global $ID; 232 global $conf; 233 234 $this->header = array(); 235 $offset = $this->clevel; 236 237 if (!$this->page['exists']) return false; 238 239 // check if included page is in same namespace 240 $inclNS = getNS($this->page['id']); 241 if (getNS($ID) == $inclNS) $convert = false; 242 else $convert = true; 243 244 $n = count($this->ins); 245 for ($i = 0; $i < $n; $i++){ 246 247 // convert internal links and media from relative to absolute 248 if ($convert && (substr($this->ins[$i][0], 0, 8) == 'internal')){ 249 250 // relative subnamespace 251 if ($this->ins[$i][1][0]{0} == '.'){ 252 // parent namespace 253 if ($this->ins[$i][1][0]{1} == '.') 254 $ithis->ns[$i][1][0] = getNS($inclNS).':'.substr($this->ins[$i][1][0], 2); 255 // current namespace 256 else 257 $this->ins[$i][1][0] = $inclNS.':'.substr($this->ins[$i][1][0], 1); 258 259 // relative link 260 } elseif (strpos($this->ins[$i][1][0], ':') === false){ 261 $this->ins[$i][1][0] = $inclNS.':'.$this->ins[$i][1][0]; 262 } 263 264 // set header level to current section level + header level 265 } elseif ($this->ins[$i][0] == 'header'){ 266 if (empty($this->header)){ 267 $offset = $this->clevel - $this->ins[$i][1][1] + 1; 268 $text = $this->ins[$i][1][0]; 269 $hid = $this->renderer->_headerToLink($text, 'true'); 270 $level = $this->clevel + 1; 271 $this->header = array( 272 'hid' => $hid, 273 'title' => hsc($text), 274 'level' => $level 275 ); 276 unset($this->ins[$i]); 277 } else { 278 $level = $this->ins[$i][1][1] + $offset; 279 if ($level > 5) $level = 5; 280 $this->ins[$i][1][1] = $level; 281 } 282 283 // add TOC items 284 if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])){ 285 $this->renderer->toc[] = array( 286 'hid' => $hid, 287 'title' => $text, 288 'type' => 'ul', 289 'level' => $level - $conf['toptoclevel'] + 1 290 ); 291 } 292 293 // the same for sections 294 } elseif ($this->ins[$i][0] == 'section_open'){ 295 $level = $this->ins[$i][1][0] + $offset; 296 if ($level > 5) $level = 5; 297 $this->ins[$i][1][0] = $level; 298 299 // show only the first section? 300 } elseif ($this->firstsec && ($this->ins[$i][0] == 'section_close') 301 && ($this->ins[$i-1][0] != 'section_open')){ 302 if ($this->ins[0][0] == 'document_start'){ 303 $this->ins = array_slice($this->ins, 1, $i); 304 return true; 305 } else { 306 $this->ins = array_slice($this->ins, 0, $i); 307 return true; 308 } 309 } 310 } 311 if ($this->ins[0][0] != 'document_start'){ 312 array_unshift($this->ins, array('document_start', array(), 0)); 313 $this->ins[] = array('document_end', array(), 0); 314 } 315 return true; 316 } 317 318 /** 319 * Remove TOC, section edit buttons and tags 320 */ 321 function _cleanXHTML($xhtml){ 322 preg_match('!<div class="tags">.*?</div>!s', $xhtml, $match); 323 $this->page['tags'] = $match[0]; 324 $replace = array( 325 '!<div class="toc">.*?(</div>\n</div>)!s' => '', // remove toc 326 '#<!-- SECTION "(.*?)" \[(\d+-\d*)\] -->#e' => '', // remove section edit buttons 327 '!<div class="tags">.*?(</div>)!s' => '', // remove category tags 328 ); 329 $xhtml = preg_replace(array_keys($replace), array_values($replace), $xhtml); 330 return $xhtml; 331 } 332 333 /** 334 * Optionally display logo for the first tag found in the included page 335 */ 336 function _showTagLogos(){ 337 if (!$this->getConf('showtaglogos')) return ''; 338 339 preg_match_all('/<a [^>]*title="(.*?)" rel="tag"[^>]*>([^<]*)</', $this->page['tags'], $tag); 340 $logoID = getNS($tag[1][0]).':'.$tag[2][0]; 341 $logosrc = mediaFN($logoID); 342 $types = array('.png', '.jpg', '.gif'); // auto-detect filetype 343 foreach ($types as $type){ 344 if (!@file_exists($logosrc.$type)) continue; 345 $logoID .= $type; 346 $logosrc .= $type; 347 list($w, $h, $t, $a) = getimagesize($logosrc); 348 return ' style="min-height: '.$h.'px">'. 349 '<img class="mediaright" src="'.ml($logoID).'" alt="'.$tag[2][0].'"/'; 350 } 351 return ''; 352 } 353 354 /** 355 * Display an edit button for the included page 356 */ 357 function _editButton(){ 358 if (!isset($this->page['perm'])) 359 $this->page['perm'] = auth_quickaclcheck($this->page['id']); 360 if (@file_exists($this->page['file'])){ 361 if (($this->page['perm'] >= AUTH_EDIT) && (is_writable($this->page['file']))) 362 $action = 'edit'; 363 else return ''; 364 } elseif ($this->page['perm'] >= AUTH_CREATE){ 365 $action = 'create'; 366 } 367 return '<div class="secedit">'.DOKU_LF.DOKU_TAB. 368 html_btn($action, $this->page['id'], '', array('do' => 'edit'), 'post').DOKU_LF. 369 '</div>'.DOKU_LF; 370 } 371 372 /** 373 * Adds a read more... link at the bottom of the first section 374 */ 375 function _readMore(){ 376 $last = $this->ins[count($this->ins) - 1]; 377 if ($last[0] == 'section_close') $this->ins = array_slice($this->ins, 0, -1); 378 $this->ins[] = array('p_open', array(), $last[2]); 379 $this->ins[] = array('internallink', array($this->page['id'], $this->getLang('readmore')), $last[2]); 380 $this->ins[] = array('p_close', array(), $last[2]); 381 if ($last[0] == 'section_close') $this->ins[] = $last; 382 } 383 384 /** 385 * Returns the meta line below the included page 386 */ 387 function _footer($page){ 388 global $conf; 389 390 if (!$this->footer) return ''; // '<div class="inclmeta"> </div>'.DOKU_LF; 391 392 $id = $page['id']; 393 $meta = p_get_metadata($id); 394 $ret = array(); 395 396 // permalink 397 if ($this->getConf('showlink')){ 398 $title = ($page['title'] ? $page['title'] : $meta['title']); 399 if (!$title) $title = str_replace('_', ' ', noNS($id)); 400 $class = ($page['exists'] ? 'wikilink1' : 'wikilink2'); 401 $link = array( 402 'url' => wl($id), 403 'title' => $id, 404 'name' => hsc($title), 405 'target' => $conf['target']['wiki'], 406 'class' => $class.' permalink', 407 'more' => 'rel="bookmark"', 408 ); 409 $ret[] = $this->renderer->_formatLink($link); 410 } 411 412 // date 413 if ($this->getConf('showdate')){ 414 $date = ($page['date'] ? $page['date'] : $meta['date']['created']); 415 if ($date) 416 $ret[] = '<abbr class="published" title="'.gmdate('Y-m-d\TH:i:s\Z', $date).'">'. 417 date($conf['dformat'], $date). 418 '</abbr>'; 419 } 420 421 // author 422 if ($this->getConf('showuser')){ 423 $author = ($page['user'] ? $page['user'] : $meta['creator']); 424 if ($author){ 425 $userpage = cleanID($this->getConf('usernamespace').':'.$author); 426 resolve_pageid(getNS($ID), $id, $exists); 427 $class = ($exists ? 'wikilink1' : 'wikilink2'); 428 $link = array( 429 'url' => wl($userpage), 430 'title' => $userpage, 431 'name' => hsc($author), 432 'target' => $conf['target']['wiki'], 433 'class' => $class.' url fn', 434 'pre' => '<span class="vcard author">', 435 'suf' => '</span>', 436 ); 437 $ret[] = $this->renderer->_formatLink($link); 438 } 439 } 440 441 // comments - let Discussion Plugin do the work for us 442 if (!$page['section'] && $this->getConf('showcomments') 443 && (!plugin_isdisabled('discussion')) 444 && ($discussion =& plugin_load('helper', 'discussion'))){ 445 $disc = $discussion->td($id); 446 if ($disc) $ret[] = '<span class="comment">'.$disc.'</span>'; 447 } 448 449 $ret = implode(' · ', $ret); 450 451 // tags 452 if (($this->getConf('showtags')) && ($page['tags'])){ 453 $ret = $this->page['tags'].$ret; 454 } 455 456 if (!$ret) $ret = ' '; 457 return '<div class="inclmeta">'.DOKU_LF.$ret.DOKU_LF.'</div>'.DOKU_LF; 458 } 459 460} 461 462//Setup VIM: ex: et ts=4 enc=utf-8 : 463