xref: /plugin/statistics/admin.php (revision c4c84f989914854a8164dd20e63d673e898e858b)
1<?php
2
3// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
4use dokuwiki\Extension\AdminPlugin;
5use dokuwiki\plugin\statistics\SearchEngines;
6
7/**
8 * statistics plugin
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author     Andreas Gohr <gohr@splitbrain.org>
12 */
13class admin_plugin_statistics extends AdminPlugin
14{
15    /** @var string the currently selected page */
16    protected $opt = '';
17
18    /** @var string from date in YYYY-MM-DD */
19    protected $from = '';
20    /** @var string to date in YYYY-MM-DD */
21    protected $to = '';
22    /** @var int Offset to use when displaying paged data */
23    protected $start = 0;
24
25    /** @var helper_plugin_statistics */
26    protected $hlp;
27
28    /**
29     * Available statistic pages
30     */
31    protected $pages = [
32        'dashboard' => 1,
33        'content' => ['page', 'edits', 'images', 'downloads', 'history'],
34        'users' => ['topuser', 'topeditor', 'topgroup', 'topgroupedit', 'seenusers'],
35        'links' => ['referer', 'newreferer', 'outlinks'],
36        'search' => ['searchengines', 'searchphrases', 'searchwords', 'internalsearchphrases', 'internalsearchwords'],
37        'technology' => ['browsers', 'os', 'countries', 'resolution', 'viewport']
38    ];
39
40    /** @var array keeps a list of all real content pages, generated from above array */
41    protected $allowedpages = [];
42
43    /**
44     * Initialize the helper
45     */
46    public function __construct()
47    {
48        $this->hlp = plugin_load('helper', 'statistics');
49
50        // build a list of pages
51        foreach ($this->pages as $key => $val) {
52            if (is_array($val)) {
53                $this->allowedpages = array_merge($this->allowedpages, $val);
54            } else {
55                $this->allowedpages[] = $key;
56            }
57        }
58    }
59
60    /**
61     * Access for managers allowed
62     */
63    public function forAdminOnly()
64    {
65        return false;
66    }
67
68    /**
69     * return sort order for position in admin menu
70     */
71    public function getMenuSort()
72    {
73        return 350;
74    }
75
76    /**
77     * handle user request
78     */
79    public function handle()
80    {
81        global $INPUT;
82        $this->opt = preg_replace('/[^a-z]+/', '', $INPUT->str('opt'));
83        if (!in_array($this->opt, $this->allowedpages)) $this->opt = 'dashboard';
84
85        $this->start = $INPUT->int('s');
86        $this->setTimeframe($INPUT->str('f', date('Y-m-d')), $INPUT->str('t', date('Y-m-d')));
87    }
88
89    /**
90     * set limit clause
91     */
92    public function setTimeframe($from, $to)
93    {
94        // swap if wrong order
95        if ($from > $to) [$from, $to] = [$to, $from];
96
97        $this->hlp->getQuery()->setTimeFrame($from, $to);
98        $this->from = $from;
99        $this->to = $to;
100    }
101
102    /**
103     * Output the Statistics
104     */
105    public function html()
106    {
107        echo '<script src="' . DOKU_BASE . 'lib/plugins/statistics/lib/chart.js"></script>';
108        echo '<script src="' . DOKU_BASE . 'lib/plugins/statistics/lib/chartjs-plugin-datalabels.js"></script>';
109
110        echo '<div id="plugin__statistics">';
111        echo '<h1>' . $this->getLang('menu') . '</h1>';
112        $this->html_timeselect();
113        tpl_flush();
114
115        $method = 'html_' . $this->opt;
116        if (method_exists($this, $method)) {
117            echo '<div class="plg_stats_' . $this->opt . '">';
118            echo '<h2>' . $this->getLang($this->opt) . '</h2>';
119            $this->$method();
120            echo '</div>';
121        }
122        echo '</div>';
123    }
124
125    /**
126     * Return the TOC
127     *
128     * @return array
129     */
130    public function getTOC()
131    {
132        $toc = [];
133        foreach ($this->pages as $key => $info) {
134            if (is_array($info)) {
135                $toc[] = html_mktocitem(
136                    '',
137                    $this->getLang($key),
138                    1,
139                    ''
140                );
141
142                foreach ($info as $page) {
143                    $toc[] = html_mktocitem(
144                        '?do=admin&amp;page=statistics&amp;opt=' . $page .
145                        '&amp;f=' . $this->from .
146                        '&amp;t=' . $this->to,
147                        $this->getLang($page),
148                        2,
149                        ''
150                    );
151                }
152            } else {
153                $toc[] = html_mktocitem(
154                    '?do=admin&amp;page=statistics&amp;opt=' . $key .
155                    '&amp;f=' . $this->from .
156                    '&amp;t=' . $this->to,
157                    $this->getLang($key),
158                    1,
159                    ''
160                );
161            }
162        }
163        return $toc;
164    }
165
166    public function html_graph($name, $width, $height)
167    {
168        $this->hlp->getGraph($this->from, $this->to, $width, $height)->$name();
169    }
170
171    /**
172     * Outputs pagination links
173     *
174     * @param int $limit
175     * @param int $next
176     */
177    public function html_pager($limit, $next)
178    {
179        $params = [
180            'do' => 'admin',
181            'page' => 'statistics',
182            'opt' => $this->opt,
183            'f' => $this->from,
184            't' => $this->to,
185        ];
186
187        echo '<div class="plg_stats_pager">';
188        if ($this->start > 0) {
189            $go = max($this->start - $limit, 0);
190            $params['s'] = $go;
191            echo '<a href="?' . buildURLparams($params) . '" class="prev button">' . $this->getLang('prev') . '</a>';
192        }
193
194        if ($next) {
195            $go = $this->start + $limit;
196            $params['s'] = $go;
197            echo '<a href="?' . buildURLparams($params) . '" class="next button">' . $this->getLang('next') . '</a>';
198        }
199        echo '</div>';
200    }
201
202    /**
203     * Print the time selection menu
204     */
205    public function html_timeselect()
206    {
207        $quick = [
208            'today' => date('Y-m-d'),
209            'last1' => date('Y-m-d', time() - (60 * 60 * 24)),
210            'last7' => date('Y-m-d', time() - (60 * 60 * 24 * 7)),
211            'last30' => date('Y-m-d', time() - (60 * 60 * 24 * 30)),
212        ];
213
214
215        echo '<div class="plg_stats_timeselect">';
216        echo '<span>' . $this->getLang('time_select') . '</span> ';
217
218        echo '<form action="' . DOKU_SCRIPT . '" method="get">';
219        echo '<input type="hidden" name="do" value="admin" />';
220        echo '<input type="hidden" name="page" value="statistics" />';
221        echo '<input type="hidden" name="opt" value="' . $this->opt . '" />';
222        echo '<input type="date" name="f" value="' . $this->from . '" class="edit" />';
223        echo '<input type="date" name="t" value="' . $this->to . '" class="edit" />';
224        echo '<input type="submit" value="go" class="button" />';
225        echo '</form>';
226
227        echo '<ul>';
228        foreach ($quick as $name => $time) {
229            $url = buildURLparams([
230                'do' => 'admin',
231                'page' => 'statistics',
232                'opt' => $this->opt,
233                'f' => $time,
234                't' => $quick['today'],
235            ]);
236
237            echo '<li>';
238            echo '<a href="?' . $url . '">';
239            echo $this->getLang('time_' . $name);
240            echo '</a>';
241            echo '</li>';
242        }
243        echo '</ul>';
244
245        echo '</div>';
246    }
247
248    /**
249     * Print an introductionary screen
250     */
251    public function html_dashboard()
252    {
253        echo '<p>' . $this->getLang('intro_dashboard') . '</p>';
254
255        // general info
256        echo '<div class="plg_stats_top">';
257        $result = $this->hlp->getQuery()->aggregate();
258
259        echo '<ul>';
260        foreach (['pageviews', 'sessions', 'visitors', 'users', 'logins', 'current'] as $name) {
261            echo '<li><div class="li">' . sprintf($this->getLang('dash_' . $name), $result[$name]) . '</div></li>';
262        }
263        echo '</ul>';
264
265        echo '<ul>';
266        foreach (['bouncerate', 'timespent', 'avgpages', 'newvisitors', 'registrations'] as $name) {
267            echo '<li><div class="li">' . sprintf($this->getLang('dash_' . $name), $result[$name]) . '</div></li>';
268        }
269        echo '</ul>';
270
271        $this->html_graph('dashboardviews', 700, 280);
272        $this->html_graph('dashboardwiki', 700, 280);
273        echo '</div>';
274
275        $quickgraphs = [
276            ['lbl' => 'dash_mostpopular', 'query' => 'pages', 'opt' => 'page'],
277            ['lbl' => 'dash_newincoming', 'query' => 'newreferer', 'opt' => 'newreferer'],
278            ['lbl' => 'dash_topsearch', 'query' => 'searchphrases', 'opt' => 'internalsearchphrases'],
279        ];
280
281        foreach ($quickgraphs as $graph) {
282            $params = [
283                'do' => 'admin',
284                'page' => 'statistics',
285                'f' => $this->from,
286                't' => $this->to,
287                'opt' => $graph['opt'],
288            ];
289
290            echo '<div>';
291            echo '<h2>' . $this->getLang($graph['lbl']) . '</h2>';
292            $result = call_user_func([$this->hlp->getQuery(), $graph['query']]);
293            $this->html_resulttable($result);
294            echo '<p><a href="?' . buildURLparams($params) . '" class="more">' . $this->getLang('more') . '…</a></p>';
295            echo '</div>';
296        }
297    }
298
299    public function html_history()
300    {
301        echo '<p>' . $this->getLang('intro_history') . '</p>';
302        $this->html_graph('history_page_count', 600, 200);
303        $this->html_graph('history_page_size', 600, 200);
304        $this->html_graph('history_media_count', 600, 200);
305        $this->html_graph('history_media_size', 600, 200);
306    }
307
308    public function html_countries()
309    {
310        echo '<p>' . $this->getLang('intro_countries') . '</p>';
311        $this->html_graph('countries', 300, 300);
312        $result = $this->hlp->getQuery()->countries();
313        $this->html_resulttable($result, '', 150);
314    }
315
316    public function html_page()
317    {
318        echo '<p>' . $this->getLang('intro_page') . '</p>';
319        $result = $this->hlp->getQuery()->pages();
320        $this->html_resulttable($result, '', 150);
321    }
322
323    public function html_edits()
324    {
325        echo '<p>' . $this->getLang('intro_edits') . '</p>';
326        $result = $this->hlp->getQuery()->edits();
327        $this->html_resulttable($result, '', 150);
328    }
329
330    public function html_images()
331    {
332        echo '<p>' . $this->getLang('intro_images') . '</p>';
333
334        $result = $this->hlp->getQuery()->imagessum();
335        echo '<p>';
336        echo sprintf($this->getLang('trafficsum'), $result[0]['cnt'], filesize_h($result[0]['filesize']));
337        echo '</p>';
338
339        $result = $this->hlp->getQuery()->images();
340        $this->html_resulttable($result, '', 150);
341    }
342
343    public function html_downloads()
344    {
345        echo '<p>' . $this->getLang('intro_downloads') . '</p>';
346
347        $result = $this->hlp->getQuery()->downloadssum();
348        echo '<p>';
349        echo sprintf($this->getLang('trafficsum'), $result[0]['cnt'], filesize_h($result[0]['filesize']));
350        echo '</p>';
351
352        $result = $this->hlp->getQuery()->downloads();
353        $this->html_resulttable($result, '', 150);
354    }
355
356    public function html_browsers()
357    {
358        echo '<p>' . $this->getLang('intro_browsers') . '</p>';
359        $this->html_graph('browsers', 300, 300);
360        $result = $this->hlp->getQuery()->browsers(false);
361        $this->html_resulttable($result, '', 150);
362    }
363
364    public function html_topuser()
365    {
366        echo '<p>' . $this->getLang('intro_topuser') . '</p>';
367        $this->html_graph('topuser', 300, 300);
368        $result = $this->hlp->getQuery()->topuser();
369        $this->html_resulttable($result, '', 150);
370    }
371
372    public function html_topeditor()
373    {
374        echo '<p>' . $this->getLang('intro_topeditor') . '</p>';
375        $this->html_graph('topeditor', 300, 300);
376        $result = $this->hlp->getQuery()->topeditor();
377        $this->html_resulttable($result, '', 150);
378    }
379
380    public function html_topgroup()
381    {
382        echo '<p>' . $this->getLang('intro_topgroup') . '</p>';
383        $this->html_graph('topgroup', 300, 300);
384        $result = $this->hlp->getQuery()->topgroup();
385        $this->html_resulttable($result, '', 150);
386    }
387
388    public function html_topgroupedit()
389    {
390        echo '<p>' . $this->getLang('intro_topgroupedit') . '</p>';
391        $this->html_graph('topgroupedit', 300, 300);
392        $result = $this->hlp->getQuery()->topgroupedit();
393        $this->html_resulttable($result, '', 150);
394    }
395
396    public function html_os()
397    {
398        echo '<p>' . $this->getLang('intro_os') . '</p>';
399        $this->html_graph('os', 300, 300);
400        $result = $this->hlp->getQuery()->os();
401        $this->html_resulttable($result, '', 150);
402    }
403
404    public function html_referer()
405    {
406        $result = $this->hlp->getQuery()->aggregate();
407
408        $all = $result['search'] + $result['external'] + $result['direct'];
409
410        if ($all) {
411            printf(
412                '<p>' . $this->getLang('intro_referer') . '</p>',
413                $all,
414                $result['direct'],
415                (100 * $result['direct'] / $all),
416                $result['search'],
417                (100 * $result['search'] / $all),
418                $result['external'],
419                (100 * $result['external'] / $all)
420            );
421        }
422
423        $result = $this->hlp->getQuery()->referer();
424        $this->html_resulttable($result, '', 150);
425    }
426
427    public function html_newreferer()
428    {
429        echo '<p>' . $this->getLang('intro_newreferer') . '</p>';
430
431        $result = $this->hlp->getQuery()->newreferer();
432        $this->html_resulttable($result, '', 150);
433    }
434
435    public function html_outlinks()
436    {
437        echo '<p>' . $this->getLang('intro_outlinks') . '</p>';
438        $result = $this->hlp->getQuery()->outlinks();
439        $this->html_resulttable($result, '', 150);
440    }
441
442    public function html_searchphrases()
443    {
444        echo '<p>' . $this->getLang('intro_searchphrases') . '</p>';
445        $result = $this->hlp->getQuery()->searchphrases(true);
446        $this->html_resulttable($result, '', 150);
447    }
448
449    public function html_searchwords()
450    {
451        echo '<p>' . $this->getLang('intro_searchwords') . '</p>';
452        $result = $this->hlp->getQuery()->searchwords(true);
453        $this->html_resulttable($result, '', 150);
454    }
455
456    public function html_internalsearchphrases()
457    {
458        echo '<p>' . $this->getLang('intro_internalsearchphrases') . '</p>';
459        $result = $this->hlp->getQuery()->searchphrases(false);
460        $this->html_resulttable($result, '', 150);
461    }
462
463    public function html_internalsearchwords()
464    {
465        echo '<p>' . $this->getLang('intro_internalsearchwords') . '</p>';
466        $result = $this->hlp->getQuery()->searchwords(false);
467        $this->html_resulttable($result, '', 150);
468    }
469
470    public function html_searchengines()
471    {
472        echo '<p>' . $this->getLang('intro_searchengines') . '</p>';
473        $this->html_graph('searchengines', 400, 200);
474        $result = $this->hlp->getQuery()->searchengines();
475        $this->html_resulttable($result, '', 150);
476    }
477
478    public function html_resolution()
479    {
480        echo '<p>' . $this->getLang('intro_resolution') . '</p>';
481        $this->html_graph('resolution', 650, 490);
482        $result = $this->hlp->getQuery()->resolution();
483        $this->html_resulttable($result, '', 150);
484    }
485
486    public function html_viewport()
487    {
488        echo '<p>' . $this->getLang('intro_viewport') . '</p>';
489        $this->html_graph('viewport', 650, 490);
490        $result = $this->hlp->getQuery()->viewport();
491        $this->html_resulttable($result, '', 150);
492    }
493
494    public function html_seenusers()
495    {
496        echo '<p>' . $this->getLang('intro_seenusers') . '</p>';
497        $result = $this->hlp->getQuery()->seenusers();
498        $this->html_resulttable($result, '', 150);
499    }
500
501    /**
502     * Display a result in a HTML table
503     */
504    public function html_resulttable($result, $header = '', $pager = 0)
505    {
506        echo '<table class="inline">';
507        if (is_array($header)) {
508            echo '<tr>';
509            foreach ($header as $h) {
510                echo '<th>' . hsc($h) . '</th>';
511            }
512            echo '</tr>';
513        }
514
515        $count = 0;
516        if (is_array($result)) foreach ($result as $row) {
517            echo '<tr>';
518            foreach ($row as $k => $v) {
519                if ($k == 'res_x') continue;
520                if ($k == 'res_y') continue;
521
522                echo '<td class="plg_stats_X' . $k . '">';
523                if ($k == 'page') {
524                    echo '<a href="' . wl($v) . '" class="wikilink1">';
525                    echo hsc($v);
526                    echo '</a>';
527                } elseif ($k == 'media') {
528                    echo '<a href="' . ml($v) . '" class="wikilink1">';
529                    echo hsc($v);
530                    echo '</a>';
531                } elseif ($k == 'filesize') {
532                    echo filesize_h($v);
533                } elseif ($k == 'url') {
534                    $url = hsc($v);
535                    $url = preg_replace('/^https?:\/\/(www\.)?/', '', $url);
536                    if (strlen($url) > 45) {
537                        $url = substr($url, 0, 30) . ' &hellip; ' . substr($url, -15);
538                    }
539                    echo '<a href="' . $v . '" class="urlextern">';
540                    echo $url;
541                    echo '</a>';
542                } elseif ($k == 'ilookup') {
543                    echo '<a href="' . wl('', ['id' => $v, 'do' => 'search']) . '">Search</a>';
544                } elseif ($k == 'lookup') {
545                    echo '<a href="http://www.google.com/search?q=' . rawurlencode($v) . '">';
546                    echo '<img src="' . DOKU_BASE . 'lib/plugins/statistics/ico/search/google.png" alt="Google" />';
547                    echo '</a> ';
548
549                    echo '<a href="http://search.yahoo.com/search?p=' . rawurlencode($v) . '">';
550                    echo '<img src="' . DOKU_BASE . 'lib/plugins/statistics/ico/search/yahoo.png" alt="Yahoo!" />';
551                    echo '</a> ';
552
553                    echo '<a href="http://www.bing.com/search?q=' . rawurlencode($v) . '">';
554                    echo '<img src="' . DOKU_BASE . 'lib/plugins/statistics/ico/search/bing.png" alt="Bing" />';
555                    echo '</a> ';
556                } elseif ($k == 'engine') {
557                    $name = SearchEngines::getName($v);
558                    $url = SearchEngines::getURL($v);
559                    if ($url) {
560                        echo '<a href="' . $url . '">' . hsc($name) . '</a>';
561                    } else {
562                        echo hsc($name);
563                    }
564                } elseif ($k == 'html') {
565                    echo $v;
566                } else {
567                    echo hsc($v);
568                }
569                echo '</td>';
570            }
571            echo '</tr>';
572
573            if ($pager && ($count == $pager)) break;
574            $count++;
575        }
576        echo '</table>';
577
578        if ($pager) $this->html_pager($pager, count($result) > $pager);
579    }
580}
581