示例#1
0
        public override async Task PublishedAsync(PublishContentContext context)
        {
            var cacheContext = new SitemapCacheContext
            {
                CacheObject = context.ContentItem,
                Sitemaps    = await _sitemapManager.ListSitemapsAsync()
            };

            await _sitemapCacheManager.ClearCacheAsync(cacheContext);
        }
示例#2
0
        public async Task <IActionResult> List(SitemapIndexListOptions options, PagerParameters pagerParameters)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageSitemaps))
            {
                return(Forbid());
            }

            var siteSettings = await _siteService.GetSiteSettingsAsync();

            var pager = new Pager(pagerParameters, siteSettings.PageSize);

            // default options
            if (options == null)
            {
                options = new SitemapIndexListOptions();
            }

            var sitemaps = (await _sitemapManager.ListSitemapsAsync())
                           .OfType <SitemapIndex>();

            if (!string.IsNullOrWhiteSpace(options.Search))
            {
                sitemaps = sitemaps.Where(smp => smp.Name.Contains(options.Search));
            }

            var count = sitemaps.Count();

            var results = sitemaps
                          .Skip(pager.GetStartIndex())
                          .Take(pager.PageSize)
                          .ToList();

            // Maintain previous route data when generating page links
            var routeData = new RouteData();

            routeData.Values.Add("Options.Search", options.Search);

            var pagerShape = (await New.Pager(pager)).TotalItemCount(count).RouteData(routeData);

            var model = new ListSitemapIndexViewModel
            {
                SitemapIndexes = results.Select(sm => new SitemapIndexListEntry {
                    SitemapId = sm.SitemapId, Name = sm.Name, Enabled = sm.Enabled
                }).ToList(),
                Options = options,
                Pager   = pagerShape
            };

            return(View(model));
        }
        public async Task ValidatePathAsync(string path, IUpdateModel updater, string sitemapId = null)
        {
            // Keep localized text as similar to Autoroute as possible.
            if (path == "/")
            {
                updater.ModelState.AddModelError(Prefix, Path, S["Your permalink can't be set to the homepage"]);
            }

            if (path.IndexOfAny(InvalidCharactersForPath) > -1 || path.IndexOf(' ') > -1 || path.IndexOf("//") > -1)
            {
                var invalidCharactersForMessage = string.Join(", ", InvalidCharactersForPath.Select(c => $"\"{c}\""));
                updater.ModelState.AddModelError(Prefix, Path, S["Please do not use any of the following characters in your permalink: {0}. No spaces, or consecutive slashes, are allowed (please use dashes or underscores instead).", invalidCharactersForMessage]);
            }

            // Precludes possibility of collision with Autoroute as Autoroute excludes . as a valid path character.
            if (!path.EndsWith(SitemapPathExtension))
            {
                updater.ModelState.AddModelError(Prefix, Path, S["Your permalink must end with {0}.", SitemapPathExtension]);
            }

            if (path.Length > MaxPathLength)
            {
                updater.ModelState.AddModelError(Prefix, Path, S["Your permalink is too long. The permalink can only be up to {0} characters.", MaxPathLength]);
            }

            var routeExists = false;

            if (string.IsNullOrEmpty(sitemapId))
            {
                routeExists = (await _sitemapManager.ListSitemapsAsync())
                              .Any(p => String.Equals(p.Path, path, StringComparison.OrdinalIgnoreCase));
            }
            else
            {
                routeExists = (await _sitemapManager.ListSitemapsAsync())
                              .Any(p => p.SitemapId != sitemapId && String.Equals(p.Path, path, StringComparison.OrdinalIgnoreCase));
            }

            if (routeExists)
            {
                updater.ModelState.AddModelError(Prefix, Path, S["Your permalink is already in use."]);
            }
        }
        public override async Task BuildSitemapTypeAsync(SitemapIndex sitemap, SitemapBuilderContext context)
        {
            context.Response = new SitemapResponse
            {
                ResponseElement = new XElement(Namespace + "sitemapindex",
                                               new XAttribute(XNamespace.Xmlns + "xsi", SchemaInstance),
                                               new XAttribute(SchemaInstance + "schemaLocation", SchemaLocation))
            };

            var indexSource = sitemap.SitemapSources.FirstOrDefault() as SitemapIndexSource;

            if (indexSource == null)
            {
                return;
            }

            var containedSitemaps = (await _sitemapManager.ListSitemapsAsync())
                                    .Where(s => s.Enabled && indexSource.ContainedSitemapIds.Any(id => id == s.SitemapId));

            foreach (var containedSitemap in containedSitemaps)
            {
                var xmlSitemap = new XElement(Namespace + "sitemap");
                var loc        = new XElement(Namespace + "loc");

                var routeValues = new RouteValueDictionary(_sitemapsOptions.GlobalRouteValues)
                {
                    [_sitemapsOptions.SitemapIdKey] = containedSitemap.SitemapId
                };

                loc.Add(context.HostPrefix + context.UrlHelper.Action(routeValues["Action"].ToString(), routeValues));
                xmlSitemap.Add(loc);

                var lastModDate = await _sitemapModifiedDateProvider.GetLastModifiedDateAsync(containedSitemap);

                if (lastModDate.HasValue)
                {
                    var lastMod = new XElement(Namespace + "lastmod");
                    lastMod.Add(lastModDate.GetValueOrDefault().ToString("yyyy-MM-ddTHH:mm:sszzz"));
                    xmlSitemap.Add(lastMod);
                }

                context.Response.ResponseElement.Add(xmlSitemap);
            }
        }