1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-01-20 16:50:28 -05:00

refactor: centralize listing wiki pages

This commit is contained in:
Snoweuph 2025-01-20 00:37:08 +01:00
parent 33470c35c3
commit 400018319a
No known key found for this signature in database
GPG key ID: BEFC41DA223CEC55
4 changed files with 62 additions and 83 deletions

View file

@ -314,22 +314,22 @@ func ListWikiPages(ctx *context.APIContext) {
skip := (page - 1) * limit
max := page * limit
entries, err := commit.ListEntries()
entries, err := wiki_service.ListWikiPages(ctx, commit)
if err != nil {
ctx.ServerError("ListEntries", err)
ctx.ServerError("ListWikiPages", err)
return
}
pages := make([]*api.WikiPageMetaData, 0, len(entries))
for i, entry := range entries {
if i < skip || i >= max || !entry.IsRegular() {
if i < skip || i >= max {
continue
}
c, err := wikiRepo.GetCommitByPath(entry.Name())
c, err := wikiRepo.GetCommitByPath(entry.GitEntryName)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
}
wikiName, err := wiki_service.GitPathToPath(entry.Name())
wikiName, err := wiki_service.GitPathToPath(entry.GitEntryName)
if err != nil {
if repo_model.IsErrWikiInvalidFileName(err) {
continue

View file

@ -141,50 +141,24 @@ func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName wiki_
func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
wikiRepo, commit, err := findWikiRepoCommit(ctx)
if err != nil {
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
}
}()
if err != nil {
if !git.IsErrNotExist(err) {
ctx.ServerError("GetBranchCommit", err)
}
return nil, nil
}
// Get page list.
entries, err := commit.ListEntries()
pages, err := wiki_service.ListWikiPages(gocontext.Context(ctx), commit)
if err != nil {
if wikiRepo != nil {
wikiRepo.Close()
}
ctx.ServerError("ListEntries", err)
ctx.ServerError("ListWikiPages", err)
return nil, nil
}
pages := make([]PageMeta, 0, len(entries))
for _, entry := range entries {
if !entry.IsRegular() {
continue
}
wikiName, err := wiki_service.GitPathToPath(entry.Name())
if err != nil {
if repo_model.IsErrWikiInvalidFileName(err) {
continue
}
if wikiRepo != nil {
wikiRepo.Close()
}
ctx.ServerError("WikiFilenameToName", err)
return nil, nil
} else if wikiName.IsSidebar() || wikiName.IsFooter() {
continue
}
_, displayName := wikiName.DisplayName()
pages = append(pages, PageMeta{
Name: displayName,
SubURL: wikiName.URLPath(),
GitEntryName: entry.Name(),
})
}
ctx.Data["Pages"] = pages
// get requested page name
@ -590,59 +564,24 @@ func WikiPages(ctx *context.Context) {
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
wikiRepo, commit, err := findWikiRepoCommit(ctx)
if err != nil {
if wikiRepo != nil {
wikiRepo.Close()
}
return
}
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
}
}()
treePath := "" // To support list sub folders' pages in the future
tree, err := commit.SubTree(treePath)
if err != nil {
ctx.ServerError("SubTree", err)
if !git.IsErrNotExist(err) {
ctx.ServerError("GetBranchCommit", err)
}
return
}
allEntries, err := tree.ListEntries()
pages, err := wiki_service.ListWikiPages(gocontext.Context(ctx), commit)
if err != nil {
ctx.ServerError("ListEntries", err)
return
}
allEntries.CustomSort(base.NaturalSortLess)
entries, _, err := allEntries.GetCommitsInfo(gocontext.Context(ctx), commit, treePath)
if err != nil {
ctx.ServerError("GetCommitsInfo", err)
ctx.ServerError("ListWikiPages", err)
return
}
pages := make([]PageMeta, 0, len(entries))
for _, entry := range entries {
if !entry.Entry.IsRegular() {
continue
}
wikiName, err := wiki_service.GitPathToPath(entry.Entry.Name())
if err != nil {
if repo_model.IsErrWikiInvalidFileName(err) {
continue
}
ctx.ServerError("WikiFilenameToName", err)
return
}
_, displayName := wikiName.DisplayName()
pages = append(pages, PageMeta{
Name: displayName,
SubURL: wikiName.URLPath(),
GitEntryName: entry.Entry.Name(),
UpdatedUnix: timeutil.TimeStamp(entry.Commit.Author.When.Unix()),
})
}
ctx.Data["Pages"] = pages
ctx.HTML(http.StatusOK, tplWikiPages)

View file

@ -65,7 +65,7 @@ func assertWikiNotExists(t *testing.T, repo *repo_model.Repository, wikiName wik
}
func assertPagesMetas(t *testing.T, expectedNames []string, metas any) {
pageMetas, ok := metas.([]PageMeta)
pageMetas, ok := metas.([]wiki_service.PageMeta)
if !assert.True(t, ok) {
return
}

View file

@ -15,11 +15,13 @@ import (
system_model "code.gitea.io/gitea/models/system"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/sync"
"code.gitea.io/gitea/modules/timeutil"
asymkey_service "code.gitea.io/gitea/services/asymkey"
repo_service "code.gitea.io/gitea/services/repository"
)
@ -447,3 +449,41 @@ func SearchWikiContents(ctx context.Context, repo *repo_model.Repository, keywor
return res, nil
}
type PageMeta struct {
Name string
SubURL string
GitEntryName string
UpdatedUnix timeutil.TimeStamp
}
func ListWikiPages(ctx context.Context, commit *git.Commit) ([]PageMeta, error) {
// Get page list.
entries, err := commit.ListEntries()
if err != nil {
return nil, err
}
entries.CustomSort(base.NaturalSortLess)
pages := make([]PageMeta, 0, len(entries))
for _, entry := range entries {
if !entry.IsRegular() {
continue
}
wikiName, err := GitPathToPath(entry.Name())
if err != nil {
if repo_model.IsErrWikiInvalidFileName(err) {
continue
}
return nil, err
} else if wikiName.IsSidebar() || wikiName.IsFooter() {
continue
}
_, displayName := wikiName.DisplayName()
pages = append(pages, PageMeta{
Name: displayName,
SubURL: wikiName.URLPath(),
GitEntryName: entry.Name(),
})
}
return pages, nil
}