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