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