Your IP : 18.221.163.78
<? require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/seo/admin/seo_sitemap_run.php");
// Сущность с приоритетами
/**
* Class SitemapPriorityTable
*
* Fields:
* <ul>
* <li> ID int mandatory
* <li> UF_REGEXP string optional
* <li> UF_PRIORITY double optional
* <li> UF_SORT double optional
* </ul>
*
**/
class SitemapPriorityTable extends \Bitrix\Main\Entity\DataManager
{
/**
* Returns DB table name for entity.
*
* @return string
*/
public static function getTableName()
{
return 'sitemap_priority';
}
/**
* Returns entity map definition.
*
* @return array
*/
public static function getMap()
{
return array(
'ID' => array(
'data_type' => 'integer',
'primary' => true,
'autocomplete' => true,
'title' => 'ID',
),
'UF_REGEXP' => array(
'data_type' => 'text',
'title' => 'Регулярное выражение',
),
'UF_PRIORITY' => array(
'data_type' => 'float',
'title' => 'Приоритет',
),
'UF_SORT' => array(
'data_type' => 'float',
'title' => 'Сортировка',
),
);
}
/** Возвращает все записи таблицы, отсортированные по полю SORT **/
public static function getArray(){
$ob = self::getList(array(
'select' => array('REGEXP' => 'UF_REGEXP', 'PRIORITY' => 'UF_PRIORITY', 'SORT' => 'UF_SORT'),
'order' => array('UF_SORT' => 'asc')
));
$arPriority = array();
while($ar = $ob->fetch()){
$arPriority[] = $ar;
}
return $arPriority;
}
}
// Обработчик
class SitemapPriority {
private $priority;
public function __construct(){
$this->priority = SitemapPriorityTable::getArray();
}
public function getIncludedFiles(){
$xml = simplexml_load_file($_SERVER["DOCUMENT_ROOT"].'/sitemap.xml');
$arFiles = array();
// Get Files to Process
foreach ($xml->sitemap as $file) {
$filename = parse_url((string) $file->loc);
$arFiles[] = $filename['path'];
}
return $arFiles;
}
public function getPriority($url){
foreach ($this->priority as $p) {
if(preg_match('|'.$p['REGEXP'].'|', $url)){
return $p['PRIORITY'];
}
}
return 0;
}
private function deleteIncludedFile($filename){
unlink($_SERVER["DOCUMENT_ROOT"].$filename);
}
public function parse(){
$arFiles = $this->getIncludedFiles();
$res = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset></urlset>');
$res->addAttribute('xmlns', "http://www.sitemaps.org/schemas/sitemap/0.9");
foreach ($arFiles as $filename) {
$xml = simplexml_load_file($_SERVER["DOCUMENT_ROOT"].$filename);
foreach ($xml->url as $url) {
$u = $res->AddChild('url');
$u->AddChild('loc', (string) $url->loc);
$u->AddChild('lastmod', (string) $url->lastmod);
$parsedUrl = parse_url((string) $url->loc);
$u->AddChild('priority', $this->getPriority($parsedUrl['path']));
}
$this->deleteIncludedFile($filename);
}
$res->asXML($_SERVER["DOCUMENT_ROOT"].'/sitemap.xml');
}
}
// Срабатывает на последнем шаге генерации карты:
if($_REQUEST['action'] == 'sitemap_run' && check_bitrix_sessid() && $v >= $arValueSteps['index']){
$parser = new SitemapPriority();
$parser->parse();
}