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