Allow SearchTermParseEvent to have a bit more control over results
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
This commit is contained in:
parent
4ba3af7926
commit
12f0bc3a81
3 changed files with 43 additions and 34 deletions
|
@ -283,38 +283,11 @@ class Image
|
||||||
* Turn a bunch of strings into a bunch of TagCondition
|
* Turn a bunch of strings into a bunch of TagCondition
|
||||||
* and ImgCondition objects
|
* and ImgCondition objects
|
||||||
*/
|
*/
|
||||||
$stpe = send_event(new SearchTermParseEvent($stpen++, null, $terms));
|
foreach (array_merge([null], $terms) as $term) {
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
$stpe = send_event(new SearchTermParseEvent($stpen++, $term, $terms));
|
$stpe = send_event(new SearchTermParseEvent($stpen++, $term, $terms));
|
||||||
if ($stpe->order) {
|
$order ??= $stpe->order;
|
||||||
$order = $stpe->order;
|
$img_conditions = array_merge($img_conditions, $stpe->img_conditions);
|
||||||
} elseif (!empty($stpe->querylets)) {
|
$tag_conditions = array_merge($tag_conditions, $stpe->tag_conditions);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return [$tag_conditions, $img_conditions, $order];
|
return [$tag_conditions, $img_conditions, $order];
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,23 +12,48 @@ class SearchTermParseEvent extends Event
|
||||||
{
|
{
|
||||||
public int $id = 0;
|
public int $id = 0;
|
||||||
public ?string $term = null;
|
public ?string $term = null;
|
||||||
|
public bool $positive = true;
|
||||||
/** @var string[] */
|
/** @var string[] */
|
||||||
public array $context = [];
|
public array $context = [];
|
||||||
/** @var Querylet[] */
|
/** @var ImgCondition[] */
|
||||||
public array $querylets = [];
|
public array $img_conditions = [];
|
||||||
|
/** @var TagCondition[] */
|
||||||
|
public array $tag_conditions = [];
|
||||||
public ?string $order = null;
|
public ?string $order = null;
|
||||||
|
|
||||||
public function __construct(int $id, string $term=null, array $context=[])
|
public function __construct(int $id, string $term=null, array $context=[])
|
||||||
{
|
{
|
||||||
parent::__construct();
|
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->id = $id;
|
||||||
|
$this->positive = $positive;
|
||||||
$this->term = $term;
|
$this->term = $term;
|
||||||
$this->context = $context;
|
$this->context = $context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function add_querylet(Querylet $q)
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -257,5 +257,16 @@ class Index extends Extension
|
||||||
$seed = date("Ymd");
|
$seed = date("Ymd");
|
||||||
$event->order = "RAND($seed)";
|
$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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue