mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Adding the ability to index drafts per index
This commit is contained in:
@@ -13,11 +13,21 @@ namespace Orchard.Indexing.Handlers {
|
||||
public CreateIndexingTaskHandler(IIndexingTaskManager indexingTaskManager) {
|
||||
_indexingTaskManager = indexingTaskManager;
|
||||
|
||||
OnCreated<ContentPart>(CreateIndexingTask);
|
||||
OnVersioned<ContentPart>(CreateIndexingTask);
|
||||
OnPublished<ContentPart>(CreateIndexingTask);
|
||||
OnUnpublished<ContentPart>(CreateIndexingTask);
|
||||
OnRemoved<ContentPart>(RemoveIndexingTask);
|
||||
}
|
||||
|
||||
void CreateIndexingTask(CreateContentContext context, ContentPart part) {
|
||||
_indexingTaskManager.CreateUpdateIndexTask(context.ContentItem);
|
||||
}
|
||||
|
||||
void CreateIndexingTask(VersionContentContext context, ContentPart part1, ContentPart part2) {
|
||||
_indexingTaskManager.CreateUpdateIndexTask(context.BuildingContentItem);
|
||||
}
|
||||
|
||||
void CreateIndexingTask(PublishContentContext context, ContentPart part) {
|
||||
// "Unpublish" case: Same as "remove"
|
||||
if (context.PublishingItemVersionRecord == null) {
|
||||
|
@@ -151,7 +151,7 @@ namespace Orchard.Indexing.Services {
|
||||
|
||||
// load all content items
|
||||
var contentItems = _contentRepository
|
||||
.Table.Where(versionRecord => versionRecord.Published && versionRecord.Id > indexSettings.LastContentId)
|
||||
.Table.Where(versionRecord => versionRecord.Latest && versionRecord.Id > indexSettings.LastContentId)
|
||||
.OrderBy(versionRecord => versionRecord.Id)
|
||||
.Take(ContentItemsPerLoop)
|
||||
.ToList()
|
||||
@@ -167,9 +167,20 @@ namespace Orchard.Indexing.Services {
|
||||
foreach (var item in contentItems) {
|
||||
try {
|
||||
|
||||
// skip items from types which are not indexed
|
||||
var settings = GetTypeIndexingSettings(item);
|
||||
|
||||
// skip items from types which are not indexed
|
||||
if (settings.List.Contains(indexName)) {
|
||||
if (item.HasPublished()) {
|
||||
var published = _contentManager.Get(item.Id, VersionOptions.Published);
|
||||
IDocumentIndex documentIndex = ExtractDocumentIndex(published);
|
||||
|
||||
if (documentIndex != null && documentIndex.IsDirty) {
|
||||
addToIndex.Add(documentIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (settings.List.Contains(indexName + ":latest")) {
|
||||
IDocumentIndex documentIndex = ExtractDocumentIndex(item);
|
||||
|
||||
if (documentIndex != null && documentIndex.IsDirty) {
|
||||
@@ -211,6 +222,10 @@ namespace Orchard.Indexing.Services {
|
||||
if (settings.List.Contains(indexName)) {
|
||||
documentIndex = ExtractDocumentIndex(item.ContentItem);
|
||||
}
|
||||
else if (settings.List.Contains(indexName + ":latest")) {
|
||||
var latest = _contentManager.Get(item.Id, VersionOptions.Latest);
|
||||
documentIndex = ExtractDocumentIndex(latest);
|
||||
}
|
||||
}
|
||||
|
||||
if (documentIndex == null || item.Delete) {
|
||||
@@ -295,7 +310,7 @@ namespace Orchard.Indexing.Services {
|
||||
/// </summary>
|
||||
private IDocumentIndex ExtractDocumentIndex(ContentItem contentItem) {
|
||||
// ignore deleted or unpublished items
|
||||
if (contentItem == null || !contentItem.IsPublished()) {
|
||||
if (contentItem == null || (!contentItem.IsPublished() && !contentItem.HasDraft())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@@ -27,11 +27,18 @@ namespace Orchard.Indexing.Settings {
|
||||
}
|
||||
|
||||
public override IEnumerable<TemplateViewModel> TypeEditorUpdate(ContentTypeDefinitionBuilder builder, IUpdateModel updateModel) {
|
||||
var previous = builder.Current.Settings.GetModel<TypeIndexing>();
|
||||
|
||||
var model = new TypeIndexing();
|
||||
updateModel.TryUpdateModel(model, "TypeIndexing", null, null);
|
||||
builder.WithSetting("TypeIndexing.Indexes", model.Indexes);
|
||||
|
||||
CreateIndexingTasks();
|
||||
// create indexing tasks only if settings have changed
|
||||
if (model.Indexes != previous.Indexes) {
|
||||
|
||||
// if a an index is added, all existing content items need to be re-indexed
|
||||
CreateIndexingTasks();
|
||||
}
|
||||
|
||||
yield return DefinitionTemplate(model);
|
||||
}
|
||||
@@ -39,8 +46,7 @@ namespace Orchard.Indexing.Settings {
|
||||
/// <summary>
|
||||
/// Creates new indexing tasks to update the index document for these content items
|
||||
/// </summary>
|
||||
private void CreateIndexingTasks()
|
||||
{
|
||||
private void CreateIndexingTasks() {
|
||||
if (!_tasksCreated) {
|
||||
CreateTasksForType(_contentTypeName);
|
||||
_tasksCreated = true;
|
||||
@@ -53,17 +59,27 @@ namespace Orchard.Indexing.Settings {
|
||||
}
|
||||
|
||||
public override IEnumerable<TemplateViewModel> PartFieldEditorUpdate(ContentPartFieldDefinitionBuilder builder, IUpdateModel updateModel) {
|
||||
var previous = builder.Current.Settings.GetModel<FieldIndexing>();
|
||||
|
||||
var model = new FieldIndexing();
|
||||
updateModel.TryUpdateModel(model, "FieldIndexing", null, null);
|
||||
builder.WithSetting("FieldIndexing.Included", model.Included ? true.ToString() : null);
|
||||
|
||||
// create indexing tasks only if settings have changed
|
||||
if (model.Included != previous.Included) {
|
||||
|
||||
// if a field setting has changed, all existing content items need to be re-indexed
|
||||
CreateIndexingTasks();
|
||||
}
|
||||
|
||||
CreateIndexingTasks();
|
||||
|
||||
yield return DefinitionTemplate(model);
|
||||
}
|
||||
|
||||
private void CreateTasksForType(string type) {
|
||||
foreach (var contentItem in _contentManager.Query(VersionOptions.Published, new [] { type }).List()) {
|
||||
// we create a task even for draft items, and the executor will filter based on the settings
|
||||
foreach (var contentItem in _contentManager.Query(VersionOptions.Latest, new [] { type }).List()) {
|
||||
_indexingTaskManager.CreateUpdateIndexTask(contentItem);
|
||||
}
|
||||
}
|
||||
|
@@ -10,12 +10,13 @@
|
||||
<fieldset>
|
||||
<legend>@T("Index this content type in:")</legend>
|
||||
@{ var i = 0;}
|
||||
@foreach (var index in indexProvider.List()) {
|
||||
<div>
|
||||
<input type="checkbox" value="@index" class="check-box" id="@Html.FieldIdFor(m => m.List[i])" name="@Html.FieldNameFor(m => m.List)" checked="@(Model.List.Contains(index))"/>
|
||||
<label for="@Html.FieldIdFor(m => m.List[i])" class="forcheckbox">@index</label>
|
||||
</div>
|
||||
i++;
|
||||
}
|
||||
<select size="0" name="@Html.FieldNameFor(m => m.List)">
|
||||
@foreach (var index in indexProvider.List()) {
|
||||
<option value="" selected="@(!Model.List.Contains(index + ":latest") && !Model.List.Contains(index))">@index - Not indexed</option>
|
||||
<option value="@index" selected="@(Model.List.Contains(index))">@index - Index published version</option>
|
||||
<option value="@(index):latest" selected="@(Model.List.Contains(index + ":latest"))">@index - Index any version</option>
|
||||
i++;
|
||||
}
|
||||
</select>
|
||||
</fieldset>
|
||||
}
|
@@ -27,7 +27,7 @@ namespace Orchard.FileSystems.LockFile {
|
||||
return false;
|
||||
}
|
||||
|
||||
lockFile = new LockFile(_appDataFolder, path, _clock.UtcNow.ToString(CultureInfo.InvariantCulture), _rwLock);
|
||||
lockFile = new LockFile(_appDataFolder, path, _clock.UtcNow.ToString("u"), _rwLock);
|
||||
return true;
|
||||
}
|
||||
catch {
|
||||
@@ -63,7 +63,7 @@ namespace Orchard.FileSystems.LockFile {
|
||||
// if expired the file is not removed
|
||||
// it should be automatically as there is a finalizer in LockFile
|
||||
// or the next taker can do it, unless it also fails, again
|
||||
return creationUtc.Add(Expiration) > _clock.UtcNow;
|
||||
return creationUtc.ToUniversalTime().Add(Expiration) > _clock.UtcNow;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user