account for missing tags
This commit is contained in:
		
							parent
							
								
									f7feb4075a
								
							
						
					
					
						commit
						ef82d5f1a1
					
				| @ -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) { | ||||||
|  | |||||||
| @ -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,27 +919,34 @@ class Image | |||||||
|             && !is_null($limit) |             && !is_null($limit) | ||||||
|         ) { |         ) { | ||||||
|             $in = $positive_tag_count === 1 ? "IN" : "NOT IN"; |             $in = $positive_tag_count === 1 ? "IN" : "NOT IN"; | ||||||
|  |             // doing this inline is 100x slower?
 | ||||||
|  |             $tag_array = self::tag_or_wildcard_to_ids($tag_conditions[0]->tag); | ||||||
|  |             if (count($tag_array) == 0) { | ||||||
|  |                 if ($positive_tag_count == 1) { | ||||||
|  |                     $query = new Querylet("SELECT images.* FROM images WHERE 1=0"); | ||||||
|  |                 } else { | ||||||
|  |                     $query = new Querylet("SELECT images.* FROM images WHERE 1=1"); | ||||||
|  |                 } | ||||||
|  |             } else { | ||||||
|  |                 $set = implode(', ', $tag_array); | ||||||
|                 $query = new Querylet(" |                 $query = new Querylet(" | ||||||
|                     SELECT images.* |                     SELECT images.* | ||||||
|                     FROM images INNER JOIN ( |                     FROM images INNER JOIN ( | ||||||
|                         SELECT it.image_id |                         SELECT it.image_id | ||||||
|                         FROM image_tags it |                         FROM image_tags it | ||||||
|                     WHERE it.tag_id $in ( |                         WHERE it.tag_id $in ($set) | ||||||
|                         SELECT id |  | ||||||
|                         FROM tags |  | ||||||
|                         WHERE LOWER(tag) LIKE LOWER(:tag) |  | ||||||
|                     ) |  | ||||||
|                         ORDER BY it.image_id DESC |                         ORDER BY it.image_id DESC | ||||||
|                         LIMIT :limit OFFSET :offset |                         LIMIT :limit OFFSET :offset | ||||||
|                     ) a on a.image_id = images.id |                     ) a on a.image_id = images.id | ||||||
|                     ORDER BY images.id DESC; |                     ORDER BY images.id DESC; | ||||||
|             ", ["tag"=>$tag_conditions[0]->tag, "limit"=>$limit, "offset"=>$offset]);
 |                 ", ["limit"=>$limit, "offset"=>$offset]);
 | ||||||
|                 // don't do these at the image level because
 |                 // don't do these at the image level because
 | ||||||
|                 // we did them at the image_tags level
 |                 // we did them at the image_tags level
 | ||||||
|                 $order = null; |                 $order = null; | ||||||
|                 $limit = null; |                 $limit = null; | ||||||
|                 $offset = 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
 | ||||||
|         else { |         else { | ||||||
| @ -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) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user