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