private void StoreOldRoute(IContent entity, OldRoutesDictionary oldRoutes)
        {
            var contentCache  = _publishedSnapshotAccessor.PublishedSnapshot.Content;
            var entityContent = contentCache.GetById(entity.Id);

            if (entityContent == null)
            {
                return;
            }

            // get the default affected cultures by going up the tree until we find the first culture variant entity (default to no cultures)
            var defaultCultures = entityContent.AncestorsOrSelf()?.FirstOrDefault(a => a.Cultures.Any())?.Cultures.Keys.ToArray()
                                  ?? new[] { (string)null };

            foreach (var x in entityContent.DescendantsOrSelf())
            {
                // if this entity defines specific cultures, use those instead of the default ones
                var cultures = x.Cultures.Any() ? x.Cultures.Keys : defaultCultures;

                foreach (var culture in cultures)
                {
                    var route = contentCache.GetRouteById(x.Id, culture);
                    if (IsNotRoute(route))
                    {
                        continue;
                    }
                    oldRoutes[new ContentIdAndCulture(x.Id, culture)] = new ContentKeyAndOldRoute(x.Key, route);
                }
            }
        }
Exemplo n.º 2
0
    private void CreateRedirects(OldRoutesDictionary oldRoutes)
    {
        if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out IPublishedSnapshot? publishedSnapshot))
        {
            return;
        }

        IPublishedContentCache?contentCache = publishedSnapshot?.Content;

        if (contentCache == null)
        {
            _logger.LogWarning("Could not track redirects because there is no current published snapshot available.");
            return;
        }

        foreach (KeyValuePair <ContentIdAndCulture, ContentKeyAndOldRoute> oldRoute in oldRoutes)
        {
            var newRoute = contentCache.GetRouteById(oldRoute.Key.ContentId, oldRoute.Key.Culture);
            if (IsNotRoute(newRoute) || oldRoute.Value.OldRoute == newRoute)
            {
                continue;
            }

            _redirectUrlService.Register(oldRoute.Value.OldRoute, oldRoute.Value.ContentKey, oldRoute.Key.Culture);
        }
    }
        private OldRoutesDictionary GetOldRoutes(IDictionary <string, object> eventState)
        {
            if (!eventState.ContainsKey(_eventStateKey))
            {
                eventState[_eventStateKey] = new OldRoutesDictionary();
            }

            return(eventState[_eventStateKey] as OldRoutesDictionary);
        }
Exemplo n.º 4
0
    private void CreateRedirectsForOldRoutes(IStatefulNotification notification)
    {
        // don't let the notification handlers kick in if Redirect Tracking is turned off in the config
        if (_webRoutingSettings.CurrentValue.DisableRedirectUrlTracking)
        {
            return;
        }

        OldRoutesDictionary oldRoutes = GetOldRoutes(notification);

        CreateRedirects(oldRoutes);
    }
        private void CreateRedirects(OldRoutesDictionary oldRoutes)
        {
            var contentCache = _publishedSnapshotAccessor.PublishedSnapshot.Content;

            foreach (var oldRoute in oldRoutes)
            {
                var newRoute = contentCache.GetRouteById(oldRoute.Key.ContentId, oldRoute.Key.Culture);
                if (IsNotRoute(newRoute) || oldRoute.Value.OldRoute == newRoute)
                {
                    continue;
                }
                _redirectUrlService.Register(oldRoute.Value.OldRoute, oldRoute.Value.ContentKey, oldRoute.Key.Culture);
            }
        }
Exemplo n.º 6
0
    private void StoreOldRoute(IContent entity, OldRoutesDictionary oldRoutes)
    {
        if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out IPublishedSnapshot? publishedSnapshot))
        {
            return;
        }

        IPublishedContentCache?contentCache  = publishedSnapshot?.Content;
        IPublishedContent?     entityContent = contentCache?.GetById(entity.Id);

        if (entityContent is null)
        {
            return;
        }

        // get the default affected cultures by going up the tree until we find the first culture variant entity (default to no cultures)
        var defaultCultures = entityContent.AncestorsOrSelf().FirstOrDefault(a => a.Cultures.Any())?.Cultures.Keys
                              .ToArray()
                              ?? Array.Empty <string>();

        foreach (IPublishedContent publishedContent in entityContent.DescendantsOrSelf(_variationContextAccessor))
        {
            // if this entity defines specific cultures, use those instead of the default ones
            IEnumerable <string> cultures =
                publishedContent.Cultures.Any() ? publishedContent.Cultures.Keys : defaultCultures;

            foreach (var culture in cultures)
            {
                var route = contentCache?.GetRouteById(publishedContent.Id, culture);
                if (!IsNotRoute(route))
                {
                    oldRoutes[new ContentIdAndCulture(publishedContent.Id, culture)] = new ContentKeyAndOldRoute(publishedContent.Key, route !);
                }
                else if (string.IsNullOrEmpty(culture))
                {
                    // Retry using all languages, if this is invariant but has a variant ancestor
                    var languages = _localizationService.GetAllLanguages();
                    foreach (var language in languages)
                    {
                        route = contentCache?.GetRouteById(publishedContent.Id, language.IsoCode);
                        if (!IsNotRoute(route))
                        {
                            oldRoutes[new ContentIdAndCulture(publishedContent.Id, language.IsoCode)] =
                                new ContentKeyAndOldRoute(publishedContent.Key, route !);
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 7
0
    private void StoreOldRoutes(IEnumerable <IContent> entities, IStatefulNotification notification)
    {
        // don't let the notification handlers kick in if Redirect Tracking is turned off in the config
        if (_webRoutingSettings.CurrentValue.DisableRedirectUrlTracking)
        {
            return;
        }

        OldRoutesDictionary oldRoutes = GetOldRoutes(notification);

        foreach (IContent entity in entities)
        {
            StoreOldRoute(entity, oldRoutes);
        }
    }
Exemplo n.º 8
0
        private void StoreOldRoute(IContent entity, OldRoutesDictionary oldRoutes)
        {
            if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
            {
                return;
            }

            IPublishedContentCache?contentCache  = publishedSnapshot?.Content;
            IPublishedContent?     entityContent = contentCache?.GetById(entity.Id);

            if (entityContent is null)
            {
                return;
            }

            // get the default affected cultures by going up the tree until we find the first culture variant entity (default to no cultures)
            var defaultCultures = entityContent.AncestorsOrSelf()?.FirstOrDefault(a => a.Cultures.Any())?.Cultures.Keys.ToArray()
                                  ?? Array.Empty <string>();

            foreach (IPublishedContent publishedContent in entityContent.DescendantsOrSelf(_variationContextAccessor))
            {
                // if this entity defines specific cultures, use those instead of the default ones
                IEnumerable <string> cultures = publishedContent.Cultures.Any() ? publishedContent.Cultures.Keys : defaultCultures;

                foreach (var culture in cultures)
                {
                    var route = contentCache?.GetRouteById(publishedContent.Id, culture);
                    if (IsNotRoute(route))
                    {
                        continue;
                    }

                    oldRoutes[new ContentIdAndCulture(publishedContent.Id, culture)] = new ContentKeyAndOldRoute(publishedContent.Key, route !);
                }
            }
        }