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