xref: /dokuwiki/lib/plugins/popularity/helper.php (revision 04556a0aabe1497bd711ca45405b8cfa2888d427)
1<?php
2/**
3 * Popularity Feedback Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 */
7
8class helper_plugin_popularity extends Dokuwiki_Plugin {
9    /**
10     * The url where the data should be sent
11     */
12    var $submitUrl = 'http://update.dokuwiki.org/popularity.php';
13
14    /**
15     * Name of the file which determine if the the autosubmit is enabled,
16     * and when it was submited for the last time
17     */
18    var $autosubmitFile;
19
20    /**
21     * File where the last error which happened when we tried to autosubmit, will be log
22     */
23    var $autosubmitErrorFile;
24
25    /**
26     * Name of the file which determine when the popularity data was manually
27     * submitted for the last time
28     * (If this file doesn't exist, the data has never been sent)
29     */
30    var $popularityLastSubmitFile;
31
32
33    function helper_plugin_popularity(){
34        global $conf;
35        $this->autosubmitFile = $conf['cachedir'].'/autosubmit.txt';
36        $this->autosubmitErrorFile = $conf['cachedir'].'/autosubmitError.txt';
37        $this->popularityLastSubmitFile = $conf['cachedir'].'/lastSubmitTime.txt';
38    }
39
40    function getMethods(){
41        $result = array();
42        $result[] = array(
43                'name'   => 'isAutoSubmitEnabled',
44                'desc'   => 'Check if autosubmit is enabled',
45                'params' => array(),
46                'return' => array('result' => 'bool')
47                );
48        $result[] = array(
49                'name'   => 'sendData',
50                'desc'   => 'Send the popularity data',
51                'params' => array('data' => 'string'),
52                'return' => array()
53                );
54        $result[] = array(
55                'name' => 'gatherAsString',
56                'desc' => 'Gather the popularity data',
57                'params' => array(),
58                'return' => array('data' => 'string')
59                );
60        $result[] = array(
61                'name'   => 'lastSentTime',
62                'desc'   => 'Compute the last time popularity data was sent',
63                'params' => array(),
64                'return' => array('data' => 'int')
65                );
66        return $result;
67
68    }
69
70    /**
71     * Check if autosubmit is enabled
72     * @return TRUE if we should send data once a month, FALSE otherwise
73     */
74    function isAutoSubmitEnabled(){
75        return @file_exists($this->autosubmitFile);
76    }
77
78    /**
79     * Send the data, to the submit url
80     * @param string $data The popularity data
81     * @return An empty string if everything worked fine, a string describing the error otherwise
82     */
83    function sendData($data){
84        $error = '';
85        $httpClient = new DokuHTTPClient();
86        $status = $httpClient->sendRequest($this->submitUrl, $data, 'POST');
87        if ( ! $status ){
88            $error = $httpClient->error;
89        }
90        return $error;
91    }
92
93    /**
94     * Compute the last time the data was sent. If it has never been sent, we return 0.
95     */
96    function lastSentTime(){
97        $manualSubmission = @filemtime($this->popularityLastSubmitFile);
98        $autoSubmission   = @filemtime($this->autosubmitFile);
99
100        return max((int) $manualSubmission, (int) $autoSubmission);
101    }
102
103    /**
104     * Gather all information
105     * @return The popularity data as a string
106     */
107    function gatherAsString(){
108        $data = $this->_gather();
109        $string = '';
110        foreach($data as $key => $val){
111            if(is_array($val)) foreach($val as $v){
112                $string .=  hsc($key)."\t".hsc($v)."\n";
113            }else{
114                $string .= hsc($key)."\t".hsc($val)."\n";
115            }
116        }
117        return $string;
118    }
119
120    /**
121     * Gather all information
122     * @return The popularity data as an array
123     */
124    function _gather(){
125        global $conf;
126        global $auth;
127        $data = array();
128        $phptime = ini_get('max_execution_time');
129        @set_time_limit(0);
130
131        // version
132        $data['anon_id'] = md5(auth_cookiesalt());
133        $data['version'] = getVersion();
134        $data['popversion'] = $this->version;
135        $data['language'] = $conf['lang'];
136        $data['now']      = time();
137
138        // some config values
139        $data['conf_useacl']   = $conf['useacl'];
140        $data['conf_authtype'] = $conf['authtype'];
141        $data['conf_template'] = $conf['template'];
142
143        // number and size of pages
144        $list = array();
145        search($list,$conf['datadir'],array($this,'_search_count'),'','');
146        $data['page_count']    = $list['file_count'];
147        $data['page_size']     = $list['file_size'];
148        $data['page_biggest']  = $list['file_max'];
149        $data['page_smallest'] = $list['file_min'];
150        $data['page_nscount']  = $list['dir_count'];
151        $data['page_nsnest']   = $list['dir_nest'];
152        if($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count'];
153        $data['page_oldest']   = $list['file_oldest'];
154        unset($list);
155
156        // number and size of media
157        $list = array();
158        search($list,$conf['mediadir'],array($this,'_search_count'),array('all'=>true));
159        $data['media_count']    = $list['file_count'];
160        $data['media_size']     = $list['file_size'];
161        $data['media_biggest']  = $list['file_max'];
162        $data['media_smallest'] = $list['file_min'];
163        $data['media_nscount']  = $list['dir_count'];
164        $data['media_nsnest']   = $list['dir_nest'];
165        if($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count'];
166        unset($list);
167
168        // number and size of cache
169        $list = array();
170        search($list,$conf['cachedir'],array($this,'_search_count'),array('all'=>true));
171        $data['cache_count']    = $list['file_count'];
172        $data['cache_size']     = $list['file_size'];
173        $data['cache_biggest']  = $list['file_max'];
174        $data['cache_smallest'] = $list['file_min'];
175        if($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count'];
176        unset($list);
177
178        // number and size of index
179        $list = array();
180        search($list,$conf['indexdir'],array($this,'_search_count'),array('all'=>true));
181        $data['index_count']    = $list['file_count'];
182        $data['index_size']     = $list['file_size'];
183        $data['index_biggest']  = $list['file_max'];
184        $data['index_smallest'] = $list['file_min'];
185        if($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count'];
186        unset($list);
187
188        // number and size of meta
189        $list = array();
190        search($list,$conf['metadir'],array($this,'_search_count'),array('all'=>true));
191        $data['meta_count']    = $list['file_count'];
192        $data['meta_size']     = $list['file_size'];
193        $data['meta_biggest']  = $list['file_max'];
194        $data['meta_smallest'] = $list['file_min'];
195        if($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count'];
196        unset($list);
197
198        // number and size of attic
199        $list = array();
200        search($list,$conf['olddir'],array($this,'_search_count'),array('all'=>true));
201        $data['attic_count']    = $list['file_count'];
202        $data['attic_size']     = $list['file_size'];
203        $data['attic_biggest']  = $list['file_max'];
204        $data['attic_smallest'] = $list['file_min'];
205        if($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count'];
206        $data['attic_oldest']   = $list['file_oldest'];
207        unset($list);
208
209        // user count
210        if($auth && $auth->canDo('getUserCount')){
211            $data['user_count'] = $auth->getUserCount();
212        }
213
214        // calculate edits per day
215        $list = @file($conf['metadir'].'/_dokuwiki.changes');
216        $count = count($list);
217        if($count > 2){
218            $first = (int) substr(array_shift($list),0,10);
219            $last  = (int) substr(array_pop($list),0,10);
220            $dur = ($last - $first)/(60*60*24); // number of days in the changelog
221            $data['edits_per_day'] = $count/$dur;
222        }
223        unset($list);
224
225        // plugins
226        $data['plugin'] = plugin_list();
227
228        // pcre info
229        if(defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION;
230        $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit');
231        $data['pcre_recursion'] = ini_get('pcre.recursion_limit');
232
233        // php info
234        $data['os'] = PHP_OS;
235        $data['webserver'] = $_SERVER['SERVER_SOFTWARE'];
236        $data['php_version'] = phpversion();
237        $data['php_sapi'] = php_sapi_name();
238        $data['php_memory'] = $this->_to_byte(ini_get('memory_limit'));
239        $data['php_exectime'] = $phptime;
240        $data['php_extension'] = get_loaded_extensions();
241
242        return $data;
243    }
244
245    function _search_count(&$data,$base,$file,$type,$lvl,$opts){
246        // traverse
247        if($type == 'd'){
248            if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
249            $data['dir_count']++;
250            return true;
251        }
252
253        //only search txt files if 'all' option not set
254        if($opts['all'] || substr($file,-4) == '.txt'){
255            $size = filesize($base.'/'.$file);
256            $date = filemtime($base.'/'.$file);
257            $data['file_count']++;
258            $data['file_size'] += $size;
259            if(!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size;
260            if($data['file_max'] < $size) $data['file_max'] = $size;
261            if(!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date;
262        }
263
264        return false;
265    }
266
267    /**
268     * Convert php.ini shorthands to byte
269     *
270     * @author <gilthans dot NO dot SPAM at gmail dot com>
271     * @link   http://de3.php.net/manual/en/ini.core.php#79564
272     */
273    function _to_byte($v){
274        $l = substr($v, -1);
275        $ret = substr($v, 0, -1);
276        switch(strtoupper($l)){
277            case 'P':
278                $ret *= 1024;
279            case 'T':
280                $ret *= 1024;
281            case 'G':
282                $ret *= 1024;
283            case 'M':
284                $ret *= 1024;
285            case 'K':
286                $ret *= 1024;
287            break;
288        }
289        return $ret;
290    }
291}
292