2015-11-26 09:10:25 +08:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2018-11-28 19:26:14 +08:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2015-11-26 09:10:25 +08:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2021-04-20 06:25:08 +08:00
|
|
|
"bytes"
|
2024-12-07 02:54:29 +08:00
|
|
|
gocontext "context"
|
2017-02-14 09:13:59 +08:00
|
|
|
"fmt"
|
2021-09-22 13:38:34 +08:00
|
|
|
"io"
|
2021-04-05 23:30:52 +08:00
|
|
|
"net/http"
|
2019-10-10 02:49:37 +08:00
|
|
|
"net/url"
|
2017-02-14 09:13:59 +08:00
|
|
|
"path/filepath"
|
2015-11-27 14:50:38 +08:00
|
|
|
"strings"
|
2015-11-27 13:24:24 +08:00
|
|
|
|
2022-06-12 23:51:54 +08:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-08-25 10:31:57 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 03:57:58 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2016-11-11 00:24:48 +08:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2022-01-07 09:18:52 +08:00
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2019-03-27 17:33:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2019-04-23 04:40:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-04-21 15:01:08 +08:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2017-09-21 13:20:14 +08:00
|
|
|
"code.gitea.io/gitea/modules/markup/markdown"
|
2021-06-26 19:28:55 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-08-15 22:46:21 +08:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2017-12-13 21:46:56 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-06-09 07:33:54 +08:00
|
|
|
"code.gitea.io/gitea/routers/common"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2021-04-07 03:44:05 +08:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2020-01-08 02:27:36 +08:00
|
|
|
wiki_service "code.gitea.io/gitea/services/wiki"
|
2015-11-26 09:10:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-07-08 16:20:22 +08:00
|
|
|
tplWikiStart base.TplName = "repo/wiki/start"
|
|
|
|
tplWikiView base.TplName = "repo/wiki/view"
|
|
|
|
tplWikiRevision base.TplName = "repo/wiki/revision"
|
|
|
|
tplWikiNew base.TplName = "repo/wiki/new"
|
|
|
|
tplWikiPages base.TplName = "repo/wiki/pages"
|
2015-11-26 09:10:25 +08:00
|
|
|
)
|
|
|
|
|
2016-11-21 18:03:42 +08:00
|
|
|
// MustEnableWiki check if wiki is enabled, if external then redirect
|
2016-03-12 00:56:52 +08:00
|
|
|
func MustEnableWiki(ctx *context.Context) {
|
2021-11-10 03:57:58 +08:00
|
|
|
if !ctx.Repo.CanRead(unit.TypeWiki) &&
|
|
|
|
!ctx.Repo.CanRead(unit.TypeExternalWiki) {
|
2019-04-23 04:40:51 +08:00
|
|
|
if log.IsTrace() {
|
|
|
|
log.Trace("Permission Denied: User %-v cannot read %-v or %-v of repo %-v\n"+
|
|
|
|
"User in repo has Permissions: %-+v",
|
2022-03-22 15:03:22 +08:00
|
|
|
ctx.Doer,
|
2021-11-10 03:57:58 +08:00
|
|
|
unit.TypeWiki,
|
|
|
|
unit.TypeExternalWiki,
|
2019-04-23 04:40:51 +08:00
|
|
|
ctx.Repo.Repository,
|
|
|
|
ctx.Repo.Permission)
|
|
|
|
}
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.NotFound("MustEnableWiki", nil)
|
2015-12-11 17:55:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-10 10:46:31 +08:00
|
|
|
unit, err := ctx.Repo.Repository.GetUnit(ctx, unit.TypeExternalWiki)
|
2017-02-04 23:53:46 +08:00
|
|
|
if err == nil {
|
|
|
|
ctx.Redirect(unit.ExternalWikiConfig().ExternalWikiURL)
|
2015-12-11 17:55:08 +08:00
|
|
|
return
|
2015-12-05 10:30:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 04:02:04 +08:00
|
|
|
// PageMeta wiki page meta information
|
2015-11-27 14:50:38 +08:00
|
|
|
type PageMeta struct {
|
2023-04-20 01:50:10 +08:00
|
|
|
Name string
|
|
|
|
SubURL string
|
|
|
|
GitEntryName string
|
|
|
|
UpdatedUnix timeutil.TimeStamp
|
2015-11-27 14:50:38 +08:00
|
|
|
}
|
2015-11-26 09:10:25 +08:00
|
|
|
|
2017-11-28 17:43:51 +08:00
|
|
|
// findEntryForFile finds the tree entry for a target filepath.
|
|
|
|
func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error) {
|
2020-01-28 17:44:08 +08:00
|
|
|
entry, err := commit.GetTreeEntryByPath(target)
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
2017-02-14 09:13:59 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-28 17:44:08 +08:00
|
|
|
if entry != nil {
|
|
|
|
return entry, nil
|
2017-02-14 09:13:59 +08:00
|
|
|
}
|
2020-01-28 17:44:08 +08:00
|
|
|
|
2023-04-20 01:50:10 +08:00
|
|
|
// Then the unescaped, the shortest alternative
|
2019-10-10 02:49:37 +08:00
|
|
|
var unescapedTarget string
|
|
|
|
if unescapedTarget, err = url.QueryUnescape(target); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-28 17:44:08 +08:00
|
|
|
return commit.GetTreeEntryByPath(unescapedTarget)
|
2017-02-14 09:13:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
|
2024-03-06 14:26:32 +08:00
|
|
|
wikiGitRepo, errGitRepo := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
|
|
|
|
if errGitRepo != nil {
|
|
|
|
ctx.ServerError("OpenRepository", errGitRepo)
|
|
|
|
return nil, nil, errGitRepo
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, errCommit := wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
|
|
|
|
if git.IsErrNotExist(errCommit) {
|
|
|
|
// if the default branch recorded in database is out of sync, then re-sync it
|
2024-03-08 15:30:10 +08:00
|
|
|
gitRepoDefaultBranch, errBranch := gitrepo.GetWikiDefaultBranch(ctx, ctx.Repo.Repository)
|
2024-03-06 14:26:32 +08:00
|
|
|
if errBranch != nil {
|
|
|
|
return wikiGitRepo, nil, errBranch
|
|
|
|
}
|
|
|
|
// update the default branch in the database
|
|
|
|
errDb := repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, DefaultWikiBranch: gitRepoDefaultBranch}, "default_wiki_branch")
|
|
|
|
if errDb != nil {
|
|
|
|
return wikiGitRepo, nil, errDb
|
|
|
|
}
|
|
|
|
ctx.Repo.Repository.DefaultWikiBranch = gitRepoDefaultBranch
|
|
|
|
// retry to get the commit from the correct default branch
|
|
|
|
commit, errCommit = wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
|
2015-11-27 13:24:24 +08:00
|
|
|
}
|
2024-03-06 14:26:32 +08:00
|
|
|
if errCommit != nil {
|
|
|
|
return wikiGitRepo, nil, errCommit
|
2017-02-14 09:13:59 +08:00
|
|
|
}
|
2024-03-06 14:26:32 +08:00
|
|
|
return wikiGitRepo, commit, nil
|
2017-02-14 09:13:59 +08:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:43:51 +08:00
|
|
|
// wikiContentsByEntry returns the contents of the wiki page referenced by the
|
|
|
|
// given tree entry. Writes to ctx if an error occurs.
|
|
|
|
func wikiContentsByEntry(ctx *context.Context, entry *git.TreeEntry) []byte {
|
2019-04-19 20:17:27 +08:00
|
|
|
reader, err := entry.Blob().DataAsync()
|
2017-11-28 17:43:51 +08:00
|
|
|
if err != nil {
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.ServerError("Blob.Data", err)
|
2017-11-28 17:43:51 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-19 20:17:27 +08:00
|
|
|
defer reader.Close()
|
2021-09-22 13:38:34 +08:00
|
|
|
content, err := io.ReadAll(reader)
|
2017-11-28 17:43:51 +08:00
|
|
|
if err != nil {
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.ServerError("ReadAll", err)
|
2017-11-28 17:43:51 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2024-08-17 11:19:26 +08:00
|
|
|
// wikiEntryByName returns the entry of a wiki page, along with a boolean
|
|
|
|
// indicating whether the entry exists. Writes to ctx if an error occurs.
|
|
|
|
// The last return value indicates whether the file should be returned as a raw file
|
|
|
|
func wikiEntryByName(ctx *context.Context, commit *git.Commit, wikiName wiki_service.WebPath) (*git.TreeEntry, string, bool, bool) {
|
|
|
|
isRaw := false
|
2023-04-20 01:50:10 +08:00
|
|
|
gitFilename := wiki_service.WebPathToGitPath(wikiName)
|
|
|
|
entry, err := findEntryForFile(commit, gitFilename)
|
2020-01-28 17:44:08 +08:00
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.ServerError("findEntryForFile", err)
|
2024-08-17 11:19:26 +08:00
|
|
|
return nil, "", false, false
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
// check if the file without ".md" suffix exists
|
|
|
|
gitFilename := strings.TrimSuffix(gitFilename, ".md")
|
|
|
|
entry, err = findEntryForFile(commit, gitFilename)
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
ctx.ServerError("findEntryForFile", err)
|
|
|
|
return nil, "", false, false
|
|
|
|
}
|
|
|
|
isRaw = true
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, "", true, false
|
|
|
|
}
|
|
|
|
return entry, gitFilename, false, isRaw
|
|
|
|
}
|
|
|
|
|
|
|
|
// wikiContentsByName returns the contents of a wiki page, along with a boolean
|
|
|
|
// indicating whether the page exists. Writes to ctx if an error occurs.
|
|
|
|
func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName wiki_service.WebPath) ([]byte, *git.TreeEntry, string, bool) {
|
|
|
|
entry, gitFilename, noEntry, _ := wikiEntryByName(ctx, commit, wikiName)
|
|
|
|
if entry == nil {
|
2019-07-08 16:20:22 +08:00
|
|
|
return nil, nil, "", true
|
2017-11-28 17:43:51 +08:00
|
|
|
}
|
2024-08-17 11:19:26 +08:00
|
|
|
return wikiContentsByEntry(ctx, entry), entry, gitFilename, noEntry
|
2017-11-28 17:43:51 +08:00
|
|
|
}
|
|
|
|
|
2019-07-08 16:20:22 +08:00
|
|
|
func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
|
2017-02-14 09:13:59 +08:00
|
|
|
wikiRepo, commit, err := findWikiRepoCommit(ctx)
|
|
|
|
if err != nil {
|
2021-08-31 04:50:35 +08:00
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
2018-12-10 06:45:44 +08:00
|
|
|
if !git.IsErrNotExist(err) {
|
|
|
|
ctx.ServerError("GetBranchCommit", err)
|
|
|
|
}
|
2017-02-14 09:13:59 +08:00
|
|
|
return nil, nil
|
2015-11-27 14:50:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get page list.
|
2019-07-08 16:20:22 +08:00
|
|
|
entries, err := commit.ListEntries()
|
|
|
|
if err != nil {
|
2019-11-13 15:01:19 +08:00
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
ctx.ServerError("ListEntries", err)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
pages := make([]PageMeta, 0, len(entries))
|
|
|
|
for _, entry := range entries {
|
|
|
|
if !entry.IsRegular() {
|
|
|
|
continue
|
2015-11-27 14:50:38 +08:00
|
|
|
}
|
2023-04-20 01:50:10 +08:00
|
|
|
wikiName, err := wiki_service.GitPathToWebPath(entry.Name())
|
2019-07-08 16:20:22 +08:00
|
|
|
if err != nil {
|
2022-08-25 10:31:57 +08:00
|
|
|
if repo_model.IsErrWikiInvalidFileName(err) {
|
2017-11-28 17:43:51 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-11-13 15:01:19 +08:00
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
ctx.ServerError("WikiFilenameToName", err)
|
|
|
|
return nil, nil
|
|
|
|
} else if wikiName == "_Sidebar" || wikiName == "_Footer" {
|
|
|
|
continue
|
2015-11-27 14:50:38 +08:00
|
|
|
}
|
2023-04-20 01:50:10 +08:00
|
|
|
_, displayName := wiki_service.WebPathToUserTitle(wikiName)
|
2019-07-08 16:20:22 +08:00
|
|
|
pages = append(pages, PageMeta{
|
2023-04-20 01:50:10 +08:00
|
|
|
Name: displayName,
|
|
|
|
SubURL: wiki_service.WebPathToURLPath(wikiName),
|
|
|
|
GitEntryName: entry.Name(),
|
2019-07-08 16:20:22 +08:00
|
|
|
})
|
2015-11-27 13:24:24 +08:00
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
ctx.Data["Pages"] = pages
|
2015-11-27 13:24:24 +08:00
|
|
|
|
2023-04-20 01:50:10 +08:00
|
|
|
// get requested page name
|
2023-08-09 14:57:45 +08:00
|
|
|
pageName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*"))
|
2017-11-28 17:43:51 +08:00
|
|
|
if len(pageName) == 0 {
|
|
|
|
pageName = "Home"
|
2015-11-27 13:24:24 +08:00
|
|
|
}
|
2023-04-20 01:50:10 +08:00
|
|
|
|
|
|
|
_, displayName := wiki_service.WebPathToUserTitle(pageName)
|
|
|
|
ctx.Data["PageURL"] = wiki_service.WebPathToURLPath(pageName)
|
|
|
|
ctx.Data["old_title"] = displayName
|
|
|
|
ctx.Data["Title"] = displayName
|
|
|
|
ctx.Data["title"] = displayName
|
2015-11-27 13:24:24 +08:00
|
|
|
|
2022-04-27 07:24:20 +08:00
|
|
|
isSideBar := pageName == "_Sidebar"
|
|
|
|
isFooter := pageName == "_Footer"
|
|
|
|
|
2024-08-17 11:19:26 +08:00
|
|
|
// lookup filename in wiki - get gitTree entry , real filename
|
|
|
|
entry, pageFilename, noEntry, isRaw := wikiEntryByName(ctx, commit, pageName)
|
2019-07-08 16:20:22 +08:00
|
|
|
if noEntry {
|
2021-11-17 02:18:25 +08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/?action=_pages")
|
2019-07-08 16:20:22 +08:00
|
|
|
}
|
2024-08-17 11:19:26 +08:00
|
|
|
if isRaw {
|
|
|
|
ctx.Redirect(util.URLJoin(ctx.Repo.RepoLink, "wiki/raw", string(pageName)))
|
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
if entry == nil || ctx.Written() {
|
2019-11-13 15:01:19 +08:00
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
2017-02-14 09:13:59 +08:00
|
|
|
return nil, nil
|
2015-11-27 13:24:24 +08:00
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
|
2024-08-17 11:19:26 +08:00
|
|
|
// get filecontent
|
|
|
|
data := wikiContentsByEntry(ctx, entry)
|
|
|
|
if ctx.Written() {
|
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-04-27 07:24:20 +08:00
|
|
|
var sidebarContent []byte
|
|
|
|
if !isSideBar {
|
|
|
|
sidebarContent, _, _, _ = wikiContentsByName(ctx, commit, "_Sidebar")
|
|
|
|
if ctx.Written() {
|
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
|
|
|
return nil, nil
|
2019-11-13 15:01:19 +08:00
|
|
|
}
|
2022-04-27 07:24:20 +08:00
|
|
|
} else {
|
|
|
|
sidebarContent = data
|
2017-02-14 09:13:59 +08:00
|
|
|
}
|
2017-11-28 17:43:51 +08:00
|
|
|
|
2022-04-27 07:24:20 +08:00
|
|
|
var footerContent []byte
|
|
|
|
if !isFooter {
|
|
|
|
footerContent, _, _, _ = wikiContentsByName(ctx, commit, "_Footer")
|
|
|
|
if ctx.Written() {
|
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
|
|
|
return nil, nil
|
2019-11-13 15:01:19 +08:00
|
|
|
}
|
2022-04-27 07:24:20 +08:00
|
|
|
} else {
|
|
|
|
footerContent = data
|
2019-07-08 16:20:22 +08:00
|
|
|
}
|
2017-11-28 17:43:51 +08:00
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
rctx := &markup.RenderContext{
|
2024-01-15 16:49:24 +08:00
|
|
|
Ctx: ctx,
|
|
|
|
Metas: ctx.Repo.Repository.ComposeDocumentMetas(ctx),
|
|
|
|
Links: markup.Links{
|
|
|
|
Base: ctx.Repo.RepoLink,
|
|
|
|
},
|
|
|
|
IsWiki: true,
|
2021-04-20 06:25:08 +08:00
|
|
|
}
|
2022-08-14 02:32:34 +08:00
|
|
|
buf := &strings.Builder{}
|
2021-04-20 06:25:08 +08:00
|
|
|
|
2022-08-14 02:32:34 +08:00
|
|
|
renderFn := func(data []byte) (escaped *charset.EscapeStatus, output string, err error) {
|
|
|
|
markupRd, markupWr := io.Pipe()
|
|
|
|
defer markupWr.Close()
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
// We allow NBSP here this is rendered
|
|
|
|
escaped, _ = charset.EscapeControlReader(markupRd, buf, ctx.Locale, charset.RuneNBSP)
|
|
|
|
output = buf.String()
|
|
|
|
buf.Reset()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = markdown.Render(rctx, bytes.NewReader(data), markupWr)
|
|
|
|
_ = markupWr.CloseWithError(err)
|
|
|
|
<-done
|
|
|
|
return escaped, output, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["EscapeStatus"], ctx.Data["content"], err = renderFn(data)
|
|
|
|
if err != nil {
|
2021-08-31 04:50:35 +08:00
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
2021-04-20 06:25:08 +08:00
|
|
|
ctx.ServerError("Render", err)
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-01-07 09:18:52 +08:00
|
|
|
|
2023-06-24 03:51:43 +08:00
|
|
|
if rctx.SidebarTocNode != nil {
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
err = markdown.SpecializedMarkdown().Renderer().Render(sb, nil, rctx.SidebarTocNode)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to render wiki sidebar TOC: %v", err)
|
|
|
|
} else {
|
|
|
|
ctx.Data["sidebarTocContent"] = sb.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 07:24:20 +08:00
|
|
|
if !isSideBar {
|
|
|
|
buf.Reset()
|
2022-08-14 02:32:34 +08:00
|
|
|
ctx.Data["sidebarEscapeStatus"], ctx.Data["sidebarContent"], err = renderFn(sidebarContent)
|
|
|
|
if err != nil {
|
2022-04-27 07:24:20 +08:00
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
|
|
|
ctx.ServerError("Render", err)
|
|
|
|
return nil, nil
|
2021-08-31 04:50:35 +08:00
|
|
|
}
|
2022-04-27 07:24:20 +08:00
|
|
|
ctx.Data["sidebarPresent"] = sidebarContent != nil
|
|
|
|
} else {
|
|
|
|
ctx.Data["sidebarPresent"] = false
|
2021-04-20 06:25:08 +08:00
|
|
|
}
|
|
|
|
|
2022-04-27 07:24:20 +08:00
|
|
|
if !isFooter {
|
|
|
|
buf.Reset()
|
2022-08-14 02:32:34 +08:00
|
|
|
ctx.Data["footerEscapeStatus"], ctx.Data["footerContent"], err = renderFn(footerContent)
|
|
|
|
if err != nil {
|
2022-04-27 07:24:20 +08:00
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
|
|
|
ctx.ServerError("Render", err)
|
|
|
|
return nil, nil
|
2021-08-31 04:50:35 +08:00
|
|
|
}
|
2022-04-27 07:24:20 +08:00
|
|
|
ctx.Data["footerPresent"] = footerContent != nil
|
|
|
|
} else {
|
|
|
|
ctx.Data["footerPresent"] = false
|
2021-04-20 06:25:08 +08:00
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
|
|
|
|
// get commit count - wiki revisions
|
2024-03-06 14:26:32 +08:00
|
|
|
commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.DefaultWikiBranch, pageFilename)
|
2019-07-08 16:20:22 +08:00
|
|
|
ctx.Data["CommitCount"] = commitsCount
|
|
|
|
|
|
|
|
return wikiRepo, entry
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
|
|
|
|
wikiRepo, commit, err := findWikiRepoCommit(ctx)
|
|
|
|
if err != nil {
|
2019-11-13 15:01:19 +08:00
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
if !git.IsErrNotExist(err) {
|
|
|
|
ctx.ServerError("GetBranchCommit", err)
|
2017-02-14 09:13:59 +08:00
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// get requested pagename
|
2023-08-09 14:57:45 +08:00
|
|
|
pageName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*"))
|
2019-07-08 16:20:22 +08:00
|
|
|
if len(pageName) == 0 {
|
|
|
|
pageName = "Home"
|
|
|
|
}
|
2023-04-20 01:50:10 +08:00
|
|
|
|
|
|
|
_, displayName := wiki_service.WebPathToUserTitle(pageName)
|
|
|
|
ctx.Data["PageURL"] = wiki_service.WebPathToURLPath(pageName)
|
|
|
|
ctx.Data["old_title"] = displayName
|
|
|
|
ctx.Data["Title"] = displayName
|
|
|
|
ctx.Data["title"] = displayName
|
|
|
|
|
2020-05-17 00:38:40 +08:00
|
|
|
ctx.Data["Username"] = ctx.Repo.Owner.Name
|
|
|
|
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
|
2017-11-28 17:43:51 +08:00
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// lookup filename in wiki - get filecontent, gitTree entry , real filename
|
2019-07-08 16:20:22 +08:00
|
|
|
data, entry, pageFilename, noEntry := wikiContentsByName(ctx, commit, pageName)
|
|
|
|
if noEntry {
|
2021-11-17 02:18:25 +08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/?action=_pages")
|
2019-07-08 16:20:22 +08:00
|
|
|
}
|
|
|
|
if entry == nil || ctx.Written() {
|
2019-11-13 15:01:19 +08:00
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
return nil, nil
|
2015-11-27 14:50:38 +08:00
|
|
|
}
|
|
|
|
|
2019-07-08 16:20:22 +08:00
|
|
|
ctx.Data["content"] = string(data)
|
|
|
|
ctx.Data["sidebarPresent"] = false
|
|
|
|
ctx.Data["sidebarContent"] = ""
|
|
|
|
ctx.Data["footerPresent"] = false
|
|
|
|
ctx.Data["footerContent"] = ""
|
|
|
|
|
|
|
|
// get commit count - wiki revisions
|
2024-03-06 14:26:32 +08:00
|
|
|
commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.DefaultWikiBranch, pageFilename)
|
2019-07-08 16:20:22 +08:00
|
|
|
ctx.Data["CommitCount"] = commitsCount
|
|
|
|
|
|
|
|
// get page
|
2021-07-29 09:42:15 +08:00
|
|
|
page := ctx.FormInt("page")
|
2019-07-08 16:20:22 +08:00
|
|
|
if page <= 1 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// get Commit Count
|
2023-05-08 15:10:53 +08:00
|
|
|
commitsHistory, err := wikiRepo.CommitsByFileAndRange(
|
|
|
|
git.CommitsByFileAndRangeOptions{
|
2024-03-06 14:26:32 +08:00
|
|
|
Revision: ctx.Repo.Repository.DefaultWikiBranch,
|
2023-05-08 15:10:53 +08:00
|
|
|
File: pageFilename,
|
|
|
|
Page: page,
|
|
|
|
})
|
2019-07-08 16:20:22 +08:00
|
|
|
if err != nil {
|
2019-11-13 15:01:19 +08:00
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
2022-08-15 09:22:13 +08:00
|
|
|
ctx.ServerError("CommitsByFileAndRange", err)
|
2019-07-08 16:20:22 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|
2023-01-09 11:50:54 +08:00
|
|
|
ctx.Data["Commits"] = git_model.ConvertFromGitCommit(ctx, commitsHistory, ctx.Repo.Repository)
|
2019-07-08 16:20:22 +08:00
|
|
|
|
2021-06-26 19:28:55 +08:00
|
|
|
pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
|
2019-07-08 16:20:22 +08:00
|
|
|
pager.SetDefaultParams(ctx)
|
2024-08-04 10:45:41 +08:00
|
|
|
pager.AddParamString("action", "_revision")
|
2019-07-08 16:20:22 +08:00
|
|
|
ctx.Data["Page"] = pager
|
|
|
|
|
2017-02-14 09:13:59 +08:00
|
|
|
return wikiRepo, entry
|
2015-11-27 14:50:38 +08:00
|
|
|
}
|
|
|
|
|
2019-07-08 16:20:22 +08:00
|
|
|
func renderEditPage(ctx *context.Context) {
|
2019-11-13 15:01:19 +08:00
|
|
|
wikiRepo, commit, err := findWikiRepoCommit(ctx)
|
2024-03-06 14:26:32 +08:00
|
|
|
defer func() {
|
2019-11-13 15:01:19 +08:00
|
|
|
if wikiRepo != nil {
|
2024-03-06 14:26:32 +08:00
|
|
|
_ = wikiRepo.Close()
|
2019-11-13 15:01:19 +08:00
|
|
|
}
|
2024-03-06 14:26:32 +08:00
|
|
|
}()
|
|
|
|
if err != nil {
|
2019-07-08 16:20:22 +08:00
|
|
|
if !git.IsErrNotExist(err) {
|
|
|
|
ctx.ServerError("GetBranchCommit", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get requested pagename
|
2023-08-09 14:57:45 +08:00
|
|
|
pageName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*"))
|
2019-07-08 16:20:22 +08:00
|
|
|
if len(pageName) == 0 {
|
|
|
|
pageName = "Home"
|
|
|
|
}
|
2023-04-20 01:50:10 +08:00
|
|
|
|
|
|
|
_, displayName := wiki_service.WebPathToUserTitle(pageName)
|
|
|
|
ctx.Data["PageURL"] = wiki_service.WebPathToURLPath(pageName)
|
|
|
|
ctx.Data["old_title"] = displayName
|
|
|
|
ctx.Data["Title"] = displayName
|
|
|
|
ctx.Data["title"] = displayName
|
2019-07-08 16:20:22 +08:00
|
|
|
|
2024-08-17 11:19:26 +08:00
|
|
|
// lookup filename in wiki - gitTree entry , real filename
|
|
|
|
entry, _, noEntry, isRaw := wikiEntryByName(ctx, commit, pageName)
|
2019-07-08 16:20:22 +08:00
|
|
|
if noEntry {
|
2021-11-17 02:18:25 +08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/?action=_pages")
|
2019-07-08 16:20:22 +08:00
|
|
|
}
|
2024-08-17 11:19:26 +08:00
|
|
|
if isRaw {
|
|
|
|
ctx.Error(http.StatusForbidden, "Editing of raw wiki files is not allowed")
|
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
if entry == nil || ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-17 11:19:26 +08:00
|
|
|
// get filecontent
|
|
|
|
data := wikiContentsByEntry(ctx, entry)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:20:22 +08:00
|
|
|
ctx.Data["content"] = string(data)
|
|
|
|
ctx.Data["sidebarPresent"] = false
|
|
|
|
ctx.Data["sidebarContent"] = ""
|
|
|
|
ctx.Data["footerPresent"] = false
|
|
|
|
ctx.Data["footerContent"] = ""
|
|
|
|
}
|
|
|
|
|
2021-11-17 02:18:25 +08:00
|
|
|
// WikiPost renders post of wiki page
|
|
|
|
func WikiPost(ctx *context.Context) {
|
|
|
|
switch ctx.FormString("action") {
|
|
|
|
case "_new":
|
|
|
|
if !ctx.Repo.CanWrite(unit.TypeWiki) {
|
|
|
|
ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
NewWikiPost(ctx)
|
|
|
|
return
|
|
|
|
case "_delete":
|
|
|
|
if !ctx.Repo.CanWrite(unit.TypeWiki) {
|
|
|
|
ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DeleteWikiPagePost(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.Repo.CanWrite(unit.TypeWiki) {
|
|
|
|
ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
EditWikiPost(ctx)
|
|
|
|
}
|
|
|
|
|
2017-02-14 09:13:59 +08:00
|
|
|
// Wiki renders single wiki page
|
2016-03-12 00:56:52 +08:00
|
|
|
func Wiki(ctx *context.Context) {
|
2021-11-10 03:57:58 +08:00
|
|
|
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
|
2015-11-27 14:50:38 +08:00
|
|
|
|
2021-11-17 02:18:25 +08:00
|
|
|
switch ctx.FormString("action") {
|
|
|
|
case "_pages":
|
|
|
|
WikiPages(ctx)
|
|
|
|
return
|
|
|
|
case "_revision":
|
|
|
|
WikiRevision(ctx)
|
|
|
|
return
|
|
|
|
case "_edit":
|
|
|
|
if !ctx.Repo.CanWrite(unit.TypeWiki) {
|
|
|
|
ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
EditWiki(ctx)
|
|
|
|
return
|
|
|
|
case "_new":
|
|
|
|
if !ctx.Repo.CanWrite(unit.TypeWiki) {
|
|
|
|
ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
NewWiki(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-04 03:46:11 +08:00
|
|
|
if !ctx.Repo.Repository.HasWiki() {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki")
|
|
|
|
ctx.HTML(http.StatusOK, tplWikiStart)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:20:22 +08:00
|
|
|
wikiRepo, entry := renderViewPage(ctx)
|
2019-11-13 15:01:19 +08:00
|
|
|
defer func() {
|
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
|
|
|
}()
|
2021-08-31 04:50:35 +08:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2017-03-20 21:36:19 +08:00
|
|
|
if entry == nil {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki")
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplWikiStart)
|
2017-03-20 21:36:19 +08:00
|
|
|
return
|
|
|
|
}
|
2015-11-27 13:24:24 +08:00
|
|
|
|
2017-11-28 17:43:51 +08:00
|
|
|
wikiPath := entry.Name()
|
2024-06-19 11:23:24 +08:00
|
|
|
if markup.DetectMarkupTypeByFileName(wikiPath) != markdown.MarkupName {
|
2017-11-28 17:43:51 +08:00
|
|
|
ext := strings.ToUpper(filepath.Ext(wikiPath))
|
2017-02-14 09:13:59 +08:00
|
|
|
ctx.Data["FormatWarning"] = fmt.Sprintf("%s rendering is not supported at the moment. Rendered as Markdown.", ext)
|
|
|
|
}
|
2015-11-27 13:24:24 +08:00
|
|
|
// Get last change information.
|
2017-11-28 17:43:51 +08:00
|
|
|
lastCommit, err := wikiRepo.GetCommitByPath(wikiPath)
|
2015-11-27 13:24:24 +08:00
|
|
|
if err != nil {
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.ServerError("GetCommitByPath", err)
|
2015-11-27 13:24:24 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Author"] = lastCommit.Author
|
|
|
|
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplWikiView)
|
2015-11-26 09:10:25 +08:00
|
|
|
}
|
|
|
|
|
2019-07-08 16:20:22 +08:00
|
|
|
// WikiRevision renders file revision list of wiki page
|
|
|
|
func WikiRevision(ctx *context.Context) {
|
2021-11-10 03:57:58 +08:00
|
|
|
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
|
2019-07-08 16:20:22 +08:00
|
|
|
|
|
|
|
if !ctx.Repo.Repository.HasWiki() {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki")
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplWikiStart)
|
2019-07-08 16:20:22 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wikiRepo, entry := renderRevisionPage(ctx)
|
2019-11-13 15:01:19 +08:00
|
|
|
defer func() {
|
|
|
|
if wikiRepo != nil {
|
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
|
|
|
}()
|
2021-08-31 04:50:35 +08:00
|
|
|
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2019-07-08 16:20:22 +08:00
|
|
|
if entry == nil {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki")
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplWikiStart)
|
2019-07-08 16:20:22 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get last change information.
|
|
|
|
wikiPath := entry.Name()
|
|
|
|
lastCommit, err := wikiRepo.GetCommitByPath(wikiPath)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCommitByPath", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Author"] = lastCommit.Author
|
|
|
|
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplWikiRevision)
|
2019-07-08 16:20:22 +08:00
|
|
|
}
|
|
|
|
|
2016-11-21 18:03:42 +08:00
|
|
|
// WikiPages render wiki pages list page
|
2016-03-12 00:56:52 +08:00
|
|
|
func WikiPages(ctx *context.Context) {
|
2015-11-27 15:16:12 +08:00
|
|
|
if !ctx.Repo.Repository.HasWiki() {
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-28 19:26:14 +08:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
|
2021-11-10 03:57:58 +08:00
|
|
|
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
|
2018-11-28 19:26:14 +08:00
|
|
|
|
2017-02-14 09:13:59 +08:00
|
|
|
wikiRepo, commit, err := findWikiRepoCommit(ctx)
|
2021-08-31 04:50:35 +08:00
|
|
|
defer func() {
|
2019-11-13 15:01:19 +08:00
|
|
|
if wikiRepo != nil {
|
2024-03-06 14:26:32 +08:00
|
|
|
_ = wikiRepo.Close()
|
2019-11-13 15:01:19 +08:00
|
|
|
}
|
2021-08-31 04:50:35 +08:00
|
|
|
}()
|
2024-03-06 14:26:32 +08:00
|
|
|
if err != nil {
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
|
|
|
|
return
|
|
|
|
}
|
2019-11-13 15:01:19 +08:00
|
|
|
|
2024-12-07 02:54:29 +08:00
|
|
|
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()
|
2021-08-31 04:50:35 +08:00
|
|
|
if err != nil {
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.ServerError("ListEntries", err)
|
2015-11-27 15:16:12 +08:00
|
|
|
return
|
|
|
|
}
|
2024-12-07 02:54:29 +08:00
|
|
|
allEntries.CustomSort(base.NaturalSortLess)
|
|
|
|
|
|
|
|
entries, _, err := allEntries.GetCommitsInfo(gocontext.Context(ctx), commit, treePath)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCommitsInfo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-27 15:16:12 +08:00
|
|
|
pages := make([]PageMeta, 0, len(entries))
|
2017-11-28 17:43:51 +08:00
|
|
|
for _, entry := range entries {
|
2024-12-07 02:54:29 +08:00
|
|
|
if !entry.Entry.IsRegular() {
|
2017-11-28 17:43:51 +08:00
|
|
|
continue
|
|
|
|
}
|
2024-12-07 02:54:29 +08:00
|
|
|
wikiName, err := wiki_service.GitPathToWebPath(entry.Entry.Name())
|
2017-11-28 17:43:51 +08:00
|
|
|
if err != nil {
|
2022-08-25 10:31:57 +08:00
|
|
|
if repo_model.IsErrWikiInvalidFileName(err) {
|
2018-02-05 22:56:30 +08:00
|
|
|
continue
|
|
|
|
}
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.ServerError("WikiFilenameToName", err)
|
2017-11-28 17:43:51 +08:00
|
|
|
return
|
2015-11-27 15:16:12 +08:00
|
|
|
}
|
2023-04-20 01:50:10 +08:00
|
|
|
_, displayName := wiki_service.WebPathToUserTitle(wikiName)
|
2017-11-28 17:43:51 +08:00
|
|
|
pages = append(pages, PageMeta{
|
2023-04-20 01:50:10 +08:00
|
|
|
Name: displayName,
|
|
|
|
SubURL: wiki_service.WebPathToURLPath(wikiName),
|
2024-12-07 02:54:29 +08:00
|
|
|
GitEntryName: entry.Entry.Name(),
|
|
|
|
UpdatedUnix: timeutil.TimeStamp(entry.Commit.Author.When.Unix()),
|
2017-11-28 17:43:51 +08:00
|
|
|
})
|
2015-11-27 15:16:12 +08:00
|
|
|
}
|
|
|
|
ctx.Data["Pages"] = pages
|
2015-11-27 13:24:24 +08:00
|
|
|
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplWikiPages)
|
2015-11-27 13:24:24 +08:00
|
|
|
}
|
|
|
|
|
2017-02-14 09:13:59 +08:00
|
|
|
// WikiRaw outputs raw blob requested by user (image for example)
|
|
|
|
func WikiRaw(ctx *context.Context) {
|
|
|
|
wikiRepo, commit, err := findWikiRepoCommit(ctx)
|
2021-08-31 04:50:35 +08:00
|
|
|
defer func() {
|
2017-02-14 09:13:59 +08:00
|
|
|
if wikiRepo != nil {
|
2021-08-31 04:50:35 +08:00
|
|
|
wikiRepo.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if git.IsErrNotExist(err) {
|
|
|
|
ctx.NotFound("findEntryForFile", nil)
|
2017-02-14 09:13:59 +08:00
|
|
|
return
|
|
|
|
}
|
2021-08-31 04:50:35 +08:00
|
|
|
ctx.ServerError("findEntryForfile", err)
|
|
|
|
return
|
2017-02-14 09:13:59 +08:00
|
|
|
}
|
2019-02-06 09:58:55 +08:00
|
|
|
|
2023-08-09 14:57:45 +08:00
|
|
|
providedWebPath := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*"))
|
2023-04-20 01:50:10 +08:00
|
|
|
providedGitPath := wiki_service.WebPathToGitPath(providedWebPath)
|
2017-02-14 09:13:59 +08:00
|
|
|
var entry *git.TreeEntry
|
|
|
|
if commit != nil {
|
2019-02-06 09:58:55 +08:00
|
|
|
// Try to find a file with that name
|
2023-04-20 01:50:10 +08:00
|
|
|
entry, err = findEntryForFile(commit, providedGitPath)
|
2020-01-28 17:44:08 +08:00
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
2019-02-06 09:58:55 +08:00
|
|
|
ctx.ServerError("findFile", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry == nil {
|
|
|
|
// Try to find a wiki page with that name
|
2023-04-20 01:50:10 +08:00
|
|
|
providedGitPath = strings.TrimSuffix(providedGitPath, ".md")
|
|
|
|
entry, err = findEntryForFile(commit, providedGitPath)
|
2020-01-28 17:44:08 +08:00
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
2019-02-06 09:58:55 +08:00
|
|
|
ctx.ServerError("findFile", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-02-14 09:13:59 +08:00
|
|
|
}
|
2019-02-06 09:58:55 +08:00
|
|
|
|
|
|
|
if entry != nil {
|
2023-07-07 13:31:56 +08:00
|
|
|
if err = common.ServeBlob(ctx.Base, ctx.Repo.TreePath, entry.Blob(), nil); err != nil {
|
2019-02-06 09:58:55 +08:00
|
|
|
ctx.ServerError("ServeBlob", err)
|
|
|
|
}
|
2017-11-28 17:43:51 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:58:55 +08:00
|
|
|
ctx.NotFound("findEntryForFile", nil)
|
2017-02-14 09:13:59 +08:00
|
|
|
}
|
|
|
|
|
2016-11-21 18:03:42 +08:00
|
|
|
// NewWiki render wiki create page
|
2016-03-12 00:56:52 +08:00
|
|
|
func NewWiki(ctx *context.Context) {
|
2015-11-26 09:10:25 +08:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
|
|
|
|
|
2015-11-27 06:33:45 +08:00
|
|
|
if !ctx.Repo.Repository.HasWiki() {
|
2015-11-26 09:10:25 +08:00
|
|
|
ctx.Data["title"] = "Home"
|
|
|
|
}
|
2021-12-03 15:28:54 +08:00
|
|
|
if ctx.FormString("title") != "" {
|
|
|
|
ctx.Data["title"] = ctx.FormString("title")
|
|
|
|
}
|
2015-11-26 09:10:25 +08:00
|
|
|
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplWikiNew)
|
2015-11-26 09:10:25 +08:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:43:51 +08:00
|
|
|
// NewWikiPost response for wiki create request
|
2021-01-26 23:36:53 +08:00
|
|
|
func NewWikiPost(ctx *context.Context) {
|
2021-04-07 03:44:05 +08:00
|
|
|
form := web.GetForm(ctx).(*forms.NewWikiForm)
|
2015-11-27 06:33:45 +08:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplWikiNew)
|
2015-11-27 06:33:45 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-21 19:45:32 +08:00
|
|
|
if util.IsEmptyString(form.Title) {
|
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplWikiNew, form)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-20 01:50:10 +08:00
|
|
|
wikiName := wiki_service.UserTitleToWebPath("", form.Title)
|
2020-05-22 21:14:02 +08:00
|
|
|
|
|
|
|
if len(form.Message) == 0 {
|
2024-02-15 05:48:45 +08:00
|
|
|
form.Message = ctx.Locale.TrString("repo.editor.add", form.Title)
|
2020-05-22 21:14:02 +08:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:03:22 +08:00
|
|
|
if err := wiki_service.AddWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, wikiName, form.Content, form.Message); err != nil {
|
2022-08-25 10:31:57 +08:00
|
|
|
if repo_model.IsErrWikiReservedName(err) {
|
2017-11-28 17:43:51 +08:00
|
|
|
ctx.Data["Err_Title"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.wiki.reserved_page", wikiName), tplWikiNew, &form)
|
2022-08-25 10:31:57 +08:00
|
|
|
} else if repo_model.IsErrWikiAlreadyExist(err) {
|
2015-11-27 14:50:38 +08:00
|
|
|
ctx.Data["Err_Title"] = true
|
2016-11-21 18:03:42 +08:00
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.wiki.page_already_exists"), tplWikiNew, &form)
|
2015-11-27 14:50:38 +08:00
|
|
|
} else {
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.ServerError("AddWikiPage", err)
|
2015-11-27 14:50:38 +08:00
|
|
|
}
|
2015-11-27 06:33:45 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.NewWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, string(wikiName), form.Message)
|
2022-09-05 03:54:23 +08:00
|
|
|
|
2023-04-20 01:50:10 +08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + wiki_service.WebPathToURLPath(wikiName))
|
2015-11-27 06:33:45 +08:00
|
|
|
}
|
|
|
|
|
2016-11-21 18:03:42 +08:00
|
|
|
// EditWiki render wiki modify page
|
2016-03-12 00:56:52 +08:00
|
|
|
func EditWiki(ctx *context.Context) {
|
2015-11-27 14:50:38 +08:00
|
|
|
ctx.Data["PageIsWikiEdit"] = true
|
|
|
|
|
|
|
|
if !ctx.Repo.Repository.HasWiki() {
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:20:22 +08:00
|
|
|
renderEditPage(ctx)
|
2015-11-27 14:50:38 +08:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplWikiNew)
|
2015-11-27 14:50:38 +08:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:43:51 +08:00
|
|
|
// EditWikiPost response for wiki modify request
|
2021-01-26 23:36:53 +08:00
|
|
|
func EditWikiPost(ctx *context.Context) {
|
2021-04-07 03:44:05 +08:00
|
|
|
form := web.GetForm(ctx).(*forms.NewWikiForm)
|
2015-11-27 14:50:38 +08:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplWikiNew)
|
2015-11-27 14:50:38 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-09 14:57:45 +08:00
|
|
|
oldWikiName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*"))
|
2023-04-20 01:50:10 +08:00
|
|
|
newWikiName := wiki_service.UserTitleToWebPath("", form.Title)
|
2017-01-21 20:50:51 +08:00
|
|
|
|
2020-05-22 21:14:02 +08:00
|
|
|
if len(form.Message) == 0 {
|
2024-02-15 05:48:45 +08:00
|
|
|
form.Message = ctx.Locale.TrString("repo.editor.update", form.Title)
|
2020-05-22 21:14:02 +08:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:03:22 +08:00
|
|
|
if err := wiki_service.EditWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, oldWikiName, newWikiName, form.Content, form.Message); err != nil {
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.ServerError("EditWikiPage", err)
|
2015-11-27 14:50:38 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.EditWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, string(newWikiName), form.Message)
|
2022-09-05 03:54:23 +08:00
|
|
|
|
2023-04-20 01:50:10 +08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + wiki_service.WebPathToURLPath(newWikiName))
|
2015-11-26 09:10:25 +08:00
|
|
|
}
|
2016-03-04 06:06:50 +08:00
|
|
|
|
2016-11-21 18:03:42 +08:00
|
|
|
// DeleteWikiPagePost delete wiki page
|
2016-03-12 00:56:52 +08:00
|
|
|
func DeleteWikiPagePost(ctx *context.Context) {
|
2023-08-09 14:57:45 +08:00
|
|
|
wikiName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*"))
|
2017-11-28 17:43:51 +08:00
|
|
|
if len(wikiName) == 0 {
|
|
|
|
wikiName = "Home"
|
2016-03-04 06:06:50 +08:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:03:22 +08:00
|
|
|
if err := wiki_service.DeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, wikiName); err != nil {
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.ServerError("DeleteWikiPage", err)
|
2016-03-04 06:06:50 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.DeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, string(wikiName))
|
2022-09-05 03:54:23 +08:00
|
|
|
|
2023-07-26 14:04:01 +08:00
|
|
|
ctx.JSONRedirect(ctx.Repo.RepoLink + "/wiki/")
|
2016-03-04 06:06:50 +08:00
|
|
|
}
|