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->trans : 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, true); 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 $DATA[noNS($elemToAdd['id'])] = $elemToAdd; 147 return; 148 } 149 150 $currentLevel = array_shift($currentNSArray); 151 $nextLevel = &$DATA[$currentLevel]; 152 if (empty($nextLevel)) { 153 $nextLevel = array('pages' => array()); 154 } else { 155 $nextLevel = &$DATA[$currentLevel]['pages']; 156 } 157 158 $this->__buildTOCTree($nextLevel, $currentNSArray, $elemToAdd); 159 } 160 161 /** 162 * Create a single TOC Item 163 **/ 164 private function __TOCItem($item, $depth, $selfClosed = true) 165 { 166 $this->debug($item); 167 $targetID = $item['mapID'][0]; 168 if (empty($targetID)) { 169 $targetID = strtolower($item['name']); 170 } 171 return "\n" . str_repeat("\t", max($depth, 0)+1) . "<tocitem target=\"$targetID\"" . (intval($item['exists']) == 1 ? " text=\"" . $item['name'] . "\"" : "") . ($selfClosed ? '/' : '') . ">"; 172 } 173 174 /** 175 * Create a single TOC Item 176 **/ 177 private function __TOCItemClose($depth) 178 { 179 return "\n" . str_repeat("\t", max($depth, 0)+1) . "</tocitem>"; 180 } 181 182 /** 183 * Write the whole TOC TREE 184 **/ 185 private function __writeTOCTree($CURRENTNODE, $CURRENTNODENAME = null, $DEPTH = 0) { 186 global $conf; 187 188 $XML = ''; 189 $didOpenItem = false; 190 if (!is_array($CURRENTNODE) || empty($CURRENTNODE)) 191 { 192 // errr … no. 193 return $XML; 194 } 195 196 // This is an element! 197 if (!empty($CURRENTNODE['id']) && empty($CURRENTNODE['pages'])) 198 { 199 // This has to be an item - only -! 200 return $this->__TOCItem($CURRENTNODE, $DEPTH); 201 } 202 203 // Look for start page 204 if (!empty($CURRENTNODE[$conf['start']])) 205 { 206 // YAY! StartPage found. 207 $didOpenItem = !(count(empty($CURRENTNODE['pages']) ? $CURRENTNODE : $CURRENTNODE['pages']) == 0); 208 $XML .= $this->__TOCItem($CURRENTNODE[$conf['start']], $DEPTH, !$didOpenItem); 209 unset($CURRENTNODE[$conf['start']]); 210 } else if (!empty($CURRENTNODE['element'])) { 211 $didOpenItem = !(count($CURRENTNODE['pages']) == 0); 212 $XML .= $this->__TOCItem($CURRENTNODE['element'], $DEPTH, !$didOpenItem); 213 unset($CURRENTNODE['element']); 214 } else if ($CURRENTNODENAME != null) { 215 // We have a parent node for what is comming … lets honor that 216 $didOpenItem = !(count($CURRENTNODE) == 0); 217 $XML .= $this->__TOCItem(array('name' => ucwords($CURRENTNODENAME)), $DEPTH, !$didOpenItem); 218 } else { 219 // Woohoo … empty node? do not count up! 220 $DEPTH--; 221 } 222 223 // Circle through the entries 224 foreach (empty($CURRENTNODE['pages']) ? $CURRENTNODE : $CURRENTNODE['pages'] as $NODENAME => $ELEM) 225 { 226 // a node should have more than only one entry … otherwise we will not tell our name! 227 $XML .= $this->__writeTOCTree($ELEM, count($ELEM) >= 1 ? ( !empty($ELEM['name']) ? $ELEM['name'] : $NODENAME ) : null, $DEPTH+1); 228 } 229 230 // Close and return 231 return $XML . ($didOpenItem ? $this->__TOCItemClose($DEPTH) : ''); 232 } 233 234 235 function post(&$value, $key, array $additional) { 236 $inner_glue = $additional[0]; 237 $prefix = isset($additional[1]) ? $additional[1] : false; 238 if ($prefix === false) $prefix = $key; 239 240 $value = $value . $inner_glue . $prefix; 241 } 242 243 /** 244 * internal Sort function 245 * @param unknown_type $a 246 * @param unknown_type $b 247 */ 248 private function sortFunction($a, $b) 249 { 250 $idA = $a['id']; 251 $idB = $b['id']; 252 253 $depthA = explode(':', getNS($idA)); 254 $depthB = explode(':', getNS($idB)); 255 256 for ($i = 0; $i < min(count($depthA), count($depthB)); $i++) 257 { 258 $NSCMP = strcmp($depthA[$i], $depthB[$i]); 259 if ($NSCMP != 0) 260 { 261 // Something is different! 262 return $NSCMP; 263 } 264 } 265 266 // There is mor in B, than in A! 267 if (count($depthA) < count($depthB)) 268 { 269 return -1; 270 } else if (count($depthA) > count($depthB)) 271 { 272 // there is more in A than in B 273 return 1; 274 } 275 276 if ($NSCMP == 0) 277 { 278 // Something is different! 279 return strcmp(noNS($idA), noNS($idB)); 280 } 281 282 return 0; 283 } 284 285 /** 286 * Build the Eclipse Documentation TOC XML 287 **/ 288 public function __getTOCXML($DATA, $XML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<?NLS TYPE=\"org.eclipse.help.toc\"?>\n") { 289 290 $pagesArray = array(); 291 292 // Go through the pages 293 foreach ($DATA as $elem) { 294 295 $site = $elem['id']; 296 $elems = explode('/', $this->functions->getSiteName($site)); 297 298 // Strip Site 299 array_pop($elems); 300 301 // build the topic Tree 302 $this->__buildTopicTree($pagesArray, $elems, $site); 303 } 304 305 $XML .= $this->__addXMLTopic($pagesArray, 'toc'); 306 307 return $XML; 308 309 } 310 311 /** 312 * Load the topic Tree for the TOC - recursive 313 **/ 314 private function __buildTopicTree(&$PAGES, $DATA, $SITE, $INSERTDATA = null) { 315 316 if (empty($DATA) || !is_array($DATA)) { 317 318 if ($INSERTDATA == null) 319 { 320 $INSERTDATA = $SITE; 321 } 322 323 // This is already a namespace 324 if (is_array($PAGES[noNS($SITE)])) { 325 // The root already exists! 326 if (!empty($PAGES[noNS($SITE)][noNS($SITE)])) { 327 if (strstr($PAGES[noNS($SITE)][noNS($SITE)], $SITE)) { 328 // The SITE is in the parent Namespace, and the current Namespace has an index with same name 329 $PAGES['__' . noNS($SITE)] = $INSERTDATA; 330 } else { 331 $PAGES['__' . noNS($SITE)] = $PAGES[noNS($SITE)][noNS($SITE)]; 332 $PAGES[noNS($SITE)][noNS($SITE)] = $INSERTDATA; 333 } 334 } else { 335 $PAGES[noNS($SITE)][noNS($SITE)] = $INSERTDATA; 336 } 337 } else { 338 // just a Page 339 $PAGES[noNS($SITE)] = $INSERTDATA; 340 } 341 return; 342 } 343 344 $NS = array_shift($DATA); 345 if (!is_array($PAGES[$NS])) $PAGES[$NS] = empty($PAGES[$NS]) ? array() : array($PAGES[$NS]); 346 $this->__buildTopicTree($PAGES[$NS], $DATA, $SITE, $INSERTDATA); 347 348 return; 349 } 350 351 /** 352 * Build the Topic Tree for TOC.xml 353 **/ 354 private function __addXMLTopic($DATA, $ITEM = 'topic', $LEVEL = 0, $NODENAME = '') { 355 global $conf; 356 357 $DEPTH = str_repeat("\t", $LEVEL); 358 359 if (!is_array($DATA)) { 360 return $DEPTH . '<' . $ITEM . ' label="' . $this->functions->getSiteTitle($DATA) . '" ' . ($ITEM != 'topic' ? 'topic' : 'href') . '="' . $this->functions->getSiteName($DATA) . "\" />\n"; 361 } 362 // Is array from this point on 363 list($indexTitle, $indexFile) = $this->__getIndexItem($DATA, $NODENAME); 364 365 if (empty($indexTitle)) $indexTitle = $this->functions->getSiteTitle($conf['start']); 366 if (!empty($indexFile)) $indexFile = ($ITEM != 'topic' ? 'topic' : 'href') . "=\"$indexFile\""; 367 368 $isEmptyNode = count($DATA) == 1 && empty($indexFile); 369 370 if (!$isEmptyNode && ($this->emptyNSToc || count($DATA) > 0)) 371 $XML = "$DEPTH<$ITEM label=\"$indexTitle\" $indexFile>"; 372 373 if (!$isEmptyNode && count($DATA) > 0) $XML .= "\n"; 374 375 foreach ($DATA as $NODENAME => $NS) { 376 377 $XML .= $this->__addXMLTopic($NS, (!($this->emptyNSToc || count($DATA) > 1) && $ITEM != 'topic' ? $ITEM : 'topic'), $LEVEL+(!$isEmptyNode ? 1 : 0), $NODENAME); 378 379 } 380 381 if (!$isEmptyNode && count($DATA) > 0) $XML .= "$DEPTH"; 382 if (!$isEmptyNode && ($this->emptyNSToc || count($DATA) > 0)) { 383 $XML .= "</$ITEM>\n"; 384 } 385 386 return $XML; 387 } 388 389 390 /** 391 * Get the context XML 392 **/ 393 public function __getContextXML($DATA) { 394 395 $XML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<?NLS TYPE=\"org.eclipse.help.context\"?>\n<contexts>\n"; 396 397 $check = array(); 398 foreach ($DATA as $elem) 399 { 400 $ID = $elem['id']; 401 $meta = p_get_metadata($ID, 'context', true); 402 if (empty($meta['id'])) { continue; } 403 404 $TITLE = empty($meta['title']) ? $this->functions->getSiteTitle($ID) : $meta['title']; 405 406 // support more than one view IDs ... for more than one reference 407 $VIEWIDs = $this->functions->getMapID($elem['id'], $elem['anchor'], $check); 408 409 $DESCRIPTION = $this->functions->xmlEntities(p_get_metadata($ID, 'description abstract')); 410 411 // Build topic Links 412 $url = $this->functions->getSiteName($ID); 413 $this->shortenByTranslation($url); 414 415 $TOPICS = array($url => $TITLE . " (Details)"); 416 $REFS = p_get_metadata($ID, 'relation references', true); 417 if (is_array($REFS)) 418 foreach ($REFS as $REL => $EXISTS) { 419 if (!$EXISTS) { continue; } 420 $TOPICS[$this->functions->getSiteName($REL)] = $this->functions->getSiteTitle($REL); 421 } 422 423 // build XML - include multi view IDs 424 foreach ($VIEWIDs as $VIEWID) { 425 $XML .= "\t<context id=\"$VIEWID\" title=\"$TITLE\">\n"; 426 $XML .= "\t\t<description>$DESCRIPTION</description>\n"; 427 428 foreach ($TOPICS as $URL => $LABEL) { 429 $XML .= "\t\t<topic label=\"$LABEL\" href=\"$URL\" />\n"; 430 } 431 432 $XML .= "\t</context>\n"; 433 } 434 } 435 436 $XML .= "</contexts>"; 437 return $XML; 438 439 } 440 441 /** 442 * Determine if this is an index - and if so, find its Title 443 **/ 444 private function __getIndexItem(&$DATA, $NODENAME = '') { 445 global $conf; 446 447 if (!is_array($DATA)) { return; } 448 449 $indexTitle = ''; 450 $indexFile = ''; 451 foreach ($DATA as $NODE => $indexSearch) { 452 // Skip next Namespaces 453 if (is_array($indexSearch)) { continue; } 454 455 // Skip if this is not a start 456 if ($NODE != $conf['start']) { continue; } 457 458 $indexTitle = $this->functions->getSiteTitle($indexSearch); 459 $indexFile = $indexSearch; 460 unset($DATA[$NODE]); 461 break; 462 } 463 464 if (empty($indexFile) && !empty($DATA[$NODENAME])) { 465 $indexTitle = $this->functions->getSiteTitle($DATA[$NODENAME]); 466 $indexFile = $DATA[$NODENAME]; 467 unset($DATA[$NODENAME]); 468 } 469 470 return array($indexTitle, $this->functions->getSiteName($indexFile)); 471 } 472 473 private $doDebug = false; 474 private static $didDebug = false; 475 private function debug($data, $final = false) { 476 if ( ! $this->doDebug ) { return; } 477 478 if ( !$this->didDebug ) { 479 print "<html><pre>"; 480 $this->didDebug = true; 481 } 482 483 if ( is_array($data) ) { 484 print_r($data); 485 } else { 486 print str_replace("<", "<", str_replace(">", ">", $data));; 487 } 488 489 print "\n\n"; 490 491 if ( $final ) { 492 print "</pre></html>"; 493 exit; 494 } 495 } 496} 497