Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[WIP] limit=X meta-tag, see #1153 #1269

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions core/imageboard/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class SearchParameters
/** @var ImgCondition[] */
public array $img_conditions = [];
public ?string $order = null;
public ?int $limit = null;

/**
* Turn a human input string into a an abstract search query
Expand All @@ -87,9 +88,10 @@ public static function from_terms(array $terms): SearchParameters
$stpen = 0; // search term parse event number
foreach (array_merge([null], $terms) as $term) {
$stpe = send_event(new SearchTermParseEvent($stpen++, $term, $terms));
$sp->order ??= $stpe->order;
$sp->img_conditions = array_merge($sp->img_conditions, $stpe->img_conditions);
$sp->tag_conditions = array_merge($sp->tag_conditions, $stpe->tag_conditions);
$sp->order ??= $stpe->order;
$sp->limit ??= $stpe->limit;
}

$sp->order ??= "images.".$config->get_string(IndexConfig::ORDER);
Expand Down Expand Up @@ -134,6 +136,9 @@ private static function find_images_internal(int $start = 0, ?int $limit = null,
}

$params = SearchParameters::from_terms($tags);
if ($params->limit !== null) {
$limit = $limit ? min($params->limit, $limit) : $params->limit;
}
$querylet = self::build_search_querylet($params, $limit, $start);
return $database->get_all_iterable($querylet->sql, $querylet->variables);
}
Expand Down Expand Up @@ -453,7 +458,9 @@ public static function build_search_querylet(

if (!is_null($limit)) {
$query->append(new Querylet(" LIMIT :limit ", ["limit" => $limit]));
$query->append(new Querylet(" OFFSET :offset ", ["offset" => $offset]));
if (!is_null($offset)) {
$query->append(new Querylet(" OFFSET :offset ", ["offset" => $offset]));
}
}

return $query;
Expand Down
1 change: 1 addition & 0 deletions ext/index/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SearchTermParseEvent extends Event
/** @var TagCondition[] */
public array $tag_conditions = [];
public ?string $order = null;
public ?int $limit = null;

/**
* @param string[] $context
Expand Down
4 changes: 3 additions & 1 deletion ext/index/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,12 @@ public function onSearchTermParse(SearchTermParseEvent $event): void
// recommended to change homepage to "post/list/order:dailyshuffle/1"
$seed = (int)date("Ymd");
$event->order = $database->seeded_random($seed, "images.id");
} elseif (preg_match("/^limit[=:](\d+)$/i", $event->term, $matches)) {
$event->limit = intval($matches[1]);
}

// If we've reached this far, and nobody else has done anything with this term, then treat it as a tag
if ($event->order === null && $event->img_conditions == [] && $event->tag_conditions == []) {
if ($event->order === null && $event->limit === null && $event->img_conditions == [] && $event->tag_conditions == []) {
assert(is_string($event->term));
$event->add_tag_condition(new TagCondition($event->term, !$event->negative));
}
Expand Down