1<?php
2
3if (!defined('DOKU_PLUGIN')) die('meh');
4require_once(DOKU_PLUGIN . 'siteexport/inc/toc.php');
5
6class siteexport_javahelp
7{
8    private $functions = null;
9    private $translation = null;
10    private $filewriter = null;
11    private $NS = null;
12
13    private $tocName = 'toc.xml';
14    private $mapName = 'map.xml';
15
16    /**
17     * @param siteexport_functions $functions
18     * @param siteexport_zipfilewriter $filewriter
19     */
20    public function __construct($functions, $filewriter, $NS)
21    {
22        $this->NS = $NS;
23        $this->functions = $functions;
24        $this->filewriter = $filewriter;
25        $translation = plugin_load('helper', 'autotranslation');
26        $this->translation = &$translation;
27    }
28
29    public function createTOCFiles($data)
30    {
31        global $conf, $ID;
32
33        // Split Tree for translation
34        $translationHSFiles = array();
35        $toc = new siteexport_toc($this->functions, $this->NS);
36        $toc->debug("### Starting to create TOC Files ###");
37
38        $count = count($data);
39        for ($i = 0; $i < $count ; $i++)
40        {
41            $lang = '';
42            if ($this->translation)
43            {
44                $this->translation->translationsNs = $this->translation->setupTNS($data[$i]['id'], true);
45                $lang = $this->translation->getLangPart($data[$i]['id']);
46                $this->functions->debug->message("Setting up translation:", array(
47                    'id' => $data[$i]['id'],
48                    'tns' => $this->translation->translationsNs,
49                    'lang' => $lang
50                ), 3);
51            }
52
53            $toc->debug($lang . " -> " . $data[$i]['id'] );
54            // get all the relative URLs
55            $translationHSFiles[$lang][] = $data[$i];
56        }
57
58        // +":" at the end becaus this is already a namespace
59        $baseNameSpace = str_replace('/', ':', $this->translation && !empty($this->translation->translationsNs) ? $this->translation->translationsNs : $this->NS . ':');
60        $translationRoot = curNS($baseNameSpace);
61        $hsPrename = curNS(getNS($baseNameSpace));
62
63        $this->functions->debug->message("HelpSetPre-Name: {$hsPrename}", null, 3);
64        $this->functions->debug->message("Translation-Root: {$translationRoot}", null, 3);
65        $this->functions->debug->message("HSFiles:", $translationHSFiles, 1);
66
67        $last_key = end((array_keys($translationHSFiles)));
68
69        foreach ($translationHSFiles as $lang => $data)
70        {
71            // Prepare Translations
72            if (!empty($lang) && !$this->functions->settings->TOCMapWithoutTranslation)
73            {
74                $toc->translation = &$this->translation;
75                $rootNode = cleanID($this->translation->translationsNs . $lang) . ':';
76            } else {
77                $toc->translation = null;
78                $rootNode = '';
79            }
80
81            $toc->debug("*** Writing for Language rootNode: '".$rootNode."'***");
82
83            $tsRootPath = $hsPrename . '/' . $this->translationRootPath($translationRoot);
84            $this->functions->debug->message("Generating JavaHelpDocZip for language '$lang'", $tsRootPath, 3);
85
86            // Create toc and map for each lang
87            list($tocData, $mapData, $startPageID) = $toc->__getJavaHelpTOCXML($data);
88            $this->filewriter->__moveDataToZip($tocData, $tsRootPath . (empty($lang) ? '' : $lang . '/') . $this->tocName);
89            $this->filewriter->__moveDataToZip($mapData, $tsRootPath . (empty($lang) ? '' : $lang . '/') . $this->mapName);
90
91            // Create HS File
92            $HS = $this->getHSXML($startPageID, $this->functions->getSiteTitle($rootNode), $lang, $tsRootPath);
93            $this->filewriter->__moveDataToZip($HS, $translationRoot . (empty($lang) ? '' : '_' . $lang) . '.hs');
94
95            // Default Lang
96            if ($lang == $this->functions->settings->defaultLang || $lang == $last_key)
97            {
98                $this->functions->debug->message("Writing Default HS File for Language:", $lang, 3);
99                $this->filewriter->__moveDataToZip($HS, $translationRoot . '.hs');
100                $last_key = null;
101            }
102        }
103
104        $toc->debug("THE END", true);
105    }
106
107    private function translationRootPath($translationRoot = '')
108    {
109        if (!empty($translationRoot))
110        {
111            return $translationRoot . '/';
112        }
113
114        return $translationRoot;
115    }
116
117    private function getHSXML($rootID, $title, $lang = '', $translationRoot = '')
118    {
119        if (empty($lang) && substr($translationRoot, -1) != '/') {
120            $translationRoot .= '/';
121        } else if (!empty($lang) && substr($lang, -1) != '/') {
122            $lang .= '/';
123        }
124
125        return <<<OUTPUT
126<?xml version='1.0' encoding='ISO-8859-1' ?>
127<helpset version="1.0">
128
129    <title>{$title}</title>
130    <maps>
131        <homeID>{$rootID}</homeID>
132        <mapref location="{$translationRoot}{$lang}{$this->mapName}"/>
133     </maps>
134
135    <view>
136        <name>TOC</name>
137        <label>{$this->functions->getLang('toc')}</label>
138        <type>javax.help.TOCView</type>
139        <data>{$translationRoot}{$lang}{$this->tocName}</data>
140    </view>
141
142    <view>
143        <name>Search</name>
144        <label>{$this->functions->getLang('search')}</label>
145        <type>javax.help.SearchView</type>
146        <data engine="com.sun.java.help.search.DefaultSearchEngine">
147            {$translationRoot}{$lang}JavaHelpSearch
148        </data>
149    </view>
150
151    <impl>
152        <helpsetregistry helpbrokerclass="javax.help.DefaultHelpBroker" />
153        <viewerregistry viewertype="text/html" viewerclass="com.inet.html.InetHtmlEditorKit" />
154    </impl>
155</helpset>
156OUTPUT;
157    }
158}
159