xref: /plugin/siteexport/action/aggregate.php (revision 23be581e72d006b6a077253cb483e0c847924f00)
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',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    private $instructions = null;
19    private $originalID = null;
20
21    /**
22	* Register Plugin in DW
23	**/
24	public function register(Doku_Event_Handler $controller) {
25		$controller->register_hook('TPL_ACT_RENDER', 'BEFORE',  $this, 'siteexport_aggregate');
26		$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'siteexport_aggregate_button', array ());
27	}
28
29	function siteexport_aggregate(&$event)
30	{
31	    global $ID, $INFO, $conf;
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) && ( isset($_REQUEST['siteexport_aggregate']) || $conf['renderer_xhtml'] == 'siteexport_pdf' ) ) ) { return true; }
37
38		$exportBase = cleanID($_REQUEST['baseID']);
39		$namespace = empty($exportBase) ? $INFO['meta']['siteexport']['baseID'] : getNs($exportBase);
40
41        $functions = plugin_load('helper', 'siteexport');
42        $values = $functions->__getOrderedListOfPagesForID($namespace, $exportBase);
43
44        // If no base given, take the first one from the ordered list.
45        if ( empty($exportBase) ) {
46            // Reset to latest element
47            list($exportBase) = reset( $values );
48        }
49
50        // If only the one file should be exported, strip it down.
51        if ( !empty($_REQUEST['exportSelectedVersionOnly']) ) {
52            // Strip down values
53            $lookupNS = noNS($namespace) == $conf['start'] ? $namespace : $namespace . ':' . $conf['start'];
54
55            if ( !empty( $_REQUEST['mergecompare_start'] ) && !empty( $_REQUEST['mergecompare_end'] ) ) {
56                 $values = $functions->__getOrderedListOfPagesForStartEnd($lookupNS, $_REQUEST['mergecompare_start'], $_REQUEST['mergecompare_end']);
57            } else {
58                $values = $functions->__getOrderedListOfPagesForID($lookupNS, $exportBase);
59                $values = array(end( $values )); // the list above has the $exportBase element at the very end
60            }
61        }
62
63    	$this->originalID = (string) $ID;
64
65        // Generate a TOC that can be exported
66        $TOC = "<toc merge mergeheader>\n";
67        $thema = array();
68        foreach( $values as $value ) {
69        	list($id, $title, $sort) = $value;
70
71        	$thema[] = p_get_metadata($id, 'thema', METADATA_RENDER_USING_SIMPLE_CACHE);
72        	$TOC .= "  * [[{$id}|{$title}]]\n";
73        }
74
75        $TOC .= "</toc>";
76
77        // Only get first and last element
78        $thema = array_unique(array(reset($thema), end($thema)));
79
80        $meta = p_read_metadata($ID);
81        $ID = (string) cleanID($ID . '-toc-' . implode('-', array_filter($thema)));
82
83        $meta['current']['thema'] = implode(' - ', array_filter($thema));
84        p_save_metadata($ID, $meta);
85
86        if ( empty($TOC) ) { return true; }
87        $event->preventDefault();
88
89        $html = p_render('xhtml', p_get_instructions($TOC),$INFO);
90        $html = html_secedit($html,false);
91        if($INFO['prependTOC']) $html = tpl_toc(true).$html;
92
93        @unlink(metaFN($ID, '.meta'));
94
95        $ID = (string) $this->originalID;
96        echo $html;
97        return true;
98	}
99
100	/**
101	 * Inserts a toolbar button
102	 */
103	function siteexport_aggregate_button(& $event, $param) {
104	    $event->data[] = array (
105	        'type' => 'mediapopup',
106	        'title' => $this->getLang('toolbarButton'),
107	        'icon' => '../../plugins/siteexport/images/toolbar.png',
108	        'url' => 'lib/plugins/siteexport/exe/siteexportmanager.php?ns=',
109	        'options' => 'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes',
110	        'block' => false,
111	    );
112	}
113}