/// <summary> /// The build sitemap index. /// </summary> /// <param name="fileNames"> /// The file names. /// </param> /// <param name="site"> /// The site. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> private string BuildSitemapIndex(IEnumerable <string> fileNames, SiteDefinition site) { var serverUrl = SitemapManagerConfiguration.GetServerUrlBySite(site); var doc = new XmlDocument(); var declarationNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(declarationNode); var siteMapIndexNode = doc.CreateElement("sitemapindex"); var xmlNsAttr = doc.CreateAttribute("xmlns"); xmlNsAttr.Value = SitemapManagerConfiguration.XmlNsTpl; siteMapIndexNode.Attributes.Append(xmlNsAttr); doc.AppendChild(siteMapIndexNode); foreach (var filename in fileNames) { var url = $"{serverUrl}/{ HttpUtility.HtmlEncode(filename.Replace("\\", "/"))}"; var lastMod = HttpUtility.HtmlEncode(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz")); var sitemapNode = doc.CreateElement("sitemap"); siteMapIndexNode.AppendChild(sitemapNode); var locNode = doc.CreateElement("loc"); sitemapNode.AppendChild(locNode); locNode.AppendChild(doc.CreateTextNode(url)); var lastModNode = doc.CreateElement("lastmod"); sitemapNode.AppendChild(lastModNode); lastModNode.AppendChild(doc.CreateTextNode(lastMod)); } return(doc.OuterXml); }
/// <summary> /// The register to robots fields. /// </summary> public void RegisterToRobotsFields() { foreach (DictionaryEntry site in this.sites) { var siteName = site.Key.ToString(); var defRep = ServiceLocator.Current.GetInstance <ISiteDefinitionRepository>(); var siteDef = defRep.Get(siteName); var serverUrl = SitemapManagerConfiguration.GetServerUrlBySite(siteDef); var sitemapLine = string.Concat("Sitemap: ", serverUrl, "sitemap.xml"); var rep = ServiceLocator.Current.GetInstance <IContentRepository>(); var startPage = rep.Get <PageData>(siteDef.StartPage); // Save robots content from field if (startPage == null) { continue; } var robotsFromField = string.Empty; var robotsFromFieldProperty = startPage.GetType().GetProperty("RobotsContent"); if (robotsFromFieldProperty?.GetValue(startPage) is string) { robotsFromField = robotsFromFieldProperty.GetValue(startPage) as string; } if (string.IsNullOrEmpty(robotsFromField) || !robotsFromField.Contains(sitemapLine)) { robotsFromField = sitemapLine + Environment.NewLine; var startPageWritable = ((PageData)startPage).CreateWritableClone(); if (startPageWritable != null) { var robotsFromFieldPropertyWritable = startPageWritable.GetType().GetProperty("RobotsContent"); robotsFromFieldPropertyWritable?.SetValue(startPageWritable, robotsFromField); rep.Save( (PageData)startPageWritable, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess); } } } }
/// <summary> /// The register to robots file. /// </summary> public void RegisterToRobotsFile() { var robotsPath = System.Web.Hosting.HostingEnvironment.MapPath(string.Concat("/", "robots.txt")); var sitemapContent = new StringBuilder(string.Empty); if (robotsPath == null) { return; } if (File.Exists(robotsPath)) { var sr = new StreamReader(robotsPath); sitemapContent.Append(sr.ReadToEnd()); sr.Close(); } var sw = new StreamWriter(robotsPath, false); foreach (DictionaryEntry site in this.sites) { var siteName = site.Key.ToString(); var filename = site.Value.ToString(); var defRep = ServiceLocator.Current.GetInstance <ISiteDefinitionRepository>(); var siteDef = defRep.Get(siteName); var serverUrl = SitemapManagerConfiguration.GetServerUrlBySite(siteDef); var sitemapLine = string.Concat("Sitemap: ", serverUrl, SitemapSubFolder, "/", filename); if (!sitemapContent.ToString().Contains(sitemapLine)) { sitemapContent.AppendLine(sitemapLine); } } sw.Write(sitemapContent.ToString()); sw.Close(); }
/// <summary> /// The generate site maps. /// </summary> /// <param name="currentDataBase"> /// The current data base. /// </param> public void GenerateSiteMaps(SitemapConfigurationDataStore currentDataBase = null) { LogHelper.Information("START: SitemapManager.SitemapManager", typeof(SitemapGenerator)); this.dataFromBase = currentDataBase ?? this.sitemapManagerConfigurator.GetFirstData() ?? new SitemapConfigurationDataStore(); this.sites = SitemapManagerConfiguration.GetSites(this.dataFromBase); this.maxUrls = int.Parse(ConfigurationManager.AppSettings["SiteMap_MAX_URLS"]); this.showLocalizedUrls = bool.Parse(ConfigurationManager.AppSettings["Show_Localized_URLS"]); this.displayLocalizedUrlsSeparated = bool.Parse(ConfigurationManager.AppSettings["Display_Localized_URLS_Separated"]); this.showTrailingSlash = bool.Parse(ConfigurationManager.AppSettings["Show_Trailing_Slash"]); var languageRep = ServiceLocator.Current.GetInstance <ILanguageBranchRepository>(); this.defaultLanguage = languageRep.LoadFirstEnabledBranch(); LogHelper.Information("SitemapManager.SitemapManager: # Sites: " + this.sites.Count, typeof(SitemapGenerator)); foreach (DictionaryEntry site in this.sites) { this.BuildSiteMap(site.Key.ToString(), site.Value.ToString()); } LogHelper.Information("END: SitemapManager.SitemapManager", typeof(SitemapGenerator)); }