예제 #1
0
        private void GeneratePostSitemap()
        {
            Sitemap map        = new Sitemap();
            string  outputPath = string.Format(@"{0}\post-sitemap.xml", _sitemapOutputPath);

            foreach (var post in BlogContextManager.PostSummaries.Where(p => p.IsActive == true))
            {
                SitemapLocation location = new SitemapLocation()
                {
                    Url          = string.Format("{0}/{1}", _domain, post.StaticHtml),
                    LastModified = DateTime.Now
                };
                if (post.Images != null)
                {
                    foreach (var image in post.Images)
                    {
                        location.Images.Add(new SitemapImage()
                        {
                            Location = string.Format("{0}/{1}", _domain, image.Url)
                        });
                    }
                }
                map.Add(location);
            }
            map.WriteSitemapToFile(outputPath);
        }
예제 #2
0
        private void GeneratePageSitemap()
        {
            Sitemap map        = new Sitemap();
            string  outputPath = string.Format(@"{0}\page-sitemap.xml", _sitemapOutputPath);

            SitemapLocation l1 = new SitemapLocation()
            {
                Url          = _domain,
                LastModified = DateTime.Now
            };
            SitemapLocation l2 = new SitemapLocation()
            {
                Url          = string.Format("{0}/about", _domain),
                LastModified = DateTime.Now,
                Images       = new List <SitemapImage>()
                {
                    new SitemapImage()
                    {
                        Location = string.Format("{0}/assets/images/me.jpg", _domain)
                    }
                }
            };

            map.Add(l1);
            map.Add(l2);

            map.WriteSitemapToFile(outputPath);
        }
예제 #3
0
        private void GenerateTagSitemap()
        {
            Sitemap map        = new Sitemap();
            string  outputPath = string.Format(@"{0}\post_tag-sitemap.xml", _sitemapOutputPath);

            foreach (var tag in BlogContextManager.Tags)
            {
                SitemapLocation location = new SitemapLocation()
                {
                    Url          = string.Format("{0}/tag/{1}", _domain, tag.Name.Replace(" ", "%20")),
                    LastModified = DateTime.Now
                };
                map.Add(location);
            }
            map.WriteSitemapToFile(outputPath);
        }
예제 #4
0
        private void GenerateCategorySitemap()
        {
            Sitemap map        = new Sitemap();
            string  outputPath = string.Format(@"{0}\category-sitemap.xml", _sitemapOutputPath);

            foreach (var category in BlogContextManager.Categories)
            {
                SitemapLocation location = new SitemapLocation()
                {
                    Url          = string.Format("{0}/category/{1}", _domain, category.Name.Replace(" ", "%20")),
                    LastModified = DateTime.Now
                };
                map.Add(location);
            }
            map.WriteSitemapToFile(outputPath);
        }
예제 #5
0
        private void GenerateSitemap()
        {
            SitemapIndex map = new SitemapIndex();

            string[] urls = new string[] {
                string.Format("{0}/category-sitemap.xml", _domain),
                string.Format("{0}/page-sitemap.xml", _domain),
                string.Format("{0}/post_tag-sitemap.xml", _domain),
                string.Format("{0}/post-sitemap.xml", _domain)
            };

            string outputPath = string.Format(@"{0}\sitemap.xml", _sitemapOutputPath);

            foreach (var url in urls)
            {
                SitemapLocation location = new SitemapLocation()
                {
                    Url          = url,
                    LastModified = DateTime.Now
                };
                map.Add(location);
            }
            map.WriteSitemapToFile(outputPath);
        }
예제 #6
0
        public virtual async Task <Stream> GenerateSitemapXmlAsync(string storeId, string baseUrl, string sitemapUrl, Action <ExportImportProgressInfo> progressCallback = null)
        {
            var stream = new MemoryStream();

            var filenameSeparator   = SettingsManager.GetValue("Sitemap.FilenameSeparator", "--");
            var recordsLimitPerFile = SettingsManager.GetValue("Sitemap.RecordsLimitPerFile", 10000);

            var xmlNamespaces = new XmlSerializerNamespaces();

            xmlNamespaces.Add("", "http://www.sitemaps.org/schemas/sitemap/0.9");
            xmlNamespaces.Add("xhtml", "http://www.w3.org/1999/xhtml");

            var sitemapLocation = SitemapLocation.Parse(sitemapUrl, filenameSeparator);
            var store           = await StoreService.GetByIdAsync(storeId);

            if (sitemapLocation.Location.EqualsInvariant("sitemap.xml"))
            {
                progressCallback?.Invoke(new ExportImportProgressInfo
                {
                    Description = "Creating sitemap.xml..."
                });

                var allStoreSitemaps = await LoadAllStoreSitemaps(store, baseUrl);

                var sitemapIndexXmlRecord = new SitemapIndexXmlRecord();
                foreach (var sitemap in allStoreSitemaps)
                {
                    var xmlSiteMapRecords = sitemap.PagedLocations.Select(location => new SitemapIndexItemXmlRecord
                    {
                        //ModifiedDate = sitemap.Items.Select(x => x.ModifiedDate).OrderByDescending(x => x).FirstOrDefault()?.ToString("yyyy-MM-dd"),
                        Url = SitemapUrlBuilder.BuildStoreUrl(store, store.DefaultLanguage, location, baseUrl)
                    }).ToList();
                    sitemapIndexXmlRecord.Sitemaps.AddRange(xmlSiteMapRecords);
                }
                var xmlSerializer = new XmlSerializer(sitemapIndexXmlRecord.GetType());
                xmlSerializer.Serialize(stream, sitemapIndexXmlRecord, xmlNamespaces);
            }
            else
            {
                var sitemapSearchResult = await SitemapService.SearchAsync(new SitemapSearchCriteria { Location = sitemapLocation.Location, StoreId = storeId, Skip = 0, Take = 1 });

                var sitemap = sitemapSearchResult.Results.FirstOrDefault();
                if (sitemap != null)
                {
                    await LoadSitemapRecords(store, sitemap, baseUrl, progressCallback);

                    var distinctRecords    = sitemap.Items.SelectMany(x => x.ItemsRecords).GroupBy(x => x.Url).Select(x => x.FirstOrDefault());
                    var sitemapItemRecords = distinctRecords.Skip((sitemapLocation.PageNumber - 1) * recordsLimitPerFile).Take(recordsLimitPerFile).ToArray();
                    var sitemapRecord      = new SitemapXmlRecord
                    {
                        Items = sitemapItemRecords.Select(i => new SitemapItemXmlRecord().ToXmlModel(i)).ToList()
                    };
                    if (sitemapRecord.Items.Count > 0)
                    {
                        var xmlSerializer = new XmlSerializer(sitemapRecord.GetType());
                        xmlSerializer.Serialize(stream, sitemapRecord, xmlNamespaces);
                    }
                }
            }
            stream.Seek(0, SeekOrigin.Begin);
            return(stream);
        }