2021-06-09 01:33:54 +02:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-09 01:33:54 +02:00
|
|
|
|
|
|
|
package explore
|
|
|
|
|
|
|
|
import (
|
2023-03-29 15:41:45 +02:00
|
|
|
"fmt"
|
2021-06-09 01:33:54 +02:00
|
|
|
"net/http"
|
|
|
|
|
2021-09-24 13:32:56 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-06-09 01:33:54 +02:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2022-06-25 19:06:01 +02:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-06-09 01:33:54 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-06-25 19:06:01 +02:00
|
|
|
"code.gitea.io/gitea/modules/sitemap"
|
2024-02-27 08:12:22 +01:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2021-06-09 01:33:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// tplExploreRepos explore repositories page template
|
2023-02-04 14:26:38 +01:00
|
|
|
tplExploreRepos base.TplName = "explore/repos"
|
2023-03-29 15:41:45 +02:00
|
|
|
relevantReposOnlyParam string = "only_show_relevant"
|
2021-06-09 01:33:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// RepoSearchOptions when calling search repositories
|
|
|
|
type RepoSearchOptions struct {
|
2023-02-24 20:11:31 +01:00
|
|
|
OwnerID int64
|
|
|
|
Private bool
|
|
|
|
Restricted bool
|
|
|
|
PageSize int
|
|
|
|
OnlyShowRelevant bool
|
|
|
|
TplName base.TplName
|
2021-06-09 01:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RenderRepoSearch render repositories search page
|
2023-02-24 20:11:31 +01:00
|
|
|
// This function is also used to render the Admin Repository Management page.
|
2021-06-09 01:33:54 +02:00
|
|
|
func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
|
2022-06-25 19:06:01 +02:00
|
|
|
// Sitemap index for sitemap paths
|
2024-06-19 00:32:45 +02:00
|
|
|
page := int(ctx.PathParamInt64("idx"))
|
|
|
|
isSitemap := ctx.PathParam("idx") != ""
|
2022-06-25 19:06:01 +02:00
|
|
|
if page <= 1 {
|
|
|
|
page = ctx.FormInt("page")
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:33:54 +02:00
|
|
|
if page <= 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2022-06-25 19:06:01 +02:00
|
|
|
if isSitemap {
|
|
|
|
opts.PageSize = setting.UI.SitemapPagingNum
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:33:54 +02:00
|
|
|
var (
|
2023-02-24 20:11:31 +01:00
|
|
|
repos []*repo_model.Repository
|
|
|
|
count int64
|
|
|
|
err error
|
|
|
|
orderBy db.SearchOrderBy
|
2021-06-09 01:33:54 +02:00
|
|
|
)
|
|
|
|
|
2024-09-05 12:53:30 +02:00
|
|
|
sortOrder := ctx.FormString("sort")
|
2023-11-09 11:11:45 +01:00
|
|
|
if sortOrder == "" {
|
|
|
|
sortOrder = setting.UI.ExploreDefaultSort
|
|
|
|
}
|
|
|
|
|
2024-06-15 08:45:02 +02:00
|
|
|
if order, ok := repo_model.OrderByFlatMap[sortOrder]; ok {
|
2024-06-13 11:13:11 +02:00
|
|
|
orderBy = order
|
|
|
|
} else {
|
|
|
|
sortOrder = "recentupdate"
|
2021-11-24 10:49:20 +01:00
|
|
|
orderBy = db.SearchOrderByRecentUpdated
|
2021-06-09 01:33:54 +02:00
|
|
|
}
|
2024-06-13 11:13:11 +02:00
|
|
|
ctx.Data["SortType"] = sortOrder
|
2021-06-09 01:33:54 +02:00
|
|
|
|
2021-08-11 17:08:52 +02:00
|
|
|
keyword := ctx.FormTrim("q")
|
2022-08-25 20:38:41 +02:00
|
|
|
|
2023-02-24 20:11:31 +01:00
|
|
|
ctx.Data["OnlyShowRelevant"] = opts.OnlyShowRelevant
|
2022-08-25 20:38:41 +02:00
|
|
|
|
2021-07-29 03:42:15 +02:00
|
|
|
topicOnly := ctx.FormBool("topic")
|
2021-06-09 01:33:54 +02:00
|
|
|
ctx.Data["TopicOnly"] = topicOnly
|
|
|
|
|
2022-01-28 12:29:04 +01:00
|
|
|
language := ctx.FormTrim("language")
|
|
|
|
ctx.Data["Language"] = language
|
|
|
|
|
2024-03-03 11:18:34 +01:00
|
|
|
archived := ctx.FormOptionalBool("archived")
|
|
|
|
ctx.Data["IsArchived"] = archived
|
|
|
|
|
|
|
|
fork := ctx.FormOptionalBool("fork")
|
|
|
|
ctx.Data["IsFork"] = fork
|
|
|
|
|
|
|
|
mirror := ctx.FormOptionalBool("mirror")
|
|
|
|
ctx.Data["IsMirror"] = mirror
|
|
|
|
|
|
|
|
template := ctx.FormOptionalBool("template")
|
|
|
|
ctx.Data["IsTemplate"] = template
|
|
|
|
|
|
|
|
private := ctx.FormOptionalBool("private")
|
|
|
|
ctx.Data["IsPrivate"] = private
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
repos, count, err = repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
|
2021-09-24 13:32:56 +02:00
|
|
|
ListOptions: db.ListOptions{
|
2021-06-09 01:33:54 +02:00
|
|
|
Page: page,
|
|
|
|
PageSize: opts.PageSize,
|
|
|
|
},
|
2022-03-22 08:03:22 +01:00
|
|
|
Actor: ctx.Doer,
|
2021-06-09 01:33:54 +02:00
|
|
|
OrderBy: orderBy,
|
|
|
|
Private: opts.Private,
|
|
|
|
Keyword: keyword,
|
|
|
|
OwnerID: opts.OwnerID,
|
|
|
|
AllPublic: true,
|
|
|
|
AllLimited: true,
|
|
|
|
TopicOnly: topicOnly,
|
2022-01-28 12:29:04 +01:00
|
|
|
Language: language,
|
2021-06-09 01:33:54 +02:00
|
|
|
IncludeDescription: setting.UI.SearchRepoDescription,
|
2023-02-24 20:11:31 +01:00
|
|
|
OnlyShowRelevant: opts.OnlyShowRelevant,
|
2024-03-03 11:18:34 +01:00
|
|
|
Archived: archived,
|
|
|
|
Fork: fork,
|
|
|
|
Mirror: mirror,
|
|
|
|
Template: template,
|
|
|
|
IsPrivate: private,
|
2021-06-09 01:33:54 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SearchRepository", err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-25 19:06:01 +02: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-09 01:33:54 +02: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)
|
2024-03-16 13:07:56 +01:00
|
|
|
pager.AddParamString("topic", fmt.Sprint(topicOnly))
|
|
|
|
pager.AddParamString("language", language)
|
2023-03-29 15:41:45 +02:00
|
|
|
pager.AddParamString(relevantReposOnlyParam, fmt.Sprint(opts.OnlyShowRelevant))
|
2024-08-15 18:34:24 +02:00
|
|
|
if archived.Has() {
|
|
|
|
pager.AddParamString("archived", fmt.Sprint(archived.Value()))
|
|
|
|
}
|
|
|
|
if fork.Has() {
|
|
|
|
pager.AddParamString("fork", fmt.Sprint(fork.Value()))
|
|
|
|
}
|
|
|
|
if mirror.Has() {
|
|
|
|
pager.AddParamString("mirror", fmt.Sprint(mirror.Value()))
|
|
|
|
}
|
|
|
|
if template.Has() {
|
|
|
|
pager.AddParamString("template", fmt.Sprint(template.Value()))
|
|
|
|
}
|
|
|
|
if private.Has() {
|
|
|
|
pager.AddParamString("private", fmt.Sprint(private.Value()))
|
|
|
|
}
|
2021-06-09 01:33:54 +02:00
|
|
|
ctx.Data["Page"] = pager
|
|
|
|
|
|
|
|
ctx.HTML(http.StatusOK, opts.TplName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repos render explore repositories page
|
|
|
|
func Repos(ctx *context.Context) {
|
2024-10-22 07:09:19 +02:00
|
|
|
ctx.Data["UsersPageIsDisabled"] = setting.Service.Explore.DisableUsersPage
|
|
|
|
ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage
|
|
|
|
ctx.Data["CodePageIsDisabled"] = setting.Service.Explore.DisableCodePage
|
2021-06-09 01:33:54 +02:00
|
|
|
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 08:03:22 +01:00
|
|
|
if ctx.Doer != nil && !ctx.Doer.IsAdmin {
|
|
|
|
ownerID = ctx.Doer.ID
|
2021-06-09 01:33:54 +02:00
|
|
|
}
|
|
|
|
|
2023-03-29 15:41:45 +02:00
|
|
|
onlyShowRelevant := setting.UI.OnlyShowRelevantRepos
|
|
|
|
|
|
|
|
_ = ctx.Req.ParseForm() // parse the form first, to prepare the ctx.Req.Form field
|
|
|
|
if len(ctx.Req.Form[relevantReposOnlyParam]) != 0 {
|
|
|
|
onlyShowRelevant = ctx.FormBool(relevantReposOnlyParam)
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:33:54 +02:00
|
|
|
RenderRepoSearch(ctx, &RepoSearchOptions{
|
2023-02-24 20:11:31 +01:00
|
|
|
PageSize: setting.UI.ExplorePagingNum,
|
|
|
|
OwnerID: ownerID,
|
|
|
|
Private: ctx.Doer != nil,
|
|
|
|
TplName: tplExploreRepos,
|
2023-03-29 15:41:45 +02:00
|
|
|
OnlyShowRelevant: onlyShowRelevant,
|
2021-06-09 01:33:54 +02:00
|
|
|
})
|
|
|
|
}
|