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