public IEnumerable <Models.Sitemap> GenerateSitemaps(SitemapParams param) { if (string.IsNullOrWhiteSpace(param.BaseUrl)) { throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.BaseUrl)), nameof(param)); } if (string.IsNullOrWhiteSpace(param.Scope)) { throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.Scope)), nameof(param)); } if (param.Culture == null) { throw new ArgumentException(GetMessageOfNull(nameof(param.Culture)), nameof(param)); } var iterationIndex = 1; var offset = 0; do { var entries = EntryProvider.GetEntriesAsync( param, culture: param.Culture, offset: offset, count: NumberOfEntriesPerSitemap ).Result; var isEntriesNotEnough = entries.Count() < NumberOfEntriesPerSitemap; if (entries.Any()) { yield return(new Models.Sitemap { Name = isEntriesNotEnough && iterationIndex == 1 ? GetSitemapName(param.Culture) : GetSitemapName(param.Culture, iterationIndex), Entries = entries.ToArray(), }); offset += NumberOfEntriesPerSitemap; iterationIndex += 1; if (isEntriesNotEnough) { break; } } else { break; } }while (true); }
public IEnumerable <Models.Sitemap> GenerateSitemaps(SitemapParams sitemapParams) { Guard.NotNullOrWhiteSpace(sitemapParams.BaseUrl, nameof(sitemapParams.BaseUrl)); Guard.NotNullOrWhiteSpace(sitemapParams.Scope, nameof(sitemapParams.Scope)); Guard.NotNull(sitemapParams.Culture, nameof(sitemapParams.Culture)); var iterationIndex = 1; var offset = 0; do { var entries = EntryProvider.GetEntriesAsync( sitemapParams, culture: sitemapParams.Culture, offset: offset, count: NumberOfEntriesPerSitemap ).Result; var isEntriesNotEnough = entries.Count() < NumberOfEntriesPerSitemap; if (entries.Any()) { yield return(new Models.Sitemap { Name = isEntriesNotEnough && iterationIndex == 1 ? GetSitemapName(sitemapParams.Culture) : GetSitemapName(sitemapParams.Culture, iterationIndex), Entries = entries.ToArray(), }); offset += NumberOfEntriesPerSitemap; iterationIndex += 1; if (isEntriesNotEnough) { break; } } else { break; } }while (true); }
public void GenerateSitemaps(Guid website, string baseUrl, string baseSitemapUrl, params CultureInfo[] cultures) { Log.Info("Starting sitemaps generation"); var stopwatch = Stopwatch.StartNew(); Guard.NotNullOrWhiteSpace(baseUrl, nameof(baseUrl)); Guard.NotNullOrEmpty(cultures, nameof(cultures)); if (website == Guid.Empty) { throw new ArgumentNullException(nameof(website)); } lock (_exclusiveLock) { var sitemapDirectory = Config.GetSitemapDirectory(website); EnsureDirectoryExists(sitemapDirectory); var tasks = new List <Task>(); var sitemapNames = new List <string>(); try { EnsureDirectoryExists(Config.GetWorkingDirectory(website)); foreach (var culture in cultures) { try { string scope = SiteConfiguration.GetPublishedScopeId(culture, website); foreach (var provider in Providers) { // Start a new task for each provider. // For example we can generate content + product sitemaps at the same time. tasks.Add(Task.Factory.StartNew(() => { Log.Info($"Generating sitemap (type:{provider.GetType()}) for {culture.Name} in {scope} scope."); var sitemapParams = new SitemapParams { Website = website, BaseUrl = baseUrl, Scope = scope, Culture = culture }; try { foreach (var sitemap in provider.GenerateSitemaps(sitemapParams)) { // Write sitemap to disk Log.Info($"Writing sitemap {sitemap.Name}"); WriteSitemap(sitemap, website); // Add sitemap name to the list for the index creation later lock (sitemapNames) { sitemapNames.Add(sitemap.Name); } } } catch (Exception e) { Log.Error(e.ToString()); } })); } } catch (ArgumentException) { } } Task.WhenAll(tasks).Wait(); // Write sitemap index var index = IndexGenerator.Generate(baseSitemapUrl, sitemapNames); WriteSitemapIndex(index, website); // Deploy sitemaps and sitemap index Log.Info($"Deploying sitemaps to {sitemapDirectory}"); DeploySitemaps(website); } finally { DeleteWorkingDirectory(); } // Log stopwatch duration Log.Info($"Sitemaps generation completed. Generation took {stopwatch.Elapsed.TotalSeconds} seconds."); } }