xref: /plugin/statistics/helper.php (revision b6eece2f150cec03317dc34c6cacfe19914a03bd)
16b6f8822SAndreas Gohr<?php
26b6f8822SAndreas Gohr/**
36b6f8822SAndreas Gohr * Statistics Plugin
46b6f8822SAndreas Gohr *
56b6f8822SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
66b6f8822SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
76b6f8822SAndreas Gohr */
86b6f8822SAndreas Gohr
96b6f8822SAndreas Gohrclass helper_plugin_statistics extends Dokuwiki_Plugin {
106b6f8822SAndreas Gohr
116b6f8822SAndreas Gohr    private $dblink = null;
126b6f8822SAndreas Gohr    public  $prefix;
136b6f8822SAndreas Gohr    private $oQuery  = null;
146b6f8822SAndreas Gohr    private $oLogger = null;
156b6f8822SAndreas Gohr    private $oGraph  = null;
166b6f8822SAndreas Gohr
176b6f8822SAndreas Gohr    /**
186b6f8822SAndreas Gohr     * Return an instance of the query class
196b6f8822SAndreas Gohr     *
206b6f8822SAndreas Gohr     * @return object
216b6f8822SAndreas Gohr     */
226b6f8822SAndreas Gohr    public function Query(){
23*b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
246b6f8822SAndreas Gohr        if(is_null($this->oQuery)){
256b6f8822SAndreas Gohr            require dirname(__FILE__).'/inc/StatisticsQuery.class.php';
266b6f8822SAndreas Gohr            $this->oQuery = new StatisticsQuery($this);
276b6f8822SAndreas Gohr        }
286b6f8822SAndreas Gohr        return $this->oQuery;
296b6f8822SAndreas Gohr    }
306b6f8822SAndreas Gohr
316b6f8822SAndreas Gohr    /**
326b6f8822SAndreas Gohr     * Return an instance of the logger class
336b6f8822SAndreas Gohr     *
346b6f8822SAndreas Gohr     * @return object
356b6f8822SAndreas Gohr     */
366b6f8822SAndreas Gohr    public function Logger(){
37*b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
386b6f8822SAndreas Gohr        if(is_null($this->oLogger)){
396b6f8822SAndreas Gohr            require dirname(__FILE__).'/inc/StatisticsLogger.class.php';
406b6f8822SAndreas Gohr            $this->oLogger = new StatisticsLog($this);
416b6f8822SAndreas Gohr        }
426b6f8822SAndreas Gohr        return $this->oLogger;
436b6f8822SAndreas Gohr    }
446b6f8822SAndreas Gohr
456b6f8822SAndreas Gohr    /**
466b6f8822SAndreas Gohr     * Return an instance of the Graph class
476b6f8822SAndreas Gohr     *
486b6f8822SAndreas Gohr     * @return object
496b6f8822SAndreas Gohr     */
506b6f8822SAndreas Gohr    public function Graph(){
51*b6eece2fSAndreas Gohr        $this->prefix = $this->getConf('db_prefix');
526b6f8822SAndreas Gohr        if(is_null($this->oGraph)){
536b6f8822SAndreas Gohr            require dirname(__FILE__).'/inc/StatisticsGraph.class.php';
546b6f8822SAndreas Gohr            $this->oGraph = new StatisticsGraph($this);
556b6f8822SAndreas Gohr        }
566b6f8822SAndreas Gohr        return $this->oGraph;
576b6f8822SAndreas Gohr    }
586b6f8822SAndreas Gohr
596b6f8822SAndreas Gohr    /**
606b6f8822SAndreas Gohr     * Return a link to the DB, opening the connection if needed
616b6f8822SAndreas Gohr     */
626b6f8822SAndreas Gohr    protected function dbLink(){
636b6f8822SAndreas Gohr        // connect to DB if needed
646b6f8822SAndreas Gohr        if(!$this->dblink){
656b6f8822SAndreas Gohr            $this->dblink = mysql_connect($this->getConf('db_server'),
666b6f8822SAndreas Gohr                                          $this->getConf('db_user'),
676b6f8822SAndreas Gohr                                          $this->getConf('db_password'));
686b6f8822SAndreas Gohr            if(!$this->dblink){
696b6f8822SAndreas Gohr                msg('DB Error: connection failed',-1);
706b6f8822SAndreas Gohr                return null;
716b6f8822SAndreas Gohr            }
726b6f8822SAndreas Gohr            // set utf-8
736b6f8822SAndreas Gohr            if(!mysql_db_query($this->getConf('db_database'),'set names utf8',$this->dblink)){
746b6f8822SAndreas Gohr                msg('DB Error: could not set UTF-8 ('.mysql_error($this->dblink).')',-1);
756b6f8822SAndreas Gohr                return null;
766b6f8822SAndreas Gohr            }
776b6f8822SAndreas Gohr        }
786b6f8822SAndreas Gohr        return $this->dblink;
796b6f8822SAndreas Gohr    }
806b6f8822SAndreas Gohr
816b6f8822SAndreas Gohr    /**
826b6f8822SAndreas Gohr     * Simple function to run a DB query
836b6f8822SAndreas Gohr     */
846b6f8822SAndreas Gohr    public function runSQL($sql_string) {
856b6f8822SAndreas Gohr        $link = $this->dbLink();
866b6f8822SAndreas Gohr
876b6f8822SAndreas Gohr        $result = mysql_db_query($this->conf['db_database'],$sql_string,$link);
886b6f8822SAndreas Gohr        if(!$result){
896b6f8822SAndreas Gohr            msg('DB Error: '.mysql_error($link).' '.hsc($sql_string),-1);
906b6f8822SAndreas Gohr            return null;
916b6f8822SAndreas Gohr        }
926b6f8822SAndreas Gohr
936b6f8822SAndreas Gohr        $resultarray = array();
946b6f8822SAndreas Gohr
956b6f8822SAndreas Gohr        //mysql_db_query returns 1 on a insert statement -> no need to ask for results
966b6f8822SAndreas Gohr        if ($result != 1) {
976b6f8822SAndreas Gohr            for($i=0; $i< mysql_num_rows($result); $i++) {
986b6f8822SAndreas Gohr                $temparray = mysql_fetch_assoc($result);
996b6f8822SAndreas Gohr                $resultarray[]=$temparray;
1006b6f8822SAndreas Gohr            }
1016b6f8822SAndreas Gohr            mysql_free_result($result);
1026b6f8822SAndreas Gohr        }
1036b6f8822SAndreas Gohr
1046b6f8822SAndreas Gohr        if (mysql_insert_id($link)) {
1056b6f8822SAndreas Gohr            $resultarray = mysql_insert_id($link); //give back ID on insert
1066b6f8822SAndreas Gohr        }
1076b6f8822SAndreas Gohr
1086b6f8822SAndreas Gohr        return $resultarray;
1096b6f8822SAndreas Gohr    }
1106b6f8822SAndreas Gohr
1116b6f8822SAndreas Gohr}
112