1<?php 2/** 3 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 4 * @author Esther Brunner <wikidesign@gmail.com> 5 * @author Christopher Smith <chris@jalakai.co.uk> 6 * @author Gina Häußge, Michael Klier <dokuwiki@chimeric.de> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) die(); 11 12if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/'); 15 16class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin 17 18 var $includes = array(); 19 var $toplevel_id = NULL; 20 var $toplevel = 0; 21 var $defaults = array(); 22 23 /** 24 * Constructor loads default config settings once 25 */ 26 function helper_plugin_include() { 27 $this->defaults['firstsec'] = $this->getConf('firstseconly'); 28 $this->defaults['editbtn'] = $this->getConf('showeditbtn'); 29 $this->defaults['taglogos'] = $this->getConf('showtaglogos'); 30 $this->defaults['footer'] = $this->getConf('showfooter'); 31 $this->defaults['redirect'] = $this->getConf('doredirect'); 32 $this->defaults['date'] = $this->getConf('showdate'); 33 $this->defaults['user'] = $this->getConf('showuser'); 34 $this->defaults['comments'] = $this->getConf('showcomments'); 35 $this->defaults['linkbacks'] = $this->getConf('showlinkbacks'); 36 $this->defaults['tags'] = $this->getConf('showtags'); 37 $this->defaults['link'] = $this->getConf('showlink'); 38 $this->defaults['permalink'] = $this->getConf('showpermalink'); 39 } 40 41 function getInfo() { 42 return array( 43 'author' => 'Gina Häußge, Michael Klier, Esther Brunner', 44 'email' => 'dokuwiki@chimeric.de', 45 'date' => @file_get_contents(DOKU_PLUGIN . 'blog/VERSION'), 46 'name' => 'Include Plugin (helper class)', 47 'desc' => 'Functions to include another page in a wiki page', 48 'url' => 'http://dokuwiki.org/plugin:include', 49 ); 50 } 51 52 /** 53 * Available methods for other plugins 54 */ 55 function getMethods() { 56 $result = array(); 57 $result[] = array( 58 'name' => 'get_flags', 59 'desc' => 'overrides standard values for showfooter and firstseconly settings', 60 'params' => array('flags' => 'array'), 61 ); 62 return $result; 63 } 64 65 /** 66 * Overrides standard values for showfooter and firstseconly settings 67 */ 68 function get_flags($setflags) { 69 // load defaults 70 $flags = array(); 71 $flags = $this->defaults; 72 foreach ($setflags as $flag) { 73 switch ($flag) { 74 case 'footer': 75 $flags['footer'] = 1; 76 break; 77 case 'nofooter': 78 $flags['footer'] = 0; 79 break; 80 case 'firstseconly': 81 case 'firstsectiononly': 82 $flags['firstsec'] = 1; 83 break; 84 case 'fullpage': 85 $flags['firstsec'] = 0; 86 break; 87 case 'noheader': 88 $flags['noheader'] = 1; 89 break; 90 case 'editbtn': 91 case 'editbutton': 92 $flags['editbtn'] = 1; 93 break; 94 case 'noeditbtn': 95 case 'noeditbutton': 96 $flags['editbtn'] = 0; 97 break; 98 case 'permalink': 99 $flags['permalink'] = 1; 100 break; 101 case 'nopermalink': 102 $flags['permalink'] = 0; 103 break; 104 case 'redirect': 105 $flags['redirect'] = 1; 106 break; 107 case 'noredirect': 108 $flags['redirect'] = 0; 109 break; 110 case 'link': 111 $flags['link'] = 1; 112 break; 113 } 114 } 115 return $flags; 116 } 117 118 /** 119 * Parses the instructions list of the page which contains the includes 120 * 121 * @author Michael Klier <chi@chimeric.de> 122 */ 123 function parse_instructions($id, &$ins) { 124 $num = count($ins); 125 126 $lvl = false; 127 $prev_lvl = false; 128 $mode = ''; 129 $page = ''; 130 $flags = array(); 131 $range = false; 132 $scope = $id; 133 134 for($i=0; $i<$num; $i++) { 135 // set current level 136 if($ins[$i][0] == 'section_open') { 137 $lvl = $ins[$i][1][0]; 138 if($i > $range) $prev_lvl = $lvl; 139 } 140 141 if($ins[$i][0] == 'plugin' && $ins[$i][1][0] == 'include_include' ) { 142 // found no previous section set lvl to 0 143 if(!$lvl) $lvl = 0; 144 145 $mode = $ins[$i][1][1][0]; 146 $page = $ins[$i][1][1][1]; 147 $sect = $ins[$i][1][1][2]; 148 $flags = $ins[$i][1][1][3]; 149 150 $page = $this->_apply_macro($page); 151 resolve_pageid(getNS($scope), $page, $exists); // resolve shortcuts 152 $ins[$i][1][1][4] = $scope; 153 $scope = $page; 154 155 $flags = $this->get_flags($flags); 156 157 $ins_inc = $this->_get_instructions($page, $sect, $mode, $lvl, $flags); 158 159 if(!empty($ins_inc)) { 160 // combine instructions and reset counter 161 $ins_start = array_slice($ins, 0, $i+1); 162 $ins_end = array_slice($ins, $i+1); 163 $range = $i + count($ins_inc); 164 $ins = array_merge($ins_start, $ins_inc, $ins_end); 165 $num = count($ins); 166 } 167 } 168 169 // check if we left the range of possible sub includes and reset lvl and scope to toplevel_id 170 if($range && ($i > $range)) { 171 $lvl = ($prev_lvl == 0) ? 1 : $prev_lvl; 172 $prev_lvl = false; 173 $range = false; 174 // reset scope to toplevel_id 175 $scope = $this->toplevel_id; 176 } 177 } 178 } 179 180 /** 181 * Returns the converted instructions of a give page/section 182 * 183 * @author Michael Klier <chi@chimeric.de> 184 */ 185 function _get_instructions($page, $sect, $mode, $lvl, $flags) { 186 global $ID; 187 188 if(($ID == $page) || (auth_quickaclcheck($page) < AUTH_READ) || (!page_exists($page)) && (auth_quickaclcheck($page) < AUTH_CREATE)) return array(); 189 $key = ($sect) ? $page . '#' . $sect : $page; 190 191 // prevent recursion 192 if(!$this->includes[$key]) { 193 $ins = p_cached_instructions(wikiFN($page)); 194 $this->includes[$key] = true; 195 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags); 196 return $ins; 197 } 198 } 199 200 /** 201 * Converts instructions of the included page 202 * 203 * The funcion iterates over the given list of instructions and generates 204 * an index of header and section indicies. It also removes document 205 * start/end instructions, converts links, and removes unwanted 206 * instructions like tags, comments, linkbacks. 207 * 208 * Later all header/section levels are convertet to match the current 209 * inclusion level. 210 * 211 * @author Michael Klier <chi@chimeric.de> 212 */ 213 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags) { 214 215 // filter instructions if needed 216 if(!empty($sect)) { 217 $this->_get_section($ins, $sect); // section required 218 } 219 220 if($flags['firstsec']) { 221 $this->_get_firstsec($ins, $page); // only first section 222 } 223 224 $ns = getNS($page); 225 $num = count($ins); 226 227 $conv_idx = array(); // conversion index 228 $lvl_max = false; // max level 229 $first_header = -1; 230 $no_header = false; 231 $sect_title = false; 232 233 for($i=0; $i<$num; $i++) { 234 switch($ins[$i][0]) { 235 case 'document_start': 236 case 'document_end': 237 case 'section_edit': 238 unset($ins[$i]); 239 break; 240 case 'header': 241 // get section title of first section 242 if($sect && !$sect_title) { 243 $sect_title = $ins[$i][1][0]; 244 } 245 // check if we need to skip the first header 246 if((!$no_header) && $flags['noheader']) { 247 $no_header = true; 248 } 249 $conv_idx[] = $i; 250 // get index of first header 251 if($first_header == -1) $first_header = $i; 252 // get max level if this instructions set 253 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 254 $lvl_max = $ins[$i][1][1]; 255 } 256 break; 257 case 'section_open': 258 $conv_idx[] = $i; 259 break; 260 case 'internallink': 261 case 'internalmedia': 262 if($ins[$i][1][0]{0} == '.') { 263 if($ins[$i][1][0]{1} == '.') { 264 $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace 265 } else { 266 $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace 267 } 268 } elseif (strpos($ins[$i][1][0], ':') === false) { 269 $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links 270 } 271 break; 272 case 'plugin': 273 // FIXME skip other plugins? 274 switch($ins[$i][1][0]) { 275 case 'tag_tag': // skip tags 276 case 'discussion_comments': // skip comments 277 case 'linkback': // skip linkbacks 278 case 'data_entry': // skip data plugin 279 case 'meta': // skip meta plugin 280 unset($ins[$i]); 281 break; 282 } 283 break; 284 default: 285 break; 286 } 287 } 288 289 // calculate difference between header/section level and include level 290 $diff = 0; 291 if (!$lvl_max) $lvl_max = 0; // if no level found in target, set to 0 292 $diff = $lvl - $lvl_max + 1; 293 if ($no_header) $diff -= 1; // push up one level if "noheader" 294 295 // convert headers and set footer/permalink 296 $hdr_deleted = false; 297 $has_permalink = false; 298 $footer_lvl = false; 299 foreach($conv_idx as $idx) { 300 if($ins[$idx][0] == 'header') { 301 if($no_header && !$hdr_deleted) { 302 unset ($ins[$idx]); 303 $hdr_deleted = true; 304 continue; 305 } 306 $new_lvl = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 307 $ins[$idx][1][1] = $new_lvl; 308 309 // set permalink 310 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 311 $this->_permalink($ins[$idx], $page, $sect, $flags); 312 $has_permalink = true; 313 } 314 } else { 315 // it's a section 316 $new_lvl = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 317 $ins[$idx][1][0] = $new_lvl; 318 // check if noheader is used and set the footer level to the first section 319 if($no_header && !$footer_lvl) $footer_lvl = $new_lvl; 320 } 321 322 // set footer level 323 if(!$footer_lvl && ($idx == $first_header)) $footer_lvl = $new_lvl; 324 } 325 326 // add edit button 327 if($flags['editbtn'] && (auth_quickaclcheck($page) >= AUTH_EDIT)) $this->_editbtn($ins, $page, $sect, $sect_title); 328 329 // add footer 330 if($flags['footer']) $this->_footer($ins, $page, $sect, $sect_title, $flags, $footer_lvl); 331 332 // add instructions entry divs 333 array_unshift($ins, array('plugin', array('include_div', array('open', $page)))); 334 array_push($ins, array('plugin', array('include_div', array('close')))); 335 336 // close previous section if any and re-open after inclusion 337 if($lvl != 0) { 338 array_unshift($ins, array('section_close')); 339 $ins[] = array('section_open', array($lvl)); 340 } 341 } 342 343 /** 344 * Appends instruction item for the include plugin footer 345 * 346 * @author Michael Klier <chi@chimeric.de> 347 */ 348 function _footer(&$ins, $page, $sect, $sect_title, $flags, $footer_lvl) { 349 $footer = array(); 350 $footer[0] = 'plugin'; 351 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $this->toplevel_id, $footer_lvl)); 352 $ins[] = $footer; 353 } 354 355 /** 356 * Appends instruction item for an edit button 357 * 358 * @author Michael Klier <chi@chimeric.de> 359 */ 360 function _editbtn(&$ins, $page, $sect, $sect_title) { 361 $editbtn = array(); 362 $editbtn[0] = 'plugin'; 363 $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $this->toplevel_id)); 364 $ins[] = $editbtn; 365 } 366 367 /** 368 * Convert instruction item for a permalink header 369 * 370 * @author Michael Klier <chi@chimeric.de> 371 */ 372 function _permalink(&$ins, $page, $sect, $flags) { 373 $ins[0] = 'plugin'; 374 $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags)); 375 } 376 377 /** 378 * Get a section including its subsections 379 * 380 * @author Michael Klier <chi@chimeric.de> 381 */ 382 function _get_section(&$ins, $sect) { 383 $num = count($ins); 384 $offset = false; 385 $lvl = false; 386 $end = false; 387 388 for($i=0; $i<$num; $i++) { 389 if ($ins[$i][0] == 'header') { 390 391 // found the right header 392 if (cleanID($ins[$i][1][0]) == $sect) { 393 $offset = $i; 394 $lvl = $ins[$i][1][1]; 395 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 396 $end = $i - $offset; 397 break; 398 } 399 } 400 } 401 $offset = $offset ? $offset : 0; 402 $end = $end ? $end : ($num - 1); 403 if(is_array($ins)) { 404 $ins = array_slice($ins, $offset, $end); 405 } 406 } 407 408 /** 409 * Only display the first section of a page and a readmore link 410 * 411 * @author Michael Klier <chi@chimeric.de> 412 */ 413 function _get_firstsec(&$ins, $page) { 414 $num = count($ins); 415 $first_sect = false; 416 for($i=0; $i<$num; $i++) { 417 if($ins[$i][0] == 'section_close') { 418 $first_sect = $i; 419 } 420 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 421 $ins = array_slice($ins, 0, $first_sect); 422 $ins[] = array('p_open', array()); 423 $ins[] = array('internallink',array($page, $this->getLang('readmore'))); 424 $ins[] = array('p_close', array()); 425 $ins[] = array('section_close'); 426 return; 427 } 428 } 429 } 430 431 /** 432 * Makes user or date dependent includes possible 433 */ 434 function _apply_macro($id) { 435 global $INFO; 436 global $auth; 437 438 // if we don't have an auth object, do nothing 439 if (!$auth) return $id; 440 441 $user = $_SERVER['REMOTE_USER']; 442 $group = $INFO['userinfo']['grps'][0]; 443 444 $replace = array( 445 '@USER@' => cleanID($user), 446 '@NAME@' => cleanID($INFO['userinfo']['name']), 447 '@GROUP@' => cleanID($group), 448 '@YEAR@' => date('Y'), 449 '@MONTH@' => date('m'), 450 '@DAY@' => date('d'), 451 ); 452 return str_replace(array_keys($replace), array_values($replace), $id); 453 } 454} 455//vim:ts=4:sw=4:et:enc=utf-8: 456