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            // Remove entries that are  from the selectes version, but only if more than these entries exists
72            $baseVersion = intval(p_get_metadata($exportBase, 'mergecompare'));
73            if ( $baseVersion == 0 ) {
74                // Not set?!
75                // print_r($values);
76                $baseVersion = $values[count($values)-1][2];
77            }
78
79            $checkValues = array_filter($values, array(new helper_plugin_siteexport_page_remove($baseVersion +1), '_page_remove'));
80            if ( count($checkValues) > 0 ) {
81                $values = $checkValues;
82            }
83        }
84
85        $originalID = (string) $ID;
86
87        // Generate a TOC that can be exported
88        $TOC = "<toc merge mergeheader";
89
90        // add a mergehint, or better remove it if not required
91        if( $INPUT->bool('mergehint', true, true ) ) {
92            $TOC .= " mergehint";
93        }
94
95        $TOC .= ">\n";
96        $thema = array();
97        foreach( $values as $value ) {
98            list($id, $title) = $value;
99
100            $thema[] = p_get_metadata($id, 'thema', METADATA_RENDER_USING_SIMPLE_CACHE);
101            $TOC .= "  * [[{$id}|{$title}]]\n";
102        }
103
104        $TOC .= "</toc>";
105
106        // Only get first and last element
107        $thema = array_reverse(array_unique(array(reset($thema), end($thema))));
108
109        $meta = p_read_metadata($originalID);
110        // Temporary ID for rendering a document.
111        $ID = (string) cleanID($originalID . '-toc-' . implode('-', array_filter($thema)));
112
113        $meta['current']['thema'] = implode(' - ', array_filter($thema));
114        p_save_metadata($originalID, $meta);
115        p_save_metadata($ID, $meta);
116
117        if (empty($TOC)) { return true; }
118        $event->preventDefault();
119
120        $renderer = $INPUT->str('renderer', $conf['renderer_xhtml'], true);
121        $INPUT->set('do', 'export_' . $renderer);
122
123        $html = p_render($renderer, p_get_instructions($TOC), $INFO);
124        if ($INFO['prependTOC']) $html = tpl_toc(true) . $html;
125
126        if (@unlink(metaFN($ID, '.meta')) === false) {
127            dbglog("Could not delete old meta file", metaFN($ID, '.meta'), 1 );
128        }
129
130        $ID = (string) $originalID;
131        echo $html;
132
133        return true;
134    }
135
136    /**
137     * Inserts a toolbar button
138     */
139    public function siteexport_aggregate_button(& $event, $param) {
140        $event->data[] = array (
141            'type' => 'mediapopup',
142            'title' => $this->getLang('toolbarButton'),
143            'icon' => '../../plugins/siteexport/images/toolbar.png',
144            'url' => 'lib/plugins/siteexport/exe/siteexportmanager.php?ns=',
145            'options' => 'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes',
146            'block' => false,
147        );
148    }
149}
150