1<?php
2/**
3 * DokuWiki Plugin numberof (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  S.C. Yoo <dryoo@live.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_numberof extends DokuWiki_Syntax_Plugin {
13    public function getType() { return 'substition'; }
14    public function getSort() { return 32; }
15
16    public function connectTo($mode) {
17      $this->Lexer->addSpecialPattern('\{\{NUMBEROF[^\}]*\}\}',$mode,'plugin_numberof');
18    }
19
20    public function handle($match, $state, $pos, Doku_Handler $handler){
21	global $conf;
22        $list = array(
23            'file_count' => 0,
24            'dir_count' => 0,
25            'dir_nest' => 0,
26        );
27        $match=substr($match,10,-2);
28        $matches = sexplode(">", $match, 2, '');
29        $matches[1]=str_replace(":","/",$matches[1]);
30        switch ($matches[0]) {
31            case "PAGES":
32                search($list,$conf['datadir'].$matches[1],array($this,'_search_count'),array('all'=>false),'');
33                break;
34
35            case "MEDIAS":
36                search($list,$conf['mediadir'].$matches[1],array($this,'_search_count'),array('all'=>true));
37                break;
38          }
39        return ['count' => $list['file_count']];
40    }
41
42    public function render($mode, Doku_Renderer $renderer, $data) {
43        if($mode != 'xhtml') return false;
44        $renderer->doc .= $data['count'];
45        return true;
46    }
47
48    function _search_count(&$data,$base,$file,$type,$lvl,$opts){
49        if($type == 'd'){
50            if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
51            $data['dir_count']++;
52            return true;
53        }
54        if($opts['all'] || substr($file,-4) == '.txt'){
55            $data['file_count']++;
56        }
57        return false;
58    }
59}
60
61// vim:ts=4:sw=4:et:
62