xref: /dokuwiki/lib/plugins/popularity/helper.php (revision 3dc2d50c5fda9c4bf708ff4c26e266ba239af62c)
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     */
12*3dc2d50cSAndreas Gohr    public $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     */
18*3dc2d50cSAndreas Gohr    public $autosubmitFile;
198596046dSAndreas Gohr
208596046dSAndreas Gohr    /**
218596046dSAndreas Gohr     * File where the last error which happened when we tried to autosubmit, will be log
228596046dSAndreas Gohr     */
23*3dc2d50cSAndreas Gohr    public $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     */
30*3dc2d50cSAndreas Gohr    public $popularityLastSubmitFile;
315827ba0bSGuillaume Turri
325827ba0bSGuillaume Turri
33*3dc2d50cSAndreas Gohr    public function __construct(){
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
4036013a6fSGerrit Uitslag    /**
418596046dSAndreas Gohr     * Check if autosubmit is enabled
42253d4b48SGerrit Uitslag     *
4338479cbbSDominik Eckelmann     * @return boolean TRUE if we should send data once a month, FALSE otherwise
448596046dSAndreas Gohr     */
45*3dc2d50cSAndreas Gohr    public function isAutoSubmitEnabled(){
4679e79377SAndreas Gohr        return file_exists($this->autosubmitFile);
478596046dSAndreas Gohr    }
488596046dSAndreas Gohr
498596046dSAndreas Gohr    /**
508596046dSAndreas Gohr     * Send the data, to the submit url
51253d4b48SGerrit Uitslag     *
528596046dSAndreas Gohr     * @param string $data The popularity data
5338479cbbSDominik Eckelmann     * @return string An empty string if everything worked fine, a string describing the error otherwise
548596046dSAndreas Gohr     */
55*3dc2d50cSAndreas Gohr    public function sendData($data){
568596046dSAndreas Gohr        $error = '';
578596046dSAndreas Gohr        $httpClient = new DokuHTTPClient();
587d8a6abbSMichael Hamann        $status = $httpClient->sendRequest($this->submitUrl, array('data' => $data), 'POST');
598596046dSAndreas Gohr        if ( ! $status ){
608596046dSAndreas Gohr            $error = $httpClient->error;
618596046dSAndreas Gohr        }
628596046dSAndreas Gohr        return $error;
638596046dSAndreas Gohr    }
648596046dSAndreas Gohr
658596046dSAndreas Gohr    /**
665827ba0bSGuillaume Turri     * Compute the last time the data was sent. If it has never been sent, we return 0.
67253d4b48SGerrit Uitslag     *
68253d4b48SGerrit Uitslag     * @return int
695827ba0bSGuillaume Turri     */
70*3dc2d50cSAndreas Gohr    public function lastSentTime(){
715827ba0bSGuillaume Turri        $manualSubmission = @filemtime($this->popularityLastSubmitFile);
725827ba0bSGuillaume Turri        $autoSubmission   = @filemtime($this->autosubmitFile);
735827ba0bSGuillaume Turri
745827ba0bSGuillaume Turri        return max((int) $manualSubmission, (int) $autoSubmission);
755827ba0bSGuillaume Turri    }
765827ba0bSGuillaume Turri
775827ba0bSGuillaume Turri    /**
788596046dSAndreas Gohr     * Gather all information
79253d4b48SGerrit Uitslag     *
8038479cbbSDominik Eckelmann     * @return string The popularity data as a string
818596046dSAndreas Gohr     */
82*3dc2d50cSAndreas Gohr    public function gatherAsString(){
838596046dSAndreas Gohr        $data = $this->_gather();
848596046dSAndreas Gohr        $string = '';
858596046dSAndreas Gohr        foreach($data as $key => $val){
868596046dSAndreas Gohr            if(is_array($val)) foreach($val as $v){
878596046dSAndreas Gohr                $string .=  hsc($key)."\t".hsc($v)."\n";
888596046dSAndreas Gohr            }else{
898596046dSAndreas Gohr                $string .= hsc($key)."\t".hsc($val)."\n";
908596046dSAndreas Gohr            }
918596046dSAndreas Gohr        }
928596046dSAndreas Gohr        return $string;
938596046dSAndreas Gohr    }
948596046dSAndreas Gohr
958596046dSAndreas Gohr    /**
968596046dSAndreas Gohr     * Gather all information
97253d4b48SGerrit Uitslag     *
9838479cbbSDominik Eckelmann     * @return array The popularity data as an array
998596046dSAndreas Gohr     */
100*3dc2d50cSAndreas Gohr    protected function _gather(){
1018596046dSAndreas Gohr        global $conf;
10236013a6fSGerrit Uitslag        /** @var $auth DokuWiki_Auth_Plugin */
1038596046dSAndreas Gohr        global $auth;
1048596046dSAndreas Gohr        $data = array();
1058596046dSAndreas Gohr        $phptime = ini_get('max_execution_time');
1068596046dSAndreas Gohr        @set_time_limit(0);
107f119fb20SGerrit Uitslag        $pluginInfo = $this->getInfo();
1088596046dSAndreas Gohr
1098596046dSAndreas Gohr        // version
1108596046dSAndreas Gohr        $data['anon_id'] = md5(auth_cookiesalt());
1118596046dSAndreas Gohr        $data['version'] = getVersion();
112f119fb20SGerrit Uitslag        $data['popversion'] = $pluginInfo['date'];
1138596046dSAndreas Gohr        $data['language'] = $conf['lang'];
1148596046dSAndreas Gohr        $data['now']      = time();
115d4228d2dSAndreas Gohr        $data['popauto']  = (int) $this->isAutoSubmitEnabled();
1168596046dSAndreas Gohr
1178596046dSAndreas Gohr        // some config values
1188596046dSAndreas Gohr        $data['conf_useacl']   = $conf['useacl'];
1198596046dSAndreas Gohr        $data['conf_authtype'] = $conf['authtype'];
1208596046dSAndreas Gohr        $data['conf_template'] = $conf['template'];
1218596046dSAndreas Gohr
1228596046dSAndreas Gohr        // number and size of pages
1238596046dSAndreas Gohr        $list = array();
124506c1de7SAndreas Gohr        search($list,$conf['datadir'],array($this,'_search_count'),array('all'=>false),'');
1258596046dSAndreas Gohr        $data['page_count']    = $list['file_count'];
1268596046dSAndreas Gohr        $data['page_size']     = $list['file_size'];
1278596046dSAndreas Gohr        $data['page_biggest']  = $list['file_max'];
1288596046dSAndreas Gohr        $data['page_smallest'] = $list['file_min'];
1298596046dSAndreas Gohr        $data['page_nscount']  = $list['dir_count'];
1308596046dSAndreas Gohr        $data['page_nsnest']   = $list['dir_nest'];
1318596046dSAndreas Gohr        if($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count'];
1328596046dSAndreas Gohr        $data['page_oldest']   = $list['file_oldest'];
1338596046dSAndreas Gohr        unset($list);
1348596046dSAndreas Gohr
1358596046dSAndreas Gohr        // number and size of media
1368596046dSAndreas Gohr        $list = array();
1378596046dSAndreas Gohr        search($list,$conf['mediadir'],array($this,'_search_count'),array('all'=>true));
1388596046dSAndreas Gohr        $data['media_count']    = $list['file_count'];
1398596046dSAndreas Gohr        $data['media_size']     = $list['file_size'];
1408596046dSAndreas Gohr        $data['media_biggest']  = $list['file_max'];
1418596046dSAndreas Gohr        $data['media_smallest'] = $list['file_min'];
1428596046dSAndreas Gohr        $data['media_nscount']  = $list['dir_count'];
1438596046dSAndreas Gohr        $data['media_nsnest']   = $list['dir_nest'];
1448596046dSAndreas Gohr        if($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count'];
1458596046dSAndreas Gohr        unset($list);
1468596046dSAndreas Gohr
1478596046dSAndreas Gohr        // number and size of cache
1488596046dSAndreas Gohr        $list = array();
1498596046dSAndreas Gohr        search($list,$conf['cachedir'],array($this,'_search_count'),array('all'=>true));
1508596046dSAndreas Gohr        $data['cache_count']    = $list['file_count'];
1518596046dSAndreas Gohr        $data['cache_size']     = $list['file_size'];
1528596046dSAndreas Gohr        $data['cache_biggest']  = $list['file_max'];
1538596046dSAndreas Gohr        $data['cache_smallest'] = $list['file_min'];
1548596046dSAndreas Gohr        if($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count'];
1558596046dSAndreas Gohr        unset($list);
1568596046dSAndreas Gohr
1578596046dSAndreas Gohr        // number and size of index
1588596046dSAndreas Gohr        $list = array();
1598596046dSAndreas Gohr        search($list,$conf['indexdir'],array($this,'_search_count'),array('all'=>true));
1608596046dSAndreas Gohr        $data['index_count']    = $list['file_count'];
1618596046dSAndreas Gohr        $data['index_size']     = $list['file_size'];
1628596046dSAndreas Gohr        $data['index_biggest']  = $list['file_max'];
1638596046dSAndreas Gohr        $data['index_smallest'] = $list['file_min'];
1648596046dSAndreas Gohr        if($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count'];
1658596046dSAndreas Gohr        unset($list);
1668596046dSAndreas Gohr
1678596046dSAndreas Gohr        // number and size of meta
1688596046dSAndreas Gohr        $list = array();
1698596046dSAndreas Gohr        search($list,$conf['metadir'],array($this,'_search_count'),array('all'=>true));
1708596046dSAndreas Gohr        $data['meta_count']    = $list['file_count'];
1718596046dSAndreas Gohr        $data['meta_size']     = $list['file_size'];
1728596046dSAndreas Gohr        $data['meta_biggest']  = $list['file_max'];
1738596046dSAndreas Gohr        $data['meta_smallest'] = $list['file_min'];
1748596046dSAndreas Gohr        if($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count'];
1758596046dSAndreas Gohr        unset($list);
1768596046dSAndreas Gohr
1778596046dSAndreas Gohr        // number and size of attic
1788596046dSAndreas Gohr        $list = array();
1798596046dSAndreas Gohr        search($list,$conf['olddir'],array($this,'_search_count'),array('all'=>true));
1808596046dSAndreas Gohr        $data['attic_count']    = $list['file_count'];
1818596046dSAndreas Gohr        $data['attic_size']     = $list['file_size'];
1828596046dSAndreas Gohr        $data['attic_biggest']  = $list['file_max'];
1838596046dSAndreas Gohr        $data['attic_smallest'] = $list['file_min'];
1848596046dSAndreas Gohr        if($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count'];
1858596046dSAndreas Gohr        $data['attic_oldest']   = $list['file_oldest'];
1868596046dSAndreas Gohr        unset($list);
1878596046dSAndreas Gohr
1888596046dSAndreas Gohr        // user count
1898596046dSAndreas Gohr        if($auth && $auth->canDo('getUserCount')){
1908596046dSAndreas Gohr            $data['user_count'] = $auth->getUserCount();
1918596046dSAndreas Gohr        }
1928596046dSAndreas Gohr
1938596046dSAndreas Gohr        // calculate edits per day
1948596046dSAndreas Gohr        $list = @file($conf['metadir'].'/_dokuwiki.changes');
1958596046dSAndreas Gohr        $count = count($list);
1968596046dSAndreas Gohr        if($count > 2){
1978596046dSAndreas Gohr            $first = (int) substr(array_shift($list),0,10);
1988596046dSAndreas Gohr            $last  = (int) substr(array_pop($list),0,10);
1998596046dSAndreas Gohr            $dur = ($last - $first)/(60*60*24); // number of days in the changelog
2008596046dSAndreas Gohr            $data['edits_per_day'] = $count/$dur;
2018596046dSAndreas Gohr        }
2028596046dSAndreas Gohr        unset($list);
2038596046dSAndreas Gohr
2048596046dSAndreas Gohr        // plugins
2058596046dSAndreas Gohr        $data['plugin'] = plugin_list();
2068596046dSAndreas Gohr
2078596046dSAndreas Gohr        // pcre info
2088596046dSAndreas Gohr        if(defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION;
2098596046dSAndreas Gohr        $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit');
2108596046dSAndreas Gohr        $data['pcre_recursion'] = ini_get('pcre.recursion_limit');
2118596046dSAndreas Gohr
2128596046dSAndreas Gohr        // php info
2138596046dSAndreas Gohr        $data['os'] = PHP_OS;
2148596046dSAndreas Gohr        $data['webserver'] = $_SERVER['SERVER_SOFTWARE'];
2158596046dSAndreas Gohr        $data['php_version'] = phpversion();
2168596046dSAndreas Gohr        $data['php_sapi'] = php_sapi_name();
2178596046dSAndreas Gohr        $data['php_memory'] = $this->_to_byte(ini_get('memory_limit'));
2188596046dSAndreas Gohr        $data['php_exectime'] = $phptime;
2198596046dSAndreas Gohr        $data['php_extension'] = get_loaded_extensions();
2208596046dSAndreas Gohr
2215875e534SGuillaume Turri        // plugin usage data
2225875e534SGuillaume Turri        $this->_add_plugin_usage_data($data);
2235875e534SGuillaume Turri
2248596046dSAndreas Gohr        return $data;
2258596046dSAndreas Gohr    }
2268596046dSAndreas Gohr
227*3dc2d50cSAndreas Gohr    /**
228*3dc2d50cSAndreas Gohr     * Triggers event to let plugins add their own data
229*3dc2d50cSAndreas Gohr     *
230*3dc2d50cSAndreas Gohr     * @param $data
231*3dc2d50cSAndreas Gohr     */
2325875e534SGuillaume Turri    protected function _add_plugin_usage_data(&$data){
2335875e534SGuillaume Turri        $pluginsData = array();
2345875e534SGuillaume Turri        trigger_event('PLUGIN_POPULARITY_DATA_SETUP', $pluginsData);
2355875e534SGuillaume Turri        foreach($pluginsData as $plugin => $d){
2365875e534SGuillaume Turri           if ( is_array($d) ) {
2375875e534SGuillaume Turri               foreach($d as $key => $value){
2385875e534SGuillaume Turri                   $data['plugin_' . $plugin . '_' . $key] = $value;
2395875e534SGuillaume Turri               }
2405875e534SGuillaume Turri           } else {
2415875e534SGuillaume Turri               $data['plugin_' . $plugin] = $d;
2425875e534SGuillaume Turri           }
2435875e534SGuillaume Turri        }
2445875e534SGuillaume Turri    }
2455875e534SGuillaume Turri
24636013a6fSGerrit Uitslag    /**
24736013a6fSGerrit Uitslag     * Callback to search and count the content of directories in DokuWiki
24836013a6fSGerrit Uitslag     *
24936013a6fSGerrit Uitslag     * @param array &$data  Reference to the result data structure
25036013a6fSGerrit Uitslag     * @param string $base  Base usually $conf['datadir']
25136013a6fSGerrit Uitslag     * @param string $file  current file or directory relative to $base
25236013a6fSGerrit Uitslag     * @param string $type  Type either 'd' for directory or 'f' for file
25336013a6fSGerrit Uitslag     * @param int    $lvl   Current recursion depht
25436013a6fSGerrit Uitslag     * @param array  $opts  option array as given to search()
25536013a6fSGerrit Uitslag     * @return bool
25636013a6fSGerrit Uitslag     */
257*3dc2d50cSAndreas Gohr    protected function _search_count(&$data,$base,$file,$type,$lvl,$opts){
2588596046dSAndreas Gohr        // traverse
2598596046dSAndreas Gohr        if($type == 'd'){
2608596046dSAndreas Gohr            if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
2618596046dSAndreas Gohr            $data['dir_count']++;
2628596046dSAndreas Gohr            return true;
2638596046dSAndreas Gohr        }
2648596046dSAndreas Gohr
2658596046dSAndreas Gohr        //only search txt files if 'all' option not set
2668596046dSAndreas Gohr        if($opts['all'] || substr($file,-4) == '.txt'){
2678596046dSAndreas Gohr            $size = filesize($base.'/'.$file);
2688596046dSAndreas Gohr            $date = filemtime($base.'/'.$file);
2698596046dSAndreas Gohr            $data['file_count']++;
2708596046dSAndreas Gohr            $data['file_size'] += $size;
2718596046dSAndreas Gohr            if(!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size;
2728596046dSAndreas Gohr            if($data['file_max'] < $size) $data['file_max'] = $size;
2738596046dSAndreas Gohr            if(!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date;
2748596046dSAndreas Gohr        }
2758596046dSAndreas Gohr
2768596046dSAndreas Gohr        return false;
2778596046dSAndreas Gohr    }
2788596046dSAndreas Gohr
2798596046dSAndreas Gohr    /**
2808596046dSAndreas Gohr     * Convert php.ini shorthands to byte
2818596046dSAndreas Gohr     *
2828596046dSAndreas Gohr     * @author <gilthans dot NO dot SPAM at gmail dot com>
28359752844SAnders Sandblad     * @link   http://php.net/manual/en/ini.core.php#79564
284253d4b48SGerrit Uitslag     *
285253d4b48SGerrit Uitslag     * @param string $v
286253d4b48SGerrit Uitslag     * @return int|string
2878596046dSAndreas Gohr     */
288*3dc2d50cSAndreas Gohr    protected function _to_byte($v){
2898596046dSAndreas Gohr        $l = substr($v, -1);
2908596046dSAndreas Gohr        $ret = substr($v, 0, -1);
2918596046dSAndreas Gohr        switch(strtoupper($l)){
292253d4b48SGerrit Uitslag            /** @noinspection PhpMissingBreakStatementInspection */
2938596046dSAndreas Gohr            case 'P':
2948596046dSAndreas Gohr                $ret *= 1024;
295253d4b48SGerrit Uitslag            /** @noinspection PhpMissingBreakStatementInspection */
2968596046dSAndreas Gohr            case 'T':
2978596046dSAndreas Gohr                $ret *= 1024;
298253d4b48SGerrit Uitslag            /** @noinspection PhpMissingBreakStatementInspection */
2998596046dSAndreas Gohr            case 'G':
3008596046dSAndreas Gohr                $ret *= 1024;
301253d4b48SGerrit Uitslag            /** @noinspection PhpMissingBreakStatementInspection */
3028596046dSAndreas Gohr            case 'M':
3038596046dSAndreas Gohr                $ret *= 1024;
3048596046dSAndreas Gohr            case 'K':
3058596046dSAndreas Gohr                $ret *= 1024;
3068596046dSAndreas Gohr            break;
3078596046dSAndreas Gohr        }
3088596046dSAndreas Gohr        return $ret;
3098596046dSAndreas Gohr    }
3108596046dSAndreas Gohr}
311