Files
Umbraco-CMS/src/Umbraco.Web.BackOffice/Controllers/ExamineManagementController.cs
Bjarke Berg b3a9730442 Merge remote-tracking branch 'origin/v8/dev' into netcore/dev
# Conflicts:
#	build/NuSpecs/UmbracoCms.Web.nuspec
#	src/SolutionInfo.cs
#	src/Umbraco.Core/Cache/MediaCacheRefresher.cs
#	src/Umbraco.Core/Composing/ComponentCollection.cs
#	src/Umbraco.Core/Composing/Composers.cs
#	src/Umbraco.Core/Composing/TypeFinder.cs
#	src/Umbraco.Core/Composing/TypeLoader.cs
#	src/Umbraco.Core/Configuration/Grid/GridEditorsConfig.cs
#	src/Umbraco.Core/Constants-SvgSanitizer.cs
#	src/Umbraco.Core/ContentApps/ContentAppFactoryCollection.cs
#	src/Umbraco.Core/Extensions/PublishedContentExtensions.cs
#	src/Umbraco.Core/Extensions/PublishedPropertyExtension.cs
#	src/Umbraco.Core/Extensions/StringExtensions.cs
#	src/Umbraco.Core/HealthChecks/Checks/Security/ExcessiveHeadersCheck.cs
#	src/Umbraco.Core/HealthChecks/HealthCheckResults.cs
#	src/Umbraco.Core/IO/FileSystems.cs
#	src/Umbraco.Core/IO/IOHelper.cs
#	src/Umbraco.Core/IO/MediaFileSystem.cs
#	src/Umbraco.Core/IO/PhysicalFileSystem.cs
#	src/Umbraco.Core/Logging/DebugDiagnosticsLogger.cs
#	src/Umbraco.Core/Logging/DisposableTimer.cs
#	src/Umbraco.Core/Logging/ILogger.cs
#	src/Umbraco.Core/Logging/LogProfiler.cs
#	src/Umbraco.Core/Logging/OwinLogger.cs
#	src/Umbraco.Core/Manifest/ManifestWatcher.cs
#	src/Umbraco.Core/Mapping/UmbracoMapper.cs
#	src/Umbraco.Core/Media/UploadAutoFillProperties.cs
#	src/Umbraco.Core/Migrations/Install/DatabaseDataCreator.cs
#	src/Umbraco.Core/Migrations/Upgrade/V_8_0_0/DataTypes/PreValueMigratorCollection.cs
#	src/Umbraco.Core/Models/Mapping/ContentPropertyBasicMapper.cs
#	src/Umbraco.Core/Models/Mapping/DataTypeMapDefinition.cs
#	src/Umbraco.Core/Models/Mapping/MacroMapDefinition.cs
#	src/Umbraco.Core/Models/Member.cs
#	src/Umbraco.Core/Packaging/PackageActionRunner.cs
#	src/Umbraco.Core/PropertyEditors/DataValueEditor.cs
#	src/Umbraco.Core/PropertyEditors/Validators/EyeDropperColorPickerConfigurationEditor.cs
#	src/Umbraco.Core/PropertyEditors/Validators/EyeDropperColorPickerPropertyEditor.cs
#	src/Umbraco.Core/Routing/DefaultUrlProvider.cs
#	src/Umbraco.Core/Runtime/CoreRuntime.cs
#	src/Umbraco.Core/Runtime/MainDom.cs
#	src/Umbraco.Core/RuntimeState.cs
#	src/Umbraco.Core/Scoping/ScopeProvider.cs
#	src/Umbraco.Core/Sync/DatabaseServerMessenger.cs
#	src/Umbraco.Core/Templates/HtmlUrlParser.cs
#	src/Umbraco.Core/UriExtensions.cs
#	src/Umbraco.Examine.Lucene/UmbracoContentIndex.cs
#	src/Umbraco.Examine.Lucene/UmbracoExamineIndex.cs
#	src/Umbraco.Infrastructure/Examine/IndexRebuilder.cs
#	src/Umbraco.Infrastructure/Manifest/DataEditorConverter.cs
#	src/Umbraco.Infrastructure/Manifest/ManifestParser.cs
#	src/Umbraco.Infrastructure/Migrations/Install/DatabaseBuilder.cs
#	src/Umbraco.Infrastructure/Migrations/Install/DatabaseSchemaCreator.cs
#	src/Umbraco.Infrastructure/Migrations/MigrationPlan.cs
#	src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/DataTypeMigration.cs
#	src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/DropDownPropertyEditorsMigration.cs
#	src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/MergeDateAndDateTimePropertyEditor.cs
#	src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/RadioAndCheckboxPropertyEditorsMigration.cs
#	src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs
#	src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentRepositoryBase.cs
#	src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentTypeRepository.cs
#	src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs
#	src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MediaRepository.cs
#	src/Umbraco.Infrastructure/Persistence/UmbracoDatabase.cs
#	src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs
#	src/Umbraco.Infrastructure/PropertyEditors/BlockEditorPropertyEditor.cs
#	src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs
#	src/Umbraco.Infrastructure/PropertyEditors/MultiUrlPickerValueEditor.cs
#	src/Umbraco.Infrastructure/PropertyEditors/NestedContentPropertyEditor.cs
#	src/Umbraco.Infrastructure/PropertyEditors/RichTextEditorPastedImages.cs
#	src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/GridValueConverter.cs
#	src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueConverter.cs
#	src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/JsonValueConverter.cs
#	src/Umbraco.Infrastructure/PublishedCache/PublishedContentTypeCache.cs
#	src/Umbraco.Infrastructure/Runtime/SqlMainDomLock.cs
#	src/Umbraco.Infrastructure/Scoping/Scope.cs
#	src/Umbraco.Infrastructure/Search/ExamineNotificationHandler.cs
#	src/Umbraco.Infrastructure/Services/Implement/ContentService.cs
#	src/Umbraco.Infrastructure/Services/Implement/LocalizedTextService.cs
#	src/Umbraco.Infrastructure/Services/Implement/LocalizedTextServiceFileSources.cs
#	src/Umbraco.Infrastructure/Services/Implement/MediaService.cs
#	src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs
#	src/Umbraco.Infrastructure/Sync/ServerMessengerBase.cs
#	src/Umbraco.ModelsBuilder.Embedded/Compose/ModelsBuilderComposer.cs
#	src/Umbraco.ModelsBuilder.Embedded/LiveModelsProvider.cs
#	src/Umbraco.PublishedCache.NuCache/ContentStore.cs
#	src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentEventsTests.cs
#	src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Mapping/MappingTests.cs
#	src/Umbraco.Tests.UnitTests/Umbraco.ModelsBuilder.Embedded/BuilderTests.cs
#	src/Umbraco.Tests/Composing/TypeLoaderTests.cs
#	src/Umbraco.Tests/LegacyXmlPublishedCache/DictionaryPublishedContent.cs
#	src/Umbraco.Tests/LegacyXmlPublishedCache/LegacyBackgroundTask/BackgroundTaskRunner.cs
#	src/Umbraco.Tests/LegacyXmlPublishedCache/PreviewContent.cs
#	src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedMediaCache.cs
#	src/Umbraco.Tests/LegacyXmlPublishedCache/XmlStore.cs
#	src/Umbraco.Tests/LegacyXmlPublishedCache/XmlStoreFilePersister.cs
#	src/Umbraco.Tests/Services/ContentServicePerformanceTest.cs
#	src/Umbraco.Tests/Services/PerformanceTests.cs
#	src/Umbraco.Tests/TestHelpers/ConsoleLogger.cs
#	src/Umbraco.Tests/Testing/TestingTests/MockTests.cs
#	src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs
#	src/Umbraco.Web.BackOffice/Controllers/ContentController.cs
#	src/Umbraco.Web.BackOffice/Controllers/DashboardController.cs
#	src/Umbraco.Web.BackOffice/Controllers/ExamineManagementController.cs
#	src/Umbraco.Web.BackOffice/HealthChecks/HealthCheckController.cs
#	src/Umbraco.Web.BackOffice/PropertyEditors/RteEmbedController.cs
#	src/Umbraco.Web.BackOffice/Services/IconService.cs
#	src/Umbraco.Web.BackOffice/Trees/ContentTreeControllerBase.cs
#	src/Umbraco.Web.Common/Extensions/FormCollectionExtensions.cs
#	src/Umbraco.Web.Common/Install/InstallApiController.cs
#	src/Umbraco.Web.Common/Macros/MacroRenderer.cs
#	src/Umbraco.Web.Common/ModelsBuilder/PureLiveModelFactory.cs
#	src/Umbraco.Web.UI.Client/package-lock.json
#	src/Umbraco.Web.UI.Client/src/views/memberTypes/copy.controller.js
#	src/Umbraco.Web.UI.Client/src/views/memberTypes/copy.html
#	src/Umbraco.Web.UI.NetCore/umbraco/UmbracoBackOffice/Default.cshtml
#	src/Umbraco.Web.UI.NetCore/umbraco/config/lang/da.xml
#	src/Umbraco.Web.UI.NetCore/umbraco/config/lang/en.xml
#	src/Umbraco.Web.UI.NetCore/umbraco/config/lang/en_us.xml
#	src/Umbraco.Web.UI/config/splashes/noNodes.aspx
#	src/Umbraco.Web/AspNet/AspNetHttpContextAccessor.cs
#	src/Umbraco.Web/Cache/DistributedCacheBinder.cs
#	src/Umbraco.Web/Cache/DistributedCacheBinder_Handlers.cs
#	src/Umbraco.Web/Editors/AuthenticationController.cs
#	src/Umbraco.Web/Editors/BackOfficeController.cs
#	src/Umbraco.Web/Editors/Binders/ContentModelBinderHelper.cs
#	src/Umbraco.Web/Editors/ContentControllerBase.cs
#	src/Umbraco.Web/Editors/ContentTypeController.cs
#	src/Umbraco.Web/Editors/DictionaryController.cs
#	src/Umbraco.Web/Editors/MemberTypeController.cs
#	src/Umbraco.Web/Editors/PasswordChanger.cs
#	src/Umbraco.Web/Editors/RelationTypeController.cs
#	src/Umbraco.Web/Editors/TinyMceController.cs
#	src/Umbraco.Web/HealthCheck/Checks/Security/HttpsCheck.cs
#	src/Umbraco.Web/HtmlHelperRenderExtensions.cs
#	src/Umbraco.Web/HttpUrlHelperExtensions.cs
#	src/Umbraco.Web/HybridEventMessagesAccessor.cs
#	src/Umbraco.Web/ImageCropperTemplateExtensions.cs
#	src/Umbraco.Web/JavaScript/ClientDependencyConfiguration.cs
#	src/Umbraco.Web/Mvc/RenderMvcController.cs
#	src/Umbraco.Web/PublishedCache/NuCache/DataSource/DatabaseDataSource.cs
#	src/Umbraco.Web/PublishedCache/NuCache/PublishedSnapshotService.cs
#	src/Umbraco.Web/Routing/ContentFinderByConfigured404.cs
#	src/Umbraco.Web/Routing/ContentFinderByIdPath.cs
#	src/Umbraco.Web/Routing/ContentFinderByRedirectUrl.cs
#	src/Umbraco.Web/Routing/ContentFinderByUrl.cs
#	src/Umbraco.Web/Routing/ContentFinderByUrlAlias.cs
#	src/Umbraco.Web/Routing/ContentFinderByUrlAndTemplate.cs
#	src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs
#	src/Umbraco.Web/Routing/PublishedRouter.cs
#	src/Umbraco.Web/Runtime/WebInitialComposer.cs
#	src/Umbraco.Web/Scheduling/KeepAlive.cs
#	src/Umbraco.Web/Scheduling/ScheduledPublishing.cs
#	src/Umbraco.Web/Scheduling/TempFileCleanup.cs
#	src/Umbraco.Web/Security/MembershipHelper.cs
#	src/Umbraco.Web/Security/Providers/UmbracoMembershipProvider.cs
#	src/Umbraco.Web/Trees/MemberTreeController.cs
#	src/Umbraco.Web/Trees/MemberTypeAndGroupTreeControllerBase.cs
#	src/Umbraco.Web/Trees/MemberTypeTreeController.cs
#	src/Umbraco.Web/UmbracoApplicationBase.cs
#	src/Umbraco.Web/UmbracoInjectedModule.cs
#	src/Umbraco.Web/UmbracoModule.cs
#	src/Umbraco.Web/WebApi/AngularJsonMediaTypeFormatter.cs
#	src/Umbraco.Web/WebApi/Filters/FileUploadCleanupFilterAttribute.cs
#	src/Umbraco.Web/WebApi/UnhandledExceptionLogger.cs
2021-04-20 19:34:18 +02:00

253 lines
9.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Examine;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Infrastructure.Examine;
using Umbraco.Cms.Infrastructure.Search;
using Umbraco.Cms.Web.Common.Attributes;
using Umbraco.Extensions;
using Constants = Umbraco.Cms.Core.Constants;
using SearchResult = Umbraco.Cms.Core.Models.ContentEditing.SearchResult;
namespace Umbraco.Cms.Web.BackOffice.Controllers
{
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
public class ExamineManagementController : UmbracoAuthorizedJsonController
{
private readonly IExamineManager _examineManager;
private readonly ILogger<ExamineManagementController> _logger;
private readonly IIOHelper _ioHelper;
private readonly IIndexDiagnosticsFactory _indexDiagnosticsFactory;
private readonly IAppPolicyCache _runtimeCache;
private readonly IndexRebuilder _indexRebuilder;
public ExamineManagementController(IExamineManager examineManager, ILogger<ExamineManagementController> logger, IIOHelper ioHelper, IIndexDiagnosticsFactory indexDiagnosticsFactory,
AppCaches appCaches,
IndexRebuilder indexRebuilder)
{
_examineManager = examineManager;
_logger = logger;
_ioHelper = ioHelper;
_indexDiagnosticsFactory = indexDiagnosticsFactory;
_runtimeCache = appCaches.RuntimeCache;
_indexRebuilder = indexRebuilder;
}
/// <summary>
/// Get the details for indexers
/// </summary>
/// <returns></returns>
public IEnumerable<ExamineIndexModel> GetIndexerDetails()
{
return _examineManager.Indexes.Select(CreateModel).OrderBy(x => x.Name.TrimEnd("Indexer"));
}
/// <summary>
/// Get the details for searchers
/// </summary>
/// <returns></returns>
public IEnumerable<ExamineSearcherModel> GetSearcherDetails()
{
var model = new List<ExamineSearcherModel>(
_examineManager.RegisteredSearchers.Select(searcher => new ExamineSearcherModel { Name = searcher.Name })
.OrderBy(x => x.Name.TrimEnd("Searcher"))); //order by name , but strip the "Searcher" from the end if it exists
return model;
}
public ActionResult<SearchResults> GetSearchResults(string searcherName, string query, int pageIndex = 0, int pageSize = 20)
{
if (query.IsNullOrWhiteSpace())
return SearchResults.Empty();
var msg = ValidateSearcher(searcherName, out var searcher);
if (!msg.IsSuccessStatusCode())
return msg;
// NativeQuery will work for a single word/phrase too (but depends on the implementation) the lucene one will work.
var results = searcher.CreateQuery().NativeQuery(query).Execute(maxResults: pageSize * (pageIndex + 1));
var pagedResults = results.Skip(pageIndex * pageSize);
return new SearchResults
{
TotalRecords = results.TotalItemCount,
Results = pagedResults.Select(x => new SearchResult
{
Id = x.Id,
Score = x.Score,
Values = x.AllValues.OrderBy(y => y.Key).ToDictionary(y => y.Key, y => y.Value)
})
};
}
/// <summary>
/// Check if the index has been rebuilt
/// </summary>
/// <param name="indexName"></param>
/// <returns></returns>
/// <remarks>
/// This is kind of rudimentary since there's no way we can know that the index has rebuilt, we
/// have a listener for the index op complete so we'll just check if that key is no longer there in the runtime cache
/// </remarks>
public ActionResult<ExamineIndexModel> PostCheckRebuildIndex(string indexName)
{
var validate = ValidateIndex(indexName, out var index);
if (!validate.IsSuccessStatusCode())
return validate;
validate = ValidatePopulator(index);
if (!validate.IsSuccessStatusCode())
return validate;
var cacheKey = "temp_indexing_op_" + indexName;
var found = _runtimeCache.Get(cacheKey);
//if its still there then it's not done
return found != null
? null
: CreateModel(index);
}
/// <summary>
/// Rebuilds the index
/// </summary>
/// <param name="indexName"></param>
/// <returns></returns>
public IActionResult PostRebuildIndex(string indexName)
{
var validate = ValidateIndex(indexName, out var index);
if (!validate.IsSuccessStatusCode())
return validate;
validate = ValidatePopulator(index);
if (!validate.IsSuccessStatusCode())
return validate;
_logger.LogInformation("Rebuilding index '{IndexName}'", indexName);
//remove it in case there's a handler there already
index.IndexOperationComplete -= Indexer_IndexOperationComplete;
//now add a single handler
index.IndexOperationComplete += Indexer_IndexOperationComplete;
try
{
var cacheKey = "temp_indexing_op_" + index.Name;
//put temp val in cache which is used as a rudimentary way to know when the indexing is done
_runtimeCache.Insert(cacheKey, () => "tempValue", TimeSpan.FromMinutes(5));
_indexRebuilder.RebuildIndex(indexName);
////populate it
//foreach (var populator in _populators.Where(x => x.IsRegistered(indexName)))
// populator.Populate(index);
return new OkResult();
}
catch (Exception ex)
{
//ensure it's not listening
index.IndexOperationComplete -= Indexer_IndexOperationComplete;
_logger.LogError(ex, "An error occurred rebuilding index");
var response = new ConflictObjectResult("The index could not be rebuilt at this time, most likely there is another thread currently writing to the index. Error: {ex}");
HttpContext.SetReasonPhrase("Could Not Rebuild");
return response;
}
}
private ExamineIndexModel CreateModel(IIndex index)
{
var indexName = index.Name;
var indexDiag = _indexDiagnosticsFactory.Create(index);
var isHealth = indexDiag.IsHealthy();
var properties = new Dictionary<string, object>
{
[nameof(IIndexDiagnostics.DocumentCount)] = indexDiag.DocumentCount,
[nameof(IIndexDiagnostics.FieldCount)] = indexDiag.FieldCount,
};
foreach (var p in indexDiag.Metadata)
properties[p.Key] = p.Value;
var indexerModel = new ExamineIndexModel
{
Name = indexName,
HealthStatus = isHealth.Success ? (isHealth.Result ?? "Healthy") : (isHealth.Result ?? "Unhealthy"),
ProviderProperties = properties,
CanRebuild = _indexRebuilder.CanRebuild(index)
};
return indexerModel;
}
private ActionResult ValidateSearcher(string searcherName, out ISearcher searcher)
{
//try to get the searcher from the indexes
if (_examineManager.TryGetIndex(searcherName, out var index))
{
searcher = index.GetSearcher();
return new OkResult();
}
//if we didn't find anything try to find it by an explicitly declared searcher
if (_examineManager.TryGetSearcher(searcherName, out searcher))
return new OkResult();
var response1 = new BadRequestObjectResult($"No searcher found with name = {searcherName}");
HttpContext.SetReasonPhrase("Searcher Not Found");
return response1;
}
private ActionResult ValidatePopulator(IIndex index)
{
if (_indexRebuilder.CanRebuild(index))
return new OkResult();
var response = new BadRequestObjectResult($"The index {index.Name} cannot be rebuilt because it does not have an associated {typeof(IIndexPopulator)}");
HttpContext.SetReasonPhrase("Index cannot be rebuilt");
return response;
}
private ActionResult ValidateIndex(string indexName, out IIndex index)
{
index = null;
if (_examineManager.TryGetIndex(indexName, out index))
{
//return Ok!
return new OkResult();
}
var response = new BadRequestObjectResult($"No index found with name = {indexName}");
HttpContext.SetReasonPhrase("Index Not Found");
return response;
}
private void Indexer_IndexOperationComplete(object sender, EventArgs e)
{
var indexer = (IIndex)sender;
_logger.LogDebug("Logging operation completed for index {IndexName}", indexer.Name);
//ensure it's not listening anymore
indexer.IndexOperationComplete -= Indexer_IndexOperationComplete;
_logger.LogInformation($"Rebuilding index '{indexer.Name}' done.");
var cacheKey = "temp_indexing_op_" + indexer.Name;
_runtimeCache.Clear(cacheKey);
}
}
}