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