1<?php
2/**
3 * Siteexport Plugin helper
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12require_once(DOKU_PLUGIN.'siteexport/preload.php');
13
14class helper_plugin_siteexport_page_remove {
15    private $start, $end;
16
17    /**
18     * @param integer $start
19     * @param integer $end
20     */
21    public function __construct($start, $end=null) {
22        $this->start = $start;
23        $this->end = $end;
24    }
25
26    public function _page_remove($elem) {
27        return $elem[2] >= $this->start && ( is_null( $this->end ) || $elem[2] <= $this->end);
28    }
29}
30
31class helper_plugin_siteexport extends DokuWiki_Plugin {
32
33    /*
34     * return all the templates that this wiki has
35     */
36    public function __getTemplates() {
37
38        // populate $this->_choices with a list of directories
39        $list = array();
40
41        $_dir = DOKU_INC . 'lib/tpl/';
42        $_pattern = '/^[\w-]+$/';
43        if ($dh = @opendir($_dir)) {
44            while (false !== ($entry = readdir($dh))) {
45                if ($entry == '.' || $entry == '..') {
46                    continue;
47                }
48                if ($entry == '.' || $entry == '..') {
49                    continue;
50                }
51                if ($_pattern && !preg_match($_pattern,$entry)) {
52                    continue;
53                }
54
55                $file = (is_link($_dir.$entry)) ? readlink($_dir.$entry) : $entry;
56                if (is_dir($_dir.$file)) {
57                    $list[] = $entry;
58                }
59            }
60            closedir($dh);
61        }
62
63
64        sort($list);
65        return $list;
66    }
67
68    /*
69     * Return array list of plugins that exist
70     */
71    public function __getPluginList() {
72        global $plugin_controller;
73
74        $allPlugins = array();
75        foreach ($plugin_controller->getList(null, true) as $plugin) { // All plugins
76            // check for CSS or JS
77            if (!file_exists(DOKU_PLUGIN . "$plugin/script.js") && !file_exists(DOKU_PLUGIN . "$plugin/style.css") && !file_exists(DOKU_PLUGIN . "$plugin/print.css")) { continue; }
78            $allPlugins[] = $plugin;
79        }
80
81        return array($allPlugins, $plugin_controller->getList());
82    }
83
84    public function _page_sort($a, $b)
85    {
86        if ( $a[2] == $b[2] ) {
87            return 0;
88        }
89
90        return $a[2] > $b[2] ? -1 : 1;
91    }
92
93    public function __getOrderedListOfPagesForID($IDs, $start=null)
94    {
95        global $conf;
96        require_once(dirname(__FILE__)."/inc/functions.php");
97        $functions = new siteexport_functions(false);
98
99        if ( !is_array($IDs) ) {
100            $IDs = array($IDs);
101        }
102
103        $sites = $values = array();
104        foreach( $IDs as $ID ) {
105            $page = null;
106            search($sites, $conf['datadir'], 'search_allpages', array(), $functions->getNamespaceFromID($ID, $page));
107            foreach( $sites as $site ) {
108
109                if ( $ID == $site['id'] ) {
110                    continue;
111                }
112                $sortIdentifier = intval(p_get_metadata($site['id'], 'mergecompare'));
113                $entry = array(':' . $site['id'], $functions->getSiteTitle($site['id']), $sortIdentifier);
114
115                if ( !in_array($entry[0], array_column($values, 0)) ) {
116                    array_push($values, $entry);
117                }
118            }
119        }
120
121
122        if ( $start != null ) {
123            // filter using the newerThanPage indicator
124            $sortIdentifier = intval(p_get_metadata($start, 'mergecompare'));
125            $values = array_filter($values, array(new helper_plugin_siteexport_page_remove($sortIdentifier), '_page_remove'));
126        }
127
128        usort($values, array($this, '_page_sort'));
129
130        return $values;
131    }
132
133    public function __getOrderedListOfPagesForStartEnd($ID, $start, $end)
134    {
135        $values = $this->__getOrderedListOfPagesForID($ID);
136
137           // filter using the newerThanPage indicator
138        $values = array_filter($values, array(new helper_plugin_siteexport_page_remove(intval($start), intval($end)), '_page_remove'));
139
140        usort($values, array($this, '_page_sort'));
141        return $values;
142    }
143
144    public function __siteexport_addpage() {
145
146        global $ID, $conf;
147
148        $templateSwitching = false;
149        $pdfExport = false;
150        $translationAvailable = false;
151        $usenumberedheading = true;
152        $trans = array();
153
154        $preload = plugin_load('preload', 'siteexport');
155        if ($preload && $preload->__create_preload_function()) {
156            $templateSwitching = true;
157        }
158
159        $dw2pdf = plugin_load('action', 'dw2pdf');
160        if ($dw2pdf) {
161            $pdfExport = true;
162        }
163
164        $translation = plugin_load('helper', 'autotranslation');
165        if ($translation) {
166            $translationAvailable = true;
167            $trans = $translation->translations;
168        }
169
170        print $this->locale_xhtml((defined('DOKU_SITEEXPORT_MANAGER') ? 'manager' : '') . 'intro');
171
172        $form = new Doku_Form('siteexport', null, 'post');
173        $form->startFieldset($this->getLang('startingNamespace'));
174
175        $form->addElement(form_makeTextField('ns', $ID, $this->getLang('ns') . ':', 'ns'));
176        $form->addElement(form_makeTag('br'));
177        $form->addElement(form_makeTextField('ens', $ID, $this->getLang('ens') . ':', 'ens'));
178
179        $form->addElement(form_makeTag('br'));
180        $form->addElement(form_makeListboxField('depthType', array("0.0" => $this->getLang('depth.pageOnly'), "1.0" => $this->getLang('depth.allSubNameSpaces'), "2.0" => $this->getLang('depth.specifiedDepth')), (empty($_REQUEST['depthType']) ? $this->getLang('depth.allSubNameSpaces') : $_REQUEST['depthType']), $this->getLang('depthType') . ':', 'depthType', null, array_merge(array('class' => 'edit'))));
181
182        $form->addElement(form_makeTag('br'));
183        $form->addElement(form_makeOpenTag("div", array('style' => 'display:' . ($_REQUEST['depthType'] == "2" ? "block" : "none") . ';', 'id' => 'depthContainer')));
184        $form->addElement(form_makeTextField('depth', $this->getConf('depth'), $this->getLang('depth') . ':', 'depth'));
185        $form->addElement(form_makeCloseTag("div"));
186
187        $form->endFieldset();
188        $form->addElement(form_makeTag('br'));
189
190        $form->startFieldset($this->getLang('selectYourOptions'));
191        $form->addElement(form_makeCheckboxField('absolutePath', 1, $this->getLang('absolutePath') . ':', 'absolutePath'));
192        $form->addElement(form_makeTag('br'));
193        // The parameter needs lowercase
194        $form->addElement(form_makeCheckboxField('exportbody', 1, $this->getLang('exportBody') . ':', 'exportbody'));
195        $form->addElement(form_makeTag('br'));
196        $form->addElement(form_makeCheckboxField('exportLinkedPages', 1, $this->getLang('exportLinkedPages') . ':', 'exportLinkedPages', 'sendIfNotSet', array('checked' => 'checked')));
197        $form->addElement(form_makeTag('br'));
198        $form->addElement(form_makeCheckboxField('disableCache', 1, $this->getLang('disableCache') . ':', 'disableCache'));
199        $form->addElement(form_makeTag('br'));
200        $form->addElement(form_makeCheckboxField('addParams', 1, $this->getLang('addParams') . ':', 'addParams', null, array_merge(array('checked' => ($conf['userewrite'] != 1 ? 'checked' : '')))));
201        $form->addElement(form_makeTag('br'));
202        $form->addElement(form_makeTag('br'));
203        $form->addElement(form_makeListboxField('renderer', array_merge(array('', 'xhtml'), plugin_list('renderer')), '', $this->getLang('renderer') . ':', 'renderer', null, array_merge(array('class' => 'edit'))));
204
205        $form->addElement(form_makeTag('br'));
206        if ($templateSwitching) {
207            $form->addElement(form_makeListboxField('template', $this->__getTemplates(), $conf['template'], $this->getLang('template') . ':', 'template', null, array_merge(array('class' => 'edit'))));
208            $form->addElement(form_makeTag('br'));
209        } else
210        {
211            $form->addElement(form_makeTag('br'));
212            $form->addElement(form_makeOpenTag('p', array('style' => 'color: #a00;')));
213            $form->addElement('Can\'t create preload file in \'inc\' directory. Template switching is not available. Plugin disabling is not available.');
214            $form->addElement(form_makeCloseTag('p'));
215        }
216
217        $form->addElement(form_makeTag('br'));
218        $form->addElement(form_makeCheckboxField('pdfExport', 1, $this->getLang('pdfExport') . ':', 'pdfExport', null, $pdfExport ? array() : array_merge(array('disabled' => 'disabled'))));
219
220        // Hint for dw2pdf
221        $this->addPluginHint( $form, $pdfExport, "the PDF export", "dw2pdf" );
222
223        $form->addElement(form_makeTag('br'));
224        $form->addElement(form_makeCheckboxField('usenumberedheading', 1, $this->getLang('usenumberedheading') . ':', 'usenumberedheading', null, $usenumberedheading && $pdfExport ? array() : array_merge(array('disabled' => 'disabled'))));
225        $form->addElement(form_makeTag('br'));
226
227        // Hint for nodetailsxhtml
228        $this->addPluginHint( $form, $usenumberedheading, "numbered headings", "nodetailsxhtml" );
229
230        $form->endFieldset();
231        $form->addElement(form_makeTag('br'));
232
233        $form->startFieldset($this->getLang('helpCreationOptions'));
234        $form->addElement(form_makeCheckboxField('eclipseDocZip', 1, $this->getLang('eclipseDocZip') . ':', 'eclipseDocZip'));
235        $form->addElement(form_makeTag('br'));
236        $form->addElement(form_makeCheckboxField('JavaHelpDocZip', 1, $this->getLang('JavaHelpDocZip') . ':', 'JavaHelpDocZip'));
237        $form->addElement(form_makeTag('br'));
238        $form->addElement(form_makeCheckboxField('useTocFile', 1, $this->getLang('useTocFile') . ':', 'useTocFile'));
239        $form->addElement(form_makeTag('br'));
240        $form->addElement(form_makeCheckboxField('emptyTocElem', 1, $this->getLang('emptyTocElem') . ':', 'emptyTocElem'));
241        $form->addElement(form_makeTag('br'));
242        if (!$translationAvailable) {
243            $form->addElement(form_makeCheckboxField('TOCMapWithoutTranslation', 1, $this->getLang('TOCMapWithoutTranslation') . ':', 'TOCMapWithoutTranslation'));
244            $form->addElement(form_makeTag('br'));
245        } else {
246
247            if (!is_array($trans)) {
248                $trans = array($trans);
249            }
250
251            $trans = array_unique(array_merge($trans, array($conf['lang'])));
252            $form->addElement(form_makeListboxField('defaultLang', $trans, $conf['lang'], $this->getLang('defaultLang') . ':', 'defaultLang'));
253            $form->addElement(form_makeTag('br'));
254        }
255        $form->endFieldset();
256        $form->addElement(form_makeTag('br'));
257
258        if ($templateSwitching)
259        {
260            $form->startFieldset($this->getLang('disablePluginsOption'));
261
262            $form->addElement(form_makeCheckboxField("disableall", 1, 'Disable All:', "disableall", 'forceVisible'));
263            $form->addElement(form_makeTag('br'));
264            $form->addElement(form_makeTag('br'));
265
266            list($allPlugins, $enabledPlugins) = $this->__getPluginList();
267            foreach ($allPlugins as $plugin) {
268                $form->addElement(form_makeCheckboxField("disableplugin[]", $plugin, $plugin . ':', "disableplugin_$plugin", null, (!in_array($plugin, $enabledPlugins) ? array('checked' => 'checked', 'disabled' => 'disabled') : array())));
269                $form->addElement(form_makeTag('br'));
270            }
271
272            $form->endFieldset();
273            $form->addElement(form_makeTag('br'));
274        }
275
276        $form->startFieldset( $this->getLang('customOptions') );
277        $form->addElement(form_makeOpenTag('p'));
278        $form->addElement( $this->getLang('customOptionsDescription') );
279        $form->addElement(form_makeCloseTag('p'));
280
281        $form->addElement(form_makeOpenTag('ul', array('id' => 'siteexport__customActions')));
282        $form->addElement(form_makeCloseTag('ul'));
283        $form->addElement(form_makeTag('br', array('class'=>'clear')));
284        $form->addElement(form_makeButton('submit', 'addoption', $this->getLang('addCustomOption') , array('style' => 'float:right;') ));
285
286        $form->endFieldset();
287        $form->addElement(form_makeTag('br'));
288
289        if ( !defined('DOKU_SITEEXPORT_MANAGER') ) {
290
291
292            $form->startFieldset( $this->getLang('startProcess') );
293            $form->addElement(form_makeTextField('copyurl', "", $this->getLang('directDownloadLink') . ':', 'copyurl', null, array('readonly' => 'readonly') ));
294            $form->addElement(form_makeTag('br'));
295            $form->addElement(form_makeTextField('wgeturl', "", $this->getLang('wgetURLLink') . ':', 'wgeturl', null, array('readonly' => 'readonly') ));
296            $form->addElement(form_makeTag('br'));
297            $form->addElement(form_makeTextField('curlurl', "", $this->getLang('curlURLLink') . ':', 'curlurl', null, array('readonly' => 'readonly') ));
298            $form->addElement(form_makeTag('br', array('class'=>'clear')));
299            $form->addElement(form_makeButton('submit', 'siteexport', $this->getLang('start') , array('style' => 'float:right;')));
300            $form->endFieldset();
301            $form->addElement(form_makeTag('br'));
302
303            $form->endFieldset();
304            $form->addElement(form_makeTag('br'));
305
306            $form->startFieldset( $this->getLang('status') );
307            $form->addElement(form_makeOpenTag('span', array('id' => 'siteexport__out')));
308
309            $form->addElement(form_makeCloseTag('span'));
310            $form->addElement(form_makeOpenTag('span', array('class' => 'siteexport__throbber')));
311
312            $throbber = DOKU_BASE.'lib/images/loading.gif';
313            if ( !file_exists( $throbber) ) {
314                $throbber = DOKU_BASE.'lib/images/throbber.gif';
315            }
316
317            $form->addElement(form_makeTag('img', array('src' => $throbber, 'id' => 'siteexport__throbber')));
318            $form->addElement(form_makeCloseTag('span'));
319            $form->endFieldset();
320            $form->addElement(form_makeTag('br'));
321
322        } else {
323            $form->startFieldset( $this->getLang('startProcess') );
324            $form->addElement(form_makeButton('submit', 'siteexport', $this->getLang('useOptionsInEditor') , array('style' => 'width:100%;')));
325        }
326
327        $form->endFieldset();
328        $form->addElement(form_makeTag('br'));
329
330        $form->printForm();
331    }
332
333    private function addPluginHint( &$form, $condition, $hint, $plugin ) {
334        if ($condition) { return; }
335
336        $form->addElement(form_makeOpenTag('p', array('style' => 'color: #a00;')));
337        $form->addElement('In order to use ' . $hint . ', please ');
338        $form->addElement(form_makeOpenTag('a', array('href' => 'http://www.dokuwiki.org/plugin:' . $plugin, 'alt' => 'install plugin', 'target' => '_blank')));
339        $form->addElement('install the ' . $plugin . ' plugin.');
340        $form->addElement(form_makeCloseTag('a'));
341        $form->addElement(form_makeCloseTag('p'));
342    }
343
344}
345