A
A
Alexander Balya2019-06-10 22:16:45
1C-Bitrix
Alexander Balya, 2019-06-10 22:16:45

How to force Bitrix to index included files?

The site page has a plug-in area, something like this:

<? $APPLICATION->IncludeFile(
    SITE_TEMPLATE_PATH.'/include/contacts/directory.html',
    array(),
    array( 'MODE' => 'html')
)?>

There is some text inside the included file.
The problem is that Bitrix does not index the text inside this included file , and if you request it in the search, the page will not be found.
At the same time, if you insert the text simply on the page, it is normally indexed.
How can you set up the system so that the content of included files is fully indexed as if it were present on the page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Savrasov, 2019-06-11
@alcorn

AddEventHandler("search", "BeforeIndex", Array("MyClass", "BeforeIndexHandler"));
class MyClass
{
   private $file_abs_path = "";

   function BeforeIndexHandler($arFields)
   {
      $io = CBXVirtualIo::GetInstance();

      if($arFields["MODULE_ID"] == "main")
      {
         list($site_id, $file_rel_path) = explode("|", $arFields["ITEM_ID"]);
         $file_doc_root = CSite::GetSiteDocRoot($site_id);
         $file_abs_path = preg_replace("#[\\\\\\/]+#", "/", $file_doc_root."/".$file_rel_path);
         $f = $io->GetFile($file_abs_path);
         $sFile = $f->GetContents();
         if(isset($sFile) && $sFile != "")
         {
            $replacer = new MyClass;
            $replacer->file_abs_path = $file_abs_path;
            $sFile = preg_replace_callback("/<\\?\\\$APPLICATION->IncludeComponent\\(\\s*\"bitrix:main.include\",(.*?)\\?>/mis", array($replacer, "replace"), $sFile);
            $arFields["BODY"] = CSearch::KillTags($sFile);
         }
      }
      return $arFields;
   }
   function replace($matchParams)
   {
      $io = CBXVirtualIo::GetInstance();

      if (preg_match("/\"AREA_FILE_SUFFIX\"\\s*=>\\s*\"(.*?)\",/", $matchParams[1], $match))
      {
         $slash_pos = strrpos($this->file_abs_path, "/");
         $sFilePath = substr($this->file_abs_path, 0, $slash_pos+1);
         $sFileName = substr($this->file_abs_path, $slash_pos+1);
         $sFileName = substr($sFileName, 0, strlen($sFileName)-4)."_".$match[1].".php";

         $f = $io->GetFile($sFilePath.$sFileName);
         return $f->GetContents();
      }
      return "";
   }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question