• Модуль: search
  • Путь к файлу: ~/bitrix/modules/search/classes/general/search.php
  • Класс: CAllSearch
  • Вызов: CAllSearch::Search
function Search($arParams, $aSort = array(), $aParamsEx = array(), $bTagsCloud = false)
{
	$DB = CDatabase::GetModuleConnection('search');

	if (!is_array($arParams))
		$arParams = array("QUERY" => $arParams);

	if (!is_set($arParams, "SITE_ID") && is_set($arParams, "LID"))
	{
		$arParams["SITE_ID"] = $arParams["LID"];
		unset($arParams["LID"]);
	}

	if (array_key_exists("TAGS", $arParams))
	{
		$this->strTagsText = $arParams["TAGS"];
		$arTags = explode(",", $arParams["TAGS"]);
		foreach ($arTags as $i => $strTag)
		{
			$strTag = trim($strTag);
			if($strTag <> '')
			{
				$arTags[$i] = str_replace(""", "\"", $strTag);
			}
			else
			{
				unset($arTags[$i]);
			}
		}

		if (count($arTags))
			$arParams["TAGS"] = '"'.implode('","', $arTags).'"';
		else
			unset($arParams["TAGS"]);
	}

	$this->strQueryText = $strQuery = trim($arParams["QUERY"]);
	$this->strTags = $strTags = $arParams["TAGS"];

	if (($strQuery == '') && ($strTags <> ''))
	{
		$strQuery = $strTags;
		$bTagsSearch = true;
	}
	else
	{
		if($strTags <> '')
		{
			$strQuery .= " ".$strTags;
		}
		$strQuery = preg_replace_callback("/&#(\d+);/", array($this, "chr"), $strQuery);
		$bTagsSearch = false;
	}

	if (!array_key_exists("STEMMING", $aParamsEx))
		$aParamsEx["STEMMING"] = COption::GetOptionString("search", "use_stemming") == "Y";
	$this->Query = new CSearchQuery("and", "yes", 0, $arParams["SITE_ID"]);
	if ($this->_opt_NO_WORD_LOGIC)
		$this->Query->no_bool_lang = true;
	$query = $this->Query->GetQueryString((BX_SEARCH_VERSION > 1? "sct": "sc").".SEARCHABLE_CONTENT", $strQuery, $bTagsSearch, $aParamsEx["STEMMING"], $this->_opt_ERROR_ON_EMPTY_STEM);

	$fullTextParams = $aParamsEx;
	if (!isset($fullTextParams["LIMIT"]))
		$fullTextParams["LIMIT"] = $this->limit;
	$fullTextParams["OFFSET"] = $this->offset;
	$fullTextParams["QUERY_OBJECT"] = $this->Query;
	$result = CSearchFullText::getInstance()->search($arParams, $aSort, $fullTextParams, $bTagsCloud);
	if (is_array($result))
	{
		$this->error = CSearchFullText::getInstance()->getErrorText();
		$this->errorno = CSearchFullText::getInstance()->getErrorNumber();
		$this->formatter = CSearchFullText::getInstance()->getRowFormatter();
		if ($this->errorno > 0)
			return;
	}
	else
	{
		if (!$query || trim($query) == '')
		{
			if ($bTagsCloud)
			{
				$query = "1=1";
			}
			else
			{
				$this->error = $this->Query->error;
				$this->errorno = $this->Query->errorno;
				return;
			}
		}

		if (mb_strlen($query) > 2000)
		{
			$this->error = GetMessage("SEARCH_ERROR4");
			$this->errorno = 4;
			return;
		}
	}

	foreach (GetModuleEvents("search", "OnSearch", true) as $arEvent)
	{
		$r = "";
		if ($bTagsSearch)
		{
			if($strTags <> '')
			{
				$r = ExecuteModuleEventEx($arEvent, array("tags:".$strTags));
			}
		}
		else
		{
			$r = ExecuteModuleEventEx($arEvent, array($strQuery));
		}
		if ($r <> "")
			$this->url_add_params[] = $r;
	}

	if (is_array($result))
	{
		$r = new CDBResult;
		$r->InitFromArray($result);
	}
	elseif (
		BX_SEARCH_VERSION > 1
		&& !empty($this->Query->m_stemmed_words_id)
		&& is_array($this->Query->m_stemmed_words_id)
		&& array_sum($this->Query->m_stemmed_words_id) === 0
	)
	{
		$r = new CDBResult;
		$r->InitFromArray(array());
	}
	else
	{
		$this->strSqlWhere = "";
		$bIncSites = false;

		$arSqlWhere = array();
		if (is_array($aParamsEx) && !empty($aParamsEx))
		{
			foreach ($aParamsEx as $aParamEx)
			{
				$strSqlWhere = CSearch::__PrepareFilter($aParamEx, $bIncSites);
				if ($strSqlWhere != "")
					$arSqlWhere[] = $strSqlWhere;
			}
		}
		if (!empty($arSqlWhere))
		{
			$arSqlWhere = array(
				"ntttt(".implode(")ntttttOR(", $arSqlWhere)."ntttt)",
			);
		}

		$strSqlWhere = CSearch::__PrepareFilter($arParams, $bIncSites);
		if ($strSqlWhere != "")
			array_unshift($arSqlWhere, $strSqlWhere);

		$strSqlOrder = $this->__PrepareSort($aSort, "sc.", $bTagsCloud);

		if (!array_key_exists("USE_TF_FILTER", $aParamsEx))
			$aParamsEx["USE_TF_FILTER"] = COption::GetOptionString("search", "use_tf_cache") == "Y";

		$bStem = !$bTagsSearch && count($this->Query->m_stemmed_words) > 0;
		//calculate freq of the word on the whole site_id
		if ($bStem && count($this->Query->m_stemmed_words))
		{
			$arStat = $this->GetFreqStatistics($this->Query->m_lang, $this->Query->m_stemmed_words, $arParams["SITE_ID"]);
			$this->tf_hwm_site_id = ($arParams["SITE_ID"] <> ''? $arParams["SITE_ID"]: "");

			//we'll make filter by it's contrast
			if (!$bTagsCloud && $aParamsEx["USE_TF_FILTER"])
			{
				$hwm = false;
				foreach ($this->Query->m_stemmed_words as $i => $stem)
				{
					if (!array_key_exists($stem, $arStat))
					{
						$hwm = 0;
						break;
					}
					elseif ($hwm === false)
					{
						$hwm = $arStat[$stem]["TF"];
					}
					elseif ($hwm > $arStat[$stem]["TF"])
					{
						$hwm = $arStat[$stem]["TF"];
					}
				}

				if ($hwm > 0)
				{
					$arSqlWhere[] = "st.TF >= ".number_format($hwm, 2, ".", "");
					$this->tf_hwm = $hwm;
				}
			}
		}

		if (!empty($arSqlWhere))
		{
			$this->strSqlWhere = "nttttAND (nttttt(".implode(")ntttttAND(", $arSqlWhere).")ntttt)";
		}

		if ($bTagsCloud)
			$strSql = $this->tagsMakeSQL($query, $this->strSqlWhere, $strSqlOrder, $bIncSites, $bStem, $aParamsEx["LIMIT"]);
		else
			$strSql = $this->MakeSQL($query, $this->strSqlWhere, $strSqlOrder, $bIncSites, $bStem);

		$r = $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__); } parent::__construct($r); }