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 * @author Michael Hamann <michael@content-space.de> 8 */ 9 10// must be run within Dokuwiki 11if (!defined('DOKU_INC')) die(); 12 13if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 14if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 15if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/'); 16 17/** 18 * Helper functions for the include plugin and other plugins that want to include pages. 19 */ 20class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin 21 22 var $defaults = array(); 23 var $sec_close = true; 24 /** @var helper_plugin_tag $taghelper */ 25 var $taghelper = null; 26 var $includes = array(); // deprecated - compatibility code for the blog plugin 27 28 /** 29 * Constructor loads default config settings once 30 */ 31 function helper_plugin_include() { 32 $this->defaults['noheader'] = $this->getConf('noheader'); 33 $this->defaults['firstsec'] = $this->getConf('firstseconly'); 34 $this->defaults['editbtn'] = $this->getConf('showeditbtn'); 35 $this->defaults['taglogos'] = $this->getConf('showtaglogos'); 36 $this->defaults['footer'] = $this->getConf('showfooter'); 37 $this->defaults['redirect'] = $this->getConf('doredirect'); 38 $this->defaults['date'] = $this->getConf('showdate'); 39 $this->defaults['mdate'] = $this->getConf('showmdate'); 40 $this->defaults['user'] = $this->getConf('showuser'); 41 $this->defaults['comments'] = $this->getConf('showcomments'); 42 $this->defaults['linkbacks'] = $this->getConf('showlinkbacks'); 43 $this->defaults['tags'] = $this->getConf('showtags'); 44 $this->defaults['link'] = $this->getConf('showlink'); 45 $this->defaults['permalink'] = $this->getConf('showpermalink'); 46 $this->defaults['indent'] = $this->getConf('doindent'); 47 $this->defaults['linkonly'] = $this->getConf('linkonly'); 48 $this->defaults['title'] = $this->getConf('title'); 49 $this->defaults['pageexists'] = $this->getConf('pageexists'); 50 $this->defaults['parlink'] = $this->getConf('parlink'); 51 $this->defaults['inline'] = false; 52 $this->defaults['order'] = $this->getConf('order'); 53 $this->defaults['rsort'] = $this->getConf('rsort'); 54 $this->defaults['depth'] = $this->getConf('depth'); 55 $this->defaults['readmore'] = $this->getConf('readmore'); 56 } 57 58 /** 59 * Available methods for other plugins 60 */ 61 function getMethods() { 62 $result = array(); 63 $result[] = array( 64 'name' => 'get_flags', 65 'desc' => 'overrides standard values for showfooter and firstseconly settings', 66 'params' => array('flags' => 'array'), 67 ); 68 return $result; 69 } 70 71 /** 72 * Overrides standard values for showfooter and firstseconly settings 73 */ 74 function get_flags($setflags) { 75 // load defaults 76 $flags = $this->defaults; 77 foreach ($setflags as $flag) { 78 $value = ''; 79 if (strpos($flag, '=') !== -1) { 80 list($flag, $value) = explode('=', $flag, 2); 81 } 82 switch ($flag) { 83 case 'footer': 84 $flags['footer'] = 1; 85 break; 86 case 'nofooter': 87 $flags['footer'] = 0; 88 break; 89 case 'firstseconly': 90 case 'firstsectiononly': 91 $flags['firstsec'] = 1; 92 break; 93 case 'fullpage': 94 $flags['firstsec'] = 0; 95 break; 96 case 'showheader': 97 case 'header': 98 $flags['noheader'] = 0; 99 break; 100 case 'noheader': 101 $flags['noheader'] = 1; 102 break; 103 case 'editbtn': 104 case 'editbutton': 105 $flags['editbtn'] = 1; 106 break; 107 case 'noeditbtn': 108 case 'noeditbutton': 109 $flags['editbtn'] = 0; 110 break; 111 case 'permalink': 112 $flags['permalink'] = 1; 113 break; 114 case 'nopermalink': 115 $flags['permalink'] = 0; 116 break; 117 case 'redirect': 118 $flags['redirect'] = 1; 119 break; 120 case 'noredirect': 121 $flags['redirect'] = 0; 122 break; 123 case 'link': 124 $flags['link'] = 1; 125 break; 126 case 'nolink': 127 $flags['link'] = 0; 128 break; 129 case 'user': 130 $flags['user'] = 1; 131 break; 132 case 'nouser': 133 $flags['user'] = 0; 134 break; 135 case 'comments': 136 $flags['comments'] = 1; 137 break; 138 case 'nocomments': 139 $flags['comments'] = 0; 140 break; 141 case 'linkbacks': 142 $flags['linkbacks'] = 1; 143 break; 144 case 'nolinkbacks': 145 $flags['linkbacks'] = 0; 146 break; 147 case 'tags': 148 $flags['tags'] = 1; 149 break; 150 case 'notags': 151 $flags['tags'] = 0; 152 break; 153 case 'date': 154 $flags['date'] = 1; 155 break; 156 case 'nodate': 157 $flags['date'] = 0; 158 break; 159 case 'mdate': 160 $flags['mdate'] = 1; 161 break; 162 case 'nomdate': 163 $flags['mdate'] = 0; 164 break; 165 case 'indent': 166 $flags['indent'] = 1; 167 break; 168 case 'noindent': 169 $flags['indent'] = 0; 170 break; 171 case 'linkonly': 172 $flags['linkonly'] = 1; 173 break; 174 case 'nolinkonly': 175 case 'include_content': 176 $flags['linkonly'] = 0; 177 break; 178 case 'inline': 179 $flags['inline'] = 1; 180 break; 181 case 'title': 182 $flags['title'] = 1; 183 break; 184 case 'notitle': 185 $flags['title'] = 0; 186 break; 187 case 'pageexists': 188 $flags['pageexists'] = 1; 189 break; 190 case 'nopageexists': 191 $flags['pageexists'] = 0; 192 break; 193 case 'existlink': 194 $flags['pageexists'] = 1; 195 $flags['linkonly'] = 1; 196 break; 197 case 'parlink': 198 $flags['parlink'] = 1; 199 break; 200 case 'noparlink': 201 $flags['parlink'] = 0; 202 break; 203 case 'order': 204 $flags['order'] = $value; 205 break; 206 case 'sort': 207 $flags['rsort'] = 0; 208 break; 209 case 'rsort': 210 $flags['rsort'] = 1; 211 break; 212 case 'depth': 213 $flags['depth'] = max(intval($value), 0); 214 break; 215 case 'beforeeach': 216 $flags['beforeeach'] = $value; 217 break; 218 case 'aftereach': 219 $flags['aftereach'] = $value; 220 break; 221 case 'readmore': 222 $flags['readmore'] = 1; 223 break; 224 case 'noreadmore': 225 $flags['readmore'] = 0; 226 break; 227 } 228 } 229 // the include_content URL parameter overrides flags 230 if (isset($_REQUEST['include_content'])) 231 $flags['linkonly'] = 0; 232 return $flags; 233 } 234 235 /** 236 * Returns the converted instructions of a give page/section 237 * 238 * @author Michael Klier <chi@chimeric.de> 239 * @author Michael Hamann <michael@content-space.de> 240 */ 241 function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $included_pages = array()) { 242 $key = ($sect) ? $page . '#' . $sect : $page; 243 $this->includes[$key] = true; // legacy code for keeping compatibility with other plugins 244 245 // keep compatibility with other plugins that don't know the $root_id parameter 246 if (is_null($root_id)) { 247 global $ID; 248 $root_id = $ID; 249 } 250 251 if ($flags['linkonly']) { 252 if (page_exists($page) || $flags['pageexists'] == 0) { 253 $title = ''; 254 if ($flags['title']) 255 $title = p_get_first_heading($page); 256 if($flags['parlink']) { 257 $ins = array( 258 array('p_open', array()), 259 array('internallink', array(':'.$key, $title)), 260 array('p_close', array()), 261 ); 262 } else { 263 $ins = array(array('internallink', array(':'.$key,$title))); 264 } 265 }else { 266 $ins = array(); 267 } 268 } else { 269 if (page_exists($page)) { 270 global $ID; 271 $backupID = $ID; 272 $ID = $page; // Change the global $ID as otherwise plugins like the discussion plugin will save data for the wrong page 273 $ins = p_cached_instructions(wikiFN($page), false, $page); 274 $ID = $backupID; 275 } else { 276 $ins = array(); 277 } 278 279 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id, $included_pages); 280 } 281 return $ins; 282 } 283 284 /** 285 * Converts instructions of the included page 286 * 287 * The funcion iterates over the given list of instructions and generates 288 * an index of header and section indicies. It also removes document 289 * start/end instructions, converts links, and removes unwanted 290 * instructions like tags, comments, linkbacks. 291 * 292 * Later all header/section levels are convertet to match the current 293 * inclusion level. 294 * 295 * @author Michael Klier <chi@chimeric.de> 296 */ 297 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $included_pages = array()) { 298 global $conf; 299 300 // filter instructions if needed 301 if(!empty($sect)) { 302 $this->_get_section($ins, $sect); // section required 303 } 304 305 if($flags['firstsec']) { 306 $this->_get_firstsec($ins, $page, $flags); // only first section 307 } 308 309 $ns = getNS($page); 310 $num = count($ins); 311 312 $conv_idx = array(); // conversion index 313 $lvl_max = false; // max level 314 $first_header = -1; 315 $no_header = false; 316 $sect_title = false; 317 $endpos = null; // end position of the raw wiki text 318 319 for($i=0; $i<$num; $i++) { 320 // adjust links with image titles 321 if (strpos($ins[$i][0], 'link') !== false && isset($ins[$i][1][1]) && is_array($ins[$i][1][1]) && $ins[$i][1][1]['type'] == 'internalmedia') { 322 // resolve relative ids, but without cleaning in order to preserve the name 323 $media_id = resolve_id($ns, $ins[$i][1][1]['src']); 324 // make sure that after resolving the link again it will be the same link 325 if ($media_id{0} != ':') $media_id = ':'.$media_id; 326 $ins[$i][1][1]['src'] = $media_id; 327 } 328 329 switch($ins[$i][0]) { 330 case 'document_start': 331 case 'document_end': 332 case 'section_edit': 333 unset($ins[$i]); 334 break; 335 case 'header': 336 // get section title of first section 337 if($sect && !$sect_title) { 338 $sect_title = $ins[$i][1][0]; 339 } 340 // check if we need to skip the first header 341 if((!$no_header) && $flags['noheader']) { 342 $no_header = true; 343 } 344 345 $conv_idx[] = $i; 346 // get index of first header 347 if($first_header == -1) $first_header = $i; 348 // get max level of this instructions set 349 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 350 $lvl_max = $ins[$i][1][1]; 351 } 352 break; 353 case 'section_open': 354 if ($flags['inline']) 355 unset($ins[$i]); 356 else 357 $conv_idx[] = $i; 358 break; 359 case 'section_close': 360 if ($flags['inline']) 361 unset($ins[$i]); 362 break; 363 case 'internallink': 364 case 'internalmedia': 365 // make sure parameters aren't touched 366 $link_params = ''; 367 $link_id = $ins[$i][1][0]; 368 $link_parts = explode('?', $link_id, 2); 369 if (count($link_parts) === 2) { 370 $link_id = $link_parts[0]; 371 $link_params = $link_parts[1]; 372 } 373 // resolve the id without cleaning it 374 $link_id = resolve_id($ns, $link_id, false); 375 // this id is internal (i.e. absolute) now, add ':' to make resolve_id work again 376 if ($link_id{0} != ':') $link_id = ':'.$link_id; 377 // restore parameters 378 $ins[$i][1][0] = ($link_params != '') ? $link_id.'?'.$link_params : $link_id; 379 if ($ins[$i][0] == 'internallink' && !empty($included_pages)) { 380 // change links to included pages into local links 381 $link_id = $ins[$i][1][0]; 382 $link_parts = explode('?', $link_id, 2); 383 // only adapt links without parameters 384 if (count($link_parts) === 1) { 385 $link_parts = explode('#', $link_id, 2); 386 $hash = ''; 387 if (count($link_parts) === 2) { 388 list($link_id, $hash) = $link_parts; 389 } 390 $exists = false; 391 resolve_pageid($ns, $link_id, $exists); 392 if (array_key_exists($link_id, $included_pages)) { 393 if ($hash) { 394 // hopefully the hash is also unique in the including page (otherwise this might be the wrong link target) 395 $ins[$i][0] = 'locallink'; 396 $ins[$i][1][0] = $hash; 397 } else { 398 // the include section ids are different from normal section ids (so they won't conflict) but this 399 // also means that the normal locallink function can't be used 400 $ins[$i][0] = 'plugin'; 401 $ins[$i][1] = array('include_locallink', array($included_pages[$link_id]['hid'], $ins[$i][1][1], $ins[$i][1][0])); 402 } 403 } 404 } 405 } 406 break; 407 case 'locallink': 408 /* Convert local links to internal links if the page hasn't been fully included */ 409 if ($included_pages == null || !array_key_exists($page, $included_pages)) { 410 $ins[$i][0] = 'internallink'; 411 $ins[$i][1][0] = ':'.$page.'#'.$ins[$i][1][0]; 412 } 413 break; 414 case 'plugin': 415 // FIXME skip other plugins? 416 switch($ins[$i][1][0]) { 417 case 'tag_tag': // skip tags 418 case 'discussion_comments': // skip comments 419 case 'linkback': // skip linkbacks 420 case 'data_entry': // skip data plugin 421 case 'meta': // skip meta plugin 422 case 'indexmenu_tag': // skip indexmenu sort tag 423 case 'include_sorttag': // skip include plugin sort tag 424 unset($ins[$i]); 425 break; 426 // adapt indentation level of nested includes 427 case 'include_include': 428 if (!$flags['inline'] && $flags['indent']) 429 $ins[$i][1][1][4] += $lvl; 430 break; 431 /* 432 * if there is already a closelastsecedit instruction (was added by one of the section 433 * functions), store its position but delete it as it can't be determined yet if it is needed, 434 * i.e. if there is a header which generates a section edit (depends on the levels, level 435 * adjustments, $no_header, ...) 436 */ 437 case 'include_closelastsecedit': 438 $endpos = $ins[$i][1][1][0]; 439 unset($ins[$i]); 440 break; 441 } 442 break; 443 default: 444 break; 445 } 446 } 447 448 // calculate difference between header/section level and include level 449 $diff = 0; 450 if (!isset($lvl_max)) $lvl_max = 0; // if no level found in target, set to 0 451 $diff = $lvl - $lvl_max + 1; 452 if ($no_header) $diff -= 1; // push up one level if "noheader" 453 454 // convert headers and set footer/permalink 455 $hdr_deleted = false; 456 $has_permalink = false; 457 $footer_lvl = false; 458 $contains_secedit = false; 459 $section_close_at = false; 460 foreach($conv_idx as $idx) { 461 if($ins[$idx][0] == 'header') { 462 if ($section_close_at === false && isset($ins[$idx+1]) && $ins[$idx+1][0] == 'section_open') { 463 // store the index of the first heading that is followed by a new section 464 // the wrap plugin creates sections without section_open so the section shouldn't be closed before them 465 $section_close_at = $idx; 466 } 467 468 if($no_header && !$hdr_deleted) { 469 unset ($ins[$idx]); 470 $hdr_deleted = true; 471 continue; 472 } 473 474 if($flags['indent']) { 475 $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 476 $ins[$idx][1][1] = $lvl_new; 477 } 478 479 if($ins[$idx][1][1] <= $conf['maxseclevel']) 480 $contains_secedit = true; 481 482 // set permalink 483 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 484 $this->_permalink($ins[$idx], $page, $sect, $flags); 485 $has_permalink = true; 486 } 487 488 // set footer level 489 if(!$footer_lvl && ($idx == $first_header) && !$no_header) { 490 if($flags['indent'] && isset($lvl_new)) { 491 $footer_lvl = $lvl_new; 492 } else { 493 $footer_lvl = $lvl_max; 494 } 495 } 496 } else { 497 // it's a section 498 if($flags['indent']) { 499 $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 500 $ins[$idx][1][0] = $lvl_new; 501 } 502 503 // check if noheader is used and set the footer level to the first section 504 if($no_header && !$footer_lvl) { 505 if($flags['indent'] && isset($lvl_new)) { 506 $footer_lvl = $lvl_new; 507 } else { 508 $footer_lvl = $lvl_max; 509 } 510 } 511 } 512 } 513 514 // close last open section of the included page if there is any 515 if ($contains_secedit) { 516 array_push($ins, array('plugin', array('include_closelastsecedit', array($endpos)))); 517 } 518 519 // add edit button 520 if($flags['editbtn']) { 521 $this->_editbtn($ins, $page, $sect, $sect_title, ($flags['redirect'] ? $root_id : false)); 522 } 523 524 // add footer 525 if($flags['footer']) { 526 $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id); 527 } 528 529 // wrap content at the beginning of the include that is not in a section in a section 530 if ($lvl > 0 && $section_close_at !== 0 && $flags['indent'] && !$flags['inline']) { 531 if ($section_close_at === false) { 532 $ins[] = array('section_close', array()); 533 array_unshift($ins, array('section_open', array($lvl))); 534 } else { 535 $section_close_idx = array_search($section_close_at, array_keys($ins)); 536 if ($section_close_idx > 0) { 537 $before_ins = array_slice($ins, 0, $section_close_idx); 538 $after_ins = array_slice($ins, $section_close_idx); 539 $ins = array_merge($before_ins, array(array('section_close', array())), $after_ins); 540 array_unshift($ins, array('section_open', array($lvl))); 541 } 542 } 543 } 544 545 // add instructions entry wrapper 546 $include_secid = (isset($flags['include_secid']) ? $flags['include_secid'] : NULL); 547 array_unshift($ins, array('plugin', array('include_wrap', array('open', $page, $flags['redirect'], $include_secid)))); 548 if (isset($flags['beforeeach'])) 549 array_unshift($ins, array('entity', array($flags['beforeeach']))); 550 array_push($ins, array('plugin', array('include_wrap', array('close')))); 551 if (isset($flags['aftereach'])) 552 array_push($ins, array('entity', array($flags['aftereach']))); 553 554 // close previous section if any and re-open after inclusion 555 if($lvl != 0 && $this->sec_close && !$flags['inline']) { 556 array_unshift($ins, array('section_close', array())); 557 $ins[] = array('section_open', array($lvl)); 558 } 559 } 560 561 /** 562 * Appends instruction item for the include plugin footer 563 * 564 * @author Michael Klier <chi@chimeric.de> 565 */ 566 function _footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id) { 567 $footer = array(); 568 $footer[0] = 'plugin'; 569 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $root_id, $footer_lvl)); 570 return $footer; 571 } 572 573 /** 574 * Appends instruction item for an edit button 575 * 576 * @author Michael Klier <chi@chimeric.de> 577 */ 578 function _editbtn(&$ins, $page, $sect, $sect_title, $root_id) { 579 $title = ($sect) ? $sect_title : $page; 580 $editbtn = array(); 581 $editbtn[0] = 'plugin'; 582 $editbtn[1] = array('include_editbtn', array($title)); 583 $ins[] = $editbtn; 584 } 585 586 /** 587 * Convert instruction item for a permalink header 588 * 589 * @author Michael Klier <chi@chimeric.de> 590 */ 591 function _permalink(&$ins, $page, $sect, $flags) { 592 $ins[0] = 'plugin'; 593 $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $ins[1][2], $page, $sect, $flags)); 594 } 595 596 /** 597 * Get a section including its subsections 598 * 599 * @author Michael Klier <chi@chimeric.de> 600 */ 601 function _get_section(&$ins, $sect) { 602 $num = count($ins); 603 $offset = false; 604 $lvl = false; 605 $end = false; 606 $endpos = null; // end position in the input text, needed for section edit buttons 607 608 $check = array(); // used for sectionID() in order to get the same ids as the xhtml renderer 609 610 for($i=0; $i<$num; $i++) { 611 if ($ins[$i][0] == 'header') { 612 613 // found the right header 614 if (sectionID($ins[$i][1][0], $check) == $sect) { 615 $offset = $i; 616 $lvl = $ins[$i][1][1]; 617 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 618 $end = $i - $offset; 619 $endpos = $ins[$i][1][2]; // the position directly after the found section, needed for the section edit button 620 break; 621 } 622 } 623 } 624 $offset = $offset ? $offset : 0; 625 $end = $end ? $end : ($num - 1); 626 if(is_array($ins)) { 627 $ins = array_slice($ins, $offset, $end); 628 // store the end position in the include_closelastsecedit instruction so it can generate a matching button 629 $ins[] = array('plugin', array('include_closelastsecedit', array($endpos))); 630 } 631 } 632 633 /** 634 * Only display the first section of a page and a readmore link 635 * 636 * @author Michael Klier <chi@chimeric.de> 637 */ 638 function _get_firstsec(&$ins, $page, $flags) { 639 $num = count($ins); 640 $first_sect = false; 641 $endpos = null; // end position in the input text 642 for($i=0; $i<$num; $i++) { 643 if($ins[$i][0] == 'section_close') { 644 $first_sect = $i; 645 } 646 if ($ins[$i][0] == 'header') { 647 /* 648 * Store the position of the last header that is encountered. As section_close/open-instruction are 649 * always (unless some plugin modifies this) around a header instruction this means that the last 650 * position that is stored here is exactly the position of the section_close/open at which the content 651 * is truncated. 652 */ 653 $endpos = $ins[$i][1][2]; 654 } 655 // only truncate the content and add the read more link when there is really 656 // more than that first section 657 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 658 $ins = array_slice($ins, 0, $first_sect); 659 if ($flags['readmore']) { 660 $ins[] = array('plugin', array('include_readmore', array($page))); 661 } 662 $ins[] = array('section_close', array()); 663 // store the end position in the include_closelastsecedit instruction so it can generate a matching button 664 $ins[] = array('plugin', array('include_closelastsecedit', array($endpos))); 665 return; 666 } 667 } 668 } 669 670 /** 671 * Gives a list of pages for a given include statement 672 * 673 * @author Michael Hamann <michael@content-space.de> 674 */ 675 function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { 676 global $conf; 677 $pages = array(); 678 switch($mode) { 679 case 'namespace': 680 $page = cleanID($page); 681 $ns = utf8_encodeFN(str_replace(':', '/', $page)); 682 // depth is absolute depth, not relative depth, but 0 has a special meaning. 683 $depth = $flags['depth'] ? $flags['depth'] + substr_count($page, ':') + ($page ? 1 : 0) : 0; 684 search($pagearrays, $conf['datadir'], 'search_allpages', array('depth' => $depth), $ns); 685 if (is_array($pagearrays)) { 686 foreach ($pagearrays as $pagearray) { 687 if (!isHiddenPage($pagearray['id'])) // skip hidden pages 688 $pages[] = $pagearray['id']; 689 } 690 } 691 break; 692 case 'tagtopic': 693 if (!$this->taghelper) 694 $this->taghelper =& plugin_load('helper', 'tag'); 695 if(!$this->taghelper) { 696 msg('You have to install the tag plugin to use this functionality!', -1); 697 return array(); 698 } 699 $tag = $page; 700 $sect = ''; 701 $pagearrays = $this->taghelper->getTopic('', null, $tag); 702 foreach ($pagearrays as $pagearray) { 703 $pages[] = $pagearray['id']; 704 } 705 break; 706 default: 707 $page = $this->_apply_macro($page, $parent_id); 708 resolve_pageid(getNS($parent_id), $page, $exists); // resolve shortcuts and clean ID 709 if (auth_quickaclcheck($page) >= AUTH_READ) 710 $pages[] = $page; 711 } 712 713 if (count($pages) > 1) { 714 if ($flags['order'] === 'id') { 715 if ($flags['rsort']) { 716 usort($pages, array($this, '_r_strnatcasecmp')); 717 } else { 718 natcasesort($pages); 719 } 720 } else { 721 $ordered_pages = array(); 722 foreach ($pages as $page) { 723 $key = ''; 724 switch ($flags['order']) { 725 case 'title': 726 $key = p_get_first_heading($page); 727 break; 728 case 'created': 729 $key = p_get_metadata($page, 'date created', METADATA_DONT_RENDER); 730 break; 731 case 'modified': 732 $key = p_get_metadata($page, 'date modified', METADATA_DONT_RENDER); 733 break; 734 case 'indexmenu': 735 $key = p_get_metadata($page, 'indexmenu_n', METADATA_RENDER_USING_SIMPLE_CACHE); 736 if ($key === null) 737 $key = ''; 738 break; 739 case 'custom': 740 $key = p_get_metadata($page, 'include_n', METADATA_RENDER_USING_SIMPLE_CACHE); 741 if ($key === null) 742 $key = ''; 743 break; 744 } 745 $key .= '_'.$page; 746 $ordered_pages[$key] = $page; 747 } 748 if ($flags['rsort']) { 749 uksort($ordered_pages, array($this, '_r_strnatcasecmp')); 750 } else { 751 uksort($ordered_pages, 'strnatcasecmp'); 752 } 753 $pages = $ordered_pages; 754 } 755 } 756 757 $result = array(); 758 foreach ($pages as $page) { 759 $exists = page_exists($page); 760 $result[] = array('id' => $page, 'exists' => $exists, 'parent_id' => $parent_id); 761 } 762 return $result; 763 } 764 765 /** 766 * String comparisons using a "natural order" algorithm in reverse order 767 * 768 * @link http://php.net/manual/en/function.strnatcmp.php 769 * @param string $a First string 770 * @param string $b Second string 771 * @return int Similar to other string comparison functions, this one returns < 0 if 772 * str1 is greater than str2; > 773 * 0 if str1 is lesser than 774 * str2, and 0 if they are equal. 775 */ 776 function _r_strnatcasecmp($a, $b) { 777 return strnatcasecmp($b, $a); 778 } 779 780 /** 781 * This function generates the list of all included pages from a list of metadata 782 * instructions. 783 */ 784 function _get_included_pages_from_meta_instructions($instructions) { 785 $pages = array(); 786 foreach ($instructions as $instruction) { 787 $mode = $instruction['mode']; 788 $page = $instruction['page']; 789 $sect = $instruction['sect']; 790 $parent_id = $instruction['parent_id']; 791 $flags = $instruction['flags']; 792 $pages = array_merge($pages, $this->_get_included_pages($mode, $page, $sect, $parent_id, $flags)); 793 } 794 return $pages; 795 } 796 797 /** 798 * Get wiki language from "HTTP_ACCEPT_LANGUAGE" 799 * We allow the pattern e.g. "ja,en-US;q=0.7,en;q=0.3" 800 */ 801 function _get_language_of_wiki($id, $parent_id) { 802 global $conf; 803 $result = $conf['lang']; 804 if(strpos($id, '@BROWSER_LANG@') !== false){ 805 $brlangp = "/(^|,)([a-z][a-z](-[A-Z][A-Z])?)(;q=(0.[0-9]+))?/"; 806 if(preg_match_all( 807 $brlangp, $_SERVER["HTTP_ACCEPT_LANGUAGE"], 808 $matches, PREG_SET_ORDER 809 )){ 810 $langs = array(); 811 foreach($matches as $match){ 812 $langs[$match[2]] = $match[5]==''?1.0:$match[5]; 813 } 814 arsort($langs); 815 foreach($langs as $lang => $langq){ 816 $testpage = $this->_apply_macro(str_replace('@BROWSER_LANG@', $lang, $id), $parent_id); 817 resolve_pageid(getNS($parent_id), $testpage, $exists); 818 if($exists){ 819 $result = $lang; 820 break; 821 } 822 } 823 } 824 } 825 return cleanID($result); 826 } 827 828 /** 829 * Makes user or date dependent includes possible 830 */ 831 function _apply_macro($id, $parent_id) { 832 global $INFO; 833 global $auth; 834 835 // if we don't have an auth object, do nothing 836 if (!$auth) return $id; 837 838 $user = $_SERVER['REMOTE_USER']; 839 $group = $INFO['userinfo']['grps'][0]; 840 841 // set group for unregistered users 842 if (!isset($group)) { 843 $group = 'ALL'; 844 } 845 846 $time_stamp = time(); 847 if(preg_match('/@DATE(\w+)@/',$id,$matches)) { 848 switch($matches[1]) { 849 case 'PMONTH': 850 $time_stamp = strtotime("-1 month"); 851 break; 852 case 'NMONTH': 853 $time_stamp = strtotime("+1 month"); 854 break; 855 case 'NWEEK': 856 $time_stamp = strtotime("+1 week"); 857 break; 858 case 'PWEEK': 859 $time_stamp = strtotime("-1 week"); 860 break; 861 case 'TOMORROW': 862 $time_stamp = strtotime("+1 day"); 863 break; 864 case 'YESTERDAY': 865 $time_stamp = strtotime("-1 day"); 866 break; 867 case 'NYEAR': 868 $time_stamp = strtotime("+1 year"); 869 break; 870 case 'PYEAR': 871 $time_stamp = strtotime("-1 year"); 872 break; 873 } 874 $id = preg_replace('/@DATE(\w+)@/','', $id); 875 } 876 877 $replace = array( 878 '@USER@' => cleanID($user), 879 '@NAME@' => cleanID($INFO['userinfo']['name']), 880 '@GROUP@' => cleanID($group), 881 '@BROWSER_LANG@' => $this->_get_language_of_wiki($id, $parent_id), 882 '@YEAR@' => date('Y',$time_stamp), 883 '@MONTH@' => date('m',$time_stamp), 884 '@WEEK@' => date('W',$time_stamp), 885 '@DAY@' => date('d',$time_stamp), 886 '@YEARPMONTH@' => date('Ym',strtotime("-1 month")), 887 '@PMONTH@' => date('m',strtotime("-1 month")), 888 '@NMONTH@' => date('m',strtotime("+1 month")), 889 '@YEARNMONTH@' => date('Ym',strtotime("+1 month")), 890 '@YEARPWEEK@' => date('YW',strtotime("-1 week")), 891 '@PWEEK@' => date('W',strtotime("-1 week")), 892 '@NWEEK@' => date('W',strtotime("+1 week")), 893 '@YEARNWEEK@' => date('YW',strtotime("+1 week")), 894 ); 895 return str_replace(array_keys($replace), array_values($replace), $id); 896 } 897} 898// vim:ts=4:sw=4:et: 899