xref: /plugin/siteexport/action/aggregate.php (revision a310139ec414f72d15e5025c094e0522b71674e8)
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(Doku_Event &$event)
26    {
27        global $ID, $INFO, $conf, $INPUT;
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) && ( $INPUT->has( 'siteexport_aggregate' ) || $conf['renderer_xhtml'] == 'siteexport_pdf' ) ) ) { return true; }
33
34        $exportBase = cleanID( $INPUT->str('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 ( $INPUT->bool('exportSelectedVersionOnly' ) ) {
48            // Strip down values
49            $lookupNS = noNS($namespace) == $conf['start'] ? $namespace : $namespace . ':' . $conf['start'];
50
51            if ( $INPUT->has( 'mergecompare_start' ) && $INPUT->has( 'mergecompare_end' ) ) {
52                    $values = $functions->__getOrderedListOfPagesForStartEnd($lookupNS, $INPUT->int( 'mergecompare_start' ), $INPUT->int( '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        if( $INPUT->bool('includeSelectedVersion', true, true ) && count( $values ) > 1 ) {
60            array_pop( $values ); // Remove last entry which is the selected version, but only if more than one entry exists
61        }
62
63        $originalID = (string) $ID;
64
65        // Generate a TOC that can be exported
66        $TOC = "<toc merge mergeheader";
67
68        // add a mergehint, or better remove it if not required
69        if( $INPUT->bool('mergehint', true, true ) ) {
70            $TOC .= " mergehint";
71        }
72
73        $TOC .= ">\n";
74        $thema = array();
75        foreach( $values as $value ) {
76            list($id, $title, $sort) = $value;
77
78            $thema[] = p_get_metadata($id, 'thema', METADATA_RENDER_USING_SIMPLE_CACHE);
79            $TOC .= "  * [[{$id}|{$title}]]\n";
80        }
81
82        $TOC .= "</toc>";
83
84        // Only get first and last element
85        $thema = array_reverse(array_unique(array(reset($thema), end($thema))));
86
87        $meta = p_read_metadata($originalID);
88        // Temporary ID for rendering a document.
89        $ID = (string) cleanID($originalID . '-toc-' . implode('-', array_filter($thema)));
90
91        $meta['current']['thema'] = implode(' - ', array_filter($thema));
92        p_save_metadata($originalID, $meta);
93        p_save_metadata($ID, $meta);
94
95        if (empty($TOC)) { return true; }
96        $event->preventDefault();
97
98        $html = p_render('xhtml', p_get_instructions($TOC), $INFO);
99        // $html = html_secedit($html,false);
100        if ($INFO['prependTOC']) $html = tpl_toc(true) . $html;
101
102        @unlink(metaFN($ID, '.meta'));
103
104        $ID = (string) $originalID;
105        echo $html;
106        return true;
107    }
108
109    /**
110     * Inserts a toolbar button
111     */
112    function siteexport_aggregate_button(& $event, $param) {
113        $event->data[] = array (
114            'type' => 'mediapopup',
115            'title' => $this->getLang('toolbarButton'),
116            'icon' => '../../plugins/siteexport/images/toolbar.png',
117            'url' => 'lib/plugins/siteexport/exe/siteexportmanager.php?ns=',
118            'options' => 'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes',
119            'block' => false,
120        );
121    }
122}
123