using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Web.PublishedCache;
using Umbraco.Core;
namespace Umbraco.Web.Routing
{
///
/// Provides urls.
///
public class UrlProvider
{
#region Ctor and configuration
///
/// Initializes a new instance of the class with an Umbraco context and a list of url providers.
///
/// The Umbraco context.
/// The list of url providers.
internal UrlProvider(UmbracoContext umbracoContext, IEnumerable urlProviders)
{
_umbracoContext = umbracoContext;
_urlProviders = urlProviders;
var provider = UrlProviderMode.Auto;
Mode = provider;
if (Enum.TryParse(UmbracoConfig.For.UmbracoSettings().WebRouting.UrlProviderMode, out provider))
{
Mode = provider;
}
}
private readonly UmbracoContext _umbracoContext;
private readonly IEnumerable _urlProviders;
///
/// Gets or sets the provider url mode.
///
public UrlProviderMode Mode { get; set; }
#endregion
#region GetUrl
///
/// Gets the url of a published content.
///
/// The published content identifier.
/// The url for the published content.
///
/// The url is absolute or relative depending on Mode and on the current url.
/// If the provider is unable to provide a url, it returns "#".
///
public string GetUrl(int id)
{
return GetUrl(id, _umbracoContext.CleanedUmbracoUrl, Mode);
}
///
/// Gets the nice url of a published content.
///
/// The published content identifier.
/// A value indicating whether the url should be absolute in any case.
/// The url for the published content.
///
/// The url is absolute or relative depending on Mode and on current, unless
/// absolute is true, in which case the url is always absolute.
/// If the provider is unable to provide a url, it returns "#".
///
public string GetUrl(int id, bool absolute)
{
var mode = absolute ? UrlProviderMode.Absolute : Mode;
return GetUrl(id, _umbracoContext.CleanedUmbracoUrl, mode);
}
///
/// Gets the nice url of a published content.
///
/// The published content id.
/// The current absolute url.
/// A value indicating whether the url should be absolute in any case.
/// The url for the published content.
///
/// The url is absolute or relative depending on Mode and on current, unless
/// absolute is true, in which case the url is always absolute.
/// If the provider is unable to provide a url, it returns "#".
///
public string GetUrl(int id, Uri current, bool absolute)
{
var mode = absolute ? UrlProviderMode.Absolute : Mode;
return GetUrl(id, current, mode);
}
///
/// Gets the nice url of a published content.
///
/// The published content identifier.
/// The url mode.
/// The url for the published content.
///
/// The url is absolute or relative depending on mode and on the current url.
/// If the provider is unable to provide a url, it returns "#".
///
public string GetUrl(int id, UrlProviderMode mode)
{
return GetUrl(id, _umbracoContext.CleanedUmbracoUrl, mode);
}
///
/// Gets the nice url of a published content.
///
/// The published content id.
/// The current absolute url.
/// The url mode.
/// The url for the published content.
///
/// The url is absolute or relative depending on mode and on current.
/// If the provider is unable to provide a url, it returns "#".
///
public string GetUrl(int id, Uri current, UrlProviderMode mode)
{
var url = _urlProviders.Select(provider => provider.GetUrl(_umbracoContext, id, current, mode))
.FirstOrDefault(u => u != null);
return url ?? "#"; // legacy wants this
}
#endregion
#region GetOtherUrls
///
/// Gets the other urls of a published content.
///
/// The published content id.
/// The other urls for the published content.
///
/// Other urls are those that GetUrl would not return in the current context, but would be valid
/// urls for the node in other contexts (different domain for current request, umbracoUrlAlias...).
/// The results depend on the current url.
///
public IEnumerable GetOtherUrls(int id)
{
return GetOtherUrls(id, _umbracoContext.CleanedUmbracoUrl);
}
///
/// Gets the other urls of a published content.
///
/// The published content id.
/// The current absolute url.
/// The other urls for the published content.
///
/// Other urls are those that GetUrl would not return in the current context, but would be valid
/// urls for the node in other contexts (different domain for current request, umbracoUrlAlias...).
///
public IEnumerable GetOtherUrls(int id, Uri current)
{
// providers can return null or an empty list or a non-empty list, be prepared
var urls = _urlProviders.SelectMany(provider => provider.GetOtherUrls(_umbracoContext, id, current) ?? Enumerable.Empty());
return urls;
}
#endregion
}
}