&& $_REQUEST["ajax_call"] === "y" && ( !isset($_REQUEST["INPUT_ID"]) || $_REQUEST["INPUT_ID"] == $arParams["INPUT_ID"] ) && CModule::IncludeModule("search") ) { CUtil::decodeURIComponent($query); $arResult["alt_query"] = ""; if($arParams["USE_LANGUAGE_GUESS"] !== "N") { $arLang = CSearchLanguage::GuessLanguage($query); if(is_array($arLang) && $arLang["from"] != $arLang["to"]) $arResult["alt_query"] = CSearchLanguage::ConvertKeyboardLayout($query, $arLang["from"], $arLang["to"]); } $arResult["query"] = $query; $arResult["phrase"] = stemming_split($query, LANGUAGE_ID); $arParams["NUM_CATEGORIES"] = intval($arParams["NUM_CATEGORIES"]); if($arParams["NUM_CATEGORIES"] <= 0) $arParams["NUM_CATEGORIES"] = 1; $arParams["TOP_COUNT"] = intval($arParams["TOP_COUNT"]); if($arParams["TOP_COUNT"] <= 0) $arParams["TOP_COUNT"] = 5; $arOthersFilter = array("LOGIC"=>"OR");
} } } } if ($arParams["SEARCH_IN_TREE"] == "Y") { if (isset($_REQUEST["q"])) { $q = trim($_REQUEST["q"]); } else { $q = false; } $arQuery = array(); if ($q !== false) { if (CModule::IncludeModule("search")) { $arLang = CSearchLanguage::GuessLanguage($q); if (is_array($arLang) && $arLang["from"] != $arLang["to"]) { $arQuery["QUERY"] = htmlspecialcharsex(CSearchLanguage::ConvertKeyboardLayout($q, $arLang["from"], $arLang["to"])); } else { $arQuery["QUERY"] = htmlspecialcharsex($q); } } else { $arQuery["QUERY"] = htmlspecialcharsex($q); } } else { $arQuery["QUERY"] = false; } if ($arQuery["QUERY"]) { $qu = $arQuery["QUERY"]; $len_q = mb_strlen($qu); if ($len_q > 3) { if (CModule::IncludeModule("iblock")) { /*IBLOCKS*/
$arResult["DROPDOWN"] = htmlspecialcharsex($arrDropdown); $arResult["REQUEST"]["HOW"] = htmlspecialcharsbx($how); $arResult["REQUEST"]["~FROM"] = $from; $arResult["REQUEST"]["FROM"] = htmlspecialcharsbx($from); $arResult["REQUEST"]["~TO"] = $to; $arResult["REQUEST"]["TO"] = htmlspecialcharsbx($to); if ($q !== false) { if ($arParams["USE_LANGUAGE_GUESS"] == "N" || isset($_REQUEST["spell"])) { $arResult["REQUEST"]["~QUERY"] = $q; $arResult["REQUEST"]["QUERY"] = htmlspecialcharsex($q); } else { $arLang = CSearchLanguage::GuessLanguage($q); if (is_array($arLang) && $arLang["from"] != $arLang["to"]) { $arResult["REQUEST"]["~ORIGINAL_QUERY"] = $q; $arResult["REQUEST"]["ORIGINAL_QUERY"] = htmlspecialcharsex($q); $arResult["REQUEST"]["~QUERY"] = CSearchLanguage::ConvertKeyboardLayout($arResult["REQUEST"]["~ORIGINAL_QUERY"], $arLang["from"], $arLang["to"]); $arResult["REQUEST"]["QUERY"] = htmlspecialcharsex($arResult["REQUEST"]["~QUERY"]); } else { $arResult["REQUEST"]["~QUERY"] = $q; $arResult["REQUEST"]["QUERY"] = htmlspecialcharsex($q); } } } else { $arResult["REQUEST"]["~QUERY"] = false; $arResult["REQUEST"]["QUERY"] = false; } if ($tags !== false) { $arResult["REQUEST"]["~TAGS_ARRAY"] = array(); $arTags = explode(",", $tags); foreach ($arTags as $tag) { $tag = trim($tag);
function GuessLanguage($text, $lang = false) { if (strlen($text) <= 0) { return false; } static $cache = array(); if (empty($cache)) { $cache[] = "en"; //English is always in mind and on the first place $rsLanguages = CLanguage::GetList($b = "", $o = ""); while ($arLanguage = $rsLanguages->Fetch()) { if ($arLanguage["LID"] != "en") { $cache[] = $arLanguage["LID"]; } } } if (is_array($lang)) { $arLanguages = $lang; } else { $arLanguages = $cache; } if (count($arLanguages) < 2) { return false; } $languages_from = array(); $max_len = 0; //Give customized languages a chance to guess foreach ($arLanguages as $lang) { $ob = CSearchLanguage::GetLanguage($lang); $res = $ob->PreGuessLanguage($text, $lang); if (is_array($res)) { return $res; } elseif ($res === true) { return false; } } //First try to detect language which //was used to type the phrase foreach ($arLanguages as $lang) { $ob = CSearchLanguage::GetLanguage($lang); $arScanCodesTmp1 = $ob->ConvertToScancode($text, true); $arScanCodesTmp2_cnt = count(array_filter($arScanCodesTmp1)); //It will be one with most converted chars if ($arScanCodesTmp2_cnt > $max_len) { $max_len = $arScanCodesTmp2_cnt; $languages_from = array($lang => $arScanCodesTmp1); } elseif ($arScanCodesTmp2_cnt == $max_len) { $languages_from[$lang] = $arScanCodesTmp1; } } if ($max_len < 2) { return false; } if (count($languages_from) <= 0) { return false; } //If more than one language is detected as input //try to get one with best trigram info $arDetectionFrom = array(); $i = 0; foreach ($languages_from as $lang => $arScanCodes) { $arDetectionFrom[$lang] = array(); $ob = CSearchLanguage::GetLanguage($lang); $arDetectionFrom[$lang][] = $ob->HasTrigrams(); $arDetectionFrom[$lang][] = $ob->CheckTrigrams($arScanCodes); //Calculate how far sequence of scan codes //is from language model //$deviation = $ob->GetDeviation($arScanCodes); //$arDetection[$lang_from_to][] = $deviation[1]; //$arDetection[$lang_from_to][] = intval($deviation[0]*100); //Delay till compare $arDetectionFrom[$lang][] = $ob; $arDetectionFrom[$lang][] = $arScanCodes; $arDetectionFrom[$lang][] = $i; $i++; } uasort($arDetectionFrom, array("CSearchLanguage", "cmp")); //echo "<pre>";foreach($arDetectionFrom as $i=>$ar){var_dump($i); print_r(array($ar[0],$ar[1],$ar[3],$ar[4],));}echo "<pre>"; //Now try the best to detect the language $arDetection = array(); $i = 0; foreach ($arDetectionFrom as $lang_from => $arTemp) { $arScanCodes = $languages_from[$lang_from]; foreach ($arLanguages as $lang) { $lang_from_to = $lang_from . "=>" . $lang; $arDetection[$lang_from_to] = array(); $ob = CSearchLanguage::GetLanguage($lang); $arDetection[$lang_from_to][] = $ob->HasBigrammInfo(); $arDetection[$lang_from_to][] = $ob->CheckTrigrams($arScanCodes); //Calculate how far sequence of scan codes //is from language model //$deviation = $ob->GetDeviation($arScanCodes); //$arDetection[$lang_from_to][] = $deviation[1]; //$arDetection[$lang_from_to][] = intval($deviation[0]*100); //Delay till compare $arDetection[$lang_from_to][] = $ob; $arDetection[$lang_from_to][] = $arScanCodes; $alt_text = CSearchLanguage::ConvertKeyboardLayout($text, $lang_from, $lang); $arDetection[$lang_from_to][] = $alt_text !== $text; $arDetection[$lang_from_to][] = $i; $arDetection[$lang_from_to][] = $lang_from_to; $i++; } } uasort($arDetection, array("CSearchLanguage", "cmp")); $language_from_to = key($arDetection); list($language_from, $language_to) = explode("=>", $language_from_to); //echo "<pre>";foreach($arDetection as $i=>$ar){var_dump($i); print_r(array($ar[0],$ar[1],$ar[3],$ar[4],$ar[5],));}echo "<pre>"; $alt_text = CSearchLanguage::ConvertKeyboardLayout($text, $language_from, $language_to); if ($alt_text === $text) { return false; } return array("from" => $language_from, "to" => $language_to); }
/** * $arParams, * $request */ function getSearchedElement($arParams, $request) { if (!CModule::IncludeModule("search")) { return false; } $arParams["SHOW_WHEN"] = $arParams["SHOW_WHEN"] == "Y"; if (!is_array($arParams["arrWHERE"])) { $arParams["arrWHERE"] = array(); } $arParams["PAGE_RESULT_COUNT"] = 50; if ($arParams["DEFAULT_SORT"] !== "date") { $arParams["DEFAULT_SORT"] = "rank"; } if (strlen($arParams["FILTER_NAME"]) <= 0 || !preg_match("/^[A-Za-z_][A-Za-z01-9_]*\$/", $arParams["FILTER_NAME"])) { $arFILTERCustom = array(); } else { $arFILTERCustom = $GLOBALS[$arParams["FILTER_NAME"]]; if (!is_array($arFILTERCustom)) { $arFILTERCustom = array(); } } $exFILTER = CSearchParameters::ConvertParamsToFilter($arParams, "arrFILTER"); //options if (isset($_REQUEST["q"])) { $q = trim($_REQUEST["q"]); } else { $q = false; } if ($q !== false) { if ($arParams["USE_LANGUAGE_GUESS"] == "N" || isset($_REQUEST["spell"])) { $arResult["REQUEST"]["~QUERY"] = $q; $arResult["REQUEST"]["QUERY"] = htmlspecialcharsex($q); } else { $arLang = CSearchLanguage::GuessLanguage($q); if (is_array($arLang) && $arLang["from"] != $arLang["to"]) { $arResult["REQUEST"]["~ORIGINAL_QUERY"] = $q; $arResult["REQUEST"]["ORIGINAL_QUERY"] = htmlspecialcharsex($q); $arResult["REQUEST"]["~QUERY"] = CSearchLanguage::ConvertKeyboardLayout($arResult["REQUEST"]["~ORIGINAL_QUERY"], $arLang["from"], $arLang["to"]); $arResult["REQUEST"]["QUERY"] = htmlspecialcharsex($arResult["REQUEST"]["~QUERY"]); } else { $arResult["REQUEST"]["~QUERY"] = $q; $arResult["REQUEST"]["QUERY"] = htmlspecialcharsex($q); } } } $arResult["URL"] = $APPLICATION->GetCurPage() . "?q=" . urlencode($q) . (isset($_REQUEST["spell"]) ? "&spell=1" : "") . ($tags !== false ? "&tags=" . urlencode($tags) : ""); if (isset($arResult["REQUEST"]["~ORIGINAL_QUERY"])) { $arResult["ORIGINAL_QUERY_URL"] = $APPLICATION->GetCurPage() . "?q=" . urlencode($arResult["REQUEST"]["~ORIGINAL_QUERY"]) . "&spell=1" . "&where=" . urlencode($arResult["REQUEST"]["WHERE"]) . ($arResult["REQUEST"]["HOW"] == "d" ? "&how=d" : "") . ($arResult["REQUEST"]["FROM"] ? '&from=' . urlencode($arResult["REQUEST"]["~FROM"]) : "") . ($arResult["REQUEST"]["TO"] ? '&to=' . urlencode($arResult["REQUEST"]["~TO"]) : "") . ($tags !== false ? "&tags=" . urlencode($tags) : ""); } $templatePage = ""; $arReturn = false; $arFilter = array("SITE_ID" => SITE_ID, "QUERY" => $arResult["REQUEST"]["~QUERY"], "TAGS" => $arResult["REQUEST"]["~TAGS"]); $arFilter = array_merge($arFILTERCustom, $arFilter); if ($from) { $arFilter[">=DATE_CHANGE"] = $from; } if ($to) { $arFilter["<=DATE_CHANGE"] = $to; } $obSearch = new CSearch(); //When restart option is set we will ignore error on query with only stop words $obSearch->SetOptions(array("ERROR_ON_EMPTY_STEM" => $arParams["RESTART"] != "Y", "NO_WORD_LOGIC" => $arParams["NO_WORD_LOGIC"] == "Y")); $obSearch->Search($arFilter, $aSort, $exFILTER); $arResult["ERROR_CODE"] = $obSearch->errorno; $arResult["ERROR_TEXT"] = $obSearch->error; $arResult["SEARCH"] = array(); if ($obSearch->errorno == 0) { $obSearch->NavStart($arParams["PAGE_RESULT_COUNT"], false); $ar = $obSearch->GetNext(); //Search restart if (!$ar && $arParams["RESTART"] == "Y" && $obSearch->Query->bStemming) { $exFILTER["STEMMING"] = false; $obSearch = new CSearch(); $obSearch->Search($arFilter, $aSort, $exFILTER); $arResult["ERROR_CODE"] = $obSearch->errorno; $arResult["ERROR_TEXT"] = $obSearch->error; if ($obSearch->errorno == 0) { $obSearch->NavStart($arParams["PAGE_RESULT_COUNT"], false); $ar = $obSearch->GetNext(); } } $arReturn = array(); while ($ar) { $arReturn[$ar["ID"]] = $ar["ITEM_ID"]; $ar["CHAIN_PATH"] = $APPLICATION->GetNavChain($ar["URL"], 0, $folderPath . "/chain_template.php", true, false); $ar["URL"] = htmlspecialcharsbx($ar["URL"]); $ar["TAGS"] = array(); if (!empty($ar["~TAGS_FORMATED"])) { foreach ($ar["~TAGS_FORMATED"] as $name => $tag) { if ($arParams["TAGS_INHERIT"] == "Y") { $arTags = $arResult["REQUEST"]["~TAGS_ARRAY"]; $arTags[$tag] = $tag; $tags = implode(",", $arTags); } else { $tags = $tag; } $ar["TAGS"][] = array("URL" => $APPLICATION->GetCurPageParam("tags=" . urlencode($tags), array("tags")), "TAG_NAME" => htmlspecialcharsex($name)); } } $arResult["SEARCH"][] = $ar; $ar = $obSearch->GetNext(); } return $arResult; } }