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