From 6ee17dad038386eac3bb12e200642649d9c432f9 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Sat, 2 Feb 2013 02:16:44 +0600 Subject: [PATCH] Updated configs to ensure that the correct (non-obsoleted) config section definition is used for Examine. Updates Examine index configs to be simplified, don't need all of that config there! Updates UmbracoExamine indexers to not initialize if the app/database is not ready. Updates Examine event binders to not bind if the app/database is not ready. Updates Examine events to use new 6.0+ events and removes binding entirely to the old api events (where we could) --- .../config/ExamineIndex.Release.config | 30 +-- src/Umbraco.Web.UI/config/ExamineIndex.config | 41 +--- src/Umbraco.Web.UI/web.Template.Debug.config | 6 + src/Umbraco.Web/Search/ExamineEvents.cs | 193 +++++++----------- src/UmbracoExamine/BaseUmbracoIndexer.cs | 25 +++ src/UmbracoExamine/UmbracoContentIndexer.cs | 6 + src/UmbracoExamine/UmbracoExamineSearcher.cs | 28 +++ 7 files changed, 154 insertions(+), 175 deletions(-) diff --git a/src/Umbraco.Web.UI/config/ExamineIndex.Release.config b/src/Umbraco.Web.UI/config/ExamineIndex.Release.config index ce180cd9c2..7f3c3d51b6 100644 --- a/src/Umbraco.Web.UI/config/ExamineIndex.Release.config +++ b/src/Umbraco.Web.UI/config/ExamineIndex.Release.config @@ -8,36 +8,10 @@ More information and documentation can be found on CodePlex: http://umbracoexami --> - - - - - - - - - - - - - - + - - - - - - - - - - - - - - + diff --git a/src/Umbraco.Web.UI/config/ExamineIndex.config b/src/Umbraco.Web.UI/config/ExamineIndex.config index c9d2eece88..c791075f63 100644 --- a/src/Umbraco.Web.UI/config/ExamineIndex.config +++ b/src/Umbraco.Web.UI/config/ExamineIndex.config @@ -6,37 +6,12 @@ Index/Search providers can be defined in the UmbracoSettings.config More information and documentation can be found on CodePlex: http://umbracoexamine.codeplex.com --> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + \ No newline at end of file diff --git a/src/Umbraco.Web.UI/web.Template.Debug.config b/src/Umbraco.Web.UI/web.Template.Debug.config index f9dcde4d78..937cee3f3a 100644 --- a/src/Umbraco.Web.UI/web.Template.Debug.config +++ b/src/Umbraco.Web.UI/web.Template.Debug.config @@ -16,6 +16,12 @@ 2012-11-08 SJ - Add Medium trust for everybody so we'll see any MedTrust related errors early on --> + + +
+
+ + diff --git a/src/Umbraco.Web/Search/ExamineEvents.cs b/src/Umbraco.Web/Search/ExamineEvents.cs index a2c36e935c..473a1705ff 100644 --- a/src/Umbraco.Web/Search/ExamineEvents.cs +++ b/src/Umbraco.Web/Search/ExamineEvents.cs @@ -8,12 +8,15 @@ using Examine.LuceneEngine; using Lucene.Net.Documents; using Umbraco.Core; using Umbraco.Core.Logging; +using Umbraco.Core.Models; +using Umbraco.Core.Services; using UmbracoExamine; using umbraco; using umbraco.BusinessLogic; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.member; using umbraco.interfaces; +using Content = umbraco.cms.businesslogic.Content; using Document = umbraco.cms.businesslogic.web.Document; namespace Umbraco.Web.Search @@ -42,6 +45,18 @@ namespace Umbraco.Web.Search [SecuritySafeCritical] public void OnApplicationStarted(UmbracoApplicationBase httpApplication, ApplicationContext applicationContext) { + //do not initialize if the application is not configured + //We need to check if we actually can initialize, if not then don't continue + if (ApplicationContext.Current == null + || !ApplicationContext.Current.IsConfigured + || !ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured) + { + LogHelper.Info("Not initializing Examine because the application and/or database is not configured"); + return; + } + + LogHelper.Info("Initializing Examine and binding to business logic events"); + var registeredProviders = ExamineManager.Instance.IndexProviderCollection .OfType().Count(x => x.EnableDefaultEventHandler); @@ -51,18 +66,17 @@ namespace Umbraco.Web.Search if (registeredProviders == 0) return; - global::umbraco.cms.businesslogic.media.Media.AfterSave += MediaAfterSave; - global::umbraco.cms.businesslogic.media.Media.AfterDelete += MediaAfterDelete; - CMSNode.AfterMove += MediaAfterMove; + MediaService.Saved += MediaServiceSaved; + MediaService.Deleted += MediaServiceDeleted; + MediaService.Moved += MediaServiceMoved; + ContentService.Saved += ContentServiceSaved; + ContentService.Deleted += ContentService_Deleted; + ContentService.Moved += ContentService_Moved; //These should only fire for providers that DONT have SupportUnpublishedContent set to true content.AfterUpdateDocumentCache += ContentAfterUpdateDocumentCache; content.AfterClearDocumentCache += ContentAfterClearDocumentCache; - //These should only fire for providers that have SupportUnpublishedContent set to true - Document.AfterSave += DocumentAfterSave; - Document.AfterDelete += DocumentAfterDelete; - Member.AfterSave += MemberAfterSave; Member.AfterDelete += MemberAfterDelete; @@ -78,6 +92,50 @@ namespace Umbraco.Web.Search } } + [SecuritySafeCritical] + void ContentService_Moved(IContentService sender, Umbraco.Core.Events.MoveEventArgs e) + { + IndexConent(e.Entity); + } + + [SecuritySafeCritical] + void ContentService_Deleted(IContentService sender, Umbraco.Core.Events.DeleteEventArgs e) + { + e.DeletedEntities.ForEach( + content => + ExamineManager.Instance.DeleteFromIndex( + content.Id.ToString(), + ExamineManager.Instance.IndexProviderCollection.OfType().Where(x => x.EnableDefaultEventHandler))); + } + + [SecuritySafeCritical] + void ContentServiceSaved(IContentService sender, Umbraco.Core.Events.SaveEventArgs e) + { + e.SavedEntities.ForEach(IndexConent); + } + + [SecuritySafeCritical] + void MediaServiceMoved(IMediaService sender, Umbraco.Core.Events.MoveEventArgs e) + { + IndexMedia(e.Entity); + } + + [SecuritySafeCritical] + void MediaServiceDeleted(IMediaService sender, Umbraco.Core.Events.DeleteEventArgs e) + { + e.DeletedEntities.ForEach( + media => + ExamineManager.Instance.DeleteFromIndex( + media.Id.ToString(), + ExamineManager.Instance.IndexProviderCollection.OfType().Where(x => x.EnableDefaultEventHandler))); + } + + [SecuritySafeCritical] + void MediaServiceSaved(IMediaService sender, Umbraco.Core.Events.SaveEventArgs e) + { + e.SavedEntities.ForEach(IndexMedia); + } + [SecuritySafeCritical] private static void MemberAfterSave(Member sender, SaveEventArgs e) { @@ -99,113 +157,6 @@ namespace Umbraco.Web.Search .Where(x => x.EnableDefaultEventHandler)); } - /// - /// Only index using providers that SupportUnpublishedContent - /// - /// - /// - [SecuritySafeCritical] - private static void DocumentAfterSave(Document sender, SaveEventArgs e) - { - //ensure that only the providers that have unpublishing support enabled - //that are also flagged to listen - - //there's a bug in 4.0.x that fires the Document saving event handler for media when media is moved, - //therefore, we need to try to figure out if this is media or content. Currently one way to do this - //is by checking the creator ID property as it will be null if it is media. We then need to try to - //create the media object, see if it exists, and pass it to the media save event handler... yeah i know, - //pretty horrible but has to be done. - - try - { - var creator = sender.Creator; - if (creator != null) - { - //it's def a Document - ExamineManager.Instance.ReIndexNode(ToXDocument(sender, false).Root, IndexTypes.Content, - ExamineManager.Instance.IndexProviderCollection.OfType() - .Where(x => x.SupportUnpublishedContent - && x.EnableDefaultEventHandler)); - - return; //exit, we've indexed the content - } - } - catch (Exception) - { - //if we get this exception, it means it's most likely media, so well do our check next. - - //TODO: Update this logic for 6.0 as we're not dealing with 4.0x - - //this is most likely media, not sure what kind of exception might get thrown in 4.0.x or 4.1 if accessing a null - //creator, so we catch everything. - } - - - - var m = new global::umbraco.cms.businesslogic.media.Media(sender.Id); - if (string.IsNullOrEmpty(m.Path)) return; - //this is a media item, no exception occurred on access to path and it's not empty which means it was found - MediaAfterSave(m, e); - } - - /// - /// Only remove indexes using providers that SupportUnpublishedContent - /// - /// - /// - [SecuritySafeCritical] - private static void DocumentAfterDelete(Document sender, DeleteEventArgs e) - { - var nodeId = sender.Id.ToString(); - - //ensure that only the providers that have unpublishing support enabled - //that are also flagged to listen - ExamineManager.Instance.DeleteFromIndex(nodeId, - ExamineManager.Instance.IndexProviderCollection.OfType() - .Where(x => x.SupportUnpublishedContent - && x.EnableDefaultEventHandler)); - } - - [SecuritySafeCritical] - private static void MediaAfterDelete(global::umbraco.cms.businesslogic.media.Media sender, DeleteEventArgs e) - { - var nodeId = sender.Id.ToString(); - - //ensure that only the providers are flagged to listen execute - ExamineManager.Instance.DeleteFromIndex(nodeId, - ExamineManager.Instance.IndexProviderCollection.OfType() - .Where(x => x.EnableDefaultEventHandler)); - } - - [SecuritySafeCritical] - private static void MediaAfterSave(global::umbraco.cms.businesslogic.media.Media sender, SaveEventArgs e) - { - //ensure that only the providers are flagged to listen execute - IndexMedia(sender); - } - - [SecuritySafeCritical] - private static void IndexMedia(global::umbraco.cms.businesslogic.media.Media sender) - { - ExamineManager.Instance.ReIndexNode(ToXDocument(sender, true).Root, IndexTypes.Media, - ExamineManager.Instance.IndexProviderCollection.OfType() - .Where(x => x.EnableDefaultEventHandler)); - } - - /// - /// When media is moved, re-index - /// - /// - /// - [SecuritySafeCritical] - private static void MediaAfterMove(object sender, MoveEventArgs e) - { - var media = sender as global::umbraco.cms.businesslogic.media.Media; - if (media == null) return; - global::umbraco.cms.businesslogic.media.Media m = media; - IndexMedia(m); - } - /// /// Only Update indexes for providers that dont SupportUnpublishedContent /// @@ -261,6 +212,20 @@ namespace Umbraco.Web.Search } + private void IndexMedia(IMedia sender) + { + ExamineManager.Instance.ReIndexNode( + sender.ToXml(), "media", + ExamineManager.Instance.IndexProviderCollection.OfType().Where(x => x.EnableDefaultEventHandler)); + } + + private void IndexConent(IContent sender) + { + ExamineManager.Instance.ReIndexNode( + sender.ToXml(), "content", + ExamineManager.Instance.IndexProviderCollection.OfType().Where(x => x.EnableDefaultEventHandler)); + } + /// /// Converts a content node to XDocument /// diff --git a/src/UmbracoExamine/BaseUmbracoIndexer.cs b/src/UmbracoExamine/BaseUmbracoIndexer.cs index 542706a99b..dc6c1c704f 100644 --- a/src/UmbracoExamine/BaseUmbracoIndexer.cs +++ b/src/UmbracoExamine/BaseUmbracoIndexer.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Web; using Examine.LuceneEngine.Providers; using Lucene.Net.Analysis; +using Umbraco.Core; using umbraco.BasePages; using umbraco.BusinessLogic; using UmbracoExamine.DataServices; @@ -92,6 +93,14 @@ namespace UmbracoExamine /// public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) { + + //We need to check if we actually can initialize, if not then don't continue + if (!CanInitialized()) + { + return; + } + + if (config["dataService"] != null && !string.IsNullOrEmpty(config["dataService"])) { //this should be a fully qualified type @@ -160,6 +169,22 @@ namespace UmbracoExamine #region Protected + /// + /// Returns true if the Umbraco application is in a state that we can initialize the examine indexes + /// + /// + protected bool CanInitialized() + { + //We need to check if we actually can initialize, if not then don't continue + if (ApplicationContext.Current == null + || !ApplicationContext.Current.IsConfigured + || !ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured) + { + return false; + } + return true; + } + ///// ///// Calls a web request in a worker thread to rebuild the indexes ///// diff --git a/src/UmbracoExamine/UmbracoContentIndexer.cs b/src/UmbracoExamine/UmbracoContentIndexer.cs index f58bec2dad..1fb1a19c69 100644 --- a/src/UmbracoExamine/UmbracoContentIndexer.cs +++ b/src/UmbracoExamine/UmbracoContentIndexer.cs @@ -120,6 +120,12 @@ namespace UmbracoExamine public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) { + //We need to check if we actually can initialize, if not then don't continue + if (!CanInitialized()) + { + return; + } + //check if there's a flag specifying to support unpublished content, //if not, set to false; bool supportUnpublished; diff --git a/src/UmbracoExamine/UmbracoExamineSearcher.cs b/src/UmbracoExamine/UmbracoExamineSearcher.cs index 5fe5d5ba87..b0fc1a54e9 100644 --- a/src/UmbracoExamine/UmbracoExamineSearcher.cs +++ b/src/UmbracoExamine/UmbracoExamineSearcher.cs @@ -5,6 +5,7 @@ using System.Security; using Examine; using Examine.Providers; using Examine.SearchCriteria; +using Umbraco.Core; using UmbracoExamine.Config; using Examine.LuceneEngine; using Examine.LuceneEngine.Providers; @@ -30,6 +31,17 @@ namespace UmbracoExamine { } + public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) + { + //We need to check if we actually can initialize, if not then don't continue + if (!CanInitialized()) + { + return; + } + + base.Initialize(name, config); + } + /// /// Constructor to allow for creating an indexer at runtime /// @@ -54,6 +66,22 @@ namespace UmbracoExamine #endregion + /// + /// Returns true if the Umbraco application is in a state that we can initialize the examine indexes + /// + /// + protected bool CanInitialized() + { + //We need to check if we actually can initialize, if not then don't continue + if (ApplicationContext.Current == null + || !ApplicationContext.Current.IsConfigured + || !ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured) + { + return false; + } + return true; + } + /// /// Override in order to set the nodeTypeAlias field name of the underlying SearchCriteria to __NodeTypeAlias ///