131 lines
3.5 KiB
PHP
131 lines
3.5 KiB
PHP
<?php
|
|
|
|
use dokuwiki\plugin\luxtools\Output;
|
|
|
|
require_once(__DIR__ . '/AbstractSyntax.php');
|
|
|
|
/**
|
|
* luxtools Plugin: Image gallery syntax.
|
|
*
|
|
* Renders a thumbnail gallery of images matching a glob pattern.
|
|
*/
|
|
class syntax_plugin_luxtools_images extends syntax_plugin_luxtools_abstract
|
|
{
|
|
/** @inheritdoc */
|
|
protected function getSyntaxKeyword(): string
|
|
{
|
|
return 'images';
|
|
}
|
|
|
|
/** @inheritdoc */
|
|
protected function getDefaultParams(): array
|
|
{
|
|
// Images syntax doesn't use some of the common params
|
|
return [
|
|
'style' => null,
|
|
'tableheader' => null,
|
|
'showsize' => null,
|
|
'showdate' => null,
|
|
'listsep' => null,
|
|
];
|
|
}
|
|
|
|
/** @inheritdoc */
|
|
protected function processPath(string $path): array
|
|
{
|
|
[$base, $pattern] = $this->separatePathAndPattern($path);
|
|
return ['base' => $base, 'pattern' => $pattern];
|
|
}
|
|
|
|
/** @inheritdoc */
|
|
protected function doRender(string $format, \Doku_Renderer $renderer, array $pathData, array $params): bool
|
|
{
|
|
$pathInfo = $this->getPathInfoSafe($pathData['base'], $renderer);
|
|
if ($pathInfo === false) {
|
|
return true;
|
|
}
|
|
|
|
$crawler = $this->createCrawler($params);
|
|
$result = $crawler->crawl(
|
|
$pathInfo['root'],
|
|
$pathInfo['local'],
|
|
$pathData['pattern'],
|
|
$params['recursive'],
|
|
$params['titlefile']
|
|
);
|
|
|
|
$items = $this->flattenResultTree($result);
|
|
$items = $this->filterImages($items);
|
|
|
|
if ($items == []) {
|
|
$this->renderEmptyState($renderer, 'empty_images');
|
|
return true;
|
|
}
|
|
|
|
$output = new Output($renderer, $pathInfo['root'], $pathInfo['web'], $items);
|
|
|
|
if ($format == 'xhtml') {
|
|
$output->renderAsGallery($params);
|
|
return true;
|
|
}
|
|
|
|
// Fallback for non-XHTML formats: render as a list of links
|
|
$params['style'] = 'list';
|
|
$params['showsize'] = 0;
|
|
$params['showdate'] = 0;
|
|
$params['listsep'] = ', ';
|
|
$output->renderAsList($params);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Flattens the crawl result tree into a list of file items.
|
|
*
|
|
* @param array $items
|
|
* @param string $prefix
|
|
* @return array
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Keep only image files.
|
|
*
|
|
* @param array $items
|
|
* @return array
|
|
*/
|
|
protected function filterImages($items)
|
|
{
|
|
$images = [];
|
|
foreach ($items as $item) {
|
|
if (!isset($item['path']) || !is_string($item['path'])) continue;
|
|
if (!is_file($item['path'])) continue;
|
|
|
|
try {
|
|
[, $mime,] = mimetype($item['path'], false);
|
|
} catch (Throwable $e) {
|
|
continue;
|
|
}
|
|
|
|
if (is_string($mime) && str_starts_with($mime, 'image/')) {
|
|
$images[] = $item;
|
|
}
|
|
}
|
|
return $images;
|
|
}
|
|
}
|