public async Task GetContentItemsAsync(ContentTypesSitemapSource source, ContentItemsQueryContext context)
        {
            var routeableContentTypeDefinitions = _routeableContentTypeCoordinator.ListRoutableTypeDefinitions();

            if (source.IndexAll)
            {
                var rctdNames = routeableContentTypeDefinitions.Select(rctd => rctd.Name);

                var queryResults = await _session.Query <ContentItem>()
                                   .With <ContentItemIndex>(x => x.Published && x.ContentType.IsIn(rctdNames))
                                   .OrderBy(x => x.CreatedUtc)
                                   .ListAsync();

                context.ContentItems = queryResults;
            }
            else if (source.LimitItems)
            {
                // Test that content type is still valid to include in sitemap.
                var typeIsValid = routeableContentTypeDefinitions
                                  .Any(ctd => String.Equals(source.LimitedContentType.ContentTypeName, ctd.Name));

                if (typeIsValid)
                {
                    var queryResults = await _session.Query <ContentItem>()
                                       .With <ContentItemIndex>(x => x.ContentType == source.LimitedContentType.ContentTypeName && x.Published)
                                       .OrderBy(x => x.CreatedUtc)
                                       .Skip(source.LimitedContentType.Skip)
                                       .Take(source.LimitedContentType.Take)
                                       .ListAsync();

                    context.ContentItems = queryResults;
                }
            }
            else
            {
                // Test that content types are still valid to include in sitemap.
                var typesToIndex = routeableContentTypeDefinitions
                                   .Where(ctd => source.ContentTypes.Any(s => String.Equals(ctd.Name, s.ContentTypeName)))
                                   .Select(x => x.Name);

                var queryResults = await _session.Query <ContentItem>()
                                   .With <ContentItemIndex>(x => x.ContentType.IsIn(typesToIndex) && x.Published)
                                   .OrderBy(x => x.CreatedUtc)
                                   .ListAsync();

                context.ContentItems = queryResults;
            }
        }
        public override async Task BuildSourceAsync(ContentTypesSitemapSource source, SitemapBuilderContext context)
        {
            var queryContext = new ContentItemsQueryContext();
            await _contentItemsQueryProvider.GetContentItemsAsync(source, queryContext);

            foreach (var sciemp in _sitemapContentItemExtendedMetadataProviders)
            {
                context.Response.ResponseElement.Add(sciemp.GetExtendedAttribute);
            }

            foreach (var contentItem in queryContext.ContentItems)
            {
                var url = new XElement(Namespace + "url");

                if (await BuildUrlsetMetadataAsync(source, context, queryContext, contentItem, url))
                {
                    context.Response.ResponseElement.Add(url);
                }
            }
        }
        public async Task <bool> ApplyExtendedMetadataAsync(
            SitemapBuilderContext context,
            ContentItemsQueryContext queryContext,
            ContentItem contentItem,
            XElement url)
        {
            var part = contentItem.As <LocalizationPart>();

            if (part == null)
            {
                return(true);
            }

            var localizedContentParts = queryContext.ReferenceContentItems
                                        .Select(ci => ci.As <LocalizationPart>())
                                        .Where(cp => cp.LocalizationSet == part.LocalizationSet);

            foreach (var localizedPart in localizedContentParts)
            {
                var sitemapMetadataAspect = await _contentManager.PopulateAspectAsync <SitemapMetadataAspect>(localizedPart.ContentItem);

                if (sitemapMetadataAspect.Exclude)
                {
                    continue;
                }

                var hrefValue = await _routeableContentTypeCoordinator.GetRouteAsync(context, localizedPart.ContentItem);

                var linkNode = new XElement(ExtendedNamespace + "link",
                                            new XAttribute("rel", "alternate"),
                                            new XAttribute("hreflang", localizedPart.Culture),
                                            new XAttribute("href", hrefValue));

                url.Add(linkNode);
            }

            return(true);
        }
        private async Task <bool> BuildExtendedMetadataAsync(SitemapBuilderContext context, ContentItemsQueryContext queryContext, ContentItem contentItem, XElement url)
        {
            var suceeded = true;

            foreach (var sc in _sitemapContentItemExtendedMetadataProviders)
            {
                if (!await sc.ApplyExtendedMetadataAsync(context, queryContext, contentItem, url))
                {
                    suceeded = false;
                }
            }
            return(suceeded);
        }
        private async Task <bool> BuildUrlsetMetadataAsync(ContentTypesSitemapSource source, SitemapBuilderContext context, ContentItemsQueryContext queryContext, ContentItem contentItem, XElement url)
        {
            if (await BuildUrlAsync(context, contentItem, url))
            {
                if (await BuildExtendedMetadataAsync(context, queryContext, contentItem, url))
                {
                    PopulateLastMod(contentItem, url);
                    await PopulateChangeFrequencyPriority(source, contentItem, url);

                    return(true);
                }

                return(false);
            }
            ;

            return(false);
        }
Example #6
0
        public async Task GetContentItems(ContentTypesSitemapSource source, ContentItemsQueryContext queryContext)
        {
            var routeableContentTypeDefinitions = _routeableContentTypeCoordinator.ListRoutableTypeDefinitions();

            if (source.IndexAll)
            {
                // Assumption here is that at least one content type will be localized.
                var rctdNames = routeableContentTypeDefinitions.Select(rctd => rctd.Name);

                var queryResults = await _session.Query <ContentItem>()
                                   .With <ContentItemIndex>(x => x.Published && x.ContentType.IsIn(rctdNames))
                                   .OrderBy(x => x.CreatedUtc)
                                   .ListAsync();

                queryContext.ContentItems = queryResults;

                // Provide all content items with localization as reference content items.
                queryContext.ReferenceContentItems = queryResults
                                                     .Where(ci => ci.Has <LocalizationPart>());
            }
            else if (source.LimitItems)
            {
                // Test that content type is still valid to include in sitemap.
                var contentType = routeableContentTypeDefinitions
                                  .FirstOrDefault(ctd => String.Equals(source.LimitedContentType.ContentTypeName, ctd.Name));

                if (contentType == null)
                {
                    return;
                }

                if (contentType.Parts.Any(ctd => String.Equals(ctd.Name, nameof(LocalizationPart))))
                {
                    // Get all content items here for reference. Then reduce by default culture.
                    // We know that the content item should be localized.
                    // If it doesn't have a localization part, the content item should have been saved.
                    var queryResults = await _session.Query <ContentItem>()
                                       .With <ContentItemIndex>(ci => ci.ContentType == source.LimitedContentType.ContentTypeName && ci.Published)
                                       .OrderBy(ci => ci.CreatedUtc)
                                       .With <LocalizedContentItemIndex>()
                                       .ListAsync();

                    // When limiting items Content item is valid if it is for the default culture.
                    var defaultCulture = await _localizationService.GetDefaultCultureAsync();

                    // Reduce by default culture.
                    var items = queryResults
                                .Where(ci => String.Equals(ci.As <LocalizationPart>().Culture, defaultCulture))
                                .Skip(source.LimitedContentType.Skip)
                                .Take(source.LimitedContentType.Take);

                    queryContext.ContentItems = items;

                    // Provide all content items with localization as reference content items.
                    queryContext.ReferenceContentItems = queryResults
                                                         .Where(ci => ci.Has <LocalizationPart>());
                }
                else
                {
                    // Content type is not localized. Produce standard results.
                    var queryResults = await _session.Query <ContentItem>()
                                       .With <ContentItemIndex>(x => x.ContentType == source.LimitedContentType.ContentTypeName && x.Published)
                                       .OrderBy(x => x.CreatedUtc)
                                       .Skip(source.LimitedContentType.Skip)
                                       .Take(source.LimitedContentType.Take)
                                       .ListAsync();

                    queryContext.ContentItems = queryResults;
                }
            }
            else
            {
                // Test that content types are still valid to include in sitemap.
                var typesToIndex = routeableContentTypeDefinitions
                                   .Where(ctd => source.ContentTypes.Any(s => String.Equals(ctd.Name, s.ContentTypeName)))
                                   .Select(x => x.Name);

                // No advantage here in reducing with localized index.
                var queryResults = await _session.Query <ContentItem>()
                                   .With <ContentItemIndex>(x => x.ContentType.IsIn(typesToIndex) && x.Published)
                                   .OrderBy(x => x.CreatedUtc)
                                   .ListAsync();

                queryContext.ContentItems = queryResults;

                // Provide all content items with localization as reference content items.
                queryContext.ReferenceContentItems = queryResults
                                                     .Where(ci => ci.Has <LocalizationPart>());
            }
        }