Skip to content

Commit

Permalink
Allow SearchTermParseEvent to have a bit more control over results
Browse files Browse the repository at this point in the history
Rather than "add querylet or do nothing", moving more code into the
event means that event handlers are able to add a positive or negative
querylet, add a positive or negative tag, or do nothing

This means that events can respond to the `null` search term by adding a
tag, which would be useful for #917
  • Loading branch information
shish committed May 25, 2023
1 parent 4ba3af7 commit 12f0bc3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
35 changes: 4 additions & 31 deletions core/imageboard/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,38 +283,11 @@ private static function terms_to_conditions(array $terms): array
* Turn a bunch of strings into a bunch of TagCondition
* and ImgCondition objects
*/
$stpe = send_event(new SearchTermParseEvent($stpen++, null, $terms));
if ($stpe->order) {
$order = $stpe->order;
} elseif (!empty($stpe->querylets)) {
foreach ($stpe->querylets as $querylet) {
$img_conditions[] = new ImgCondition($querylet, true);
}
}

foreach ($terms as $term) {
$positive = true;
if (is_string($term) && !empty($term) && ($term[0] == '-')) {
$positive = false;
$term = substr($term, 1);
}
if (strlen($term) === 0) {
continue;
}

foreach (array_merge([null], $terms) as $term) {
$stpe = send_event(new SearchTermParseEvent($stpen++, $term, $terms));
if ($stpe->order) {
$order = $stpe->order;
} elseif (!empty($stpe->querylets)) {
foreach ($stpe->querylets as $querylet) {
$img_conditions[] = new ImgCondition($querylet, $positive);
}
} else {
// if the whole match is wild, skip this
if (str_replace("*", "", $term) != "") {
$tag_conditions[] = new TagCondition($term, $positive);
}
}
$order ??= $stpe->order;
$img_conditions = array_merge($img_conditions, $stpe->img_conditions);
$tag_conditions = array_merge($tag_conditions, $stpe->tag_conditions);
}
return [$tag_conditions, $img_conditions, $order];
}
Expand Down
31 changes: 28 additions & 3 deletions ext/index/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,48 @@ class SearchTermParseEvent extends Event
{
public int $id = 0;
public ?string $term = null;
public bool $positive = true;
/** @var string[] */
public array $context = [];
/** @var Querylet[] */
public array $querylets = [];
/** @var ImgCondition[] */
public array $img_conditions = [];
/** @var TagCondition[] */
public array $tag_conditions = [];
public ?string $order = null;

public function __construct(int $id, string $term=null, array $context=[])
{
parent::__construct();

if ($term == "-" || $term == "*") {
throw new SearchTermParseException("'$term' is not a valid search term");
}

$positive = true;
if (is_string($term) && !empty($term) && ($term[0] == '-')) {
$positive = false;
$term = substr($term, 1);
}

$this->id = $id;
$this->positive = $positive;
$this->term = $term;
$this->context = $context;
}

public function add_querylet(Querylet $q)
{
$this->querylets[] = $q;
$this->add_img_condition(new ImgCondition($q, $this->positive));
}

public function add_img_condition(ImgCondition $c)
{
$this->img_conditions[] = $c;
}

public function add_tag_condition(TagCondition $c)
{
$this->tag_conditions[] = $c;
}
}

Expand Down
11 changes: 11 additions & 0 deletions ext/index/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,16 @@ public function onSearchTermParse(SearchTermParseEvent $event)
$seed = date("Ymd");
$event->order = "RAND($seed)";
}

// 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 == []) {
$event->add_tag_condition(new TagCondition($event->term, $event->positive));
}
}

public function get_priority(): int
{
// we want to turn a search term into a TagCondition only if nobody did anything else with that term
return 95;
}
}

0 comments on commit 12f0bc3

Please sign in to comment.