Files
luxtools-plugin/syntax.php
Andreas Gohr 28af4b6776 Major Refactoring (will need some cleanup)
This refactors the plugin from one mega syntax file into multiple
classes.

The plugin now focuses on one usecase: listing files that are external
to the wiki and making them downloadable. All other usecases have been
dropped. Also a bunch of other options have been dropped.

A new dispatcher makes it possible to deliver files without the need to
have the webserver pointing at them.
2024-02-27 14:49:08 +01:00

149 lines
4.0 KiB
PHP

<?php
use dokuwiki\Extension\SyntaxPlugin;
use dokuwiki\plugin\filelist\Crawler;
use dokuwiki\plugin\filelist\Output;
use dokuwiki\plugin\filelist\Path;
/**
* Filelist Plugin: Lists files matching a given glob pattern.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Gina Haeussge <osd@foosel.net>
*/
class syntax_plugin_filelist extends SyntaxPlugin
{
/** @inheritdoc */
public function getType()
{
return 'substition';
}
/** @inheritdoc */
public function getPType()
{
return 'block';
}
/** @inheritdoc */
public function getSort()
{
return 222;
}
/** @inheritdoc */
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('\{\{filelist>.+?\}\}', $mode, 'plugin_filelist');
}
/** @inheritdoc */
public function handle($match, $state, $pos, Doku_Handler $handler)
{
global $INPUT;
// do not allow the syntax in discussion plugin comments
if (!$this->getConf('allow_in_comments') && $INPUT->has('comment')) {
return false;
}
$match = substr($match, strlen('{{filelist>'), -2);
[$path, $flags] = explode('&', $match, 2);
// load default config options
$flags = $this->getConf('defaults') . '&' . $flags;
$flags = explode('&', $flags);
$params = [
'sort' => 'name',
'order' => 'asc',
'index' => 0,
'limit' => 0,
'offset' => 0,
'style' => 'list',
'tableheader' => 0,
'tableshowsize' => 0,
'tableshowdate' => 0,
'direct' => 0,
'recursive' => 0,
'titlefile' => '_title.txt',
'cache' => 0,
'randlinks' => 0,
'preview' => 0,
'previewsize' => 32,
'link' => 2,
'showsize' => 0,
'showdate' => 0,
'showcreator' => 0,
'listsep' => '", "',
'onhover' => 0,
'ftp' => 0
];
foreach ($flags as $flag) {
[$name, $value] = explode('=', $flag);
$params[trim($name)] = trim(trim($value), '"'); // quotes can be use to keep whitespace
}
// separate path and pattern
$path = Path::cleanPath($path, false);
$parts = explode('/', $path);
$pattern = array_pop($parts);
$base = join('/', $parts) . '/';
return [$base, $pattern, $params];
}
/**
* Create output
*/
public function render($format, Doku_Renderer $renderer, $data)
{
[$base, $pattern, $params] = $data;
if ($format != 'xhtml' && $format != 'odt') {
return false;
}
// disable caching
if ($params['cache'] === 0) {
$renderer->nocache();
}
try {
$pathHelper = new Path($this->getConf('paths'));
$pathInfo = $pathHelper->getPathInfo($base);
} catch (Exception $e) {
$renderer->cdata('[n/a: ' . $this->getLang('error_outsidejail') . ']');
return true;
}
$crawler = new Crawler($this->getConf('extensions'));
$crawler->setSortBy($params['sort']);
$crawler->setSortReverse($params['order'] === 'desc');
$result = $crawler->crawl($pathInfo['root'], $pathInfo['local'], $pattern, $params['recursive'], $params['titlefile']);
// if we got nothing back, display a message
if ($result == []) {
$renderer->cdata('[n/a: ' . $this->getLang('error_nomatch') . ']');
return true;
}
$output = new Output($renderer, $pathInfo['root'], $pathInfo['web'], $result);
switch ($params['style']) {
case 'list':
case 'olist':
$output->renderAsList($params);
break;
case 'table':
$output->renderAsTable($params);
break;
}
return true;
}
}