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