xref: /plugin/filelist/syntax.php (revision 55e6f8f9aab64fe9ab92bb6bac15d4d9fb65d4b5)
1b11bec64S[Author not set in preferences]<?php
21c9be825SAndreas Gohr
31c9be825SAndreas Gohruse dokuwiki\Extension\SyntaxPlugin;
428af4b67SAndreas Gohruse dokuwiki\plugin\filelist\Crawler;
528af4b67SAndreas Gohruse dokuwiki\plugin\filelist\Output;
628af4b67SAndreas Gohruse dokuwiki\plugin\filelist\Path;
71c9be825SAndreas Gohr
8b11bec64S[Author not set in preferences]/**
9b11bec64S[Author not set in preferences] * Filelist Plugin: Lists files matching a given glob pattern.
10b11bec64S[Author not set in preferences] *
11b11bec64S[Author not set in preferences] * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
12b11bec64S[Author not set in preferences] * @author     Gina Haeussge <osd@foosel.net>
13b11bec64S[Author not set in preferences] */
141c9be825SAndreas Gohrclass syntax_plugin_filelist extends SyntaxPlugin
151c9be825SAndreas Gohr{
1628af4b67SAndreas Gohr    /** @inheritdoc */
171c9be825SAndreas Gohr    public function getType()
181c9be825SAndreas Gohr    {
191c9be825SAndreas Gohr        return 'substition';
201c9be825SAndreas Gohr    }
21b11bec64S[Author not set in preferences]
2228af4b67SAndreas Gohr    /** @inheritdoc */
231c9be825SAndreas Gohr    public function getPType()
241c9be825SAndreas Gohr    {
251c9be825SAndreas Gohr        return 'block';
261c9be825SAndreas Gohr    }
271c9be825SAndreas Gohr
2828af4b67SAndreas Gohr    /** @inheritdoc */
291c9be825SAndreas Gohr    public function getSort()
301c9be825SAndreas Gohr    {
311c9be825SAndreas Gohr        return 222;
321c9be825SAndreas Gohr    }
331c9be825SAndreas Gohr
3428af4b67SAndreas Gohr    /** @inheritdoc */
351c9be825SAndreas Gohr    public function connectTo($mode)
361c9be825SAndreas Gohr    {
37b11bec64S[Author not set in preferences]        $this->Lexer->addSpecialPattern('\{\{filelist>.+?\}\}', $mode, 'plugin_filelist');
38b11bec64S[Author not set in preferences]    }
39b11bec64S[Author not set in preferences]
4028af4b67SAndreas Gohr    /** @inheritdoc */
411c9be825SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
421c9be825SAndreas Gohr    {
4328af4b67SAndreas Gohr        global $INPUT;
44b11bec64S[Author not set in preferences]
4528af4b67SAndreas Gohr        // do not allow the syntax in discussion plugin comments
4628af4b67SAndreas Gohr        if (!$this->getConf('allow_in_comments') && $INPUT->has('comment')) {
47b11bec64S[Author not set in preferences]            return false;
48b11bec64S[Author not set in preferences]        }
4928af4b67SAndreas Gohr
5028af4b67SAndreas Gohr        $match = substr($match, strlen('{{filelist>'), -2);
5128af4b67SAndreas Gohr        [$path, $flags] = explode('&', $match, 2);
52b11bec64S[Author not set in preferences]
5308094166SAndreas Gohr        // load default config options
5408094166SAndreas Gohr        $flags = $this->getConf('defaults') . '&' . $flags;
55cedaaa98SAndreas Gohr        $flags = explode('&', $flags);
561c9be825SAndreas Gohr
5728af4b67SAndreas Gohr        $params = [
5828af4b67SAndreas Gohr            'sort' => 'name',
5928af4b67SAndreas Gohr            'order' => 'asc',
6028af4b67SAndreas Gohr            'style' => 'list',
6128af4b67SAndreas Gohr            'tableheader' => 0,
6228af4b67SAndreas Gohr            'recursive' => 0,
6328af4b67SAndreas Gohr            'titlefile' => '_title.txt',
6428af4b67SAndreas Gohr            'cache' => 0,
6528af4b67SAndreas Gohr            'randlinks' => 0,
6628af4b67SAndreas Gohr            'showsize' => 0,
6728af4b67SAndreas Gohr            'showdate' => 0,
68*55e6f8f9SAndreas Gohr            'listsep' => ', ',
6928af4b67SAndreas Gohr        ];
70b11bec64S[Author not set in preferences]        foreach ($flags as $flag) {
711eeb87d7SAndreas Gohr            [$name, $value] = sexplode('=', $flag, 2, '');
7228af4b67SAndreas Gohr            $params[trim($name)] = trim(trim($value), '"'); // quotes can be use to keep whitespace
73b11bec64S[Author not set in preferences]        }
74b11bec64S[Author not set in preferences]
7528af4b67SAndreas Gohr        // separate path and pattern
7628af4b67SAndreas Gohr        $path = Path::cleanPath($path, false);
7728af4b67SAndreas Gohr        $parts = explode('/', $path);
7828af4b67SAndreas Gohr        $pattern = array_pop($parts);
7905f444a4SAndreas Gohr        $base = implode('/', $parts) . '/';
80ada0b695SGina Haeussge
8128af4b67SAndreas Gohr        return [$base, $pattern, $params];
82b11bec64S[Author not set in preferences]    }
83b11bec64S[Author not set in preferences]
84b11bec64S[Author not set in preferences]    /**
85b11bec64S[Author not set in preferences]     * Create output
86b11bec64S[Author not set in preferences]     */
8728af4b67SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
881c9be825SAndreas Gohr    {
8928af4b67SAndreas Gohr        [$base, $pattern, $params] = $data;
90b11bec64S[Author not set in preferences]
9128af4b67SAndreas Gohr        if ($format != 'xhtml' && $format != 'odt') {
9228af4b67SAndreas Gohr            return false;
937441ea06SLarsDW223        }
947441ea06SLarsDW223
95ada0b695SGina Haeussge        // disable caching
9641ca2bd3Slpaulsen93        if ($params['cache'] === 0) {
9741ca2bd3Slpaulsen93            $renderer->nocache();
9841ca2bd3Slpaulsen93        }
99ada0b695SGina Haeussge
10028af4b67SAndreas Gohr
10128af4b67SAndreas Gohr        try {
10228af4b67SAndreas Gohr            $pathHelper = new Path($this->getConf('paths'));
10328af4b67SAndreas Gohr            $pathInfo = $pathHelper->getPathInfo($base);
10428af4b67SAndreas Gohr        } catch (Exception $e) {
10537723cbeSLarsDW223            $renderer->cdata('[n/a: ' . $this->getLang('error_outsidejail') . ']');
106b11bec64S[Author not set in preferences]            return true;
107b11bec64S[Author not set in preferences]        }
108b11bec64S[Author not set in preferences]
10928af4b67SAndreas Gohr        $crawler = new Crawler($this->getConf('extensions'));
11028af4b67SAndreas Gohr        $crawler->setSortBy($params['sort']);
11128af4b67SAndreas Gohr        $crawler->setSortReverse($params['order'] === 'desc');
11205f444a4SAndreas Gohr
11305f444a4SAndreas Gohr        $result = $crawler->crawl(
11405f444a4SAndreas Gohr            $pathInfo['root'],
11505f444a4SAndreas Gohr            $pathInfo['local'],
11605f444a4SAndreas Gohr            $pattern,
11705f444a4SAndreas Gohr            $params['recursive'],
11805f444a4SAndreas Gohr            $params['titlefile']
11905f444a4SAndreas Gohr        );
12028af4b67SAndreas Gohr
12128af4b67SAndreas Gohr        // if we got nothing back, display a message
12228af4b67SAndreas Gohr        if ($result == []) {
12328af4b67SAndreas Gohr            $renderer->cdata('[n/a: ' . $this->getLang('error_nomatch') . ']');
12428af4b67SAndreas Gohr            return true;
125b11bec64S[Author not set in preferences]        }
126b11bec64S[Author not set in preferences]
12728af4b67SAndreas Gohr        $output = new Output($renderer, $pathInfo['root'], $pathInfo['web'], $result);
128b11bec64S[Author not set in preferences]
129b11bec64S[Author not set in preferences]        switch ($params['style']) {
130b11bec64S[Author not set in preferences]            case 'list':
131b11bec64S[Author not set in preferences]            case 'olist':
13228af4b67SAndreas Gohr                $output->renderAsList($params);
133b11bec64S[Author not set in preferences]                break;
134b11bec64S[Author not set in preferences]            case 'table':
13528af4b67SAndreas Gohr                $output->renderAsTable($params);
136ada0b695SGina Haeussge                break;
137ada0b695SGina Haeussge        }
138ada0b695SGina Haeussge        return true;
1393c09f837SLarsDW223    }
140ada0b695SGina Haeussge}
141