123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- // 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 (
- "strings"
- "time"
- "github.com/gogs/git-module"
- "gogs.io/gogs/internal/context"
- "gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/form"
- "gogs.io/gogs/internal/gitutil"
- "gogs.io/gogs/internal/markup"
- )
- const (
- WIKI_START = "repo/wiki/start"
- WIKI_VIEW = "repo/wiki/view"
- WIKI_NEW = "repo/wiki/new"
- WIKI_PAGES = "repo/wiki/pages"
- )
- func MustEnableWiki(c *context.Context) {
- if !c.Repo.Repository.EnableWiki {
- c.NotFound()
- return
- }
- if c.Repo.Repository.EnableExternalWiki {
- c.Redirect(c.Repo.Repository.ExternalWikiURL)
- return
- }
- }
- type PageMeta struct {
- Name string
- URL string
- Updated time.Time
- }
- func renderWikiPage(c *context.Context, isViewPage bool) (*git.Repository, string) {
- wikiRepo, err := git.Open(c.Repo.Repository.WikiPath())
- if err != nil {
- c.Error(err, "open repository")
- return nil, ""
- }
- commit, err := wikiRepo.BranchCommit("master")
- if err != nil {
- c.Error(err, "get branch commit")
- return nil, ""
- }
- // Get page list.
- if isViewPage {
- entries, err := commit.Entries()
- if err != nil {
- c.Error(err, "list entries")
- return nil, ""
- }
- pages := make([]PageMeta, 0, len(entries))
- for i := range entries {
- if entries[i].Type() == git.ObjectBlob && strings.HasSuffix(entries[i].Name(), ".md") {
- name := strings.TrimSuffix(entries[i].Name(), ".md")
- pages = append(pages, PageMeta{
- Name: name,
- URL: db.ToWikiPageURL(name),
- })
- }
- }
- c.Data["Pages"] = pages
- }
- pageURL := c.Params(":page")
- if len(pageURL) == 0 {
- pageURL = "Home"
- }
- c.Data["PageURL"] = pageURL
- pageName := db.ToWikiPageName(pageURL)
- c.Data["old_title"] = pageName
- c.Data["Title"] = pageName
- c.Data["title"] = pageName
- c.Data["RequireHighlightJS"] = true
- blob, err := commit.Blob(pageName + ".md")
- if err != nil {
- if gitutil.IsErrRevisionNotExist(err) {
- c.Redirect(c.Repo.RepoLink + "/wiki/_pages")
- } else {
- c.Error(err, "get blob")
- }
- return nil, ""
- }
- p, err := blob.Bytes()
- if err != nil {
- c.Error(err, "read blob")
- return nil, ""
- }
- if isViewPage {
- c.Data["content"] = string(markup.Markdown(p, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
- } else {
- c.Data["content"] = string(p)
- }
- return wikiRepo, pageName
- }
- func Wiki(c *context.Context) {
- c.Data["PageIsWiki"] = true
- if !c.Repo.Repository.HasWiki() {
- c.Data["Title"] = c.Tr("repo.wiki")
- c.Success(WIKI_START)
- return
- }
- wikiRepo, pageName := renderWikiPage(c, true)
- if c.Written() {
- return
- }
- // Get last change information.
- commits, err := wikiRepo.Log(git.RefsHeads+"master", git.LogOptions{Path: pageName + ".md"})
- if err != nil {
- c.Error(err, "get commits by path")
- return
- }
- c.Data["Author"] = commits[0].Author
- c.Success(WIKI_VIEW)
- }
- func WikiPages(c *context.Context) {
- c.Data["Title"] = c.Tr("repo.wiki.pages")
- c.Data["PageIsWiki"] = true
- if !c.Repo.Repository.HasWiki() {
- c.Redirect(c.Repo.RepoLink + "/wiki")
- return
- }
- wikiRepo, err := git.Open(c.Repo.Repository.WikiPath())
- if err != nil {
- c.Error(err, "open repository")
- return
- }
- commit, err := wikiRepo.BranchCommit("master")
- if err != nil {
- c.Error(err, "get branch commit")
- return
- }
- entries, err := commit.Entries()
- if err != nil {
- c.Error(err, "list entries")
- return
- }
- pages := make([]PageMeta, 0, len(entries))
- for i := range entries {
- if entries[i].Type() == git.ObjectBlob && strings.HasSuffix(entries[i].Name(), ".md") {
- commits, err := wikiRepo.Log(git.RefsHeads+"master", git.LogOptions{Path: entries[i].Name()})
- if err != nil {
- c.Error(err, "get commits by path")
- return
- }
- name := strings.TrimSuffix(entries[i].Name(), ".md")
- pages = append(pages, PageMeta{
- Name: name,
- URL: db.ToWikiPageURL(name),
- Updated: commits[0].Author.When,
- })
- }
- }
- c.Data["Pages"] = pages
- c.Success(WIKI_PAGES)
- }
- func NewWiki(c *context.Context) {
- c.Data["Title"] = c.Tr("repo.wiki.new_page")
- c.Data["PageIsWiki"] = true
- c.Data["RequireSimpleMDE"] = true
- if !c.Repo.Repository.HasWiki() {
- c.Data["title"] = "Home"
- }
- c.Success(WIKI_NEW)
- }
- func NewWikiPost(c *context.Context, f form.NewWiki) {
- c.Data["Title"] = c.Tr("repo.wiki.new_page")
- c.Data["PageIsWiki"] = true
- c.Data["RequireSimpleMDE"] = true
- if c.HasError() {
- c.Success(WIKI_NEW)
- return
- }
- if err := c.Repo.Repository.AddWikiPage(c.User, f.Title, f.Content, f.Message); err != nil {
- if db.IsErrWikiAlreadyExist(err) {
- c.Data["Err_Title"] = true
- c.RenderWithErr(c.Tr("repo.wiki.page_already_exists"), WIKI_NEW, &f)
- } else {
- c.Error(err, "add wiki page")
- }
- return
- }
- c.Redirect(c.Repo.RepoLink + "/wiki/" + db.ToWikiPageURL(db.ToWikiPageName(f.Title)))
- }
- func EditWiki(c *context.Context) {
- c.Data["PageIsWiki"] = true
- c.Data["PageIsWikiEdit"] = true
- c.Data["RequireSimpleMDE"] = true
- if !c.Repo.Repository.HasWiki() {
- c.Redirect(c.Repo.RepoLink + "/wiki")
- return
- }
- renderWikiPage(c, false)
- if c.Written() {
- return
- }
- c.Success(WIKI_NEW)
- }
- func EditWikiPost(c *context.Context, f form.NewWiki) {
- c.Data["Title"] = c.Tr("repo.wiki.new_page")
- c.Data["PageIsWiki"] = true
- c.Data["RequireSimpleMDE"] = true
- if c.HasError() {
- c.Success(WIKI_NEW)
- return
- }
- if err := c.Repo.Repository.EditWikiPage(c.User, f.OldTitle, f.Title, f.Content, f.Message); err != nil {
- c.Error(err, "edit wiki page")
- return
- }
- c.Redirect(c.Repo.RepoLink + "/wiki/" + db.ToWikiPageURL(db.ToWikiPageName(f.Title)))
- }
- func DeleteWikiPagePost(c *context.Context) {
- pageURL := c.Params(":page")
- if len(pageURL) == 0 {
- pageURL = "Home"
- }
- pageName := db.ToWikiPageName(pageURL)
- if err := c.Repo.Repository.DeleteWikiPage(c.User, pageName); err != nil {
- c.Error(err, "delete wiki page")
- return
- }
- c.JSONSuccess(map[string]interface{}{
- "redirect": c.Repo.RepoLink + "/wiki/",
- })
- }
|