forgejo-federation/routers/repo/wiki.go

437 lines
11 KiB
Go
Raw Normal View History

2015-11-26 06:40:25 +05:30
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2018 The Gitea Authors. All rights reserved.
2015-11-26 06:40:25 +05:30
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package repo
import (
"fmt"
2015-11-27 10:54:24 +05:30
"io/ioutil"
"path/filepath"
2015-11-27 12:20:38 +05:30
"strings"
2015-11-27 10:54:24 +05:30
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/util"
2015-11-26 06:40:25 +05:30
)
const (
tplWikiStart base.TplName = "repo/wiki/start"
tplWikiView base.TplName = "repo/wiki/view"
tplWikiNew base.TplName = "repo/wiki/new"
tplWikiPages base.TplName = "repo/wiki/pages"
2015-11-26 06:40:25 +05:30
)
// MustEnableWiki check if wiki is enabled, if external then redirect
2016-03-11 22:26:52 +05:30
func MustEnableWiki(ctx *context.Context) {
if !ctx.Repo.CanRead(models.UnitTypeWiki) &&
!ctx.Repo.CanRead(models.UnitTypeExternalWiki) {
ctx.NotFound("MustEnableWiki", nil)
2015-12-11 15:25:08 +05:30
return
}
unit, err := ctx.Repo.Repository.GetUnit(models.UnitTypeExternalWiki)
if err == nil {
ctx.Redirect(unit.ExternalWikiConfig().ExternalWikiURL)
2015-12-11 15:25:08 +05:30
return
2015-12-05 08:00:33 +05:30
}
}
// PageMeta wiki page meat information
2015-11-27 12:20:38 +05:30
type PageMeta struct {
Name string
SubURL string
UpdatedUnix util.TimeStamp
2015-11-27 12:20:38 +05:30
}
2015-11-26 06:40:25 +05:30
// findEntryForFile finds the tree entry for a target filepath.
func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error) {
entries, err := commit.ListEntries()
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.Type == git.ObjectBlob && entry.Name() == target {
return entry, nil
}
}
return nil, nil
}
func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
2015-11-27 10:54:24 +05:30
wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return nil, nil, err
2015-11-27 10:54:24 +05:30
}
2017-03-20 19:06:19 +05:30
commit, err := wikiRepo.GetBranchCommit("master")
2015-11-27 10:54:24 +05:30
if err != nil {
return wikiRepo, nil, err
}
return wikiRepo, commit, nil
}
// 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 {
reader, err := entry.Blob().Data()
if err != nil {
ctx.ServerError("Blob.Data", err)
return nil
}
content, err := ioutil.ReadAll(reader)
if err != nil {
ctx.ServerError("ReadAll", err)
return nil
}
return content
}
// 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 string) ([]byte, bool) {
entry, err := findEntryForFile(commit, models.WikiNameToFilename(wikiName))
if err != nil {
ctx.ServerError("findEntryForFile", err)
return nil, false
} else if entry == nil {
return nil, false
}
return wikiContentsByEntry(ctx, entry), true
}
func renderWikiPage(ctx *context.Context, isViewPage bool) (*git.Repository, *git.TreeEntry) {
wikiRepo, commit, err := findWikiRepoCommit(ctx)
if err != nil {
if !git.IsErrNotExist(err) {
ctx.ServerError("GetBranchCommit", err)
}
return nil, nil
2015-11-27 12:20:38 +05:30
}
// Get page list.
if isViewPage {
entries, err := commit.ListEntries()
if err != nil {
ctx.ServerError("ListEntries", err)
return nil, nil
2015-11-27 12:20:38 +05:30
}
pages := make([]PageMeta, 0, len(entries))
for _, entry := range entries {
if entry.Type != git.ObjectBlob {
continue
2015-11-27 12:20:38 +05:30
}
wikiName, err := models.WikiFilenameToName(entry.Name())
if err != nil {
if models.IsErrWikiInvalidFileName(err) {
continue
}
ctx.ServerError("WikiFilenameToName", err)
return nil, nil
} else if wikiName == "_Sidebar" || wikiName == "_Footer" {
continue
}
pages = append(pages, PageMeta{
Name: wikiName,
SubURL: models.WikiNameToSubURL(wikiName),
})
2015-11-27 12:20:38 +05:30
}
ctx.Data["Pages"] = pages
2015-11-27 10:54:24 +05:30
}
pageName := models.NormalizeWikiName(ctx.Params(":page"))
if len(pageName) == 0 {
pageName = "Home"
2015-11-27 10:54:24 +05:30
}
ctx.Data["PageURL"] = models.WikiNameToSubURL(pageName)
2015-11-27 12:20:38 +05:30
ctx.Data["old_title"] = pageName
ctx.Data["Title"] = pageName
ctx.Data["title"] = pageName
2015-11-27 10:54:24 +05:30
ctx.Data["RequireHighlightJS"] = true
pageFilename := models.WikiNameToFilename(pageName)
var entry *git.TreeEntry
if entry, err = findEntryForFile(commit, pageFilename); err != nil {
ctx.ServerError("findEntryForFile", err)
return nil, nil
} else if entry == nil {
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
return nil, nil
2015-11-27 10:54:24 +05:30
}
data := wikiContentsByEntry(ctx, entry)
if ctx.Written() {
return nil, nil
}
if isViewPage {
sidebarContent, sidebarPresent := wikiContentsByName(ctx, commit, "_Sidebar")
if ctx.Written() {
return nil, nil
}
footerContent, footerPresent := wikiContentsByName(ctx, commit, "_Footer")
if ctx.Written() {
return nil, nil
}
metas := ctx.Repo.Repository.ComposeMetas()
ctx.Data["content"] = markdown.RenderWiki(data, ctx.Repo.RepoLink, metas)
ctx.Data["sidebarPresent"] = sidebarPresent
ctx.Data["sidebarContent"] = markdown.RenderWiki(sidebarContent, ctx.Repo.RepoLink, metas)
ctx.Data["footerPresent"] = footerPresent
ctx.Data["footerContent"] = markdown.RenderWiki(footerContent, ctx.Repo.RepoLink, metas)
2015-11-27 12:20:38 +05:30
} else {
ctx.Data["content"] = string(data)
ctx.Data["sidebarPresent"] = false
ctx.Data["sidebarContent"] = ""
ctx.Data["footerPresent"] = false
ctx.Data["footerContent"] = ""
2015-11-27 12:20:38 +05:30
}
return wikiRepo, entry
2015-11-27 12:20:38 +05:30
}
// Wiki renders single wiki page
2016-03-11 22:26:52 +05:30
func Wiki(ctx *context.Context) {
2015-11-27 12:20:38 +05:30
ctx.Data["PageIsWiki"] = true
2019-01-24 00:28:38 +05:30
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(models.UnitTypeWiki) && !ctx.Repo.Repository.IsArchived
2015-11-27 12:20:38 +05:30
if !ctx.Repo.Repository.HasWiki() {
ctx.Data["Title"] = ctx.Tr("repo.wiki")
ctx.HTML(200, tplWikiStart)
2015-11-27 12:20:38 +05:30
return
}
wikiRepo, entry := renderWikiPage(ctx, true)
2015-11-27 12:20:38 +05:30
if ctx.Written() {
2015-11-27 10:54:24 +05:30
return
}
2017-03-20 19:06:19 +05:30
if entry == nil {
ctx.Data["Title"] = ctx.Tr("repo.wiki")
ctx.HTML(200, tplWikiStart)
return
}
2015-11-27 10:54:24 +05:30
wikiPath := entry.Name()
if markup.Type(wikiPath) != markdown.MarkupName {
ext := strings.ToUpper(filepath.Ext(wikiPath))
ctx.Data["FormatWarning"] = fmt.Sprintf("%s rendering is not supported at the moment. Rendered as Markdown.", ext)
}
2015-11-27 10:54:24 +05:30
// Get last change information.
lastCommit, err := wikiRepo.GetCommitByPath(wikiPath)
2015-11-27 10:54:24 +05:30
if err != nil {
ctx.ServerError("GetCommitByPath", err)
2015-11-27 10:54:24 +05:30
return
}
ctx.Data["Author"] = lastCommit.Author
ctx.HTML(200, tplWikiView)
2015-11-26 06:40:25 +05:30
}
// WikiPages render wiki pages list page
2016-03-11 22:26:52 +05:30
func WikiPages(ctx *context.Context) {
2015-11-27 12:46:12 +05:30
if !ctx.Repo.Repository.HasWiki() {
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
return
}
ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
ctx.Data["PageIsWiki"] = true
2019-01-24 00:28:38 +05:30
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(models.UnitTypeWiki) && !ctx.Repo.Repository.IsArchived
wikiRepo, commit, err := findWikiRepoCommit(ctx)
2015-11-27 12:46:12 +05:30
if err != nil {
return
}
entries, err := commit.ListEntries()
if err != nil {
ctx.ServerError("ListEntries", err)
2015-11-27 12:46:12 +05:30
return
}
pages := make([]PageMeta, 0, len(entries))
for _, entry := range entries {
if entry.Type != git.ObjectBlob {
continue
}
c, err := wikiRepo.GetCommitByPath(entry.Name())
if err != nil {
ctx.ServerError("GetCommit", err)
return
}
wikiName, err := models.WikiFilenameToName(entry.Name())
if err != nil {
if models.IsErrWikiInvalidFileName(err) {
continue
}
ctx.ServerError("WikiFilenameToName", err)
return
2015-11-27 12:46:12 +05:30
}
pages = append(pages, PageMeta{
Name: wikiName,
SubURL: models.WikiNameToSubURL(wikiName),
UpdatedUnix: util.TimeStamp(c.Author.When.Unix()),
})
2015-11-27 12:46:12 +05:30
}
ctx.Data["Pages"] = pages
2015-11-27 10:54:24 +05:30
ctx.HTML(200, tplWikiPages)
2015-11-27 10:54:24 +05:30
}
// WikiRaw outputs raw blob requested by user (image for example)
func WikiRaw(ctx *context.Context) {
wikiRepo, commit, err := findWikiRepoCommit(ctx)
if err != nil {
if wikiRepo != nil {
return
}
}
providedPath := ctx.Params("*")
var entry *git.TreeEntry
if commit != nil {
// Try to find a file with that name
entry, err = findEntryForFile(commit, providedPath)
if err != nil {
ctx.ServerError("findFile", err)
return
}
if entry == nil {
// Try to find a wiki page with that name
if strings.HasSuffix(providedPath, ".md") {
providedPath = providedPath[:len(providedPath)-3]
}
wikiPath := models.WikiNameToFilename(providedPath)
entry, err = findEntryForFile(commit, wikiPath)
if err != nil {
ctx.ServerError("findFile", err)
return
}
}
}
if entry != nil {
if err = ServeBlob(ctx, entry.Blob()); err != nil {
ctx.ServerError("ServeBlob", err)
}
return
}
ctx.NotFound("findEntryForFile", nil)
}
// NewWiki render wiki create page
2016-03-11 22:26:52 +05:30
func NewWiki(ctx *context.Context) {
2015-11-26 06:40:25 +05:30
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
ctx.Data["PageIsWiki"] = true
ctx.Data["RequireSimpleMDE"] = true
2015-11-27 04:03:45 +05:30
if !ctx.Repo.Repository.HasWiki() {
2015-11-26 06:40:25 +05:30
ctx.Data["title"] = "Home"
}
ctx.HTML(200, tplWikiNew)
2015-11-26 06:40:25 +05:30
}
// NewWikiPost response for wiki create request
2016-03-11 22:26:52 +05:30
func NewWikiPost(ctx *context.Context, form auth.NewWikiForm) {
2015-11-27 04:03:45 +05:30
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
ctx.Data["PageIsWiki"] = true
ctx.Data["RequireSimpleMDE"] = true
if ctx.HasError() {
ctx.HTML(200, tplWikiNew)
2015-11-27 04:03:45 +05:30
return
}
if util.IsEmptyString(form.Title) {
ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplWikiNew, form)
return
}
wikiName := models.NormalizeWikiName(form.Title)
if err := ctx.Repo.Repository.AddWikiPage(ctx.User, wikiName, form.Content, form.Message); err != nil {
if models.IsErrWikiReservedName(err) {
ctx.Data["Err_Title"] = true
ctx.RenderWithErr(ctx.Tr("repo.wiki.reserved_page", wikiName), tplWikiNew, &form)
} else if models.IsErrWikiAlreadyExist(err) {
2015-11-27 12:20:38 +05:30
ctx.Data["Err_Title"] = true
ctx.RenderWithErr(ctx.Tr("repo.wiki.page_already_exists"), tplWikiNew, &form)
2015-11-27 12:20:38 +05:30
} else {
ctx.ServerError("AddWikiPage", err)
2015-11-27 12:20:38 +05:30
}
2015-11-27 04:03:45 +05:30
return
}
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.WikiNameToSubURL(wikiName))
2015-11-27 04:03:45 +05:30
}
// EditWiki render wiki modify page
2016-03-11 22:26:52 +05:30
func EditWiki(ctx *context.Context) {
2015-11-27 12:20:38 +05:30
ctx.Data["PageIsWiki"] = true
ctx.Data["PageIsWikiEdit"] = true
ctx.Data["RequireSimpleMDE"] = true
if !ctx.Repo.Repository.HasWiki() {
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
return
}
renderWikiPage(ctx, false)
if ctx.Written() {
return
}
ctx.HTML(200, tplWikiNew)
2015-11-27 12:20:38 +05:30
}
// EditWikiPost response for wiki modify request
2016-03-11 22:26:52 +05:30
func EditWikiPost(ctx *context.Context, form auth.NewWikiForm) {
2015-11-27 12:20:38 +05:30
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
ctx.Data["PageIsWiki"] = true
ctx.Data["RequireSimpleMDE"] = true
if ctx.HasError() {
ctx.HTML(200, tplWikiNew)
2015-11-27 12:20:38 +05:30
return
}
oldWikiName := models.NormalizeWikiName(ctx.Params(":page"))
newWikiName := models.NormalizeWikiName(form.Title)
if err := ctx.Repo.Repository.EditWikiPage(ctx.User, oldWikiName, newWikiName, form.Content, form.Message); err != nil {
ctx.ServerError("EditWikiPage", err)
2015-11-27 12:20:38 +05:30
return
}
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.WikiNameToSubURL(newWikiName))
2015-11-26 06:40:25 +05:30
}
// DeleteWikiPagePost delete wiki page
2016-03-11 22:26:52 +05:30
func DeleteWikiPagePost(ctx *context.Context) {
wikiName := models.NormalizeWikiName(ctx.Params(":page"))
if len(wikiName) == 0 {
wikiName = "Home"
}
if err := ctx.Repo.Repository.DeleteWikiPage(ctx.User, wikiName); err != nil {
ctx.ServerError("DeleteWikiPage", err)
return
}
ctx.JSON(200, map[string]interface{}{
"redirect": ctx.Repo.RepoLink + "/wiki/",
})
}