<commit_before><?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

class Search_Query implements Search_Query_Interface
{
	private $objectList;
	private $expr;
	private $sortOrder;
	private $start = 0;
	private $count = 50;
	private $weightCalculator = null;
	private $identifierFields = null;

	private $subQueries = array();
	private $facets = [];
	private $foreignQueries = [];
	private $transformations = [];

	function __construct($query = null)
	{
		$this->expr = new Search_Expr_And(array());

		if ($query) {
			$this->filterContent($query);
		}
	}

	function __clone()
	{
		$this->expr = clone $this->expr;
	}

	function setIdentifierFields(array $fields)
	{
		$this->identifierFields = $fields;
	}

	function addObject($type, $objectId)
	{
		if (is_null($this->objectList)) {
			$this->objectList = new Search_Expr_Or(array());
			$this->expr->addPart($this->objectList);
		}

		$type = new Search_Expr_Token($type, 'identifier', 'object_type');
		$objectId = new Search_Expr_Token($objectId, 'identifier', 'object_id');

		$this->objectList->addPart(new Search_Expr_And(array($type, $objectId)));
	}

	function filterContent($query, $field = 'contents')
	{
		$this->addPart($query, 'plaintext', $field);
	}

	function filterIdentifier($query, $field)
	{
		$this->addPart(new Search_Expr_Token($query), 'identifier', $field);
	}

	function filterType($types)
	{
		if (is_array($types)) {
			foreach ($types as $type) {
				$tokens[] = new Search_Expr_Token($type);
			}
			$or =  new Search_Expr_Or($tokens);
			$this->addPart($or, 'identifier', 'object_type');
		} else {
			$token = new Search_Expr_Token($types);
			$this->addPart($token, 'identifier', 'object_type');
		}
	}

	function filterMultivalue($query, $field)
	{
		$this->addPart($query, 'multivalue', $field);
	}

	function filterContributors($query)
	{
		$this->filterMultivalue($query, 'contributors');
	}

	function filterCategory($query, $deep = false)
	{
		$this->filterMultivalue($query, $deep ? 'deep_categories' : 'categories');
	}

	function filterTags($query)
	{
		$this->filterMultivalue($query, 'freetags');
	}

	function filterLanguage($query)
	{
		$this->addPart($query, 'identifier', 'language');
	}

	function filterPermissions(array $groups)
	{
		global $user;

		$tokens = array();
		foreach ($groups as $group) {
			$tokens[] = new Search_Expr_Token($group);
		}

		$or = new Search_Expr_Or($tokens);

		$sub = $this->getSubQuery('permissions');
		$sub->filterMultivalue($or, 'allowed_groups');
		$sub->filterMultivalue($user, 'allowed_users');
	}

	/**
	 * Sets up Zend search term for a date range
	 *
	 * @param string	$from date - a unix timestamp or most date strings such as 'now', '2011-11-21', 'last week' etc
	 * @param string	$to date as with $from (other examples: '-42 days', 'last tuesday')<commit_msg>[ENH] Regarding r52294, probably does not make a difference but looks more solid this way<commit_after><?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

class Search_Query implements Search_Query_Interface
{
	private $objectList;
	private $expr;
	private $sortOrder;
	private $start = 0;
	private $count = 50;
	private $weightCalculator = null;
	private $identifierFields = null;

	private $subQueries = array();
	private $facets = [];
	private $foreignQueries = [];
	private $transformations = [];

	function __construct($query = null)
	{
		$this->expr = new Search_Expr_And(array());

		if ($query) {
			$this->filterContent($query);
		}
	}

	function __clone()
	{
		$this->expr = clone $this->expr;
	}

	function setIdentifierFields(array $fields)
	{
		$this->identifierFields = $fields;
	}

	function addObject($type, $objectId)
	{
		if (is_null($this->objectList)) {
			$this->objectList = new Search_Expr_Or(array());
			$this->expr->addPart($this->objectList);
		}

		$type = new Search_Expr_Token($type, 'identifier', 'object_type');
		$objectId = new Search_Expr_Token($objectId, 'identifier', 'object_id');

		$this->objectList->addPart(new Search_Expr_And(array($type, $objectId)));
	}

	function filterContent($query, $field = 'contents')
	{
		$this->addPart($query, 'plaintext', $field);
	}

	function filterIdentifier($query, $field)
	{
		$this->addPart(new Search_Expr_Token($query), 'identifier', $field);
	}

	function filterType($types)
	{
		if (is_array($types)) {
			foreach ($types as $type) {
				$tokens[] = new Search_Expr_Token($type);
			}
			$or =  new Search_Expr_Or($tokens);
			$this->addPart($or, 'identifier', 'object_type');
		} else {
			$token = new Search_Expr_Token($types);
			$this->addPart($token, 'identifier', 'object_type');
		}
	}

	function filterMultivalue($query, $field)
	{
		$this->addPart($query, 'multivalue', $field);
	}

	function filterContributors($query)
	{
		$this->filterMultivalue($query, 'contributors');
	}

	function filterCategory($query, $deep = false)
	{
		$this->filterMultivalue($query, $deep ? 'deep_categories' : 'categories');
	}

	function filterTags($query)
	{
		$this->filterMultivalue($query, 'freetags');
	}

	function filterLanguage($query)
	{
		$this->addPart($query, 'identifier', 'language');
	}

	function filterPermissions(array $groups)
	{
		global $user;

		$tokens = array();
		foreach ($groups as $group) {
			$tokens[] = new Search_Expr_Token($group);
		}

		$or = new Search_Expr_Or($tokens);

		if ($user) {
			$sub = $this->getSubQuery('permissions');
			$sub->filterMultivalue($or, 'allowed_groups');
			$sub->filterMultivalue($user, 'allowed_users');
		} else {
			$this->addPart($or, 'multivalue', 'allowed_groups');
		}
	}

	/**
	 * Sets up Zend search term for a date range
	 *
	 * @param string	$from date - a unix timestamp or most date strings such as 'now', '2011-11-21', 'last week' etc
	 * @param string	$to date as with $from (other examples: '-42 days', 'last tuesday')
{'fraction_non_alphanumeric': 0.15923361777491546, 'fraction_numerical': 0.007567219449364031, 'mean_word_length': 4.606498194945848, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, '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': '4479741', 'n_tokens_mistral': 2264, 'n_tokens_neox': 2050, 'n_words': 497}