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