xref: /plugin/statistics/helper.php (revision c4c84f989914854a8164dd20e63d673e898e858b)
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    public $prefix;
18    protected $oQuery;
19    protected ?Logger $oLogger = null;
20    protected ?StatisticsGraph $oGraph = null;
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 instanceof SQLiteDB) {
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 getQuery(): 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 getLogger(): ?Logger
58    {
59        if (is_null($this->oLogger)) {
60            $this->oLogger = new Logger($this);
61        }
62        return $this->oLogger;
63    }
64
65    /**
66     * Return an instance of the Graph class
67     *
68     * @return StatisticsGraph
69     */
70    public function getGraph($from, $to, $width, $height)
71    {
72        if (is_null($this->oGraph)) {
73            $this->oGraph = new StatisticsGraph($this, $from, $to, $width, $height);
74        }
75        return $this->oGraph;
76    }
77
78    /**
79     * Just send a 1x1 pixel blank gif to the browser
80     *
81     * @called from log.php
82     *
83     * @author Andreas Gohr <andi@splitbrain.org>
84     * @author Harry Fuecks <fuecks@gmail.com>
85     */
86    public function sendGIF($transparent = true)
87    {
88        if ($transparent) {
89            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
90        } else {
91            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
92        }
93        header('Content-Type: image/gif');
94        header('Content-Length: ' . strlen($img));
95        header('Connection: Close');
96        echo $img;
97        flush();
98        // Browser should drop connection after this
99        // Thinks it got the whole image
100    }
101}
102