forked from lpaulsen93/dokuwiki-plugin-odt
/
action.php
398 lines (335 loc) · 12.1 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* ODT Plugin: Exports to ODT
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Aurelien Bompard <aurelien@bompard.org>
* @author Florian Lamml <info@florian-lamml.de>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
/**
* Add the template as a page dependency for the caching system
*/
class action_plugin_odt extends DokuWiki_Action_Plugin {
/**
* @var array
*/
protected $list = array();
/**
* Register the events
*
* @param Doku_Event_Handler $controller
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
$controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
$controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handle_cache_prepare');
}
/**
* Add dependencies to cache
*
* @param Doku_Event $event
*/
public function handle_cache_prepare(Doku_Event $event) {
global $conf, $ID;
$cache =& $event->data;
// only the ODT rendering mode needs caching tweaks
if($cache->mode != "odt") return;
$odt_meta = p_get_metadata($ID, 'relation odt');
$template_name = $odt_meta["template"];
if(!$template_name) {
return;
}
$template_path = $conf['mediadir'] . '/' . $this->getConf("tpl_dir") . "/" . $template_name;
if(file_exists($template_path)) {
$cache->depends['files'][] = $template_path;
}
}
/**
* Add 'export odt'-button to pagetools
*
* @param Doku_Event $event
*/
public function addbutton(Doku_Event $event) {
global $ID, $REV;
if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
$params = array('do' => 'export_odt');
if($REV) {
$params['rev'] = $REV;
}
// insert button at position before last (up to top)
$event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
array('export_odt' =>
'<li>'
. '<a href="' . wl($ID, $params) . '" class="action export_odt" rel="nofollow" title="' . $this->getLang('export_odt_button') . '">'
. '<span>' . $this->getLang('export_odt_button') . '</span>'
. '</a>'
. '</li>'
) +
array_slice($event->data['items'], -1, 1, true);
}
}
/***********************************************************************************
* Book export *
***********************************************************************************/
/**
* Do article(s) to ODT conversion work
*
* @param Doku_Event $event
* @return bool
*/
public function convert(Doku_Event $event) {
global $ACT;
global $ID;
// single page export: rename to the actual renderer component
if($ACT == 'export_odt') {
$ACT = 'export_odt_page';
}
// the book export?
if(($ACT != 'export_odtbook') && ($ACT != 'export_odtns')) return false;
// check user's rights
if(auth_quickaclcheck($ID) < AUTH_READ) return false;
if($data = $this->collectExportPages($event)) {
list($title, $this->list) = $data;
} else {
return false;
}
// it's ours, no one else's
$event->preventDefault();
// prepare cache and its dependencies
$depends = array();
$cache = $this->prepareCache($title, $depends);
// hard work only when no cache available
if(!$this->getConf('usecache') || !$cache->useCache($depends)) {
$this->generateODT($cache->cache, $title);
}
// deliver the file
$this->sendODTFile($cache->cache, $title);
return true;
}
/**
* Obtain list of pages and title, based on url parameters
*
* @param Doku_Event $event
* @return string|bool
*/
protected function collectExportPages(Doku_Event $event) {
global $ACT;
global $ID;
global $INPUT;
global $conf;
// list of one or multiple pages
$list = array();
if($ACT == 'export_odt') {
$list[0] = $ID;
$title = $INPUT->str('book_title');
if(!$title) {
$title = p_get_first_heading($ID);
}
} elseif($ACT == 'export_odtns') {
//check input for title and ns
if(!$title = $INPUT->str('book_title')) {
$this->showPageWithErrorMsg($event, 'needtitle');
return false;
}
$docnamespace = cleanID($INPUT->str('book_ns'));
if(!@is_dir(dirname(wikiFN($docnamespace . ':dummy')))) {
$this->showPageWithErrorMsg($event, 'needns');
return false;
}
//sort order
$order = $INPUT->str('book_order', 'natural', true);
$sortoptions = array('pagename', 'date', 'natural');
if(!in_array($order, $sortoptions)) {
$order = 'natural';
}
//search depth
$depth = $INPUT->int('book_nsdepth', 0);
if($depth < 0) {
$depth = 0;
}
//page search
$result = array();
$opts = array('depth' => $depth); //recursive all levels
$dir = utf8_encodeFN(str_replace(':', '/', $docnamespace));
search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
//sorting
if(count($result) > 0) {
if($order == 'date') {
usort($result, array($this, '_datesort'));
} elseif($order == 'pagename') {
usort($result, array($this, '_pagenamesort'));
}
}
foreach($result as $item) {
$list[] = $item['id'];
}
} elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
// Here is $ACT == 'export_odtbook'
//is in Bookmanager of bookcreator plugin a title given?
if(!$title = $INPUT->str('book_title')) {
$this->showPageWithErrorMsg($event, 'needtitle');
return false;
} else {
$list = explode("|", $_COOKIE['list-pagelist']);
}
} else {
//show empty bookcreator message
$this->showPageWithErrorMsg($event, 'empty');
return false;
}
$list = array_map('cleanID', $list);
return array($title, $list);
}
/**
* Set error notification and reload page again
*
* @param Doku_Event $event
* @param string $msglangkey key of translation key
*/
private function showPageWithErrorMsg(Doku_Event $event, $msglangkey) {
msg($this->getLang($msglangkey), -1);
$event->data = 'show';
$_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
}
/**
* Prepare cache
*
* @param string $title
* @param array $depends (reference) array with dependencies
* @return cache
*/
protected function prepareCache($title, &$depends) {
global $REV;
global $INPUT;
//different caches for varying config settings
$template = $this->getConf("tpl_default");
$template = $INPUT->get->str('odt-template', $template, true);
$cachekey = join(',', $this->list)
. $REV
. $template
. $title;
$cache = new cache($cachekey, '.odt');
$dependencies = array();
foreach($this->list as $pageid) {
$relations = p_get_metadata($pageid, 'relation');
if(is_array($relations)) {
if(array_key_exists('media', $relations) && is_array($relations['media'])) {
foreach($relations['media'] as $mediaid => $exists) {
if($exists) {
$dependencies[] = mediaFN($mediaid);
}
}
}
if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
foreach($relations['haspart'] as $part_pageid => $exists) {
if($exists) {
$dependencies[] = wikiFN($part_pageid);
}
}
}
}
$dependencies[] = metaFN($pageid, '.meta');
}
$depends['files'] = array_map('wikiFN', $this->list);
$depends['files'][] = __FILE__;
$depends['files'][] = dirname(__FILE__) . '/renderer/page.php';
$depends['files'][] = dirname(__FILE__) . '/renderer/book.php';
$depends['files'][] = dirname(__FILE__) . '/plugin.info.txt';
$depends['files'] = array_merge(
$depends['files'],
$dependencies,
getConfigFiles('main')
);
return $cache;
}
/**
* Build a ODT from the articles
*
* @param string $cachefile
* @param string $title
*/
protected function generateODT($cachefile, $title) {
global $ID;
global $REV;
/** @var renderer_plugin_odt_book $odt */
$odt = plugin_load('renderer','odt_book');
// store original pageid
$keep = $ID;
// loop over all pages
$xmlcontent = '';
$cnt = count($this->list);
for($n = 0; $n < $cnt; $n++) {
$page = $this->list[$n];
// set global pageid to the rendered page
$ID = $page;
$xmlcontent .= p_render('odt_book', p_cached_instructions(wikiFN($page, $REV),false,$page), $info);
}
//restore ID
$ID = $keep;
$odt->doc = $xmlcontent;
$odt->setTitle($title);
$odt->finalize_ODTfile();
// write to cache file
io_savefile($cachefile, $odt->doc);
}
/**
* @param string $cachefile
* @param string $title
*/
protected function sendODTFile($cachefile, $title) {
header('Content-Type: application/vnd.oasis.opendocument.text');
header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
header('Pragma: public');
http_conditionalRequest(filemtime($cachefile));
$filename = rawurlencode(cleanID(strtr($title, ':/;"', ' ')));
if($this->getConf('output') == 'file') {
header('Content-Disposition: attachment; filename="' . $filename . '.odt";');
} else {
header('Content-Disposition: inline; filename="' . $filename . '.odt";');
}
//try to send file, and exit if done
http_sendfile($cachefile);
$fp = @fopen($cachefile, "rb");
if($fp) {
http_rangeRequest($fp, filesize($cachefile), 'application/vnd.oasis.opendocument.text');
} else {
header("HTTP/1.0 500 Internal Server Error");
print "Could not read file - bad permissions?";
}
exit();
}
/**
* Returns array of wiki pages which will be included in the exported document
*
* @return array
*/
public function getExportedPages() {
return $this->list;
}
/**
* usort callback to sort by file lastmodified time
*
* @param array $a
* @param array $b
* @return int
*/
public function _datesort($a, $b) {
if($b['rev'] < $a['rev']) return -1;
if($b['rev'] > $a['rev']) return 1;
return strcmp($b['id'], $a['id']);
}
/**
* usort callback to sort by page id
*
* @param array $a
* @param array $b
* @return int
*/
public function _pagenamesort($a, $b) {
if($a['id'] <= $b['id']) return -1;
if($a['id'] > $b['id']) return 1;
return 0;
}
}