xref: /plugin/statistics/Query.php (revision b6632b6ef30a6f5a10a0a925445db5529b84c915)
1*b6632b6eSAndreas Gohr<?php
2*b6632b6eSAndreas Gohr
3*b6632b6eSAndreas Gohrnamespace dokuwiki\plugin\statistics;
4*b6632b6eSAndreas Gohr
5*b6632b6eSAndreas Gohruse helper_plugin_statistics;
6*b6632b6eSAndreas Gohr
7*b6632b6eSAndreas Gohrclass Query
8*b6632b6eSAndreas Gohr{
9*b6632b6eSAndreas Gohr    protected $hlp;
10*b6632b6eSAndreas Gohr
11*b6632b6eSAndreas Gohr    public function __construct(helper_plugin_statistics $hlp)
12*b6632b6eSAndreas Gohr    {
13*b6632b6eSAndreas Gohr        $this->hlp = $hlp;
14*b6632b6eSAndreas Gohr    }
15*b6632b6eSAndreas Gohr
16*b6632b6eSAndreas Gohr    /**
17*b6632b6eSAndreas Gohr     * Return some aggregated statistics
18*b6632b6eSAndreas Gohr     */
19*b6632b6eSAndreas Gohr    public function aggregate($tlimit)
20*b6632b6eSAndreas Gohr    {
21*b6632b6eSAndreas Gohr        $data = [];
22*b6632b6eSAndreas Gohr
23*b6632b6eSAndreas Gohr        $sql = "SELECT ref_type, COUNT(*) as cnt
24*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A
25*b6632b6eSAndreas Gohr                 WHERE $tlimit
26*b6632b6eSAndreas Gohr                   AND ua_type = 'browser'
27*b6632b6eSAndreas Gohr              GROUP BY ref_type";
28*b6632b6eSAndreas Gohr        $result = $this->hlp->runSQL($sql);
29*b6632b6eSAndreas Gohr
30*b6632b6eSAndreas Gohr        if (is_array($result)) foreach ($result as $row) {
31*b6632b6eSAndreas Gohr            if ($row['ref_type'] == 'search') $data['search'] = $row['cnt'];
32*b6632b6eSAndreas Gohr            if ($row['ref_type'] == 'external') $data['external'] = $row['cnt'];
33*b6632b6eSAndreas Gohr            if ($row['ref_type'] == 'internal') $data['internal'] = $row['cnt'];
34*b6632b6eSAndreas Gohr            if ($row['ref_type'] == '') $data['direct'] = $row['cnt'];
35*b6632b6eSAndreas Gohr        }
36*b6632b6eSAndreas Gohr
37*b6632b6eSAndreas Gohr        // general user and session info
38*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(DISTINCT session) as sessions,
39*b6632b6eSAndreas Gohr                       COUNT(session) as views,
40*b6632b6eSAndreas Gohr                       COUNT(DISTINCT user) as users,
41*b6632b6eSAndreas Gohr                       COUNT(DISTINCT uid) as visitors
42*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A
43*b6632b6eSAndreas Gohr                 WHERE $tlimit
44*b6632b6eSAndreas Gohr                   AND ua_type = 'browser'";
45*b6632b6eSAndreas Gohr        $result = $this->hlp->runSQL($sql);
46*b6632b6eSAndreas Gohr
47*b6632b6eSAndreas Gohr        $data['users'] = max($result[0]['users'] - 1, 0); // subtract empty user
48*b6632b6eSAndreas Gohr        $data['sessions'] = $result[0]['sessions'];
49*b6632b6eSAndreas Gohr        $data['pageviews'] = $result[0]['views'];
50*b6632b6eSAndreas Gohr        $data['visitors'] = $result[0]['visitors'];
51*b6632b6eSAndreas Gohr
52*b6632b6eSAndreas Gohr        // calculate bounce rate
53*b6632b6eSAndreas Gohr        if ($data['sessions']) {
54*b6632b6eSAndreas Gohr            $sql = "SELECT COUNT(*) as cnt
55*b6632b6eSAndreas Gohr                      FROM " . $this->hlp->prefix . "session as A
56*b6632b6eSAndreas Gohr                     WHERE $tlimit
57*b6632b6eSAndreas Gohr                       AND views = 1";
58*b6632b6eSAndreas Gohr            $result = $this->hlp->runSQL($sql);
59*b6632b6eSAndreas Gohr            $data['bouncerate'] = $result[0]['cnt'] * 100 / $data['sessions'];
60*b6632b6eSAndreas Gohr            $result = $this->hlp->runSQL($sql);
61*b6632b6eSAndreas Gohr            $data['newvisitors'] = $result[0]['cnt'] * 100 / $data['sessions'];
62*b6632b6eSAndreas Gohr        }
63*b6632b6eSAndreas Gohr
64*b6632b6eSAndreas Gohr        // calculate avg. number of views per session
65*b6632b6eSAndreas Gohr        $sql = "SELECT AVG(views) as cnt
66*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "session as A
67*b6632b6eSAndreas Gohr                     WHERE $tlimit";
68*b6632b6eSAndreas Gohr        $result = $this->hlp->runSQL($sql);
69*b6632b6eSAndreas Gohr        $data['avgpages'] = $result[0]['cnt'];
70*b6632b6eSAndreas Gohr
71*b6632b6eSAndreas Gohr        /* not used currently
72*b6632b6eSAndreas Gohr                $sql = "SELECT COUNT(id) as robots
73*b6632b6eSAndreas Gohr                          FROM ".$this->hlp->prefix."access as A
74*b6632b6eSAndreas Gohr                         WHERE $tlimit
75*b6632b6eSAndreas Gohr                           AND ua_type = 'robot'";
76*b6632b6eSAndreas Gohr                $result = $this->hlp->runSQL($sql);
77*b6632b6eSAndreas Gohr                $data['robots'] = $result[0]['robots'];
78*b6632b6eSAndreas Gohr        */
79*b6632b6eSAndreas Gohr
80*b6632b6eSAndreas Gohr        // average time spent on the site
81*b6632b6eSAndreas Gohr        $sql = "SELECT AVG(end - dt)/60 as time
82*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "session as A
83*b6632b6eSAndreas Gohr                 WHERE $tlimit
84*b6632b6eSAndreas Gohr                   AND dt != end
85*b6632b6eSAndreas Gohr                   AND DATE(dt) = DATE(end)";
86*b6632b6eSAndreas Gohr        $result = $this->hlp->runSQL($sql);
87*b6632b6eSAndreas Gohr        $data['timespent'] = $result[0]['time'];
88*b6632b6eSAndreas Gohr
89*b6632b6eSAndreas Gohr        // logins
90*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as logins
91*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "logins as A
92*b6632b6eSAndreas Gohr                 WHERE $tlimit
93*b6632b6eSAndreas Gohr                   AND (type = 'l' OR type = 'p')";
94*b6632b6eSAndreas Gohr        $result = $this->hlp->runSQL($sql);
95*b6632b6eSAndreas Gohr        $data['logins'] = $result[0]['logins'];
96*b6632b6eSAndreas Gohr
97*b6632b6eSAndreas Gohr        // registrations
98*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as registrations
99*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "logins as A
100*b6632b6eSAndreas Gohr                 WHERE $tlimit
101*b6632b6eSAndreas Gohr                   AND type = 'C'";
102*b6632b6eSAndreas Gohr        $result = $this->hlp->runSQL($sql);
103*b6632b6eSAndreas Gohr        $data['registrations'] = $result[0]['registrations'];
104*b6632b6eSAndreas Gohr
105*b6632b6eSAndreas Gohr        // current users
106*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as current
107*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "lastseen
108*b6632b6eSAndreas Gohr                 WHERE `dt` >= NOW() - INTERVAL 10 MINUTE";
109*b6632b6eSAndreas Gohr        $result = $this->hlp->runSQL($sql);
110*b6632b6eSAndreas Gohr        $data['current'] = $result[0]['current'];
111*b6632b6eSAndreas Gohr
112*b6632b6eSAndreas Gohr        return $data;
113*b6632b6eSAndreas Gohr    }
114*b6632b6eSAndreas Gohr
115*b6632b6eSAndreas Gohr    /**
116*b6632b6eSAndreas Gohr     * standard statistics follow, only accesses made by browsers are counted
117*b6632b6eSAndreas Gohr     * for general stats like browser or OS only visitors not pageviews are counted
118*b6632b6eSAndreas Gohr     */
119*b6632b6eSAndreas Gohr
120*b6632b6eSAndreas Gohr    /**
121*b6632b6eSAndreas Gohr     * Return some trend data about visits and edits in the wiki
122*b6632b6eSAndreas Gohr     */
123*b6632b6eSAndreas Gohr    public function dashboardviews($tlimit, $hours = false)
124*b6632b6eSAndreas Gohr    {
125*b6632b6eSAndreas Gohr        if ($hours) {
126*b6632b6eSAndreas Gohr            $TIME = 'HOUR(dt)';
127*b6632b6eSAndreas Gohr        } else {
128*b6632b6eSAndreas Gohr            $TIME = 'DATE(dt)';
129*b6632b6eSAndreas Gohr        }
130*b6632b6eSAndreas Gohr
131*b6632b6eSAndreas Gohr        $data = [];
132*b6632b6eSAndreas Gohr
133*b6632b6eSAndreas Gohr        // access trends
134*b6632b6eSAndreas Gohr        $sql = "SELECT $TIME as time,
135*b6632b6eSAndreas Gohr                       COUNT(DISTINCT session) as sessions,
136*b6632b6eSAndreas Gohr                       COUNT(session) as pageviews,
137*b6632b6eSAndreas Gohr                       COUNT(DISTINCT uid) as visitors
138*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A
139*b6632b6eSAndreas Gohr                 WHERE $tlimit
140*b6632b6eSAndreas Gohr                   AND ua_type = 'browser'
141*b6632b6eSAndreas Gohr              GROUP BY $TIME
142*b6632b6eSAndreas Gohr              ORDER BY time";
143*b6632b6eSAndreas Gohr        $result = $this->hlp->runSQL($sql);
144*b6632b6eSAndreas Gohr        foreach ($result as $row) {
145*b6632b6eSAndreas Gohr            $data[$row['time']]['sessions'] = $row['sessions'];
146*b6632b6eSAndreas Gohr            $data[$row['time']]['pageviews'] = $row['pageviews'];
147*b6632b6eSAndreas Gohr            $data[$row['time']]['visitors'] = $row['visitors'];
148*b6632b6eSAndreas Gohr        }
149*b6632b6eSAndreas Gohr        return $data;
150*b6632b6eSAndreas Gohr    }
151*b6632b6eSAndreas Gohr
152*b6632b6eSAndreas Gohr    public function dashboardwiki($tlimit, $hours = false)
153*b6632b6eSAndreas Gohr    {
154*b6632b6eSAndreas Gohr        if ($hours) {
155*b6632b6eSAndreas Gohr            $TIME = 'HOUR(dt)';
156*b6632b6eSAndreas Gohr        } else {
157*b6632b6eSAndreas Gohr            $TIME = 'DATE(dt)';
158*b6632b6eSAndreas Gohr        }
159*b6632b6eSAndreas Gohr
160*b6632b6eSAndreas Gohr        $data = [];
161*b6632b6eSAndreas Gohr
162*b6632b6eSAndreas Gohr        // edit trends
163*b6632b6eSAndreas Gohr        foreach (['E', 'C', 'D'] as $type) {
164*b6632b6eSAndreas Gohr            $sql = "SELECT $TIME as time,
165*b6632b6eSAndreas Gohr                           COUNT(*) as cnt
166*b6632b6eSAndreas Gohr                      FROM " . $this->hlp->prefix . "edits as A
167*b6632b6eSAndreas Gohr                     WHERE $tlimit
168*b6632b6eSAndreas Gohr                       AND type = '$type'
169*b6632b6eSAndreas Gohr                  GROUP BY $TIME
170*b6632b6eSAndreas Gohr                  ORDER BY time";
171*b6632b6eSAndreas Gohr            $result = $this->hlp->runSQL($sql);
172*b6632b6eSAndreas Gohr            foreach ($result as $row) {
173*b6632b6eSAndreas Gohr                $data[$row['time']][$type] = $row['cnt'];
174*b6632b6eSAndreas Gohr            }
175*b6632b6eSAndreas Gohr        }
176*b6632b6eSAndreas Gohr        ksort($data);
177*b6632b6eSAndreas Gohr        return $data;
178*b6632b6eSAndreas Gohr    }
179*b6632b6eSAndreas Gohr
180*b6632b6eSAndreas Gohr    public function history($tlimit, $info, $interval = false)
181*b6632b6eSAndreas Gohr    {
182*b6632b6eSAndreas Gohr        if ($interval == 'weeks') {
183*b6632b6eSAndreas Gohr            $TIME = 'EXTRACT(YEAR FROM dt), EXTRACT(WEEK FROM dt)';
184*b6632b6eSAndreas Gohr        } elseif ($interval == 'months') {
185*b6632b6eSAndreas Gohr            $TIME = 'EXTRACT(YEAR_MONTH FROM dt)';
186*b6632b6eSAndreas Gohr        } else {
187*b6632b6eSAndreas Gohr            $TIME = 'dt';
188*b6632b6eSAndreas Gohr        }
189*b6632b6eSAndreas Gohr
190*b6632b6eSAndreas Gohr        $mod = 1;
191*b6632b6eSAndreas Gohr        if ($info == 'media_size' || $info == 'page_size') {
192*b6632b6eSAndreas Gohr            $mod = 1024 * 1024;
193*b6632b6eSAndreas Gohr        }
194*b6632b6eSAndreas Gohr
195*b6632b6eSAndreas Gohr        $sql = "SELECT $TIME as time,
196*b6632b6eSAndreas Gohr                       AVG(`value`)/$mod as cnt
197*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "history as A
198*b6632b6eSAndreas Gohr                 WHERE $tlimit
199*b6632b6eSAndreas Gohr                   AND info = '$info'
200*b6632b6eSAndreas Gohr                  GROUP BY $TIME
201*b6632b6eSAndreas Gohr                  ORDER BY $TIME";
202*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
203*b6632b6eSAndreas Gohr    }
204*b6632b6eSAndreas Gohr
205*b6632b6eSAndreas Gohr    public function searchengines($tlimit, $start = 0, $limit = 20)
206*b6632b6eSAndreas Gohr    {
207*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, engine as eflag, engine
208*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "search as A
209*b6632b6eSAndreas Gohr                 WHERE $tlimit
210*b6632b6eSAndreas Gohr              GROUP BY engine
211*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, engine" .
212*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
213*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
214*b6632b6eSAndreas Gohr    }
215*b6632b6eSAndreas Gohr
216*b6632b6eSAndreas Gohr    public function searchphrases($extern, $tlimit, $start = 0, $limit = 20)
217*b6632b6eSAndreas Gohr    {
218*b6632b6eSAndreas Gohr        if ($extern) {
219*b6632b6eSAndreas Gohr            $WHERE = "engine != 'dokuwiki'";
220*b6632b6eSAndreas Gohr            $I = '';
221*b6632b6eSAndreas Gohr        } else {
222*b6632b6eSAndreas Gohr            $WHERE = "engine = 'dokuwiki'";
223*b6632b6eSAndreas Gohr            $I = 'i';
224*b6632b6eSAndreas Gohr        }
225*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, query, query as ${I}lookup
226*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "search as A
227*b6632b6eSAndreas Gohr                 WHERE $tlimit
228*b6632b6eSAndreas Gohr                   AND $WHERE
229*b6632b6eSAndreas Gohr              GROUP BY query
230*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, query" .
231*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
232*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
233*b6632b6eSAndreas Gohr    }
234*b6632b6eSAndreas Gohr
235*b6632b6eSAndreas Gohr    public function searchwords($extern, $tlimit, $start = 0, $limit = 20)
236*b6632b6eSAndreas Gohr    {
237*b6632b6eSAndreas Gohr        if ($extern) {
238*b6632b6eSAndreas Gohr            $WHERE = "engine != 'dokuwiki'";
239*b6632b6eSAndreas Gohr            $I = '';
240*b6632b6eSAndreas Gohr        } else {
241*b6632b6eSAndreas Gohr            $WHERE = "engine = 'dokuwiki'";
242*b6632b6eSAndreas Gohr            $I = 'i';
243*b6632b6eSAndreas Gohr        }
244*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, word, word as ${I}lookup
245*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "search as A,
246*b6632b6eSAndreas Gohr                       " . $this->hlp->prefix . "searchwords as B
247*b6632b6eSAndreas Gohr                 WHERE $tlimit
248*b6632b6eSAndreas Gohr                   AND A.id = B.sid
249*b6632b6eSAndreas Gohr                   AND $WHERE
250*b6632b6eSAndreas Gohr              GROUP BY word
251*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, word" .
252*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
253*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
254*b6632b6eSAndreas Gohr    }
255*b6632b6eSAndreas Gohr
256*b6632b6eSAndreas Gohr    public function outlinks($tlimit, $start = 0, $limit = 20)
257*b6632b6eSAndreas Gohr    {
258*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, link as url
259*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "outlinks as A
260*b6632b6eSAndreas Gohr                 WHERE $tlimit
261*b6632b6eSAndreas Gohr              GROUP BY link
262*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, link" .
263*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
264*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
265*b6632b6eSAndreas Gohr    }
266*b6632b6eSAndreas Gohr
267*b6632b6eSAndreas Gohr    public function pages($tlimit, $start = 0, $limit = 20)
268*b6632b6eSAndreas Gohr    {
269*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, page
270*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A
271*b6632b6eSAndreas Gohr                 WHERE $tlimit
272*b6632b6eSAndreas Gohr                   AND ua_type = 'browser'
273*b6632b6eSAndreas Gohr              GROUP BY page
274*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, page" .
275*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
276*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
277*b6632b6eSAndreas Gohr    }
278*b6632b6eSAndreas Gohr
279*b6632b6eSAndreas Gohr    public function edits($tlimit, $start = 0, $limit = 20)
280*b6632b6eSAndreas Gohr    {
281*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, page
282*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "edits as A
283*b6632b6eSAndreas Gohr                 WHERE $tlimit
284*b6632b6eSAndreas Gohr              GROUP BY page
285*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, page" .
286*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
287*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
288*b6632b6eSAndreas Gohr    }
289*b6632b6eSAndreas Gohr
290*b6632b6eSAndreas Gohr    public function images($tlimit, $start = 0, $limit = 20)
291*b6632b6eSAndreas Gohr    {
292*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, media, SUM(size) as filesize
293*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "media as A
294*b6632b6eSAndreas Gohr                 WHERE $tlimit
295*b6632b6eSAndreas Gohr                   AND mime1 = 'image'
296*b6632b6eSAndreas Gohr              GROUP BY media
297*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, media" .
298*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
299*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
300*b6632b6eSAndreas Gohr    }
301*b6632b6eSAndreas Gohr
302*b6632b6eSAndreas Gohr    public function imagessum($tlimit)
303*b6632b6eSAndreas Gohr    {
304*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, SUM(size) as filesize
305*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "media as A
306*b6632b6eSAndreas Gohr                 WHERE $tlimit
307*b6632b6eSAndreas Gohr                   AND mime1 = 'image'";
308*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
309*b6632b6eSAndreas Gohr    }
310*b6632b6eSAndreas Gohr
311*b6632b6eSAndreas Gohr    public function downloads($tlimit, $start = 0, $limit = 20)
312*b6632b6eSAndreas Gohr    {
313*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, media, SUM(size) as filesize
314*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "media as A
315*b6632b6eSAndreas Gohr                 WHERE $tlimit
316*b6632b6eSAndreas Gohr                   AND mime1 != 'image'
317*b6632b6eSAndreas Gohr              GROUP BY media
318*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, media" .
319*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
320*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
321*b6632b6eSAndreas Gohr    }
322*b6632b6eSAndreas Gohr
323*b6632b6eSAndreas Gohr    public function downloadssum($tlimit)
324*b6632b6eSAndreas Gohr    {
325*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, SUM(size) as filesize
326*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "media as A
327*b6632b6eSAndreas Gohr                 WHERE $tlimit
328*b6632b6eSAndreas Gohr                   AND mime1 != 'image'";
329*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
330*b6632b6eSAndreas Gohr    }
331*b6632b6eSAndreas Gohr
332*b6632b6eSAndreas Gohr    public function referer($tlimit, $start = 0, $limit = 20)
333*b6632b6eSAndreas Gohr    {
334*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, ref as url
335*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A
336*b6632b6eSAndreas Gohr                 WHERE $tlimit
337*b6632b6eSAndreas Gohr                   AND ua_type = 'browser'
338*b6632b6eSAndreas Gohr                   AND ref_type = 'external'
339*b6632b6eSAndreas Gohr              GROUP BY ref_md5
340*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, url" .
341*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
342*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
343*b6632b6eSAndreas Gohr    }
344*b6632b6eSAndreas Gohr
345*b6632b6eSAndreas Gohr    public function newreferer($tlimit, $start = 0, $limit = 20)
346*b6632b6eSAndreas Gohr    {
347*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, ref as url
348*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as B,
349*b6632b6eSAndreas Gohr                       " . $this->hlp->prefix . "refseen as A
350*b6632b6eSAndreas Gohr                 WHERE $tlimit
351*b6632b6eSAndreas Gohr                   AND ua_type = 'browser'
352*b6632b6eSAndreas Gohr                   AND ref_type = 'external'
353*b6632b6eSAndreas Gohr                   AND A.ref_md5 = B.ref_md5
354*b6632b6eSAndreas Gohr              GROUP BY A.ref_md5
355*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, url" .
356*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
357*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
358*b6632b6eSAndreas Gohr    }
359*b6632b6eSAndreas Gohr
360*b6632b6eSAndreas Gohr    public function countries($tlimit, $start = 0, $limit = 20)
361*b6632b6eSAndreas Gohr    {
362*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(DISTINCT session) as cnt, B.code AS cflag, B.country
363*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A,
364*b6632b6eSAndreas Gohr                       " . $this->hlp->prefix . "iplocation as B
365*b6632b6eSAndreas Gohr                 WHERE $tlimit
366*b6632b6eSAndreas Gohr                   AND A.ip = B.ip
367*b6632b6eSAndreas Gohr              GROUP BY B.code
368*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, B.country" .
369*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
370*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
371*b6632b6eSAndreas Gohr    }
372*b6632b6eSAndreas Gohr
373*b6632b6eSAndreas Gohr    public function browsers($tlimit, $start = 0, $limit = 20, $ext = true)
374*b6632b6eSAndreas Gohr    {
375*b6632b6eSAndreas Gohr        if ($ext) {
376*b6632b6eSAndreas Gohr            $sel = 'ua_info as bflag, ua_info as browser, ua_ver';
377*b6632b6eSAndreas Gohr            $grp = 'ua_info, ua_ver';
378*b6632b6eSAndreas Gohr        } else {
379*b6632b6eSAndreas Gohr            $grp = 'ua_info';
380*b6632b6eSAndreas Gohr            $sel = 'ua_info';
381*b6632b6eSAndreas Gohr        }
382*b6632b6eSAndreas Gohr
383*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(DISTINCT session) as cnt, $sel
384*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A
385*b6632b6eSAndreas Gohr                 WHERE $tlimit
386*b6632b6eSAndreas Gohr                   AND ua_type = 'browser'
387*b6632b6eSAndreas Gohr              GROUP BY $grp
388*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, ua_info" .
389*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
390*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
391*b6632b6eSAndreas Gohr    }
392*b6632b6eSAndreas Gohr
393*b6632b6eSAndreas Gohr    public function os($tlimit, $start = 0, $limit = 20)
394*b6632b6eSAndreas Gohr    {
395*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(DISTINCT session) as cnt, os as osflag, os
396*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A
397*b6632b6eSAndreas Gohr                 WHERE $tlimit
398*b6632b6eSAndreas Gohr                   AND ua_type = 'browser'
399*b6632b6eSAndreas Gohr              GROUP BY os
400*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, os" .
401*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
402*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
403*b6632b6eSAndreas Gohr    }
404*b6632b6eSAndreas Gohr
405*b6632b6eSAndreas Gohr    public function topuser($tlimit, $start = 0, $limit = 20)
406*b6632b6eSAndreas Gohr    {
407*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, user
408*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A
409*b6632b6eSAndreas Gohr                 WHERE $tlimit
410*b6632b6eSAndreas Gohr                   AND ua_type = 'browser'
411*b6632b6eSAndreas Gohr                   AND user != ''
412*b6632b6eSAndreas Gohr              GROUP BY user
413*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, user" .
414*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
415*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
416*b6632b6eSAndreas Gohr    }
417*b6632b6eSAndreas Gohr
418*b6632b6eSAndreas Gohr    public function topeditor($tlimit, $start = 0, $limit = 20)
419*b6632b6eSAndreas Gohr    {
420*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, user
421*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "edits as A
422*b6632b6eSAndreas Gohr                 WHERE $tlimit
423*b6632b6eSAndreas Gohr                   AND user != ''
424*b6632b6eSAndreas Gohr              GROUP BY user
425*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, user" .
426*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
427*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
428*b6632b6eSAndreas Gohr    }
429*b6632b6eSAndreas Gohr
430*b6632b6eSAndreas Gohr    public function topgroup($tlimit, $start = 0, $limit = 20)
431*b6632b6eSAndreas Gohr    {
432*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, `group`
433*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "groups as A
434*b6632b6eSAndreas Gohr                 WHERE $tlimit
435*b6632b6eSAndreas Gohr                   AND `type` = 'view'
436*b6632b6eSAndreas Gohr              GROUP BY `group`
437*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, `group`" .
438*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
439*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
440*b6632b6eSAndreas Gohr    }
441*b6632b6eSAndreas Gohr
442*b6632b6eSAndreas Gohr    public function topgroupedit($tlimit, $start = 0, $limit = 20)
443*b6632b6eSAndreas Gohr    {
444*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(*) as cnt, `group`
445*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "groups as A
446*b6632b6eSAndreas Gohr                 WHERE $tlimit
447*b6632b6eSAndreas Gohr                   AND `type` = 'edit'
448*b6632b6eSAndreas Gohr              GROUP BY `group`
449*b6632b6eSAndreas Gohr              ORDER BY cnt DESC, `group`" .
450*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
451*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
452*b6632b6eSAndreas Gohr    }
453*b6632b6eSAndreas Gohr
454*b6632b6eSAndreas Gohr
455*b6632b6eSAndreas Gohr    public function resolution($tlimit, $start = 0, $limit = 20)
456*b6632b6eSAndreas Gohr    {
457*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(DISTINCT uid) as cnt,
458*b6632b6eSAndreas Gohr                       ROUND(screen_x/100)*100 as res_x,
459*b6632b6eSAndreas Gohr                       ROUND(screen_y/100)*100 as res_y,
460*b6632b6eSAndreas Gohr                       CONCAT(ROUND(screen_x/100)*100,'x',ROUND(screen_y/100)*100) as resolution
461*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A
462*b6632b6eSAndreas Gohr                 WHERE $tlimit
463*b6632b6eSAndreas Gohr                   AND ua_type  = 'browser'
464*b6632b6eSAndreas Gohr                   AND screen_x != 0
465*b6632b6eSAndreas Gohr                   AND screen_y != 0
466*b6632b6eSAndreas Gohr              GROUP BY resolution
467*b6632b6eSAndreas Gohr              ORDER BY cnt DESC" .
468*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
469*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
470*b6632b6eSAndreas Gohr    }
471*b6632b6eSAndreas Gohr
472*b6632b6eSAndreas Gohr    public function viewport($tlimit, $start = 0, $limit = 20)
473*b6632b6eSAndreas Gohr    {
474*b6632b6eSAndreas Gohr        $sql = "SELECT COUNT(DISTINCT uid) as cnt,
475*b6632b6eSAndreas Gohr                       ROUND(view_x/100)*100 as res_x,
476*b6632b6eSAndreas Gohr                       ROUND(view_y/100)*100 as res_y,
477*b6632b6eSAndreas Gohr                       CONCAT(ROUND(view_x/100)*100,'x',ROUND(view_y/100)*100) as resolution
478*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "access as A
479*b6632b6eSAndreas Gohr                 WHERE $tlimit
480*b6632b6eSAndreas Gohr                   AND ua_type  = 'browser'
481*b6632b6eSAndreas Gohr                   AND view_x != 0
482*b6632b6eSAndreas Gohr                   AND view_y != 0
483*b6632b6eSAndreas Gohr              GROUP BY resolution
484*b6632b6eSAndreas Gohr              ORDER BY cnt DESC" .
485*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
486*b6632b6eSAndreas Gohr
487*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
488*b6632b6eSAndreas Gohr    }
489*b6632b6eSAndreas Gohr
490*b6632b6eSAndreas Gohr    public function seenusers($tlimit, $start = 0, $limit = 20)
491*b6632b6eSAndreas Gohr    {
492*b6632b6eSAndreas Gohr        $sql = "SELECT `user`, `dt`
493*b6632b6eSAndreas Gohr                  FROM " . $this->hlp->prefix . "lastseen as A
494*b6632b6eSAndreas Gohr              ORDER BY `dt` DESC" .
495*b6632b6eSAndreas Gohr            $this->mklimit($start, $limit);
496*b6632b6eSAndreas Gohr
497*b6632b6eSAndreas Gohr        return $this->hlp->runSQL($sql);
498*b6632b6eSAndreas Gohr    }
499*b6632b6eSAndreas Gohr
500*b6632b6eSAndreas Gohr
501*b6632b6eSAndreas Gohr    /**
502*b6632b6eSAndreas Gohr     * Builds a limit clause
503*b6632b6eSAndreas Gohr     */
504*b6632b6eSAndreas Gohr    public function mklimit($start, $limit)
505*b6632b6eSAndreas Gohr    {
506*b6632b6eSAndreas Gohr        $start = (int)$start;
507*b6632b6eSAndreas Gohr        $limit = (int)$limit;
508*b6632b6eSAndreas Gohr        if ($limit) {
509*b6632b6eSAndreas Gohr            $limit += 1;
510*b6632b6eSAndreas Gohr            return " LIMIT $start,$limit";
511*b6632b6eSAndreas Gohr        } elseif ($start) {
512*b6632b6eSAndreas Gohr            return " OFFSET $start";
513*b6632b6eSAndreas Gohr        }
514*b6632b6eSAndreas Gohr        return '';
515*b6632b6eSAndreas Gohr    }
516*b6632b6eSAndreas Gohr
517*b6632b6eSAndreas Gohr    /**
518*b6632b6eSAndreas Gohr     * Create a time limit for use in SQL
519*b6632b6eSAndreas Gohr     */
520*b6632b6eSAndreas Gohr    public function mktlimit(&$from, &$to)
521*b6632b6eSAndreas Gohr    {
522*b6632b6eSAndreas Gohr        // fixme add better sanity checking here:
523*b6632b6eSAndreas Gohr        $from = preg_replace('/[^\d\-]+/', '', $from);
524*b6632b6eSAndreas Gohr        $to = preg_replace('/[^\d\-]+/', '', $to);
525*b6632b6eSAndreas Gohr        if (!$from) $from = date('Y-m-d');
526*b6632b6eSAndreas Gohr        if (!$to) $to = date('Y-m-d');
527*b6632b6eSAndreas Gohr
528*b6632b6eSAndreas Gohr        return "A.dt >= '$from 00:00:00' AND A.dt <= '$to 23:59:59'";
529*b6632b6eSAndreas Gohr    }
530*b6632b6eSAndreas Gohr}
531