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