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