xref: /plugin/statistics/helper.php (revision 41d1fffc4a3b58bed7f96d983ba8317fe9e225a5)
16b6f8822SAndreas Gohr<?php
2a8acb244SAndreas Gohr
3d5ef99ddSAndreas Gohruse dokuwiki\Extension\Plugin;
4d5ef99ddSAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB;
5aab59130SAndreas Gohruse dokuwiki\plugin\statistics\Logger;
6b6632b6eSAndreas Gohruse dokuwiki\plugin\statistics\Query;
764ba7f66SAnna Dabrowskause dokuwiki\plugin\statistics\StatisticsGraph;
8d5ef99ddSAndreas Gohr
96b6f8822SAndreas Gohr/**
106b6f8822SAndreas Gohr * Statistics Plugin
116b6f8822SAndreas Gohr *
126b6f8822SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
136b6f8822SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
146b6f8822SAndreas Gohr */
15d5ef99ddSAndreas Gohrclass helper_plugin_statistics extends Plugin
16a8acb244SAndreas Gohr{
17*41d1fffcSAndreas Gohr    protected ?Query $oQuery = null;
18c4c84f98SAndreas Gohr    protected ?StatisticsGraph $oGraph = null;
19762f4807SAndreas Gohr    protected ?SQLiteDB $db = null;
206b6f8822SAndreas Gohr
216b6f8822SAndreas Gohr    /**
22d5ef99ddSAndreas Gohr     * Get SQLiteDB instance
23d5ef99ddSAndreas Gohr     *
24d5ef99ddSAndreas Gohr     * @return SQLiteDB|null
25483101d3SAndreas Gohr     * @throws Exception when SQLite initialization failed
26d5ef99ddSAndreas Gohr     */
27483101d3SAndreas Gohr    public function getDB(): ?SQLiteDB
28d5ef99ddSAndreas Gohr    {
292adee4c6SAndreas Gohr        if (!$this->db instanceof SQLiteDB) {
30211caa5dSAndreas Gohr            if (!class_exists(SQLiteDB::class)) throw new Exception('SQLite Plugin missing');
31762f4807SAndreas Gohr            $this->db = new SQLiteDB('statistics', DOKU_PLUGIN . 'statistics/db/');
32d5ef99ddSAndreas Gohr        }
33d5ef99ddSAndreas Gohr        return $this->db;
34d5ef99ddSAndreas Gohr    }
35d5ef99ddSAndreas Gohr
36d5ef99ddSAndreas Gohr
37d5ef99ddSAndreas Gohr    /**
386b6f8822SAndreas Gohr     * Return an instance of the query class
396b6f8822SAndreas Gohr     *
40b6632b6eSAndreas Gohr     * @return Query
416b6f8822SAndreas Gohr     */
42211caa5dSAndreas Gohr    public function getQuery(): Query
43a8acb244SAndreas Gohr    {
446b6f8822SAndreas Gohr        if (is_null($this->oQuery)) {
45b6632b6eSAndreas Gohr            $this->oQuery = new Query($this);
466b6f8822SAndreas Gohr        }
476b6f8822SAndreas Gohr        return $this->oQuery;
486b6f8822SAndreas Gohr    }
496b6f8822SAndreas Gohr
506b6f8822SAndreas Gohr    /**
516b6f8822SAndreas Gohr     * Return an instance of the logger class
526b6f8822SAndreas Gohr     *
53aab59130SAndreas Gohr     * @return Logger
546b6f8822SAndreas Gohr     */
55211caa5dSAndreas Gohr    public function getLogger(): ?Logger
56a8acb244SAndreas Gohr    {
57*41d1fffcSAndreas Gohr        return new Logger($this);
586b6f8822SAndreas Gohr    }
596b6f8822SAndreas Gohr
606b6f8822SAndreas Gohr    /**
616b6f8822SAndreas Gohr     * Return an instance of the Graph class
626b6f8822SAndreas Gohr     *
631664ba1dSAndreas Gohr     * @return StatisticsGraph
646b6f8822SAndreas Gohr     */
65211caa5dSAndreas Gohr    public function getGraph($from, $to, $width, $height)
66a8acb244SAndreas Gohr    {
676b6f8822SAndreas Gohr        if (is_null($this->oGraph)) {
68f0a4cceeSAnna Dabrowska            $this->oGraph = new StatisticsGraph($this, $from, $to, $width, $height);
696b6f8822SAndreas Gohr        }
706b6f8822SAndreas Gohr        return $this->oGraph;
716b6f8822SAndreas Gohr    }
726b6f8822SAndreas Gohr
736b6f8822SAndreas Gohr    /**
744f41a2ccSAndreas Gohr     * Just send a 1x1 pixel blank gif to the browser
754f41a2ccSAndreas Gohr     *
764f41a2ccSAndreas Gohr     * @called from log.php
774f41a2ccSAndreas Gohr     *
784f41a2ccSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
794f41a2ccSAndreas Gohr     * @author Harry Fuecks <fuecks@gmail.com>
804f41a2ccSAndreas Gohr     */
81a8acb244SAndreas Gohr    public function sendGIF($transparent = true)
82a8acb244SAndreas Gohr    {
83259897e1SAndreas Gohr        if ($transparent) {
844f41a2ccSAndreas Gohr            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
85259897e1SAndreas Gohr        } else {
86259897e1SAndreas Gohr            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
87259897e1SAndreas Gohr        }
884f41a2ccSAndreas Gohr        header('Content-Type: image/gif');
894f41a2ccSAndreas Gohr        header('Content-Length: ' . strlen($img));
904f41a2ccSAndreas Gohr        header('Connection: Close');
91a8acb244SAndreas Gohr        echo $img;
924f41a2ccSAndreas Gohr        flush();
934f41a2ccSAndreas Gohr        // Browser should drop connection after this
94211caa5dSAndreas Gohr        // Thinks it got the whole image
954f41a2ccSAndreas Gohr    }
966b6f8822SAndreas Gohr}
97