xref: /plugin/siteexport/helper.php (revision 56c4b32d8c91c103da10f9e5918de8f7bcaa82cd)
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, $INPUT;
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 (!function_exists('siteexport_dw2pdf_has_mpdf_engine')) {
161            require_once DOKU_PLUGIN . 'siteexport/inc/pdfgenerator.php';
162        }
163        // mPDF may live in legacy dw2pdf/mpdf/ or current dw2pdf/vendor/ (+ DokuPDF)
164        $pdfExport = siteexport_dw2pdf_has_mpdf_engine();
165
166        $translation = plugin_load('helper', 'autotranslation');
167        if ($translation) {
168            $translationAvailable = true;
169            $trans = $translation->translations;
170        }
171
172        print $this->locale_xhtml((defined('DOKU_SITEEXPORT_MANAGER') ? 'manager' : '') . 'intro');
173
174        $form = new Doku_Form('siteexport', null, 'post');
175        $form->startFieldset($this->getLang('startingNamespace'));
176
177        $form->addElement(form_makeTextField('ns', $ID, $this->getLang('ns') . ':', 'ns'));
178        $form->addElement(form_makeTag('br'));
179        $form->addElement(form_makeTextField('ens', $ID, $this->getLang('ens') . ':', 'ens'));
180
181        $form->addElement(form_makeTag('br'));
182        $form->addElement(form_makeListboxField('depthType', array("0.0" => $this->getLang('depth.pageOnly'), "1.0" => $this->getLang('depth.allSubNameSpaces'), "2.0" => $this->getLang('depth.specifiedDepth')), $INPUT->str('depthType', $this->getLang('depth.allSubNameSpaces'), true), $this->getLang('depthType') . ':', 'depthType', null, array_merge(array('class' => 'edit'))));
183
184        $form->addElement(form_makeTag('br'));
185        $form->addElement(form_makeOpenTag("div", array('style' => 'display:' . ($INPUT->str('depthType') == "2" ? "block" : "none") . ';', 'id' => 'depthContainer')));
186        $form->addElement(form_makeTextField('depth', $this->getConf('depth'), $this->getLang('depth') . ':', 'depth'));
187        $form->addElement(form_makeCloseTag("div"));
188
189        $form->endFieldset();
190        $form->addElement(form_makeTag('br'));
191
192        $form->startFieldset($this->getLang('selectYourOptions'));
193        $form->addElement(form_makeCheckboxField('absolutePath', 1, $this->getLang('absolutePath') . ':', 'absolutePath'));
194        $form->addElement(form_makeTag('br'));
195        // The parameter needs lowercase
196        $form->addElement(form_makeCheckboxField('exportbody', 1, $this->getLang('exportBody') . ':', 'exportbody'));
197        $form->addElement(form_makeTag('br'));
198        $form->addElement(form_makeCheckboxField('exportLinkedPages', 1, $this->getLang('exportLinkedPages') . ':', 'exportLinkedPages', 'sendIfNotSet', array('checked' => 'checked')));
199        $form->addElement(form_makeTag('br'));
200        $form->addElement(form_makeCheckboxField('disableCache', 1, $this->getLang('disableCache') . ':', 'disableCache'));
201        $form->addElement(form_makeTag('br'));
202        $form->addElement(form_makeCheckboxField('addParams', 1, $this->getLang('addParams') . ':', 'addParams', null, array_merge(array('checked' => ($conf['userewrite'] != 1 ? 'checked' : '')))));
203        $form->addElement(form_makeTag('br'));
204        $form->addElement(form_makeTag('br'));
205        $form->addElement(form_makeListboxField('renderer', array_merge(array('', 'xhtml'), plugin_list('renderer')), '', $this->getLang('renderer') . ':', 'renderer', null, array_merge(array('class' => 'edit'))));
206
207        $form->addElement(form_makeTag('br'));
208        if ($templateSwitching) {
209            $form->addElement(form_makeListboxField('template', $this->__getTemplates(), $conf['template'], $this->getLang('template') . ':', 'template', null, array_merge(array('class' => 'edit'))));
210            $form->addElement(form_makeTag('br'));
211        } else
212        {
213            $form->addElement(form_makeTag('br'));
214            $form->addElement(form_makeOpenTag('p', array('style' => 'color: #a00;')));
215            $form->addElement('Can\'t create preload file in \'inc\' directory. Template switching is not available. Plugin disabling is not available.');
216            $form->addElement(form_makeCloseTag('p'));
217        }
218
219        $form->addElement(form_makeTag('br'));
220        $form->addElement(form_makeCheckboxField('pdfExport', 1, $this->getLang('pdfExport') . ':', 'pdfExport', null, $pdfExport ? array() : array_merge(array('disabled' => 'disabled'))));
221
222        // Hint for dw2pdf
223        $this->addPluginHint( $form, $pdfExport, "the PDF export", "dw2pdf" );
224
225        $form->addElement(form_makeTag('br'));
226        $form->addElement(form_makeCheckboxField('usenumberedheading', 1, $this->getLang('usenumberedheading') . ':', 'usenumberedheading', null, $usenumberedheading && $pdfExport ? array() : array_merge(array('disabled' => 'disabled'))));
227        $form->addElement(form_makeTag('br'));
228
229        // Hint for nodetailsxhtml
230        $this->addPluginHint( $form, $usenumberedheading, "numbered headings", "nodetailsxhtml" );
231
232        $form->endFieldset();
233        $form->addElement(form_makeTag('br'));
234
235        $form->startFieldset($this->getLang('helpCreationOptions'));
236        $form->addElement(form_makeCheckboxField('eclipseDocZip', 1, $this->getLang('eclipseDocZip') . ':', 'eclipseDocZip'));
237        $form->addElement(form_makeTag('br'));
238        $form->addElement(form_makeCheckboxField('JavaHelpDocZip', 1, $this->getLang('JavaHelpDocZip') . ':', 'JavaHelpDocZip'));
239        $form->addElement(form_makeTag('br'));
240        $form->addElement(form_makeCheckboxField('useTocFile', 1, $this->getLang('useTocFile') . ':', 'useTocFile'));
241        $form->addElement(form_makeTag('br'));
242        $form->addElement(form_makeCheckboxField('emptyTocElem', 1, $this->getLang('emptyTocElem') . ':', 'emptyTocElem'));
243        $form->addElement(form_makeTag('br'));
244        if (!$translationAvailable) {
245            $form->addElement(form_makeCheckboxField('TOCMapWithoutTranslation', 1, $this->getLang('TOCMapWithoutTranslation') . ':', 'TOCMapWithoutTranslation'));
246            $form->addElement(form_makeTag('br'));
247        } else {
248
249            if (!is_array($trans)) {
250                $trans = array($trans);
251            }
252
253            $trans = array_unique(array_merge($trans, array($conf['lang'])));
254            $form->addElement(form_makeListboxField('defaultLang', $trans, $conf['lang'], $this->getLang('defaultLang') . ':', 'defaultLang'));
255            $form->addElement(form_makeTag('br'));
256        }
257        $form->endFieldset();
258        $form->addElement(form_makeTag('br'));
259
260        if ($templateSwitching)
261        {
262            $form->startFieldset($this->getLang('disablePluginsOption'));
263
264            $form->addElement(form_makeCheckboxField("disableall", 1, 'Disable All:', "disableall", 'forceVisible'));
265            $form->addElement(form_makeTag('br'));
266            $form->addElement(form_makeTag('br'));
267
268            list($allPlugins, $enabledPlugins) = $this->__getPluginList();
269            foreach ($allPlugins as $plugin) {
270                $form->addElement(form_makeCheckboxField("disableplugin[]", $plugin, $plugin . ':', "disableplugin_$plugin", null, (!in_array($plugin, $enabledPlugins) ? array('checked' => 'checked', 'disabled' => 'disabled') : array())));
271                $form->addElement(form_makeTag('br'));
272            }
273
274            $form->endFieldset();
275            $form->addElement(form_makeTag('br'));
276        }
277
278        $form->startFieldset( $this->getLang('customOptions') );
279        $form->addElement(form_makeOpenTag('p'));
280        $form->addElement( $this->getLang('customOptionsDescription') );
281        $form->addElement(form_makeCloseTag('p'));
282
283        $form->addElement(form_makeOpenTag('ul', array('id' => 'siteexport__customActions')));
284        $form->addElement(form_makeCloseTag('ul'));
285        $form->addElement(form_makeTag('br', array('class'=>'clear')));
286        $form->addElement(form_makeButton('submit', 'addoption', $this->getLang('addCustomOption') , array('style' => 'float:right;') ));
287
288        $form->endFieldset();
289        $form->addElement(form_makeTag('br'));
290
291        if ( !defined('DOKU_SITEEXPORT_MANAGER') ) {
292
293
294            $form->startFieldset( $this->getLang('startProcess') );
295            $form->addElement(form_makeTextField('copyurl', "", $this->getLang('directDownloadLink') . ':', 'copyurl', null, array('readonly' => 'readonly') ));
296            $form->addElement(form_makeTag('br'));
297            $form->addElement(form_makeTextField('wgeturl', "", $this->getLang('wgetURLLink') . ':', 'wgeturl', null, array('readonly' => 'readonly') ));
298            $form->addElement(form_makeTag('br'));
299            $form->addElement(form_makeTextField('curlurl', "", $this->getLang('curlURLLink') . ':', 'curlurl', null, array('readonly' => 'readonly') ));
300            $form->addElement(form_makeTag('br', array('class'=>'clear')));
301            $form->addElement(form_makeButton('submit', 'siteexport', $this->getLang('start') , array('style' => 'float:right;')));
302            $form->endFieldset();
303            $form->addElement(form_makeTag('br'));
304
305            $form->endFieldset();
306            $form->addElement(form_makeTag('br'));
307
308            $form->startFieldset( $this->getLang('status') );
309            $form->addElement(form_makeOpenTag('span', array('id' => 'siteexport__out')));
310
311            $form->addElement(form_makeCloseTag('span'));
312            $form->addElement(form_makeOpenTag('span', array('class' => 'siteexport__throbber')));
313
314            $throbber = DOKU_BASE.'lib/images/loading.gif';
315            if ( !file_exists( $throbber) ) {
316                $throbber = DOKU_BASE.'lib/images/throbber.gif';
317            }
318
319            $form->addElement(form_makeTag('img', array('src' => $throbber, 'id' => 'siteexport__throbber')));
320            $form->addElement(form_makeCloseTag('span'));
321            $form->endFieldset();
322            $form->addElement(form_makeTag('br'));
323
324        } else {
325            $form->startFieldset( $this->getLang('startProcess') );
326            $form->addElement(form_makeButton('submit', 'siteexport', $this->getLang('useOptionsInEditor') , array('style' => 'width:100%;')));
327        }
328
329        $form->endFieldset();
330        $form->addElement(form_makeTag('br'));
331
332        $form->printForm();
333    }
334
335    private function addPluginHint( &$form, $condition, $hint, $plugin ) {
336        if ($condition) { return; }
337
338        $form->addElement(form_makeOpenTag('p', array('style' => 'color: #a00;')));
339        $form->addElement('In order to use ' . $hint . ', please ');
340        $form->addElement(form_makeOpenTag('a', array('href' => 'http://www.dokuwiki.org/plugin:' . $plugin, 'alt' => 'install plugin', 'target' => '_blank')));
341        $form->addElement('install the ' . $plugin . ' plugin.');
342        $form->addElement(form_makeCloseTag('a'));
343        $form->addElement(form_makeCloseTag('p'));
344    }
345
346}
347