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