xref: /plugin/siteexport/inc/javahelp.php (revision a8c17ab5b37308343f86651acb8c4a1b3f36f0ae)
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 = count($data); $i >= 0; $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        $last_key = end((array_keys($translationHSFiles)));
65
66        foreach ($translationHSFiles as $lang => $data)
67        {
68            // Prepare Translations
69            if (!empty($lang) && !$this->functions->settings->TOCMapWithoutTranslation)
70            {
71                $toc->translation = &$this->translation;
72                $rootNode = cleanID($this->translation->translationsNs . $lang) . ':';
73            } else {
74                $toc->translation = null;
75                $rootNode = '';
76            }
77
78            $tsRootPath = $hsPrename . '/' . $this->translationRootPath($translationRoot);
79            $this->functions->debug->message("Generating JavaHelpDocZip for language '$lang'", $tsRootPath, 3);
80
81            // Create toc and map for each lang
82            list($tocData, $mapData, $startPageID) = $toc->__getJavaHelpTOCXML($data);
83            $this->filewriter->__moveDataToZip($tocData, $tsRootPath . (empty($lang) ? '' : $lang . '/') . $this->tocName);
84            $this->filewriter->__moveDataToZip($mapData, $tsRootPath . (empty($lang) ? '' : $lang . '/') . $this->mapName);
85
86            // Create HS File
87            $HS = $this->getHSXML($startPageID, $this->functions->getSiteTitle($rootNode), $lang, $tsRootPath);
88            $this->filewriter->__moveDataToZip($HS, $translationRoot . (empty($lang) ? '' : '_' . $lang) . '.hs');
89
90            // Default Lang
91            if ($lang == $this->functions->settings->defaultLang || $lang == $last_key)
92            {
93                $this->functions->debug->message("Writing Default HS File for Language:", $lang, 3);
94                $this->filewriter->__moveDataToZip($HS, $translationRoot . '.hs');
95                $last_key = null;
96            }
97        }
98
99        $toc->debug("THE END", true);
100    }
101
102    private function translationRootPath($translationRoot = '')
103    {
104        if (!empty($translationRoot))
105        {
106            return $translationRoot . '/';
107        }
108
109        return $translationRoot;
110    }
111
112    private function getHSXML($rootID, $title, $lang = '', $translationRoot = '')
113    {
114        if (empty($lang) && substr($translationRoot, -1) != '/') {
115            $translationRoot .= '/';
116        } else if (!empty($lang) && substr($lang, -1) != '/') {
117            $lang .= '/';
118        }
119
120        return <<<OUTPUT
121<?xml version='1.0' encoding='ISO-8859-1' ?>
122<helpset version="1.0">
123
124    <title>{$title}</title>
125    <maps>
126        <homeID>{$rootID}</homeID>
127        <mapref location="{$translationRoot}{$lang}{$this->mapName}"/>
128     </maps>
129
130    <view>
131        <name>TOC</name>
132        <label>{$this->functions->getLang('toc')}</label>
133        <type>javax.help.TOCView</type>
134        <data>{$translationRoot}{$lang}{$this->tocName}</data>
135    </view>
136
137    <view>
138        <name>Search</name>
139        <label>{$this->functions->getLang('search')}</label>
140        <type>javax.help.SearchView</type>
141        <data engine="com.sun.java.help.search.DefaultSearchEngine">
142            {$translationRoot}{$lang}JavaHelpSearch
143        </data>
144    </view>
145
146    <impl>
147        <helpsetregistry helpbrokerclass="javax.help.DefaultHelpBroker" />
148        <viewerregistry viewertype="text/html" viewerclass="com.inet.html.InetHtmlEditorKit" />
149    </impl>
150</helpset>
151OUTPUT;
152    }
153}
154