예제 #1
0
        /// <summary>
        /// Regenerates sitemap for all configured sites
        /// </summary>
        public void RegenerateSitemap()
        {
            EnsureSitemapsDirectoryExists();

            ReadConfigurations();

            if (SiteConfigurations.Count == 0)
            {
                Sitecore.Diagnostics.Log.Warn(Messages.ExecutionInterrupted, this);
                return;
            }

            GenerateSitemaps();
            GenerateSitemapsIndex();

            List <String> sitemapUrls = new List <String>();

            if (DynamicSitemapConfiguration.UseSitemapsIndexFile)
            {
                sitemapUrls.Add(SitemapIndex.Url);
            }

            else
            {
                sitemapUrls.AddRange(SiteConfigurations.Select(x => x.SitemapUrl));
            }

            _robotsService.RegisterSitemapToRobotsFile(sitemapUrls);

            if (DynamicSitemapConfiguration.IsProductionEnvironment)
            {
                var submitter = new SitemapSubmitterService(SitecoreConfiguration, SiteConfigurations, Database);
                submitter.SubmitSitemapsToSearchEngines();
            }
        }
        /// <summary>
        /// Registers sitemaps to robots.txt
        /// </summary>
        public virtual void RegisterSitemapToRobotsFile()
        {
            if (DynamicSitemapConfiguration.RefreshRobotsFile)
            {
                String robotsPath = MainUtil.MapPath("/" + "robots.txt");

                StringBuilder stringBuilder = new StringBuilder(String.Empty);

                if (File.Exists(robotsPath))
                {
                    StreamReader streamReader = new StreamReader(robotsPath);
                    stringBuilder.Append(streamReader.ReadToEnd());
                    streamReader.Close();
                }

                StreamWriter streamWriter = new StreamWriter(robotsPath, false);

                List <String> sitemapUrls = new List <String>();

                if (DynamicSitemapConfiguration.UseSitemapsIndexFile)
                {
                    sitemapUrls.Add(SitemapIndex.Url);
                }

                else
                {
                    sitemapUrls.AddRange(SiteConfigurations.Select(x => x.SitemapUrl));
                }

                foreach (var url in sitemapUrls)
                {
                    String value = "Sitemap: " + url;

                    if (!stringBuilder.ToString().Contains(value))
                    {
                        if (!stringBuilder.ToString().EndsWith(Environment.NewLine) && stringBuilder.ToString().Trim() != String.Empty)
                        {
                            stringBuilder.AppendLine();
                        }

                        stringBuilder.AppendLine(value);
                    }
                }

                streamWriter.Write(stringBuilder.ToString());
                streamWriter.Close();
            }
        }