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