xref: /dokuwiki/lib/plugins/popularity/helper.php (revision f51641de504e32f77a1aad6246ec001344380b66)
18596046dSAndreas Gohr<?php
2198564abSMichael Große
36547cfc7SGerrit Uitslaguse dokuwiki\Extension\AuthPlugin;
45a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient;
5cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
6198564abSMichael Große
78596046dSAndreas Gohr/**
88596046dSAndreas Gohr * Popularity Feedback Plugin
98596046dSAndreas Gohr *
108596046dSAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
118596046dSAndreas Gohr */
1229fc53cfSAndreas Gohrclass helper_plugin_popularity extends Dokuwiki_Plugin
1329fc53cfSAndreas Gohr{
148596046dSAndreas Gohr    /**
158596046dSAndreas Gohr     * The url where the data should be sent
168596046dSAndreas Gohr     */
17cf381878SNuno Silva    public $submitUrl = 'https://update.dokuwiki.org/popularity.php';
188596046dSAndreas Gohr
198596046dSAndreas Gohr    /**
208596046dSAndreas Gohr     * Name of the file which determine if the the autosubmit is enabled,
215827ba0bSGuillaume Turri     * and when it was submited for the last time
228596046dSAndreas Gohr     */
233dc2d50cSAndreas Gohr    public $autosubmitFile;
248596046dSAndreas Gohr
258596046dSAndreas Gohr    /**
268596046dSAndreas Gohr     * File where the last error which happened when we tried to autosubmit, will be log
278596046dSAndreas Gohr     */
283dc2d50cSAndreas Gohr    public $autosubmitErrorFile;
298596046dSAndreas Gohr
305827ba0bSGuillaume Turri    /**
315827ba0bSGuillaume Turri     * Name of the file which determine when the popularity data was manually
325827ba0bSGuillaume Turri     * submitted for the last time
335827ba0bSGuillaume Turri     * (If this file doesn't exist, the data has never been sent)
345827ba0bSGuillaume Turri     */
353dc2d50cSAndreas Gohr    public $popularityLastSubmitFile;
365827ba0bSGuillaume Turri
3729fc53cfSAndreas Gohr    /**
3829fc53cfSAndreas Gohr     * helper_plugin_popularity constructor.
3929fc53cfSAndreas Gohr     */
4029fc53cfSAndreas Gohr    public function __construct()
4129fc53cfSAndreas Gohr    {
428596046dSAndreas Gohr        global $conf;
438596046dSAndreas Gohr        $this->autosubmitFile = $conf['cachedir'] . '/autosubmit.txt';
448596046dSAndreas Gohr        $this->autosubmitErrorFile = $conf['cachedir'] . '/autosubmitError.txt';
455827ba0bSGuillaume Turri        $this->popularityLastSubmitFile = $conf['cachedir'] . '/lastSubmitTime.txt';
468596046dSAndreas Gohr    }
478596046dSAndreas Gohr
4836013a6fSGerrit Uitslag    /**
498596046dSAndreas Gohr     * Check if autosubmit is enabled
50253d4b48SGerrit Uitslag     *
5138479cbbSDominik Eckelmann     * @return boolean TRUE if we should send data once a month, FALSE otherwise
528596046dSAndreas Gohr     */
5329fc53cfSAndreas Gohr    public function isAutoSubmitEnabled()
5429fc53cfSAndreas Gohr    {
5579e79377SAndreas Gohr        return file_exists($this->autosubmitFile);
568596046dSAndreas Gohr    }
578596046dSAndreas Gohr
588596046dSAndreas Gohr    /**
598596046dSAndreas Gohr     * Send the data, to the submit url
60253d4b48SGerrit Uitslag     *
618596046dSAndreas Gohr     * @param string $data The popularity data
6238479cbbSDominik Eckelmann     * @return string An empty string if everything worked fine, a string describing the error otherwise
638596046dSAndreas Gohr     */
6429fc53cfSAndreas Gohr    public function sendData($data)
6529fc53cfSAndreas Gohr    {
668596046dSAndreas Gohr        $error = '';
678596046dSAndreas Gohr        $httpClient = new DokuHTTPClient();
6854cc7aa4SAndreas Gohr        $status = $httpClient->sendRequest($this->submitUrl, ['data' => $data], 'POST');
698596046dSAndreas Gohr        if (! $status) {
708596046dSAndreas Gohr            $error = $httpClient->error;
718596046dSAndreas Gohr        }
728596046dSAndreas Gohr        return $error;
738596046dSAndreas Gohr    }
748596046dSAndreas Gohr
758596046dSAndreas Gohr    /**
765827ba0bSGuillaume Turri     * Compute the last time the data was sent. If it has never been sent, we return 0.
77253d4b48SGerrit Uitslag     *
78253d4b48SGerrit Uitslag     * @return int
795827ba0bSGuillaume Turri     */
8029fc53cfSAndreas Gohr    public function lastSentTime()
8129fc53cfSAndreas Gohr    {
825827ba0bSGuillaume Turri        $manualSubmission = @filemtime($this->popularityLastSubmitFile);
835827ba0bSGuillaume Turri        $autoSubmission   = @filemtime($this->autosubmitFile);
845827ba0bSGuillaume Turri
855827ba0bSGuillaume Turri        return max((int) $manualSubmission, (int) $autoSubmission);
865827ba0bSGuillaume Turri    }
875827ba0bSGuillaume Turri
885827ba0bSGuillaume Turri    /**
898596046dSAndreas Gohr     * Gather all information
90253d4b48SGerrit Uitslag     *
9138479cbbSDominik Eckelmann     * @return string The popularity data as a string
928596046dSAndreas Gohr     */
9329fc53cfSAndreas Gohr    public function gatherAsString()
9429fc53cfSAndreas Gohr    {
9529fc53cfSAndreas Gohr        $data = $this->gather();
968596046dSAndreas Gohr        $string = '';
978596046dSAndreas Gohr        foreach ($data as $key => $val) {
988596046dSAndreas Gohr            if (is_array($val)) foreach ($val as $v) {
998596046dSAndreas Gohr                $string .=  hsc($key) . "\t" . hsc($v) . "\n";
1008596046dSAndreas Gohr            } else {
1018596046dSAndreas Gohr                $string .= hsc($key) . "\t" . hsc($val) . "\n";
1028596046dSAndreas Gohr            }
1038596046dSAndreas Gohr        }
1048596046dSAndreas Gohr        return $string;
1058596046dSAndreas Gohr    }
1068596046dSAndreas Gohr
1078596046dSAndreas Gohr    /**
1080d48ec5cSAndreas Gohr     * Initialize an empty list to be used in file traversing
1090d48ec5cSAndreas Gohr     *
1100d48ec5cSAndreas Gohr     * @return array
1110d48ec5cSAndreas Gohr     * @see searchCountCallback
1120d48ec5cSAndreas Gohr     */
113*f51641deSAndreas Gohr    public function initEmptySearchList()
1140d48ec5cSAndreas Gohr    {
115*f51641deSAndreas Gohr        return array_fill_keys([
1160d48ec5cSAndreas Gohr            'file_count',
1170d48ec5cSAndreas Gohr            'file_size',
1180d48ec5cSAndreas Gohr            'file_max',
1190d48ec5cSAndreas Gohr            'file_min',
1200d48ec5cSAndreas Gohr            'dir_count',
1210d48ec5cSAndreas Gohr            'dir_nest',
1220d48ec5cSAndreas Gohr            'file_oldest'
1230d48ec5cSAndreas Gohr        ], 0);
1240d48ec5cSAndreas Gohr    }
1250d48ec5cSAndreas Gohr
1260d48ec5cSAndreas Gohr    /**
1278596046dSAndreas Gohr     * Gather all information
128253d4b48SGerrit Uitslag     *
12938479cbbSDominik Eckelmann     * @return array The popularity data as an array
1308596046dSAndreas Gohr     */
13129fc53cfSAndreas Gohr    protected function gather()
13229fc53cfSAndreas Gohr    {
1338596046dSAndreas Gohr        global $conf;
13436013a6fSGerrit Uitslag        /** @var $auth DokuWiki_Auth_Plugin */
1358596046dSAndreas Gohr        global $auth;
13654cc7aa4SAndreas Gohr        $data = [];
1378596046dSAndreas Gohr        $phptime = ini_get('max_execution_time');
1388596046dSAndreas Gohr        @set_time_limit(0);
139f119fb20SGerrit Uitslag        $pluginInfo = $this->getInfo();
1408596046dSAndreas Gohr
1418596046dSAndreas Gohr        // version
1428596046dSAndreas Gohr        $data['anon_id'] = md5(auth_cookiesalt());
1438596046dSAndreas Gohr        $data['version'] = getVersion();
144f119fb20SGerrit Uitslag        $data['popversion'] = $pluginInfo['date'];
1458596046dSAndreas Gohr        $data['language'] = $conf['lang'];
1468596046dSAndreas Gohr        $data['now']      = time();
147d4228d2dSAndreas Gohr        $data['popauto']  = (int) $this->isAutoSubmitEnabled();
1488596046dSAndreas Gohr
1498596046dSAndreas Gohr        // some config values
1508596046dSAndreas Gohr        $data['conf_useacl']   = $conf['useacl'];
1518596046dSAndreas Gohr        $data['conf_authtype'] = $conf['authtype'];
1528596046dSAndreas Gohr        $data['conf_template'] = $conf['template'];
1538596046dSAndreas Gohr
1548596046dSAndreas Gohr        // number and size of pages
1550d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
15654cc7aa4SAndreas Gohr        search($list, $conf['datadir'], [$this, 'searchCountCallback'], ['all' => false], '');
1578596046dSAndreas Gohr        $data['page_count']    = $list['file_count'];
1588596046dSAndreas Gohr        $data['page_size']     = $list['file_size'];
1598596046dSAndreas Gohr        $data['page_biggest']  = $list['file_max'];
1608596046dSAndreas Gohr        $data['page_smallest'] = $list['file_min'];
1618596046dSAndreas Gohr        $data['page_nscount']  = $list['dir_count'];
1628596046dSAndreas Gohr        $data['page_nsnest']   = $list['dir_nest'];
1638596046dSAndreas Gohr        if ($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count'];
1648596046dSAndreas Gohr        $data['page_oldest']   = $list['file_oldest'];
1658596046dSAndreas Gohr        unset($list);
1668596046dSAndreas Gohr
1678596046dSAndreas Gohr        // number and size of media
1680d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
16954cc7aa4SAndreas Gohr        search($list, $conf['mediadir'], [$this, 'searchCountCallback'], ['all' => true]);
1708596046dSAndreas Gohr        $data['media_count']    = $list['file_count'];
1718596046dSAndreas Gohr        $data['media_size']     = $list['file_size'];
1728596046dSAndreas Gohr        $data['media_biggest']  = $list['file_max'];
1738596046dSAndreas Gohr        $data['media_smallest'] = $list['file_min'];
1748596046dSAndreas Gohr        $data['media_nscount']  = $list['dir_count'];
1758596046dSAndreas Gohr        $data['media_nsnest']   = $list['dir_nest'];
1768596046dSAndreas Gohr        if ($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count'];
1778596046dSAndreas Gohr        unset($list);
1788596046dSAndreas Gohr
1798596046dSAndreas Gohr        // number and size of cache
1800d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
18154cc7aa4SAndreas Gohr        search($list, $conf['cachedir'], [$this, 'searchCountCallback'], ['all' => true]);
1828596046dSAndreas Gohr        $data['cache_count']    = $list['file_count'];
1838596046dSAndreas Gohr        $data['cache_size']     = $list['file_size'];
1848596046dSAndreas Gohr        $data['cache_biggest']  = $list['file_max'];
1858596046dSAndreas Gohr        $data['cache_smallest'] = $list['file_min'];
1868596046dSAndreas Gohr        if ($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count'];
1878596046dSAndreas Gohr        unset($list);
1888596046dSAndreas Gohr
1898596046dSAndreas Gohr        // number and size of index
1900d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
19154cc7aa4SAndreas Gohr        search($list, $conf['indexdir'], [$this, 'searchCountCallback'], ['all' => true]);
1928596046dSAndreas Gohr        $data['index_count']    = $list['file_count'];
1938596046dSAndreas Gohr        $data['index_size']     = $list['file_size'];
1948596046dSAndreas Gohr        $data['index_biggest']  = $list['file_max'];
1958596046dSAndreas Gohr        $data['index_smallest'] = $list['file_min'];
1968596046dSAndreas Gohr        if ($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count'];
1978596046dSAndreas Gohr        unset($list);
1988596046dSAndreas Gohr
1998596046dSAndreas Gohr        // number and size of meta
2000d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
20154cc7aa4SAndreas Gohr        search($list, $conf['metadir'], [$this, 'searchCountCallback'], ['all' => true]);
2028596046dSAndreas Gohr        $data['meta_count']    = $list['file_count'];
2038596046dSAndreas Gohr        $data['meta_size']     = $list['file_size'];
2048596046dSAndreas Gohr        $data['meta_biggest']  = $list['file_max'];
2058596046dSAndreas Gohr        $data['meta_smallest'] = $list['file_min'];
2068596046dSAndreas Gohr        if ($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count'];
2078596046dSAndreas Gohr        unset($list);
2088596046dSAndreas Gohr
2098596046dSAndreas Gohr        // number and size of attic
2100d48ec5cSAndreas Gohr        $list = $this->initEmptySearchList();
21154cc7aa4SAndreas Gohr        search($list, $conf['olddir'], [$this, 'searchCountCallback'], ['all' => true]);
2128596046dSAndreas Gohr        $data['attic_count']    = $list['file_count'];
2138596046dSAndreas Gohr        $data['attic_size']     = $list['file_size'];
2148596046dSAndreas Gohr        $data['attic_biggest']  = $list['file_max'];
2158596046dSAndreas Gohr        $data['attic_smallest'] = $list['file_min'];
2168596046dSAndreas Gohr        if ($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count'];
2178596046dSAndreas Gohr        $data['attic_oldest']   = $list['file_oldest'];
2188596046dSAndreas Gohr        unset($list);
2198596046dSAndreas Gohr
2208596046dSAndreas Gohr        // user count
2216547cfc7SGerrit Uitslag        if ($auth instanceof AuthPlugin && $auth->canDo('getUserCount')) {
2228596046dSAndreas Gohr            $data['user_count'] = $auth->getUserCount();
2238596046dSAndreas Gohr        }
2248596046dSAndreas Gohr
2258596046dSAndreas Gohr        // calculate edits per day
2260d48ec5cSAndreas Gohr        $list = (array) @file($conf['metadir'] . '/_dokuwiki.changes');
2278596046dSAndreas Gohr        $count = count($list);
2288596046dSAndreas Gohr        if ($count > 2) {
2298596046dSAndreas Gohr            $first = (int) substr(array_shift($list), 0, 10);
2308596046dSAndreas Gohr            $last  = (int) substr(array_pop($list), 0, 10);
2318596046dSAndreas Gohr            $dur = ($last - $first) / (60 * 60 * 24); // number of days in the changelog
2328596046dSAndreas Gohr            $data['edits_per_day'] = $count / $dur;
2338596046dSAndreas Gohr        }
2348596046dSAndreas Gohr        unset($list);
2358596046dSAndreas Gohr
2368596046dSAndreas Gohr        // plugins
2378596046dSAndreas Gohr        $data['plugin'] = plugin_list();
2388596046dSAndreas Gohr
2398596046dSAndreas Gohr        // pcre info
2408596046dSAndreas Gohr        if (defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION;
2418596046dSAndreas Gohr        $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit');
2428596046dSAndreas Gohr        $data['pcre_recursion'] = ini_get('pcre.recursion_limit');
2438596046dSAndreas Gohr
2448596046dSAndreas Gohr        // php info
2458596046dSAndreas Gohr        $data['os'] = PHP_OS;
2468596046dSAndreas Gohr        $data['webserver'] = $_SERVER['SERVER_SOFTWARE'];
2478596046dSAndreas Gohr        $data['php_version'] = phpversion();
24854cc7aa4SAndreas Gohr        $data['php_sapi'] = PHP_SAPI;
24929fc53cfSAndreas Gohr        $data['php_memory'] = php_to_byte(ini_get('memory_limit'));
2508596046dSAndreas Gohr        $data['php_exectime'] = $phptime;
2518596046dSAndreas Gohr        $data['php_extension'] = get_loaded_extensions();
2528596046dSAndreas Gohr
2535875e534SGuillaume Turri        // plugin usage data
25429fc53cfSAndreas Gohr        $this->addPluginUsageData($data);
2555875e534SGuillaume Turri
2568596046dSAndreas Gohr        return $data;
2578596046dSAndreas Gohr    }
2588596046dSAndreas Gohr
2593dc2d50cSAndreas Gohr    /**
2603dc2d50cSAndreas Gohr     * Triggers event to let plugins add their own data
2613dc2d50cSAndreas Gohr     *
2623dc2d50cSAndreas Gohr     * @param $data
2633dc2d50cSAndreas Gohr     */
26429fc53cfSAndreas Gohr    protected function addPluginUsageData(&$data)
26529fc53cfSAndreas Gohr    {
26654cc7aa4SAndreas Gohr        $pluginsData = [];
267cbb44eabSAndreas Gohr        Event::createAndTrigger('PLUGIN_POPULARITY_DATA_SETUP', $pluginsData);
2685875e534SGuillaume Turri        foreach ($pluginsData as $plugin => $d) {
2695875e534SGuillaume Turri            if (is_array($d)) {
2705875e534SGuillaume Turri                foreach ($d as $key => $value) {
2715875e534SGuillaume Turri                    $data['plugin_' . $plugin . '_' . $key] = $value;
2725875e534SGuillaume Turri                }
2735875e534SGuillaume Turri            } else {
2745875e534SGuillaume Turri                $data['plugin_' . $plugin] = $d;
2755875e534SGuillaume Turri            }
2765875e534SGuillaume Turri        }
2775875e534SGuillaume Turri    }
2785875e534SGuillaume Turri
27936013a6fSGerrit Uitslag    /**
28036013a6fSGerrit Uitslag     * Callback to search and count the content of directories in DokuWiki
28136013a6fSGerrit Uitslag     *
282*f51641deSAndreas Gohr     * @param array &$data  Reference to the result data structure, init with initEmptySearchList()
28336013a6fSGerrit Uitslag     * @param string $base  Base usually $conf['datadir']
28436013a6fSGerrit Uitslag     * @param string $file  current file or directory relative to $base
28536013a6fSGerrit Uitslag     * @param string $type  Type either 'd' for directory or 'f' for file
28636013a6fSGerrit Uitslag     * @param int    $lvl   Current recursion depht
28736013a6fSGerrit Uitslag     * @param array  $opts  option array as given to search()
28836013a6fSGerrit Uitslag     * @return bool
28936013a6fSGerrit Uitslag     */
2903409ba76SAndreas Gohr    public function searchCountCallback(&$data, $base, $file, $type, $lvl, $opts)
29129fc53cfSAndreas Gohr    {
2928596046dSAndreas Gohr        // traverse
2938596046dSAndreas Gohr        if ($type == 'd') {
2948596046dSAndreas Gohr            if ($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
2958596046dSAndreas Gohr            $data['dir_count']++;
2968596046dSAndreas Gohr            return true;
2978596046dSAndreas Gohr        }
2988596046dSAndreas Gohr
2998596046dSAndreas Gohr        //only search txt files if 'all' option not set
3001b2deed9Sfiwswe        if ($opts['all'] || str_ends_with($file, '.txt')) {
3018596046dSAndreas Gohr            $size = filesize($base . '/' . $file);
3028596046dSAndreas Gohr            $date = filemtime($base . '/' . $file);
3038596046dSAndreas Gohr            $data['file_count']++;
3048596046dSAndreas Gohr            $data['file_size'] += $size;
3058596046dSAndreas Gohr            if (!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size;
3068596046dSAndreas Gohr            if ($data['file_max'] < $size) $data['file_max'] = $size;
3078596046dSAndreas Gohr            if (!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date;
3088596046dSAndreas Gohr        }
3098596046dSAndreas Gohr
3108596046dSAndreas Gohr        return false;
3118596046dSAndreas Gohr    }
3128596046dSAndreas Gohr}
313