Files
luxtools-plugin/syntax/images.php
2026-01-22 20:27:47 +01:00

123 lines
3.2 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 [
'tableheader' => null,
'showsize' => null,
'showdate' => 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;
}
// Images syntax only supports XHTML format (gallery rendering)
if ($format !== 'xhtml') {
return false;
}
$output = new Output($renderer, $pathInfo['root'], $pathInfo['web'], $items, $this);
$output->renderAsGallery($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;
}
}