private static void HandleChildren(PageData data, string oldUrl, CultureInfo cultureInfo, SystemRedirectReason systemRedirectReason) { var languageSelector = new LanguageSelector(cultureInfo.Name); var pageDataCollection = DataFactory.Instance.GetChildren(data.PageLink, languageSelector); foreach (var pageData in pageDataCollection) { var oldChildUrl = SystemRedirectsHelper.Combine(oldUrl, pageData.URLSegment); AddRedirects(pageData, oldChildUrl, systemRedirectReason); HandleChildren(pageData, oldChildUrl, cultureInfo, systemRedirectReason); } }
private void PublishedContentHandler(object sender, ContentEventArgs e) { if (Configuration.Configuration.AddAutomaticRedirects == false) { return; } var lastVersion = ContentVersionRepository .Service .List(e.ContentLink) .Where(p => p.Status == VersionStatus.PreviouslyPublished) .OrderByDescending(p => p.Saved) .FirstOrDefault(); if (lastVersion == null) { return; } var oldUrl = GetContentUrl(lastVersion.ContentLink, lastVersion.LanguageBranch); if (oldUrl == null) { return; } var newUrl = GetContentUrl(e.ContentLink, e.Content.LanguageBranch()); if (oldUrl == newUrl) { return; } var lastVersionPageData = ContentRepository.Service.Get <IContentData>(lastVersion.ContentLink) as PageData; if (lastVersionPageData == null) { return; } SystemRedirectsActions.AddRedirects(lastVersionPageData, oldUrl, SystemRedirectsHelper.GetCultureInfo(e), SystemRedirectReason.PublishedContent); }
private static void AddRedirects(PageData pageData, string oldUrl, SystemRedirectReason systemRedirectReason) { if (!(pageData.Status == VersionStatus.PreviouslyPublished || pageData.Status == VersionStatus.Published)) { return; } var redirectRule = RedirectRule.NewFromSystem( UrlPath.NormalizePath(oldUrl), pageData.ContentLink.ID, RedirectType.Permanent, RedirectRuleType.ExactMatch, SystemRedirectsHelper.GetSystemRedirectReason(systemRedirectReason)); var redirectRuleRepository = ServiceLocator.Current.GetInstance <IRedirectRuleRepository>(); try { redirectRuleRepository.Add(redirectRule); } catch (ApplicationException) { } }
private void SavedContentHandler(object sender, ContentEventArgs e) { if (Configuration.Configuration.AddAutomaticRedirects == false) { return; } var oldUrl = e.Items[OldUrlKey]?.ToString(); if (oldUrl == null) { return; } var newUrl = UrlResolver.Service.GetUrl(e.ContentLink, null); if (newUrl != oldUrl) { var pageData = ContentRepository.Service.Get <IContentData>(e.ContentLink) as PageData; SystemRedirectsActions.AddRedirects(pageData, oldUrl, SystemRedirectsHelper.GetCultureInfo(e), SystemRedirectReason.SavedContent); } e.Items.Remove(OldUrlKey); }