xref: /plugin/statistics/admin.php (revision 338987f55a16788ed8f4cc623bd70d3b0220ce1a)
1<?php
2/**
3 * statistics plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <gohr@splitbrain.org>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12/**
13 * All DokuWiki plugins to extend the admin function
14 * need to inherit from this class
15 */
16class admin_plugin_statistics extends DokuWiki_Admin_Plugin {
17    /** @var string the currently selected page */
18    protected $opt = '';
19
20    /** @var string from date in YYYY-MM-DD */
21    protected $from = '';
22    /** @var string to date in YYYY-MM-DD */
23    protected $to = '';
24    /** @var int Offset to use when displaying paged data */
25    protected $start = 0;
26
27    /** @var string MySQL timelimit statement */
28    protected $tlimit = '';
29
30    /** @var helper_plugin_statistics  */
31    protected $hlp;
32
33    /**
34     * Available statistic pages
35     */
36    protected $pages = array(
37        'dashboard', 'page', 'edits', 'images', 'downloads',
38        'history', 'referer', 'newreferer',
39        'outlinks', 'searchengines', 'searchphrases',
40        'searchwords', 'internalsearchphrases',
41        'internalsearchwords', 'browsers', 'os',
42        'countries', 'resolution', 'viewport',
43        'seenusers'
44    );
45
46    /**
47     * Initialize the helper
48     */
49    public function __construct() {
50        $this->hlp = plugin_load('helper', 'statistics');
51    }
52
53    /**
54     * Access for managers allowed
55     */
56    public function forAdminOnly() {
57        return false;
58    }
59
60    /**
61     * return sort order for position in admin menu
62     */
63    public function getMenuSort() {
64        return 350;
65    }
66
67    /**
68     * handle user request
69     */
70    public function handle() {
71        $this->opt = preg_replace('/[^a-z]+/', '', $_REQUEST['opt']);
72        if(!in_array($this->opt, $this->pages)) $this->opt = 'dashboard';
73
74        $this->start = (int) $_REQUEST['s'];
75        $this->setTimeframe($_REQUEST['f'], $_REQUEST['t']);
76    }
77
78    /**
79     * set limit clause
80     */
81    public function setTimeframe($from, $to) {
82        $this->tlimit = $this->hlp->Query()->mktlimit($from, $to);
83        $this->from   = $from;
84        $this->to     = $to;
85    }
86
87    /**
88     * Output the Statistics
89     */
90    function html() {
91        echo '<div id="plugin__statistics">';
92        echo '<h1>' . $this->getLang('menu') . '</h1>';
93        $this->html_timeselect();
94        tpl_flush();
95
96        $method = 'html_' . $this->opt;
97        if(method_exists($this, $method)) {
98            echo '<div class="plg_stats_' . $this->opt . '">';
99            echo '<h2>' . $this->getLang($this->opt) . '</h2>';
100            $this->$method();
101            echo '</div>';
102        }
103        echo '</div>';
104    }
105
106    /**
107     * Return the TOC
108     *
109     * @return array
110     */
111    function getTOC() {
112        $toc = array();
113        foreach($this->pages as $page) {
114            $toc[] = array(
115                'link'  => '?do=admin&amp;page=statistics&amp;opt=' . $page . '&amp;f=' . $this->from . '&amp;t=' . $this->to,
116                'title' => $this->getLang($page),
117                'level' => 1,
118                'type'  => 'ul'
119            );
120        }
121        return $toc;
122    }
123
124    function html_graph($name, $width, $height) {
125        $url = DOKU_BASE . 'lib/plugins/statistics/img.php?img=' . $name .
126            '&amp;f=' . $this->from . '&amp;t=' . $this->to;
127        echo '<img src="' . $url . '" class="graph" width="' . $width . '" height="' . $height . '"/>';
128    }
129
130    /**
131     * Outputs pagination links
132     *
133     * @param int $limit
134     * @param int $next
135     */
136    function html_pager($limit, $next) {
137        echo '<div class="plg_stats_pager">';
138
139        if($this->start > 0) {
140            $go = max($this->start - $limit, 0);
141            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>';
142        }
143
144        if($next) {
145            $go = $this->start + $limit;
146            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>';
147        }
148        echo '</div>';
149    }
150
151    /**
152     * Print the time selection menu
153     */
154    function html_timeselect() {
155        $today  = date('Y-m-d');
156        $last1  = date('Y-m-d', time() - (60 * 60 * 24));
157        $last7  = date('Y-m-d', time() - (60 * 60 * 24 * 7));
158        $last30 = date('Y-m-d', time() - (60 * 60 * 24 * 30));
159
160        echo '<div class="plg_stats_timeselect">';
161        echo '<span>' . $this->getLang('time_select') . '</span> ';
162
163        echo '<form action="" method="get">';
164        echo '<input type="hidden" name="do" value="admin" />';
165        echo '<input type="hidden" name="page" value="statistics" />';
166        echo '<input type="hidden" name="opt" value="' . $this->opt . '" />';
167        echo '<input type="text" name="f" value="' . $this->from . '" class="edit" />';
168        echo '<input type="text" name="t" value="' . $this->to . '" class="edit" />';
169        echo '<input type="submit" value="go" class="button" />';
170        echo '</form>';
171
172        echo '<ul>';
173        foreach(array('today', 'last1', 'last7', 'last30') as $time) {
174            echo '<li>';
175            echo '<a href="?do=admin&amp;page=statistics&amp;opt=' . $this->opt . '&amp;f=' . $$time . '&amp;t=' . $today . '">';
176            echo $this->getLang('time_' . $time);
177            echo '</a>';
178            echo '</li>';
179        }
180        echo '</ul>';
181
182        echo '</div>';
183    }
184
185    /**
186     * Print an introductionary screen
187     */
188    function html_dashboard() {
189        echo '<p>' . $this->getLang('intro_dashboard') . '</p>';
190
191        // general info
192        echo '<div class="plg_stats_top">';
193        $result = $this->hlp->Query()->aggregate($this->tlimit);
194
195        echo '<ul class="left">';
196        foreach(array('pageviews', 'sessions', 'visitors', 'users', 'logins', 'current') as $name) {
197            echo '<li><div class="li">' . sprintf($this->getLang('dash_' . $name), $result[$name]) . '</div></li>';
198        }
199        echo '</ul>';
200
201        echo '<ul class="left">';
202        foreach(array('bouncerate', 'timespent', 'avgpages', 'newvisitors', 'registrations') as $name) {
203            echo '<li><div class="li">' . sprintf($this->getLang('dash_' . $name), $result[$name]) . '</div></li>';
204        }
205        echo '</ul>';
206
207        $this->html_graph('dashboardviews', 700, 280);
208        $this->html_graph('dashboardwiki', 700, 280);
209        echo '</div>';
210
211        // top pages today
212        echo '<div>';
213        echo '<h2>' . $this->getLang('dash_mostpopular') . '</h2>';
214        $result = $this->hlp->Query()->pages($this->tlimit, $this->start, 15);
215        $this->html_resulttable($result);
216        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>';
217        echo '</div>';
218
219        // top referer today
220        echo '<div>';
221        echo '<h2>' . $this->getLang('dash_newincoming') . '</h2>';
222        $result = $this->hlp->Query()->newreferer($this->tlimit, $this->start, 15);
223        $this->html_resulttable($result);
224        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>';
225        echo '</div>';
226
227        // top searches today
228        echo '<div>';
229        echo '<h2>' . $this->getLang('dash_topsearch') . '</h2>';
230        $result = $this->hlp->Query()->searchphrases(true, $this->tlimit, $this->start, 15);
231        $this->html_resulttable($result);
232        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>';
233        echo '</div>';
234    }
235
236    function html_history() {
237        echo '<p>' . $this->getLang('intro_history') . '</p>';
238        $this->html_graph('history_page_count', 600, 200);
239        $this->html_graph('history_page_size', 600, 200);
240        $this->html_graph('history_media_count', 600, 200);
241        $this->html_graph('history_media_size', 600, 200);
242    }
243
244    function html_countries() {
245        echo '<p>' . $this->getLang('intro_countries') . '</p>';
246        $this->html_graph('countries', 400, 200);
247        $result = $this->hlp->Query()->countries($this->tlimit, $this->start, 150);
248        $this->html_resulttable($result, '', 150);
249    }
250
251    function html_page() {
252        echo '<p>' . $this->getLang('intro_page') . '</p>';
253        $result = $this->hlp->Query()->pages($this->tlimit, $this->start, 150);
254        $this->html_resulttable($result, '', 150);
255    }
256
257    function html_edits() {
258        echo '<p>' . $this->getLang('intro_edits') . '</p>';
259        $result = $this->hlp->Query()->edits($this->tlimit, $this->start, 150);
260        $this->html_resulttable($result, '', 150);
261    }
262
263    function html_images() {
264        echo '<p>' . $this->getLang('intro_images') . '</p>';
265        $result = $this->hlp->Query()->images($this->tlimit, $this->start, 150);
266        $this->html_resulttable($result, '', 150);
267    }
268
269    function html_downloads() {
270        echo '<p>' . $this->getLang('intro_downloads') . '</p>';
271        $result = $this->hlp->Query()->downloads($this->tlimit, $this->start, 150);
272        $this->html_resulttable($result, '', 150);
273    }
274
275    function html_browsers() {
276        echo '<p>' . $this->getLang('intro_browsers') . '</p>';
277        $this->html_graph('browsers', 400, 200);
278        $result = $this->hlp->Query()->browsers($this->tlimit, $this->start, 150, true);
279        $this->html_resulttable($result, '', 150);
280    }
281
282    function html_os() {
283        echo '<p>' . $this->getLang('intro_os') . '</p>';
284        $this->html_graph('os', 400, 200);
285        $result = $this->hlp->Query()->os($this->tlimit, $this->start, 150, true);
286        $this->html_resulttable($result, '', 150);
287    }
288
289    function html_referer() {
290        $result = $this->hlp->Query()->aggregate($this->tlimit);
291
292        $all = $result['search'] + $result['external'] + $result['direct'];
293
294        if($all) {
295            printf(
296                '<p>' . $this->getLang('intro_referer') . '</p>',
297                $all, $result['direct'], (100 * $result['direct'] / $all),
298                $result['search'], (100 * $result['search'] / $all), $result['external'],
299                (100 * $result['external'] / $all)
300            );
301        }
302
303        $result = $this->hlp->Query()->referer($this->tlimit, $this->start, 150);
304        $this->html_resulttable($result, '', 150);
305    }
306
307    function html_newreferer() {
308        echo '<p>' . $this->getLang('intro_newreferer') . '</p>';
309
310        $result = $this->hlp->Query()->newreferer($this->tlimit, $this->start, 150);
311        $this->html_resulttable($result, '', 150);
312    }
313
314    function html_outlinks() {
315        echo '<p>' . $this->getLang('intro_outlinks') . '</p>';
316        $result = $this->hlp->Query()->outlinks($this->tlimit, $this->start, 150);
317        $this->html_resulttable($result, '', 150);
318    }
319
320    function html_searchphrases() {
321        echo '<p>' . $this->getLang('intro_searchphrases') . '</p>';
322        $result = $this->hlp->Query()->searchphrases(true, $this->tlimit, $this->start, 150);
323        $this->html_resulttable($result, '', 150);
324    }
325
326    function html_searchwords() {
327        echo '<p>' . $this->getLang('intro_searchwords') . '</p>';
328        $result = $this->hlp->Query()->searchwords(true, $this->tlimit, $this->start, 150);
329        $this->html_resulttable($result, '', 150);
330    }
331
332    function html_internalsearchphrases() {
333        echo '<p>' . $this->getLang('intro_internalsearchphrases') . '</p>';
334        $result = $this->hlp->Query()->searchphrases(false, $this->tlimit, $this->start, 150);
335        $this->html_resulttable($result, '', 150);
336    }
337
338    function html_internalsearchwords() {
339        echo '<p>' . $this->getLang('intro_internalsearchwords') . '</p>';
340        $result = $this->hlp->Query()->searchwords(false, $this->tlimit, $this->start, 150);
341        $this->html_resulttable($result, '', 150);
342    }
343
344    function html_searchengines() {
345        echo '<p>' . $this->getLang('intro_searchengines') . '</p>';
346        $this->html_graph('searchengines', 400, 200);
347        $result = $this->hlp->Query()->searchengines($this->tlimit, $this->start, 150);
348        $this->html_resulttable($result, '', 150);
349    }
350
351    function html_resolution() {
352        echo '<p>' . $this->getLang('intro_resolution') . '</p>';
353        $this->html_graph('resolution', 650, 490);
354        $result = $this->hlp->Query()->resolution($this->tlimit, $this->start, 150);
355        $this->html_resulttable($result, '', 150);
356    }
357
358    function html_viewport() {
359        echo '<p>' . $this->getLang('intro_viewport') . '</p>';
360        $this->html_graph('viewport', 650, 490);
361        $result = $this->hlp->Query()->viewport($this->tlimit, $this->start, 150);
362        $this->html_resulttable($result, '', 150);
363    }
364
365    function html_seenusers() {
366        echo '<p>' . $this->getLang('intro_seenusers') . '</p>';
367        $result = $this->hlp->Query()->seenusers($this->tlimit, $this->start, 150);
368        $this->html_resulttable($result, '', 150);
369    }
370
371    /**
372     * Display a result in a HTML table
373     */
374    function html_resulttable($result, $header = '', $pager = 0) {
375        echo '<table>';
376        if(is_array($header)) {
377            echo '<tr>';
378            foreach($header as $h) {
379                echo '<th>' . hsc($h) . '</th>';
380            }
381            echo '</tr>';
382        }
383
384        $count = 0;
385        if(is_array($result)) foreach($result as $row) {
386            echo '<tr>';
387            foreach($row as $k => $v) {
388                if($k == 'res_x') continue;
389                if($k == 'res_y') continue;
390
391                echo '<td class="plg_stats_X' . $k . '">';
392                if($k == 'page') {
393                    echo '<a href="' . wl($v) . '" class="wikilink1">';
394                    echo hsc($v);
395                    echo '</a>';
396                } elseif($k == 'media') {
397                        echo '<a href="' . ml($v) . '" class="wikilink1">';
398                        echo hsc($v);
399                        echo '</a>';
400                } elseif($k == 'filesize') {
401                    echo filesize_h($v);
402                } elseif($k == 'url') {
403                    $url = hsc($v);
404                    $url = preg_replace('/^https?:\/\/(www\.)?/', '', $url);
405                    if(strlen($url) > 45) {
406                        $url = substr($url, 0, 30) . ' &hellip; ' . substr($url, -15);
407                    }
408                    echo '<a href="' . $v . '" class="urlextern">';
409                    echo $url;
410                    echo '</a>';
411                } elseif($k == 'ilookup') {
412                    echo '<a href="' . wl('', array('id' => $v, 'do' => 'search')) . '">Search</a>';
413                } elseif($k == 'lookup') {
414                    echo '<a href="http://www.google.com/search?q=' . rawurlencode($v) . '">';
415                    echo '<img src="' . DOKU_BASE . 'lib/plugins/statistics/ico/search/google.png" alt="Google" border="0" />';
416                    echo '</a> ';
417
418                    echo '<a href="http://search.yahoo.com/search?p=' . rawurlencode($v) . '">';
419                    echo '<img src="' . DOKU_BASE . 'lib/plugins/statistics/ico/search/yahoo.png" alt="Yahoo!" border="0" />';
420                    echo '</a> ';
421
422                    echo '<a href="http://www.bing.com/search?q=' . rawurlencode($v) . '">';
423                    echo '<img src="' . DOKU_BASE . 'lib/plugins/statistics/ico/search/bing.png" alt="Bing" border="0" />';
424                    echo '</a> ';
425
426                } elseif($k == 'engine') {
427                    include_once(dirname(__FILE__) . '/inc/searchengines.php');
428                    if(isset($SEARCHENGINEINFO[$v])) {
429                        echo '<a href="' . $SEARCHENGINEINFO[$v][1] . '">' . $SEARCHENGINEINFO[$v][0] . '</a>';
430                    } else {
431                        echo hsc(ucwords($v));
432                    }
433                } elseif($k == 'eflag') {
434                    $this->html_icon('search', $v);
435                } elseif($k == 'bflag') {
436                    $this->html_icon('browser', $v);
437                } elseif($k == 'osflag') {
438                    $this->html_icon('os', $v);
439                } elseif($k == 'cflag') {
440                    $this->html_icon('flags', $v);
441                } elseif($k == 'html') {
442                    echo $v;
443                } else {
444                    echo hsc($v);
445                }
446                echo '</td>';
447            }
448            echo '</tr>';
449
450            if($pager && ($count == $pager)) break;
451            $count++;
452        }
453        echo '</table>';
454
455        if($pager) $this->html_pager($pager, count($result) > $pager);
456    }
457
458    function html_icon($type, $value) {
459        $value = strtolower(preg_replace('/[^\w]+/', '', $value));
460        $value = str_replace(' ', '_', $value);
461        $file  = 'lib/plugins/statistics/ico/' . $type . '/' . $value . '.png';
462        if($type == 'flags') {
463            $w = 18;
464            $h = 12;
465        } else {
466            $w = 16;
467            $h = 16;
468        }
469        if(file_exists(DOKU_INC . $file)) {
470            echo '<img src="' . DOKU_BASE . $file . '" alt="' . hsc($value) . '" width="' . $w . '" height="' . $h . '" />';
471        }
472    }
473}
474