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