xref: /plugin/statistics/helper.php (revision 6b6f8822aea95e0fbdfc3340464d791e6676d91e)
1<?php
2/**
3 * Statistics Plugin
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9class helper_plugin_statistics extends Dokuwiki_Plugin {
10
11    private $dblink = null;
12    public  $prefix;
13    private $oQuery  = null;
14    private $oLogger = null;
15    private $oGraph  = null;
16
17    /**
18     * Return an instance of the query class
19     *
20     * @return object
21     */
22    public function Query(){
23        if(is_null($this->oQuery)){
24            require dirname(__FILE__).'/inc/StatisticsQuery.class.php';
25            $this->oQuery = new StatisticsQuery($this);
26        }
27        return $this->oQuery;
28    }
29
30    /**
31     * Return an instance of the logger class
32     *
33     * @return object
34     */
35    public function Logger(){
36        if(is_null($this->oLogger)){
37            require dirname(__FILE__).'/inc/StatisticsLogger.class.php';
38            $this->oLogger = new StatisticsLog($this);
39        }
40        return $this->oLogger;
41    }
42
43    /**
44     * Return an instance of the Graph class
45     *
46     * @return object
47     */
48    public function Graph(){
49        if(is_null($this->oGraph)){
50            require dirname(__FILE__).'/inc/StatisticsGraph.class.php';
51            $this->oGraph = new StatisticsGraph($this);
52        }
53        return $this->oGraph;
54    }
55
56    /**
57     * Return a link to the DB, opening the connection if needed
58     */
59    protected function dbLink(){
60        // connect to DB if needed
61        if(!$this->dblink){
62            $this->prefix = $this->getConf('db_prefix');
63            $this->dblink = mysql_connect($this->getConf('db_server'),
64                                          $this->getConf('db_user'),
65                                          $this->getConf('db_password'));
66            if(!$this->dblink){
67                msg('DB Error: connection failed',-1);
68                return null;
69            }
70            // set utf-8
71            if(!mysql_db_query($this->getConf('db_database'),'set names utf8',$this->dblink)){
72                msg('DB Error: could not set UTF-8 ('.mysql_error($this->dblink).')',-1);
73                return null;
74            }
75        }
76        return $this->dblink;
77    }
78
79    /**
80     * Simple function to run a DB query
81     */
82    public function runSQL($sql_string) {
83        $link = $this->dbLink();
84
85        $result = mysql_db_query($this->conf['db_database'],$sql_string,$link);
86        if(!$result){
87            msg('DB Error: '.mysql_error($link).' '.hsc($sql_string),-1);
88            return null;
89        }
90
91        $resultarray = array();
92
93        //mysql_db_query returns 1 on a insert statement -> no need to ask for results
94        if ($result != 1) {
95            for($i=0; $i< mysql_num_rows($result); $i++) {
96                $temparray = mysql_fetch_assoc($result);
97                $resultarray[]=$temparray;
98            }
99            mysql_free_result($result);
100        }
101
102        if (mysql_insert_id($link)) {
103            $resultarray = mysql_insert_id($link); //give back ID on insert
104        }
105
106        return $resultarray;
107    }
108
109}
110