2015-11-26 10:10:25 +09:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2015-11-27 14:24:24 +09:00
|
|
|
"io/ioutil"
|
2015-11-27 15:50:38 +09:00
|
|
|
"strings"
|
2015-11-27 16:16:12 +09:00
|
|
|
"time"
|
2015-11-27 14:24:24 +09:00
|
|
|
|
2015-12-16 07:25:45 +09:00
|
|
|
"github.com/gogits/git-module"
|
2015-11-27 14:24:24 +09:00
|
|
|
|
2016-11-03 21:29:56 +09:00
|
|
|
"github.com/go-gitea/gitea/models"
|
|
|
|
"github.com/go-gitea/gitea/modules/auth"
|
|
|
|
"github.com/go-gitea/gitea/modules/base"
|
|
|
|
"github.com/go-gitea/gitea/modules/context"
|
|
|
|
"github.com/go-gitea/gitea/modules/markdown"
|
2015-11-26 10:10:25 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
WIKI_START base.TplName = "repo/wiki/start"
|
|
|
|
WIKI_VIEW base.TplName = "repo/wiki/view"
|
|
|
|
WIKI_NEW base.TplName = "repo/wiki/new"
|
2015-11-27 16:16:12 +09:00
|
|
|
WIKI_PAGES base.TplName = "repo/wiki/pages"
|
2015-11-26 10:10:25 +09:00
|
|
|
)
|
|
|
|
|
2016-03-12 01:56:52 +09:00
|
|
|
func MustEnableWiki(ctx *context.Context) {
|
2015-12-05 11:30:33 +09:00
|
|
|
if !ctx.Repo.Repository.EnableWiki {
|
|
|
|
ctx.Handle(404, "MustEnableWiki", nil)
|
2015-12-11 18:55:08 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Repo.Repository.EnableExternalWiki {
|
|
|
|
ctx.Redirect(ctx.Repo.Repository.ExternalWikiURL)
|
|
|
|
return
|
2015-12-05 11:30:33 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 15:50:38 +09:00
|
|
|
type PageMeta struct {
|
2015-11-27 16:16:12 +09:00
|
|
|
Name string
|
|
|
|
URL string
|
|
|
|
Updated time.Time
|
2015-11-27 15:50:38 +09:00
|
|
|
}
|
2015-11-26 10:10:25 +09:00
|
|
|
|
2016-03-12 01:56:52 +09:00
|
|
|
func renderWikiPage(ctx *context.Context, isViewPage bool) (*git.Repository, string) {
|
2015-11-27 14:24:24 +09:00
|
|
|
wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
|
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, "OpenRepository", err)
|
2015-11-27 15:50:38 +09:00
|
|
|
return nil, ""
|
2015-11-27 14:24:24 +09:00
|
|
|
}
|
2015-12-10 10:46:05 +09:00
|
|
|
commit, err := wikiRepo.GetBranchCommit("master")
|
2015-11-27 14:24:24 +09:00
|
|
|
if err != nil {
|
2015-12-10 10:46:05 +09:00
|
|
|
ctx.Handle(500, "GetBranchCommit", err)
|
2015-11-27 15:50:38 +09:00
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get page list.
|
|
|
|
if isViewPage {
|
|
|
|
entries, err := commit.ListEntries()
|
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, "ListEntries", err)
|
|
|
|
return nil, ""
|
|
|
|
}
|
2015-11-27 16:16:12 +09:00
|
|
|
pages := make([]PageMeta, 0, len(entries))
|
2015-11-27 15:50:38 +09:00
|
|
|
for i := range entries {
|
2016-07-01 16:34:03 +09:00
|
|
|
if entries[i].Type == git.OBJECT_BLOB && strings.HasSuffix(entries[i].Name(), ".md") {
|
2015-11-27 16:16:12 +09:00
|
|
|
name := strings.TrimSuffix(entries[i].Name(), ".md")
|
|
|
|
pages = append(pages, PageMeta{
|
|
|
|
Name: name,
|
|
|
|
URL: models.ToWikiPageURL(name),
|
|
|
|
})
|
2015-11-27 15:50:38 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Data["Pages"] = pages
|
2015-11-27 14:24:24 +09:00
|
|
|
}
|
|
|
|
|
2015-11-27 15:50:38 +09:00
|
|
|
pageURL := ctx.Params(":page")
|
|
|
|
if len(pageURL) == 0 {
|
|
|
|
pageURL = "Home"
|
2015-11-27 14:24:24 +09:00
|
|
|
}
|
2015-11-27 15:50:38 +09:00
|
|
|
ctx.Data["PageURL"] = pageURL
|
|
|
|
|
|
|
|
pageName := models.ToWikiPageName(pageURL)
|
|
|
|
ctx.Data["old_title"] = pageName
|
|
|
|
ctx.Data["Title"] = pageName
|
|
|
|
ctx.Data["title"] = pageName
|
2015-11-27 14:24:24 +09:00
|
|
|
ctx.Data["RequireHighlightJS"] = true
|
|
|
|
|
2015-11-27 15:50:38 +09:00
|
|
|
blob, err := commit.GetBlobByPath(pageName + ".md")
|
2015-11-27 14:24:24 +09:00
|
|
|
if err != nil {
|
|
|
|
if git.IsErrNotExist(err) {
|
2015-11-27 15:50:38 +09:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
|
2015-11-27 14:24:24 +09:00
|
|
|
} else {
|
|
|
|
ctx.Handle(500, "GetBlobByPath", err)
|
|
|
|
}
|
2015-11-27 15:50:38 +09:00
|
|
|
return nil, ""
|
2015-11-27 14:24:24 +09:00
|
|
|
}
|
|
|
|
r, err := blob.Data()
|
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, "Data", err)
|
2015-11-27 15:50:38 +09:00
|
|
|
return nil, ""
|
2015-11-27 14:24:24 +09:00
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, "ReadAll", err)
|
2015-11-27 15:50:38 +09:00
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
if isViewPage {
|
2016-02-21 07:10:05 +09:00
|
|
|
ctx.Data["content"] = string(markdown.Render(data, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()))
|
2015-11-27 15:50:38 +09:00
|
|
|
} else {
|
|
|
|
ctx.Data["content"] = string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wikiRepo, pageName
|
|
|
|
}
|
|
|
|
|
2016-03-12 01:56:52 +09:00
|
|
|
func Wiki(ctx *context.Context) {
|
2015-11-27 15:50:38 +09:00
|
|
|
ctx.Data["PageIsWiki"] = true
|
|
|
|
|
|
|
|
if !ctx.Repo.Repository.HasWiki() {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki")
|
|
|
|
ctx.HTML(200, WIKI_START)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wikiRepo, pageName := renderWikiPage(ctx, true)
|
|
|
|
if ctx.Written() {
|
2015-11-27 14:24:24 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get last change information.
|
2015-11-27 15:50:38 +09:00
|
|
|
lastCommit, err := wikiRepo.GetCommitByPath(pageName + ".md")
|
2015-11-27 14:24:24 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, "GetCommitByPath", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Author"] = lastCommit.Author
|
|
|
|
|
2015-11-26 10:10:25 +09:00
|
|
|
ctx.HTML(200, WIKI_VIEW)
|
|
|
|
}
|
|
|
|
|
2016-03-12 01:56:52 +09:00
|
|
|
func WikiPages(ctx *context.Context) {
|
2015-11-27 16:16:12 +09:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
|
|
|
|
ctx.Data["PageIsWiki"] = true
|
|
|
|
|
|
|
|
if !ctx.Repo.Repository.HasWiki() {
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
|
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, "OpenRepository", err)
|
|
|
|
return
|
|
|
|
}
|
2015-12-10 10:46:05 +09:00
|
|
|
commit, err := wikiRepo.GetBranchCommit("master")
|
2015-11-27 16:16:12 +09:00
|
|
|
if err != nil {
|
2015-12-10 10:46:05 +09:00
|
|
|
ctx.Handle(500, "GetBranchCommit", err)
|
2015-11-27 16:16:12 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := commit.ListEntries()
|
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, "ListEntries", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pages := make([]PageMeta, 0, len(entries))
|
|
|
|
for i := range entries {
|
2016-07-01 16:34:03 +09:00
|
|
|
if entries[i].Type == git.OBJECT_BLOB && strings.HasSuffix(entries[i].Name(), ".md") {
|
2015-11-27 16:16:12 +09:00
|
|
|
c, err := wikiRepo.GetCommitByPath(entries[i].Name())
|
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, "GetCommit", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := strings.TrimSuffix(entries[i].Name(), ".md")
|
|
|
|
pages = append(pages, PageMeta{
|
|
|
|
Name: name,
|
|
|
|
URL: models.ToWikiPageURL(name),
|
|
|
|
Updated: c.Author.When,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Data["Pages"] = pages
|
2015-11-27 14:24:24 +09:00
|
|
|
|
2015-11-27 16:16:12 +09:00
|
|
|
ctx.HTML(200, WIKI_PAGES)
|
2015-11-27 14:24:24 +09:00
|
|
|
}
|
|
|
|
|
2016-03-12 01:56:52 +09:00
|
|
|
func NewWiki(ctx *context.Context) {
|
2015-11-26 10:10:25 +09:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
|
|
|
|
ctx.Data["PageIsWiki"] = true
|
|
|
|
ctx.Data["RequireSimpleMDE"] = true
|
|
|
|
|
2015-11-27 07:33:45 +09:00
|
|
|
if !ctx.Repo.Repository.HasWiki() {
|
2015-11-26 10:10:25 +09:00
|
|
|
ctx.Data["title"] = "Home"
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.HTML(200, WIKI_NEW)
|
|
|
|
}
|
|
|
|
|
2016-03-12 01:56:52 +09:00
|
|
|
func NewWikiPost(ctx *context.Context, form auth.NewWikiForm) {
|
2015-11-27 07:33:45 +09:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
|
|
|
|
ctx.Data["PageIsWiki"] = true
|
|
|
|
ctx.Data["RequireSimpleMDE"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, WIKI_NEW)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-27 14:24:24 +09:00
|
|
|
if err := ctx.Repo.Repository.AddWikiPage(ctx.User, form.Title, form.Content, form.Message); err != nil {
|
2015-11-27 15:50:38 +09:00
|
|
|
if models.IsErrWikiAlreadyExist(err) {
|
|
|
|
ctx.Data["Err_Title"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.wiki.page_already_exists"), WIKI_NEW, &form)
|
|
|
|
} else {
|
|
|
|
ctx.Handle(500, "AddWikiPage", err)
|
|
|
|
}
|
2015-11-27 07:33:45 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-27 14:24:24 +09:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title))
|
2015-11-27 07:33:45 +09:00
|
|
|
}
|
|
|
|
|
2016-03-12 01:56:52 +09:00
|
|
|
func EditWiki(ctx *context.Context) {
|
2015-11-27 15:50:38 +09:00
|
|
|
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, WIKI_NEW)
|
|
|
|
}
|
|
|
|
|
2016-03-12 01:56:52 +09:00
|
|
|
func EditWikiPost(ctx *context.Context, form auth.NewWikiForm) {
|
2015-11-27 15:50:38 +09:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
|
|
|
|
ctx.Data["PageIsWiki"] = true
|
|
|
|
ctx.Data["RequireSimpleMDE"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, WIKI_NEW)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.Repo.Repository.EditWikiPage(ctx.User, form.OldTitle, form.Title, form.Content, form.Message); err != nil {
|
|
|
|
ctx.Handle(500, "EditWikiPage", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title))
|
2015-11-26 10:10:25 +09:00
|
|
|
}
|
2016-03-04 07:06:50 +09:00
|
|
|
|
2016-03-12 01:56:52 +09:00
|
|
|
func DeleteWikiPagePost(ctx *context.Context) {
|
2016-03-04 07:06:50 +09:00
|
|
|
pageURL := ctx.Params(":page")
|
|
|
|
if len(pageURL) == 0 {
|
|
|
|
pageURL = "Home"
|
|
|
|
}
|
|
|
|
|
|
|
|
pageName := models.ToWikiPageName(pageURL)
|
|
|
|
if err := ctx.Repo.Repository.DeleteWikiPage(ctx.User, pageName); err != nil {
|
|
|
|
ctx.Handle(500, "DeleteWikiPage", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(200, map[string]interface{}{
|
|
|
|
"redirect": ctx.Repo.RepoLink + "/wiki/",
|
|
|
|
})
|
|
|
|
}
|