mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Merge
--HG-- branch : dev
This commit is contained in:
16
src/Orchard.Web/Modules/Orchard.Search/AdminMenu.cs
Normal file
16
src/Orchard.Web/Modules/Orchard.Search/AdminMenu.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Orchard.Localization;
|
||||
using Orchard.UI.Navigation;
|
||||
|
||||
namespace Orchard.Search {
|
||||
public class AdminMenu : INavigationProvider {
|
||||
public Localizer T { get; set; }
|
||||
public string MenuName { get { return "admin"; } }
|
||||
|
||||
public void GetNavigation(NavigationBuilder builder) {
|
||||
builder.Add(T("Site"), "11",
|
||||
menu => menu
|
||||
.Add(T("Search Index"), "10.0", item => item.Action("Index", "Admin", new {area = "Orchard.Search"})
|
||||
.Permission(Permissions.ManageSearchIndex)));
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Search.Services;
|
||||
using Orchard.Search.ViewModels;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.Search.Controllers {
|
||||
public class AdminController : Controller {
|
||||
private readonly ISearchService _searchService;
|
||||
|
||||
public AdminController(ISearchService searchService, IOrchardServices services) {
|
||||
_searchService = searchService;
|
||||
Services = services;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public IOrchardServices Services { get; private set; }
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public ActionResult Index() {
|
||||
var viewModel = new SearchIndexViewModel {HasIndexToManage = _searchService.HasIndexToManage, IndexUpdatedUtc = _searchService.GetIndexUpdatedUtc()};
|
||||
|
||||
if (!viewModel.HasIndexToManage)
|
||||
Services.Notifier.Information(T("There is not search index to manage for this site."));
|
||||
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Update() {
|
||||
if (!Services.Authorizer.Authorize(Permissions.ManageSearchIndex, T("Not allowed to manage the search index.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
_searchService.UpdateIndex();
|
||||
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Rebuild() {
|
||||
if (!Services.Authorizer.Authorize(Permissions.ManageSearchIndex, T("Not allowed to manage the search index.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
_searchService.RebuildIndex();
|
||||
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Search.Services;
|
||||
@@ -14,14 +15,17 @@ namespace Orchard.Search.Controllers {
|
||||
_contentManager = contentManager;
|
||||
}
|
||||
|
||||
public ActionResult Index(string q) {
|
||||
var searchViewModel = new SearchViewModel {Query = q};
|
||||
public ActionResult Index(string q, int page = 1, int pageSize = 10) {
|
||||
var searchViewModel = new SearchViewModel {
|
||||
Query = q,
|
||||
DefaultPageSize = 10, // <- yeah, I know :|
|
||||
PageOfResults = _searchService.Query(q, page, pageSize, searchHit => new SearchResultViewModel {
|
||||
Content = _contentManager.BuildDisplayModel(_contentManager.Get(searchHit.Id), "SummaryForSearch"),
|
||||
SearchHit = searchHit
|
||||
})
|
||||
};
|
||||
|
||||
var results = _searchService.Query(q);
|
||||
searchViewModel.Results = results.Select(result => new SearchResultViewModel {
|
||||
Content = _contentManager.BuildDisplayModel(_contentManager.Get(result.Id), "SummaryForSearch"),
|
||||
SearchHit = result
|
||||
}).ToList();
|
||||
//todo: deal with page requests beyond result count
|
||||
|
||||
return View(searchViewModel);
|
||||
}
|
||||
|
@@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Indexing;
|
||||
|
||||
namespace Orchard.Search.Models {
|
||||
public interface ISearchResult {
|
||||
IEnumerable<ISearchHit> Page { get; set; }
|
||||
int TotalCount { get; set; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Indexing;
|
||||
|
||||
namespace Orchard.Search.Models {
|
||||
public class SearchResult : ISearchResult {
|
||||
public IEnumerable<ISearchHit> Page { get; set; }
|
||||
public int TotalCount { get; set; }
|
||||
}
|
||||
}
|
@@ -65,11 +65,17 @@
|
||||
<Reference Include="System.EnterpriseServices" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AdminMenu.cs" />
|
||||
<Compile Include="Controllers\AdminController.cs" />
|
||||
<Compile Include="Controllers\SearchController.cs" />
|
||||
<Compile Include="Filters\SearchFilter.cs" />
|
||||
<Compile Include="Permissions.cs" />
|
||||
<Compile Include="Routes.cs" />
|
||||
<Compile Include="Models\ISearchResult.cs" />
|
||||
<Compile Include="Services\ISearchService.cs" />
|
||||
<Compile Include="Models\SearchResult.cs" />
|
||||
<Compile Include="Services\SearchService.cs" />
|
||||
<Compile Include="ViewModels\SearchIndexViewModel.cs" />
|
||||
<Compile Include="ViewModels\SearchResultViewModel.cs" />
|
||||
<Compile Include="ViewModels\SearchViewModel.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
@@ -82,7 +88,9 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Module.txt" />
|
||||
<Content Include="Styles\admin.css" />
|
||||
<Content Include="Styles\search.css" />
|
||||
<Content Include="Views\Admin\Index.ascx" />
|
||||
<Content Include="Views\SearchForm.ascx" />
|
||||
<Content Include="Views\Search\Index.ascx" />
|
||||
<Content Include="Views\Web.config" />
|
||||
|
29
src/Orchard.Web/Modules/Orchard.Search/Permissions.cs
Normal file
29
src/Orchard.Web/Modules/Orchard.Search/Permissions.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Security.Permissions;
|
||||
|
||||
namespace Orchard.Search {
|
||||
public class Permissions : IPermissionProvider {
|
||||
public static readonly Permission ManageSearchIndex = new Permission { Description = "Manage Search Index", Name = "ManageSearchIndex" };
|
||||
|
||||
public string ModuleName {
|
||||
get {
|
||||
return "Search";
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Permission> GetPermissions() {
|
||||
return new Permission[] {
|
||||
ManageSearchIndex,
|
||||
};
|
||||
}
|
||||
|
||||
public IEnumerable<PermissionStereotype> GetDefaultStereotypes() {
|
||||
return new[] {
|
||||
new PermissionStereotype {
|
||||
Name = "Administrator",
|
||||
Permissions = new[] {ManageSearchIndex}
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,8 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using Orchard.Collections;
|
||||
using Orchard.Indexing;
|
||||
|
||||
namespace Orchard.Search.Services {
|
||||
public interface ISearchService : IDependency {
|
||||
IEnumerable<ISearchHit> Query(string term);
|
||||
bool HasIndexToManage { get; }
|
||||
IPageOfItems<T> Query<T>(string query, int skip, int? take, Func<ISearchHit, T> shapeResult);
|
||||
void RebuildIndex();
|
||||
void UpdateIndex();
|
||||
DateTime GetIndexUpdatedUtc();
|
||||
}
|
||||
}
|
@@ -1,25 +1,84 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Orchard.Collections;
|
||||
using Orchard.Indexing;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Search.Models;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.Search.Services
|
||||
{
|
||||
public class SearchService : ISearchService
|
||||
{
|
||||
private const string SearchIndexName = "Search";
|
||||
private readonly IIndexManager _indexManager;
|
||||
private readonly IEnumerable<IIndexNotifierHandler> _indexNotifierHandlers;
|
||||
|
||||
public SearchService(IIndexManager indexManager) {
|
||||
public SearchService(IOrchardServices services, IIndexManager indexManager, IEnumerable<IIndexNotifierHandler> indexNotifierHandlers) {
|
||||
Services = services;
|
||||
_indexManager = indexManager;
|
||||
_indexNotifierHandlers = indexNotifierHandlers;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public IEnumerable<ISearchHit> Query(string term) {
|
||||
if (string.IsNullOrWhiteSpace(term) || !_indexManager.HasIndexProvider())
|
||||
return Enumerable.Empty<ISearchHit>();
|
||||
public IOrchardServices Services { get; set; }
|
||||
public Localizer T { get; set; }
|
||||
|
||||
return _indexManager.GetSearchIndexProvider().CreateSearchBuilder("search")
|
||||
.WithField("title", term)
|
||||
.WithField("body", term)
|
||||
.Search();
|
||||
public bool HasIndexToManage {
|
||||
get { return _indexManager.HasIndexProvider(); }
|
||||
}
|
||||
|
||||
IPageOfItems<T> ISearchService.Query<T>(string query, int page, int? pageSize, Func<ISearchHit, T> shapeResult) {
|
||||
if (string.IsNullOrWhiteSpace(query) || !_indexManager.HasIndexProvider())
|
||||
return null;
|
||||
|
||||
var searchBuilder = _indexManager.GetSearchIndexProvider().CreateSearchBuilder(SearchIndexName)
|
||||
.WithField("title", query)
|
||||
.WithField("body", query);
|
||||
|
||||
var totalCount = searchBuilder.Count();
|
||||
if (pageSize != null)
|
||||
searchBuilder = searchBuilder
|
||||
.Slice((page > 0 ? page - 1 : 0) * (int)pageSize, (int)pageSize);
|
||||
|
||||
|
||||
var pageOfItems = new PageOfItems<T>(searchBuilder.Search().Select(shapeResult)) {
|
||||
PageNumber = page,
|
||||
PageSize = pageSize != null ? (int) pageSize : totalCount,
|
||||
TotalItemCount = totalCount
|
||||
};
|
||||
|
||||
return pageOfItems;
|
||||
}
|
||||
|
||||
void ISearchService.RebuildIndex() {
|
||||
if (!_indexManager.HasIndexProvider()) {
|
||||
Services.Notifier.Warning(T("There is no search index to rebuild."));
|
||||
return;
|
||||
}
|
||||
|
||||
var searchProvider = _indexManager.GetSearchIndexProvider();
|
||||
if (searchProvider.Exists(SearchIndexName))
|
||||
searchProvider.DeleteIndex(SearchIndexName);
|
||||
|
||||
searchProvider.CreateIndex(SearchIndexName); // or just reset the updated date and let the background process recreate the index
|
||||
Services.Notifier.Information(T("The search index has been rebuilt."));
|
||||
}
|
||||
|
||||
void ISearchService.UpdateIndex() {
|
||||
|
||||
foreach(var handler in _indexNotifierHandlers) {
|
||||
handler.UpdateIndex(SearchIndexName);
|
||||
}
|
||||
|
||||
Services.Notifier.Information(T("The search index has been updated."));
|
||||
}
|
||||
|
||||
DateTime ISearchService.GetIndexUpdatedUtc() {
|
||||
return !HasIndexToManage
|
||||
? DateTime.MinValue
|
||||
: _indexManager.GetSearchIndexProvider().GetLastIndexUtc(SearchIndexName);
|
||||
}
|
||||
}
|
||||
}
|
3
src/Orchard.Web/Modules/Orchard.Search/Styles/admin.css
Normal file
3
src/Orchard.Web/Modules/Orchard.Search/Styles/admin.css
Normal file
@@ -0,0 +1,3 @@
|
||||
#main button {
|
||||
display:block;
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.Search.ViewModels {
|
||||
public class SearchIndexViewModel : BaseViewModel {
|
||||
public bool HasIndexToManage { get; set; }
|
||||
//todo: hang the index updated date off here to show in the admin UI (e.g. -> index updated: June 4, 2010 [update index])
|
||||
public DateTime IndexUpdatedUtc { get; set; }
|
||||
}
|
||||
}
|
@@ -1,9 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Collections;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.Search.ViewModels {
|
||||
public class SearchViewModel : BaseViewModel {
|
||||
public IEnumerable<SearchResultViewModel> Results { get; set; }
|
||||
public string Query { get; set; }
|
||||
public int DefaultPageSize { get; set; }
|
||||
public IPageOfItems<SearchResultViewModel> PageOfResults { get; set; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<Orchard.Search.ViewModels.SearchIndexViewModel>" %>
|
||||
<%@ Import Namespace="Orchard.Mvc.Html" %><%
|
||||
Html.RegisterStyle("admin.css"); %>
|
||||
<h1><%=Html.TitleForPage(T("Search Index Mangement").ToString()) %></h1><%
|
||||
using (Html.BeginForm("update", "admin", FormMethod.Post, new {area = "Orchard.Search"})) { %>
|
||||
<fieldset>
|
||||
<p><%=T("The search index was last updated {0}. <button type=\"submit\" title=\"Update the search index.\" class=\"primaryAction\">Update</button>", Html.DateTimeRelative(Model.IndexUpdatedUtc))%></p>
|
||||
<%=Html.AntiForgeryTokenOrchard() %>
|
||||
</fieldset><%
|
||||
}
|
||||
using (Html.BeginForm("rebuild", "admin", FormMethod.Post, new {area = "Orchard.Search"})) { %>
|
||||
<fieldset>
|
||||
<p><%=T("Rebuild the search index for a fresh start. <button type=\"submit\" title=\"Rebuild the search index.\">Rebuild</button>") %></p>
|
||||
<%=Html.AntiForgeryTokenOrchard() %>
|
||||
</fieldset><%
|
||||
} %>
|
@@ -1,14 +1,17 @@
|
||||
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<Orchard.Search.ViewModels.SearchViewModel>" %>
|
||||
<%@ Import Namespace="Orchard.Mvc.Html" %><%
|
||||
Html.RegisterStyle("search.css"); %>
|
||||
<h1><%=Html.TitleForPage(T("Search"))%></h1>
|
||||
<%
|
||||
Html.Zone("search"); %><%
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Model.Query)) { %>
|
||||
<p class="search-summary"><%=T("<em>{0}</em> results", Model.Results.Count()) %></p><%
|
||||
<h1><%=Html.TitleForPage(T("Search").Text)%></h1><%
|
||||
Html.Zone("search");
|
||||
if (!string.IsNullOrWhiteSpace(Model.Query)) {
|
||||
if (Model.PageOfResults.Count() == 0) { %>
|
||||
<p class="search-summary"><%=T("<em>zero</em> results") %></p><%
|
||||
}
|
||||
else { %>
|
||||
<p class="search-summary"><%=T("<em>{0} - {1}</em> of <em>{2}</em> results", Model.PageOfResults.StartPosition, Model.PageOfResults.EndPosition, Model.PageOfResults.TotalItemCount)%></p><%
|
||||
}
|
||||
}
|
||||
|
||||
if (Model.Results.Count() > 0) { %>
|
||||
<%=Html.UnorderedList(Model.Results, (r, i) => Html.DisplayForItem(r.Content).ToHtmlString(), "search-results contentItems") %><%
|
||||
if (Model.PageOfResults != null && Model.PageOfResults.Count() > 0) { %>
|
||||
<%=Html.UnorderedList(Model.PageOfResults, (r, i) => Html.DisplayForItem(r.Content).ToHtmlString() , "search-results contentItems") %>
|
||||
<%=Html.Pager(Model.PageOfResults, Model.PageOfResults.PageNumber, Model.DefaultPageSize, new {q = Model.Query}) %><%
|
||||
} %>
|
@@ -64,6 +64,7 @@ namespace Orchard.Setup.Services {
|
||||
"Navigation",
|
||||
"Scheduling",
|
||||
"Indexing",
|
||||
"Localization",
|
||||
"Settings",
|
||||
"XmlRpc",
|
||||
"Orchard.Users",
|
||||
|
@@ -1,7 +1,7 @@
|
||||
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<LogOnViewModel>" %>
|
||||
<%@ Import Namespace="Orchard.Users.ViewModels"%>
|
||||
<h1><%=Html.TitleForPage(Model.Title)%></h1>
|
||||
<p><%=_Encoded("Please enter your username and password.")%> <%= Html.ActionLink(T("Register"), "Register")%><%=_Encoded(" if you don't have an account.")%></p>
|
||||
<p><%=_Encoded("Please enter your username and password.")%> <%= Html.ActionLink(T("Register").Text, "Register")%><%=_Encoded(" if you don't have an account.")%></p>
|
||||
<%= Html.ValidationSummary(T("Login was unsuccessful. Please correct the errors and try again.").ToString())%>
|
||||
<%
|
||||
using (Html.BeginFormAntiForgeryPost(Url.Action("LogOn", new {ReturnUrl = Request.QueryString["ReturnUrl"]}))) { %>
|
||||
|
Reference in New Issue
Block a user