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