-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlWarmupPlugin.cs
78 lines (70 loc) · 3.13 KB
/
XmlWarmupPlugin.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
77
78
using Louw.SitemapParser;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Warmup;
namespace Avisra.WarmupPlugins
{
public class XmlSitemapPlugin : IWarmupPlugin
{
public virtual string Name { get; private set; }
public virtual IEnumerable<WarmupUrl> GetUrls()
{
return this.pageUrls.Select(u => new WarmupUrl(u) { Priority = this.priority });
}
public virtual void GetLinksFromSitemap(Sitemap sitemap)
{
if (sitemap.SitemapType == SitemapType.Index)
Log.Write($"Parsing Sitemap Index {sitemap.SitemapLocation.AbsoluteUri} for warmup. Contains {sitemap.Sitemaps.Count()} sitemaps", TraceEventType.Information);
else if (sitemap.SitemapType == SitemapType.Items)
Log.Write($"Parsing Sitemap {sitemap.SitemapLocation.AbsoluteUri} for warmup. Contains {sitemap.Items.Count()} items", TraceEventType.Information);
if (sitemap.Items != null && sitemap.Items.Count() > 0)
{
this.pageUrls = this.pageUrls.Union(sitemap.Items.Where(i => Uri.IsWellFormedUriString(i.Location.AbsoluteUri, UriKind.Absolute)).Select(i => i.Location.AbsoluteUri));
}
if(sitemap.Sitemaps != null && sitemap.Sitemaps.Count() > 0)
{
foreach(var childSitemap in sitemap.Sitemaps)
{
var loadedSitemap = Task.Run<Sitemap>(async () => await childSitemap.LoadAsync(new CustomSitemapFetcher())).Result;
this.GetLinksFromSitemap(loadedSitemap);
}
}
}
public virtual void Initialize(string name, NameValueCollection parameters)
{
this.Name = name;
var priorityKey = parameters[XmlSitemapPlugin.PriorityKey]?.ToLower();
if (!priorityKey.IsNullOrEmpty())
{
switch (priorityKey)
{
case "high":
this.priority = WarmupPriority.High;
break;
case "low":
this.priority = WarmupPriority.Low;
break;
default:
this.priority = WarmupPriority.Normal;
break;
}
}
var sitemapUri = parameters[XmlSitemapPlugin.SitemapUriKey];
if (!sitemapUri.IsNullOrEmpty())
{
var sitemapLink = new Sitemap(new Uri(sitemapUri));
var loadedSitemap = Task.Run<Sitemap>(async () => await sitemapLink.LoadAsync(new CustomSitemapFetcher())).Result;
GetLinksFromSitemap(loadedSitemap);
}
}
private IEnumerable<string> pageUrls = Enumerable.Empty<string>();
private WarmupPriority priority = WarmupPriority.Normal;
private const string SitemapUriKey = "sitemapUri";
private const string PriorityKey = "warmupPriority";
}
}