xref: /dokuwiki/lib/plugins/popularity/admin.php (revision 7ae265d3e681ebbb637211616aac15b18b8689a1)
1<?php
2/**
3 * Popularity Feedback Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11/**
12 * All DokuWiki plugins to extend the admin function
13 * need to inherit from this class
14 */
15class admin_plugin_popularity extends DokuWiki_Admin_Plugin {
16    var $version = '2010-09-17';
17
18
19    /**
20     * return some info
21     */
22    function getInfo(){
23        return array(
24            'author' => 'Andreas Gohr',
25            'email'  => 'andi@splitbrain.org',
26            'date'   => $this->version,
27            'name'   => 'Popularity Feedback Plugin',
28            'desc'   => 'Send anonymous data about your wiki to the developers.',
29            'url'    => 'http://www.dokuwiki.org/plugin:popularity',
30        );
31    }
32
33    /**
34     * return prompt for admin menu
35     */
36    function getMenuText($language) {
37        return $this->getLang('name');
38    }
39
40    /**
41     * return sort order for position in admin menu
42     */
43    function getMenuSort() {
44        return 2000;
45    }
46
47    /**
48     * Accessible for managers
49     */
50    function forAdminOnly() {
51        return false;
52    }
53
54
55    /**
56     * handle user request
57     */
58    function handle() {
59    }
60
61    /**
62     * Output HTML form
63     */
64    function html() {
65        echo $this->locale_xhtml('intro');
66
67        flush();
68        $data = $this->_gather();
69        echo '<form method="post" action="http://update.dokuwiki.org/popularity.php" accept-charset="utf-8">';
70        echo '<fieldset style="width: 60%;">';
71        echo '<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">';
72        foreach($data as $key => $val){
73            if(is_array($val)) foreach($val as $v){
74                echo hsc($key)."\t".hsc($v)."\n";
75            }else{
76                echo hsc($key)."\t".hsc($val)."\n";
77            }
78        }
79        echo '</textarea><br />';
80        echo '<input type="submit" class="button" value="'.$this->getLang('submit').'"/>';
81        echo '</fieldset>';
82        echo '</form>';
83
84//        dbg($data);
85    }
86
87
88    /**
89     * Gather all information
90     */
91    function _gather(){
92        global $conf;
93        global $auth;
94        $data = array();
95        $phptime = ini_get('max_execution_time');
96        @set_time_limit(0);
97
98        // version
99        $data['anon_id'] = md5(auth_cookiesalt());
100        $data['version'] = getVersion();
101        $data['popversion'] = $this->version;
102        $data['language'] = $conf['lang'];
103        $data['now']      = time();
104
105        // some config values
106        $data['conf_useacl']   = $conf['useacl'];
107        $data['conf_authtype'] = $conf['authtype'];
108        $data['conf_template'] = $conf['template'];
109
110        // number and size of pages
111        $list = array();
112        search($list,$conf['datadir'],array($this,'_search_count'),'','');
113        $data['page_count']    = $list['file_count'];
114        $data['page_size']     = $list['file_size'];
115        $data['page_biggest']  = $list['file_max'];
116        $data['page_smallest'] = $list['file_min'];
117        $data['page_nscount']  = $list['dir_count'];
118        $data['page_nsnest']   = $list['dir_nest'];
119        if($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count'];
120        $data['page_oldest']   = $list['file_oldest'];
121        unset($list);
122
123        // number and size of media
124        $list = array();
125        search($list,$conf['mediadir'],array($this,'_search_count'),array('all'=>true));
126        $data['media_count']    = $list['file_count'];
127        $data['media_size']     = $list['file_size'];
128        $data['media_biggest']  = $list['file_max'];
129        $data['media_smallest'] = $list['file_min'];
130        $data['media_nscount']  = $list['dir_count'];
131        $data['media_nsnest']   = $list['dir_nest'];
132        if($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count'];
133        unset($list);
134
135        // number and size of cache
136        $list = array();
137        search($list,$conf['cachedir'],array($this,'_search_count'),array('all'=>true));
138        $data['cache_count']    = $list['file_count'];
139        $data['cache_size']     = $list['file_size'];
140        $data['cache_biggest']  = $list['file_max'];
141        $data['cache_smallest'] = $list['file_min'];
142        if($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count'];
143        unset($list);
144
145        // number and size of index
146        $list = array();
147        search($list,$conf['indexdir'],array($this,'_search_count'),array('all'=>true));
148        $data['index_count']    = $list['file_count'];
149        $data['index_size']     = $list['file_size'];
150        $data['index_biggest']  = $list['file_max'];
151        $data['index_smallest'] = $list['file_min'];
152        if($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count'];
153        unset($list);
154
155        // number and size of meta
156        $list = array();
157        search($list,$conf['metadir'],array($this,'_search_count'),array('all'=>true));
158        $data['meta_count']    = $list['file_count'];
159        $data['meta_size']     = $list['file_size'];
160        $data['meta_biggest']  = $list['file_max'];
161        $data['meta_smallest'] = $list['file_min'];
162        if($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count'];
163        unset($list);
164
165        // number and size of attic
166        $list = array();
167        search($list,$conf['olddir'],array($this,'_search_count'),array('all'=>true));
168        $data['attic_count']    = $list['file_count'];
169        $data['attic_size']     = $list['file_size'];
170        $data['attic_biggest']  = $list['file_max'];
171        $data['attic_smallest'] = $list['file_min'];
172        if($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count'];
173        $data['attic_oldest']   = $list['file_oldest'];
174        unset($list);
175
176        // user count
177        if($auth && $auth->canDo('getUserCount')){
178            $data['user_count'] = $auth->getUserCount();
179        }
180
181        // calculate edits per day
182        $list = @file($conf['metadir'].'/_dokuwiki.changes');
183        $count = count($list);
184        if($count > 2){
185            $first = (int) substr(array_shift($list),0,10);
186            $last  = (int) substr(array_pop($list),0,10);
187            $dur = ($last - $first)/(60*60*24); // number of days in the changelog
188            $data['edits_per_day'] = $count/$dur;
189        }
190        unset($list);
191
192        // plugins
193        $data['plugin'] = plugin_list();
194
195        // pcre info
196        if(defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION;
197        $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit');
198        $data['pcre_recursion'] = ini_get('pcre.recursion_limit');
199
200        // php info
201        $data['os'] = PHP_OS;
202        $data['webserver'] = $_SERVER['SERVER_SOFTWARE'];
203        $data['php_version'] = phpversion();
204        $data['php_sapi'] = php_sapi_name();
205        $data['php_memory'] = $this->_to_byte(ini_get('memory_limit'));
206        $data['php_exectime'] = $phptime;
207        $data['php_extension'] = get_loaded_extensions();
208
209        return $data;
210    }
211
212
213    function _search_count(&$data,$base,$file,$type,$lvl,$opts){
214        // traverse
215        if($type == 'd'){
216            if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
217            $data['dir_count']++;
218            return true;
219        }
220
221        //only search txt files if 'all' option not set
222        if($opts['all'] || substr($file,-4) == '.txt'){
223            $size = filesize($base.'/'.$file);
224            $date = filemtime($base.'/'.$file);
225            $data['file_count']++;
226            $data['file_size'] += $size;
227            if(!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size;
228            if($data['file_max'] < $size) $data['file_max'] = $size;
229            if(!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date;
230        }
231
232        return false;
233    }
234
235    /**
236     * Convert php.ini shorthands to byte
237     *
238     * @author <gilthans dot NO dot SPAM at gmail dot com>
239     * @link   http://de3.php.net/manual/en/ini.core.php#79564
240     */
241    function _to_byte($v){
242        $l = substr($v, -1);
243        $ret = substr($v, 0, -1);
244        switch(strtoupper($l)){
245            case 'P':
246                $ret *= 1024;
247            case 'T':
248                $ret *= 1024;
249            case 'G':
250                $ret *= 1024;
251            case 'M':
252                $ret *= 1024;
253            case 'K':
254                $ret *= 1024;
255            break;
256        }
257        return $ret;
258    }
259}
260