-
Notifications
You must be signed in to change notification settings - Fork 9
/
Output.php
283 lines (239 loc) · 8.38 KB
/
Output.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
<?php
namespace dokuwiki\plugin\filelist;
class Output
{
/** @var \Doku_Renderer */
protected $renderer;
/** @var string */
protected $basedir;
/** @var string */
protected $webdir;
/** @var array */
protected $files;
public function __construct(\Doku_Renderer $renderer, $basedir, $webdir, $files)
{
$this->renderer = $renderer;
$this->basedir = $basedir;
$this->webdir = $webdir;
$this->files = $files;
}
public function renderAsList($params)
{
if ($this->renderer instanceof \Doku_Renderer_xhtml) {
$this->renderer->doc .= '<div class="filelist-plugin">';
}
$this->renderListItems($this->files, $params);
if ($this->renderer instanceof \Doku_Renderer_xhtml) {
$this->renderer->doc .= '</div>';
}
}
/**
* Renders the files as a table, including details if configured that way.
*
* @param array $params the parameters of the filelist call
*/
public function renderAsTable($params)
{
if ($this->renderer instanceof \Doku_Renderer_xhtml) {
$this->renderer->doc .= '<div class="filelist-plugin">';
}
$items = $this->flattenResultTree($this->files);
$this->renderTableItems($items, $params);
if ($this->renderer instanceof \Doku_Renderer_xhtml) {
$this->renderer->doc .= '</div>';
}
}
/**
* Renders the files as a table, including details if configured that way.
*
* @param array $params the parameters of the filelist call
*/
protected function renderTableItems($items, $params)
{
$renderer = $this->renderer;
// count the columns
$columns = 1;
if ($params['showsize']) {
$columns++;
}
if ($params['showdate']) {
$columns++;
}
$renderer->table_open($columns);
if ($params['tableheader']) {
$renderer->tablethead_open();
$renderer->tablerow_open();
$renderer->tableheader_open();
$renderer->cdata($this->getLang('filename'));
$renderer->tableheader_close();
if ($params['showsize']) {
$renderer->tableheader_open();
$renderer->cdata($this->getLang('filesize'));
$renderer->tableheader_close();
}
if ($params['showdate']) {
$renderer->tableheader_open();
$renderer->cdata($this->getLang('lastmodified'));
$renderer->tableheader_close();
}
$renderer->tablerow_close();
$renderer->tablethead_close();
}
$renderer->tabletbody_open();
foreach ($items as $item) {
$renderer->tablerow_open();
$renderer->tablecell_open();
$this->renderItemLink($item, $params['randlinks']);
$renderer->tablecell_close();
if ($params['showsize']) {
$renderer->tablecell_open(1, 'right');
$renderer->cdata(filesize_h($item['size']));
$renderer->tablecell_close();
}
if ($params['showdate']) {
$renderer->tablecell_open();
$renderer->cdata(dformat($item['mtime']));
$renderer->tablecell_close();
}
$renderer->tablerow_close();
}
$renderer->tabletbody_close();
$renderer->table_close();
}
/**
* Recursively renders a tree of files as list items.
*
* @param array $items the files to render
* @param array $params the parameters of the filelist call
* @param int $level the level to render
* @return void
*/
protected function renderListItems($items, $params, $level = 1)
{
if ($params['style'] == 'olist') {
$this->renderer->listo_open();
} else {
$this->renderer->listu_open();
}
foreach ($items as $file) {
if ($file['children'] === false && $file['treesize'] === 0) continue; // empty directory
$this->renderer->listitem_open($level);
$this->renderer->listcontent_open();
if ($file['children'] !== false && $file['treesize'] > 0) {
// render the directory and its subtree
$this->renderer->cdata($file['name']);
$this->renderListItems($file['children'], $params, $level + 1);
} elseif ($file['children'] === false) {
// render the file link
$this->renderItemLink($file, $params['randlinks']);
// render filesize
if ($params['showsize']) {
$this->renderer->cdata($params['listsep'] . filesize_h($file['size']));
}
// render lastmodified
if ($params['showdate']) {
$this->renderer->cdata($params['listsep'] . dformat($file['mtime']));
}
}
$this->renderer->listcontent_close();
$this->renderer->listitem_close();
}
if ($params['style'] == 'olist') {
$this->renderer->listo_close();
} else {
$this->renderer->listu_close();
}
}
protected function renderItemLink($item, $cachebuster = false)
{
if ($this->renderer instanceof \Doku_Renderer_xhtml) {
$this->renderItemLinkXHTML($item, $cachebuster);
} else {
$this->renderItemLinkAny($item, $cachebuster);
}
}
/**
* Render a file link on the XHTML renderer
*/
protected function renderItemLinkXHTML($item, $cachebuster = false)
{
global $conf;
/** @var \Doku_Renderer_xhtml $renderer */
$renderer = $this->renderer;
//prepare for formating
$link['target'] = $conf['target']['extern'];
$link['style'] = '';
$link['pre'] = '';
$link['suf'] = '';
$link['more'] = '';
$link['url'] = $this->itemWebUrl($item, $cachebuster);
$link['name'] = $item['name'];
$link['title'] = $renderer->_xmlEntities($link['url']);
if ($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
[$ext,] = mimetype(basename($item['local']));
$link['class'] = 'media mediafile mf_' . $ext;
$renderer->doc .= $renderer->_formatLink($link);
}
/**
* Render a file link on any Renderer
* @param array $item
* @param bool $cachebuster
* @return void
*/
protected function renderItemLinkAny($item, $cachebuster = false)
{
$this->renderer->externalmedialink($this->itemWebUrl($item, $cachebuster), $item['name']);
}
/**
* Construct the Web URL for a given item
*
* @param array $item The item data as returned by the Crawler
* @param bool $cachbuster add a cachebuster to the URL?
* @return string
*/
protected function itemWebUrl($item, $cachbuster = false)
{
if (str_ends_with($this->webdir, '=')) {
$url = $this->webdir . rawurlencode($item['local']);
} else {
$url = $this->webdir . $item['local'];
}
if ($cachbuster) {
if (strpos($url, '?') === false) {
$url .= '?t=' . $item['mtime'];
} else {
$url .= '&t=' . $item['mtime'];
}
}
return $url;
}
/**
* Flattens the filelist by recursively walking through all subtrees and
* merging them with a prefix attached to the filenames.
*
* @param array $items the tree to flatten
* @param string $prefix the prefix to attach to all processed nodes
* @return array a flattened representation of the tree
*/
protected function flattenResultTree($items, $prefix = '')
{
$result = [];
foreach ($items as $file) {
if ($file['children'] !== false) {
$result = array_merge(
$result,
$this->flattenResultTree($file['children'], $prefix . $file['name'] . '/')
);
} else {
$file['name'] = $prefix . $file['name'];
$result[] = $file;
}
}
return $result;
}
protected function getLang($key)
{
$syntax = plugin_load('syntax', 'filelist');
return $syntax->getLang($key);
}
}