xref: /plugin/siteexport/helper.php (revision 6292ca7aa5efa4bec9bd483c6855b04a53f4166c)
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        $page = null;
105        foreach( $IDs as $ID ) {
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                array_push($values, array(':' . $site['id'], $functions->getSiteTitle($site['id']), $sortIdentifier));
114            }
115        }
116
117        if ( $start != null ) {
118
119            // filter using the newerThanPage indicator
120            $sortIdentifier = intval(p_get_metadata($start, 'mergecompare'));
121            $values = array_filter($values, array(new helper_plugin_siteexport_page_remove($sortIdentifier), '_page_remove'));
122        }
123
124        usort($values, array($this, '_page_sort'));
125
126        return $values;
127    }
128
129    public function __getOrderedListOfPagesForStartEnd($ID, $start, $end)
130    {
131        $values = $this->__getOrderedListOfPagesForID($ID);
132
133           // filter using the newerThanPage indicator
134        $values = array_filter($values, array(new helper_plugin_siteexport_page_remove(intval($start), intval($end)), '_page_remove'));
135
136        usort($values, array($this, '_page_sort'));
137        return $values;
138    }
139
140    public function __siteexport_addpage() {
141
142        global $ID, $conf;
143
144        $templateSwitching = false;
145        $pdfExport = false;
146        $translationAvailable = false;
147        $usenumberedheading = true;
148        $trans = array();
149
150        $preload = plugin_load('preload', 'siteexport');
151        if ($preload && $preload->__create_preload_function()) {
152            $templateSwitching = true;
153        }
154
155        $dw2pdf = plugin_load('action', 'dw2pdf');
156        if ($dw2pdf) {
157            $pdfExport = true;
158        }
159
160        $translation = plugin_load('helper', 'autotranslation');
161        if ($translation) {
162            $translationAvailable = true;
163            $trans = $translation->translations;
164        }
165
166        print $this->locale_xhtml((defined('DOKU_SITEEXPORT_MANAGER') ? 'manager' : '') . 'intro');
167
168        $form = new Doku_Form('siteexport', null, 'post');
169        $form->startFieldset($this->getLang('startingNamespace'));
170
171        $form->addElement(form_makeTextField('ns', $ID, $this->getLang('ns') . ':', 'ns'));
172        $form->addElement(form_makeTag('br'));
173        $form->addElement(form_makeTextField('ens', $ID, $this->getLang('ens') . ':', 'ens'));
174
175        $form->addElement(form_makeTag('br'));
176        $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'))));
177
178        $form->addElement(form_makeTag('br'));
179        $form->addElement(form_makeOpenTag("div", array('style' => 'display:' . ($_REQUEST['depthType'] == "2" ? "block" : "none") . ';', 'id' => 'depthContainer')));
180        $form->addElement(form_makeTextField('depth', $this->getConf('depth'), $this->getLang('depth') . ':', 'depth'));
181        $form->addElement(form_makeCloseTag("div"));
182
183        $form->endFieldset();
184        $form->addElement(form_makeTag('br'));
185
186        $form->startFieldset($this->getLang('selectYourOptions'));
187        $form->addElement(form_makeCheckboxField('absolutePath', 1, $this->getLang('absolutePath') . ':', 'absolutePath'));
188        $form->addElement(form_makeTag('br'));
189        // The parameter needs lowercase
190        $form->addElement(form_makeCheckboxField('exportbody', 1, $this->getLang('exportBody') . ':', 'exportbody'));
191        $form->addElement(form_makeTag('br'));
192        $form->addElement(form_makeCheckboxField('exportLinkedPages', 1, $this->getLang('exportLinkedPages') . ':', 'exportLinkedPages', 'sendIfNotSet', array('checked' => 'checked')));
193        $form->addElement(form_makeTag('br'));
194        $form->addElement(form_makeCheckboxField('disableCache', 1, $this->getLang('disableCache') . ':', 'disableCache'));
195        $form->addElement(form_makeTag('br'));
196        $form->addElement(form_makeCheckboxField('addParams', 1, $this->getLang('addParams') . ':', 'addParams', null, array_merge(array('checked' => ($conf['userewrite'] != 1 ? 'checked' : '')))));
197        $form->addElement(form_makeTag('br'));
198        $form->addElement(form_makeTag('br'));
199        $form->addElement(form_makeListboxField('renderer', array_merge(array('', 'xhtml'), plugin_list('renderer')), '', $this->getLang('renderer') . ':', 'renderer', null, array_merge(array('class' => 'edit'))));
200
201        $form->addElement(form_makeTag('br'));
202        if ($templateSwitching) {
203            $form->addElement(form_makeListboxField('template', $this->__getTemplates(), $conf['template'], $this->getLang('template') . ':', 'template', null, array_merge(array('class' => 'edit'))));
204            $form->addElement(form_makeTag('br'));
205        } else
206        {
207            $form->addElement(form_makeTag('br'));
208            $form->addElement(form_makeOpenTag('p', array('style' => 'color: #a00;')));
209            $form->addElement('Can\'t create preload file in \'inc\' directory. Template switching is not available. Plugin disabling is not available.');
210            $form->addElement(form_makeCloseTag('p'));
211        }
212
213        $form->addElement(form_makeTag('br'));
214        $form->addElement(form_makeCheckboxField('pdfExport', 1, $this->getLang('pdfExport') . ':', 'pdfExport', null, $pdfExport ? array() : array_merge(array('disabled' => 'disabled'))));
215
216        // Hint for dw2pdf
217        $this->addPluginHint( $form, $pdfExport, "the PDF export", "dw2pdf" );
218
219        $form->addElement(form_makeTag('br'));
220        $form->addElement(form_makeCheckboxField('usenumberedheading', 1, $this->getLang('usenumberedheading') . ':', 'usenumberedheading', null, $usenumberedheading && $pdfExport ? array() : array_merge(array('disabled' => 'disabled'))));
221        $form->addElement(form_makeTag('br'));
222
223        // Hint for nodetailsxhtml
224        $this->addPluginHint( $form, $usenumberedheading, "numbered headings", "nodetailsxhtml" );
225
226        $form->endFieldset();
227        $form->addElement(form_makeTag('br'));
228
229        $form->startFieldset($this->getLang('helpCreationOptions'));
230        $form->addElement(form_makeCheckboxField('eclipseDocZip', 1, $this->getLang('eclipseDocZip') . ':', 'eclipseDocZip'));
231        $form->addElement(form_makeTag('br'));
232        $form->addElement(form_makeCheckboxField('JavaHelpDocZip', 1, $this->getLang('JavaHelpDocZip') . ':', 'JavaHelpDocZip'));
233        $form->addElement(form_makeTag('br'));
234        $form->addElement(form_makeCheckboxField('useTocFile', 1, $this->getLang('useTocFile') . ':', 'useTocFile'));
235        $form->addElement(form_makeTag('br'));
236        $form->addElement(form_makeCheckboxField('emptyTocElem', 1, $this->getLang('emptyTocElem') . ':', 'emptyTocElem'));
237        $form->addElement(form_makeTag('br'));
238        if (!$translationAvailable) {
239            $form->addElement(form_makeCheckboxField('TOCMapWithoutTranslation', 1, $this->getLang('TOCMapWithoutTranslation') . ':', 'TOCMapWithoutTranslation'));
240            $form->addElement(form_makeTag('br'));
241        } else {
242
243            if (!is_array($trans)) {
244                $trans = array($trans);
245            }
246
247            $trans = array_unique(array_merge($trans, array($conf['lang'])));
248            $form->addElement(form_makeListboxField('defaultLang', $trans, $conf['lang'], $this->getLang('defaultLang') . ':', 'defaultLang'));
249            $form->addElement(form_makeTag('br'));
250        }
251        $form->endFieldset();
252        $form->addElement(form_makeTag('br'));
253
254        if ($templateSwitching)
255        {
256            $form->startFieldset($this->getLang('disablePluginsOption'));
257
258            $form->addElement(form_makeCheckboxField("disableall", 1, 'Disable All:', "disableall", 'forceVisible'));
259            $form->addElement(form_makeTag('br'));
260            $form->addElement(form_makeTag('br'));
261
262            list($allPlugins, $enabledPlugins) = $this->__getPluginList();
263            foreach ($allPlugins as $plugin) {
264                $form->addElement(form_makeCheckboxField("disableplugin[]", $plugin, $plugin . ':', "disableplugin_$plugin", null, (!in_array($plugin, $enabledPlugins) ? array('checked' => 'checked', 'disabled' => 'disabled') : array())));
265                $form->addElement(form_makeTag('br'));
266            }
267
268            $form->endFieldset();
269            $form->addElement(form_makeTag('br'));
270        }
271
272        $form->startFieldset( $this->getLang('customOptions') );
273        $form->addElement(form_makeOpenTag('p'));
274        $form->addElement( $this->getLang('customOptionsDescription') );
275        $form->addElement(form_makeCloseTag('p'));
276
277        $form->addElement(form_makeOpenTag('ul', array('id' => 'siteexport__customActions')));
278        $form->addElement(form_makeCloseTag('ul'));
279        $form->addElement(form_makeTag('br', array('class'=>'clear')));
280        $form->addElement(form_makeButton('submit', 'addoption', $this->getLang('addCustomOption') , array('style' => 'float:right;') ));
281
282        $form->endFieldset();
283        $form->addElement(form_makeTag('br'));
284
285        if ( !defined('DOKU_SITEEXPORT_MANAGER') ) {
286
287
288            $form->startFieldset( $this->getLang('startProcess') );
289            $form->addElement(form_makeTextField('copyurl', "", $this->getLang('directDownloadLink') . ':', 'copyurl', null, array('readonly' => 'readonly') ));
290            $form->addElement(form_makeTag('br'));
291            $form->addElement(form_makeTextField('wgeturl', "", $this->getLang('wgetURLLink') . ':', 'wgeturl', null, array('readonly' => 'readonly') ));
292            $form->addElement(form_makeTag('br'));
293            $form->addElement(form_makeTextField('curlurl', "", $this->getLang('curlURLLink') . ':', 'curlurl', null, array('readonly' => 'readonly') ));
294            $form->addElement(form_makeTag('br', array('class'=>'clear')));
295            $form->addElement(form_makeButton('submit', 'siteexport', $this->getLang('start') , array('style' => 'float:right;')));
296            $form->endFieldset();
297            $form->addElement(form_makeTag('br'));
298
299            $form->endFieldset();
300            $form->addElement(form_makeTag('br'));
301
302            $form->startFieldset( $this->getLang('status') );
303            $form->addElement(form_makeOpenTag('span', array('id' => 'siteexport__out')));
304
305            $form->addElement(form_makeCloseTag('span'));
306            $form->addElement(form_makeOpenTag('span', array('class' => 'siteexport__throbber')));
307
308            $throbber = DOKU_BASE.'lib/images/loading.gif';
309            if ( !file_exists( $throbber) ) {
310                $throbber = DOKU_BASE.'lib/images/throbber.gif';
311            }
312
313            $form->addElement(form_makeTag('img', array('src' => $throbber, 'id' => 'siteexport__throbber')));
314            $form->addElement(form_makeCloseTag('span'));
315            $form->endFieldset();
316            $form->addElement(form_makeTag('br'));
317
318        } else {
319            $form->startFieldset( $this->getLang('startProcess') );
320            $form->addElement(form_makeButton('submit', 'siteexport', $this->getLang('useOptionsInEditor') , array('style' => 'width:100%;')));
321        }
322
323        $form->endFieldset();
324        $form->addElement(form_makeTag('br'));
325
326        $form->printForm();
327    }
328
329    private function addPluginHint( &$form, $condition, $hint, $plugin ) {
330        if ($condition) { return; }
331
332        $form->addElement(form_makeOpenTag('p', array('style' => 'color: #a00;')));
333        $form->addElement('In order to use ' . $hint . ', please ');
334        $form->addElement(form_makeOpenTag('a', array('href' => 'http://www.dokuwiki.org/plugin:' . $plugin, 'alt' => 'install plugin', 'target' => '_blank')));
335        $form->addElement('install the ' . $plugin . ' plugin.');
336        $form->addElement(form_makeCloseTag('a'));
337        $form->addElement(form_makeCloseTag('p'));
338    }
339
340}
341