2021-06-08 23:33:54 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-08 23:33:54 +00:00
|
|
|
|
|
|
|
package explore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-09-24 11:32:56 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 01:27:50 +00:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-06-08 23:33:54 +00:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-06-25 17:06:01 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-06-08 23:33:54 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-06-25 17:06:01 +00:00
|
|
|
"code.gitea.io/gitea/modules/sitemap"
|
2021-06-08 23:33:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// tplExploreRepos explore repositories page template
|
2023-02-04 13:26:38 +00:00
|
|
|
tplExploreRepos base.TplName = "explore/repos"
|
|
|
|
relevantReposOnlyParam string = "no_filter"
|
2021-06-08 23:33:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RepoSearchOptions when calling search repositories
|
|
|
|
type RepoSearchOptions struct {
|
2023-02-24 20:17:21 +00:00
|
|
|
OwnerID int64
|
|
|
|
Private bool
|
|
|
|
Restricted bool
|
|
|
|
PageSize int
|
|
|
|
OnlyShowRelevant bool
|
|
|
|
TplName base.TplName
|
2021-06-08 23:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RenderRepoSearch render repositories search page
|
2023-02-24 20:17:21 +00:00
|
|
|
// This function is also used to render the Admin Repository Management page.
|
2021-06-08 23:33:54 +00:00
|
|
|
func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
|
2022-06-25 17:06:01 +00:00
|
|
|
// Sitemap index for sitemap paths
|
|
|
|
page := int(ctx.ParamsInt64("idx"))
|
|
|
|
isSitemap := ctx.Params("idx") != ""
|
|
|
|
if page <= 1 {
|
|
|
|
page = ctx.FormInt("page")
|
|
|
|
}
|
|
|
|
|
2021-06-08 23:33:54 +00:00
|
|
|
if page <= 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2022-06-25 17:06:01 +00:00
|
|
|
if isSitemap {
|
|
|
|
opts.PageSize = setting.UI.SitemapPagingNum
|
|
|
|
}
|
|
|
|
|
2021-06-08 23:33:54 +00:00
|
|
|
var (
|
2023-02-24 20:17:21 +00:00
|
|
|
repos []*repo_model.Repository
|
|
|
|
count int64
|
|
|
|
err error
|
|
|
|
orderBy db.SearchOrderBy
|
2021-06-08 23:33:54 +00:00
|
|
|
)
|
|
|
|
|
2021-08-11 00:31:13 +00:00
|
|
|
ctx.Data["SortType"] = ctx.FormString("sort")
|
|
|
|
switch ctx.FormString("sort") {
|
2021-06-08 23:33:54 +00:00
|
|
|
case "newest":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderByNewest
|
2021-06-08 23:33:54 +00:00
|
|
|
case "oldest":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderByOldest
|
2021-06-08 23:33:54 +00:00
|
|
|
case "leastupdate":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderByLeastUpdated
|
2021-06-08 23:33:54 +00:00
|
|
|
case "reversealphabetically":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderByAlphabeticallyReverse
|
2021-06-08 23:33:54 +00:00
|
|
|
case "alphabetically":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderByAlphabetically
|
2021-06-08 23:33:54 +00:00
|
|
|
case "reversesize":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderBySizeReverse
|
2021-06-08 23:33:54 +00:00
|
|
|
case "size":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderBySize
|
2021-06-08 23:33:54 +00:00
|
|
|
case "moststars":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderByStarsReverse
|
2021-06-08 23:33:54 +00:00
|
|
|
case "feweststars":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderByStars
|
2021-06-08 23:33:54 +00:00
|
|
|
case "mostforks":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderByForksReverse
|
2021-06-08 23:33:54 +00:00
|
|
|
case "fewestforks":
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderByForks
|
2021-06-08 23:33:54 +00:00
|
|
|
default:
|
|
|
|
ctx.Data["SortType"] = "recentupdate"
|
2021-11-24 09:49:20 +00:00
|
|
|
orderBy = db.SearchOrderByRecentUpdated
|
2021-06-08 23:33:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 15:08:52 +00:00
|
|
|
keyword := ctx.FormTrim("q")
|
2022-08-25 18:38:41 +00:00
|
|
|
|
2023-02-24 20:17:21 +00:00
|
|
|
ctx.Data["OnlyShowRelevant"] = opts.OnlyShowRelevant
|
2022-08-25 18:38:41 +00:00
|
|
|
|
2021-07-29 01:42:15 +00:00
|
|
|
topicOnly := ctx.FormBool("topic")
|
2021-06-08 23:33:54 +00:00
|
|
|
ctx.Data["TopicOnly"] = topicOnly
|
|
|
|
|
2022-01-28 11:29:04 +00:00
|
|
|
language := ctx.FormTrim("language")
|
|
|
|
ctx.Data["Language"] = language
|
|
|
|
|
2022-11-19 08:12:33 +00:00
|
|
|
repos, count, err = repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
|
2021-09-24 11:32:56 +00:00
|
|
|
ListOptions: db.ListOptions{
|
2021-06-08 23:33:54 +00:00
|
|
|
Page: page,
|
|
|
|
PageSize: opts.PageSize,
|
|
|
|
},
|
2022-03-22 07:03:22 +00:00
|
|
|
Actor: ctx.Doer,
|
2021-06-08 23:33:54 +00:00
|
|
|
OrderBy: orderBy,
|
|
|
|
Private: opts.Private,
|
|
|
|
Keyword: keyword,
|
|
|
|
OwnerID: opts.OwnerID,
|
|
|
|
AllPublic: true,
|
|
|
|
AllLimited: true,
|
|
|
|
TopicOnly: topicOnly,
|
2022-01-28 11:29:04 +00:00
|
|
|
Language: language,
|
2021-06-08 23:33:54 +00:00
|
|
|
IncludeDescription: setting.UI.SearchRepoDescription,
|
2023-02-24 20:17:21 +00:00
|
|
|
OnlyShowRelevant: opts.OnlyShowRelevant,
|
2021-06-08 23:33:54 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SearchRepository", err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-25 17:06:01 +00:00
|
|
|
if isSitemap {
|
|
|
|
m := sitemap.NewSitemap()
|
|
|
|
for _, item := range repos {
|
|
|
|
m.Add(sitemap.URL{URL: item.HTMLURL(), LastMod: item.UpdatedUnix.AsTimePtr()})
|
|
|
|
}
|
|
|
|
ctx.Resp.Header().Set("Content-Type", "text/xml")
|
|
|
|
if _, err := m.WriteTo(ctx.Resp); err != nil {
|
|
|
|
log.Error("Failed writing sitemap: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-08 23:33:54 +00:00
|
|
|
ctx.Data["Keyword"] = keyword
|
|
|
|
ctx.Data["Total"] = count
|
|
|
|
ctx.Data["Repos"] = repos
|
|
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
|
|
|
|
|
|
|
pager := context.NewPagination(int(count), opts.PageSize, page, 5)
|
|
|
|
pager.SetDefaultParams(ctx)
|
|
|
|
pager.AddParam(ctx, "topic", "TopicOnly")
|
2022-01-28 11:29:04 +00:00
|
|
|
pager.AddParam(ctx, "language", "Language")
|
2023-02-04 13:26:38 +00:00
|
|
|
pager.AddParamString(relevantReposOnlyParam, ctx.FormString(relevantReposOnlyParam))
|
2021-06-08 23:33:54 +00:00
|
|
|
ctx.Data["Page"] = pager
|
|
|
|
|
|
|
|
ctx.HTML(http.StatusOK, opts.TplName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repos render explore repositories page
|
|
|
|
func Repos(ctx *context.Context) {
|
|
|
|
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
|
|
|
|
ctx.Data["Title"] = ctx.Tr("explore")
|
|
|
|
ctx.Data["PageIsExplore"] = true
|
|
|
|
ctx.Data["PageIsExploreRepositories"] = true
|
|
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
|
|
|
|
|
|
|
var ownerID int64
|
2022-03-22 07:03:22 +00:00
|
|
|
if ctx.Doer != nil && !ctx.Doer.IsAdmin {
|
|
|
|
ownerID = ctx.Doer.ID
|
2021-06-08 23:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RenderRepoSearch(ctx, &RepoSearchOptions{
|
2023-02-24 20:17:21 +00:00
|
|
|
PageSize: setting.UI.ExplorePagingNum,
|
|
|
|
OwnerID: ownerID,
|
|
|
|
Private: ctx.Doer != nil,
|
|
|
|
TplName: tplExploreRepos,
|
|
|
|
OnlyShowRelevant: !ctx.FormBool(relevantReposOnlyParam),
|
2021-06-08 23:33:54 +00:00
|
|
|
})
|
|
|
|
}
|