1<?php
2
3use dokuwiki\Extension\AuthPlugin;
4use dokuwiki\HTTP\DokuHTTPClient;
5use dokuwiki\Extension\Event;
6
7/**
8 * Popularity Feedback Plugin
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 */
12class helper_plugin_popularity extends Dokuwiki_Plugin
13{
14    /**
15     * The url where the data should be sent
16     */
17    public $submitUrl = 'https://update.dokuwiki.org/popularity.php';
18
19    /**
20     * Name of the file which determine if the the autosubmit is enabled,
21     * and when it was submited for the last time
22     */
23    public $autosubmitFile;
24
25    /**
26     * File where the last error which happened when we tried to autosubmit, will be log
27     */
28    public $autosubmitErrorFile;
29
30    /**
31     * Name of the file which determine when the popularity data was manually
32     * submitted for the last time
33     * (If this file doesn't exist, the data has never been sent)
34     */
35    public $popularityLastSubmitFile;
36
37    /**
38     * helper_plugin_popularity constructor.
39     */
40    public function __construct()
41    {
42        global $conf;
43        $this->autosubmitFile = $conf['cachedir'] . '/autosubmit.txt';
44        $this->autosubmitErrorFile = $conf['cachedir'] . '/autosubmitError.txt';
45        $this->popularityLastSubmitFile = $conf['cachedir'] . '/lastSubmitTime.txt';
46    }
47
48    /**
49     * Check if autosubmit is enabled
50     *
51     * @return boolean TRUE if we should send data once a month, FALSE otherwise
52     */
53    public function isAutoSubmitEnabled()
54    {
55        return file_exists($this->autosubmitFile);
56    }
57
58    /**
59     * Send the data, to the submit url
60     *
61     * @param string $data The popularity data
62     * @return string An empty string if everything worked fine, a string describing the error otherwise
63     */
64    public function sendData($data)
65    {
66        $error = '';
67        $httpClient = new DokuHTTPClient();
68        $status = $httpClient->sendRequest($this->submitUrl, ['data' => $data], 'POST');
69        if (! $status) {
70            $error = $httpClient->error;
71        }
72        return $error;
73    }
74
75    /**
76     * Compute the last time the data was sent. If it has never been sent, we return 0.
77     *
78     * @return int
79     */
80    public function lastSentTime()
81    {
82        $manualSubmission = @filemtime($this->popularityLastSubmitFile);
83        $autoSubmission   = @filemtime($this->autosubmitFile);
84
85        return max((int) $manualSubmission, (int) $autoSubmission);
86    }
87
88    /**
89     * Gather all information
90     *
91     * @return string The popularity data as a string
92     */
93    public function gatherAsString()
94    {
95        $data = $this->gather();
96        $string = '';
97        foreach ($data as $key => $val) {
98            if (is_array($val)) foreach ($val as $v) {
99                $string .=  hsc($key) . "\t" . hsc($v) . "\n";
100            } else {
101                $string .= hsc($key) . "\t" . hsc($val) . "\n";
102            }
103        }
104        return $string;
105    }
106
107    /**
108     * Initialize an empty list to be used in file traversing
109     *
110     * @return array
111     * @see searchCountCallback
112     */
113    protected function initEmptySearchList()
114    {
115        return $list = array_fill_keys([
116            'file_count',
117            'file_size',
118            'file_max',
119            'file_min',
120            'dir_count',
121            'dir_nest',
122            'file_oldest'
123        ], 0);
124    }
125
126    /**
127     * Gather all information
128     *
129     * @return array The popularity data as an array
130     */
131    protected function gather()
132    {
133        global $conf;
134        /** @var $auth DokuWiki_Auth_Plugin */
135        global $auth;
136        $data = [];
137        $phptime = ini_get('max_execution_time');
138        @set_time_limit(0);
139        $pluginInfo = $this->getInfo();
140
141        // version
142        $data['anon_id'] = md5(auth_cookiesalt());
143        $data['version'] = getVersion();
144        $data['popversion'] = $pluginInfo['date'];
145        $data['language'] = $conf['lang'];
146        $data['now']      = time();
147        $data['popauto']  = (int) $this->isAutoSubmitEnabled();
148
149        // some config values
150        $data['conf_useacl']   = $conf['useacl'];
151        $data['conf_authtype'] = $conf['authtype'];
152        $data['conf_template'] = $conf['template'];
153
154        // number and size of pages
155        $list = $this->initEmptySearchList();
156        search($list, $conf['datadir'], [$this, 'searchCountCallback'], ['all' => false], '');
157        $data['page_count']    = $list['file_count'];
158        $data['page_size']     = $list['file_size'];
159        $data['page_biggest']  = $list['file_max'];
160        $data['page_smallest'] = $list['file_min'];
161        $data['page_nscount']  = $list['dir_count'];
162        $data['page_nsnest']   = $list['dir_nest'];
163        if ($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count'];
164        $data['page_oldest']   = $list['file_oldest'];
165        unset($list);
166
167        // number and size of media
168        $list = $this->initEmptySearchList();
169        search($list, $conf['mediadir'], [$this, 'searchCountCallback'], ['all' => true]);
170        $data['media_count']    = $list['file_count'];
171        $data['media_size']     = $list['file_size'];
172        $data['media_biggest']  = $list['file_max'];
173        $data['media_smallest'] = $list['file_min'];
174        $data['media_nscount']  = $list['dir_count'];
175        $data['media_nsnest']   = $list['dir_nest'];
176        if ($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count'];
177        unset($list);
178
179        // number and size of cache
180        $list = $this->initEmptySearchList();
181        search($list, $conf['cachedir'], [$this, 'searchCountCallback'], ['all' => true]);
182        $data['cache_count']    = $list['file_count'];
183        $data['cache_size']     = $list['file_size'];
184        $data['cache_biggest']  = $list['file_max'];
185        $data['cache_smallest'] = $list['file_min'];
186        if ($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count'];
187        unset($list);
188
189        // number and size of index
190        $list = $this->initEmptySearchList();
191        search($list, $conf['indexdir'], [$this, 'searchCountCallback'], ['all' => true]);
192        $data['index_count']    = $list['file_count'];
193        $data['index_size']     = $list['file_size'];
194        $data['index_biggest']  = $list['file_max'];
195        $data['index_smallest'] = $list['file_min'];
196        if ($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count'];
197        unset($list);
198
199        // number and size of meta
200        $list = $this->initEmptySearchList();
201        search($list, $conf['metadir'], [$this, 'searchCountCallback'], ['all' => true]);
202        $data['meta_count']    = $list['file_count'];
203        $data['meta_size']     = $list['file_size'];
204        $data['meta_biggest']  = $list['file_max'];
205        $data['meta_smallest'] = $list['file_min'];
206        if ($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count'];
207        unset($list);
208
209        // number and size of attic
210        $list = $this->initEmptySearchList();
211        search($list, $conf['olddir'], [$this, 'searchCountCallback'], ['all' => true]);
212        $data['attic_count']    = $list['file_count'];
213        $data['attic_size']     = $list['file_size'];
214        $data['attic_biggest']  = $list['file_max'];
215        $data['attic_smallest'] = $list['file_min'];
216        if ($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count'];
217        $data['attic_oldest']   = $list['file_oldest'];
218        unset($list);
219
220        // user count
221        if ($auth instanceof AuthPlugin && $auth->canDo('getUserCount')) {
222            $data['user_count'] = $auth->getUserCount();
223        }
224
225        // calculate edits per day
226        $list = (array) @file($conf['metadir'] . '/_dokuwiki.changes');
227        $count = count($list);
228        if ($count > 2) {
229            $first = (int) substr(array_shift($list), 0, 10);
230            $last  = (int) substr(array_pop($list), 0, 10);
231            $dur = ($last - $first) / (60 * 60 * 24); // number of days in the changelog
232            $data['edits_per_day'] = $count / $dur;
233        }
234        unset($list);
235
236        // plugins
237        $data['plugin'] = plugin_list();
238
239        // pcre info
240        if (defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION;
241        $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit');
242        $data['pcre_recursion'] = ini_get('pcre.recursion_limit');
243
244        // php info
245        $data['os'] = PHP_OS;
246        $data['webserver'] = $_SERVER['SERVER_SOFTWARE'];
247        $data['php_version'] = phpversion();
248        $data['php_sapi'] = PHP_SAPI;
249        $data['php_memory'] = php_to_byte(ini_get('memory_limit'));
250        $data['php_exectime'] = $phptime;
251        $data['php_extension'] = get_loaded_extensions();
252
253        // plugin usage data
254        $this->addPluginUsageData($data);
255
256        return $data;
257    }
258
259    /**
260     * Triggers event to let plugins add their own data
261     *
262     * @param $data
263     */
264    protected function addPluginUsageData(&$data)
265    {
266        $pluginsData = [];
267        Event::createAndTrigger('PLUGIN_POPULARITY_DATA_SETUP', $pluginsData);
268        foreach ($pluginsData as $plugin => $d) {
269            if (is_array($d)) {
270                foreach ($d as $key => $value) {
271                    $data['plugin_' . $plugin . '_' . $key] = $value;
272                }
273            } else {
274                $data['plugin_' . $plugin] = $d;
275            }
276        }
277    }
278
279    /**
280     * Callback to search and count the content of directories in DokuWiki
281     *
282     * @param array &$data  Reference to the result data structure
283     * @param string $base  Base usually $conf['datadir']
284     * @param string $file  current file or directory relative to $base
285     * @param string $type  Type either 'd' for directory or 'f' for file
286     * @param int    $lvl   Current recursion depht
287     * @param array  $opts  option array as given to search()
288     * @return bool
289     */
290    public function searchCountCallback(&$data, $base, $file, $type, $lvl, $opts)
291    {
292        // traverse
293        if ($type == 'd') {
294            if ($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
295            $data['dir_count']++;
296            return true;
297        }
298
299        //only search txt files if 'all' option not set
300        if ($opts['all'] || str_ends_with($file, '.txt')) {
301            $size = filesize($base . '/' . $file);
302            $date = filemtime($base . '/' . $file);
303            $data['file_count']++;
304            $data['file_size'] += $size;
305            if (!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size;
306            if ($data['file_max'] < $size) $data['file_max'] = $size;
307            if (!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date;
308        }
309
310        return false;
311    }
312}
313