xref: /plugin/siteexport/action/aggregate.php (revision 8ef92d7af0c60802dae178d7910acaa762474256)
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            $lookupNS = noNS($namespace) == $conf['start'] ? $namespace : $namespace . ':' . $conf['start'];
55            $values = $functions->__getOrderedListOfPagesForID($lookupNS, $exportBase);
56            $values = array(end( $values )); // the list above has the $exportBase element at the very end
57        }
58
59    	$this->originalID = (string) $ID;
60
61        // Generate a TOC that can be exported
62        $TOC = "~~NOCACHE~~\n<toc merge mergeheader>\n";
63        $thema = array();
64        foreach( $values as $value ) {
65        	list($id, $title, $sort) = $value;
66
67        	$thema[] = p_get_metadata($id, 'thema', METADATA_RENDER_USING_SIMPLE_CACHE);
68        	$TOC .= "  * [[{$id}|{$title}]]\n";
69        }
70
71        $TOC .= "</toc>";
72
73        // Only get first and last element
74        $thema = array_unique(array(reset($thema), end($thema)));
75
76        $meta = p_read_metadata($ID);
77        $ID = (string) cleanID($ID . '-toc-' . implode('-', array_filter($thema)));
78
79        $meta['current']['thema'] = implode(' - ', array_filter($thema));
80        p_save_metadata($ID, $meta);
81
82        $this->instructions = $TOC;
83	}
84
85	function siteexport_aggregate(&$event)
86	{
87		global $ID, $INFO;
88
89        if ( empty($this->instructions) ) { return true; }
90        $event->preventDefault();
91
92        $html = p_render('xhtml', p_get_instructions($this->instructions),$INFO);
93        $html = html_secedit($html,false);
94        if($INFO['prependTOC']) $html = tpl_toc(true).$html;
95
96        @unlink(metaFN($ID, '.meta'));
97
98        $ID = (string) $this->originalID;
99
100        echo $html;
101        return false;
102	}
103
104	/**
105	 * Inserts a toolbar button
106	 */
107	function siteexport_aggregate_button(& $event, $param) {
108	    $event->data[] = array (
109	        'type' => 'mediapopup',
110	        'title' => $this->getLang('toolbarButton'),
111	        'icon' => '../../plugins/siteexport/images/toolbar.png',
112	        'url' => 'lib/plugins/siteexport/exe/siteexportmanager.php?ns=',
113	        'options' => 'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes',
114	        'block' => false,
115	    );
116	}
117}