allow randomness to be limited, because sql's OFFSET N is O(n)

This commit is contained in:
Shish 2019-07-25 11:07:05 +01:00
parent e6411c32aa
commit 9341c408b9
2 changed files with 5 additions and 2 deletions

View file

@ -101,12 +101,15 @@ class Image
return ($row ? new Image($row) : null); return ($row ? new Image($row) : null);
} }
public static function by_random(array $tags=[]): ?Image public static function by_random(array $tags=[], int $limit_range=0): ?Image
{ {
$max = Image::count_images($tags); $max = Image::count_images($tags);
if ($max < 1) { if ($max < 1) {
return null; return null;
} // From Issue #22 - opened by HungryFeline on May 30, 2011. } // From Issue #22 - opened by HungryFeline on May 30, 2011.
if ($max > $limit_range) {
$max = $limit_range;
}
$rand = mt_rand(0, $max-1); $rand = mt_rand(0, $max-1);
$set = Image::find_images($rand, 1, $tags); $set = Image::find_images($rand, 1, $tags);
if (count($set) > 0) { if (count($set) > 0) {

View file

@ -395,7 +395,7 @@ class TagList extends Extension
} }
$h_tag = html_escape($row['tag']); $h_tag = html_escape($row['tag']);
$link = $this->tag_link($row['tag']); $link = $this->tag_link($row['tag']);
$image = Image::by_random([$row['tag']]); $image = Image::by_random([$row['tag']], 100);
if (is_null($image)) { if (is_null($image)) {
continue; continue;
} // one of the popular tags has no images } // one of the popular tags has no images