-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.php
133 lines (111 loc) · 4.07 KB
/
sitemap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?
include_once(__DIR__ . '/class_sitemap.php');
function GenerateSitemapXmlExtConfig($siteId)
{
$arResult = [];
if ($siteId == 's1') {
/* Инфоблоки */
$arResult['IBLOCK'][] = [
'IBLOCK_ID' => 1, /* ID инфоблока */
'SECTION' => 'Y', /* Y/N - влючать ли разделы инфоблока */
'SECTION_FILTER' => [], /* Фильтр для разделов */
'DETAIL' => 'Y', /* Y/N - влючать ли страницы элементов инфоблока */
'DETAIL_FILTER' => [] /* Фильтр для элементов инфоблока */
];
/* Меню сайта */
$arResult['MENU'] = [
'/.top.menu.php',
'/.bottom.menu.php'
];
/* Добавить урлы */
$arResult['URLS'] = [
'https://site.ru/my_urs/',
'https://site.ru/my_urs2/'
];
/* Игнорируемые урлы */
$arResult['IGNORE'] = [
'https://site.ru/ignore_urs/',
'https://site.ru/ignore_urs2/'
];
/* Максимально кол-во страниц в одном sitemap */
/* Максимальное кол-во по Google и Yandex - 50000 */
$arResult['MAX_URLS_IN_SITEMAP'] = 1000;
}
return $arResult;
}
function GenerateSitemapXmlExt(string $siteId = "s1", bool $useHttps = true)
{
$result = 'GenerateSitemapXmlExt("' . $siteId . '", ' . ($useHttps ? 'true' : 'false') . ');';
if (!\Bitrix\Main\Loader::includeModule("iblock")) {
\CEventLog::Add([
"SEVERITY" => "ERROR",
"AUDIT_TYPE_ID" => "GenerateSitemapXmlExt",
"MODULE_ID" => "",
"ITEM_ID" => "",
"DESCRIPTION" => "Не удалось загрудить модуль iblock",
]);
return $result;
}
$site = \Bitrix\Main\SiteTable::getById($siteId)->fetch();
$http = $useHttps ? 'https://' : 'http://';
$SERVER_HTTP_HOST = $site['SERVER_NAME'];
$home_url = $http . $SERVER_HTTP_HOST;
$document_root = trim($site['DOC_ROOT']);
if ($document_root == '') {
$document_root = \Bitrix\Main\Application::getDocumentRoot();
};
if ($document_root == '') {
$dirs = realpath(dirname(__FILE__));
if (strpos($dirs, '/local/') !== false) {
list($document_root, $trash) = explode('/local/', $dirs);
} elseif (strpos($dirs, '/bitrix/') !== false) {
list($document_root, $trash) = explode('/bitrix/', $dirs);
};
};
$document_root = $document_root.$site['DIR'];
$arConfig = GenerateSitemapXmlExtConfig($siteId);
$arBlocks = $arConfig['IBLOCK'] ? $arConfig['IBLOCK'] : [];
$arMenus = $arConfig['MENU'] ? $arConfig['MENU'] : [];
$items_on_page = $arConfig['MAX_URLS_IN_SITEMAP'] ? $arConfig['MAX_URLS_IN_SITEMAP'] : 50000;
$arUrl = $arConfig['URLS'] ? $arConfig['URLS'] : [];
$arIgnore = $arConfig['IGNORE'] ? $arConfig['IGNORE'] : [];
$sitemap = new sitemapXmlExt($home_url, $arBlocks);
$sitemap->AddPage($home_url);
if (is_array($arMenus)) {
foreach ($arMenus as $menu) {
$sitemap->AddPagesFromMenuFile(str_replace('//', '/', $document_root . $menu), $home_url);
}
}
if (is_array($arUrl)) {
foreach ($arUrl as $url) {
$sitemap->AddPage($url);
}
}
if (is_array($arIgnore)) {
foreach ($arIgnore as $ignore) {
$sitemap->AddIgnorePage($ignore);
}
}
$sitemap->generate();
$total_items = count($sitemap->pages);
$maxfiles = max(1, ceil($total_items / $items_on_page));
if ($maxfiles == 1) {
file_put_contents($document_root . '/sitemap.xml', $sitemap->Show());
} else {
while ($maxfiles > 0) {
$maxfiles--;
$fileSitemap = $document_root . '/sitemap_' . $maxfiles . '.xml';
$sitemap->AddToSitemapIndex($home_url . '/sitemap_' . $maxfiles . '.xml');
file_put_contents($fileSitemap, $sitemap->Show($maxfiles * $items_on_page, $items_on_page));
}
file_put_contents($document_root . '/sitemap.xml', $sitemap->ShowSitemapIndex());
}
\CEventLog::Add([
"SEVERITY" => "INFO",
"AUDIT_TYPE_ID" => "GenerateSitemapXmlExt",
"MODULE_ID" => "",
"ITEM_ID" => "",
"DESCRIPTION" => "Sitemap.xml сформирован $total_items страниц",
]);
return $result;
}