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 case 'nolink': 114 $flags['link'] = 0; 115 break; 116 case 'user': 117 $flags['user'] = 1; 118 break; 119 case 'nouser': 120 $flags['user'] = 0; 121 break; 122 case 'comments': 123 $flags['comments'] = 1; 124 break; 125 case 'nocomments': 126 $flags['comments'] = 0; 127 break; 128 case 'linkbacks': 129 $flags['linkbacks'] = 1; 130 break; 131 case 'nolinkbacks': 132 $flags['linkbacks'] = 0; 133 break; 134 case 'tags': 135 $flags['tags'] = 1; 136 break; 137 case 'notags': 138 $flags['tags'] = 0; 139 break; 140 case 'date': 141 $flags['date'] = 1; 142 break; 143 case 'nodate': 144 $flags['date'] = 0; 145 break; 146 } 147 } 148 return $flags; 149 } 150 151 /** 152 * Parses the instructions list of the page which contains the includes 153 * 154 * @author Michael Klier <chi@chimeric.de> 155 */ 156 function parse_instructions($id, &$ins) { 157 $num = count($ins); 158 159 $lvl = false; 160 $prev_lvl = false; 161 $mode = ''; 162 $page = ''; 163 $flags = array(); 164 $range = false; 165 $scope = $id; 166 167 for($i=0; $i<$num; $i++) { 168 // set current level 169 if($ins[$i][0] == 'section_open') { 170 $lvl = $ins[$i][1][0]; 171 if($i > $range) $prev_lvl = $lvl; 172 } 173 174 if($ins[$i][0] == 'plugin' && $ins[$i][1][0] == 'include_include' ) { 175 // found no previous section set lvl to 0 176 if(!$lvl) $lvl = 0; 177 178 $mode = $ins[$i][1][1][0]; 179 $page = $ins[$i][1][1][1]; 180 $sect = $ins[$i][1][1][2]; 181 $flags = $ins[$i][1][1][3]; 182 183 $page = $this->_apply_macro($page); 184 resolve_pageid(getNS($scope), $page, $exists); // resolve shortcuts 185 $ins[$i][1][1][4] = $scope; 186 $scope = $page; 187 188 $flags = $this->get_flags($flags); 189 190 $ins_inc = $this->_get_instructions($page, $sect, $mode, $lvl, $flags); 191 192 if(!empty($ins_inc)) { 193 // combine instructions and reset counter 194 $ins_start = array_slice($ins, 0, $i+1); 195 $ins_end = array_slice($ins, $i+1); 196 $range = $i + count($ins_inc); 197 $ins = array_merge($ins_start, $ins_inc, $ins_end); 198 $num = count($ins); 199 } 200 } 201 202 // check if we left the range of possible sub includes and reset lvl and scope to toplevel_id 203 if($range && ($i > $range)) { 204 $lvl = ($prev_lvl == 0) ? 1 : $prev_lvl; 205 $prev_lvl = false; 206 $range = false; 207 // reset scope to toplevel_id 208 $scope = $this->toplevel_id; 209 } 210 } 211 } 212 213 /** 214 * Returns the converted instructions of a give page/section 215 * 216 * @author Michael Klier <chi@chimeric.de> 217 */ 218 function _get_instructions($page, $sect, $mode, $lvl, $flags) { 219 global $ID; 220 221 if(($ID == $page) || (auth_quickaclcheck($page) < AUTH_READ) 222 || (!page_exists($page)) 223 && (auth_quickaclcheck($page) < AUTH_CREATE)) return array(); 224 225 $key = ($sect) ? $page . '#' . $sect : $page; 226 227 // prevent recursion 228 if(!$this->includes[$key]) { 229 $ins = p_cached_instructions(wikiFN($page)); 230 $this->includes[$key] = true; 231 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags); 232 return $ins; 233 } 234 } 235 236 /** 237 * Converts instructions of the included page 238 * 239 * The funcion iterates over the given list of instructions and generates 240 * an index of header and section indicies. It also removes document 241 * start/end instructions, converts links, and removes unwanted 242 * instructions like tags, comments, linkbacks. 243 * 244 * Later all header/section levels are convertet to match the current 245 * inclusion level. 246 * 247 * @author Michael Klier <chi@chimeric.de> 248 */ 249 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags) { 250 251 // filter instructions if needed 252 if(!empty($sect)) { 253 $this->_get_section($ins, $sect); // section required 254 } 255 256 if($flags['firstsec']) { 257 $this->_get_firstsec($ins, $page); // only first section 258 } 259 260 $ns = getNS($page); 261 $num = count($ins); 262 263 $conv_idx = array(); // conversion index 264 $lvl_max = false; // max level 265 $first_header = -1; 266 $no_header = false; 267 $sect_title = false; 268 269 for($i=0; $i<$num; $i++) { 270 switch($ins[$i][0]) { 271 case 'document_start': 272 case 'document_end': 273 case 'section_edit': 274 unset($ins[$i]); 275 break; 276 case 'header': 277 // get section title of first section 278 if($sect && !$sect_title) { 279 $sect_title = $ins[$i][1][0]; 280 } 281 // check if we need to skip the first header 282 if((!$no_header) && $flags['noheader']) { 283 $no_header = true; 284 } 285 286 $conv_idx[] = $i; 287 // get index of first header 288 if($first_header == -1) $first_header = $i; 289 // get max level of this instructions set 290 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 291 $lvl_max = $ins[$i][1][1]; 292 } 293 break; 294 case 'section_open': 295 $conv_idx[] = $i; 296 break; 297 case 'internallink': 298 case 'internalmedia': 299 if($ins[$i][1][0]{0} == '.') { 300 if($ins[$i][1][0]{1} == '.') { 301 $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace 302 } else { 303 $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace 304 } 305 } elseif (strpos($ins[$i][1][0], ':') === false) { 306 $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links 307 } 308 break; 309 case 'plugin': 310 // FIXME skip other plugins? 311 switch($ins[$i][1][0]) { 312 case 'tag_tag': // skip tags 313 case 'discussion_comments': // skip comments 314 case 'linkback': // skip linkbacks 315 case 'data_entry': // skip data plugin 316 case 'meta': // skip meta plugin 317 unset($ins[$i]); 318 break; 319 } 320 break; 321 default: 322 break; 323 } 324 } 325 326 // calculate difference between header/section level and include level 327 $diff = 0; 328 if (!$lvl_max) $lvl_max = 0; // if no level found in target, set to 0 329 $diff = $lvl - $lvl_max + 1; 330 if ($no_header) $diff -= 1; // push up one level if "noheader" 331 332 // convert headers and set footer/permalink 333 $hdr_deleted = false; 334 $has_permalink = false; 335 $footer_lvl = false; 336 foreach($conv_idx as $idx) { 337 if($ins[$idx][0] == 'header') { 338 if($no_header && !$hdr_deleted) { 339 unset ($ins[$idx]); 340 $hdr_deleted = true; 341 continue; 342 } 343 $new_lvl = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 344 $ins[$idx][1][1] = $new_lvl; 345 346 // set permalink 347 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 348 $this->_permalink($ins[$idx], $page, $sect, $flags); 349 $has_permalink = true; 350 } 351 } else { 352 // it's a section 353 $new_lvl = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 354 $ins[$idx][1][0] = $new_lvl; 355 // check if noheader is used and set the footer level to the first section 356 if($no_header && !$footer_lvl) $footer_lvl = $new_lvl; 357 } 358 359 // set footer level 360 if(!$footer_lvl && ($idx == $first_header)) $footer_lvl = $new_lvl; 361 } 362 363 // add edit button 364 if($flags['editbtn'] && (auth_quickaclcheck($page) >= AUTH_EDIT)) { 365 $this->_editbtn($ins, $page, $sect, $sect_title); 366 } 367 368 // add footer 369 if($flags['footer']) { 370 $this->_footer($ins, $page, $sect, $sect_title, $flags, $footer_lvl); 371 } 372 373 // add instructions entry divs 374 array_unshift($ins, array('plugin', array('include_div', array('open', $page)))); 375 array_push($ins, array('plugin', array('include_div', array('close')))); 376 377 // close previous section if any and re-open after inclusion 378 if($lvl != 0) { 379 array_unshift($ins, array('section_close')); 380 $ins[] = array('section_open', array($lvl)); 381 } 382 } 383 384 /** 385 * Appends instruction item for the include plugin footer 386 * 387 * @author Michael Klier <chi@chimeric.de> 388 */ 389 function _footer(&$ins, $page, $sect, $sect_title, $flags, $footer_lvl) { 390 $footer = array(); 391 $footer[0] = 'plugin'; 392 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $this->toplevel_id, $footer_lvl)); 393 $ins[] = $footer; 394 } 395 396 /** 397 * Appends instruction item for an edit button 398 * 399 * @author Michael Klier <chi@chimeric.de> 400 */ 401 function _editbtn(&$ins, $page, $sect, $sect_title) { 402 $editbtn = array(); 403 $editbtn[0] = 'plugin'; 404 $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $this->toplevel_id)); 405 $ins[] = $editbtn; 406 } 407 408 /** 409 * Convert instruction item for a permalink header 410 * 411 * @author Michael Klier <chi@chimeric.de> 412 */ 413 function _permalink(&$ins, $page, $sect, $flags) { 414 $ins[0] = 'plugin'; 415 $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags)); 416 } 417 418 /** 419 * Get a section including its subsections 420 * 421 * @author Michael Klier <chi@chimeric.de> 422 */ 423 function _get_section(&$ins, $sect) { 424 $num = count($ins); 425 $offset = false; 426 $lvl = false; 427 $end = false; 428 429 for($i=0; $i<$num; $i++) { 430 if ($ins[$i][0] == 'header') { 431 432 // found the right header 433 if (cleanID($ins[$i][1][0]) == $sect) { 434 $offset = $i; 435 $lvl = $ins[$i][1][1]; 436 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 437 $end = $i - $offset; 438 break; 439 } 440 } 441 } 442 $offset = $offset ? $offset : 0; 443 $end = $end ? $end : ($num - 1); 444 if(is_array($ins)) { 445 $ins = array_slice($ins, $offset, $end); 446 } 447 } 448 449 /** 450 * Only display the first section of a page and a readmore link 451 * 452 * @author Michael Klier <chi@chimeric.de> 453 */ 454 function _get_firstsec(&$ins, $page) { 455 $num = count($ins); 456 $first_sect = false; 457 for($i=0; $i<$num; $i++) { 458 if($ins[$i][0] == 'section_close') { 459 $first_sect = $i; 460 } 461 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 462 $ins = array_slice($ins, 0, $first_sect); 463 $ins[] = array('p_open', array()); 464 $ins[] = array('internallink',array($page, $this->getLang('readmore'))); 465 $ins[] = array('p_close', array()); 466 $ins[] = array('section_close'); 467 return; 468 } 469 } 470 } 471 472 /** 473 * Makes user or date dependent includes possible 474 */ 475 function _apply_macro($id) { 476 global $INFO; 477 global $auth; 478 479 // if we don't have an auth object, do nothing 480 if (!$auth) return $id; 481 482 $user = $_SERVER['REMOTE_USER']; 483 $group = $INFO['userinfo']['grps'][0]; 484 485 $replace = array( 486 '@USER@' => cleanID($user), 487 '@NAME@' => cleanID($INFO['userinfo']['name']), 488 '@GROUP@' => cleanID($group), 489 '@YEAR@' => date('Y'), 490 '@MONTH@' => date('m'), 491 '@DAY@' => date('d'), 492 ); 493 return str_replace(array_keys($replace), array_values($replace), $id); 494 } 495} 496//vim:ts=4:sw=4:et:enc=utf-8: 497