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