diff --git a/core/imageboard/image.php b/core/imageboard/image.php index a2018db1..dec82aa2 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -134,7 +134,6 @@ class Image } } - private static function find_images_internal(int $start = 0, ?int $limit = null, array $tags=[]): iterable { global $database, $user, $config; @@ -211,23 +210,35 @@ class Image $tag_count = count($tags); if ($tag_count === 0) { + // total number of images in the DB $total = $cache->get("image-count"); if (!$total) { $total = (int)$database->get_one("SELECT COUNT(*) FROM images"); $cache->set("image-count", $total, 600); } } elseif ($tag_count === 1 && !preg_match("/[:=><\*\?]/", $tags[0])) { + // one tag - we can look that up directly + // TODO: one negative tag = (total - tag.count) $total = (int)$database->get_one( "SELECT count FROM tags WHERE LOWER(tag) = LOWER(:tag)", ["tag"=>$tags[0]] ); } else { - if (Extension::is_enabled(RatingsInfo::KEY)) { - $tags[] = "rating:*"; + // complex query + $total = $cache->get("image-count:" . Tag::implode($tags)); + if(!$total) { + if (Extension::is_enabled(RatingsInfo::KEY)) { + $tags[] = "rating:*"; + } + list($tag_conditions, $img_conditions) = self::terms_to_conditions($tags); + $querylet = Image::build_search_querylet($tag_conditions, $img_conditions); + $total = (int)$database->get_one("SELECT COUNT(*) AS cnt FROM ($querylet->sql) AS tbl", $querylet->variables); + if (SPEED_HAX && $total > 5000) { + // when we have a ton of images, the count + // won't change dramatically very often + $cache->set("image-count:" . Tag::implode($tags), $total, 3600); + } } - list($tag_conditions, $img_conditions) = self::terms_to_conditions($tags); - $querylet = Image::build_search_querylet($tag_conditions, $img_conditions); - $total = (int)$database->get_one("SELECT COUNT(*) AS cnt FROM ($querylet->sql) AS tbl", $querylet->variables); } if (is_null($total)) { return 0; diff --git a/ext/index/main.php b/ext/index/main.php index 9218468a..fcb017ec 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -43,26 +43,26 @@ class Index extends Extension $count_search_terms = count($search_terms); try { - #log_debug("index", "Search for ".Tag::implode($search_terms), false, array("terms"=>$search_terms)); + $fast_page_limit = 500; + + if (SPEED_HAX && $page_number > $fast_page_limit && !$user->can("big_search")) { + $this->theme->display_error( + 404, + "Search limit hit", + "Only $fast_page_limit pages of results are searchable - " . + "if you want to find older results, use more specific search terms" + ); + return; + } + $total_pages = Image::count_pages($search_terms); $images = []; + if (SPEED_HAX && $total_pages > $fast_page_limit && !$user->can("big_search")) { + $total_pages = $fast_page_limit; + } + if (SPEED_HAX) { - if (!$user->can("big_search")) { - $fast_page_limit = 500; - if ($total_pages > $fast_page_limit) { - $total_pages = $fast_page_limit; - } - if ($page_number > $fast_page_limit) { - $this->theme->display_error( - 404, - "Search limit hit", - "Only $fast_page_limit pages of results are searchable - " . - "if you want to find older results, use more specific search terms" - ); - return; - } - } if ($count_search_terms === 0 && ($page_number < 10)) { // extra caching for the first few post/list pages $images = $cache->get("post-list:$page_number");