1<?php
2if(!defined('DOKU_INC')) die();
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4require_once(DOKU_PLUGIN.'syntax.php');
5class syntax_plugin_nssize extends DokuWiki_Syntax_Plugin {
6  function getType(){    return 'substition';  }
7  function getSort(){    return 150;  }
8  function connectTo($mode) { $this->Lexer->addSpecialPattern('\{\{nssize>[^}]*\}\}',$mode,'plugin_nssize'); }
9
10    // Handling lexer
11    function handle($match, $state, $pos, Doku_Handler $handler){
12        $match = substr($match,8,-2);
13        if($match[0]=='>') $match = substr($match,1);
14        return array($state,$match);
15    }
16
17    function render($mode, Doku_Renderer $renderer, $data) {
18        global $conf;
19        if($mode!='xhtml') return false;
20        $paths = array('datadir'   => 'pages',
21                'olddir'    => 'attic',
22                'mediadir'  => 'media',
23                'mediaolddir' => 'media_attic',
24                'metadir'   => 'meta',
25                'mediametadir' => 'media_meta',
26                'cachedir'  => 'cache',
27                'indexdir'  => 'index',
28                'lockdir'   => 'locks',
29                'tmpdir'    => 'tmp');
30        list($state, $match) = $data;
31        $renderer->table_open(2);
32        $this->_nssize_header($renderer,'Namespace','Size');
33        $total = 0;
34        foreach($paths as $c => $p) {
35            if($conf['display_'.$c]===0) continue;
36            $path = empty($conf[$c]) ? $conf['savedir'].'/'.$p.'/'.$match : $conf[$c].'/'.$match;
37            $conf[$c] = init_path($path);
38            $bytes = $this->_du($conf[$c]);
39            $nssize = $this->_formatSize($bytes);
40            $alert = ($bytes>$this->getConf('alert_size'));
41            $name = $this->getConf('show_abs_path')===1?$path:$p.'/'.$match;
42            $this->_row($renderer,$name,$nssize,$alert);
43            $total = $total+$bytes;
44        }
45        if($this->getConf('display_sum')){
46            $this->_nssize_header($renderer,'Sum',$this->_formatSize($total));
47        }
48        $renderer->table_close();
49        return true;
50    }
51
52  /**
53   * calculate disk usage
54   * Author:  Gregor Mosheh
55   * Website: http://php.net/manual/en/ref.filesystem.php
56   * @param string $location
57   */
58  function _du($location) {
59       if (!$location or !is_dir($location)) {
60          return 0;
61       }
62       $total = 0;
63       $all = opendir($location);
64       while ($file = readdir($all)) {
65          if (is_dir($location.'/'.$file) and $file <> ".." and $file <> ".") {
66             $total += $this->_du($location.'/'.$file);
67             unset($file);
68          }
69          elseif (!is_dir($location.'/'.$file)) {
70             $stats = stat($location.'/'.$file);
71             $total += $stats['size'];
72             unset($file);
73          }
74       }
75       closedir($all);
76       unset($all);
77       return $total;
78  }
79
80/**
81 * format unit of size
82 * Author:   Darian Brown
83 * Date:     2011-06-06
84 * Website:  http://www.darian-brown.com/get-and-display-hard-disk-space-usage-using-php/
85 * @param int $bytes
86 */
87    function _formatSize( $bytes ){
88        $types = array( 'B', 'KB', 'MB', 'GB', 'TB' );
89        for( $i = 0; $bytes >= 1024 && $i < ( count( $types ) -1 ); $bytes /= 1024, $i++ );
90        return( round( $bytes, 2 ) . " " . $types[$i] );
91    }
92
93/**
94 * Render table row for a template
95 * @param Doku_Renderer $renderer
96 * @param string $head
97 * @param string $cell
98 */
99    function _row(&$renderer,$head,$cell,$strong){
100        if(empty($cell))return;
101        $renderer->tablerow_open();
102        $renderer->tablecell_open();
103        $renderer->doc.=$head;
104        $renderer->tablecell_close();
105        $renderer->tablecell_open();
106        if($strong) $renderer->strong_open();
107        $renderer->doc.=$cell;
108        if($strong) $renderer->strong_close();
109        $renderer->tablecell_close();
110        $renderer->tablerow_close();
111    }
112/**
113 * table name row for a template
114 * @param Doku_Renderer $renderer
115 * @param string $cid
116 * @param string $iupac
117 * @param string $title
118 */
119    function _nssize_header(&$renderer,$name,$size){
120        $renderer->tablerow_open();
121        $renderer->tableheader_open();
122        $renderer->doc.=$name;
123        $renderer->tableheader_close();
124        $renderer->tableheader_open();
125        $renderer->doc.=$size;
126        $renderer->tableheader_close();
127        $renderer->tablerow_close();
128    }
129}
130?>