P
P
Peter2019-11-12 14:57:32
1C-Bitrix
Peter, 2019-11-12 14:57:32

How to search for text in include areas?

There are many static pages in which the text is located in the included areas. Is it possible to make the Bitrix search find the text placed in these include areas?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2019-11-12
@gtronic

The solution turned out to be not so simple. It is possible
to add include areas to the search index , but this requires intervention in the indexing process. for this, the following code is added to /local/php_interface/init.php:

<?
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 "";
   }
}

After inserting the code, do not forget to reindex.
This code works when including include areas with the AREA_FILE_SUFFIX option. In other cases, it needs to be improved.
In any case, search by include areas in Bitrix is ​​possible.

M
Maxim, 2019-11-12
@Tomio

I'm afraid not. The included area can be connected anywhere: in any static sections, in infoblock components (and even for some if conditions), even in the infoblock elements themselves (with some skill). Therefore, Bitrix will not be able to display relevant pages in the search results for this text.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question