private static IList<XElement> GenerateXmlElements(IEnumerable<PageReference> pages,
                                                        ISet<string> urlSet,
                                                        SitemapData sitemapData,
                                                        ISitemapXmlGenerator sitemapGenerator)
        {
            IList<XElement> sitemapXmlElements = new List<XElement>();

            var baseUrl = string.IsNullOrEmpty(sitemapData.SiteUrl)
                             ? Settings.Instance.SiteUrl.ToString()
                             : sitemapData.SiteUrl;

            foreach (PageReference pageReference in pages)
            {
                var languagePages = DataFactory.Instance.GetLanguageBranches(pageReference);

                foreach (var page in languagePages)
                {
                    if (urlSet.Count >= MaxSitemapEntryCount)
                    {
                        sitemapData.ExceedsMaximumEntryCount = true;
                        return sitemapXmlElements;
                    }

                    AddFilteredPageElement(page, baseUrl, urlSet, sitemapData, sitemapGenerator, sitemapXmlElements);
                }
            }

            return sitemapXmlElements;
        }
        /// <summary>
        /// Generates a xml sitemap about pages on site
        /// </summary>
        /// <param name="sitemapData">SitemapData object containing configuration info for sitemap</param>
        /// <param name="entryCount">out count of site entries in generated sitemap</param>
        /// <returns>True if sitemap generation successful, false if error encountered</returns>
        public bool Generate(SitemapData sitemapData, out int entryCount)
        {
            try
            {
                XElement sitemap = SitemapContentHelper.CreateSitemapXmlContents(sitemapData, out entryCount);

                var doc = new XDocument(new XDeclaration("1.0", "utf-8", null));
                doc.Add(sitemap);

                using (var ms = new MemoryStream())
                {
                    var xtw = new XmlTextWriter(ms, Encoding.UTF8);
                    doc.Save(xtw);
                    xtw.Flush();
                    sitemapData.Data = ms.ToArray();
                }

                sitemapRepository.Save(sitemapData);
                return true;
            }
            catch (Exception e)
            {
                Log.Error("Error on generating xml sitemap" + Environment.NewLine + e);
                entryCount = 0;
                return false;
            }
        }
        public void Save(SitemapData sitemapData)
        {
            if (sitemapData == null)
            {
                return;
            }

            SitemapStore.Save(sitemapData);
        }
 private static SitemapData CreateDefaultConfig()
 {
     var blankConfig = new SitemapData
     {
         Host = "sitemap.xml",
         IncludeDebugInfo = false,
         SitemapFormat = SitemapFormat.Standard
     };
     
     return blankConfig;
 }
        public static bool IsUrlFiltered(string url, SitemapData sitemapConfig)
        {
            IList<string> whiteList = sitemapConfig.PathsToInclude;
            IList<string> blackList = sitemapConfig.PathsToAvoid;

            if (IsNotInWhiteList(url, whiteList) || IsInBlackList(url, blackList))
            {
                return true;
            }

            return false;
        }
        /// <summary>
        /// Creates xml content for a given sitemap configuration entity
        /// </summary>
        /// <param name="sitemapData">sitemap configuration object</param>
        /// <param name="entryCount">out: count of sitemap entries in the returned element</param>
        /// <returns>XElement that contains sitemap entries according to the configuration</returns>
        public static XElement CreateSitemapXmlContents(SitemapData sitemapData, out int entryCount)
        {
            ISitemapXmlGenerator sitemapGenerator = GetSitemapXmlGenerator(sitemapData);

            var sitemapElement = sitemapGenerator.GenerateRootElement();

            ISet<string> urlSet = new HashSet<string>();

            sitemapElement.Add(GetSitemapXmlElements(sitemapData, sitemapGenerator, urlSet));

            entryCount = urlSet.Count;
            return sitemapElement;
        }
        private void GenerateSitemaps(SitemapData sitemapConfig, StringBuilder message)
        {
            int entryCount;
            bool success = _sitemapXmlGeneratorFactory.GetSitemapXmlGenerator(sitemapConfig).Generate(sitemapConfig, out entryCount);

            if (success)
            {
                message.Append(string.Format("<br/>\"{0}{1}\": {2} entries", sitemapConfig.SiteUrl, sitemapConfig.Host, entryCount));
            }
            else
            {
                message.Append("<br/>Error creating sitemap for \"" + sitemapConfig.Host + "\"");
            }
        }
        private void GenerateSitemaps(SitemapData sitemapConfig, StringBuilder builder)
        {
            int entryCount;
            var success = sitemapService.Generate(sitemapConfig, out entryCount);

            if (success)
            {
                builder.Append(string.Format("<br/>\"{0}\": {1} entries", sitemapConfig.Host, entryCount));
            }
            else
            {
                builder.Append("<br/>Error creating sitemap for \"" + sitemapConfig.Host + "\"");
            }
        }
        private void GenerateSitemaps(SitemapData sitemapConfig, StringBuilder message)
        {
            int entryCount;
            _currentGenerator = _sitemapXmlGeneratorFactory.GetSitemapXmlGenerator(sitemapConfig);
            bool success = _currentGenerator.Generate(sitemapConfig, true, out entryCount);

            if (success)
            {
                message.Append(string.Format("<br/>\"{0}{1}\": {2} entries", sitemapConfig.SiteUrl, _sitemapRepository.GetHostWithLanguage(sitemapConfig), entryCount));
            }
            else
            {
                message.Append("<br/>Error creating sitemap for \"" + _sitemapRepository.GetHostWithLanguage(sitemapConfig) + "\"");
            }
        }
        private bool GetSitemapData(SitemapData sitemapData)
        {
            int entryCount;
            string userAgent = Request.ServerVariables["USER_AGENT"];

            var isGoogleBot = userAgent != null &&
                              userAgent.IndexOf("Googlebot", StringComparison.InvariantCultureIgnoreCase) > -1;

            string googleBotCacheKey = isGoogleBot ? "Google-" : string.Empty;

            if (SitemapSettings.Instance.EnableRealtimeSitemap)
            {
                string cacheKey = googleBotCacheKey + _sitemapRepository.GetSitemapUrl(sitemapData);

                var sitemapDataData = CacheManager.Get(cacheKey) as byte[];

                if (sitemapDataData != null)
                {
                    sitemapData.Data = sitemapDataData;
                    return true;
                }

                if (_sitemapXmlGeneratorFactory.GetSitemapXmlGenerator(sitemapData).Generate(sitemapData, false, out entryCount))
                {
                    if (SitemapSettings.Instance.EnableRealtimeCaching)
                    {
                        CacheEvictionPolicy cachePolicy;

                        if (isGoogleBot)
                        {
                            cachePolicy = new CacheEvictionPolicy(null, new[] {DataFactoryCache.VersionKey}, null, Cache.NoSlidingExpiration, CacheTimeoutType.Sliding);
                        }
                        else
                        {
                            cachePolicy = null;
                        }

                        CacheManager.Insert(cacheKey, sitemapData.Data, cachePolicy);
                    }

                    return true;
                }

                return false;
            }

            return _sitemapXmlGeneratorFactory.GetSitemapXmlGenerator(sitemapData).Generate(sitemapData, !SitemapSettings.Instance.EnableRealtimeSitemap, out entryCount);
        }
        public string GetHostWithLanguage(SitemapData sitemapData)
        {
            if (string.IsNullOrWhiteSpace(sitemapData.Language))
            {
                return sitemapData.Host.ToLowerInvariant();
            }

            var languageBranch = _languageBranchRepository.Load(sitemapData.Language);

            if (languageBranch != null)
            {
                return string.Format("{0}/{1}", languageBranch.CurrentUrlSegment, sitemapData.Host).ToLowerInvariant();
            }

            return sitemapData.Host.ToLowerInvariant();
        }
        private static IList<XElement> GetSitemapXmlElements(SitemapData sitemapData, 
                                                            ISitemapXmlGenerator sitemapGenerator, 
                                                            ISet<string> urlSet)
        {
            var rootPage = sitemapData.RootPageId == 0
                               ? PageReference.RootPage
                               : new PageReference(sitemapData.RootPageId);

            var descendants = DataFactory.Instance.GetDescendents(rootPage);

            if (rootPage != PageReference.RootPage)
            {
                descendants.Add(rootPage);
            }

            return GenerateXmlElements(descendants, urlSet, sitemapData, sitemapGenerator);
        }
        private static ISitemapXmlGenerator GetSitemapXmlGenerator(SitemapData sitemapData)
        {
            ISitemapXmlGenerator xmlGenerator;

            switch (sitemapData.SitemapFormat)
            {
                case SitemapFormat.Mobile:
                    xmlGenerator = new MobileSitemapXmlGenerator();
                    break;

                default:
                    xmlGenerator = new StandardSitemapXmlGenerator();
                    break;
            }

            xmlGenerator.IsDebugMode = sitemapData.IncludeDebugInfo;

            return xmlGenerator;
        }
        public ISitemapXmlGenerator GetSitemapXmlGenerator(SitemapData sitemapData)
        {
            ISitemapXmlGenerator xmlGenerator;

            switch (sitemapData.SitemapFormat)
            {
                case SitemapFormat.Mobile:
                    xmlGenerator = ServiceLocator.Current.GetInstance<IMobileSitemapXmlGenerator>();
                    break;
                case SitemapFormat.Commerce:
                    xmlGenerator = ServiceLocator.Current.GetInstance<ICommerceSitemapXmlGenerator>();
                    break;
                case SitemapFormat.StandardAndCommerce:
                    xmlGenerator = ServiceLocator.Current.GetInstance<ICommerceAndStandardSitemapXmlGenerator>();
                    break;
                default:
                    xmlGenerator = ServiceLocator.Current.GetInstance<IStandardSitemapXmlGenerator>();
                    break;
            }

            xmlGenerator.IsDebugMode = sitemapData.IncludeDebugInfo;

            return xmlGenerator;
        } 
        private void InsertSitemapData(ListViewItem insertItem)
        {
            var sitemapData = new SitemapData
                {
                    SiteUrl = GetSelectedSiteUrl(insertItem),
                    Host = ((TextBox) insertItem.FindControl("txtHost")).Text + SitemapHostPostfix,
                    PathsToAvoid = GetDirectoryList(insertItem, "txtDirectoriesToAvoid"),
                    PathsToInclude = GetDirectoryList(insertItem, "txtDirectoriesToInclude"),
                    IncludeDebugInfo = ((CheckBox) insertItem.FindControl("cbIncludeDebugInfo")).Checked,
                    SitemapFormat = IsMobileSitemapFormatChecked(insertItem) ? SitemapFormat.Mobile : SitemapFormat.Standard,
                    RootPageId = TryParse(((TextBox) insertItem.FindControl("txtRootPageId")).Text)
                };

            sitemapRepository.Save(sitemapData);

            CloseInsert();
            BindList();
        }
        private void InsertSitemapData(ListViewItem insertItem)
        {
            var sitemapData = new SitemapData
            {
                SiteUrl = GetSelectedSiteUrl(insertItem),
                Host = ((TextBox)insertItem.FindControl("txtHost")).Text + SitemapHostPostfix,
                Language = ((DropDownList)insertItem.FindControl("ddlLanguage")).SelectedValue,
                EnableLanguageFallback = ((CheckBox)insertItem.FindControl("cbEnableLanguageFallback")).Checked,
                IncludeAlternateLanguagePages = ((CheckBox)insertItem.FindControl("cbIncludeAlternateLanguagePages")).Checked,
                PathsToAvoid = GetDirectoryList(insertItem, "txtDirectoriesToAvoid"),
                PathsToInclude = GetDirectoryList(insertItem, "txtDirectoriesToInclude"),
                IncludeDebugInfo = ((CheckBox)insertItem.FindControl("cbIncludeDebugInfo")).Checked,
                SitemapFormat = GetSitemapFormat(insertItem),
                RootPageId = TryParse(((TextBox)insertItem.FindControl("txtRootPageId")).Text)
            };

            SitemapRepository.Service.Save(sitemapData);

            CloseInsert();
            BindList();
        }
 public void Save(SitemapData sitemapData)
 {
     sitemapRepository.Save(sitemapData);
 }
        /// <summary>
        /// Generates a xml sitemap about pages on site
        /// </summary>
        /// <param name="sitemapData">SitemapData object containing configuration info for sitemap</param>
        /// <param name="persistData">True if the sitemap data should be persisted in DDS</param>
        /// <param name="entryCount">out count of site entries in generated sitemap</param>
        /// <returns>True if sitemap generation successful, false if error encountered</returns>
        public virtual bool Generate(SitemapData sitemapData, bool persistData, out int entryCount)
        {
            try
            {

                this.SitemapData = sitemapData;
                var sitemapSiteUri = new Uri(this.SitemapData.SiteUrl);
                this.SiteSettings = GetSiteDefinitionFromSiteUri(sitemapSiteUri);
                this._hostLanguageBranch = GetHostLanguageBranch();
                XElement sitemap = CreateSitemapXmlContents(out entryCount);

                var doc = new XDocument(new XDeclaration("1.0", "utf-8", null));
                doc.Add(sitemap);

                using (var ms = new MemoryStream())
                {
                    var xtw = new XmlTextWriter(ms, Encoding.UTF8);
                    doc.Save(xtw);
                    xtw.Flush();
                    sitemapData.Data = ms.ToArray();
                }

                if (persistData && !_stopGeneration)
                {
                    this.SitemapRepository.Save(sitemapData);
                }

                return true;
            }
            catch (Exception e)
            {
                Log.Error("Error on generating xml sitemap" + Environment.NewLine + e);
                entryCount = 0;
                return false;
            }
        }
        private static void AddFilteredPageElement(PageData page,
                                                string baseUrl,
                                                ISet<string> urlSet,
                                                SitemapData sitemapData,
                                                ISitemapXmlGenerator sitemapGenerator,
                                                IList<XElement> xmlElements)
        {
            // filter the page
            if (PageFilter.FilterPage(page))
            {
                return;
            }

            // get page url
            string contentUrl = UrlHelper.GetContentUrl(page);
            string fullPageUrl = UrlHelper.CombineUrl(baseUrl, contentUrl);

            // filter url
            if (urlSet.Contains(fullPageUrl) || UrlFilter.IsUrlFiltered(contentUrl, sitemapData))
            {
                return;
            }

            // get xml element
            var pageElement = sitemapGenerator.GenerateSiteElement(page, fullPageUrl);

            xmlElements.Add(pageElement);
            urlSet.Add(fullPageUrl);
        }
 public string GetSitemapUrl(SitemapData sitemapData)
 {
     return string.Format("{0}{1}", sitemapData.SiteUrl, GetHostWithLanguage(sitemapData));
 }