xref: /plugin/siteexport/action/aggregate.php (revision 6292ca7aa5efa4bec9bd483c6855b04a53f4166c)
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    private function prefixStart($entry) {
26        return noNS($namespace) == $conf['start'] ? $namespace : $namespace . ':' . $conf['start'];
27    }
28
29    public function siteexport_aggregate(Doku_Event &$event)
30    {
31        global $ID, $INFO, $conf, $INPUT;
32
33        // Aggregate only if
34        // (1) this page really has an aggregator and we did submit a request to do so
35        // (2) this page really has an aggregator and we export as PDF
36        if ( !( (!empty($INFO['meta']['siteexport']) && $INFO['meta']['siteexport']['hasaggregator'] == true) && ( $INPUT->has( 'siteexport_aggregate' ) || $conf['renderer_xhtml'] == 'siteexport_pdf' ) ) ) { return true; }
37
38        $exportBase = $INPUT->str('baseID');
39        $namespaces = empty($exportBase) ? $INFO['meta']['siteexport']['baseID'] : getNs($exportBase);
40        $namespaces = explode('|', $namespaces);
41        $namespaces = array_map('cleanID', $namespaces);
42        $namespace = $exportBase = $namespaces[0];
43
44        $functions = plugin_load('helper', 'siteexport');
45        $values = $functions->__getOrderedListOfPagesForID($namespaces, $exportBase);
46
47        // If no base given, take the first one from the ordered list.
48        if ( empty($exportBase) ) {
49            // Reset to latest element
50            list($exportBase) = reset( $values );
51        }
52
53        // If only the one file should be exported, strip it down.
54        if ( $INPUT->bool('exportSelectedVersionOnly' ) ) {
55            // Strip down values
56            $lookupNS = array_map(array($this, 'prefixStart'), $namespaces);
57            if ( $INPUT->has( 'mergecompare_start' ) && $INPUT->has( 'mergecompare_end' ) ) {
58                    $values = $functions->__getOrderedListOfPagesForStartEnd($lookupNS, $INPUT->int( 'mergecompare_start' ), $INPUT->int( 'mergecompare_end', PHP_INT_MAX ) );
59            } else {
60                $values = $functions->__getOrderedListOfPagesForID($lookupNS, $exportBase);
61                $values = array(end( $values )); // the list above has the $exportBase element at the very end
62            }
63        }
64
65        $includeSelected = $INPUT->str('includeSelectedVersion', 'true', true ) === 'true';
66        if( !$includeSelected && count( $values ) > 1 ) {
67            array_pop( $values ); // Remove last entry which is the selected version, but only if more than one entry exists
68        }
69
70        $originalID = (string) $ID;
71
72        // Generate a TOC that can be exported
73        $TOC = "<toc merge mergeheader";
74
75        // add a mergehint, or better remove it if not required
76        if( $INPUT->bool('mergehint', true, true ) ) {
77            $TOC .= " mergehint";
78        }
79
80        $TOC .= ">\n";
81        $thema = array();
82        foreach( $values as $value ) {
83            list($id, $title) = $value;
84
85            $thema[] = p_get_metadata($id, 'thema', METADATA_RENDER_USING_SIMPLE_CACHE);
86            $TOC .= "  * [[{$id}|{$title}]]\n";
87        }
88
89        $TOC .= "</toc>";
90
91        // Only get first and last element
92        $thema = array_reverse(array_unique(array(reset($thema), end($thema))));
93
94        $meta = p_read_metadata($originalID);
95        // Temporary ID for rendering a document.
96        $ID = (string) cleanID($originalID . '-toc-' . implode('-', array_filter($thema)));
97
98        $meta['current']['thema'] = implode(' - ', array_filter($thema));
99        p_save_metadata($originalID, $meta);
100        p_save_metadata($ID, $meta);
101
102        if (empty($TOC)) { return true; }
103        $event->preventDefault();
104
105        $html = p_render('xhtml', p_get_instructions($TOC), $INFO);
106        if ($INFO['prependTOC']) $html = tpl_toc(true) . $html;
107
108        if (@unlink(metaFN($ID, '.meta')) === false) {
109            dbglog("Could not delete old meta file", metaFN($ID, '.meta'), 1 );
110        }
111
112        $ID = (string) $originalID;
113        echo $html;
114        return true;
115    }
116
117    /**
118     * Inserts a toolbar button
119     */
120    public function siteexport_aggregate_button(& $event, $param) {
121        $event->data[] = array (
122            'type' => 'mediapopup',
123            'title' => $this->getLang('toolbarButton'),
124            'icon' => '../../plugins/siteexport/images/toolbar.png',
125            'url' => 'lib/plugins/siteexport/exe/siteexportmanager.php?ns=',
126            'options' => 'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes',
127            'block' => false,
128        );
129    }
130}
131