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