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