function Create($site_id, $max_execution_time, $NS, $arOptions = array())
{
@set_time_limit(0);
if (!is_array($NS))
{
$NS = Array(
"ID" => 0,
"CNT" => 0,
"FILE_SIZE" => 0,
"FILE_ID" => 1,
"FILE_URL_CNT" => 0,
"ERROR_CNT" => 0,
"PARAM2" => 0,
);
}
else
{
$NS = Array(
"ID" => intval($NS["ID"]),
"CNT" => intval($NS["CNT"]),
"FILE_SIZE" => intval($NS["FILE_SIZE"]),
"FILE_ID" => intval($NS["FILE_ID"]),
"FILE_URL_CNT" => intval($NS["FILE_URL_CNT"]),
"ERROR_CNT" => intval($NS["ERROR_CNT"]),
"PARAM2" => intval($NS["ID"]),
);
}
if (is_array($max_execution_time))
{
$record_limit = $max_execution_time[1];
$max_execution_time = $max_execution_time[0];
}
else
{
$record_limit = 5000;
}
if ($max_execution_time > 0)
{
$end_of_execution = time() + $max_execution_time;
}
else
{
$end_of_execution = 0;
}
if (is_array($arOptions) && ($arOptions["FORUM_TOPICS_ONLY"] == "Y"))
$bForumTopicsOnly = CModule::IncludeModule("forum");
else
$bForumTopicsOnly = false;
if (is_array($arOptions) && ($arOptions["BLOG_NO_COMMENTS"] == "Y"))
$bBlogNoComments = CModule::IncludeModule("blog");
else
$bBlogNoComments = false;
if (is_array($arOptions) && ($arOptions["USE_HTTPS"] == "Y"))
$strProto = "https://";
else
$strProto = "http://";
$rsSite = CSite::GetByID($site_id);
if ($arSite = $rsSite->Fetch())
{
$SERVER_NAME = trim($arSite["SERVER_NAME"]);
if ($SERVER_NAME == '')
{
$this->m_error = GetMessage("SEARCH_ERROR_SERVER_NAME", array("#SITE_ID#" => ''.htmlspecialcharsbx($site_id).''))."
";
return false;
}
//Cache events
$this->m_events = GetModuleEvents("search", "OnSearchGetURL", true);
//Clear error file
if ($NS["ID"] == 0 && $NS["CNT"] == 0)
{
$e = fopen($arSite["ABS_DOC_ROOT"].$arSite["DIR"]."sitemap_errors.xml", "w");
$strBegin = "nn";
fwrite($e, $strBegin);
}
//Or open it for append
else
{
$e = fopen($arSite["ABS_DOC_ROOT"].$arSite["DIR"]."sitemap_errors.xml", "a");
}
if (!$e)
{
$this->m_error = GetMessage("SEARCH_ERROR_OPEN_FILE")." ".$arSite["ABS_DOC_ROOT"].$arSite["DIR"]."sitemap_errors.xml"."
";
return false;
}
//Open current sitemap file
if ($NS["FILE_SIZE"] == 0)
{
$f = fopen($arSite["ABS_DOC_ROOT"].$arSite["DIR"]."sitemap_".sprintf("%03d", $NS["FILE_ID"]).".xml", "w");
$strBegin = "nn";
fwrite($f, $strBegin);
$NS["FILE_SIZE"] += mb_strlen($strBegin);
}
else
{
$f = fopen($arSite["ABS_DOC_ROOT"].$arSite["DIR"]."sitemap_".sprintf("%03d", $NS["FILE_ID"]).".xml", "a");
}
if (!$f)
{
$this->m_error = GetMessage("SEARCH_ERROR_OPEN_FILE")." ".$arSite["ABS_DOC_ROOT"].$arSite["DIR"]."sitemap_".sprintf("%03d", $NS["FILE_ID"]).".xml"."
";
return false;
}
CTimeZone::Disable();
$this->GetURLs($site_id, $NS["ID"], $record_limit);
$bFileIsFull = false;
while (!$bFileIsFull && $ar = $this->Fetch())
{
$record_limit--;
$NS["ID"] = $ar["ID"];
if (mb_strlen($ar["URL"]) < 1)
continue;
if ($bForumTopicsOnly && ($ar["MODULE_ID"] == "forum"))
{
//Forum topic ID
$PARAM2 = intval($ar["PARAM2"]);
if ($NS["PARAM2"] < $PARAM2)
{
$NS["PARAM2"] = $PARAM2;
$arTopic = CForumTopic::GetByIDEx($PARAM2);
if ($arTopic)
$ar["FULL_DATE_CHANGE"] = $arTopic["LAST_POST_DATE"];
}
else
{
continue;
}
}
if ($bBlogNoComments && ($ar["MODULE_ID"] == "blog"))
{
if (mb_substr($ar["ITEM_ID"], 0, 1) === "C")
continue;
}
if (preg_match("/^[a-z]+:\/\//", $ar["URL"]))
$strURL = $ar["URL"];
else
$strURL = $strProto.$ar["SERVER_NAME"].$ar["URL"];
$strURL = $this->LocationEncode($this->URLEncode($strURL, "UTF-8"));
$strTime = $this->TimeEncode(MakeTimeStamp(ConvertDateTime($ar["FULL_DATE_CHANGE"], "DD.MM.YYYY HH:MI:SS"), "DD.MM.YYYY HH:MI:SS"));
$strToWrite = "tntt".$strURL."ntt".$strTime."ntn";
if (mb_strlen($strURL) > 2048)
{
fwrite($e, $strToWrite);
$NS["ERROR_CNT"]++;
}
else
{
$NS["CNT"]++;
$NS["FILE_SIZE"] += fwrite($f, $strToWrite);
$NS["FILE_URL_CNT"]++;
}
//Next File on file size or url count limit
if ($NS["FILE_SIZE"] > 9000000 || $NS["FILE_URL_CNT"] >= 50000)
{
$bFileIsFull = true;
}
elseif ($end_of_execution)
{
if (time() > $end_of_execution)
{
fclose($e);
fclose($f);
CTimeZone::Enable();
return $NS;
}
}
}
CTimeZone::Enable();
if ($bFileIsFull)
{
fwrite($e, "n");
fclose($e);
fwrite($f, "n");
fclose($f);
$NS["FILE_SIZE"] = 0;
$NS["FILE_URL_CNT"] = 0;
$NS["FILE_ID"]++;
return $NS;
}
elseif ($record_limit <= 0)
{
return $NS;
}
else
{
fwrite($e, "n");
fclose($e);
fwrite($f, "n");
fclose($f);
}
//WRITE INDEX FILE HERE
$f = fopen($arSite["ABS_DOC_ROOT"].$arSite["DIR"]."sitemap_index.xml", "w");
if (!$f)
{
$this->m_error = GetMessage("SEARCH_ERROR_OPEN_FILE")." ".$arSite["ABS_DOC_ROOT"].$arSite["DIR"]."sitemap_index.xml"."
";
return false;
}
$strBegin = "nn";
fwrite($f, $strBegin);
for ($i = 0; $i <= $NS["FILE_ID"]; $i++)
{
$strFile = $arSite["DIR"]."sitemap_".sprintf("%03d", $i).".xml";
$strTime = $this->TimeEncode(filemtime($arSite["ABS_DOC_ROOT"].$strFile));
fwrite($f, "tntt".$this->URLEncode($strProto.$arSite["SERVER_NAME"].$strFile, "UTF-8")."ntt".$strTime."ntn");
}
fwrite($f, "n");
fclose($f);
$this->m_errors_count = $NS["ERROR_CNT"];
$this->m_errors_href = $strProto.$arSite["SERVER_NAME"].$arSite["DIR"]."sitemap_errors.xml";
$this->m_href = $strProto.$arSite["SERVER_NAME"].$arSite["DIR"]."sitemap_index.xml";
return true;
}
else
{
$this->m_error = GetMessage("SEARCH_ERROR_SITE_ID")."
";
return false;
}
}