Example #1
0
	function Index($MODULE_ID, $ITEM_ID, $arFields, $bOverWrite=false, $SEARCH_SESS_ID="")
	{
		$DB = CDatabase::GetModuleConnection('search');

		$arFields["MODULE_ID"] = $MODULE_ID;
		$arFields["ITEM_ID"] = $ITEM_ID;
		foreach(GetModuleEvents("search", "BeforeIndex", true) as $arEvent)
		{
			$arEventResult = ExecuteModuleEventEx($arEvent, array($arFields));
			if(is_array($arEventResult))
				$arFields = $arEventResult;
		}
		unset($arFields["MODULE_ID"]);
		unset($arFields["ITEM_ID"]);

		$bTitle = array_key_exists("TITLE", $arFields);
		if($bTitle)
			$arFields["TITLE"] = trim($arFields["TITLE"]);
		$bBody = array_key_exists("BODY", $arFields);
		if($bBody)
			$arFields["BODY"] = trim($arFields["BODY"]);
		$bTags = array_key_exists("TAGS", $arFields);
		if($bTags)
			$arFields["TAGS"] = trim($arFields["TAGS"]);

		if(!array_key_exists("SITE_ID", $arFields) && array_key_exists("LID", $arFields))
			$arFields["SITE_ID"] = $arFields["LID"];

		if(array_key_exists("SITE_ID", $arFields))
		{
			if(!is_array($arFields["SITE_ID"]))
			{
				$arFields["SITE_ID"] = Array($arFields["SITE_ID"]=>"");
			}
			else
			{
				$bNotAssoc = true;
				$i = 0;
				foreach($arFields["SITE_ID"] as $k=>$val)
				{
					if("".$k!="".$i)
					{
						$bNotAssoc=false;
						break;
					}
					$i++;
				}
				if($bNotAssoc)
				{
					$x = $arFields["SITE_ID"];
					$arFields["SITE_ID"] = Array();
					foreach($x as $val)
						$arFields["SITE_ID"][$val] = "";
				}
			}

			if(count($arFields["SITE_ID"])<=0)
				return 0;

			reset($arFields["SITE_ID"]);
			list($arFields["LID"], $url) = each($arFields["SITE_ID"]);

			$arSites = array();
			foreach($arFields["SITE_ID"] as $site => $url)
			{
				$arSites[] = $DB->ForSQL($site, 2);
			}

			$strSql = "
				SELECT CR.RANK
				FROM b_search_custom_rank CR
				WHERE CR.SITE_ID in ('".implode("', '", $arSites)."')
				AND CR.MODULE_ID='".$DB->ForSQL($MODULE_ID)."'
				".(is_set($arFields, "PARAM1")?"AND (CR.PARAM1 IS NULL OR CR.PARAM1='".$DB->ForSQL($arFields["PARAM1"])."')":"")."
				".(is_set($arFields, "PARAM2")?"AND (CR.PARAM2 IS NULL OR CR.PARAM2='".$DB->ForSQL($arFields["PARAM2"])."')":"")."
				".($ITEM_ID<>""?"AND (CR.ITEM_ID IS NULL OR CR.ITEM_ID='".$DB->ForSQL($ITEM_ID)."')":"")."
				ORDER BY
					PARAM1 DESC, PARAM2 DESC, ITEM_ID DESC
			";
			$r = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
			$arFields["CUSTOM_RANK_SQL"]=$strSql;
			if($arResult = $r->Fetch())
				$arFields["CUSTOM_RANK"]=$arResult["RANK"];
		}

		$arGroups = array();
		if(is_set($arFields, "PERMISSIONS"))
		{
			foreach($arFields["PERMISSIONS"] as $group_id)
			{
				if(is_numeric($group_id))
					$arGroups[$group_id] = "G".intval($group_id);
				else
					$arGroups[$group_id] = $group_id;
			}
		}

		$strSqlSelect = "";
		if($bBody) $strSqlSelect .= ",BODY";
		if($bTitle) $strSqlSelect .= ",TITLE";
		if($bTags) $strSqlSelect .= ",TAGS";

		$strSql =
			"SELECT ID, ".$DB->DateToCharFunction("DATE_CHANGE")." as DATE_CHANGE
			".$strSqlSelect."
			FROM b_search_content
			WHERE MODULE_ID = '".$DB->ForSQL($MODULE_ID)."'
				AND ITEM_ID = '".$DB->ForSQL($ITEM_ID)."' ";

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

		if($arResult = $r->Fetch())
		{
			$ID = $arResult["ID"];

			if($bTitle && $bBody && strlen($arFields["BODY"])<=0 && strlen($arFields["TITLE"])<=0)
			{
				foreach(GetModuleEvents("search", "OnBeforeIndexDelete", true) as $arEvent)
					ExecuteModuleEventEx($arEvent, array("SEARCH_CONTENT_ID = ".$ID));

				CSearchTags::CleanCache("", $ID);
				CSearch::CleanFreqCache($ID);
				$DB->Query("DELETE FROM b_search_content_param WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				$DB->Query("DELETE FROM b_search_content_right WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				$DB->Query("DELETE FROM b_search_content_site WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				$DB->Query("DELETE FROM b_search_content_title WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				$DB->Query("DELETE FROM b_search_content_stem WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				if(BX_SEARCH_VERSION > 1)
					$DB->Query("DELETE FROM b_search_content_text WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				$DB->Query("DELETE FROM b_search_tags WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				$DB->Query("DELETE FROM b_search_content WHERE ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);

				return 0;
			}

			if(is_set($arFields, "PARAMS"))
				CAllSearch::SetContentItemParams($ID, $arFields["PARAMS"]);

			if(count($arGroups) > 0)
				CAllSearch::SetContentItemGroups($ID, $arGroups);

			if(is_set($arFields, "SITE_ID"))
			{
				$arSITE_ID = $arFields["SITE_ID"];
				$strSql = "
					SELECT SITE_ID, URL
					FROM b_search_content_site
					WHERE SEARCH_CONTENT_ID = ".$ID."
				";
				$rsSite = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				while($arSite = $rsSite->Fetch())
				{
					if(array_key_exists($arSite["SITE_ID"], $arSITE_ID))
					{
						if($arSite["URL"] !== $arSITE_ID[$arSite["SITE_ID"]])
							$strSql = "
								UPDATE b_search_content_site
								SET URL = '".$DB->ForSql($url, 2000)."'
								WHERE SEARCH_CONTENT_ID = ".$ID."
								AND SITE_ID = '".$DB->ForSql($arSite["SITE_ID"])."'
							";
						else
							$strSql = "";

						unset($arSITE_ID[$arSite["SITE_ID"]]);
					}
					else
					{
						$strSql = "
							DELETE FROM b_search_content_site
							WHERE SEARCH_CONTENT_ID = ".$ID."
							AND SITE_ID = '".$DB->ForSql($arSite["SITE_ID"])."'
						";
					}

					if(!empty($strSql))
						$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				}

				foreach($arSITE_ID as $site => $url)
				{
					$strSql = "
						INSERT INTO b_search_content_site(SEARCH_CONTENT_ID, SITE_ID, URL)
						VALUES(".$ID.", '".$DB->ForSql($site, 2)."', '".$DB->ForSql($url, 2000)."')
					";
					$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				}
			}

			if(array_key_exists("LAST_MODIFIED", $arFields))
				$arFields["~DATE_CHANGE"] = $arFields["DATE_CHANGE"] = $DATE_CHANGE = $arFields["LAST_MODIFIED"];
			elseif(array_key_exists("DATE_CHANGE", $arFields))
				$arFields["~DATE_CHANGE"] = $arFields["DATE_CHANGE"] = $DATE_CHANGE = $DB->FormatDate($arFields["DATE_CHANGE"], "DD.MM.YYYY HH:MI:SS", CLang::GetDateFormat());
			else
				$DATE_CHANGE = '';

			if(!$bOverWrite && $DATE_CHANGE == $arResult["DATE_CHANGE"])
			{
				if(strlen($SEARCH_SESS_ID)>0)
					$DB->Query("UPDATE b_search_content SET UPD='".$SEARCH_SESS_ID."' WHERE ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				//$DB->Commit();
				return $ID;
			}

			unset($arFields["MODULE_ID"]);
			unset($arFields["ITEM_ID"]);

			if($bBody || $bTitle || $bTags)
			{

				if(array_key_exists("INDEX_TITLE", $arFields) && $arFields["INDEX_TITLE"] === false)
				{
					$content = "";
				}
				else
				{
					if($bTitle)
						$content = $arFields["TITLE"]."\r\n";
					else
						$content = $arResult["TITLE"]."\r\n";
				}

				if($bBody)
					$content .= $arFields["BODY"]."\r\n";
				else
					$content .= $arResult["BODY"]."\r\n";

				if($bTags)
					$content .= $arFields["TAGS"];
				else
					$content .= $arResult["TAGS"];

				$content = preg_replace("'&#(\d+);'e", "chr(\\1)", $content);
				$arFields["SEARCHABLE_CONTENT"] = CSearch::KillEntities(ToUpper($content));
			}

			if(strlen($SEARCH_SESS_ID)>0)
				$arFields["UPD"] = $SEARCH_SESS_ID;

			if(array_key_exists("TITLE", $arFields))
			{
				$DB->Query("DELETE FROM b_search_content_title WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				if(
					!array_key_exists("INDEX_TITLE", $arFields)
					|| $arFields["INDEX_TITLE"] !== false
				)
					CSearch::IndexTitle($arFields["SITE_ID"], $ID, $arFields["TITLE"]);
			}

			if(is_set($arFields, "SEARCHABLE_CONTENT"))
			{
				if(BX_SEARCH_VERSION > 1)
				{
					$text_md5 = md5($arFields["SEARCHABLE_CONTENT"]);
					$rsText = $DB->Query("SELECT SEARCH_CONTENT_MD5 FROM b_search_content_text WHERE SEARCH_CONTENT_ID = ".$ID);
					$arText = $rsText->Fetch();
					if(!$arText || $arText["SEARCH_CONTENT_MD5"] !== $text_md5)
					{
						CSearch::CleanFreqCache($ID);
						$DB->Query("DELETE FROM b_search_content_stem WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
						if (COption::GetOptionString("search", "agent_stemming") === "Y")
							CSearch::DelayStemIndex($ID);
						else
							CSearch::StemIndex($arFields["SITE_ID"], $ID, $arFields["SEARCHABLE_CONTENT"]);
						$DB->Query("DELETE FROM b_search_content_text WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
						$arText = array(
							"ID" => 1,
							"SEARCH_CONTENT_ID" => $ID,
							"SEARCH_CONTENT_MD5" => $text_md5,
							"SEARCHABLE_CONTENT" => $arFields["SEARCHABLE_CONTENT"]
						);
						$DB->Add("b_search_content_text", $arText, Array("SEARCHABLE_CONTENT"));
					}
				}
				else
				{
					CSearch::CleanFreqCache($ID);
					$DB->Query("DELETE FROM b_search_content_stem WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
					if (COption::GetOptionString("search", "agent_stemming") === "Y")
						CSearch::DelayStemIndex($ID);
					else
						CSearch::StemIndex($arFields["SITE_ID"], $ID, $arFields["SEARCHABLE_CONTENT"]);
				}
			}

			if($bTags && ($arResult["TAGS"] != $arFields["TAGS"]))
			{
				CSearchTags::CleanCache("", $ID);
				$DB->Query("DELETE FROM b_search_tags WHERE SEARCH_CONTENT_ID = ".$ID, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				CSearch::TagsIndex($arFields["SITE_ID"], $ID, $arFields["TAGS"]);
			}

			foreach(GetModuleEvents("search", "OnBeforeIndexUpdate", true) as $arEvent)
				ExecuteModuleEventEx($arEvent, array($ID, $arFields));

			CSearch::Update($ID, $arFields);
		}
		else
		{
			if($bTitle && $bBody && strlen($arFields["BODY"])<=0 && strlen($arFields["TITLE"])<=0)
			{
				//$DB->Commit();
				return 0;
			}

			$arFields["MODULE_ID"] = $MODULE_ID;
			$arFields["ITEM_ID"] = $ITEM_ID;

			if(array_key_exists("INDEX_TITLE", $arFields) && $arFields["INDEX_TITLE"] === false)
				$content = $arFields["BODY"]."\r\n".$arFields["TAGS"];
			else
				$content = $arFields["TITLE"]."\r\n".$arFields["BODY"]."\r\n".$arFields["TAGS"];

			$content = preg_replace ("'&#(\d+);'e", "chr(\\1)", $content);
			$arFields["SEARCHABLE_CONTENT"] = CSearch::KillEntities(ToUpper($content));

			if($SEARCH_SESS_ID!="")
				$arFields["UPD"] = $SEARCH_SESS_ID;

			$ID = CSearch::Add($arFields);

			foreach(GetModuleEvents("search", "OnAfterIndexAdd", true) as $arEvent)
				ExecuteModuleEventEx($arEvent, array($ID, $arFields));

			if(is_set($arFields, "PARAMS"))
				CAllSearch::SetContentItemParams($ID, $arFields["PARAMS"]);

			CAllSearch::SetContentItemGroups($ID, $arGroups);

			foreach($arFields["SITE_ID"] as $site=>$url)
			{
				$strSql = "
					INSERT INTO b_search_content_site(SEARCH_CONTENT_ID, SITE_ID, URL)
					VALUES(".$ID.", '".$DB->ForSql($site, 2)."', '".$DB->ForSql($url, 2000)."')";
				$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
			}

			if(
				!array_key_exists("INDEX_TITLE", $arFields)
				|| $arFields["INDEX_TITLE"] !== false
			)
				CSearch::IndexTitle($arFields["SITE_ID"], $ID, $arFields["TITLE"]);

			if (COption::GetOptionString("search", "agent_stemming") === "Y")
				CSearch::DelayStemIndex($ID);
			else
				CSearch::StemIndex($arFields["SITE_ID"], $ID, $arFields["SEARCHABLE_CONTENT"]);

			if(BX_SEARCH_VERSION > 1)
			{
				$text_md5 = md5($arFields["SEARCHABLE_CONTENT"]);
				$arText = array(
					"ID" => 1,
					"SEARCH_CONTENT_ID" => $ID,
					"SEARCH_CONTENT_MD5" => $text_md5,
					"SEARCHABLE_CONTENT" => $arFields["SEARCHABLE_CONTENT"]
				);
				$DB->Add("b_search_content_text", $arText, Array("SEARCHABLE_CONTENT"));
			}

			CSearch::TagsIndex($arFields["SITE_ID"], $ID, $arFields["TAGS"]);

			CSearch::CleanFreqCache($ID);
		}
		//$DB->Commit();

		return $ID;
	}