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