private void BuildListSiteMap(SitemapIndex index, string sitemapPath, IEnumerable <ContentListDecorator> lists)
        {
            foreach (var list in lists)
            {
                var _recreate = false;
                var sitemap   = new Sitemap.Sitemap();
                var fileName  = list.Web.Name + "-" + list.Name + "." + list.Locale.ToLower();
                if (System.IO.File.Exists(sitemapPath + fileName + ".xml"))
                {
                    var fileInfo = new FileInfo(sitemapPath + fileName + ".xml");
                    if ((list.LastModified - fileInfo.CreationTime).Days == 0)
                    {
                        sitemap.LastModified = fileInfo.CreationTime.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        _recreate = true;
                    }
                }
                else
                {
                    _recreate = true;
                }

                if (_recreate)
                {
                    var _pageUrlset = new SitemapUrlset()
                    {
                        Urls = new List <SitemapUrl>()
                    };
                    //var pages = web.Pages.Where(p => p.ShowInSitemap && p.AllowAnonymous).OrderByDescending(p => p.LastModified);
                    var approved = (int)ModerateStates.Approved;
                    var notset   = (int)ModerateStates.Notset;
                    var items    = list.Items.Where(i => i.IsCurrentVersion && i.IsPublished && (i.ModerateState == approved || i.ModerateState == notset) && i.ShareID == Guid.Empty);

                    if (items.Count() == 0)
                    {
                        continue;
                    }

                    foreach (var item in items)
                    {
                        var itemWrapper = App.Get().Wrap(item);
                        _pageUrlset.Urls.Add(new SitemapUrl()
                        {
                            Url              = itemWrapper.UrlComponent,
                            LastModified     = itemWrapper.Published.Value.ToString("yyyy-MM-dd"),
                            Priority         = 0.5M,
                            ChangeFrequently = SitemapChangeFrequently.Weekly
                        });
                    }
                    sitemap.LastModified = list.LastModified.ToString("yyyy-MM-dd");
                    DNA.Utility.XmlSerializerUtility.SerializeToXmlFile(sitemapPath + fileName + ".xml", typeof(SitemapUrlset), _pageUrlset);
                }

                sitemap.Location = App.Get().Context.AppUrl.ToString() + "webshared/home/sitemaps/" + fileName + ".xml";
                index.SiteMapFiles.Add(sitemap);
            }
        }
Example #2
0
        private async Task DoPing(string url)
        {
            do
            {
                using (var client = new PingerWebClient("Mozilla/5.0 (compatible; Pingerbot/0.2)"))
                {
                    SitemapIndex index = null;
                    try
                    {
                        using (var reader = await client.GetAsync(url))
                        {
                            var s = new XmlSerializer(typeof(SitemapIndex));

                            index = (SitemapIndex)s.Deserialize(await reader.Content.ReadAsStreamAsync());
                        }
                    }
                    catch (Exception exc)
                    {
                        logger.Error(exc, "Error on first call");
                        await Task.Delay(30 * 1000);

                        continue;
                    }

                    logger.Info("sitemaps to ping -> " + index.Sitemaps.Count);

                    foreach (var sitemap in index.Sitemaps)
                    {
                        UrlSet urlSet = null;
                        using (var reader = await client.GetAsync(sitemap.loc))
                        {
                            var s = new XmlSerializer(typeof(UrlSet));
                            urlSet = (UrlSet)s.Deserialize(await reader.Content.ReadAsStreamAsync());
                        }

                        logger.Info("Urls to ping -> " + urlSet.Urls.Count);

                        foreach (var urlToPing in urlSet.Urls)
                        {
                            logger.Info("Pinging " + urlToPing.loc);
                            try
                            {
                                await client.GetAsync(new Uri(urlToPing.loc));
                            }
                            catch (Exception exc)
                            {
                                logger.Error(exc, $"Error downloading page: {urlToPing.loc}");
                                //throw new Exception("Error downloading page: " + urlToPing.loc, exc);
                            }
                            await Task.Delay(30 * 1000);
                        }
                    }
                }
            } while (true);
        }
        private bool AssertCorrectSitemapIndexWasSerialized(IEnumerable<SitemapInfo> expectedSitemaps, SitemapIndex actualSitemapIndex)
        {
            foreach (var expectedSitemap in expectedSitemaps)
            {
                if (!actualSitemapIndex.Sitemaps.Contains(expectedSitemap))
                {
                    Assert.Fail("Received a call to .SerializeAndSave, but at least one of the expected sitemapInfos was missing.");
                }
            }

            return true;
        }
        public async Task <IActionResult> Create(CreateSitemapIndexViewModel model)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageSitemaps))
            {
                return(Forbid());
            }

            var sitemap = new SitemapIndex
            {
                SitemapId = _sitemapIdGenerator.GenerateUniqueId()
            };

            var indexSource = new SitemapIndexSource
            {
                Id = _sitemapIdGenerator.GenerateUniqueId()
            };

            sitemap.SitemapSources.Add(indexSource);

            if (ModelState.IsValid)
            {
                await _sitemapService.ValidatePathAsync(model.Path, _updateModelAccessor.ModelUpdater);
            }

            // Path validation may invalidate model state.
            if (ModelState.IsValid)
            {
                sitemap.Name    = model.Name;
                sitemap.Enabled = model.Enabled;
                sitemap.Path    = model.Path;

                indexSource.ContainedSitemapIds = model.ContainableSitemaps
                                                  .Where(m => m.IsChecked)
                                                  .Select(m => m.SitemapId)
                                                  .ToArray();

                await _sitemapManager.UpdateSitemapAsync(sitemap);

                _notifier.Success(H["Sitemap index created successfully"]);

                return(RedirectToAction(nameof(List)));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Example #5
0
        private void UpdateIndexSitemap(DateTime lastModified)
        {
            var logFile = File.Create(Path.Combine(_environment.WebRootPath, "sitemap.xml"));

            using (var stream = new StreamWriter(logFile))
            {
                var sitemap = new SitemapIndex(stream);
                sitemap.WriteStartDocument();

                sitemap.WriteItem($"{_websiteDomainName}/sitemap-pages.xml", lastModified);
                sitemap.WriteItem($"{_websiteDomainName}/sitemap-posts.xml", lastModified);
                sitemap.WriteItem($"{_websiteDomainName}/sitemap-categories.xml", lastModified);

                sitemap.WriteEndDocument();
                sitemap.Close();
            }
        }
        public void It_Saves_The_XML_File_To_The_Correct_Directory_And_File_Name()
        {
            //--arrange
            var directory = new DirectoryInfo(_sitemapLocation);
            var fileName  = "sitemapindex.xml";

            var sitemapIndex = new SitemapIndex(new List <SitemapInfo>
            {
                new SitemapInfo(new Uri("http://example.com/sitemap1.xml"), DateTime.UtcNow),
                new SitemapInfo(new Uri("http://example.com/sitemap2.xml"), DateTime.UtcNow.AddDays(-1))
            });

            //--act
            var result = _serializer.SerializeAndSave(
                sitemapIndex,
                directory,
                fileName);

            Assert.True(result.PhysicalPath.Contains("sitemapindex"));
            Assert.AreEqual(fileName, result.Name);
        }
        public void It_Saves_The_XML_File_To_The_Correct_Directory_And_File_Name()
        {
            //--arrange
            var directory = new DirectoryInfo("x");
            string fileName = "sitemapindex.xml";

            var sitemapIndex = new SitemapIndex(new List<SitemapInfo>
            {
                new SitemapInfo(new Uri("http://example.com/sitemap1.xml"), DateTime.UtcNow),
                new SitemapInfo(new Uri("http://example.com/sitemap2.xml"), DateTime.UtcNow.AddDays(-1))
            });

            //--act
            _autoMocker.ClassUnderTest.SerializeAndSave(
                sitemapIndex,
                directory,
                fileName);

            //--assert
            _autoMocker.Get<IFileSystemWrapper>().Received().WriteFile(
                Arg.Is<string>(x => x.Contains("<sitemapindex")), 
                Arg.Is<DirectoryInfo>(x => x == directory), 
                Arg.Is<string>(x => x == fileName));
        }
Example #8
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);
        }
Example #9
0
        public void It_Saves_The_XML_File_To_The_Correct_Directory_And_File_Name()
        {
            //--arrange
            var    directory = new DirectoryInfo("x");
            string fileName  = "sitemapindex.xml";

            var sitemapIndex = new SitemapIndex(new List <SitemapInfo>
            {
                new SitemapInfo(new Uri("http://example.com/sitemap1.xml"), DateTime.UtcNow),
                new SitemapInfo(new Uri("http://example.com/sitemap2.xml"), DateTime.UtcNow.AddDays(-1))
            });

            //--act
            _autoMocker.ClassUnderTest.SerializeAndSave(
                sitemapIndex,
                directory,
                fileName);

            //--assert
            _autoMocker.Get <IFileSystemWrapper>().Received().WriteFile(
                Arg.Is <string>(x => x.Contains("<sitemapindex")),
                Arg.Is <DirectoryInfo>(x => x == directory),
                Arg.Is <string>(x => x == fileName));
        }
        private bool AssertCorrectSitemapIndexWasSerialized(IEnumerable <SitemapInfo> expectedSitemaps, SitemapIndex actualSitemapIndex)
        {
            foreach (var expectedSitemap in expectedSitemaps)
            {
                if (!actualSitemapIndex.Sitemaps.Contains(expectedSitemap))
                {
                    Assert.Fail("Received a call to .SerializeAndSave, but at least one of the expected sitemapInfos was missing.");
                }
            }

            return(true);
        }
        private void WriteSitemapIndex(SitemapIndex sitemapIndex, Guid website)
        {
            var filePath = Path.Combine(Config.GetWorkingDirectory(website), SitemapIndexFilename);

            sitemapIndex.WriteToXml(filePath);
        }
Example #12
0
 public void Add(SitemapIndex o) => SitemapIndexes.Add(o);
        public ActionResult Sitemap()
        {
            var    app         = App.Get();
            string sitemapPath = Server.MapPath("~/app_data/files/public/sitemaps/");

            if (!Directory.Exists(sitemapPath))
            {
                Directory.CreateDirectory(sitemapPath);
            }

            var sitemapIndex = new SitemapIndex();

            sitemapIndex.SiteMapFiles = new System.Collections.Generic.List <Sitemap.Sitemap>();

            foreach (var web in app.Webs)
            {
                var _recreate = false;
                var sitemap   = new Sitemap.Sitemap();

                if (System.IO.File.Exists(sitemapPath + web.Name + ".xml"))
                {
                    var fileInfo = new FileInfo(sitemapPath + web.Name + ".xml");
                    if ((DateTime.Now - fileInfo.CreationTime).Days == 0)
                    {
                        sitemap.LastModified = fileInfo.CreationTime.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        _recreate = true;
                    }
                }
                else
                {
                    _recreate = true;
                }

                if (_recreate)
                {
                    var _pageUrlset = new SitemapUrlset()
                    {
                        Urls = new List <SitemapUrl>()
                    };
                    var pages = web.Pages.Where(p => p.ShowInSitemap && p.AllowAnonymous).OrderByDescending(p => p.LastModified);

                    foreach (var page in pages)
                    {
                        if (page.IsShared)
                        {
                            continue;
                        }

                        _pageUrlset.Urls.Add(new SitemapUrl()
                        {
                            Url              = page.FullUrl,
                            LastModified     = page.LastModified.ToString("yyyy-MM-dd"),
                            Priority         = 0.5M,
                            ChangeFrequently = SitemapChangeFrequently.Weekly
                        });
                    }
                    sitemap.LastModified = DateTime.Now.ToString("yyyy-MM-dd");
                    DNA.Utility.XmlSerializerUtility.SerializeToXmlFile(sitemapPath + web.Name + ".xml", typeof(SitemapUrlset), _pageUrlset);
                }

                BuildListSiteMap(sitemapIndex, sitemapPath, web.Lists);

                sitemap.Location = app.Context.AppUrl.ToString() + "webshared/home/sitemaps/" + web.Name + ".xml";
                sitemapIndex.SiteMapFiles.Add(sitemap);
            }

            if (sitemapIndex.SiteMapFiles.Count == 1)
            {
                var sitemapFile = app.NetDrive.MapPath(new Uri(sitemapIndex.SiteMapFiles.FirstOrDefault().Location));
                return(File(sitemapFile, "text/xml"));
            }
            else
            {
                var xml = DNA.Utility.XmlSerializerUtility.SerializeToXml(sitemapIndex);
                return(Content(xml, "text/xml"));
            }
        }
Example #14
0
 public static string ComposeSitemapIndex(SitemapIndex o) => Compose(o);
Example #15
0
 public SitemapEngine()
 {
     _SitemapIndex = new SitemapIndex();
 }