Skip to content

Commit

Permalink
[tag_list] make search refining work again, see #1351
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Dec 14, 2024
1 parent 6eed6f0 commit 1f0ad73
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 71 deletions.
49 changes: 12 additions & 37 deletions ext/tag_list/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ public function onDisplayingImage(DisplayingImageEvent $event): void
if ($config->get_int(TagListConfig::LENGTH) > 0) {
$type = $config->get_string(TagListConfig::IMAGE_TYPE);
if ($type == TagListConfig::TYPE_TAGS || $type == TagListConfig::TYPE_BOTH) {
if (Extension::is_enabled(TagCategoriesInfo::KEY) and $config->get_bool(TagCategoriesConfig::SPLIT_ON_VIEW)) {
$this->add_split_tags_block($page, $event->image);
} else {
$this->add_tags_block($page, $event->image);
}
$this->add_tags_block($page, $event->image);
}
if ($type == TagListConfig::TYPE_RELATED || $type == TagListConfig::TYPE_BOTH) {
$this->add_related_block($page, $event->image);
Expand Down Expand Up @@ -159,41 +155,23 @@ private function add_related_block(Page $page, Image $image): void
}
}

private function add_split_tags_block(Page $page, Image $image): void
{
global $database;

$query = "
SELECT tags.tag, tags.count
FROM tags, image_tags
WHERE tags.id = image_tags.tag_id
AND image_tags.image_id = :image_id
ORDER BY tags.count DESC
";
$args = ["image_id" => $image->id];

$tags = $database->get_all($query, $args);
if (count($tags) > 0) {
$this->theme->display_split_related_block($page, $tags);
}
}

private function add_tags_block(Page $page, Image $image): void
{
global $database;
global $config, $database;

$query = "
$tags = $database->get_all("
SELECT tags.tag, tags.count
FROM tags, image_tags
WHERE tags.id = image_tags.tag_id
AND image_tags.image_id = :image_id
ORDER BY tags.count DESC
";
$args = ["image_id" => $image->id];

$tags = $database->get_all($query, $args);
", ["image_id" => $image->id]);
if (count($tags) > 0) {
$this->theme->display_related_block($page, $tags, "Tags");
if (Extension::is_enabled(TagCategoriesInfo::KEY) and $config->get_bool(TagCategoriesConfig::SPLIT_ON_VIEW)) {
$this->theme->display_split_related_block($page, $tags);
} else {
$this->theme->display_related_block($page, $tags, "Tags");
}
}
}

Expand Down Expand Up @@ -240,18 +218,16 @@ private function add_popular_block(Page $page): void
*/
private function add_refine_block(Page $page, array $search): void
{
global $config;
global $cache, $config, $database;

if (count($search) > 5) {
return;
}

$wild_tags = $search;

$related_tags = self::get_related_tags($search, $config->get_int(TagListConfig::LENGTH));

if (!empty($related_tags)) {
$this->theme->display_refine_block($page, $related_tags, $wild_tags);
$this->theme->display_refine_block($page, $related_tags, $search);
}
}

Expand All @@ -263,7 +239,6 @@ public static function get_related_tags(array $search, int $limit): array
{
global $cache, $database;

$wild_tags = $search;
$cache_key = "related_tags:" . md5(Tag::implode($search));
$related_tags = $cache->get($cache_key);

Expand All @@ -272,7 +247,7 @@ public static function get_related_tags(array $search, int $limit): array

$starting_tags = [];
$tags_ok = true;
foreach ($wild_tags as $tag) {
foreach ($search as $tag) {
if ($tag[0] == "-" || str_starts_with($tag, "tagme")) {
continue;
}
Expand Down
77 changes: 43 additions & 34 deletions ext/tag_list/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MicroHTML\HTMLElement;

use function MicroHTML\{A, BR, rawHTML, emptyHTML, TABLE, COLGROUP, COL, THEAD, TH, TR, TD, SPAN};
use function MicroHTML\joinHTML;

class TagListTheme extends Themelet
{
Expand Down Expand Up @@ -85,16 +86,17 @@ public function display_split_related_block(Page $page, array $tag_infos): void

/**
* @param array<array{tag: string, count: int}> $tag_infos
* @param string[] $search
*/
private function get_tag_list_html(array $tag_infos, string $sort): HTMLElement
private function get_tag_list_html(array $tag_infos, string $sort, array $search = []): HTMLElement
{
if ($sort == TagListConfig::SORT_ALPHABETICAL) {
asort($tag_infos);
}

$table = $this->get_tag_list_preamble();
foreach ($tag_infos as $row) {
$table->appendChild(self::build_tag_row($row));
$table->appendChild(self::build_tag_row($row, $search));
}
return $table;
}
Expand Down Expand Up @@ -144,7 +146,8 @@ public function display_refine_block(Page $page, array $tag_infos, array $search
$main_html = emptyHTML(
$this->get_tag_list_html(
$tag_infos,
$config->get_string(TagListConfig::POPULAR_SORT)
$config->get_string(TagListConfig::POPULAR_SORT),
$search
),
rawHTML("&nbsp;"),
BR(),
Expand All @@ -156,8 +159,9 @@ public function display_refine_block(Page $page, array $tag_infos, array $search

/**
* @param array{tag: string, count: int} $row
* @param string[] $search
*/
public function build_tag_row(array $row): HTMLElement
protected function build_tag_row(array $row, array $search = []): HTMLElement
{
global $config;

Expand Down Expand Up @@ -192,7 +196,11 @@ public function build_tag_row(array $row): HTMLElement

$tr->appendChild(TD(
["class" => "tag_name_cell"],
$this->build_tag($tag, show_underscores: false, show_category: false)
emptyHTML(
$this->build_tag($tag, show_underscores: false, show_category: false),
" ",
$search ? $this->ars($search, $tag) : emptyHTML(),
)
));

if ($config->get_bool(TagListConfig::SHOW_NUMBERS)) {
Expand All @@ -208,56 +216,57 @@ public function build_tag_row(array $row): HTMLElement
/**
* @param string[] $tags
*/
protected function ars(string $tag, array $tags): string
protected function ars(array $tags, string $tag): HTMLElement
{
// FIXME: a better fix would be to make sure the inputs are correct
$tag = strtolower($tag);
$tags = array_map("strtolower", $tags);
$html = "";
$html .= " <span class='ars'>(";
$html .= $this->get_add_link($tags, $tag);
$html .= $this->get_remove_link($tags, $tag);
$html .= $this->get_subtract_link($tags, $tag);
$html .= ")</span>";
return $html;
return SPAN(
["class" => "ars"],
joinHTML(
" ",
[
$this->get_add_link($tags, $tag),
$this->get_remove_link($tags, $tag),
$this->get_subtract_link($tags, $tag),
]
),
);
}

/**
* @param string[] $tags
* @param string[] $search
*/
protected function get_remove_link(array $tags, string $tag): string
protected function get_remove_link(array $search, string $tag): ?HTMLElement
{
if (!in_array($tag, $tags) && !in_array("-$tag", $tags)) {
return "";
} else {
$tags = array_diff($tags, [$tag, "-$tag"]);
return "<a href='".search_link($tags)."' title='Remove' rel='nofollow'>R</a>";
if (in_array($tag, $search) || in_array("-$tag", $search)) {
$new_search = array_diff($search, [$tag, "-$tag"]);
return A(["href" => search_link($new_search), "title" => "Remove", "rel" => "nofollow",], "[x]");
}
return null;
}

/**
* @param string[] $tags
* @param string[] $search
*/
protected function get_add_link(array $tags, string $tag): string
protected function get_add_link(array $search, string $tag): ?HTMLElement
{
if (in_array($tag, $tags)) {
return "";
} else {
$tags = array_diff($tags, ["-$tag"]) + [$tag];
return "<a href='".search_link($tags)."' title='Add' rel='nofollow'>A</a>";
if (!in_array($tag, $search)) {
$new_search = array_merge(array_diff($search, ["-$tag"]), [$tag]);
return A(["href" => search_link($new_search), "title" => "Add", "rel" => "nofollow"], "[+]");
}
return null;
}

/**
* @param string[] $tags
* @param string[] $search
*/
protected function get_subtract_link(array $tags, string $tag): string
protected function get_subtract_link(array $search, string $tag): ?HTMLElement
{
if (in_array("-$tag", $tags)) {
return "";
} else {
$tags = array_diff($tags, [$tag]) + ["-$tag"];
return "<a href='".search_link($tags)."' title='Subtract' rel='nofollow'>S</a>";
if (!in_array("-$tag", $search)) {
$search = array_merge(array_diff($search, [$tag]), ["-$tag"]);
return A(["href" => search_link($search), "title" => "Subtract", "rel" => "nofollow"], "[-]");
}
return null;
}
}

0 comments on commit 1f0ad73

Please sign in to comment.