xref: /dokuwiki/lib/plugins/popularity/helper.php (revision 23735ba4c5021df9ab8c1b4a0e8322cebdaf5931)
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        $data['popauto']  = (int) $this->isAutoSubmitEnabled();
138
139        // some config values
140        $data['conf_useacl']   = $conf['useacl'];
141        $data['conf_authtype'] = $conf['authtype'];
142        $data['conf_template'] = $conf['template'];
143
144        // number and size of pages
145        $list = array();
146        search($list,$conf['datadir'],array($this,'_search_count'),'','');
147        $data['page_count']    = $list['file_count'];
148        $data['page_size']     = $list['file_size'];
149        $data['page_biggest']  = $list['file_max'];
150        $data['page_smallest'] = $list['file_min'];
151        $data['page_nscount']  = $list['dir_count'];
152        $data['page_nsnest']   = $list['dir_nest'];
153        if($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count'];
154        $data['page_oldest']   = $list['file_oldest'];
155        unset($list);
156
157        // number and size of media
158        $list = array();
159        search($list,$conf['mediadir'],array($this,'_search_count'),array('all'=>true));
160        $data['media_count']    = $list['file_count'];
161        $data['media_size']     = $list['file_size'];
162        $data['media_biggest']  = $list['file_max'];
163        $data['media_smallest'] = $list['file_min'];
164        $data['media_nscount']  = $list['dir_count'];
165        $data['media_nsnest']   = $list['dir_nest'];
166        if($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count'];
167        unset($list);
168
169        // number and size of cache
170        $list = array();
171        search($list,$conf['cachedir'],array($this,'_search_count'),array('all'=>true));
172        $data['cache_count']    = $list['file_count'];
173        $data['cache_size']     = $list['file_size'];
174        $data['cache_biggest']  = $list['file_max'];
175        $data['cache_smallest'] = $list['file_min'];
176        if($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count'];
177        unset($list);
178
179        // number and size of index
180        $list = array();
181        search($list,$conf['indexdir'],array($this,'_search_count'),array('all'=>true));
182        $data['index_count']    = $list['file_count'];
183        $data['index_size']     = $list['file_size'];
184        $data['index_biggest']  = $list['file_max'];
185        $data['index_smallest'] = $list['file_min'];
186        if($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count'];
187        unset($list);
188
189        // number and size of meta
190        $list = array();
191        search($list,$conf['metadir'],array($this,'_search_count'),array('all'=>true));
192        $data['meta_count']    = $list['file_count'];
193        $data['meta_size']     = $list['file_size'];
194        $data['meta_biggest']  = $list['file_max'];
195        $data['meta_smallest'] = $list['file_min'];
196        if($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count'];
197        unset($list);
198
199        // number and size of attic
200        $list = array();
201        search($list,$conf['olddir'],array($this,'_search_count'),array('all'=>true));
202        $data['attic_count']    = $list['file_count'];
203        $data['attic_size']     = $list['file_size'];
204        $data['attic_biggest']  = $list['file_max'];
205        $data['attic_smallest'] = $list['file_min'];
206        if($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count'];
207        $data['attic_oldest']   = $list['file_oldest'];
208        unset($list);
209
210        // user count
211        if($auth && $auth->canDo('getUserCount')){
212            $data['user_count'] = $auth->getUserCount();
213        }
214
215        // calculate edits per day
216        $list = @file($conf['metadir'].'/_dokuwiki.changes');
217        $count = count($list);
218        if($count > 2){
219            $first = (int) substr(array_shift($list),0,10);
220            $last  = (int) substr(array_pop($list),0,10);
221            $dur = ($last - $first)/(60*60*24); // number of days in the changelog
222            $data['edits_per_day'] = $count/$dur;
223        }
224        unset($list);
225
226        // plugins
227        $data['plugin'] = plugin_list();
228
229        // pcre info
230        if(defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION;
231        $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit');
232        $data['pcre_recursion'] = ini_get('pcre.recursion_limit');
233
234        // php info
235        $data['os'] = PHP_OS;
236        $data['webserver'] = $_SERVER['SERVER_SOFTWARE'];
237        $data['php_version'] = phpversion();
238        $data['php_sapi'] = php_sapi_name();
239        $data['php_memory'] = $this->_to_byte(ini_get('memory_limit'));
240        $data['php_exectime'] = $phptime;
241        $data['php_extension'] = get_loaded_extensions();
242
243        return $data;
244    }
245
246    function _search_count(&$data,$base,$file,$type,$lvl,$opts){
247        // traverse
248        if($type == 'd'){
249            if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
250            $data['dir_count']++;
251            return true;
252        }
253
254        //only search txt files if 'all' option not set
255        if($opts['all'] || substr($file,-4) == '.txt'){
256            $size = filesize($base.'/'.$file);
257            $date = filemtime($base.'/'.$file);
258            $data['file_count']++;
259            $data['file_size'] += $size;
260            if(!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size;
261            if($data['file_max'] < $size) $data['file_max'] = $size;
262            if(!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date;
263        }
264
265        return false;
266    }
267
268    /**
269     * Convert php.ini shorthands to byte
270     *
271     * @author <gilthans dot NO dot SPAM at gmail dot com>
272     * @link   http://de3.php.net/manual/en/ini.core.php#79564
273     */
274    function _to_byte($v){
275        $l = substr($v, -1);
276        $ret = substr($v, 0, -1);
277        switch(strtoupper($l)){
278            case 'P':
279                $ret *= 1024;
280            case 'T':
281                $ret *= 1024;
282            case 'G':
283                $ret *= 1024;
284            case 'M':
285                $ret *= 1024;
286            case 'K':
287                $ret *= 1024;
288            break;
289        }
290        return $ret;
291    }
292}
293