xref: /dokuwiki/lib/plugins/popularity/helper.php (revision 54cc7aa41e0f453bd6887b0e79242a139d84a47a)
18596046dSAndreas Gohr<?php
2198564abSMichael Große
35a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient;
4cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
5198564abSMichael Große
68596046dSAndreas Gohr/**
78596046dSAndreas Gohr * Popularity Feedback Plugin
88596046dSAndreas Gohr *
98596046dSAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
108596046dSAndreas Gohr */
1129fc53cfSAndreas Gohrclass helper_plugin_popularity extends Dokuwiki_Plugin
1229fc53cfSAndreas Gohr{
138596046dSAndreas Gohr    /**
148596046dSAndreas Gohr     * The url where the data should be sent
158596046dSAndreas Gohr     */
16cf381878SNuno Silva    public $submitUrl = 'https://update.dokuwiki.org/popularity.php';
178596046dSAndreas Gohr
188596046dSAndreas Gohr    /**
198596046dSAndreas Gohr     * Name of the file which determine if the the autosubmit is enabled,
205827ba0bSGuillaume Turri     * and when it was submited for the last time
218596046dSAndreas Gohr     */
223dc2d50cSAndreas Gohr    public $autosubmitFile;
238596046dSAndreas Gohr
248596046dSAndreas Gohr    /**
258596046dSAndreas Gohr     * File where the last error which happened when we tried to autosubmit, will be log
268596046dSAndreas Gohr     */
273dc2d50cSAndreas Gohr    public $autosubmitErrorFile;
288596046dSAndreas Gohr
295827ba0bSGuillaume Turri    /**
305827ba0bSGuillaume Turri     * Name of the file which determine when the popularity data was manually
315827ba0bSGuillaume Turri     * submitted for the last time
325827ba0bSGuillaume Turri     * (If this file doesn't exist, the data has never been sent)
335827ba0bSGuillaume Turri     */
343dc2d50cSAndreas Gohr    public $popularityLastSubmitFile;
355827ba0bSGuillaume Turri
3629fc53cfSAndreas Gohr    /**
3729fc53cfSAndreas Gohr     * helper_plugin_popularity constructor.
3829fc53cfSAndreas Gohr     */
3929fc53cfSAndreas Gohr    public function __construct()
4029fc53cfSAndreas Gohr    {
418596046dSAndreas Gohr        global $conf;
428596046dSAndreas Gohr        $this->autosubmitFile = $conf['cachedir'].'/autosubmit.txt';
438596046dSAndreas Gohr        $this->autosubmitErrorFile = $conf['cachedir'].'/autosubmitError.txt';
445827ba0bSGuillaume Turri        $this->popularityLastSubmitFile = $conf['cachedir'].'/lastSubmitTime.txt';
458596046dSAndreas Gohr    }
468596046dSAndreas Gohr
4736013a6fSGerrit Uitslag    /**
488596046dSAndreas Gohr     * Check if autosubmit is enabled
49253d4b48SGerrit Uitslag     *
5038479cbbSDominik Eckelmann     * @return boolean TRUE if we should send data once a month, FALSE otherwise
518596046dSAndreas Gohr     */
5229fc53cfSAndreas Gohr    public function isAutoSubmitEnabled()
5329fc53cfSAndreas Gohr    {
5479e79377SAndreas Gohr        return file_exists($this->autosubmitFile);
558596046dSAndreas Gohr    }
568596046dSAndreas Gohr
578596046dSAndreas Gohr    /**
588596046dSAndreas Gohr     * Send the data, to the submit url
59253d4b48SGerrit Uitslag     *
608596046dSAndreas Gohr     * @param string $data The popularity data
6138479cbbSDominik Eckelmann     * @return string An empty string if everything worked fine, a string describing the error otherwise
628596046dSAndreas Gohr     */
6329fc53cfSAndreas Gohr    public function sendData($data)
6429fc53cfSAndreas Gohr    {
658596046dSAndreas Gohr        $error = '';
668596046dSAndreas Gohr        $httpClient = new DokuHTTPClient();
67*54cc7aa4SAndreas Gohr        $status = $httpClient->sendRequest($this->submitUrl, ['data' => $data], 'POST');
688596046dSAndreas Gohr        if (! $status) {
698596046dSAndreas Gohr            $error = $httpClient->error;
708596046dSAndreas Gohr        }
718596046dSAndreas Gohr        return $error;
728596046dSAndreas Gohr    }
738596046dSAndreas Gohr
748596046dSAndreas Gohr    /**
755827ba0bSGuillaume Turri     * Compute the last time the data was sent. If it has never been sent, we return 0.
76253d4b48SGerrit Uitslag     *
77253d4b48SGerrit Uitslag     * @return int
785827ba0bSGuillaume Turri     */
7929fc53cfSAndreas Gohr    public function lastSentTime()
8029fc53cfSAndreas Gohr    {
815827ba0bSGuillaume Turri        $manualSubmission = @filemtime($this->popularityLastSubmitFile);
825827ba0bSGuillaume Turri        $autoSubmission   = @filemtime($this->autosubmitFile);
835827ba0bSGuillaume Turri
845827ba0bSGuillaume Turri        return max((int) $manualSubmission, (int) $autoSubmission);
855827ba0bSGuillaume Turri    }
865827ba0bSGuillaume Turri
875827ba0bSGuillaume Turri    /**
888596046dSAndreas Gohr     * Gather all information
89253d4b48SGerrit Uitslag     *
9038479cbbSDominik Eckelmann     * @return string The popularity data as a string
918596046dSAndreas Gohr     */
9229fc53cfSAndreas Gohr    public function gatherAsString()
9329fc53cfSAndreas Gohr    {
9429fc53cfSAndreas Gohr        $data = $this->gather();
958596046dSAndreas Gohr        $string = '';
968596046dSAndreas Gohr        foreach ($data as $key => $val) {
978596046dSAndreas Gohr            if (is_array($val)) foreach ($val as $v) {
988596046dSAndreas Gohr                $string .=  hsc($key)."\t".hsc($v)."\n";
998596046dSAndreas Gohr            } else {
1008596046dSAndreas Gohr                $string .= hsc($key)."\t".hsc($val)."\n";
1018596046dSAndreas Gohr            }
1028596046dSAndreas Gohr        }
1038596046dSAndreas Gohr        return $string;
1048596046dSAndreas Gohr    }
1058596046dSAndreas Gohr
1068596046dSAndreas Gohr    /**
1070d48ec5cSAndreas Gohr     * Initialize an empty list to be used in file traversing
1080d48ec5cSAndreas Gohr     *
1090d48ec5cSAndreas Gohr     * @return array
1100d48ec5cSAndreas Gohr     * @see searchCountCallback
1110d48ec5cSAndreas Gohr     */
112042e7b39SAndreas Gohr    protected function initEmptySearchList()
1130d48ec5cSAndreas Gohr    {
1140d48ec5cSAndreas Gohr        return $list = array_fill_keys([
1150d48ec5cSAndreas Gohr            'file_count',
1160d48ec5cSAndreas Gohr            'file_size',
1170d48ec5cSAndreas Gohr            'file_max',
1180d48ec5cSAndreas Gohr            'file_min',
1190d48ec5cSAndreas Gohr            'dir_count',
1200d48ec5cSAndreas Gohr            'dir_nest',
1210d48ec5cSAndreas Gohr            'file_oldest'
1220d48ec5cSAndreas Gohr        ], 0);
1230d48ec5cSAndreas Gohr    }
1240d48ec5cSAndreas Gohr
1250d48ec5cSAndreas Gohr    /**
1268596046dSAndreas Gohr     * Gather all information
127253d4b48SGerrit Uitslag     *
12838479cbbSDominik Eckelmann     * @return array The popularity data as an array
1298596046dSAndreas Gohr     */
13029fc53cfSAndreas Gohr    protected function gather()
13129fc53cfSAndreas Gohr    {
1328596046dSAndreas Gohr        global $conf;
13336013a6fSGerrit Uitslag        /** @var $auth DokuWiki_Auth_Plugin */
1348596046dSAndreas Gohr        global $auth;
135*54cc7aa4SAndreas Gohr        $data = [];
1368596046dSAndreas Gohr        $phptime = ini_get('max_execution_time');
1378596046dSAndreas Gohr        @set_time_limit(0);
138f119fb20SGerrit Uitslag        $pluginInfo = $this->getInfo();
1398596046dSAndreas Gohr
1408596046dSAndreas Gohr        // version
1418596046dSAndreas Gohr        $data['anon_id'] = md5(auth_cookiesalt());
1428596046dSAndreas Gohr        $data['version'] = getVersion();
143f119fb20SGerrit Uitslag        $data['popversion'] = $pluginInfo['date'];
1448596046dSAndreas Gohr        $data['language'] = $conf['lang'];
1458596046dSAndreas Gohr        $data['now']      = time();
146d4228d2dSAndreas Gohr        $data['popauto']  = (int) $this->isAutoSubmitEnabled();
1478596046dSAndreas Gohr
1488596046dSAndreas Gohr        // some config values
1498596046dSAndreas Gohr        $data['conf_useacl']   = $conf['useacl'];
1508596046dSAndreas Gohr        $data['conf_authtype'] = $conf['authtype'];
1518596046dSAndreas Gohr        $data['conf_template'] = $conf['template'];
1528596046dSAndreas Gohr
1538596046dSAndreas Gohr        // number and size of pages
1540d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
155*54cc7aa4SAndreas Gohr        search($list, $conf['datadir'], [$this, 'searchCountCallback'], ['all'=>false], '');
1568596046dSAndreas Gohr        $data['page_count']    = $list['file_count'];
1578596046dSAndreas Gohr        $data['page_size']     = $list['file_size'];
1588596046dSAndreas Gohr        $data['page_biggest']  = $list['file_max'];
1598596046dSAndreas Gohr        $data['page_smallest'] = $list['file_min'];
1608596046dSAndreas Gohr        $data['page_nscount']  = $list['dir_count'];
1618596046dSAndreas Gohr        $data['page_nsnest']   = $list['dir_nest'];
1628596046dSAndreas Gohr        if ($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count'];
1638596046dSAndreas Gohr        $data['page_oldest']   = $list['file_oldest'];
1648596046dSAndreas Gohr        unset($list);
1658596046dSAndreas Gohr
1668596046dSAndreas Gohr        // number and size of media
1670d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
168*54cc7aa4SAndreas Gohr        search($list, $conf['mediadir'], [$this, 'searchCountCallback'], ['all'=>true]);
1698596046dSAndreas Gohr        $data['media_count']    = $list['file_count'];
1708596046dSAndreas Gohr        $data['media_size']     = $list['file_size'];
1718596046dSAndreas Gohr        $data['media_biggest']  = $list['file_max'];
1728596046dSAndreas Gohr        $data['media_smallest'] = $list['file_min'];
1738596046dSAndreas Gohr        $data['media_nscount']  = $list['dir_count'];
1748596046dSAndreas Gohr        $data['media_nsnest']   = $list['dir_nest'];
1758596046dSAndreas Gohr        if ($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count'];
1768596046dSAndreas Gohr        unset($list);
1778596046dSAndreas Gohr
1788596046dSAndreas Gohr        // number and size of cache
1790d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
180*54cc7aa4SAndreas Gohr        search($list, $conf['cachedir'], [$this, 'searchCountCallback'], ['all'=>true]);
1818596046dSAndreas Gohr        $data['cache_count']    = $list['file_count'];
1828596046dSAndreas Gohr        $data['cache_size']     = $list['file_size'];
1838596046dSAndreas Gohr        $data['cache_biggest']  = $list['file_max'];
1848596046dSAndreas Gohr        $data['cache_smallest'] = $list['file_min'];
1858596046dSAndreas Gohr        if ($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count'];
1868596046dSAndreas Gohr        unset($list);
1878596046dSAndreas Gohr
1888596046dSAndreas Gohr        // number and size of index
1890d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
190*54cc7aa4SAndreas Gohr        search($list, $conf['indexdir'], [$this, 'searchCountCallback'], ['all'=>true]);
1918596046dSAndreas Gohr        $data['index_count']    = $list['file_count'];
1928596046dSAndreas Gohr        $data['index_size']     = $list['file_size'];
1938596046dSAndreas Gohr        $data['index_biggest']  = $list['file_max'];
1948596046dSAndreas Gohr        $data['index_smallest'] = $list['file_min'];
1958596046dSAndreas Gohr        if ($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count'];
1968596046dSAndreas Gohr        unset($list);
1978596046dSAndreas Gohr
1988596046dSAndreas Gohr        // number and size of meta
1990d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
200*54cc7aa4SAndreas Gohr        search($list, $conf['metadir'], [$this, 'searchCountCallback'], ['all'=>true]);
2018596046dSAndreas Gohr        $data['meta_count']    = $list['file_count'];
2028596046dSAndreas Gohr        $data['meta_size']     = $list['file_size'];
2038596046dSAndreas Gohr        $data['meta_biggest']  = $list['file_max'];
2048596046dSAndreas Gohr        $data['meta_smallest'] = $list['file_min'];
2058596046dSAndreas Gohr        if ($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count'];
2068596046dSAndreas Gohr        unset($list);
2078596046dSAndreas Gohr
2088596046dSAndreas Gohr        // number and size of attic
2090d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
210*54cc7aa4SAndreas Gohr        search($list, $conf['olddir'], [$this, 'searchCountCallback'], ['all'=>true]);
2118596046dSAndreas Gohr        $data['attic_count']    = $list['file_count'];
2128596046dSAndreas Gohr        $data['attic_size']     = $list['file_size'];
2138596046dSAndreas Gohr        $data['attic_biggest']  = $list['file_max'];
2148596046dSAndreas Gohr        $data['attic_smallest'] = $list['file_min'];
2158596046dSAndreas Gohr        if ($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count'];
2168596046dSAndreas Gohr        $data['attic_oldest']   = $list['file_oldest'];
2178596046dSAndreas Gohr        unset($list);
2188596046dSAndreas Gohr
2198596046dSAndreas Gohr        // user count
2208596046dSAndreas Gohr        if ($auth && $auth->canDo('getUserCount')) {
2218596046dSAndreas Gohr            $data['user_count'] = $auth->getUserCount();
2228596046dSAndreas Gohr        }
2238596046dSAndreas Gohr
2248596046dSAndreas Gohr        // calculate edits per day
2250d48ec5cSAndreas Gohr        $list = (array) @file($conf['metadir'].'/_dokuwiki.changes');
2268596046dSAndreas Gohr        $count = count($list);
2278596046dSAndreas Gohr        if ($count > 2) {
2288596046dSAndreas Gohr            $first = (int) substr(array_shift($list), 0, 10);
2298596046dSAndreas Gohr            $last  = (int) substr(array_pop($list), 0, 10);
2308596046dSAndreas Gohr            $dur = ($last - $first)/(60*60*24); // number of days in the changelog
2318596046dSAndreas Gohr            $data['edits_per_day'] = $count/$dur;
2328596046dSAndreas Gohr        }
2338596046dSAndreas Gohr        unset($list);
2348596046dSAndreas Gohr
2358596046dSAndreas Gohr        // plugins
2368596046dSAndreas Gohr        $data['plugin'] = plugin_list();
2378596046dSAndreas Gohr
2388596046dSAndreas Gohr        // pcre info
2398596046dSAndreas Gohr        if (defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION;
2408596046dSAndreas Gohr        $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit');
2418596046dSAndreas Gohr        $data['pcre_recursion'] = ini_get('pcre.recursion_limit');
2428596046dSAndreas Gohr
2438596046dSAndreas Gohr        // php info
2448596046dSAndreas Gohr        $data['os'] = PHP_OS;
2458596046dSAndreas Gohr        $data['webserver'] = $_SERVER['SERVER_SOFTWARE'];
2468596046dSAndreas Gohr        $data['php_version'] = phpversion();
247*54cc7aa4SAndreas Gohr        $data['php_sapi'] = PHP_SAPI;
24829fc53cfSAndreas Gohr        $data['php_memory'] = php_to_byte(ini_get('memory_limit'));
2498596046dSAndreas Gohr        $data['php_exectime'] = $phptime;
2508596046dSAndreas Gohr        $data['php_extension'] = get_loaded_extensions();
2518596046dSAndreas Gohr
2525875e534SGuillaume Turri        // plugin usage data
25329fc53cfSAndreas Gohr        $this->addPluginUsageData($data);
2545875e534SGuillaume Turri
2558596046dSAndreas Gohr        return $data;
2568596046dSAndreas Gohr    }
2578596046dSAndreas Gohr
2583dc2d50cSAndreas Gohr    /**
2593dc2d50cSAndreas Gohr     * Triggers event to let plugins add their own data
2603dc2d50cSAndreas Gohr     *
2613dc2d50cSAndreas Gohr     * @param $data
2623dc2d50cSAndreas Gohr     */
26329fc53cfSAndreas Gohr    protected function addPluginUsageData(&$data)
26429fc53cfSAndreas Gohr    {
265*54cc7aa4SAndreas Gohr        $pluginsData = [];
266cbb44eabSAndreas Gohr        Event::createAndTrigger('PLUGIN_POPULARITY_DATA_SETUP', $pluginsData);
2675875e534SGuillaume Turri        foreach ($pluginsData as $plugin => $d) {
2685875e534SGuillaume Turri            if (is_array($d)) {
2695875e534SGuillaume Turri                foreach ($d as $key => $value) {
2705875e534SGuillaume Turri                    $data['plugin_' . $plugin . '_' . $key] = $value;
2715875e534SGuillaume Turri                }
2725875e534SGuillaume Turri            } else {
2735875e534SGuillaume Turri                $data['plugin_' . $plugin] = $d;
2745875e534SGuillaume Turri            }
2755875e534SGuillaume Turri        }
2765875e534SGuillaume Turri    }
2775875e534SGuillaume Turri
27836013a6fSGerrit Uitslag    /**
27936013a6fSGerrit Uitslag     * Callback to search and count the content of directories in DokuWiki
28036013a6fSGerrit Uitslag     *
28136013a6fSGerrit Uitslag     * @param array &$data  Reference to the result data structure
28236013a6fSGerrit Uitslag     * @param string $base  Base usually $conf['datadir']
28336013a6fSGerrit Uitslag     * @param string $file  current file or directory relative to $base
28436013a6fSGerrit Uitslag     * @param string $type  Type either 'd' for directory or 'f' for file
28536013a6fSGerrit Uitslag     * @param int    $lvl   Current recursion depht
28636013a6fSGerrit Uitslag     * @param array  $opts  option array as given to search()
28736013a6fSGerrit Uitslag     * @return bool
28836013a6fSGerrit Uitslag     */
2893409ba76SAndreas Gohr    public function searchCountCallback(&$data, $base, $file, $type, $lvl, $opts)
29029fc53cfSAndreas Gohr    {
2918596046dSAndreas Gohr        // traverse
2928596046dSAndreas Gohr        if ($type == 'd') {
2938596046dSAndreas Gohr            if ($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
2948596046dSAndreas Gohr            $data['dir_count']++;
2958596046dSAndreas Gohr            return true;
2968596046dSAndreas Gohr        }
2978596046dSAndreas Gohr
2988596046dSAndreas Gohr        //only search txt files if 'all' option not set
2998596046dSAndreas Gohr        if ($opts['all'] || substr($file, -4) == '.txt') {
3008596046dSAndreas Gohr            $size = filesize($base.'/'.$file);
3018596046dSAndreas Gohr            $date = filemtime($base.'/'.$file);
3028596046dSAndreas Gohr            $data['file_count']++;
3038596046dSAndreas Gohr            $data['file_size'] += $size;
3048596046dSAndreas Gohr            if (!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size;
3058596046dSAndreas Gohr            if ($data['file_max'] < $size) $data['file_max'] = $size;
3068596046dSAndreas Gohr            if (!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date;
3078596046dSAndreas Gohr        }
3088596046dSAndreas Gohr
3098596046dSAndreas Gohr        return false;
3108596046dSAndreas Gohr    }
3118596046dSAndreas Gohr}
312