1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
/*
* Stats Class
*
* PHP version 5
*
* LICENSE: GNU General Public License, version 2 (GPLv2)
* Copyright (c) 2001 - 2011 Ampache.org All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License v2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @category Stats
* @package Ampache
* @author Karl Vollmer <vollmer@ampache.org>
* @copyright 2001 - 2011 Ampache.org
* @license http://opensource.org/licenses/gpl-2.0 GPLv2
* @version PHP 5.2
* @link http://www.ampache.org/
* @since File available since Release 1.0
*/
/**
* Stats Class
*
* this class handles the object_count
* Stuff, before this was done in the user class
* but that's not good, all done through here.
*
* @category Stats
* @package Ampache
* @author Karl Vollmer <vollmer@ampache.org>
* @copyright 2001 - 2011 Ampache.org
* @license http://opensource.org/licenses/gpl-2.0 GPLv2
* @version Release: 3.6
* @link http://www.ampache.org/
* @since Class available since Release 1.0
*/
class Stats {
/* Base vars */
var $id;
var $object_type;
var $object_id;
var $date;
var $user;
/**
* Constructor
* This doesn't do anything currently
*/
public function __construct() {
return true;
} // Constructor
/**
* insert
* This inserts a new record for the specified object
* with the specified information, amazing!
*/
public static function insert($type,$oid,$user) {
$type = self::validate_type($type);
$oid = Dba::escape($oid);
$user = Dba::escape($user);
$date = time();
$sql = "INSERT INTO `object_count` (`object_type`,`object_id`,`date`,`user`) " .
" VALUES ('$type','$oid','$date','$user')";
$db_results = Dba::write($sql);
if (!$db_results) {
debug_event('statistics','Unabled to insert statistics:' . $sql,'3');
}
} // insert
/**
* get_last_song
* This returns the full data for the last song that was played, including when it
* was played, this is used by, among other things, the LastFM plugin to figure out
* if we should re-submit or if this is a duplicate / if it's too soon. This takes an
* optional user_id because when streaming we don't have $GLOBALS()
*/
public static function get_last_song($user_id='') {
$user_id = $user_id ? $user_id : $GLOBALS['user']->id;
$user_id = Dba::escape($user_id);
$sql = "SELECT * FROM `object_count` WHERE `user`='$user_id' AND `object_type`='song' ORDER BY `date` DESC LIMIT 1";
$db_results = Dba::read($sql);
$results = Dba::fetch_assoc($db_results);
return $results;
} // get_last_song
/**
* get_object_history
* This returns the objects that have happened for $user_id sometime after $time
* used primarly by the democratic cooldown code
*/
public static function get_object_history($user_id='',$time) {
$user_id = $user_id ? $user_id : $GLOBALS['user']->id;
$user_id = Dba::escape($user_id);
$time = Dba::escape($time);
$sql = "SELECT * FROM `object_count` WHERE `user`='$user_id' AND `object_type`='song' AND `date`>='$time' " .
"ORDER BY `date` DESC";
$db_results = Dba::read($sql);
$results = array();
while ($row = Dba::fetch_assoc($db_results)) {
$results[] = $row['object_id'];
}
return $results;
} // get_object_history
/**
* get_top
* This returns the top X for type Y from the
* last conf('stats_threshold') days
*/
public static function get_top($type,$count='',$threshold = '') {
/* If they don't pass one, then use the preference */
if (!$threshold) {
$threshold = Config::get('stats_threshold');
}
if (!$count) {
$count = Config::get('popular_threshold');
}
$count = intval($count);
$type = self::validate_type($type);
$date = time() - (86400*$threshold);
/* Select Top objects counting by # of rows */
$sql = "SELECT object_id,COUNT(id) AS `count` FROM object_count" .
" WHERE object_type='$type' AND date >= '$date'" .
" GROUP BY object_id ORDER BY `count` DESC LIMIT $count";
$db_results = Dba::read($sql);
$results = array();
while ($row = Dba::fetch_assoc($db_results)) {
$results[] = $row['object_id'];
}
return $results;
} // get_top
/**
* get_user
* This gets all stats for atype based on user with thresholds and all
* If full is passed, doesn't limit based on date
*/
public static function get_user($count,$type,$user,$full='') {
$count = intval($count);
$type = self::validate_type($type);
$user = Dba::escape($user);
/* If full then don't limit on date */
if ($full) {
$date = '0';
}
else {
$date = time() - (86400*Config::get('stats_threshold'));
}
/* Select Objects based on user */
//FIXME:: Requires table scan, look at improving
$sql = "SELECT object_id,COUNT(id) AS `count` FROM object_count" .
" WHERE object_type='$type' AND date >= '$date' AND user = '$user'" .
" GROUP BY object_id ORDER BY `count` DESC LIMIT $count";
$db_results = Dba::read($sql);
$results = array();
while ($r = Dba::fetch_assoc($db_results)) {
$results[] = $r;
}
return $results;
} // get_user
/**
* validate_type
* This function takes a type and returns only those
* which are allowed, ensures good data gets put into the db
*/
public static function validate_type($type) {
switch ($type) {
case 'artist':
case 'album':
case 'genre':
case 'song':
case 'video':
return $type;
default:
return 'song';
break;
} // end switch
} // validate_type
/**
* get_newest
* This returns an array of the newest artists/albums/whatever
* in this ampache instance
*/
public static function get_newest($type,$limit='') {
if (!$limit) { $limit = Config::get('popular_threshold'); }
$type = self::validate_type($type);
$object_name = ucfirst($type);
$sql = "SELECT DISTINCT(`$type`), MIN(`addition_time`) AS `real_atime` FROM `song` GROUP BY `$type` ORDER BY `real_atime` DESC " .
"LIMIT $limit";
$db_results = Dba::read($sql);
$items = array();
while ($row = Dba::fetch_row($db_results)) {
$items[] = $row['0'];
} // end while results
return $items;
} // get_newest
} // Stats class
?>
|