xref: /plugin/siteexport/inc/javahelp.php (revision a78c49a01a03138f9d9920f80ad45e644f045899)
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
36        for ($i = 0; $i < count($data); $i++)
37        {
38            $lang = '';
39            if ($this->translation)
40            {
41                $this->translation->translationsNs = $this->translation->setupTNS($data[$i]['id']);
42                $lang = $this->translation->getLangPart($data[$i]['id']);
43                $this->functions->debug->message("Setting up translation:", array(
44                    'id' => $data[$i]['id'],
45                    'tns' => $this->translation->translationsNs,
46                    'lang' => $lang
47                ), 3);
48            }
49
50            // get all the relative URLs
51            $translationHSFiles[$lang][] = $data[$i];
52        }
53
54        $toc = new siteexport_toc($this->functions, $this->NS);
55        // +":" at the end becaus this is already a namespace
56        $baseNameSpace = str_replace('/', ':', $this->translation && !empty($this->translation->translationsNs) ? $this->translation->translationsNs : $this->NS . ':');
57        $translationRoot = curNS($baseNameSpace);
58        $hsPrename = curNS(getNS($baseNameSpace));
59
60        $this->functions->debug->message("HelpSetPre-Name: {$hsPrename}", null, 3);
61        $this->functions->debug->message("Translation-Root: {$translationRoot}", null, 3);
62        $this->functions->debug->message("HSFiles:", $translationHSFiles, 1);
63
64        $check = array();
65        $last_key = end(array_keys($translationHSFiles));
66
67        foreach ($translationHSFiles as $lang => $data)
68        {
69            // Prepare Translations
70            if (!empty($lang) && !$this->functions->settings->TOCMapWithoutTranslation)
71            {
72                $toc->translation = &$this->translation;
73                $rootNode = cleanID($this->translation->translationsNs . $lang) . ':';
74            } else {
75                $toc->translation = null;
76                $rootNode = '';
77            }
78
79            $tsRootPath = $hsPrename . '/' . $this->translationRootPath($translationRoot);
80            $this->functions->debug->message("Generating JavaHelpDocZip for language '$lang'", $tsRootPath, 3);
81
82            // Create toc and map for each lang
83            list($tocData, $mapData, $startPageID) = $toc->__getJavaHelpTOCXML($data);
84            $this->filewriter->__moveDataToZip($tocData, $tsRootPath . (empty($lang) ? '' : $lang . '/') . $this->tocName);
85            $this->filewriter->__moveDataToZip($mapData, $tsRootPath . (empty($lang) ? '' : $lang . '/') . $this->mapName);
86
87            // Create HS File
88            // array_shift($toc->getMapID($rootNode, &$check))
89            $HS = $this->getHSXML($startPageID, $this->functions->getSiteTitle($rootNode), $lang, $tsRootPath);
90            $this->filewriter->__moveDataToZip($HS, $translationRoot . (empty($lang) ? '' : '_' . $lang) . '.hs');
91
92            // Default Lang
93            if ($lang == $this->functions->settings->defaultLang || $lang == $last_key)
94            {
95                $this->functions->debug->message("Writing Default HS File for Language:", $lang, 3);
96                $this->filewriter->__moveDataToZip($HS, $translationRoot . '.hs');
97                $last_key = null;
98            }
99        }
100
101        $toc->debug("THE END", true);
102    }
103
104    private function translationRootPath($translationRoot = '')
105    {
106        if (!empty($translationRoot))
107        {
108            return $translationRoot . '/';
109        }
110
111        return $translationRoot;
112    }
113
114    private function getHSXML($rootID, $title, $lang = '', $translationRoot = '')
115    {
116        if (empty($lang) && substr($translationRoot, -1) != '/') {
117            $translationRoot .= '/';
118        } else if (!empty($lang) && substr($lang, -1) != '/') {
119            $lang .= '/';
120        }
121
122        return <<<OUTPUT
123<?xml version='1.0' encoding='ISO-8859-1' ?>
124<helpset version="1.0">
125
126    <title>{$title}</title>
127    <maps>
128        <homeID>{$rootID}</homeID>
129        <mapref location="{$translationRoot}{$lang}{$this->mapName}"/>
130     </maps>
131
132    <view>
133        <name>TOC</name>
134        <label>{$this->functions->getLang('toc')}</label>
135        <type>javax.help.TOCView</type>
136        <data>{$translationRoot}{$lang}{$this->tocName}</data>
137    </view>
138
139    <view>
140        <name>Search</name>
141        <label>{$this->functions->getLang('search')}</label>
142        <type>javax.help.SearchView</type>
143        <data engine="com.sun.java.help.search.DefaultSearchEngine">
144            {$translationRoot}{$lang}JavaHelpSearch
145        </data>
146    </view>
147
148    <impl>
149        <helpsetregistry helpbrokerclass="javax.help.DefaultHelpBroker" />
150        <viewerregistry viewertype="text/html" viewerclass="com.inet.html.InetHtmlEditorKit" />
151    </impl>
152</helpset>
153OUTPUT;
154    }
155}
156