2021-01-26 11:17:55 +01:00
using System ;
using System.Collections.Generic ;
using System.Web ;
using Examine ;
using Microsoft.AspNetCore.Html ;
2021-03-10 21:13:13 +01:00
using Umbraco.Cms.Core ;
2021-02-09 10:22:42 +01:00
using Umbraco.Cms.Core.Models.PublishedContent ;
using Umbraco.Cms.Core.Routing ;
using Umbraco.Cms.Core.Services ;
using Umbraco.Cms.Core.Web ;
2021-02-12 10:57:50 +01:00
using Umbraco.Cms.Infrastructure.Examine ;
2021-01-26 11:17:55 +01:00
2021-02-10 14:21:48 +01:00
namespace Umbraco.Extensions
2021-01-26 11:17:55 +01:00
{
public static class PublishedContentExtensions
{
#region Creator / Writer Names
/// <summary>
/// Gets the name of the content item creator.
/// </summary>
/// <param name="content">The content item.</param>
/// <param name="userService"></param>
public static string CreatorName ( this IPublishedContent content , IUserService userService ) = > userService . GetProfileById ( content . CreatorId ) ? . Name ;
/// <summary>
/// Gets the name of the content item writer.
/// </summary>
/// <param name="content">The content item.</param>
/// <param name="userService"></param>
public static string WriterName ( this IPublishedContent content , IUserService userService ) = > userService . GetProfileById ( content . WriterId ) ? . Name ;
#endregion
#region Variations
/// <summary>
/// Gets the culture assigned to a document by domains, in the context of a current Uri.
/// </summary>
/// <param name="content">The document.</param>
/// <param name="umbracoContextAccessor"></param>
/// <param name="siteDomainHelper"></param>
/// <param name="current">An optional current Uri.</param>
/// <returns>The culture assigned to the document by domains.</returns>
/// <remarks>
/// <para>In 1:1 multilingual setup, a document contains several cultures (there is not
/// one document per culture), and domains, withing the context of a current Uri, assign
/// a culture to that document.</para>
/// </remarks>
2021-04-22 20:25:25 +10:00
public static string GetCultureFromDomains ( this IPublishedContent content , IUmbracoContextAccessor umbracoContextAccessor , ISiteDomainMapper siteDomainHelper , Uri current = null )
2021-08-11 12:17:35 +02:00
{
2021-08-17 11:41:28 +02:00
var umbracoContext = umbracoContextAccessor . GetRequiredUmbracoContext ( ) ;
return DomainUtilities . GetCultureFromDomains ( content . Id , content . Path , current , umbracoContext , siteDomainHelper ) ;
2021-08-11 12:17:35 +02:00
}
2021-01-26 11:17:55 +01:00
#endregion
#region Search
public static IEnumerable < PublishedSearchResult > SearchDescendants ( this IPublishedContent content , IExamineManager examineManager , IUmbracoContextAccessor umbracoContextAccessor , string term , string indexName = null )
{
indexName = string . IsNullOrEmpty ( indexName ) ? Constants . UmbracoIndexes . ExternalIndexName : indexName ;
if ( ! examineManager . TryGetIndex ( indexName , out var index ) )
{
throw new InvalidOperationException ( "No index found with name " + indexName ) ;
}
//var t = term.Escape().Value;
//var luceneQuery = "+__Path:(" + content.Path.Replace("-", "\\-") + "*) +" + t;
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
var query = index . Searcher . CreateQuery ( )
2021-01-26 11:17:55 +01:00
. Field ( UmbracoExamineFieldNames . IndexPathFieldName , ( content . Path + "," ) . MultipleCharacterWildcard ( ) )
. And ( )
. ManagedQuery ( term ) ;
2021-08-17 11:41:28 +02:00
var umbracoContext = umbracoContextAccessor . GetRequiredUmbracoContext ( ) ;
2021-08-11 13:25:17 +02:00
return query . Execute ( ) . ToPublishedSearchResults ( umbracoContext . Content ) ;
2021-01-26 11:17:55 +01:00
}
public static IEnumerable < PublishedSearchResult > SearchChildren ( this IPublishedContent content , IExamineManager examineManager , IUmbracoContextAccessor umbracoContextAccessor , string term , string indexName = null )
{
indexName = string . IsNullOrEmpty ( indexName ) ? Constants . UmbracoIndexes . ExternalIndexName : indexName ;
if ( ! examineManager . TryGetIndex ( indexName , out var index ) )
{
throw new InvalidOperationException ( "No index found with name " + indexName ) ;
}
//var t = term.Escape().Value;
//var luceneQuery = "+parentID:" + content.Id + " +" + t;
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
var query = index . Searcher . CreateQuery ( )
2021-01-26 11:17:55 +01:00
. Field ( "parentID" , content . Id )
. And ( )
. ManagedQuery ( term ) ;
2021-08-17 11:41:28 +02:00
var umbracoContext = umbracoContextAccessor . GetRequiredUmbracoContext ( ) ;
2021-01-26 11:17:55 +01:00
2021-08-11 13:25:17 +02:00
return query . Execute ( ) . ToPublishedSearchResults ( umbracoContext . Content ) ;
2021-01-26 11:17:55 +01:00
}
#endregion
#region IsSomething : equality
/// <summary>
/// If the specified <paramref name="content" /> is equal to <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsEqual ( this IPublishedContent content , IPublishedContent other , string valueIfTrue ) = > content . IsEqual ( other , valueIfTrue , string . Empty ) ;
/// <summary>
/// If the specified <paramref name="content" /> is equal to <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsEqual ( this IPublishedContent content , IPublishedContent other , string valueIfTrue , string valueIfFalse ) = > new HtmlString ( HttpUtility . HtmlEncode ( content . IsEqual ( other ) ? valueIfTrue : valueIfFalse ) ) ;
/// <summary>
/// If the specified <paramref name="content" /> is not equal to <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsNotEqual ( this IPublishedContent content , IPublishedContent other , string valueIfTrue ) = > content . IsNotEqual ( other , valueIfTrue , string . Empty ) ;
/// <summary>
/// If the specified <paramref name="content" /> is not equal to <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsNotEqual ( this IPublishedContent content , IPublishedContent other , string valueIfTrue , string valueIfFalse ) = > new HtmlString ( HttpUtility . HtmlEncode ( content . IsNotEqual ( other ) ? valueIfTrue : valueIfFalse ) ) ;
#endregion
#region IsSomething : ancestors and descendants
/// <summary>
/// If the specified <paramref name="content" /> is a decendant of <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsDescendant ( this IPublishedContent content , IPublishedContent other , string valueIfTrue ) = > content . IsDescendant ( other , valueIfTrue , string . Empty ) ;
/// <summary>
/// If the specified <paramref name="content" /> is a decendant of <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsDescendant ( this IPublishedContent content , IPublishedContent other , string valueIfTrue , string valueIfFalse ) = > new HtmlString ( HttpUtility . HtmlEncode ( content . IsDescendant ( other ) ? valueIfTrue : valueIfFalse ) ) ;
public static IHtmlContent IsDescendantOrSelf ( this IPublishedContent content , IPublishedContent other , string valueIfTrue ) = > content . IsDescendantOrSelf ( other , valueIfTrue , string . Empty ) ;
/// <summary>
/// If the specified <paramref name="content" /> is a decendant of <paramref name="other" /> or are the same, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsDescendantOrSelf ( this IPublishedContent content , IPublishedContent other , string valueIfTrue , string valueIfFalse ) = > new HtmlString ( HttpUtility . HtmlEncode ( content . IsDescendantOrSelf ( other ) ? valueIfTrue : valueIfFalse ) ) ;
public static IHtmlContent IsAncestor ( this IPublishedContent content , IPublishedContent other , string valueIfTrue ) = > content . IsAncestor ( other , valueIfTrue , string . Empty ) ;
/// <summary>
/// If the specified <paramref name="content" /> is an ancestor of <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsAncestor ( this IPublishedContent content , IPublishedContent other , string valueIfTrue , string valueIfFalse ) = > new HtmlString ( HttpUtility . HtmlEncode ( content . IsAncestor ( other ) ? valueIfTrue : valueIfFalse ) ) ;
public static IHtmlContent IsAncestorOrSelf ( this IPublishedContent content , IPublishedContent other , string valueIfTrue ) = > content . IsAncestorOrSelf ( other , valueIfTrue , string . Empty ) ;
/// <summary>
/// If the specified <paramref name="content" /> is an ancestor of <paramref name="other" /> or are the same, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsAncestorOrSelf ( this IPublishedContent content , IPublishedContent other , string valueIfTrue , string valueIfFalse ) = > new HtmlString ( HttpUtility . HtmlEncode ( content . IsAncestorOrSelf ( other ) ? valueIfTrue : valueIfFalse ) ) ;
#endregion
}
}