Add directory listing syntax
Some checks failed
DokuWiki Default Tasks / all (push) Has been cancelled

This commit is contained in:
2026-01-06 08:56:42 +01:00
parent e59970e0b8
commit 0ad43bcf9c
4 changed files with 311 additions and 1 deletions

View File

@@ -122,6 +122,77 @@ class Crawler
return $this->sortItems($result);
}
/**
* List the direct children (files and directories) of a given local path.
*
* Unlike crawl(), this includes directories even when not recursing.
*
* @param string $root
* @param string $local
* @param string $titlefile
* @return array
*/
public function listDirectory($root, $local, $titlefile)
{
$path = $root . $local;
$path = rtrim($path, '/');
// do not list wiki or data directories
if (Path::isWikiControlled($path)) return [];
if (($dir = opendir($path)) === false) return [];
$result = [];
while (($file = readdir($dir)) !== false) {
if ($file[0] == '.' || $file == $titlefile) {
// ignore hidden, system and title files
continue;
}
$filepath = $path . '/' . $file;
if (!is_readable($filepath)) continue;
$isDir = is_dir($filepath);
if (!$isDir && !$this->isExtensionAllowed($file)) {
continue;
}
if ($this->isFileIgnored($file)) {
continue;
}
// get title file (directories only)
$filename = $file;
if ($isDir) {
$title = $filepath . '/' . $titlefile;
if (is_readable($title)) {
$filename = io_readFile($title, false);
}
}
// build a local path consistent with crawl() (leading slash for root)
$self = rtrim($local, '/') . '/' . $file;
if ($self === '/' . $file) {
// keep the original behaviour when local is empty
$self = '/' . $file;
}
$entry = [
'name' => $filename,
'local' => $self,
'path' => $filepath,
'mtime' => filemtime($filepath),
'ctime' => filectime($filepath),
'size' => $isDir ? 0 : filesize($filepath),
'children' => false,
'treesize' => 1,
'isdir' => $isDir,
];
$result[] = $entry;
}
closedir($dir);
return $this->sortItems($result);
}
/**
* Sort the given items by the current sortby and sortreverse settings
*