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