(refs #79)Wiki page search is available

This commit is contained in:
Naoki Takezoe
2016-04-01 20:18:58 +09:00
parent 5aa6f5bce3
commit 8c0581973e
7 changed files with 78 additions and 10 deletions

View File

@@ -1,9 +1,8 @@
package gitbucket.core.controller
import gitbucket.core.api._
import gitbucket.core.helper.xml
import gitbucket.core.model.Account
import gitbucket.core.service.{RepositoryService, ActivityService, AccountService, RepositorySearchService, IssuesService}
import gitbucket.core.service._
import gitbucket.core.util.Implicits._
import gitbucket.core.util.ControlUtil._
import gitbucket.core.util.{LDAPUtil, Keys, UsersAuthenticator, ReferrerAuthenticator, StringUtil}
@@ -138,13 +137,21 @@ trait IndexControllerBase extends ControllerBase {
target.toLowerCase match {
case "issue" => gitbucket.core.search.html.issues(
searchIssues(repository.owner, repository.name, query),
countFiles(repository.owner, repository.name, query),
searchIssues(repository.owner, repository.name, query),
countWikiPages(repository.owner, repository.name, query),
query, page, repository)
case "wiki" => gitbucket.core.search.html.wiki(
countFiles(repository.owner, repository.name, query),
countIssues(repository.owner, repository.name, query),
searchWikiPages(repository.owner, repository.name, query),
query, page, repository)
case _ => gitbucket.core.search.html.code(
searchFiles(repository.owner, repository.name, query),
countIssues(repository.owner, repository.name, query),
countWikiPages(repository.owner, repository.name, query),
query, page, repository)
}
}

View File

@@ -12,7 +12,8 @@ import org.eclipse.jgit.api.Git
import org.scalatra.i18n.Messages
class WikiController extends WikiControllerBase
with WikiService with RepositoryService with AccountService with ActivityService with CollaboratorsAuthenticator with ReferrerAuthenticator
with WikiService with RepositoryService with AccountService with ActivityService
with CollaboratorsAuthenticator with ReferrerAuthenticator
trait WikiControllerBase extends ControllerBase {
self: WikiService with RepositoryService with ActivityService with CollaboratorsAuthenticator with ReferrerAuthenticator =>

View File

@@ -53,7 +53,30 @@ trait RepositorySearchService { self: IssuesService =>
}
}
private def searchRepositoryFiles(git: Git, query: String): List[(String, String)] = {
def countWikiPages(owner: String, repository: String, query: String): Int =
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
if(JGitUtil.isEmpty(git)) 0 else searchRepositoryFiles(git, query).length
}
def searchWikiPages(owner: String, repository: String, query: String): List[FileSearchResult] =
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
if(JGitUtil.isEmpty(git)){
Nil
} else {
val files = searchRepositoryFiles(git, query)
val commits = JGitUtil.getLatestCommitFromPaths(git, files.map(_._1), "HEAD")
files.map { case (path, text) =>
val (highlightText, lineNumber) = getHighlightText(text, query)
FileSearchResult(
path.replaceFirst("\\.md$", ""),
commits(path).getCommitterIdent.getWhen,
highlightText,
lineNumber)
}
}
}
def searchRepositoryFiles(git: Git, query: String): List[(String, String)] = {
val revWalk = new RevWalk(git.getRepository)
val objectId = git.getRepository.resolve("HEAD")
val revCommit = revWalk.parseCommit(objectId)

View File

@@ -1,5 +1,6 @@
@(files: List[gitbucket.core.service.RepositorySearchService.FileSearchResult],
issueCount: Int,
wikiCount: Int,
query: String,
page: Int,
repository: gitbucket.core.service.RepositoryService.RepositoryInfo)(implicit context: gitbucket.core.controller.Context)
@@ -7,7 +8,7 @@
@import gitbucket.core.view.helpers._
@import gitbucket.core.service.RepositorySearchService._
@html.main("Search Results", Some(repository)){
@menu("code", files.size, issueCount, query, repository){
@menu("code", files.size, issueCount, wikiCount, query, repository){
@if(files.isEmpty){
<h4>We couldn't find any code matching '@query'</h4>
} else {

View File

@@ -1,5 +1,6 @@
@(issues: List[gitbucket.core.service.RepositorySearchService.IssueSearchResult],
fileCount: Int,
@(fileCount: Int,
issues: List[gitbucket.core.service.RepositorySearchService.IssueSearchResult],
wikiCount: Int,
query: String,
page: Int,
repository: gitbucket.core.service.RepositoryService.RepositoryInfo)(implicit context: gitbucket.core.controller.Context)
@@ -7,7 +8,7 @@
@import gitbucket.core.view.helpers._
@import gitbucket.core.service.RepositorySearchService._
@html.main("Search Results", Some(repository)){
@menu("issue", fileCount, issues.size, query, repository){
@menu("issue", fileCount, issues.size, wikiCount, query, repository){
@if(issues.isEmpty){
<h4>We couldn't find any code matching '@query'</h4>
} else {

View File

@@ -1,4 +1,4 @@
@(active: String, fileCount: Int, issueCount: Int, query: String,
@(active: String, fileCount: Int, issueCount: Int, wikiCount: Int, query: String,
repository: gitbucket.core.service.RepositoryService.RepositoryInfo)(body: Html)(implicit context: gitbucket.core.controller.Context)
@import context._
@import gitbucket.core.view.helpers._
@@ -21,6 +21,14 @@
}
</a>
</li>
<li@if(active=="wiki"){ class="active"}>
<a href="@url(repository)/search?q=@urlEncode(query)&type=wiki">
Wiki
@if(wikiCount != 0){
<span class="badge">@wikiCount</span>
}
</a>
</li>
</ul>
<form action="@url(repository)/search" method="GET" class="form-inline">
<input type="text" name="q" value="@query" class="form-control" style="width: 400px; margin-bottom: 0px;"/>

View File

@@ -0,0 +1,27 @@
@(fileCount: Int,
issueCount: Int,
wikis: List[gitbucket.core.service.RepositorySearchService.FileSearchResult],
query: String,
page: Int,
repository: gitbucket.core.service.RepositoryService.RepositoryInfo)(implicit context: gitbucket.core.controller.Context)
@import context._
@import gitbucket.core.view.helpers._
@import gitbucket.core.service.RepositorySearchService._
@html.main("Search Results", Some(repository)){
@menu("wiki", fileCount, issueCount, wikis.size, query, repository){
@if(wikis.isEmpty){
<h4>We couldn't find any code matching '@query'</h4>
} else {
<h4>We've found @wikis.size code @plural(wikis.size, "result")</h4>
}
@wikis.drop((page - 1) * CodeLimit).take(CodeLimit).map { file =>
<div>
<h5><a href="@url(repository)/wiki/@file.path">@file.path</a></h5>
<div class="small muted">Last committed @helper.html.datetimeago(file.lastModified)</div>
<pre class="prettyprint linenums:@file.highlightLineNumber" style="padding-left: 25px;">@Html(file.highlightText)</pre>
</div>
}
@helper.html.paginator(page, wikis.size, CodeLimit, 10,
s"${url(repository)}/search?q=${urlEncode(query)}&type=code")
}
}