account for missing tags

This commit is contained in:
Shish 2020-02-05 01:26:18 +00:00
parent f7feb4075a
commit ef82d5f1a1
2 changed files with 39 additions and 34 deletions

View File

@ -86,7 +86,7 @@ class BaseThemelet
$page->add_html_header("<link rel='first' href='".make_http(make_link($base.'/1', $query))."'>"); $page->add_html_header("<link rel='first' href='".make_http(make_link($base.'/1', $query))."'>");
if ($page_number < $total_pages) { if ($page_number < $total_pages) {
$page->add_html_header("<link rel='prefetch' href='".make_http(make_link($base.'/'.($page_number+1), $query))."'>"); # $page->add_html_header("<link rel='prefetch' href='".make_http(make_link($base.'/'.($page_number+1), $query))."'>");
$page->add_html_header("<link rel='next' href='".make_http(make_link($base.'/'.($page_number+1), $query))."'>"); $page->add_html_header("<link rel='next' href='".make_http(make_link($base.'/'.($page_number+1), $query))."'>");
} }
if ($page_number > 1) { if ($page_number > 1) {

View File

@ -851,6 +851,16 @@ class Image
return $tmpl; return $tmpl;
} }
private static function tag_or_wildcard_to_ids(string $tag): array
{
global $database;
$sq = "SELECT id FROM tags WHERE LOWER(tag) LIKE LOWER(:tag)";
if ($database->get_driver_name() === DatabaseDriver::SQLITE) {
$sq .= "ESCAPE '\\'";
}
return $database->get_col($sq, ["tag" => Tag::sqlify($tag)]);
}
/** /**
* #param string[] $terms * #param string[] $terms
*/ */
@ -909,26 +919,33 @@ class Image
&& !is_null($limit) && !is_null($limit)
) { ) {
$in = $positive_tag_count === 1 ? "IN" : "NOT IN"; $in = $positive_tag_count === 1 ? "IN" : "NOT IN";
$query = new Querylet(" // doing this inline is 100x slower?
SELECT images.* $tag_array = self::tag_or_wildcard_to_ids($tag_conditions[0]->tag);
FROM images INNER JOIN ( if (count($tag_array) == 0) {
SELECT it.image_id if ($positive_tag_count == 1) {
FROM image_tags it $query = new Querylet("SELECT images.* FROM images WHERE 1=0");
WHERE it.tag_id $in ( } else {
SELECT id $query = new Querylet("SELECT images.* FROM images WHERE 1=1");
FROM tags }
WHERE LOWER(tag) LIKE LOWER(:tag) } else {
) $set = implode(', ', $tag_array);
ORDER BY it.image_id DESC $query = new Querylet("
LIMIT :limit OFFSET :offset SELECT images.*
) a on a.image_id = images.id FROM images INNER JOIN (
ORDER BY images.id DESC; SELECT it.image_id
", ["tag"=>$tag_conditions[0]->tag, "limit"=>$limit, "offset"=>$offset]); FROM image_tags it
// don't do these at the image level because WHERE it.tag_id $in ($set)
// we did them at the image_tags level ORDER BY it.image_id DESC
$order = null; LIMIT :limit OFFSET :offset
$limit = null; ) a on a.image_id = images.id
$offset = null; ORDER BY images.id DESC;
", ["limit"=>$limit, "offset"=>$offset]);
// don't do these at the image level because
// we did them at the image_tags level
$order = null;
$limit = null;
$offset = null;
}
} }
// more than one positive tag, or more than zero negative tags // more than one positive tag, or more than zero negative tags
@ -938,19 +955,7 @@ class Image
$negative_tag_id_array = []; $negative_tag_id_array = [];
foreach ($tag_conditions as $tq) { foreach ($tag_conditions as $tq) {
$sq = " $tag_ids = self::tag_or_wildcard_to_ids($tq->tag);
SELECT id
FROM tags
WHERE LOWER(tag) LIKE LOWER(:tag)
";
if ($database->get_driver_name() === DatabaseDriver::SQLITE) {
$sq .= "ESCAPE '\\'";
}
$tag_ids = $database->get_col(
$sq,
["tag" => Tag::sqlify($tq->tag)]
);
$tag_count = count($tag_ids); $tag_count = count($tag_ids);
if ($tq->positive) { if ($tq->positive) {