<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$

// Controller to process requests from the custom search plugin using the list plugin to display results
// Refactored from customsearch_ajax.php for Tiki


class Services_Search_CustomSearchController
{
	private $textranges = array();
	private $dateranges = array();

	function setUp()
	{
		Services_Exception_Disabled::check('wikiplugin_list');
		Services_Exception_Disabled::check('wikiplugin_customsearch');
		Services_Exception_Disabled::check('feature_search');
	}

	function action_customsearch($input)
	{
		global $prefs;

		$this->textranges = array();
		$this->dateranges = array();

		$cachelib = TikiLib::lib('cache');
		$definition = $input->definition->word();
		if (empty($definition) || ! $definition = $cachelib->getSerialized($definition, 'customsearch')) {
			throw new Services_Exception(tra('Unfortunately, the search cache has expired. Please reload the page to start over.'));
		}

		$query = $definition['query'];
		$formatter = $definition['formatter'];
		$facetsBuilder = $definition['facets'];

		$adddata = json_decode($input->adddata->text(), true);

		$dataappend = array();

		$recalllastsearch = $input->recalllastsearch->int() ? true : false;

		$id = $input->searchid->text();
		if (empty($id)) {
			$id = '0';
		}
		// setup AJAX pagination
		$offset_jsvar = "customsearch_$id.offset";
		$onclick = "$('#customsearch_$id').submit();return false;";
		$dataappend['pagination'] = "{pagination offset_jsvar=\"$offset_jsvar\" onclick=\"$onclick\"}";

		if ($recalllastsearch && isset($_SESSION["customsearch_$id"])) {
			unset($_SESSION["customsearch_$id"]);
		}
		if ($input->sort_mode->text()) {
			if ($recalllastsearch) {
				$_SESSION["customsearch_$id"]["sort_mode"] = $input->sort_mode->text();
			}
			$query->setOrder($input->sort_mode->text());
		}
		if ($input->maxRecords->int()) {
			if ($recalllastsearch) {
				$_SESSION["customsearch_$id"]["maxRecords"] = $input->maxRecords->int();
			}
			$maxRecords = $input->maxRecords->int();	// pass request data required by list
		} else {
			$maxRecords = $prefs['maxRecords'];
		}
		if ($input->offset->int()) {
			if ($recalllastsearch) {
				$_SESSION["customsearch_$id"]["offset"] = $input->offset->int();
			}
			$offset = $input->offset->int();
		} else {
			$offset = 0;
		}
		$query->setRange($offset, $maxRecords);

		if ($adddata) {
			foreach ($adddata as $fieldid => $d) {
				$config = $d['config'];
				$name = $d['name'];
				$value = $d['value'];
				$group = empty($config['_group']) ? null : $config['_group'];

				// save values entered as defaults while session lasts
				if (empty($value) && $value != 0) {
					$value = '';		// remove false or null
				}
				if ($recalllastsearch) {
					$_SESSION["customsearch_$id"][$fieldid] = $value;
				}

				if (empty($config['type'])) {
					$config['type'] = $name;
				}

				$filter = 'content'; //default
				if (isset($config['_filter']) || $name == 'categories' || $name == 'daterange') {
					if ($config['_filter'] == 'language') {
						$filter = 'language';
					} elseif ($config['_filter'] == 'type') {
						$filter = 'type';
					} elseif ($config['_filter'] == 'categories' || $name == 'categories') {
						$filter = 'categories';
					} elseif ($name == 'daterange') {
						$filter = 'daterange';
					}
				}

				if (is_array($value) && count($value) > 1) {
					$value = implode(' ', $value);
				} elseif (is_array($value)) {
					$value = current($value);
				}

				$function = "cs_dataappend_{$filter}";
				if (method_exists($this, $function)) {
					$this->$function($query->getSubQuery($group), $config, $value);
				}
			}

			foreach ($this->textranges as $info) {
				if (count($info['values']) >= 2) {
					$from = array_shift($info['values']);
					$to = array_shift($info['values']);
					$info['query']->filterTextRange($from, $to, $info['config']['_field']);
				}
			}

			foreach ($this->dateranges as $info) {
				if (count($info['values']) >= 2) {
					$from = array_shift($info['values']);
					$to = array_shift($info['values']);
					$info['query']->filterRange($from, $to, $info['config']['_field']);
				}
			}
		}

		if ($prefs['storedsearch_enabled'] == 'y' && $queryId = $input->store_query->int()) {
			// Store prior to adding 
			$storedsearchlib = TikiLib::lib('storedsearch');
			$storeResult = $storedsearchlib->storeUserQuery($queryId, $query);

			if (! $storeResult) {
				throw new Services_Exception('Failed to store the query.', 500);
			}
		}

		$unifiedsearchlib = TikiLib::lib('unifiedsearch');
		$unifiedsearchlib->initQuery($query); // Done after cache because permissions vary

		$facetsBuilder->build($query, $unifiedsearchlib->getFacetProvider());

		$index = $unifiedsearchlib->getIndex();
		$resultSet = $query->search($index);

		$formatter->setDataSource($unifiedsearchlib->getDataSource());
		$results = $formatter->format($resultSet);

		$results = TikiLib::lib('tiki')->parse_data($results, array('is_html' => true, 'skipvalidation' => true));

		return array('html' => $results);
	}

	private function cs_dataappend_language(Search_Query $query, $config, $value)
	{
		if ($config['type'] != 'text') {
			if (!empty($config['_value'])) {
				$value = $config['_value'];
				$query->filterLanguage($value);
			} elseif ($value) {
				$query->filterLanguage($value);
			}
		}
	}

	private function cs_dataappend_type(Search_Query $query, $config, $value)
	{
		if ($config['type'] != 'text') {
			if (!empty($config['_value'])) {
				$value = $config['_value'];
				$query->filterType($value);
			} elseif ($value) {
				$query->filterType($value);
			}
		}
	}

	private function cs_dataappend_content(Search_Query $query, $config, $value)
	{
		if ($value) {
			if (isset($config['_textrange'])) {
				$this->cs_handle_textrange($config['_textrange'], $query, $config, $value);
			} elseif (isset($config['_daterange'])) {
				$this->cs_handle_daterange($config['_daterange'], $query, $config, $value);
			} elseif ($config['type'] == 'checkbox') {
				if (empty($config['_field'])) {
					return;
				}
				if (!empty($config['_value'])) {
					if ($config['_value'] == 'n') {<commit_msg>[FIX] Allow searching for 0 (zero)<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$

// Controller to process requests from the custom search plugin using the list plugin to display results
// Refactored from customsearch_ajax.php for Tiki


class Services_Search_CustomSearchController
{
	private $textranges = array();
	private $dateranges = array();

	function setUp()
	{
		Services_Exception_Disabled::check('wikiplugin_list');
		Services_Exception_Disabled::check('wikiplugin_customsearch');
		Services_Exception_Disabled::check('feature_search');
	}

	function action_customsearch($input)
	{
		global $prefs;

		$this->textranges = array();
		$this->dateranges = array();

		$cachelib = TikiLib::lib('cache');
		$definition = $input->definition->word();
		if (empty($definition) || ! $definition = $cachelib->getSerialized($definition, 'customsearch')) {
			throw new Services_Exception(tra('Unfortunately, the search cache has expired. Please reload the page to start over.'));
		}

		$query = $definition['query'];
		$formatter = $definition['formatter'];
		$facetsBuilder = $definition['facets'];

		$adddata = json_decode($input->adddata->text(), true);

		$dataappend = array();

		$recalllastsearch = $input->recalllastsearch->int() ? true : false;

		$id = $input->searchid->text();
		if (empty($id)) {
			$id = '0';
		}
		// setup AJAX pagination
		$offset_jsvar = "customsearch_$id.offset";
		$onclick = "$('#customsearch_$id').submit();return false;";
		$dataappend['pagination'] = "{pagination offset_jsvar=\"$offset_jsvar\" onclick=\"$onclick\"}";

		if ($recalllastsearch && isset($_SESSION["customsearch_$id"])) {
			unset($_SESSION["customsearch_$id"]);
		}
		if ($input->sort_mode->text()) {
			if ($recalllastsearch) {
				$_SESSION["customsearch_$id"]["sort_mode"] = $input->sort_mode->text();
			}
			$query->setOrder($input->sort_mode->text());
		}
		if ($input->maxRecords->int()) {
			if ($recalllastsearch) {
				$_SESSION["customsearch_$id"]["maxRecords"] = $input->maxRecords->int();
			}
			$maxRecords = $input->maxRecords->int();	// pass request data required by list
		} else {
			$maxRecords = $prefs['maxRecords'];
		}
		if ($input->offset->int()) {
			if ($recalllastsearch) {
				$_SESSION["customsearch_$id"]["offset"] = $input->offset->int();
			}
			$offset = $input->offset->int();
		} else {
			$offset = 0;
		}
		$query->setRange($offset, $maxRecords);

		if ($adddata) {
			foreach ($adddata as $fieldid => $d) {
				$config = $d['config'];
				$name = $d['name'];
				$value = $d['value'];
				$group = empty($config['_group']) ? null : $config['_group'];

				// save values entered as defaults while session lasts
				if (empty($value) && $value != 0) {
					$value = '';		// remove false or null
				}
				if ($recalllastsearch) {
					$_SESSION["customsearch_$id"][$fieldid] = $value;
				}

				if (empty($config['type'])) {
					$config['type'] = $name;
				}

				$filter = 'content'; //default
				if (isset($config['_filter']) || $name == 'categories' || $name == 'daterange') {
					if ($config['_filter'] == 'language') {
						$filter = 'language';
					} elseif ($config['_filter'] == 'type') {
						$filter = 'type';
					} elseif ($config['_filter'] == 'categories' || $name == 'categories') {
						$filter = 'categories';
					} elseif ($name == 'daterange') {
						$filter = 'daterange';
					}
				}

				if (is_array($value) && count($value) > 1) {
					$value = implode(' ', $value);
				} elseif (is_array($value)) {
					$value = current($value);
				}

				$function = "cs_dataappend_{$filter}";
				if (method_exists($this, $function)) {
					$this->$function($query->getSubQuery($group), $config, $value);
				}
			}

			foreach ($this->textranges as $info) {
				if (count($info['values']) >= 2) {
					$from = array_shift($info['values']);
					$to = array_shift($info['values']);
					$info['query']->filterTextRange($from, $to, $info['config']['_field']);
				}
			}

			foreach ($this->dateranges as $info) {
				if (count($info['values']) >= 2) {
					$from = array_shift($info['values']);
					$to = array_shift($info['values']);
					$info['query']->filterRange($from, $to, $info['config']['_field']);
				}
			}
		}

		if ($prefs['storedsearch_enabled'] == 'y' && $queryId = $input->store_query->int()) {
			// Store prior to adding 
			$storedsearchlib = TikiLib::lib('storedsearch');
			$storeResult = $storedsearchlib->storeUserQuery($queryId, $query);

			if (! $storeResult) {
				throw new Services_Exception('Failed to store the query.', 500);
			}
		}

		$unifiedsearchlib = TikiLib::lib('unifiedsearch');
		$unifiedsearchlib->initQuery($query); // Done after cache because permissions vary

		$facetsBuilder->build($query, $unifiedsearchlib->getFacetProvider());

		$index = $unifiedsearchlib->getIndex();
		$resultSet = $query->search($index);

		$formatter->setDataSource($unifiedsearchlib->getDataSource());
		$results = $formatter->format($resultSet);

		$results = TikiLib::lib('tiki')->parse_data($results, array('is_html' => true, 'skipvalidation' => true));

		return array('html' => $results);
	}

	private function cs_dataappend_language(Search_Query $query, $config, $value)
	{
		if ($config['type'] != 'text') {
			if (!empty($config['_value'])) {
				$value = $config['_value'];
				$query->filterLanguage($value);
			} elseif ($value) {
				$query->filterLanguage($value);
			}
		}
	}

	private function cs_dataappend_type(Search_Query $query, $config, $value)
	{
		if ($config['type'] != 'text') {
			if (!empty($config['_value'])) {
				$value = $config['_value'];
				$query->filterType($value);
			} elseif ($value) {
				$query->filterType($value);
			}
		}
	}

	private function cs_dataappend_content(Search_Query $query, $config, $value)
	{
		if ($value > '') {
			if (isset($config['_textrange'])) {
				$this->cs_handle_textrange($config['_textrange'], $query, $config, $value);
			} elseif (isset($config['_daterange'])) {
				$this->cs_handle_daterange($config['_daterange'], $query, $config, $value);
			} elseif ($config['type'] == 'checkbox') {
				if (empty($config['_field'])) {
					return;
				}
				if (!empty($config['_value'])) {
					if ($config['_value'] == 'n') {
{'fraction_non_alphanumeric': 0.19786304788644518, 'fraction_numerical': 0.002729683356730619, 'mean_word_length': 4.153938906752412, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 128, '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': '3395842', 'n_tokens_mistral': 4892, 'n_tokens_neox': 4160, 'n_words': 835}