1<?php 2/** 3 * Search with Scopes 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author i-net software <tools@inetsoftware.de> 7 * @author Gerry Weissbach <gweissbach@inetsoftware.de> 8 */ 9 10// must be run within Dokuwiki 11if(!defined('DOKU_INC')) die(); 12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 13 14require_once(DOKU_PLUGIN.'syntax.php'); 15 16class syntax_plugin_siteexport_toc extends DokuWiki_Syntax_Plugin { 17 18 var $insideToc = false; 19 var $savedToc = array(); 20 var $options = array(); 21 22 var $mergedPages = array(); 23 var $includedPages = array(); 24 25 function getType() { return 'protected'; } 26 function getPType() { return 'block'; } 27 function getAllowedTypes() { return array('container'); } 28 function getSort() { return 100; } 29 30 /** 31 * Connect pattern to lexer 32 */ 33 function connectTo($mode) { 34 $this->Lexer->addEntryPattern('<toc>(?=.*?</toc>)',$mode,'plugin_siteexport_toc'); 35 $this->Lexer->addEntryPattern('<toc .+?>(?=.*?</toc>)',$mode,'plugin_siteexport_toc'); 36 $this->Lexer->addSpecialPattern("\[\[.+?\]\]",$mode,'plugin_siteexport_toc'); 37 } 38 39 function postConnect() { 40 $this->Lexer->addExitPattern('</toc.*?>', 'plugin_siteexport_toc'); 41 } 42 43 function handle($match, $state, $pos, Doku_Handler $handler){ 44 global $ID, $INFO; 45 46 switch ($state) { 47 case DOKU_LEXER_ENTER: 48 49 $this->insideToc = true; 50 51 $this->options = explode(' ', substr($match, 5, -1)); 52 return array('start' => true, 'pos' => $pos, 'options' => $this->options); 53 break; 54 55 case DOKU_LEXER_SPECIAL: 56 57 if ( $this->insideToc ) { 58 59 $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match); 60 // Split title from URL 61 $link = explode('|',$link,2); 62 if ( !isset($link[1]) ) { 63 $link[1] = NULL; 64 } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) { 65 // If the title is an image, convert it to an array containing the image details 66 $link[1] = Doku_Handler_Parse_Media($link[1]); 67 } 68 $link[0] = trim($link[0]); 69 70 if ( ! (preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) || 71 preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$link[0]) || 72 preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) || 73 preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) || 74 preg_match('!^#.+!',$link[0]) ) 75 ) { 76 77 // Get current depth from call stack 78 $depth = 1; 79 if ( $handler->CallWriter instanceof Doku_Handler_List ) { 80 81 $calls = array_reverse($handler->CallWriter->calls); 82 $call = $calls[0]; 83 foreach ( $calls as $item ) { 84 if ( in_array( $item[0], array( 'list_item', 'list_open') ) ) { $call = $item; break;} 85 } 86 87 $depth = $handler->CallWriter->interpretSyntax($call[1][0], $listType); 88 89 } 90 91 if ( empty( $link[0] ) ) { break; } // No empty elements. This would lead to problems 92 return array($link[0], $link[1], $depth); 93 break; 94 } else { 95 // use parser! - but with another p 96 $handler->internallink($match, $state, $pos); 97 } 98 } else { 99 // use parser! 100 $handler->internallink($match, $state, $pos); 101 } 102 103 return false; 104 case DOKU_LEXER_UNMATCHED: 105 106 $handler->_addCall('cdata',array($match), $pos); 107 108 return false; 109 break; 110 case DOKU_LEXER_EXIT: 111 112 $this->insideToc = false; 113 return 'save__meta'; 114 break; 115 } 116 return false; 117 } 118 119 function render($format, Doku_Renderer $renderer, $data) { 120 global $ID, $lang, $INFO; 121 122 list( $SID, $NAME, $DEPTH ) = $data; 123 124 resolve_pageid(getNS($ID),$SID,$exists); 125// $SID = cleanID($SID); // hier kein cleanID, da sonst m�glicherweise der anker verloren geht 126 127 // Render XHTML and ODT 128 if ($mode == 'xhtml' || $mode == 'odt') { 129 130 // TOC Title 131 if ( isset($data['start']) ) { 132 133 if ( is_Array($data['options']) ) { 134 foreach( $data['options'] as $opt ) { 135 switch( $opt ) { 136 case 'description' : $renderer->meta['sitetoc']['showDescription'] = true; break; 137 case 'notoc' : $renderer->meta['sitetoc']['noTOC'] = true; break; 138 case 'merge' : $renderer->meta['sitetoc']['mergeDoc'] = true; break; 139 case 'nohead' : $renderer->meta['sitetoc']['noTocHeader'] = true; break; 140 case 'mergeheader' : $renderer->meta['sitetoc']['mergeHeader'] = true; break; 141 } 142 } 143 } 144 145 $renderer->section_open("1 sitetoc"); 146 if ( $renderer->meta['sitetoc']['noTocHeader'] === false ) { 147 $renderer->header($lang['toc'], 1, $data['pos']); 148 } 149 150 return true; 151 } 152 153 // All Output has been done 154 if ( !is_array($data) && $data == 'save__meta' ) { 155 156 // Close TOC 157 $renderer->section_close(); 158 159 if ( $renderer->meta['sitetoc']['noTOC'] === true ) { 160 $renderer->doc = preg_replace("/<div.*?sitetoc.*?$/si", "", $renderer->doc); 161 } 162 163 // If this is not set, we may have it as Metadata 164 if ( !$this->mergedPages && $renderer->meta['sitetoc']['mergeDoc'] ) { 165 $toc = $renderer->meta['sitetoc']['siteexportTOC']; 166 if ( is_array($toc)) { 167 foreach ($toc as $tocItem ) { 168 $this->mergedPages[] = $tocItem['id']; 169 } 170 } 171 } 172 173 // If there is some data to be merged 174 if ( count($this->mergedPages) > 0) { 175 176 $renderer->doc = ''; // Start fresh! 177 178 $renderer->section_open("1 mergedsite"); 179 180 // Prepare lookup Array 181 foreach ( $this->mergedPages as $tocItem ) { 182 $this->includedPages[] = array_shift(explode('#', $tocItem)); 183 } 184 185 // Load the instructions 186 $instr = array(); 187 foreach ( $this->mergedPages as $tocItem ) { 188 $file = wikiFN($tocItem); 189 190 if(@file_exists($file)) { 191 $instructions = p_cached_instructions($file, false, $tocItem); 192 } else { 193 $instructions = p_get_instructions(io_readWikiPage($file,$tocItem)); 194 } 195 196 // Convert Link instructions 197 $instructions = $this->_convertInstructions($instructions, $addID, $renderer); 198 199 if ( $renderer->meta['sitetoc']['mergeHeader'] && !empty($instr) ) { 200 // Merge 201 $instr = $this->_mergeWithHeaders($instr, $instructions, 1); 202 203 } else { 204 // Concat 205 $instr = array_merge($instr, $instructions); 206 } 207 } 208 209 if (empty($instr)) { 210 return; 211 } 212 213 $this->_cleanInstructions($instr, '/section_(close|open)/'); 214 $this->_cleanInstructions($instr, '/listu_(close|open)/'); 215 $this->_cleanInstructions($instr, '/listo_(close|open)/'); 216 217 $this->_render_output($renderer, $mode, $instr); 218 $renderer->section_close(); 219 } 220 return true; 221 } 222 223 // Save the current ID 224 $LNID = $SID; 225 226 // Add ID to flags['mergeDoc'] 227 if ( $renderer->meta['sitetoc']['mergeDoc'] === true ) { // || (count($renderer->meta['sitetoc']['siteexportTOC']) > 0 && $renderer->meta['sitetoc']['siteexportMergeDoc'] === true) ) { 228 $this->mergedPages[] = $SID; 229 $default = $renderer->_simpleTitle($SID); $isImage = false; 230 resolve_pageid(getNS($ID),$SID,$exists); 231 232 $NAME = empty($NAME) ? p_get_first_heading($SID,true) : $NAME; 233 $LNID = "$ID#" . sectionID($SID, $check); 234 } else { 235 // // print normal internal link (XHTML odt) 236 $renderer->internallink($LNID, $NAME, null); 237 238 // Display Description underneath 239 if ( $renderer->meta['sitetoc']['showDescription'] === true ) { 240 // $renderer->p_open(); 241 $renderer->cdata(p_get_metadata($SID, 'description abstract', true)); 242 // $renderer->p_close(); 243 } 244 } 245 246 // Render Metadata 247 } else if ($mode == 'metadata') { 248 if ( !is_array($data) && $data == 'save__meta' ) { 249 $renderer->meta['sitetoc']['siteexportTOC'] = $this->savedToc; 250 251 foreach ($this->savedToc as $page) { 252 $renderer->meta['relation']['references'][$page['id']] = $page['exists']; 253 } 254 255 $this->savedToc = array(); 256 } else if ( !isset($data['start']) && !isset($data['pos']) ) { 257 $this->savedToc[] = $this->__addTocItem($SID, $NAME, $DEPTH, $renderer); 258 } 259 } else { 260 return false; 261 } 262 263 return true; 264 } 265 266 /* 267 * pull apart the ID and create an Entry for the TOC 268 */ 269 function __addTocItem($id, $name, $depth, $renderer) { 270 global $conf; 271 global $ID; 272 273 // Render Title 274 $default = $renderer->_simpleTitle($id); 275 $exists = false; $isImage = false; $linktype = null; 276 resolve_pageid(getNS($ID),$id,$exists); 277 $name = $renderer->_getLinkTitle($name, $default, $isImage, $id, $linktype); 278 279 //keep hash anchor 280 list($id,$hash) = explode('#',$id,2); 281 if(!empty($hash)) $hash = $renderer->_headerToLink($hash); 282 283 // Build Sitetoc Item 284 $item = array(); 285 $item['id'] = $id; 286 $item['name'] = $name; 287 $item['anchor'] = $hash; 288 $item['depth'] = $depth; 289 $item['exists'] = $exists; 290 if(!$conf['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ){ 291 return false; 292 } 293 294 return $item; 295 } 296 297 /* 298 * Render the output of one page 299 */ 300 function _render_output($renderer, $mode, $instr) { 301 global $ID; 302 303 // Section IDs 304 // $addID = sectionID($addID, $check); //not possible to use a:b:c for id 305 306 if ( $mode == 'xhtml' ) { 307 308 //--------RENDER 309 //renderer information(TOC build / Cache used) 310 $info = array(); 311 $content = p_render($mode, $instr, $info); 312 313 //Remove TOC`s, section edit buttons and tags 314 $content = $this->_cleanXHTML($content); 315 316 // embed the included page 317 // $renderer->doc .= '<div class="include">'; 318 //add an anchor to find start of a inserted page 319 // $renderer->doc .= "<a name='$addID' id='$addID'>"; 320 $renderer->doc .= $content; 321 // $renderer->doc .= '</div>'; 322 } else if ( $mode == 'odt') { 323 324 // Loop through the instructions 325 foreach ( $instr as $instruction ) { 326 // Execute the callback against the Renderer 327 call_user_func_array(array($renderer, $instruction[0]),$instruction[1]); 328 } 329 } 330 } 331 332 /* 333 * Corrects relative internal links and media and 334 * converts headers of included pages to subheaders of the current page 335 */ 336 function _convertInstructions($instr, $id, &$renderer) { 337 global $ID; 338 global $conf; 339 340 $n = count($instr); 341 342 for ($i = 0; $i < $n; $i++){ 343 //internal links(links inside this wiki) an relative links 344 if((substr($instr[$i][0], 0, 12) == 'internallink')){ 345 $this->_convert_link($renderer,$instr[$i],$id); 346 } 347 else if((substr($instr[$i][0], 0, 13) == 'internalmedia')){ 348 $this->_convert_media($renderer,$instr[$i],$id); 349 } 350 } 351 352 //if its the document start, cut off the first element(document information) 353 if ($instr[0][0] == 'document_start') 354 return array_slice($instr, 1, -1); 355 else 356 return $instr; 357 } 358 359 /* 360 * Convert link of given instruction 361 */ 362 function _convert_link(&$renderer,&$instr,$id) { 363 global $ID; 364 365 $exists = false; 366 367 resolve_pageid(getNS($id),$instr[1][0],$exists); 368 list( $pageID, $pageReference ) = explode("#", $instr[1][0], 2); 369 370 if ( in_array($pageID, $this->includedPages) ) { 371 // Crate new internal Links 372 $check = null; 373 374 // Either get existing reference or create from first heading. If still not there take the alternate ID 375 $pageNameLink = empty( $pageReference ) ? sectionID($pageID,$check) : $pageReference; 376 377 $instr[1][0] = $ID . "#" . $pageNameLink; 378 379 } else { 380 // Convert external Links to plain Text 381 382 $instr = array( 383 "cdata", 384 array($instr[1][1]), 385 $instr[2] 386 ); 387 } 388 } 389 390 /* 391 * Convert internalmedia of given instruction 392 */ 393 function _convert_media(&$renderer,&$instr,$id) { 394 global $ID; 395 396 // Resolvemedia returns the absolute path to media by reference 397 $exists = false; 398 resolve_mediaid(getNS($id),$instr[1][0],$exists); 399 } 400 401 function _mergeWithHeaders($existing, $newInstructions, $level = 1) { 402 403 $returnInstructions = array(); 404 $preparedInstructions = array(); 405 $existingStart = $existingEnd = 0; 406 $firstRun = true; 407 408 while ( $this->_findNextHeaderSection($existing, $level, $existingStart, $existingEnd) ) { 409 410 if ( $firstRun ) { 411 $returnInstructions = array_merge($returnInstructions, array_slice($existing, 0, $existingStart)); 412 $firstRun = false; 413 } 414 415 $currentSlice = array_slice($existing, $existingStart, $existingEnd - $existingStart); 416 417 // Find matching part with headername 418 $newStart = $newEnd = 0; 419 if ( $this->_findNextHeaderSection($newInstructions, $level, $newStart, $newEnd, $currentSlice[0][1][0]) ) { 420 421 $newSlice = array_slice($newInstructions, $newStart, $newEnd - $newStart); 422 if ( $newSlice[0][0] == 'header' ) 423 array_shift($newSlice); // Remove Heading 424 425 // merge found parts on next level. 426 $returnedInstructions = $this->_mergeWithHeaders($currentSlice, $newSlice, $level+1); 427 428 // Put them at the end! 429 $preparedInstructions = array_merge($preparedInstructions, $returnedInstructions); 430 431 // Remove from input 432 array_splice($newInstructions, $newStart, $newEnd - $newStart); 433 } else { 434 $preparedInstructions = array_merge($preparedInstructions, $currentSlice); 435 } 436 437 $existingStart = $existingEnd; 438 } 439 440 // Append the rest 441 $returnInstructions = array_merge($returnInstructions, array_slice($existing, $existingStart)); 442 443 // Check for section close inconsistencies and put one at the very end ... 444 $section_postpend = array(); 445 if ( 446 ( 447 ($tmp = array_slice($newInstructions, -1)) 448 && ($tmp[0][0] == 'section_close') 449 ) 450 && 451 ( 452 ($tmp = array_slice($newInstructions, -2)) 453 && ($tmp[0][0] == 'section_close' ) 454 ) 455 ) { 456 $section_postpend = array_splice($newInstructions, -1); 457 } 458 if ( 459 ( 460 ($tmp = array_slice($returnInstructions, -1)) 461 && ($tmp[0][0] == 'section_close') 462 ) 463 && 464 ( 465 ($tmp = array_slice($returnInstructions, -2)) 466 && ($tmp[0][0] == 'section_close' ) 467 ) 468 ) { 469 $section_postpend = array_merge($section_postpend, array_splice($returnInstructions, -1)); 470 } 471 472 // What if there are headings left inside the $newInstructions????? 473 // Find matching part with headername 474 $newStart = $newEnd = 0; 475 $section_prepend = array(); 476 if ( $this->_findNextHeaderSection($newInstructions, $level, $newStart, $newEnd) ) { 477 // If there are header in here, build a prepend and have the rest at the end 478 $section_prepend = array_splice($newInstructions, 0, $newStart); 479 } else { 480 // If not, prepend all of it. 481 $section_prepend = $newInstructions; 482 $newInstructions = array(); 483 } 484 485 $returnInstructions = array_merge($returnInstructions, $section_prepend, $preparedInstructions, $newInstructions, $section_postpend); 486 487 return $returnInstructions; 488 } 489 490 function _findNextHeaderSection($section, $level, &$start, &$end, $headerName = null) { 491 492 $inCount = count($section); 493 $currentSlice = -1; 494 495 // Find Level 1 Header that matches. 496 for( $i=$start ; $i < $inCount ; $i++ ) { 497 498 $instruction = $section[$i]; 499 $end = $i; // Or it will be lost and a section close will be missing. 500 501 // First Level Header 502 if ( $instruction[0] == 'header' && $instruction[1][1] == $level ) { 503 504 if ( $currentSlice > 0 ) { 505 return true; 506 } 507 508 if ( $headerName == null || ( $headerName == $instruction[1][0] ) ) { 509 // Begin of new slice ... 510 $start = $currentSlice = $i; 511 } 512 } 513 } 514 515 // Nothing found 516 $end = $i; // Or it will be lost and a section close will be missing. 517 return $currentSlice > 0; 518 } 519 520 function _cleanInstructions(&$instructions, $tag) { 521 522 $inCount = count($instructions); 523 for( $i=0 ; $i < $inCount ; $i++ ) { 524 525 // Last instruction 526 if ( $i == $inCount-1 ) { 527 break; 528 } 529 530 if ( preg_match($tag, $instructions[$i][0]) && preg_match($tag, $instructions[$i+1][0]) && $instructions[$i][0] != $instructions[$i+1][0] ) { 531 532 // found different tags, but both match the expression and follow each other - so they can be elliminated 533 array_splice($instructions, $i, 2); 534 $inCount -= 2; 535 $i--; 536 } 537 538 } 539 } 540 541 /** 542 * Remove TOC, section edit buttons and tags 543 */ 544 function _cleanXHTML($xhtml){ 545 $replace = array( 546 '!<div class="toc">.*?(</div>\n</div>)!s' => '', // remove TOCs 547 '#<!-- SECTION \[(\d*-\d*)\] -->#e' => '', // remove section edit buttons 548 '!<div id="tags">.*?(</div>)!s' => '' // remove category tags 549 ); 550 $xhtml = preg_replace(array_keys($replace), array_values($replace), $xhtml); 551 return $xhtml; 552 } 553 554 555 /** 556 * Allow the plugin to prevent DokuWiki creating a second instance of itself 557 * 558 * @return bool true if the plugin can not be instantiated more than once 559 */ 560 function isSingleton() { 561 return true; 562 } 563} 564// vim:ts=4:sw=4:et:enc=utf-8: 565