diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 1851f1eaea..fe1c22cbd5 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -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 diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 378fd0da10..1acfd47832 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -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) - return - } - - allEntries, err := tree.ListEntries() - 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) - return - } - - pages := make([]PageMeta, 0, len(entries)) - for _, entry := range entries { - if !entry.Entry.IsRegular() { - continue + if !git.IsErrNotExist(err) { + ctx.ServerError("GetBranchCommit", err) } - 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()), - }) + return } + + pages, err := wiki_service.ListWikiPages(gocontext.Context(ctx), commit) + if err != nil { + ctx.ServerError("ListWikiPages", err) + return + } + ctx.Data["Pages"] = pages ctx.HTML(http.StatusOK, tplWikiPages) diff --git a/routers/web/repo/wiki_test.go b/routers/web/repo/wiki_test.go index 66943ebd78..2366212b69 100644 --- a/routers/web/repo/wiki_test.go +++ b/routers/web/repo/wiki_test.go @@ -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 } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 30bbc2ac1e..1941994d54 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -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 +}