xref: /plugin/statistics/helper.php (revision 569a50664ac8b883b8bdf6f42f326d88d099ad6e)
1<?php
2
3use dokuwiki\Extension\Plugin;
4use dokuwiki\plugin\sqlite\SQLiteDB;
5use dokuwiki\plugin\statistics\Logger;
6use dokuwiki\plugin\statistics\Query;
7use dokuwiki\plugin\statistics\StatisticsGraph;
8
9/**
10 * Statistics Plugin
11 *
12 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
13 * @author  Andreas Gohr <andi@splitbrain.org>
14 */
15class helper_plugin_statistics extends Plugin
16{
17    protected ?Query $oQuery = null;
18    protected ?StatisticsGraph $oGraph = null;
19    protected ?SQLiteDB $db = null;
20
21    /**
22     * Get SQLiteDB instance
23     *
24     * @return SQLiteDB|null
25     * @throws Exception when SQLite initialization failed
26     */
27    public function getDB(): ?SQLiteDB
28    {
29        if (!$this->db instanceof SQLiteDB) {
30            if (!class_exists(SQLiteDB::class)) throw new Exception('SQLite Plugin missing');
31            $this->db = new SQLiteDB('statistics', DOKU_PLUGIN . 'statistics/db/');
32        }
33        return $this->db;
34    }
35
36
37    /**
38     * Return an instance of the query class
39     *
40     * @return Query
41     */
42    public function getQuery(): Query
43    {
44        if (is_null($this->oQuery)) {
45            $this->oQuery = new Query($this);
46        }
47        return $this->oQuery;
48    }
49
50    /**
51     * Return an instance of the logger class
52     *
53     * @return Logger
54     */
55    public function getLogger(): Logger
56    {
57        return new Logger($this);
58    }
59
60    /**
61     * Return an instance of the Graph class
62     *
63     * @return StatisticsGraph
64     */
65    public function getGraph($from, $to, $width, $height)
66    {
67        if (is_null($this->oGraph)) {
68            $this->oGraph = new StatisticsGraph($this, $from, $to, $width, $height);
69        }
70        return $this->oGraph;
71    }
72
73    /**
74     * Just send a 1x1 pixel blank gif to the browser
75     *
76     * @called from log.php
77     *
78     * @author Andreas Gohr <andi@splitbrain.org>
79     * @author Harry Fuecks <fuecks@gmail.com>
80     */
81    public function sendGIF($transparent = true)
82    {
83        if ($transparent) {
84            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
85        } else {
86            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
87        }
88        header('Content-Type: image/gif');
89        header('Content-Length: ' . strlen($img));
90        header('Connection: Close');
91        echo $img;
92        flush();
93        // Browser should drop connection after this
94        // Thinks it got the whole image
95    }
96}
97