xref: /dokuwiki/lib/plugins/popularity/helper.php (revision 506c1de7b3fee44073b5ec5d944de4cbb03960a5)
18596046dSAndreas Gohr<?php
28596046dSAndreas Gohr/**
38596046dSAndreas Gohr * Popularity Feedback Plugin
48596046dSAndreas Gohr *
58596046dSAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
68596046dSAndreas Gohr */
78596046dSAndreas Gohr
88596046dSAndreas Gohrclass helper_plugin_popularity extends Dokuwiki_Plugin {
98596046dSAndreas Gohr    /**
108596046dSAndreas Gohr     * The url where the data should be sent
118596046dSAndreas Gohr     */
128596046dSAndreas Gohr    var $submitUrl = 'http://update.dokuwiki.org/popularity.php';
138596046dSAndreas Gohr
148596046dSAndreas Gohr    /**
158596046dSAndreas Gohr     * Name of the file which determine if the the autosubmit is enabled,
165827ba0bSGuillaume Turri     * and when it was submited for the last time
178596046dSAndreas Gohr     */
188596046dSAndreas Gohr    var $autosubmitFile;
198596046dSAndreas Gohr
208596046dSAndreas Gohr    /**
218596046dSAndreas Gohr     * File where the last error which happened when we tried to autosubmit, will be log
228596046dSAndreas Gohr     */
238596046dSAndreas Gohr    var $autosubmitErrorFile;
248596046dSAndreas Gohr
255827ba0bSGuillaume Turri    /**
265827ba0bSGuillaume Turri     * Name of the file which determine when the popularity data was manually
275827ba0bSGuillaume Turri     * submitted for the last time
285827ba0bSGuillaume Turri     * (If this file doesn't exist, the data has never been sent)
295827ba0bSGuillaume Turri     */
305827ba0bSGuillaume Turri    var $popularityLastSubmitFile;
315827ba0bSGuillaume Turri
325827ba0bSGuillaume Turri
338596046dSAndreas Gohr    function helper_plugin_popularity(){
348596046dSAndreas Gohr        global $conf;
358596046dSAndreas Gohr        $this->autosubmitFile = $conf['cachedir'].'/autosubmit.txt';
368596046dSAndreas Gohr        $this->autosubmitErrorFile = $conf['cachedir'].'/autosubmitError.txt';
375827ba0bSGuillaume Turri        $this->popularityLastSubmitFile = $conf['cachedir'].'/lastSubmitTime.txt';
388596046dSAndreas Gohr    }
398596046dSAndreas Gohr
408596046dSAndreas Gohr    function getMethods(){
418596046dSAndreas Gohr        $result = array();
428596046dSAndreas Gohr        $result[] = array(
438596046dSAndreas Gohr                'name'   => 'isAutoSubmitEnabled',
448596046dSAndreas Gohr                'desc'   => 'Check if autosubmit is enabled',
458596046dSAndreas Gohr                'params' => array(),
468596046dSAndreas Gohr                'return' => array('result' => 'bool')
478596046dSAndreas Gohr                );
488596046dSAndreas Gohr        $result[] = array(
498596046dSAndreas Gohr                'name'   => 'sendData',
508596046dSAndreas Gohr                'desc'   => 'Send the popularity data',
518596046dSAndreas Gohr                'params' => array('data' => 'string'),
528596046dSAndreas Gohr                'return' => array()
538596046dSAndreas Gohr                );
548596046dSAndreas Gohr        $result[] = array(
558596046dSAndreas Gohr                'name' => 'gatherAsString',
568596046dSAndreas Gohr                'desc' => 'Gather the popularity data',
578596046dSAndreas Gohr                'params' => array(),
588596046dSAndreas Gohr                'return' => array('data' => 'string')
598596046dSAndreas Gohr                );
605827ba0bSGuillaume Turri        $result[] = array(
615827ba0bSGuillaume Turri                'name'   => 'lastSentTime',
625827ba0bSGuillaume Turri                'desc'   => 'Compute the last time popularity data was sent',
63f6896f7bSPetsagourakis George                'params' => array(),
645827ba0bSGuillaume Turri                'return' => array('data' => 'int')
655827ba0bSGuillaume Turri                );
668596046dSAndreas Gohr        return $result;
678596046dSAndreas Gohr
688596046dSAndreas Gohr    }
698596046dSAndreas Gohr
708596046dSAndreas Gohr    /**
718596046dSAndreas Gohr     * Check if autosubmit is enabled
728596046dSAndreas Gohr     * @return TRUE if we should send data once a month, FALSE otherwise
738596046dSAndreas Gohr     */
748596046dSAndreas Gohr    function isAutoSubmitEnabled(){
758596046dSAndreas Gohr        return @file_exists($this->autosubmitFile);
768596046dSAndreas Gohr    }
778596046dSAndreas Gohr
788596046dSAndreas Gohr    /**
798596046dSAndreas Gohr     * Send the data, to the submit url
808596046dSAndreas Gohr     * @param string $data The popularity data
818596046dSAndreas Gohr     * @return An empty string if everything worked fine, a string describing the error otherwise
828596046dSAndreas Gohr     */
838596046dSAndreas Gohr    function sendData($data){
848596046dSAndreas Gohr        $error = '';
858596046dSAndreas Gohr        $httpClient = new DokuHTTPClient();
868596046dSAndreas Gohr        $status = $httpClient->sendRequest($this->submitUrl, $data, 'POST');
878596046dSAndreas Gohr        if ( ! $status ){
888596046dSAndreas Gohr            $error = $httpClient->error;
898596046dSAndreas Gohr        }
908596046dSAndreas Gohr        return $error;
918596046dSAndreas Gohr    }
928596046dSAndreas Gohr
938596046dSAndreas Gohr    /**
945827ba0bSGuillaume Turri     * Compute the last time the data was sent. If it has never been sent, we return 0.
955827ba0bSGuillaume Turri     */
965827ba0bSGuillaume Turri    function lastSentTime(){
975827ba0bSGuillaume Turri        $manualSubmission = @filemtime($this->popularityLastSubmitFile);
985827ba0bSGuillaume Turri        $autoSubmission   = @filemtime($this->autosubmitFile);
995827ba0bSGuillaume Turri
1005827ba0bSGuillaume Turri        return max((int) $manualSubmission, (int) $autoSubmission);
1015827ba0bSGuillaume Turri    }
1025827ba0bSGuillaume Turri
1035827ba0bSGuillaume Turri    /**
1048596046dSAndreas Gohr     * Gather all information
1058596046dSAndreas Gohr     * @return The popularity data as a string
1068596046dSAndreas Gohr     */
1078596046dSAndreas Gohr    function gatherAsString(){
1088596046dSAndreas Gohr        $data = $this->_gather();
1098596046dSAndreas Gohr        $string = '';
1108596046dSAndreas Gohr        foreach($data as $key => $val){
1118596046dSAndreas Gohr            if(is_array($val)) foreach($val as $v){
1128596046dSAndreas Gohr                $string .=  hsc($key)."\t".hsc($v)."\n";
1138596046dSAndreas Gohr            }else{
1148596046dSAndreas Gohr                $string .= hsc($key)."\t".hsc($val)."\n";
1158596046dSAndreas Gohr            }
1168596046dSAndreas Gohr        }
1178596046dSAndreas Gohr        return $string;
1188596046dSAndreas Gohr    }
1198596046dSAndreas Gohr
1208596046dSAndreas Gohr    /**
1218596046dSAndreas Gohr     * Gather all information
1228596046dSAndreas Gohr     * @return The popularity data as an array
1238596046dSAndreas Gohr     */
1248596046dSAndreas Gohr    function _gather(){
1258596046dSAndreas Gohr        global $conf;
1268596046dSAndreas Gohr        global $auth;
1278596046dSAndreas Gohr        $data = array();
1288596046dSAndreas Gohr        $phptime = ini_get('max_execution_time');
1298596046dSAndreas Gohr        @set_time_limit(0);
1308596046dSAndreas Gohr
1318596046dSAndreas Gohr        // version
1328596046dSAndreas Gohr        $data['anon_id'] = md5(auth_cookiesalt());
1338596046dSAndreas Gohr        $data['version'] = getVersion();
1348596046dSAndreas Gohr        $data['popversion'] = $this->version;
1358596046dSAndreas Gohr        $data['language'] = $conf['lang'];
1368596046dSAndreas Gohr        $data['now']      = time();
137d4228d2dSAndreas Gohr        $data['popauto']  = (int) $this->isAutoSubmitEnabled();
1388596046dSAndreas Gohr
1398596046dSAndreas Gohr        // some config values
1408596046dSAndreas Gohr        $data['conf_useacl']   = $conf['useacl'];
1418596046dSAndreas Gohr        $data['conf_authtype'] = $conf['authtype'];
1428596046dSAndreas Gohr        $data['conf_template'] = $conf['template'];
1438596046dSAndreas Gohr
1448596046dSAndreas Gohr        // number and size of pages
1458596046dSAndreas Gohr        $list = array();
146*506c1de7SAndreas Gohr        search($list,$conf['datadir'],array($this,'_search_count'),array('all'=>false),'');
1478596046dSAndreas Gohr        $data['page_count']    = $list['file_count'];
1488596046dSAndreas Gohr        $data['page_size']     = $list['file_size'];
1498596046dSAndreas Gohr        $data['page_biggest']  = $list['file_max'];
1508596046dSAndreas Gohr        $data['page_smallest'] = $list['file_min'];
1518596046dSAndreas Gohr        $data['page_nscount']  = $list['dir_count'];
1528596046dSAndreas Gohr        $data['page_nsnest']   = $list['dir_nest'];
1538596046dSAndreas Gohr        if($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count'];
1548596046dSAndreas Gohr        $data['page_oldest']   = $list['file_oldest'];
1558596046dSAndreas Gohr        unset($list);
1568596046dSAndreas Gohr
1578596046dSAndreas Gohr        // number and size of media
1588596046dSAndreas Gohr        $list = array();
1598596046dSAndreas Gohr        search($list,$conf['mediadir'],array($this,'_search_count'),array('all'=>true));
1608596046dSAndreas Gohr        $data['media_count']    = $list['file_count'];
1618596046dSAndreas Gohr        $data['media_size']     = $list['file_size'];
1628596046dSAndreas Gohr        $data['media_biggest']  = $list['file_max'];
1638596046dSAndreas Gohr        $data['media_smallest'] = $list['file_min'];
1648596046dSAndreas Gohr        $data['media_nscount']  = $list['dir_count'];
1658596046dSAndreas Gohr        $data['media_nsnest']   = $list['dir_nest'];
1668596046dSAndreas Gohr        if($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count'];
1678596046dSAndreas Gohr        unset($list);
1688596046dSAndreas Gohr
1698596046dSAndreas Gohr        // number and size of cache
1708596046dSAndreas Gohr        $list = array();
1718596046dSAndreas Gohr        search($list,$conf['cachedir'],array($this,'_search_count'),array('all'=>true));
1728596046dSAndreas Gohr        $data['cache_count']    = $list['file_count'];
1738596046dSAndreas Gohr        $data['cache_size']     = $list['file_size'];
1748596046dSAndreas Gohr        $data['cache_biggest']  = $list['file_max'];
1758596046dSAndreas Gohr        $data['cache_smallest'] = $list['file_min'];
1768596046dSAndreas Gohr        if($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count'];
1778596046dSAndreas Gohr        unset($list);
1788596046dSAndreas Gohr
1798596046dSAndreas Gohr        // number and size of index
1808596046dSAndreas Gohr        $list = array();
1818596046dSAndreas Gohr        search($list,$conf['indexdir'],array($this,'_search_count'),array('all'=>true));
1828596046dSAndreas Gohr        $data['index_count']    = $list['file_count'];
1838596046dSAndreas Gohr        $data['index_size']     = $list['file_size'];
1848596046dSAndreas Gohr        $data['index_biggest']  = $list['file_max'];
1858596046dSAndreas Gohr        $data['index_smallest'] = $list['file_min'];
1868596046dSAndreas Gohr        if($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count'];
1878596046dSAndreas Gohr        unset($list);
1888596046dSAndreas Gohr
1898596046dSAndreas Gohr        // number and size of meta
1908596046dSAndreas Gohr        $list = array();
1918596046dSAndreas Gohr        search($list,$conf['metadir'],array($this,'_search_count'),array('all'=>true));
1928596046dSAndreas Gohr        $data['meta_count']    = $list['file_count'];
1938596046dSAndreas Gohr        $data['meta_size']     = $list['file_size'];
1948596046dSAndreas Gohr        $data['meta_biggest']  = $list['file_max'];
1958596046dSAndreas Gohr        $data['meta_smallest'] = $list['file_min'];
1968596046dSAndreas Gohr        if($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count'];
1978596046dSAndreas Gohr        unset($list);
1988596046dSAndreas Gohr
1998596046dSAndreas Gohr        // number and size of attic
2008596046dSAndreas Gohr        $list = array();
2018596046dSAndreas Gohr        search($list,$conf['olddir'],array($this,'_search_count'),array('all'=>true));
2028596046dSAndreas Gohr        $data['attic_count']    = $list['file_count'];
2038596046dSAndreas Gohr        $data['attic_size']     = $list['file_size'];
2048596046dSAndreas Gohr        $data['attic_biggest']  = $list['file_max'];
2058596046dSAndreas Gohr        $data['attic_smallest'] = $list['file_min'];
2068596046dSAndreas Gohr        if($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count'];
2078596046dSAndreas Gohr        $data['attic_oldest']   = $list['file_oldest'];
2088596046dSAndreas Gohr        unset($list);
2098596046dSAndreas Gohr
2108596046dSAndreas Gohr        // user count
2118596046dSAndreas Gohr        if($auth && $auth->canDo('getUserCount')){
2128596046dSAndreas Gohr            $data['user_count'] = $auth->getUserCount();
2138596046dSAndreas Gohr        }
2148596046dSAndreas Gohr
2158596046dSAndreas Gohr        // calculate edits per day
2168596046dSAndreas Gohr        $list = @file($conf['metadir'].'/_dokuwiki.changes');
2178596046dSAndreas Gohr        $count = count($list);
2188596046dSAndreas Gohr        if($count > 2){
2198596046dSAndreas Gohr            $first = (int) substr(array_shift($list),0,10);
2208596046dSAndreas Gohr            $last  = (int) substr(array_pop($list),0,10);
2218596046dSAndreas Gohr            $dur = ($last - $first)/(60*60*24); // number of days in the changelog
2228596046dSAndreas Gohr            $data['edits_per_day'] = $count/$dur;
2238596046dSAndreas Gohr        }
2248596046dSAndreas Gohr        unset($list);
2258596046dSAndreas Gohr
2268596046dSAndreas Gohr        // plugins
2278596046dSAndreas Gohr        $data['plugin'] = plugin_list();
2288596046dSAndreas Gohr
2298596046dSAndreas Gohr        // pcre info
2308596046dSAndreas Gohr        if(defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION;
2318596046dSAndreas Gohr        $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit');
2328596046dSAndreas Gohr        $data['pcre_recursion'] = ini_get('pcre.recursion_limit');
2338596046dSAndreas Gohr
2348596046dSAndreas Gohr        // php info
2358596046dSAndreas Gohr        $data['os'] = PHP_OS;
2368596046dSAndreas Gohr        $data['webserver'] = $_SERVER['SERVER_SOFTWARE'];
2378596046dSAndreas Gohr        $data['php_version'] = phpversion();
2388596046dSAndreas Gohr        $data['php_sapi'] = php_sapi_name();
2398596046dSAndreas Gohr        $data['php_memory'] = $this->_to_byte(ini_get('memory_limit'));
2408596046dSAndreas Gohr        $data['php_exectime'] = $phptime;
2418596046dSAndreas Gohr        $data['php_extension'] = get_loaded_extensions();
2428596046dSAndreas Gohr
2438596046dSAndreas Gohr        return $data;
2448596046dSAndreas Gohr    }
2458596046dSAndreas Gohr
2468596046dSAndreas Gohr    function _search_count(&$data,$base,$file,$type,$lvl,$opts){
2478596046dSAndreas Gohr        // traverse
2488596046dSAndreas Gohr        if($type == 'd'){
2498596046dSAndreas Gohr            if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
2508596046dSAndreas Gohr            $data['dir_count']++;
2518596046dSAndreas Gohr            return true;
2528596046dSAndreas Gohr        }
2538596046dSAndreas Gohr
2548596046dSAndreas Gohr        //only search txt files if 'all' option not set
2558596046dSAndreas Gohr        if($opts['all'] || substr($file,-4) == '.txt'){
2568596046dSAndreas Gohr            $size = filesize($base.'/'.$file);
2578596046dSAndreas Gohr            $date = filemtime($base.'/'.$file);
2588596046dSAndreas Gohr            $data['file_count']++;
2598596046dSAndreas Gohr            $data['file_size'] += $size;
2608596046dSAndreas Gohr            if(!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size;
2618596046dSAndreas Gohr            if($data['file_max'] < $size) $data['file_max'] = $size;
2628596046dSAndreas Gohr            if(!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date;
2638596046dSAndreas Gohr        }
2648596046dSAndreas Gohr
2658596046dSAndreas Gohr        return false;
2668596046dSAndreas Gohr    }
2678596046dSAndreas Gohr
2688596046dSAndreas Gohr    /**
2698596046dSAndreas Gohr     * Convert php.ini shorthands to byte
2708596046dSAndreas Gohr     *
2718596046dSAndreas Gohr     * @author <gilthans dot NO dot SPAM at gmail dot com>
2728596046dSAndreas Gohr     * @link   http://de3.php.net/manual/en/ini.core.php#79564
2738596046dSAndreas Gohr     */
2748596046dSAndreas Gohr    function _to_byte($v){
2758596046dSAndreas Gohr        $l = substr($v, -1);
2768596046dSAndreas Gohr        $ret = substr($v, 0, -1);
2778596046dSAndreas Gohr        switch(strtoupper($l)){
2788596046dSAndreas Gohr            case 'P':
2798596046dSAndreas Gohr                $ret *= 1024;
2808596046dSAndreas Gohr            case 'T':
2818596046dSAndreas Gohr                $ret *= 1024;
2828596046dSAndreas Gohr            case 'G':
2838596046dSAndreas Gohr                $ret *= 1024;
2848596046dSAndreas Gohr            case 'M':
2858596046dSAndreas Gohr                $ret *= 1024;
2868596046dSAndreas Gohr            case 'K':
2878596046dSAndreas Gohr                $ret *= 1024;
2888596046dSAndreas Gohr            break;
2898596046dSAndreas Gohr        }
2908596046dSAndreas Gohr        return $ret;
2918596046dSAndreas Gohr    }
2928596046dSAndreas Gohr}
293