<commit_before><?php namespace Flarum\Tags;

use Flarum\Core\Repositories\UserRepositoryInterface as UserRepository;
use Flarum\Core\Search\SearcherInterface;
use Flarum\Core\Search\GambitAbstract;

class TagGambit extends GambitAbstract
{
    /**
     * The gambit's regex pattern.
     *
     * @var string
     */
    protected $pattern = 'tag:(.+)';

    /**
     * @var \Flarum\Tags\TagRepositoryInterface
     */
    protected $tags;

    /**
     * Instantiate the gambit.
     *
     * @param \Flarum\Tags\TagRepositoryInterface $categories
     */
    public function __construct(TagRepositoryInterface $tags)
    {
        $this->tags = $tags;
    }

    /**
     * Apply conditions to the searcher, given matches from the gambit's
     * regex.
     *
     * @param array $matches The matches from the gambit's regex.
     * @param \Flarum\Core\Search\SearcherInterface $searcher
     * @return void
     */
    public function conditions($matches, SearcherInterface $searcher)
    {
        $slugs = explode(',', trim($matches[1], '"'));

        $searcher->query()->where(function ($query) use ($slugs) {
            foreach ($slugs as $slug) {
                if ($slug === 'uncategorized') {
                    $query->orWhereNotExists(function ($query) {
                        $query->select(app('db')->raw(1))
                              ->from('discussions_tags')
                              ->whereRaw('discussion_id = discussions.id');
                    });
                } else {
                    $id = $this->tags->getIdForSlug($slug);

                    $query->orWhereExists(function ($query) use ($id) {
                        $query->select(app('db')->raw(1))
                              ->from('discussions_tags')
                              ->whereRaw('discussion_id = discussions.id AND tag_id = ?', [$id]);
                    });
                }
            }
        });
    }
}<commit_msg>Update gambit for search API<commit_after><?php namespace Flarum\Tags;

use Flarum\Core\Repositories\UserRepositoryInterface as UserRepository;
use Flarum\Core\Search\SearcherInterface;
use Flarum\Core\Search\GambitAbstract;

class TagGambit extends GambitAbstract
{
    /**
     * The gambit's regex pattern.
     *
     * @var string
     */
    protected $pattern = 'tag:(.+)';

    /**
     * @var \Flarum\Tags\TagRepositoryInterface
     */
    protected $tags;

    /**
     * Instantiate the gambit.
     *
     * @param \Flarum\Tags\TagRepositoryInterface $categories
     */
    public function __construct(TagRepositoryInterface $tags)
    {
        $this->tags = $tags;
    }

    /**
     * Apply conditions to the searcher, given matches from the gambit's
     * regex.
     *
     * @param array $matches The matches from the gambit's regex.
     * @param \Flarum\Core\Search\SearcherInterface $searcher
     * @return void
     */
    protected function conditions(SearcherInterface $searcher, array $matches, $negate)
    {
        $slugs = explode(',', trim($matches[1], '"'));

        // TODO: implement $negate
        $searcher->getQuery()->where(function ($query) use ($slugs) {
            foreach ($slugs as $slug) {
                if ($slug === 'untagged') {
                    $query->orWhereNotExists(function ($query) {
                        $query->select(app('db')->raw(1))
                              ->from('discussions_tags')
                              ->whereRaw('discussion_id = discussions.id');
                    });
                } else {
                    $id = $this->tags->getIdForSlug($slug);

                    $query->orWhereExists(function ($query) use ($id) {
                        $query->select(app('db')->raw(1))
                              ->from('discussions_tags')
                              ->whereRaw('discussion_id = discussions.id AND tag_id = ?', [$id]);
                    });
                }
            }
        });
    }
}
{'fraction_non_alphanumeric': 0.134479271991911, 'fraction_numerical': 0.0015166835187057635, 'mean_word_length': 1.730848861283644, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7084540', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1079, 'n_words': 217}