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        $data=0;
24        $match=substr($match,10,-2);
25        $matches=explode(">",$match);
26        $matches[1]=str_replace(":","/",$matches[1]);
27        switch ($matches[0]) {
28            case "PAGES":
29                search($list,$conf['datadir'].$matches[1],array($this,'_search_count'),array('all'=>false),'');
30                $data = $list['file_count'];
31                break;
32
33            case "MEDIAS":
34                search($list,$conf['mediadir'].$matches[1],array($this,'_search_count'),array('all'=>true));
35                $data    = $list['file_count'];
36                break;
37          }
38        return $data;
39    }
40
41    public function render($mode, Doku_Renderer $renderer, $data) {
42        if($mode != 'xhtml') return false;
43        $renderer->doc.= $data;
44        return true;
45    }
46
47    function _search_count(&$data,$base,$file,$type,$lvl,$opts){
48        if($type == 'd'){
49            if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
50            $data['dir_count']++;
51            return true;
52        }
53        if($opts['all'] || substr($file,-4) == '.txt'){
54            $data['file_count']++;
55        }
56        return false;
57    }
58}
59
60// vim:ts=4:sw=4:et:
61