xref: /plugin/siteexport/action/aggregate.php (revision 0571ece201b9e3bc14846f6c88d943a4f1512014)
1<?php
2/**
3 * Site Export Plugin
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')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
13require_once(DOKU_PLUGIN . 'action.php');
14
15class action_plugin_siteexport_aggregate extends DokuWiki_Action_Plugin {
16
17    /**
18     * Register Plugin in DW
19     **/
20    public function register(Doku_Event_Handler $controller) {
21        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE',  $this, 'siteexport_aggregate');
22        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'siteexport_aggregate_button', array ());
23    }
24
25    function siteexport_aggregate(&$event)
26    {
27        global $ID, $INFO, $conf;
28
29        // Aggregate only if
30        // (1) this page really has an aggregator and we did submit a request to do so
31        // (2) this page really has an aggregator and we export as PDF
32        if ( !( (!empty($INFO['meta']['siteexport']) && $INFO['meta']['siteexport']['hasaggregator'] == true) && ( isset($_REQUEST['siteexport_aggregate']) || $conf['renderer_xhtml'] == 'siteexport_pdf' ) ) ) { return true; }
33
34        $exportBase = cleanID($_REQUEST['baseID']);
35        $namespace = empty($exportBase) ? $INFO['meta']['siteexport']['baseID'] : getNs($exportBase);
36
37        $functions = plugin_load('helper', 'siteexport');
38        $values = $functions->__getOrderedListOfPagesForID($namespace, $exportBase);
39
40        // If no base given, take the first one from the ordered list.
41        if ( empty($exportBase) ) {
42            // Reset to latest element
43            list($exportBase) = reset( $values );
44        }
45
46        // If only the one file should be exported, strip it down.
47        if ( !empty($_REQUEST['exportSelectedVersionOnly']) ) {
48            // Strip down values
49            $lookupNS = noNS($namespace) == $conf['start'] ? $namespace : $namespace . ':' . $conf['start'];
50
51            if ( !empty( $_REQUEST['mergecompare_start'] ) && !empty( $_REQUEST['mergecompare_end'] ) ) {
52                    $values = $functions->__getOrderedListOfPagesForStartEnd($lookupNS, $_REQUEST['mergecompare_start'], $_REQUEST['mergecompare_end']);
53            } else {
54                $values = $functions->__getOrderedListOfPagesForID($lookupNS, $exportBase);
55                $values = array(end( $values )); // the list above has the $exportBase element at the very end
56            }
57        }
58
59        $originalID = (string) $ID;
60
61        // Generate a TOC that can be exported
62        $TOC = "<toc merge mergeheader>\n";
63        $thema = array();
64        foreach( $values as $value ) {
65            list($id, $title, $sort) = $value;
66
67            $thema[] = p_get_metadata($id, 'thema', METADATA_RENDER_USING_SIMPLE_CACHE);
68            $TOC .= "  * [[{$id}|{$title}]]\n";
69        }
70
71        $TOC .= "</toc>";
72
73        // Only get first and last element
74        $thema = array_unique(array(reset($thema), end($thema)));
75
76        $meta = p_read_metadata($ID);
77        $ID = (string) cleanID($ID . '-toc-' . implode('-', array_filter($thema)));
78
79        $meta['current']['thema'] = implode(' - ', array_filter($thema));
80        p_save_metadata($ID, $meta);
81
82        if (empty($TOC)) { return true; }
83        $event->preventDefault();
84
85        $html = p_render('xhtml', p_get_instructions($TOC), $INFO);
86        // $html = html_secedit($html,false);
87        if ($INFO['prependTOC']) $html = tpl_toc(true) . $html;
88
89        @unlink(metaFN($ID, '.meta'));
90
91        $ID = (string) $originalID;
92        echo $html;
93        return true;
94    }
95
96    /**
97     * Inserts a toolbar button
98     */
99    function siteexport_aggregate_button(& $event, $param) {
100        $event->data[] = array (
101            'type' => 'mediapopup',
102            'title' => $this->getLang('toolbarButton'),
103            'icon' => '../../plugins/siteexport/images/toolbar.png',
104            'url' => 'lib/plugins/siteexport/exe/siteexportmanager.php?ns=',
105            'options' => 'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes',
106            'block' => false,
107        );
108    }
109}