2015-01-05 10:01:58 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2017-01-04 12:56:32 +01:00
|
|
|
|
using System.Web.Security;
|
2013-09-02 15:40:14 +02:00
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using umbraco;
|
2017-01-04 12:56:32 +01:00
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Configuration;
|
2016-07-21 16:46:46 +02:00
|
|
|
|
using Umbraco.Core.Logging;
|
2013-09-02 15:40:14 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Routing
|
|
|
|
|
|
{
|
|
|
|
|
|
internal static class UrlProviderExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the URLs for the content item
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content"></param>
|
2015-01-05 10:01:58 +11:00
|
|
|
|
/// <param name="umbracoContext"></param>
|
2013-09-02 15:40:14 +02:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// Use this when displaying URLs, if there are errors genertaing the urls the urls themselves will
|
|
|
|
|
|
/// contain the errors.
|
|
|
|
|
|
/// </remarks>
|
2015-01-05 10:01:58 +11:00
|
|
|
|
public static IEnumerable<string> GetContentUrls(this IContent content, UmbracoContext umbracoContext)
|
2013-09-02 15:40:14 +02:00
|
|
|
|
{
|
2015-01-05 10:01:58 +11:00
|
|
|
|
if (content == null) throw new ArgumentNullException("content");
|
|
|
|
|
|
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
|
|
|
|
|
|
|
2013-09-02 15:40:14 +02:00
|
|
|
|
var urls = new List<string>();
|
2013-10-29 16:36:56 +11:00
|
|
|
|
|
2015-02-10 15:28:48 +01:00
|
|
|
|
if (content.HasPublishedVersion == false)
|
2013-10-29 16:36:56 +11:00
|
|
|
|
{
|
2015-01-05 10:01:58 +11:00
|
|
|
|
urls.Add(ui.Text("content", "itemNotPublished", umbracoContext.Security.CurrentUser));
|
2013-10-29 16:36:56 +11:00
|
|
|
|
return urls;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-21 16:46:46 +02:00
|
|
|
|
string url;
|
2015-01-05 10:01:58 +11:00
|
|
|
|
var urlProvider = umbracoContext.RoutingContext.UrlProvider;
|
2016-07-21 16:46:46 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
url = urlProvider.GetUrl(content.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Error<UrlProvider>("GetUrl exception.", e);
|
|
|
|
|
|
url = "#ex";
|
|
|
|
|
|
}
|
2013-09-02 15:40:14 +02:00
|
|
|
|
if (url == "#")
|
|
|
|
|
|
{
|
|
|
|
|
|
// document as a published version yet it's url is "#" => a parent must be
|
|
|
|
|
|
// unpublished, walk up the tree until we find it, and report.
|
|
|
|
|
|
var parent = content;
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
2013-09-04 16:09:54 +10:00
|
|
|
|
parent = parent.ParentId > 0 ? parent.Parent() : null;
|
2013-09-02 15:40:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
while (parent != null && parent.Published);
|
|
|
|
|
|
|
|
|
|
|
|
if (parent == null) // oops - internal error
|
2015-01-05 10:01:58 +11:00
|
|
|
|
urls.Add(ui.Text("content", "parentNotPublishedAnomaly", umbracoContext.Security.CurrentUser));
|
2013-09-02 15:40:14 +02:00
|
|
|
|
else
|
2015-01-05 10:01:58 +11:00
|
|
|
|
urls.Add(ui.Text("content", "parentNotPublished", parent.Name, umbracoContext.Security.CurrentUser));
|
2013-09-02 15:40:14 +02:00
|
|
|
|
}
|
2016-07-21 16:46:46 +02:00
|
|
|
|
else if (url == "#ex")
|
|
|
|
|
|
{
|
|
|
|
|
|
urls.Add(ui.Text("content", "getUrlException", umbracoContext.Security.CurrentUser));
|
|
|
|
|
|
}
|
2017-01-04 12:56:32 +01:00
|
|
|
|
else
|
2016-05-04 12:45:20 +02:00
|
|
|
|
{
|
2017-01-04 12:56:32 +01:00
|
|
|
|
// test for collisions
|
2017-07-20 10:21:21 +02:00
|
|
|
|
var uri = new Uri(url.TrimEnd('/'), UriKind.RelativeOrAbsolute);
|
2017-01-04 12:56:32 +01:00
|
|
|
|
if (uri.IsAbsoluteUri == false) uri = uri.MakeAbsolute(UmbracoContext.Current.CleanedUmbracoUrl);
|
2017-07-20 10:21:21 +02:00
|
|
|
|
uri = UriUtility.UriToUmbraco(uri);
|
2017-01-04 12:56:32 +01:00
|
|
|
|
var pcr = new PublishedContentRequest(uri, UmbracoContext.Current.RoutingContext, UmbracoConfig.For.UmbracoSettings().WebRouting, s => Roles.Provider.GetRolesForUser(s));
|
|
|
|
|
|
pcr.Engine.TryRouteRequest();
|
|
|
|
|
|
|
|
|
|
|
|
if (pcr.HasPublishedContent == false)
|
2016-05-04 12:45:20 +02:00
|
|
|
|
{
|
2017-01-04 12:56:32 +01:00
|
|
|
|
urls.Add(ui.Text("content", "routeError", "(error)", umbracoContext.Security.CurrentUser));
|
2016-05-04 12:45:20 +02:00
|
|
|
|
}
|
2017-08-23 23:14:49 +02:00
|
|
|
|
else if (pcr.IgnorePublishedContentCollisions == false && pcr.PublishedContent.Id != content.Id)
|
2016-05-04 12:45:20 +02:00
|
|
|
|
{
|
2017-01-04 12:56:32 +01:00
|
|
|
|
var o = pcr.PublishedContent;
|
|
|
|
|
|
string s;
|
|
|
|
|
|
if (o == null)
|
2016-05-04 12:45:20 +02:00
|
|
|
|
{
|
2017-01-04 12:56:32 +01:00
|
|
|
|
s = "(unknown)";
|
2016-05-04 12:45:20 +02:00
|
|
|
|
}
|
2017-01-04 12:56:32 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var l = new List<string>();
|
|
|
|
|
|
while (o != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
l.Add(o.Name);
|
|
|
|
|
|
o = o.Parent;
|
|
|
|
|
|
}
|
|
|
|
|
|
l.Reverse();
|
|
|
|
|
|
s = "/" + string.Join("/", l) + " (id=" + pcr.PublishedContent.Id + ")";
|
2016-05-04 12:45:20 +02:00
|
|
|
|
|
2017-01-04 12:56:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
urls.Add(ui.Text("content", "routeError", s, umbracoContext.Security.CurrentUser));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
urls.Add(url);
|
|
|
|
|
|
urls.AddRange(urlProvider.GetOtherUrls(content.Id));
|
2016-05-04 12:45:20 +02:00
|
|
|
|
}
|
2013-09-02 15:40:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
return urls;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-08-12 15:57:54 +10:00
|
|
|
|
}
|