diff --git a/core/imageboard/search.php b/core/imageboard/search.php index 2b9dfff5..7c526e95 100644 --- a/core/imageboard/search.php +++ b/core/imageboard/search.php @@ -104,6 +104,26 @@ class Search } } + /** + * Get a specific set of images, in the order that the set specifies, + * with all the search stuff (rating filters etc) taken into account + * + * @param int[] $ids + * @return Image[] + */ + public static function get_images(array $ids): array + { + $visible_images = []; + foreach(Search::find_images(tags: ["id=" . implode(",", $ids)]) as $image) { + $visible_images[$image->id] = $image; + } + $visible_ids = array_keys($visible_images); + + $visible_popular_ids = array_filter($ids, fn ($id) => in_array($id, $visible_ids)); + $images = array_map(fn ($id) => $visible_images[$id], $visible_popular_ids); + return $images; + } + /* * Image-related utility functions */ diff --git a/core/tests/SearchTest.php b/core/tests/SearchTest.php index 303072f7..c95197d7 100644 --- a/core/tests/SearchTest.php +++ b/core/tests/SearchTest.php @@ -487,4 +487,19 @@ class SearchTest extends ShimmiePHPUnitTestCase path: ["general", "some_positives"], ); } + + /** + * get_images + */ + #[Depends('testUpload')] + public function test_get_images() + { + $image_ids = $this->testUpload(); + + $res = Search::get_images($image_ids); + $this->assertGreaterThan($res[0]->id, $res[1]->id); + + $res = Search::get_images(array_reverse($image_ids)); + $this->assertLessThan($res[0]->id, $res[1]->id); + } }