summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarl 'vollmerk' Vollmer <vollmer@ampache.org>2007-07-27 23:18:40 +0000
committerKarl 'vollmerk' Vollmer <vollmer@ampache.org>2007-07-27 23:18:40 +0000
commit755734ee8825b90993249b6fef87a59f1ac755c3 (patch)
tree76fd5d565a040c8d54c304bf567717b200445e25
parent14c432a4b14a119c390404dfda001c4fd7503343 (diff)
downloadampache-755734ee8825b90993249b6fef87a59f1ac755c3.tar.gz
ampache-755734ee8825b90993249b6fef87a59f1ac755c3.tar.bz2
ampache-755734ee8825b90993249b6fef87a59f1ac755c3.zip
stupid vollmer no biscut
-rw-r--r--lib/album.lib.php58
1 files changed, 26 insertions, 32 deletions
diff --git a/lib/album.lib.php b/lib/album.lib.php
index c484eca4..6edfc932 100644
--- a/lib/album.lib.php
+++ b/lib/album.lib.php
@@ -79,42 +79,36 @@ function get_image_from_source($data) {
* This returns a random number of albums from the catalogs
* this is used by the index to return some 'potential' albums to play
*/
-function get_random_albums($count=6) {
- // There's a slight chance with this logic that the number of albums
- // returned will be less than the number requested if the id's for the
- // albums have signifigant gaps, but the speed increase is probably
- // worth it
- // - Vlet
-
- $sql = 'SELECT ';
-
- for ($i = 0; $i < $count; $i++) {
- if ($i > 0) $sql .= ', ';
-
- $sql .= 'floor(rand() * count(id))';
- }
- $sql .= ' FROM `album`';
-
- $db_results = Dba::query($sql);
-
- $sql = '';
-
- $row = Dba::fetch_assoc($db_results);
-
- for ($i = 0; $i < $count; $i++) {
- if ($i > 0) $sql .= ' UNION ';
+function get_random_albums($count='') {
+
+ if (!$count) { $count = 5; }
- $sql .= "SELECT * FROM (SELECT `id` FROM `album` LIMIT " . $row[$i] . ",1) t".$i;
- }
+ $count = Dba::escape($count);
- $db_results = Dba::query($sql);
+ // We avoid a table scan by using the id index and then using a rand to pick a row #
+ $sql = "SELECT `id` FROM `album`";
+ $db_results = Dba::query($sql);
- $results = array();
+ while ($r = Dba::fetch_assoc($db_results)) {
+ $albums[] = $r['id'];
+ }
+
+ $total = count($albums);
- while($row = Dba::fetch_row($db_results)) {
- $results[] = $row[0];
- }
+ if ($total < ($count+2)) { return array(); }
+
+ for ($i=0; $i <= $count; $i++) {
+ $tries++;
+ $record = rand(0,$total);
+ if (isset($results[$record]) || !$albums[$record]) { $i--; continue; }
+ else {
+ $results[$record] = $albums[$record];
+ }
+ if ($tries > 50) { return array(); }
+ } // end for
+
+ return $results;
- return $results;
} // get_random_albums
+
?>