1<?php 2 3if (!defined('DOKU_PLUGIN')) die('meh'); 4 5class siteexport_toc 6{ 7 private $emptyNSToc = true; 8 private $functions = null; 9 private $NS = null; 10 public $translation = null; 11 12 public function __construct($functions, $NS) 13 { 14 $this->doDebug = !empty($_REQUEST['tocDebug']); 15 $this->emptyNSToc = !empty($_REQUEST['emptyTocElem']); 16 $this->functions = $functions; 17 $this->NS = $NS; 18 } 19 20 private function shortenByTranslation(&$inputURL, $deepSearch = false) 21 { 22 // Mandatory: we allways want '/' insteadf of ':' here 23 $inputURL = str_replace(':', '/', $inputURL); 24 $checkArray = $this->translation ? $this->translation->translations : array(array_pop(explode(':', $this->NS))); 25 26 $url = explode('/', $inputURL); 27 28 for ($i = 0; $i < count($url); $i++) 29 { 30 if (in_array($url[$i], $checkArray)) 31 { 32 // Rauswerfen und weg 33 $url[$i] = ''; 34 break; 35 } 36 37 if (!$deepSearch) 38 { 39 break; 40 } 41 42 // Ok, remove anyway 43 $url[$i] = ''; 44 } 45 46 $inputURL = implode('/', $url); 47 $inputURL = preg_replace("$\/+$", "/", $inputURL); 48 49 if (strlen($inputURL) > 0 && substr($inputURL, 0, 1) == '/') 50 { 51 $inputURL = substr($inputURL, 1); 52 } 53 54 return $inputURL; 55 } 56 57 /** 58 * Build the Java Documentation TOC XML 59 **/ 60 public function __getJavaHelpTOCXML($DATA) { 61 62 if (count($DATA) == 0) { 63 return false; 64 } 65 66 $TOCXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<toc>"; 67 $MAPXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<map version=\"1.0\">"; 68 // usort($DATA, array($this, 'sortFunction')); 69 70 // Go through the pages 71 $CHECKDATA = array(); 72 $nData = $DATA; 73 $DATA = array(); 74 $check = array(); 75 $startPageID = null; 76 77 foreach ( $nData as $elem ) 78 { 79 // Check if available 80 $anchor = ( !empty($elem['anchor']) ? '#' . $elem['anchor'] : '' ); 81 $elem['url'] = $this->functions->getSiteName($elem['id'], true); // Override - we need a clean name 82 $elem['mapURL'] = $elem['url']; 83 $this->shortenByTranslation($elem['url']); 84 85 // only add an url once 86 if ( in_array($elem['url'], $CHECKDATA) ) { continue; } 87 88 if ( !isset($elem['exists']) ) { 89 resolve_pageid(getNS($elem['id']),$elem['id'],$elem['exists']); 90 $this->functions->debug->message("EXISTS previously not set.", $elem, 1); 91 } 92 93 // if not there, no map ids will be generated 94 $elem['mapID'] = intval($elem['exists']) == 1 ? $this->functions->getMapID($elem['id'], $elem['anchor'], $check) : array(); 95 96 if ( empty($elem['depth']) ) { 97 $elem['depth'] = count(explode('/', $elem['url'])); 98 } 99 $CHECKDATA[] = $elem['url']; 100 101 if ( $startPageID == null ) 102 { 103 $startPageID = $elem['mapID'][0]; 104 } 105 106 if ( empty( $elem['name'] ) || $elem['name'] == noNs($elem['id']) ) { 107 $elem['name'] = $this->functions->getSiteTitle($elem['id']); 108 $this->debug($elem); 109 } 110 111 // Go on building mapXML 112 $this->shortenByTranslation($elem['mapURL'], true); // true to already remove all language stuff - false if not 113 foreach ( $elem['mapID'] as $VIEWID ) { 114 $MAPXML .= "\n\t<mapID target=\"$VIEWID\" url=\"" . $elem['mapURL'] . $anchor . "\"/>"; 115 } 116 117 $elem['tocNS'] = getNS(cleanID($elem['url'])); 118 $elem['tocNS'] = $this->shortenByTranslation($elem['tocNS'], true); 119 $elem['tocNS'] = strlen($elem['tocNS']) > 0 ? explode('/', $elem['tocNS']) : array(); 120 $this->functions->debug->message("This will be the TOC elements data:", $elem, 1); 121 122 $this->__buildTOCTree($DATA, $elem['tocNS'], $elem); 123 } 124 125 $this->debug("#### Writing TOC Tree ####"); 126 $TOCXML .= $this->__writeTOCTree($DATA) . "\n</toc>"; 127 $this->debug("#### DONE: Writing TOC Tree ####"); 128 $MAPXML .= "\n</map>"; 129 130 $this->debug($DATA); 131 $this->debug($TOCXML); 132 $this->debug($MAPXML); 133 134 return array($TOCXML, $MAPXML, $startPageID); 135 } 136 137 /** 138 * Prepare the TOC Tree 139 **/ 140 private function __buildTOCTree(&$DATA, $currentNSArray, $elemToAdd) 141 { 142 global $conf; 143 144 // Actual level 145 if (empty($currentNSArray)) { 146 $elemToAdd['isStartPage'] = noNS($elemToAdd['id']) == $conf['start']; 147 $key = empty($elemToAdd['name']) ? noNS($elemToAdd['id']) : $elemToAdd['name']; 148 $DATA[$key] = $elemToAdd; 149 return; 150 } 151 152 $currentLevel = array_shift($currentNSArray); 153 $nextLevel = &$DATA[$currentLevel]; 154 if (empty($nextLevel)) { 155 $nextLevel = array('pages' => array()); 156 } else { 157 $nextLevel = &$DATA[$currentLevel]['pages']; 158 } 159 160 $this->__buildTOCTree($nextLevel, $currentNSArray, $elemToAdd); 161 } 162 163 /** 164 * Create a single TOC Item 165 **/ 166 private function __TOCItem($item, $depth, $selfClosed = true) 167 { 168 $this->debug($item); 169 $targetID = $item['mapID'][0]; 170 if (empty($targetID)) { 171 $targetID = strtolower($item['name']); 172 } 173 return "\n" . str_repeat("\t", max($depth, 0)+1) . "<tocitem target=\"$targetID\"" . (intval($item['exists']) == 1 ? " text=\"" . $item['name'] . "\"" : "") . ($selfClosed ? '/' : '') . ">"; 174 } 175 176 /** 177 * Create a single TOC Item 178 **/ 179 private function __TOCItemClose($depth) 180 { 181 return "\n" . str_repeat("\t", max($depth, 0)+1) . "</tocitem>"; 182 } 183 184 /** 185 * Write the whole TOC TREE 186 **/ 187 private function __writeTOCTree($CURRENTNODE, $CURRENTNODENAME = null, $DEPTH = 0) { 188 global $conf; 189 190 $XML = ''; 191 $didOpenItem = false; 192 if (!is_array($CURRENTNODE) || empty($CURRENTNODE)) 193 { 194 // errr … no. 195 return $XML; 196 } 197 198 // This is an element! 199 if (!empty($CURRENTNODE['id']) && empty($CURRENTNODE['pages'])) 200 { 201 // This has to be an item - only -! 202 return $this->__TOCItem($CURRENTNODE, $DEPTH); 203 } 204 205 // Look for start page 206 if (!empty($CURRENTNODE[$conf['start']])) 207 { 208 // YAY! StartPage found. 209 $didOpenItem = !(count(empty($CURRENTNODE['pages']) ? $CURRENTNODE : $CURRENTNODE['pages']) == 0); 210 $XML .= $this->__TOCItem($CURRENTNODE[$conf['start']], $DEPTH, !$didOpenItem); 211 unset($CURRENTNODE[$conf['start']]); 212 } else if (!empty($CURRENTNODE['element'])) { 213 $didOpenItem = !(count($CURRENTNODE['pages']) == 0); 214 $XML .= $this->__TOCItem($CURRENTNODE['element'], $DEPTH, !$didOpenItem); 215 unset($CURRENTNODE['element']); 216 } else if ($CURRENTNODENAME != null) { 217 // We have a parent node for what is comming … lets honor that 218 $didOpenItem = !(count($CURRENTNODE) == 0); 219 $XML .= $this->__TOCItem(array('name' => ucwords($CURRENTNODENAME)), $DEPTH, !$didOpenItem); 220 } else { 221 // Woohoo … empty node? do not count up! 222 $DEPTH--; 223 } 224 225 $this->debug("-- This is the current node --"); 226 $this->debug($CURRENTNODE); 227 228 // Circle through the entries 229 foreach (empty($CURRENTNODE['pages']) ? $CURRENTNODE : $CURRENTNODE['pages'] as $NODENAME => $ELEM) 230 { 231 // a node should have more than only one entry … otherwise we will not tell our name! 232 $XML .= $this->__writeTOCTree($ELEM, count($ELEM) >= 1 ? ( !empty($ELEM['name']) ? $ELEM['name'] : $NODENAME ) : null, $DEPTH+1); 233 } 234 235 // Close and return 236 return $XML . ($didOpenItem ? $this->__TOCItemClose($DEPTH) : ''); 237 } 238 239 240 function post(&$value, $key, array $additional) { 241 $inner_glue = $additional[0]; 242 $prefix = isset($additional[1]) ? $additional[1] : false; 243 if ($prefix === false) $prefix = $key; 244 245 $value = $value . $inner_glue . $prefix; 246 } 247 248 /** 249 * internal Sort function 250 * @param unknown_type $a 251 * @param unknown_type $b 252 */ 253 private function sortFunction($a, $b) 254 { 255 $idA = $a['id']; 256 $idB = $b['id']; 257 258 $depthA = explode(':', getNS($idA)); 259 $depthB = explode(':', getNS($idB)); 260 261 for ($i = 0; $i < min(count($depthA), count($depthB)); $i++) 262 { 263 $NSCMP = strcmp($depthA[$i], $depthB[$i]); 264 if ($NSCMP != 0) 265 { 266 // Something is different! 267 return $NSCMP; 268 } 269 } 270 271 // There is mor in B, than in A! 272 if (count($depthA) < count($depthB)) 273 { 274 return -1; 275 } else if (count($depthA) > count($depthB)) 276 { 277 // there is more in A than in B 278 return 1; 279 } 280 281 if ($NSCMP == 0) 282 { 283 // Something is different! 284 return strcmp(noNS($idA), noNS($idB)); 285 } 286 287 return 0; 288 } 289 290 /** 291 * Build the Eclipse Documentation TOC XML 292 **/ 293 public function __getTOCXML($DATA, $XML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<?NLS TYPE=\"org.eclipse.help.toc\"?>\n") { 294 295 $pagesArray = array(); 296 297 // Go through the pages 298 foreach ($DATA as $elem) { 299 300 $site = $elem['id']; 301 $elems = explode('/', $this->functions->getSiteName($site)); 302 303 // Strip Site 304 array_pop($elems); 305 306 // build the topic Tree 307 $this->__buildTopicTree($pagesArray, $elems, $site); 308 } 309 310 $XML .= $this->__addXMLTopic($pagesArray, 'toc'); 311 312 return $XML; 313 314 } 315 316 /** 317 * Load the topic Tree for the TOC - recursive 318 **/ 319 private function __buildTopicTree(&$PAGES, $DATA, $SITE, $INSERTDATA = null) { 320 321 if (empty($DATA) || !is_array($DATA)) { 322 323 if ($INSERTDATA == null) 324 { 325 $INSERTDATA = $SITE; 326 } 327 328 // This is already a namespace 329 if (is_array($PAGES[noNS($SITE)])) { 330 // The root already exists! 331 if (!empty($PAGES[noNS($SITE)][noNS($SITE)])) { 332 if (strstr($PAGES[noNS($SITE)][noNS($SITE)], $SITE)) { 333 // The SITE is in the parent Namespace, and the current Namespace has an index with same name 334 $PAGES['__' . noNS($SITE)] = $INSERTDATA; 335 } else { 336 $PAGES['__' . noNS($SITE)] = $PAGES[noNS($SITE)][noNS($SITE)]; 337 $PAGES[noNS($SITE)][noNS($SITE)] = $INSERTDATA; 338 } 339 } else { 340 $PAGES[noNS($SITE)][noNS($SITE)] = $INSERTDATA; 341 } 342 } else { 343 // just a Page 344 $PAGES[noNS($SITE)] = $INSERTDATA; 345 } 346 return; 347 } 348 349 $NS = array_shift($DATA); 350 if (!is_array($PAGES[$NS])) $PAGES[$NS] = empty($PAGES[$NS]) ? array() : array($PAGES[$NS]); 351 $this->__buildTopicTree($PAGES[$NS], $DATA, $SITE, $INSERTDATA); 352 353 return; 354 } 355 356 /** 357 * Build the Topic Tree for TOC.xml 358 **/ 359 private function __addXMLTopic($DATA, $ITEM = 'topic', $LEVEL = 0, $NODENAME = '') { 360 global $conf; 361 362 $DEPTH = str_repeat("\t", $LEVEL); 363 364 if (!is_array($DATA)) { 365 return $DEPTH . '<' . $ITEM . ' label="' . $this->functions->getSiteTitle($DATA) . '" ' . ($ITEM != 'topic' ? 'topic' : 'href') . '="' . $this->functions->getSiteName($DATA) . "\" />\n"; 366 } 367 // Is array from this point on 368 list($indexTitle, $indexFile) = $this->__getIndexItem($DATA, $NODENAME); 369 370 if (empty($indexTitle)) $indexTitle = $this->functions->getSiteTitle($conf['start']); 371 if (!empty($indexFile)) $indexFile = ($ITEM != 'topic' ? 'topic' : 'href') . "=\"$indexFile\""; 372 373 $isEmptyNode = count($DATA) == 1 && empty($indexFile); 374 375 if (!$isEmptyNode && ($this->emptyNSToc || count($DATA) > 0)) 376 $XML = "$DEPTH<$ITEM label=\"$indexTitle\" $indexFile>"; 377 378 if (!$isEmptyNode && count($DATA) > 0) $XML .= "\n"; 379 380 foreach ($DATA as $NODENAME => $NS) { 381 382 $XML .= $this->__addXMLTopic($NS, (!($this->emptyNSToc || count($DATA) > 1) && $ITEM != 'topic' ? $ITEM : 'topic'), $LEVEL+(!$isEmptyNode ? 1 : 0), $NODENAME); 383 384 } 385 386 if (!$isEmptyNode && count($DATA) > 0) $XML .= "$DEPTH"; 387 if (!$isEmptyNode && ($this->emptyNSToc || count($DATA) > 0)) { 388 $XML .= "</$ITEM>\n"; 389 } 390 391 return $XML; 392 } 393 394 395 /** 396 * Get the context XML 397 **/ 398 public function __getContextXML($DATA) { 399 400 $XML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<?NLS TYPE=\"org.eclipse.help.context\"?>\n<contexts>\n"; 401 402 $check = array(); 403 foreach ($DATA as $elem) 404 { 405 $ID = $elem['id']; 406 $meta = p_get_metadata($ID, 'context', true); 407 if (empty($meta['id'])) { continue; } 408 409 $TITLE = empty($meta['title']) ? $this->functions->getSiteTitle($ID) : $meta['title']; 410 411 // support more than one view IDs ... for more than one reference 412 $VIEWIDs = $this->functions->getMapID($elem['id'], $elem['anchor'], $check); 413 414 $DESCRIPTION = $this->functions->xmlEntities(p_get_metadata($ID, 'description abstract')); 415 416 // Build topic Links 417 $url = $this->functions->getSiteName($ID); 418 $this->shortenByTranslation($url); 419 420 $TOPICS = array($url => $TITLE . " (Details)"); 421 $REFS = p_get_metadata($ID, 'relation references', true); 422 if (is_array($REFS)) 423 foreach ($REFS as $REL => $EXISTS) { 424 if (!$EXISTS) { continue; } 425 $TOPICS[$this->functions->getSiteName($REL)] = $this->functions->getSiteTitle($REL); 426 } 427 428 // build XML - include multi view IDs 429 foreach ($VIEWIDs as $VIEWID) { 430 $XML .= "\t<context id=\"$VIEWID\" title=\"$TITLE\">\n"; 431 $XML .= "\t\t<description>$DESCRIPTION</description>\n"; 432 433 foreach ($TOPICS as $URL => $LABEL) { 434 $XML .= "\t\t<topic label=\"$LABEL\" href=\"$URL\" />\n"; 435 } 436 437 $XML .= "\t</context>\n"; 438 } 439 } 440 441 $XML .= "</contexts>"; 442 return $XML; 443 444 } 445 446 /** 447 * Determine if this is an index - and if so, find its Title 448 **/ 449 private function __getIndexItem(&$DATA, $NODENAME = '') { 450 global $conf; 451 452 if (!is_array($DATA)) { return; } 453 454 $indexTitle = ''; 455 $indexFile = ''; 456 foreach ($DATA as $NODE => $indexSearch) { 457 // Skip next Namespaces 458 if (is_array($indexSearch)) { continue; } 459 460 // Skip if this is not a start 461 if ($NODE != $conf['start']) { continue; } 462 463 $indexTitle = $this->functions->getSiteTitle($indexSearch); 464 $indexFile = $indexSearch; 465 unset($DATA[$NODE]); 466 break; 467 } 468 469 if (empty($indexFile) && !empty($DATA[$NODENAME])) { 470 $indexTitle = $this->functions->getSiteTitle($DATA[$NODENAME]); 471 $indexFile = $DATA[$NODENAME]; 472 unset($DATA[$NODENAME]); 473 } 474 475 return array($indexTitle, $this->functions->getSiteName($indexFile)); 476 } 477 478 private $doDebug = false; 479 private static $didDebug = false; 480 public function debug($data, $final = false) { 481 if ( ! $this->doDebug ) { return; } 482 483 if ( !$this->didDebug ) { 484 print "<html><pre>"; 485 $this->didDebug = true; 486 } 487 488 if ( is_array($data) ) { 489 print_r($data); 490 } else { 491 print str_replace("<", "<", str_replace(">", ">", $data));; 492 } 493 494 print "\n\n"; 495 496 if ( $final ) { 497 print "</pre></html>"; 498 exit; 499 } 500 } 501} 502