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 $ins = p_cached_instructions(wikiFN($page)); 166 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id); 167 return $ins; 168 } 169 170 /** 171 * Converts instructions of the included page 172 * 173 * The funcion iterates over the given list of instructions and generates 174 * an index of header and section indicies. It also removes document 175 * start/end instructions, converts links, and removes unwanted 176 * instructions like tags, comments, linkbacks. 177 * 178 * Later all header/section levels are convertet to match the current 179 * inclusion level. 180 * 181 * @author Michael Klier <chi@chimeric.de> 182 */ 183 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id) { 184 global $conf; 185 186 // filter instructions if needed 187 if(!empty($sect)) { 188 $this->_get_section($ins, $sect); // section required 189 } 190 191 if($flags['firstsec']) { 192 $this->_get_firstsec($ins, $page); // only first section 193 } 194 195 $ns = getNS($page); 196 $num = count($ins); 197 198 $conv_idx = array(); // conversion index 199 $lvl_max = false; // max level 200 $first_header = -1; 201 $no_header = false; 202 $sect_title = false; 203 204 for($i=0; $i<$num; $i++) { 205 switch($ins[$i][0]) { 206 case 'document_start': 207 case 'document_end': 208 case 'section_edit': 209 unset($ins[$i]); 210 break; 211 case 'header': 212 // get section title of first section 213 if($sect && !$sect_title) { 214 $sect_title = $ins[$i][1][0]; 215 } 216 // check if we need to skip the first header 217 if((!$no_header) && $flags['noheader']) { 218 $no_header = true; 219 } 220 221 $conv_idx[] = $i; 222 // get index of first header 223 if($first_header == -1) $first_header = $i; 224 // get max level of this instructions set 225 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 226 $lvl_max = $ins[$i][1][1]; 227 } 228 break; 229 case 'section_open': 230 $conv_idx[] = $i; 231 break; 232 case 'internallink': 233 case 'internalmedia': 234 if($ins[$i][1][0]{0} == '.') { 235 if($ins[$i][1][0]{1} == '.') { 236 $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace 237 } else { 238 $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace 239 } 240 } elseif (strpos($ins[$i][1][0], ':') === false) { 241 $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links 242 } 243 break; 244 case 'plugin': 245 // FIXME skip other plugins? 246 switch($ins[$i][1][0]) { 247 case 'tag_tag': // skip tags 248 case 'discussion_comments': // skip comments 249 case 'linkback': // skip linkbacks 250 case 'data_entry': // skip data plugin 251 case 'meta': // skip meta plugin 252 unset($ins[$i]); 253 break; 254 // adapt indentation level of nested includes 255 case 'include_include': 256 $ins[$i][1][1][4] += $lvl; 257 break; 258 } 259 break; 260 default: 261 break; 262 } 263 } 264 265 // calculate difference between header/section level and include level 266 $diff = 0; 267 if (!isset($lvl_max)) $lvl_max = 0; // if no level found in target, set to 0 268 $diff = $lvl - $lvl_max + 1; 269 if ($no_header) $diff -= 1; // push up one level if "noheader" 270 271 // convert headers and set footer/permalink 272 $hdr_deleted = false; 273 $has_permalink = false; 274 $footer_lvl = false; 275 $contains_secedit = false; 276 foreach($conv_idx as $idx) { 277 if($ins[$idx][0] == 'header') { 278 if($no_header && !$hdr_deleted) { 279 unset ($ins[$idx]); 280 $hdr_deleted = true; 281 continue; 282 } 283 284 if($flags['indent']) { 285 $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 286 $ins[$idx][1][1] = $lvl_new; 287 } 288 289 if($ins[$idx][1][1] <= $conf['maxseclevel']) 290 $contains_secedit = true; 291 292 // set permalink 293 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 294 $this->_permalink($ins[$idx], $page, $sect, $flags); 295 $has_permalink = true; 296 } 297 298 // set footer level 299 if(!$footer_lvl && ($idx == $first_header) && !$no_header) { 300 if($flags['indent']) { 301 $footer_lvl = $lvl_new; 302 } else { 303 $footer_lvl = $lvl_max; 304 } 305 } 306 } else { 307 // it's a section 308 if($flags['indent']) { 309 $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 310 $ins[$idx][1][0] = $lvl_new; 311 } 312 313 // check if noheader is used and set the footer level to the first section 314 if($no_header && !$footer_lvl) { 315 if($flags['indent']) { 316 $footer_lvl = $lvl_new; 317 } else { 318 $footer_lvl = $lvl_max; 319 } 320 } 321 } 322 } 323 324 // close last open section of the included page if there is any 325 if ($contains_secedit) { 326 array_push($ins, array('plugin', array('include_close_last_secedit', array()))); 327 } 328 329 // add edit button 330 if($flags['editbtn'] && (auth_quickaclcheck($page) >= AUTH_EDIT)) { 331 $this->_editbtn($ins, $page, $sect, $sect_title, $root_id); 332 } 333 334 // add footer 335 if($flags['footer']) { 336 $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id); 337 } 338 339 // add instructions entry divs 340 array_unshift($ins, array('plugin', array('include_div', array('open', $page)))); 341 array_push($ins, array('plugin', array('include_div', array('close')))); 342 343 // close previous section if any and re-open after inclusion 344 if($lvl != 0 && $this->sec_close) { 345 array_unshift($ins, array('section_close', array())); 346 $ins[] = array('section_open', array($lvl)); 347 } 348 } 349 350 /** 351 * Appends instruction item for the include plugin footer 352 * 353 * @author Michael Klier <chi@chimeric.de> 354 */ 355 function _footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id) { 356 $footer = array(); 357 $footer[0] = 'plugin'; 358 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $root_id, $footer_lvl)); 359 return $footer; 360 } 361 362 /** 363 * Appends instruction item for an edit button 364 * 365 * @author Michael Klier <chi@chimeric.de> 366 */ 367 function _editbtn(&$ins, $page, $sect, $sect_title, $root_id) { 368 $editbtn = array(); 369 $editbtn[0] = 'plugin'; 370 $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $root_id)); 371 $ins[] = $editbtn; 372 } 373 374 /** 375 * Convert instruction item for a permalink header 376 * 377 * @author Michael Klier <chi@chimeric.de> 378 */ 379 function _permalink(&$ins, $page, $sect, $flags) { 380 $ins[0] = 'plugin'; 381 $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags)); 382 } 383 384 /** 385 * Get a section including its subsections 386 * 387 * @author Michael Klier <chi@chimeric.de> 388 */ 389 function _get_section(&$ins, $sect) { 390 $num = count($ins); 391 $offset = false; 392 $lvl = false; 393 $end = false; 394 395 for($i=0; $i<$num; $i++) { 396 if ($ins[$i][0] == 'header') { 397 398 // found the right header 399 if (cleanID($ins[$i][1][0]) == $sect) { 400 $offset = $i; 401 $lvl = $ins[$i][1][1]; 402 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 403 $end = $i - $offset; 404 break; 405 } 406 } 407 } 408 $offset = $offset ? $offset : 0; 409 $end = $end ? $end : ($num - 1); 410 if(is_array($ins)) { 411 $ins = array_slice($ins, $offset, $end); 412 } 413 } 414 415 /** 416 * Only display the first section of a page and a readmore link 417 * 418 * @author Michael Klier <chi@chimeric.de> 419 */ 420 function _get_firstsec(&$ins, $page) { 421 $num = count($ins); 422 $first_sect = false; 423 for($i=0; $i<$num; $i++) { 424 if($ins[$i][0] == 'section_close') { 425 $first_sect = $i; 426 } 427 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 428 $ins = array_slice($ins, 0, $first_sect); 429 $ins[] = array('p_open', array()); 430 $ins[] = array('internallink', array($page, $this->getLang('readmore'))); 431 $ins[] = array('p_close', array()); 432 $ins[] = array('section_close', array()); 433 return; 434 } 435 } 436 } 437 438 /** 439 * Gives a list of pages for a given include statement 440 * 441 * @author Michael Hamann <michael@content-space.de> 442 */ 443 function _get_included_pages($mode, $page, $sect, $parent_id) { 444 global $conf; 445 $pages = array(); 446 switch($mode) { 447 case 'namespace': 448 $ns = str_replace(':', '/', cleanID($page)); 449 search($pagearrays, $conf['datadir'], 'search_list', '', $ns); 450 if (is_array($pagearrays)) { 451 foreach ($pagearrays as $pagearray) { 452 $pages[] = $pagearray['id']; 453 } 454 } 455 break; 456 case 'tagtopic': 457 if (!$this->taghelper) 458 $this->taghelper =& plugin_load('helper', 'tag'); 459 if(!$this->taghelper) { 460 msg('You have to install the tag plugin to use this functionality!', -1); 461 return array(); 462 } 463 $tag = $page; 464 $sect = ''; 465 $pagearrays = $this->taghelper->getTopic('', null, $tag); 466 foreach ($pagearrays as $pagearray) { 467 $pages[] = $pagearray['id']; 468 } 469 break; 470 default: 471 $page = $this->_apply_macro($page); 472 resolve_pageid(getNS($parent_id), $page, $exists); // resolve shortcuts and clean ID 473 if (auth_quickaclcheck($page) >= AUTH_READ) 474 $pages[] = $page; 475 } 476 477 sort($pages); 478 479 $result = array(); 480 foreach ($pages as $page) { 481 $perm = auth_quickaclcheck($page); 482 $exists = page_exists($page); 483 $result[] = array('id' => $page, 'exists' => $exists, 'can_edit' => ($perm >= AUTH_EDIT), 'parent_id' => $parent_id); 484 } 485 return $result; 486 } 487 488 /** 489 * This function generates the list of all included pages from a list of metadata 490 * instructions. 491 */ 492 function _get_included_pages_from_meta_instructions($instructions) { 493 $pages = array(); 494 foreach ($instructions as $instruction) { 495 extract($instruction); 496 $pages = array_merge($pages, $this->_get_included_pages($mode, $page, $sect, $parent_id)); 497 } 498 return $pages; 499 } 500 501 /** 502 * Makes user or date dependent includes possible 503 */ 504 function _apply_macro($id) { 505 global $INFO; 506 global $auth; 507 508 // if we don't have an auth object, do nothing 509 if (!$auth) return $id; 510 511 $user = $_SERVER['REMOTE_USER']; 512 $group = $INFO['userinfo']['grps'][0]; 513 514 $replace = array( 515 '@USER@' => cleanID($user), 516 '@NAME@' => cleanID($INFO['userinfo']['name']), 517 '@GROUP@' => cleanID($group), 518 '@YEAR@' => date('Y'), 519 '@MONTH@' => date('m'), 520 '@DAY@' => date('d'), 521 ); 522 return str_replace(array_keys($replace), array_values($replace), $id); 523 } 524} 525// vim:ts=4:sw=4:et: 526