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