1
0
Fork 0
mirror of https://github.com/Yetangitu/ampache synced 2025-10-05 19:41:55 +02:00

Be smarter about getting random albums with art

Just ask the database for what we want instead of asking for more
results, sifting through them, and hoping that we get enough
qualifying results to fulfill the request.

Also drop Random::album(), since it's duplicate code and it makes more
sense for this to live in Album.
This commit is contained in:
Paul Arthur 2013-07-22 16:35:27 -04:00
parent 591a0b17c3
commit c952ca6fa3
4 changed files with 20 additions and 33 deletions

View file

@ -418,30 +418,32 @@ class Album extends database_object {
} // update
/**
* get_random_albums
* This returns a random number of albums from the catalogs
* this is used by the index to return some 'potential' albums to play
* get_random
*
* This returns a number of random albums.
*/
public static function get_random_albums($count=6) {
public static function get_random($count = 1, $with_art = false) {
$results = false;
$sql = 'SELECT `id` FROM `album` ORDER BY RAND() LIMIT ' . ($count*2);
if ($with_art) {
$sql = 'SELECT `album`.`id` FROM `album` LEFT JOIN `image` ' .
"ON (`image`.`object_type` = 'album' AND " .
'`image`.`object_id` = `album`.`id`) ' .
'WHERE `image`.`id` IS NOT NULL ';
}
else {
$sql = 'SELECT `id` FROM `album` ';
}
$sql .= 'ORDER BY RAND() LIMIT ' . intval($count);
$db_results = Dba::read($sql);
while ($row = Dba::fetch_assoc($db_results)) {
$art = new Art($row['id'], 'album');
$art->get_db();
if ($art->raw) {
$results[] = $row['id'];
}
}
if (count($results) < $count) { return false; }
$results = array_slice($results, 0, $count);
return $results;
} // get_random_albums
}
} //end of album class

View file

@ -44,21 +44,6 @@ class Random implements media {
} // constructor
/**
* album
* This returns the ID of a random album, nothing special
*/
public static function album() {
$sql = "SELECT `id` FROM `album` ORDER BY RAND() LIMIT 1";
$db_results = Dba::read($sql);
$results = Dba::fetch_assoc($db_results);
return $results['id'];
} // album
/**
* artist
* This returns the ID of a random artist, nothing special here for now

View file

@ -27,7 +27,7 @@ if (!defined('AJAX_INCLUDE')) { exit; }
switch ($_REQUEST['action']) {
case 'random_albums':
$albums = Album::get_random_albums('6');
$albums = Album::get_random(6, true);
if (count($albums) AND is_array($albums)) {
ob_start();
require_once Config::get('prefix') . '/templates/show_random_albums.inc.php';

View file

@ -27,7 +27,7 @@ if (!defined('AJAX_INCLUDE')) { exit; }
switch ($_REQUEST['action']) {
case 'album':
$album_id = Random::album();
$album_id = Album::get_random();
// If we don't get anything stop
if (!$album_id) { $results['rfc3514'] = '0x1'; break; }