xref: /plugin/statistics/admin.php (revision b0cf211850e31e6c691318f638790d8b0ed0f3f0)
11878f16fSAndreas Gohr<?php
2a8acb244SAndreas Gohr
3a8acb244SAndreas Gohruse dokuwiki\Extension\AdminPlugin;
4a8acb244SAndreas Gohr
51878f16fSAndreas Gohr/**
61878f16fSAndreas Gohr * statistics plugin
71878f16fSAndreas Gohr *
81878f16fSAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
96b6f8822SAndreas Gohr * @author     Andreas Gohr <gohr@splitbrain.org>
101878f16fSAndreas Gohr */
11a8acb244SAndreas Gohrclass admin_plugin_statistics extends AdminPlugin
12a8acb244SAndreas Gohr{
1333a136e5SAndreas Gohr    /** @var string the currently selected page */
14a901d721SAndreas Gohr    protected $opt = '';
1533a136e5SAndreas Gohr
1633a136e5SAndreas Gohr    /** @var string from date in YYYY-MM-DD */
17a901d721SAndreas Gohr    protected $from = '';
1833a136e5SAndreas Gohr    /** @var string to date in YYYY-MM-DD */
19a901d721SAndreas Gohr    protected $to = '';
2033a136e5SAndreas Gohr    /** @var int Offset to use when displaying paged data */
2133a136e5SAndreas Gohr    protected $start = 0;
2233a136e5SAndreas Gohr
2333a136e5SAndreas Gohr    /** @var helper_plugin_statistics */
2433a136e5SAndreas Gohr    protected $hlp;
2533a136e5SAndreas Gohr
26a901d721SAndreas Gohr    /**
27a901d721SAndreas Gohr     * Available statistic pages
28a901d721SAndreas Gohr     */
29483101d3SAndreas Gohr    protected $pages = [
30483101d3SAndreas Gohr        'dashboard' => 1,
31483101d3SAndreas Gohr        'content' => ['page', 'edits', 'images', 'downloads', 'history'],
32483101d3SAndreas Gohr        'users' => ['topuser', 'topeditor', 'topgroup', 'topgroupedit', 'seenusers'],
33483101d3SAndreas Gohr        'links' => ['referer', 'newreferer', 'outlinks'],
34483101d3SAndreas Gohr        'search' => ['searchengines', 'searchphrases', 'searchwords', 'internalsearchphrases', 'internalsearchwords'],
35483101d3SAndreas Gohr        'technology' => ['browsers', 'os', 'countries', 'resolution', 'viewport']
36483101d3SAndreas Gohr    ];
371878f16fSAndreas Gohr
3881ff4c3aSAndreas Gohr    /** @var array keeps a list of all real content pages, generated from above array */
39a8acb244SAndreas Gohr    protected $allowedpages = [];
4081ff4c3aSAndreas Gohr
411878f16fSAndreas Gohr    /**
426b6f8822SAndreas Gohr     * Initialize the helper
436b6f8822SAndreas Gohr     */
44a8acb244SAndreas Gohr    public function __construct()
45a8acb244SAndreas Gohr    {
466b6f8822SAndreas Gohr        $this->hlp = plugin_load('helper', 'statistics');
4781ff4c3aSAndreas Gohr
4881ff4c3aSAndreas Gohr        // build a list of pages
4981ff4c3aSAndreas Gohr        foreach ($this->pages as $key => $val) {
5081ff4c3aSAndreas Gohr            if (is_array($val)) {
5181ff4c3aSAndreas Gohr                $this->allowedpages = array_merge($this->allowedpages, $val);
5281ff4c3aSAndreas Gohr            } else {
5381ff4c3aSAndreas Gohr                $this->allowedpages[] = $key;
5481ff4c3aSAndreas Gohr            }
5581ff4c3aSAndreas Gohr        }
566b6f8822SAndreas Gohr    }
576b6f8822SAndreas Gohr
586b6f8822SAndreas Gohr    /**
591878f16fSAndreas Gohr     * Access for managers allowed
601878f16fSAndreas Gohr     */
61a8acb244SAndreas Gohr    public function forAdminOnly()
62a8acb244SAndreas Gohr    {
631878f16fSAndreas Gohr        return false;
641878f16fSAndreas Gohr    }
651878f16fSAndreas Gohr
661878f16fSAndreas Gohr    /**
671878f16fSAndreas Gohr     * return sort order for position in admin menu
681878f16fSAndreas Gohr     */
69a8acb244SAndreas Gohr    public function getMenuSort()
70a8acb244SAndreas Gohr    {
716b6f8822SAndreas Gohr        return 350;
721878f16fSAndreas Gohr    }
731878f16fSAndreas Gohr
741878f16fSAndreas Gohr    /**
751878f16fSAndreas Gohr     * handle user request
761878f16fSAndreas Gohr     */
77a8acb244SAndreas Gohr    public function handle()
78a8acb244SAndreas Gohr    {
79483101d3SAndreas Gohr        global $INPUT;
80483101d3SAndreas Gohr        $this->opt = preg_replace('/[^a-z]+/', '', $INPUT->str('opt'));
8181ff4c3aSAndreas Gohr        if (!in_array($this->opt, $this->allowedpages)) $this->opt = 'dashboard';
82a901d721SAndreas Gohr
83483101d3SAndreas Gohr        $this->start = $INPUT->int('s');
84483101d3SAndreas Gohr        $this->setTimeframe($INPUT->str('f', date('Y-m-d')), $INPUT->str('t', date('Y-m-d')));
85e8699bceSAndreas Gohr    }
8695eb68e6SAndreas Gohr
87e8699bceSAndreas Gohr    /**
88e8699bceSAndreas Gohr     * set limit clause
89e8699bceSAndreas Gohr     */
90a8acb244SAndreas Gohr    public function setTimeframe($from, $to)
91a8acb244SAndreas Gohr    {
92047fcb0fSAndreas Gohr        // swap if wrong order
93a8acb244SAndreas Gohr        if ($from > $to) [$from, $to] = [$to, $from];
94047fcb0fSAndreas Gohr
957428e816SAndreas Gohr        $this->hlp->Query()->setTimeFrame($from, $to);
96e8699bceSAndreas Gohr        $this->from = $from;
97e8699bceSAndreas Gohr        $this->to = $to;
981878f16fSAndreas Gohr    }
991878f16fSAndreas Gohr
1001878f16fSAndreas Gohr    /**
10179b4a855SAndreas Gohr     * Output the Statistics
1021878f16fSAndreas Gohr     */
103a8acb244SAndreas Gohr    public function html()
104a8acb244SAndreas Gohr    {
105*b0cf2118SAnna Dabrowska        echo '<script src="' . DOKU_BASE . 'lib/plugins/statistics/lib/chart.js"></script>';
106*b0cf2118SAnna Dabrowska        echo '<script src="' . DOKU_BASE . 'lib/plugins/statistics/lib/chartjs-plugin-datalabels.js"></script>';
107*b0cf2118SAnna Dabrowska
1081d2d78ccSAndreas Gohr        echo '<div id="plugin__statistics">';
1090c3b1e44SAndreas Gohr        echo '<h1>' . $this->getLang('menu') . '</h1>';
110264f1744SAndreas Gohr        $this->html_timeselect();
111441bfb8eSAndreas Gohr        tpl_flush();
112264f1744SAndreas Gohr
11379b4a855SAndreas Gohr        $method = 'html_' . $this->opt;
11479b4a855SAndreas Gohr        if (method_exists($this, $method)) {
115a901d721SAndreas Gohr            echo '<div class="plg_stats_' . $this->opt . '">';
116a901d721SAndreas Gohr            echo '<h2>' . $this->getLang($this->opt) . '</h2>';
11779b4a855SAndreas Gohr            $this->$method();
118a901d721SAndreas Gohr            echo '</div>';
11914d99ec0SAndreas Gohr        }
1201d2d78ccSAndreas Gohr        echo '</div>';
12114d99ec0SAndreas Gohr    }
12214d99ec0SAndreas Gohr
1236b6f8822SAndreas Gohr    /**
1246b6f8822SAndreas Gohr     * Return the TOC
1256b6f8822SAndreas Gohr     *
1266b6f8822SAndreas Gohr     * @return array
1276b6f8822SAndreas Gohr     */
128a8acb244SAndreas Gohr    public function getTOC()
129a8acb244SAndreas Gohr    {
130a8acb244SAndreas Gohr        $toc = [];
13181ff4c3aSAndreas Gohr        foreach ($this->pages as $key => $info) {
13281ff4c3aSAndreas Gohr            if (is_array($info)) {
13381ff4c3aSAndreas Gohr                $toc[] = html_mktocitem(
13481ff4c3aSAndreas Gohr                    '',
13581ff4c3aSAndreas Gohr                    $this->getLang($key),
13681ff4c3aSAndreas Gohr                    1,
13781ff4c3aSAndreas Gohr                    ''
13847ffcf7dSAndreas Gohr                );
13981ff4c3aSAndreas Gohr
14081ff4c3aSAndreas Gohr                foreach ($info as $page) {
14181ff4c3aSAndreas Gohr                    $toc[] = html_mktocitem(
14281ff4c3aSAndreas Gohr                        '?do=admin&amp;page=statistics&amp;opt=' . $page . '&amp;f=' . $this->from . '&amp;t=' . $this->to,
14381ff4c3aSAndreas Gohr                        $this->getLang($page),
14481ff4c3aSAndreas Gohr                        2,
14581ff4c3aSAndreas Gohr                        ''
14681ff4c3aSAndreas Gohr                    );
14781ff4c3aSAndreas Gohr                }
14881ff4c3aSAndreas Gohr            } else {
14981ff4c3aSAndreas Gohr                $toc[] = html_mktocitem(
15081ff4c3aSAndreas Gohr                    '?do=admin&amp;page=statistics&amp;opt=' . $key . '&amp;f=' . $this->from . '&amp;t=' . $this->to,
15181ff4c3aSAndreas Gohr                    $this->getLang($key),
15281ff4c3aSAndreas Gohr                    1,
15381ff4c3aSAndreas Gohr                    ''
15481ff4c3aSAndreas Gohr                );
15581ff4c3aSAndreas Gohr            }
15647ffcf7dSAndreas Gohr        }
15747ffcf7dSAndreas Gohr        return $toc;
1589da6395dSAndreas Gohr    }
1599da6395dSAndreas Gohr
160a8acb244SAndreas Gohr    public function html_graph($name, $width, $height)
161a8acb244SAndreas Gohr    {
162f0a4cceeSAnna Dabrowska        $this->hlp->Graph($this->from, $this->to, $width, $height)->$name();
163dc7b1e5eSAndreas Gohr    }
164dc7b1e5eSAndreas Gohr
1656b6f8822SAndreas Gohr    /**
1666b6f8822SAndreas Gohr     * Outputs pagination links
1676b6f8822SAndreas Gohr     *
16833a136e5SAndreas Gohr     * @param int $limit
16933a136e5SAndreas Gohr     * @param int $next
1706b6f8822SAndreas Gohr     */
171a8acb244SAndreas Gohr    public function html_pager($limit, $next)
172a8acb244SAndreas Gohr    {
1732507f8e0SAndreas Gohr        echo '<div class="plg_stats_pager">';
1742507f8e0SAndreas Gohr
1752507f8e0SAndreas Gohr        if ($this->start > 0) {
1762507f8e0SAndreas Gohr            $go = max($this->start - $limit, 0);
177d43cd6e0SAndreas Gohr            echo '<a href="?do=admin&amp;page=statistics&amp;opt=' . $this->opt . '&amp;f=' . $this->from . '&amp;t=' . $this->to . '&amp;s=' . $go . '" class="prev button">' . $this->getLang('prev') . '</a>';
1782507f8e0SAndreas Gohr        }
1792507f8e0SAndreas Gohr
1802507f8e0SAndreas Gohr        if ($next) {
1812507f8e0SAndreas Gohr            $go = $this->start + $limit;
182d43cd6e0SAndreas Gohr            echo '<a href="?do=admin&amp;page=statistics&amp;opt=' . $this->opt . '&amp;f=' . $this->from . '&amp;t=' . $this->to . '&amp;s=' . $go . '" class="next button">' . $this->getLang('next') . '</a>';
1832507f8e0SAndreas Gohr        }
1842507f8e0SAndreas Gohr        echo '</div>';
1852507f8e0SAndreas Gohr    }
1862507f8e0SAndreas Gohr
187264f1744SAndreas Gohr    /**
188264f1744SAndreas Gohr     * Print the time selection menu
189264f1744SAndreas Gohr     */
190a8acb244SAndreas Gohr    public function html_timeselect()
191a8acb244SAndreas Gohr    {
192483101d3SAndreas Gohr        $quick = [
193483101d3SAndreas Gohr            'today' => date('Y-m-d'),
194483101d3SAndreas Gohr            'last1' => date('Y-m-d', time() - (60 * 60 * 24)),
195483101d3SAndreas Gohr            'last7' => date('Y-m-d', time() - (60 * 60 * 24 * 7)),
196483101d3SAndreas Gohr            'last30' => date('Y-m-d', time() - (60 * 60 * 24 * 30)),
197483101d3SAndreas Gohr        ];
198483101d3SAndreas Gohr
19914d99ec0SAndreas Gohr
200264f1744SAndreas Gohr        echo '<div class="plg_stats_timeselect">';
2016985b606SAndreas Gohr        echo '<span>' . $this->getLang('time_select') . '</span> ';
202264f1744SAndreas Gohr
203047fcb0fSAndreas Gohr        echo '<form action="' . DOKU_SCRIPT . '" method="get">';
204264f1744SAndreas Gohr        echo '<input type="hidden" name="do" value="admin" />';
205264f1744SAndreas Gohr        echo '<input type="hidden" name="page" value="statistics" />';
206264f1744SAndreas Gohr        echo '<input type="hidden" name="opt" value="' . $this->opt . '" />';
207483101d3SAndreas Gohr        echo '<input type="date" name="f" value="' . $this->from . '" class="edit" />';
208483101d3SAndreas Gohr        echo '<input type="date" name="t" value="' . $this->to . '" class="edit" />';
209264f1744SAndreas Gohr        echo '<input type="submit" value="go" class="button" />';
21014d99ec0SAndreas Gohr        echo '</form>';
211264f1744SAndreas Gohr
2126985b606SAndreas Gohr        echo '<ul>';
213483101d3SAndreas Gohr        foreach ($quick as $name => $time) {
2146985b606SAndreas Gohr            echo '<li>';
215483101d3SAndreas Gohr            echo '<a href="?do=admin&amp;page=statistics&amp;opt=' . $this->opt . '&amp;f=' . $time . '&amp;t=' . $quick['today'] . '">';
216483101d3SAndreas Gohr            echo $this->getLang('time_' . $name);
2176985b606SAndreas Gohr            echo '</a>';
2186985b606SAndreas Gohr            echo '</li>';
2196985b606SAndreas Gohr        }
2206985b606SAndreas Gohr        echo '</ul>';
2216985b606SAndreas Gohr
222264f1744SAndreas Gohr        echo '</div>';
22314d99ec0SAndreas Gohr    }
22414d99ec0SAndreas Gohr
225f5f32cbfSAndreas Gohr    /**
226f5f32cbfSAndreas Gohr     * Print an introductionary screen
227f5f32cbfSAndreas Gohr     */
228a8acb244SAndreas Gohr    public function html_dashboard()
229a8acb244SAndreas Gohr    {
230878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_dashboard') . '</p>';
2312812a751SAndreas Gohr
2322812a751SAndreas Gohr        // general info
2332812a751SAndreas Gohr        echo '<div class="plg_stats_top">';
2347428e816SAndreas Gohr        $result = $this->hlp->Query()->aggregate();
2351d2d78ccSAndreas Gohr
2361d2d78ccSAndreas Gohr        echo '<ul class="left">';
237a8acb244SAndreas Gohr        foreach (['pageviews', 'sessions', 'visitors', 'users', 'logins', 'current'] as $name) {
238eabe0d07SAndreas Gohr            echo '<li><div class="li">' . sprintf($this->getLang('dash_' . $name), $result[$name]) . '</div></li>';
239eabe0d07SAndreas Gohr        }
2402812a751SAndreas Gohr        echo '</ul>';
2411d2d78ccSAndreas Gohr
2421d2d78ccSAndreas Gohr        echo '<ul class="left">';
243a8acb244SAndreas Gohr        foreach (['bouncerate', 'timespent', 'avgpages', 'newvisitors', 'registrations'] as $name) {
2441d2d78ccSAndreas Gohr            echo '<li><div class="li">' . sprintf($this->getLang('dash_' . $name), $result[$name]) . '</div></li>';
2451d2d78ccSAndreas Gohr        }
2461d2d78ccSAndreas Gohr        echo '</ul>';
2471d2d78ccSAndreas Gohr
24856f647ddSAndreas Gohr        echo '<br style="clear: left" />';
24956f647ddSAndreas Gohr
250259897e1SAndreas Gohr        $this->html_graph('dashboardviews', 700, 280);
251259897e1SAndreas Gohr        $this->html_graph('dashboardwiki', 700, 280);
2522812a751SAndreas Gohr        echo '</div>';
2532812a751SAndreas Gohr
25487d5e44bSAndreas Gohr        // top pages today
255264f1744SAndreas Gohr        echo '<div>';
256dc7b1e5eSAndreas Gohr        echo '<h2>' . $this->getLang('dash_mostpopular') . '</h2>';
2577428e816SAndreas Gohr        $result = $this->hlp->Query()->pages($this->start, 15);
2582812a751SAndreas Gohr        $this->html_resulttable($result);
259d43cd6e0SAndreas Gohr        echo '<a href="?do=admin&amp;page=statistics&amp;opt=page&amp;f=' . $this->from . '&amp;t=' . $this->to . '" class="more button">' . $this->getLang('more') . '</a>';
260264f1744SAndreas Gohr        echo '</div>';
26187d5e44bSAndreas Gohr
26287d5e44bSAndreas Gohr        // top referer today
263264f1744SAndreas Gohr        echo '<div>';
264dc7b1e5eSAndreas Gohr        echo '<h2>' . $this->getLang('dash_newincoming') . '</h2>';
2657428e816SAndreas Gohr        $result = $this->hlp->Query()->newreferer($this->start, 15);
2662812a751SAndreas Gohr        $this->html_resulttable($result);
267d43cd6e0SAndreas Gohr        echo '<a href="?do=admin&amp;page=statistics&amp;opt=newreferer&amp;f=' . $this->from . '&amp;t=' . $this->to . '" class="more button">' . $this->getLang('more') . '</a>';
268264f1744SAndreas Gohr        echo '</div>';
26954f6c432SAndreas Gohr
27029dea504SAndreas Gohr        // top searches today
271264f1744SAndreas Gohr        echo '<div>';
272dc7b1e5eSAndreas Gohr        echo '<h2>' . $this->getLang('dash_topsearch') . '</h2>';
2737428e816SAndreas Gohr        $result = $this->hlp->Query()->searchphrases(true, $this->start, 15);
27429dea504SAndreas Gohr        $this->html_resulttable($result);
275d43cd6e0SAndreas Gohr        echo '<a href="?do=admin&amp;page=statistics&amp;opt=searchphrases&amp;f=' . $this->from . '&amp;t=' . $this->to . '" class="more button">' . $this->getLang('more') . '</a>';
276264f1744SAndreas Gohr        echo '</div>';
27714d99ec0SAndreas Gohr    }
27814d99ec0SAndreas Gohr
279a8acb244SAndreas Gohr    public function html_history()
280a8acb244SAndreas Gohr    {
281cae4a1c5SAndreas Gohr        echo '<p>' . $this->getLang('intro_history') . '</p>';
282338987f5SAndreas Gohr        $this->html_graph('history_page_count', 600, 200);
283338987f5SAndreas Gohr        $this->html_graph('history_page_size', 600, 200);
284338987f5SAndreas Gohr        $this->html_graph('history_media_count', 600, 200);
285338987f5SAndreas Gohr        $this->html_graph('history_media_size', 600, 200);
286cae4a1c5SAndreas Gohr    }
287cae4a1c5SAndreas Gohr
288a8acb244SAndreas Gohr    public function html_countries()
289a8acb244SAndreas Gohr    {
290878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_countries') . '</p>';
2911d379254SAnna Dabrowska        $this->html_graph('countries', 300, 300);
292f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->countries();
2932507f8e0SAndreas Gohr        $this->html_resulttable($result, '', 150);
2949da6395dSAndreas Gohr    }
2959da6395dSAndreas Gohr
296a8acb244SAndreas Gohr    public function html_page()
297a8acb244SAndreas Gohr    {
298878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_page') . '</p>';
299f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->pages();
3002507f8e0SAndreas Gohr        $this->html_resulttable($result, '', 150);
3019da6395dSAndreas Gohr    }
3029da6395dSAndreas Gohr
303a8acb244SAndreas Gohr    public function html_edits()
304a8acb244SAndreas Gohr    {
3051664ba1dSAndreas Gohr        echo '<p>' . $this->getLang('intro_edits') . '</p>';
306f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->edits();
3071664ba1dSAndreas Gohr        $this->html_resulttable($result, '', 150);
3081664ba1dSAndreas Gohr    }
3091664ba1dSAndreas Gohr
310a8acb244SAndreas Gohr    public function html_images()
311a8acb244SAndreas Gohr    {
3121664ba1dSAndreas Gohr        echo '<p>' . $this->getLang('intro_images') . '</p>';
313616c1e8bSAndreas Gohr
3147428e816SAndreas Gohr        $result = $this->hlp->Query()->imagessum();
315616c1e8bSAndreas Gohr        echo '<p>';
316616c1e8bSAndreas Gohr        echo sprintf($this->getLang('trafficsum'), $result[0]['cnt'], filesize_h($result[0]['filesize']));
317616c1e8bSAndreas Gohr        echo '</p>';
318616c1e8bSAndreas Gohr
319f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->images();
3201664ba1dSAndreas Gohr        $this->html_resulttable($result, '', 150);
3211664ba1dSAndreas Gohr    }
3221664ba1dSAndreas Gohr
323a8acb244SAndreas Gohr    public function html_downloads()
324a8acb244SAndreas Gohr    {
3251664ba1dSAndreas Gohr        echo '<p>' . $this->getLang('intro_downloads') . '</p>';
326616c1e8bSAndreas Gohr
3277428e816SAndreas Gohr        $result = $this->hlp->Query()->downloadssum();
328616c1e8bSAndreas Gohr        echo '<p>';
329616c1e8bSAndreas Gohr        echo sprintf($this->getLang('trafficsum'), $result[0]['cnt'], filesize_h($result[0]['filesize']));
330616c1e8bSAndreas Gohr        echo '</p>';
331616c1e8bSAndreas Gohr
332f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->downloads();
3331664ba1dSAndreas Gohr        $this->html_resulttable($result, '', 150);
3341664ba1dSAndreas Gohr    }
3351664ba1dSAndreas Gohr
336a8acb244SAndreas Gohr    public function html_browsers()
337a8acb244SAndreas Gohr    {
338878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_browsers') . '</p>';
3391d379254SAnna Dabrowska        $this->html_graph('browsers', 300, 300);
340f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->browsers(false);
3412507f8e0SAndreas Gohr        $this->html_resulttable($result, '', 150);
34275fa767dSAndreas Gohr    }
34375fa767dSAndreas Gohr
344a8acb244SAndreas Gohr    public function html_topuser()
345a8acb244SAndreas Gohr    {
34681ff4c3aSAndreas Gohr        echo '<p>' . $this->getLang('intro_topuser') . '</p>';
3471d379254SAnna Dabrowska        $this->html_graph('topuser', 300, 300);
348f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->topuser();
34981ff4c3aSAndreas Gohr        $this->html_resulttable($result, '', 150);
35081ff4c3aSAndreas Gohr    }
35181ff4c3aSAndreas Gohr
352a8acb244SAndreas Gohr    public function html_topeditor()
353a8acb244SAndreas Gohr    {
35481ff4c3aSAndreas Gohr        echo '<p>' . $this->getLang('intro_topeditor') . '</p>';
3551d379254SAnna Dabrowska        $this->html_graph('topeditor', 300, 300);
356f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->topeditor();
35781ff4c3aSAndreas Gohr        $this->html_resulttable($result, '', 150);
35881ff4c3aSAndreas Gohr    }
35981ff4c3aSAndreas Gohr
360a8acb244SAndreas Gohr    public function html_topgroup()
361a8acb244SAndreas Gohr    {
36281ff4c3aSAndreas Gohr        echo '<p>' . $this->getLang('intro_topgroup') . '</p>';
3631d379254SAnna Dabrowska        $this->html_graph('topgroup', 300, 300);
364f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->topgroup();
36581ff4c3aSAndreas Gohr        $this->html_resulttable($result, '', 150);
36681ff4c3aSAndreas Gohr    }
36781ff4c3aSAndreas Gohr
368a8acb244SAndreas Gohr    public function html_topgroupedit()
369a8acb244SAndreas Gohr    {
37081ff4c3aSAndreas Gohr        echo '<p>' . $this->getLang('intro_topgroupedit') . '</p>';
3711d379254SAnna Dabrowska        $this->html_graph('topgroupedit', 300, 300);
372f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->topgroupedit();
37381ff4c3aSAndreas Gohr        $this->html_resulttable($result, '', 150);
37481ff4c3aSAndreas Gohr    }
37581ff4c3aSAndreas Gohr
376a8acb244SAndreas Gohr    public function html_os()
377a8acb244SAndreas Gohr    {
378878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_os') . '</p>';
3791d379254SAnna Dabrowska        $this->html_graph('os', 300, 300);
380f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->os();
3812507f8e0SAndreas Gohr        $this->html_resulttable($result, '', 150);
382bd4217d3SAndreas Gohr    }
383bd4217d3SAndreas Gohr
384a8acb244SAndreas Gohr    public function html_referer()
385a8acb244SAndreas Gohr    {
3867428e816SAndreas Gohr        $result = $this->hlp->Query()->aggregate();
3872812a751SAndreas Gohr
3882812a751SAndreas Gohr        $all = $result['search'] + $result['external'] + $result['direct'];
3892812a751SAndreas Gohr
39094023548SAndreas Gohr        if ($all) {
3910863c19cSAndreas Gohr            printf(
3920863c19cSAndreas Gohr                '<p>' . $this->getLang('intro_referer') . '</p>',
393a8acb244SAndreas Gohr                $all,
394a8acb244SAndreas Gohr                $result['direct'],
395a8acb244SAndreas Gohr                (100 * $result['direct'] / $all),
396a8acb244SAndreas Gohr                $result['search'],
397a8acb244SAndreas Gohr                (100 * $result['search'] / $all),
398a8acb244SAndreas Gohr                $result['external'],
3990863c19cSAndreas Gohr                (100 * $result['external'] / $all)
4000863c19cSAndreas Gohr            );
40194023548SAndreas Gohr        }
4022812a751SAndreas Gohr
403f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->referer();
4042507f8e0SAndreas Gohr        $this->html_resulttable($result, '', 150);
4059da6395dSAndreas Gohr    }
4069da6395dSAndreas Gohr
407a8acb244SAndreas Gohr    public function html_newreferer()
408a8acb244SAndreas Gohr    {
409878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_newreferer') . '</p>';
410e7a2f1e0SAndreas Gohr
411f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->newreferer();
4122507f8e0SAndreas Gohr        $this->html_resulttable($result, '', 150);
413e7a2f1e0SAndreas Gohr    }
414e7a2f1e0SAndreas Gohr
415a8acb244SAndreas Gohr    public function html_outlinks()
416a8acb244SAndreas Gohr    {
417878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_outlinks') . '</p>';
418f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->outlinks();
419e25286daSAndreas Gohr        $this->html_resulttable($result, '', 150);
420e25286daSAndreas Gohr    }
421e25286daSAndreas Gohr
422a8acb244SAndreas Gohr    public function html_searchphrases()
423a8acb244SAndreas Gohr    {
424878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_searchphrases') . '</p>';
425f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->searchphrases(true);
42612dcdeccSAndreas Gohr        $this->html_resulttable($result, '', 150);
42712dcdeccSAndreas Gohr    }
42812dcdeccSAndreas Gohr
429a8acb244SAndreas Gohr    public function html_searchwords()
430a8acb244SAndreas Gohr    {
431878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_searchwords') . '</p>';
432f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->searchwords(true);
4335bccfe87SAndreas Gohr        $this->html_resulttable($result, '', 150);
4345bccfe87SAndreas Gohr    }
4355bccfe87SAndreas Gohr
436a8acb244SAndreas Gohr    public function html_internalsearchphrases()
437a8acb244SAndreas Gohr    {
438878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_internalsearchphrases') . '</p>';
439f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->searchphrases(false);
4405bccfe87SAndreas Gohr        $this->html_resulttable($result, '', 150);
4415bccfe87SAndreas Gohr    }
4425bccfe87SAndreas Gohr
443a8acb244SAndreas Gohr    public function html_internalsearchwords()
444a8acb244SAndreas Gohr    {
445878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_internalsearchwords') . '</p>';
446f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->searchwords(false);
44712dcdeccSAndreas Gohr        $this->html_resulttable($result, '', 150);
44812dcdeccSAndreas Gohr    }
44912dcdeccSAndreas Gohr
450a8acb244SAndreas Gohr    public function html_searchengines()
451a8acb244SAndreas Gohr    {
452878be5c9SAndreas Gohr        echo '<p>' . $this->getLang('intro_searchengines') . '</p>';
45325b71d4bSAndreas Gohr        $this->html_graph('searchengines', 400, 200);
454f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->searchengines();
45512dcdeccSAndreas Gohr        $this->html_resulttable($result, '', 150);
45612dcdeccSAndreas Gohr    }
45712dcdeccSAndreas Gohr
458a8acb244SAndreas Gohr    public function html_resolution()
459a8acb244SAndreas Gohr    {
46025446aa2SAndreas Gohr        echo '<p>' . $this->getLang('intro_resolution') . '</p>';
46125446aa2SAndreas Gohr        $this->html_graph('resolution', 650, 490);
462f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->resolution();
463307baf3fSAndreas Gohr        $this->html_resulttable($result, '', 150);
46425446aa2SAndreas Gohr    }
465307baf3fSAndreas Gohr
466a8acb244SAndreas Gohr    public function html_viewport()
467a8acb244SAndreas Gohr    {
46825446aa2SAndreas Gohr        echo '<p>' . $this->getLang('intro_viewport') . '</p>';
46925446aa2SAndreas Gohr        $this->html_graph('viewport', 650, 490);
470f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->viewport();
47125446aa2SAndreas Gohr        $this->html_resulttable($result, '', 150);
472c73e16f1SAndreas Gohr    }
4739da6395dSAndreas Gohr
474a8acb244SAndreas Gohr    public function html_seenusers()
475a8acb244SAndreas Gohr    {
47633a136e5SAndreas Gohr        echo '<p>' . $this->getLang('intro_seenusers') . '</p>';
477f0a4cceeSAnna Dabrowska        $result = $this->hlp->Query()->seenusers();
47833a136e5SAndreas Gohr        $this->html_resulttable($result, '', 150);
47933a136e5SAndreas Gohr    }
48033a136e5SAndreas Gohr
48114d99ec0SAndreas Gohr    /**
48214d99ec0SAndreas Gohr     * Display a result in a HTML table
48314d99ec0SAndreas Gohr     */
484a8acb244SAndreas Gohr    public function html_resulttable($result, $header = '', $pager = 0)
485a8acb244SAndreas Gohr    {
48656f647ddSAndreas Gohr        echo '<table class="inline">';
4872812a751SAndreas Gohr        if (is_array($header)) {
48814d99ec0SAndreas Gohr            echo '<tr>';
48914d99ec0SAndreas Gohr            foreach ($header as $h) {
49014d99ec0SAndreas Gohr                echo '<th>' . hsc($h) . '</th>';
49114d99ec0SAndreas Gohr            }
49214d99ec0SAndreas Gohr            echo '</tr>';
4932812a751SAndreas Gohr        }
49414d99ec0SAndreas Gohr
4952507f8e0SAndreas Gohr        $count = 0;
4962ee939eeSAndreas Gohr        if (is_array($result)) foreach ($result as $row) {
49714d99ec0SAndreas Gohr            echo '<tr>';
49814d99ec0SAndreas Gohr            foreach ($row as $k => $v) {
499f3818071SAndreas Gohr                if ($k == 'res_x') continue;
500f3818071SAndreas Gohr                if ($k == 'res_y') continue;
501f3818071SAndreas Gohr
5022812a751SAndreas Gohr                echo '<td class="plg_stats_X' . $k . '">';
50314d99ec0SAndreas Gohr                if ($k == 'page') {
50414d99ec0SAndreas Gohr                    echo '<a href="' . wl($v) . '" class="wikilink1">';
50514d99ec0SAndreas Gohr                    echo hsc($v);
50614d99ec0SAndreas Gohr                    echo '</a>';
5071664ba1dSAndreas Gohr                } elseif ($k == 'media') {
5081664ba1dSAndreas Gohr                    echo '<a href="' . ml($v) . '" class="wikilink1">';
5091664ba1dSAndreas Gohr                    echo hsc($v);
5101664ba1dSAndreas Gohr                    echo '</a>';
5111664ba1dSAndreas Gohr                } elseif ($k == 'filesize') {
5121664ba1dSAndreas Gohr                    echo filesize_h($v);
51314d99ec0SAndreas Gohr                } elseif ($k == 'url') {
51454f6c432SAndreas Gohr                    $url = hsc($v);
51583b63546SAndreas Gohr                    $url = preg_replace('/^https?:\/\/(www\.)?/', '', $url);
5162812a751SAndreas Gohr                    if (strlen($url) > 45) {
5172812a751SAndreas Gohr                        $url = substr($url, 0, 30) . ' &hellip; ' . substr($url, -15);
51854f6c432SAndreas Gohr                    }
51914d99ec0SAndreas Gohr                    echo '<a href="' . $v . '" class="urlextern">';
52054f6c432SAndreas Gohr                    echo $url;
52114d99ec0SAndreas Gohr                    echo '</a>';
5225bccfe87SAndreas Gohr                } elseif ($k == 'ilookup') {
523a8acb244SAndreas Gohr                    echo '<a href="' . wl('', ['id' => $v, 'do' => 'search']) . '">Search</a>';
52429dea504SAndreas Gohr                } elseif ($k == 'lookup') {
52529dea504SAndreas Gohr                    echo '<a href="http://www.google.com/search?q=' . rawurlencode($v) . '">';
52613a86c14SAndreas Gohr                    echo '<img src="' . DOKU_BASE . 'lib/plugins/statistics/ico/search/google.png" alt="Google" border="0" />';
52729dea504SAndreas Gohr                    echo '</a> ';
52829dea504SAndreas Gohr
52929dea504SAndreas Gohr                    echo '<a href="http://search.yahoo.com/search?p=' . rawurlencode($v) . '">';
53013a86c14SAndreas Gohr                    echo '<img src="' . DOKU_BASE . 'lib/plugins/statistics/ico/search/yahoo.png" alt="Yahoo!" border="0" />';
53129dea504SAndreas Gohr                    echo '</a> ';
53229dea504SAndreas Gohr
53313a86c14SAndreas Gohr                    echo '<a href="http://www.bing.com/search?q=' . rawurlencode($v) . '">';
53413a86c14SAndreas Gohr                    echo '<img src="' . DOKU_BASE . 'lib/plugins/statistics/ico/search/bing.png" alt="Bing" border="0" />';
53529dea504SAndreas Gohr                    echo '</a> ';
53612dcdeccSAndreas Gohr                } elseif ($k == 'engine') {
537a8acb244SAndreas Gohr                    include_once(__DIR__ . '/inc/searchengines.php');
53813a86c14SAndreas Gohr                    if (isset($SEARCHENGINEINFO[$v])) {
53913a86c14SAndreas Gohr                        echo '<a href="' . $SEARCHENGINEINFO[$v][1] . '">' . $SEARCHENGINEINFO[$v][0] . '</a>';
54013a86c14SAndreas Gohr                    } else {
54113a86c14SAndreas Gohr                        echo hsc(ucwords($v));
54213a86c14SAndreas Gohr                    }
54313a86c14SAndreas Gohr                } elseif ($k == 'eflag') {
54413a86c14SAndreas Gohr                    $this->html_icon('search', $v);
54575fa767dSAndreas Gohr                } elseif ($k == 'bflag') {
54613a86c14SAndreas Gohr                    $this->html_icon('browser', $v);
547bd4217d3SAndreas Gohr                } elseif ($k == 'osflag') {
54813a86c14SAndreas Gohr                    $this->html_icon('os', $v);
54975fa767dSAndreas Gohr                } elseif ($k == 'cflag') {
55013a86c14SAndreas Gohr                    $this->html_icon('flags', $v);
55114d99ec0SAndreas Gohr                } elseif ($k == 'html') {
55214d99ec0SAndreas Gohr                    echo $v;
55314d99ec0SAndreas Gohr                } else {
55414d99ec0SAndreas Gohr                    echo hsc($v);
55514d99ec0SAndreas Gohr                }
55614d99ec0SAndreas Gohr                echo '</td>';
55714d99ec0SAndreas Gohr            }
55814d99ec0SAndreas Gohr            echo '</tr>';
5592507f8e0SAndreas Gohr
5602507f8e0SAndreas Gohr            if ($pager && ($count == $pager)) break;
5612507f8e0SAndreas Gohr            $count++;
56214d99ec0SAndreas Gohr        }
56314d99ec0SAndreas Gohr        echo '</table>';
5642507f8e0SAndreas Gohr
5652507f8e0SAndreas Gohr        if ($pager) $this->html_pager($pager, count($result) > $pager);
5661878f16fSAndreas Gohr    }
5671878f16fSAndreas Gohr
568a8acb244SAndreas Gohr    public function html_icon($type, $value)
569a8acb244SAndreas Gohr    {
57013a86c14SAndreas Gohr        $value = strtolower(preg_replace('/[^\w]+/', '', $value));
5719bb008afSAndreas Gohr        $value = str_replace(' ', '_', $value);
572a8acb244SAndreas Gohr
57313a86c14SAndreas Gohr        $file = 'lib/plugins/statistics/ico/' . $type . '/' . $value . '.png';
574dffb869bSAndreas Gohr        if ($type == 'flags') {
575dffb869bSAndreas Gohr            $w = 18;
576dffb869bSAndreas Gohr            $h = 12;
577dffb869bSAndreas Gohr        } else {
578dffb869bSAndreas Gohr            $w = 16;
579dffb869bSAndreas Gohr            $h = 16;
580dffb869bSAndreas Gohr        }
58113a86c14SAndreas Gohr        if (file_exists(DOKU_INC . $file)) {
582dffb869bSAndreas Gohr            echo '<img src="' . DOKU_BASE . $file . '" alt="' . hsc($value) . '" width="' . $w . '" height="' . $h . '" />';
58313a86c14SAndreas Gohr        }
58413a86c14SAndreas Gohr    }
5851878f16fSAndreas Gohr}
586