/
SiteMapValidator.cs
76 lines (70 loc) · 2.71 KB
/
SiteMapValidator.cs
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
using Sitecore;
using Sitecore.Data.Items;
using System.Text;
using System.Text.RegularExpressions;
namespace SitecoreThinker.Feature.SEO.Sitemap
{
public static class SiteMapValidator
{
public static int MaxURLsPerSiteMap
{
get
{
string sitemapLimitPath = Sitecore.Context.Site?.StartPath?.Replace("/Home", "/Settings/Sitemap Limits");
if (!string.IsNullOrWhiteSpace(sitemapLimitPath))
{
Item siteMapLimitItem = Sitecore.Context.Database.GetItem(sitemapLimitPath);
if (siteMapLimitItem != null)
{
string maxURLsValue = siteMapLimitItem["Sitemap max URLs count"];
int maxURLs;
if (int.TryParse(maxURLsValue, out maxURLs))
return maxURLs;
}
}
return 50000;
}
}
public static long MaxSiteMapSizeInBytes
{
get
{
long defaultMaxSize = StringUtil.ParseSizeString("50MB");
string sitemapLimitPath = Sitecore.Context.Site?.StartPath?.Replace("/Home", "/Settings/Sitemap Limits");
if (!string.IsNullOrWhiteSpace(sitemapLimitPath))
{
Item siteMapLimitItem = Sitecore.Context.Database.GetItem(sitemapLimitPath);
if (siteMapLimitItem != null)
{
string maxSizeInMBValue = siteMapLimitItem["Sitemap max size"];
long setSize = StringUtil.ParseSizeString(maxSizeInMBValue);
return setSize <= defaultMaxSize ? setSize : defaultMaxSize;
}
}
return defaultMaxSize;
}
}
public static bool IsSiteMapURLsLimitValid(string siteMap)
{
return Regex.Matches(siteMap, "<loc>").Count <= MaxURLsPerSiteMap;
}
public static bool IsSiteMapSizeValid(string siteMap)
{
double size = Encoding.UTF8.GetByteCount(siteMap);
if (size > MaxSiteMapSizeInBytes)
return false;
return true;
}
public static bool IsSiteMapValid(string siteMap)
{
return IsSiteMapSizeValid(siteMap) && IsSiteMapURLsLimitValid(siteMap);
}
public static bool IsNestedSiteMap(string url)
{
Regex rg = new Regex(@"(?i)(sitemap)(\d+)((_)(\d+))*(.xml)");
string sitemapFileName = url.Substring(url.LastIndexOf("/") + 1);
Match match = rg.Match(url);
return match.Success;
}
}
}