| <%@ WebHandler Language="VB" Class="SiteMap" %> |
| Imports System |
| Imports System.Web |
| Imports System.IO |
| Imports System.Text |
| Imports System.Xml |
| |
| Imports Telerik.Search |
| Imports Telerik.Cms |
| Imports Telerik.Cms.data |
| Imports Telerik |
| Imports Telerik.Cms.Engine |
| |
| Imports Lucene.Net.Documents |
| Imports Lucene.Net.Search |
| |
| |
| Public Class SiteMap |
| Implements IHttpHandler |
| |
| |
| |
| Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest |
| Try |
| |
| Dim response As HttpResponse = context.Response |
| response.ContentType = "text/plain" |
| |
| Dim writer As New XmlTextWriter(response.OutputStream, Encoding.UTF8) |
| |
| writer.Formatting = Formatting.Indented |
| writer.WriteStartDocument() |
| writer.WriteStartElement("urlset") |
| |
| |
| 'writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") |
| 'writer.WriteAttributeString("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd") |
| writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9") |
| |
| 'I'd like to try using searching the index to get the output. |
| 'We could find all the pages in generic index and then look up the |
| 'CmsPage and append the node. |
| 'http://www.aspfree.com/c/a/BrainDump/Working-with-Lucene-dot-Net/4/ |
| |
| |
| Dim pages As New GetCmsPages |
| pages.LoadAll() |
| |
| 'Use this part for your custom modules |
| 'pages = AppendTestimonials(pages) |
| 'pages = AppendNews(pages) |
| |
| For Each page As CmsPage In pages |
| If page.Status = PageStatus.Published = True Then |
| AppendNode(page, writer, 0) |
| End If |
| Next |
| |
| |
| writer.WriteEndElement() |
| writer.WriteEndDocument() |
| |
| response.Flush() |
| writer.Flush() |
| |
| Catch ex As Exception |
| |
| End Try |
| |
| End Sub |
| |
| Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable |
| Get |
| Return True |
| End Get |
| End Property |
| |
| |
| Private Sub AppendNode(ByVal page As CmsPage, ByVal writer As XmlTextWriter, ByVal depth As Integer) |
| |
| Try |
| Dim n As CmsPage |
| Dim subNodeDepth As Integer |
| |
| writer.WriteStartElement("url") |
| writer.WriteElementString("loc", GetURL(page)) |
| writer.WriteElementString("lastmod", GetLastMod(page)) |
| writer.WriteElementString("changefreq", GetChangeFreq(page)) |
| writer.WriteElementString("priority", GetPriority(page)) |
| |
| writer.WriteEndElement() |
| |
| subNodeDepth = depth + 1 |
| |
| 'Not needed because get all pages is called at the beginning. |
| For Each n In page.ChildPages |
| If page.Navigable = True Then |
| AppendNode(n, writer, subNodeDepth) |
| End If |
| Next |
| |
| Catch ex As Exception |
| |
| End Try |
| End Sub |
| |
| Private Function GetURL(ByVal page As CmsPage) As String |
| Try |
| Dim vars As NameValueCollection = HttpContext.Current.Request.ServerVariables |
| Dim port As String = vars("SERVER_PORT") |
| Dim protocol As String = vars("SERVER_PORT_SECURE") |
| Dim urlResolver As New System.Web.UI.Control |
| Dim url As String |
| |
| If (port Is Nothing Or port = "80" Or port = "443") Then |
| port = "" |
| Else |
| port = ":" + port |
| End If |
| |
| If (protocol Is Nothing Or protocol = "0") Then |
| protocol = "http://" |
| Else |
| protocol = "https://" |
| End If |
| |
| url = page.DefaultUrl.Url |
| Return protocol + vars("SERVER_NAME").ToString + port + urlResolver.ResolveUrl(url) |
| |
| Catch ex As Exception |
| Return String.Empty |
| End Try |
| End Function |
| |
| Private Function GetLastMod(ByVal page As CmsPage) As String |
| Try |
| |
| Return Format(page.DateModified, "yyyyy-MM-ddThh:mm:sszzzz") |
| Catch ex As Exception |
| Return String.Empty |
| End Try |
| End Function |
| |
| Private Function GetChangeFreq(ByVal page As CmsPage) As String |
| Try |
| Dim intervalString As String = String.Empty |
| Dim intervalLong As Long = DateDiff(DateInterval.Hour, page.DateModified, Now) |
| |
| If intervalLong < 1 Then |
| intervalString = "hourly" |
| ElseIf intervalLong <= 24 And intervalLong > 1 Then |
| intervalString = "daily" |
| ElseIf intervalLong <= 168 And intervalLong > 24 Then |
| intervalString = "weekly" |
| ElseIf intervalLong <= 672 And intervalLong > 168 Then |
| intervalString = "monthly" |
| ElseIf intervalLong <= 8766 And intervalLong > 672 Then |
| intervalString = "yearly" |
| End If |
| |
| Return intervalString |
| Catch ex As Exception |
| Return String.Empty |
| End Try |
| End Function |
| |
| Private Function GetPriority(ByVal page As CmsPage) As String |
| Try |
| |
| Return "0.5" |
| |
| Catch ex As Exception |
| Return String.Empty |
| End Try |
| End Function |
| End Class |
| |