1<?php
2/**
3 * DokuWiki Plugin top (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_top extends DokuWiki_Plugin {
13
14    /** @var helper_plugin_sqlite */
15    protected $sqlite = null;
16
17    /**
18     * initializes the DB connection
19     *
20     * @return helper_plugin_sqlite|null
21     */
22    public function getDBHelper() {
23        if(!is_null($this->sqlite)) return $this->sqlite;
24
25        $this->sqlite = plugin_load('helper', 'sqlite');
26        if(!$this->sqlite) {
27            msg('The top plugin requires the sqlite plugin', -1);
28            $this->sqlite = null;
29            return null;
30        }
31
32        $ok = $this->sqlite->init('top', __DIR__ . '/db');
33        if(!$ok) {
34            msg('rating plugin sqlite initialization failed', -1);
35            $this->sqlite = null;
36            return null;
37        }
38
39        return $this->sqlite;
40    }
41
42    /**
43     * Adds a hit for the given page
44     *
45     * @param string $page the page id
46     */
47    public function add($page) {
48        $sqlite = $this->getDBHelper();
49        if(!$sqlite) return;
50
51        // ignore any bot accesses
52        if(!class_exists('Jaybizzle\CrawlerDetect\CrawlerDetect')){
53            require (__DIR__ . '/CrawlerDetect.php');
54        }
55        $CrawlerDetect = new Jaybizzle\CrawlerDetect\CrawlerDetect();
56        if($CrawlerDetect->isCrawler()) return;
57
58        $translation = plugin_load('helper', 'translation');
59        if (!$translation) {
60            $lang = '';
61        } else {
62            $lang = $translation->getLangPart($page);
63        }
64
65        $month = date('Ym');
66
67        $sql = "INSERT OR REPLACE INTO toppages (page, value, lang, month)
68                  VALUES ( ?, COALESCE( (SELECT value FROM toppages WHERE page = ? and month = ? ) + 1, 1), ?, ?)";
69        $res = $sqlite->query($sql, $page, $page, $month, $lang, $month);
70        $sqlite->res_close($res);
71    }
72
73    /**
74     * Get the most visited pages
75     *
76     * @param int $num
77     * @return array
78     */
79    public function best($lang, $month, $num = 10) {
80        $sqlite = $this->getDBHelper();
81        if(!$sqlite) return array();
82
83        $sqlbegin  = "SELECT SUM(value) as value, page FROM toppages ";
84        $sqlend = "GROUP BY page ORDER BY value DESC LIMIT ?";
85        if ($lang === null && $month === null){
86            $sql = $sqlbegin . $sqlend;
87            $res  = $sqlite->query($sql, $num);
88        } elseif ($lang !== null && $month === null) {
89            $sql = $sqlbegin . "WHERE lang = ? " . $sqlend;
90            $res  = $sqlite->query($sql, $lang, $num);
91        } elseif ($lang === null && $month !== null){
92            $sql = $sqlbegin . "WHERE month >= ? " . $sqlend;
93            $res  = $sqlite->query($sql, intval($month), $num);
94        } else {
95            $sql = $sqlbegin . "WHERE lang = ? AND month >= ? " . $sqlend;
96            $res  = $sqlite->query($sql, $lang, intval($month), $num);
97        }
98        $list = $sqlite->res2arr($res);
99        $sqlite->res_close($res);
100
101        if ($this->getConf('hide_start_pages')) {
102            $list = $this->removeStartPages($list);
103        }
104        return $list;
105    }
106
107    public function removeStartPages($list) {
108        global $conf;
109        $start = $conf['start'];
110        $startpages = array();
111        $startpages[] = $start;
112
113        if ($conf['plugin']['translation']['translations'] !== '') {
114            $translations = explode(' ', $conf['plugin']['translation']['translations']);
115            foreach($translations as $translation) {
116                $startpages[] = $translation . ':' . $start;
117            }
118        }
119
120        foreach ($list as $index => $listitem) {
121            if (in_array($listitem['page'],$startpages, true) === true ) {
122                unset($list[$index]);
123            }
124        }
125        $list = array_values($list);
126        return $list;
127    }
128
129}
130
131// vim:ts=4:sw=4:et:
132