Bitrix Site Manager v11.0

Automatic Google Sitemap creation for your Bitrix site

Automatic Google Sitemap creation for your Bitrix site

In this post we'll cover creating a Google Sitemap for your site automatically using a small script and the cron utility


Working with Google Sitemap through the product interface is described in the help:
http://www.bitrixsoft.com/help/source/search/help/en/search_sitemap.php.html

But to perform this task according to a schedule we'll make a script based on the CSiteMap::Create() method:

Code
CSiteMap::Create ($site_id, $max_execution_time, $NS, $arOptions = array())


This method requires the following parameters:
  • site_id - ID of the site for which the sitemap will be generated
  • max_execution_time - an array. First element is the max step time in seconds. Second element - max number of search index records (value less than 5000 is recommended).
  • NS - Step start value. The result of previous iteration should be passed to this parameter.
  • arOptions - Optional. If this contains the key 'FORUM_TOPICS_ONLY' with value Y, then only the topics’ starting messages fr om the forum will be added to the sitemap.


Sitemap generation is always performed in a step-by-step manner. After one step is performed, the process receives the results of the previous step and proceeds.

The script itself with comments:
Code
<?
//specify your real 'DOCUMENT_ROOT' directory path

$_SERVER["DOCUMENT_ROOT"] = realpath(dirname(__FILE__)."/. ./..");

define("NO_KEEP_STATISTIC", true);
define("NOT_CHECK_PERMISSIONS",true);

require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");

@set_time_limit(0);
@ignore_user_abort(true);

//including 'search' module
if(CModule::IncludeModule('search'))
{
   //this will be an array containing temporary data
   $NS=Array();

   //two parameters should be defined: 
   //1. max length of the iteration. If equal to zero then no limit for the iteration time is applied
   //2. maximum number of links processed during one step. Should not be too big otherwise it'll reduce script performance
   $sm_max_execution_time = 0;
   $sm_record_lim it = 5000;

   do {

      $cSiteMap = new CSiteMap;

      //perform the iteration
      $NS = $cSiteMap->Create("s1", array($sm_max_execution_time, $sm_record_lim it), $NS); 

      //CSiteMap::Create method returns array if another interation is necessary. 
      //It returns 'true' or 'false' if process has or hasn't been finished successfully
   } while(is_array($NS));

}

require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_after.php");

?>



We'll place this file in the /bitrix/php_interface/ folder. Now, all that is left is to set up 'cron' to launch it periodically.

As this method for creating a Sitemap doesn’t have an internal check to prevent it from running the same task several times at once, it's recommended to run the script once a day (or less frequently), preferably at a time when the number of visitors is minimal.

For example, the following crontab line will launch this script at midnight every day:
Code
0 0 * * * /usr/bin/php -f /var/www/bitrix/php_interface/sitemap_generation.php


You may need to change the path to the PHP on your server as well as the DOCUMENT_ROOT path to your server document root folder to make script work in your environment.
Partner Program
Free Online Training
Subscribe to Bitrix News