<commit_before>using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core;

namespace Umbraco.Web.Routing
{
	/// <summary>
	/// Provides an implementation of <see cref="IContentFinder"/> that handles page nice urls.
	/// </summary>
	/// <remarks>
	/// <para>Handles <c>/foo/bar</c> where <c>/foo/bar</c> is the nice url of a document.</para>
	/// </remarks>
	internal class ContentFinderByNiceUrl : IContentFinder
    {
	    private readonly bool _doDomainLookup;

	    public ContentFinderByNiceUrl()
	    {
	        _doDomainLookup = true;
	    }

        /// <summary>
        /// Contructor with flag to set whether we lookup domains in the repo to match the url or not
        /// </summary>
        /// <param name="doDomainLookup"></param>
	    internal ContentFinderByNiceUrl(bool doDomainLookup)
	    {
	        _doDomainLookup = doDomainLookup;
	    }

	    /// <summary>
		/// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
		/// </summary>
		/// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>		
		/// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
		public virtual bool TryFindDocument(PublishedContentRequest docRequest)
        {
			string route;
			if (docRequest.HasDomain)
				route = docRequest.Domain.RootNodeId.ToString() + DomainHelper.PathRelativeToDomain(docRequest.DomainUri, docRequest.Uri.GetAbsolutePathDecoded());
			else
				route = docRequest.Uri.GetAbsolutePathDecoded();

			var node = LookupDocumentNode(docRequest, route);
            return node != null;
        }

		/// <summary>
		/// Tries to find an Umbraco document for a <c>PublishedContentRequest</c> and a route.
		/// </summary>
		/// <param name="docreq">The document request.</param>
		/// <param name="route">The route.</param>
		/// <returns>The document node, or null.</returns>
        protected IPublishedContent LookupDocumentNode(PublishedContentRequest docreq, string route)
        {
			LogHelper.Debug<ContentFinderByNiceUrl>("Test route \"{0}\"", () => route);

			// first ask the cache for a node
			// return '0' if in preview mode
        	var nodeId = !docreq.RoutingContext.UmbracoContext.InPreviewMode
							? docreq.RoutingContext.UmbracoContext.RoutingContext.RoutesCache.GetNodeId(route)
        	             	: 0;

			// if a node was found, get it by id and ensure it exists
			// else clear the cache<commit_msg>Web.Routing - clear convoluted .RoutingContext.UmbracoContext.RoutingContext<commit_after>using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core;

namespace Umbraco.Web.Routing
{
	/// <summary>
	/// Provides an implementation of <see cref="IContentFinder"/> that handles page nice urls.
	/// </summary>
	/// <remarks>
	/// <para>Handles <c>/foo/bar</c> where <c>/foo/bar</c> is the nice url of a document.</para>
	/// </remarks>
	internal class ContentFinderByNiceUrl : IContentFinder
    {
	    private readonly bool _doDomainLookup;

	    public ContentFinderByNiceUrl()
	    {
	        _doDomainLookup = true;
	    }

        /// <summary>
        /// Contructor with flag to set whether we lookup domains in the repo to match the url or not
        /// </summary>
        /// <param name="doDomainLookup"></param>
	    internal ContentFinderByNiceUrl(bool doDomainLookup)
	    {
	        _doDomainLookup = doDomainLookup;
	    }

	    /// <summary>
		/// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
		/// </summary>
		/// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>		
		/// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
		public virtual bool TryFindDocument(PublishedContentRequest docRequest)
        {
			string route;
			if (docRequest.HasDomain)
				route = docRequest.Domain.RootNodeId.ToString() + DomainHelper.PathRelativeToDomain(docRequest.DomainUri, docRequest.Uri.GetAbsolutePathDecoded());
			else
				route = docRequest.Uri.GetAbsolutePathDecoded();

			var node = LookupDocumentNode(docRequest, route);
            return node != null;
        }

		/// <summary>
		/// Tries to find an Umbraco document for a <c>PublishedContentRequest</c> and a route.
		/// </summary>
		/// <param name="docreq">The document request.</param>
		/// <param name="route">The route.</param>
		/// <returns>The document node, or null.</returns>
        protected IPublishedContent LookupDocumentNode(PublishedContentRequest docreq, string route)
        {
			LogHelper.Debug<ContentFinderByNiceUrl>("Test route \"{0}\"", () => route);

			// first ask the cache for a node
			// return '0' if in preview mode
        	var nodeId = !docreq.RoutingContext.UmbracoContext.InPreviewMode
							? docreq.RoutingContext.RoutesCache.GetNodeId(route)
        	             	: 0;

			// if a node was found, get it by id and ensure it exists
			// else clear the cache
{'fraction_non_alphanumeric': 0.12102169065477397, 'fraction_numerical': 0.001216298398540442, 'mean_word_length': 2.9472, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6713563', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1430, 'n_words': 390}