xref: /plugin/statistics/helper.php (revision c4c84f989914854a8164dd20e63d673e898e858b)
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{
176b6f8822SAndreas Gohr    public $prefix;
18aab59130SAndreas Gohr    protected $oQuery;
19762f4807SAndreas Gohr    protected ?Logger $oLogger = null;
20*c4c84f98SAndreas Gohr    protected ?StatisticsGraph $oGraph = null;
21762f4807SAndreas Gohr    protected ?SQLiteDB $db = null;
226b6f8822SAndreas Gohr
236b6f8822SAndreas Gohr    /**
24d5ef99ddSAndreas Gohr     * Get SQLiteDB instance
25d5ef99ddSAndreas Gohr     *
26d5ef99ddSAndreas Gohr     * @return SQLiteDB|null
27483101d3SAndreas Gohr     * @throws Exception when SQLite initialization failed
28d5ef99ddSAndreas Gohr     */
29483101d3SAndreas Gohr    public function getDB(): ?SQLiteDB
30d5ef99ddSAndreas Gohr    {
312adee4c6SAndreas Gohr        if (!$this->db instanceof SQLiteDB) {
32211caa5dSAndreas Gohr            if (!class_exists(SQLiteDB::class)) throw new Exception('SQLite Plugin missing');
33762f4807SAndreas Gohr            $this->db = new SQLiteDB('statistics', DOKU_PLUGIN . 'statistics/db/');
34d5ef99ddSAndreas Gohr        }
35d5ef99ddSAndreas Gohr        return $this->db;
36d5ef99ddSAndreas Gohr    }
37d5ef99ddSAndreas Gohr
38d5ef99ddSAndreas Gohr
39d5ef99ddSAndreas Gohr    /**
406b6f8822SAndreas Gohr     * Return an instance of the query class
416b6f8822SAndreas Gohr     *
42b6632b6eSAndreas Gohr     * @return Query
436b6f8822SAndreas Gohr     */
44211caa5dSAndreas Gohr    public function getQuery(): Query
45a8acb244SAndreas Gohr    {
466b6f8822SAndreas Gohr        if (is_null($this->oQuery)) {
47b6632b6eSAndreas Gohr            $this->oQuery = new Query($this);
486b6f8822SAndreas Gohr        }
496b6f8822SAndreas Gohr        return $this->oQuery;
506b6f8822SAndreas Gohr    }
516b6f8822SAndreas Gohr
526b6f8822SAndreas Gohr    /**
536b6f8822SAndreas Gohr     * Return an instance of the logger class
546b6f8822SAndreas Gohr     *
55aab59130SAndreas Gohr     * @return Logger
566b6f8822SAndreas Gohr     */
57211caa5dSAndreas Gohr    public function getLogger(): ?Logger
58a8acb244SAndreas Gohr    {
596b6f8822SAndreas Gohr        if (is_null($this->oLogger)) {
60aab59130SAndreas Gohr            $this->oLogger = new Logger($this);
616b6f8822SAndreas Gohr        }
626b6f8822SAndreas Gohr        return $this->oLogger;
636b6f8822SAndreas Gohr    }
646b6f8822SAndreas Gohr
656b6f8822SAndreas Gohr    /**
666b6f8822SAndreas Gohr     * Return an instance of the Graph class
676b6f8822SAndreas Gohr     *
681664ba1dSAndreas Gohr     * @return StatisticsGraph
696b6f8822SAndreas Gohr     */
70211caa5dSAndreas Gohr    public function getGraph($from, $to, $width, $height)
71a8acb244SAndreas Gohr    {
726b6f8822SAndreas Gohr        if (is_null($this->oGraph)) {
73f0a4cceeSAnna Dabrowska            $this->oGraph = new StatisticsGraph($this, $from, $to, $width, $height);
746b6f8822SAndreas Gohr        }
756b6f8822SAndreas Gohr        return $this->oGraph;
766b6f8822SAndreas Gohr    }
776b6f8822SAndreas Gohr
786b6f8822SAndreas Gohr    /**
794f41a2ccSAndreas Gohr     * Just send a 1x1 pixel blank gif to the browser
804f41a2ccSAndreas Gohr     *
814f41a2ccSAndreas Gohr     * @called from log.php
824f41a2ccSAndreas Gohr     *
834f41a2ccSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
844f41a2ccSAndreas Gohr     * @author Harry Fuecks <fuecks@gmail.com>
854f41a2ccSAndreas Gohr     */
86a8acb244SAndreas Gohr    public function sendGIF($transparent = true)
87a8acb244SAndreas Gohr    {
88259897e1SAndreas Gohr        if ($transparent) {
894f41a2ccSAndreas Gohr            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
90259897e1SAndreas Gohr        } else {
91259897e1SAndreas Gohr            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
92259897e1SAndreas Gohr        }
934f41a2ccSAndreas Gohr        header('Content-Type: image/gif');
944f41a2ccSAndreas Gohr        header('Content-Length: ' . strlen($img));
954f41a2ccSAndreas Gohr        header('Connection: Close');
96a8acb244SAndreas Gohr        echo $img;
974f41a2ccSAndreas Gohr        flush();
984f41a2ccSAndreas Gohr        // Browser should drop connection after this
99211caa5dSAndreas Gohr        // Thinks it got the whole image
1004f41a2ccSAndreas Gohr    }
1016b6f8822SAndreas Gohr}
102