wiki.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "io/ioutil"
  7. "strings"
  8. "time"
  9. "github.com/gogits/git-shell"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/auth"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/middleware"
  14. )
  15. const (
  16. WIKI_START base.TplName = "repo/wiki/start"
  17. WIKI_VIEW base.TplName = "repo/wiki/view"
  18. WIKI_NEW base.TplName = "repo/wiki/new"
  19. WIKI_PAGES base.TplName = "repo/wiki/pages"
  20. )
  21. func MustEnableWiki(ctx *middleware.Context) {
  22. if !ctx.Repo.Repository.EnableWiki {
  23. ctx.Handle(404, "MustEnableWiki", nil)
  24. }
  25. }
  26. type PageMeta struct {
  27. Name string
  28. URL string
  29. Updated time.Time
  30. }
  31. func renderWikiPage(ctx *middleware.Context, isViewPage bool) (*git.Repository, string) {
  32. wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
  33. if err != nil {
  34. ctx.Handle(500, "OpenRepository", err)
  35. return nil, ""
  36. }
  37. commit, err := wikiRepo.GetCommitOfBranch("master")
  38. if err != nil {
  39. ctx.Handle(500, "GetCommitOfBranch", err)
  40. return nil, ""
  41. }
  42. // Get page list.
  43. if isViewPage {
  44. entries, err := commit.ListEntries()
  45. if err != nil {
  46. ctx.Handle(500, "ListEntries", err)
  47. return nil, ""
  48. }
  49. pages := make([]PageMeta, 0, len(entries))
  50. for i := range entries {
  51. if entries[i].Type == git.OBJECT_BLOB {
  52. name := strings.TrimSuffix(entries[i].Name(), ".md")
  53. pages = append(pages, PageMeta{
  54. Name: name,
  55. URL: models.ToWikiPageURL(name),
  56. })
  57. }
  58. }
  59. ctx.Data["Pages"] = pages
  60. }
  61. pageURL := ctx.Params(":page")
  62. if len(pageURL) == 0 {
  63. pageURL = "Home"
  64. }
  65. ctx.Data["PageURL"] = pageURL
  66. pageName := models.ToWikiPageName(pageURL)
  67. ctx.Data["old_title"] = pageName
  68. ctx.Data["Title"] = pageName
  69. ctx.Data["title"] = pageName
  70. ctx.Data["RequireHighlightJS"] = true
  71. blob, err := commit.GetBlobByPath(pageName + ".md")
  72. if err != nil {
  73. if git.IsErrNotExist(err) {
  74. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
  75. } else {
  76. ctx.Handle(500, "GetBlobByPath", err)
  77. }
  78. return nil, ""
  79. }
  80. r, err := blob.Data()
  81. if err != nil {
  82. ctx.Handle(500, "Data", err)
  83. return nil, ""
  84. }
  85. data, err := ioutil.ReadAll(r)
  86. if err != nil {
  87. ctx.Handle(500, "ReadAll", err)
  88. return nil, ""
  89. }
  90. if isViewPage {
  91. ctx.Data["content"] = string(base.RenderMarkdown(data, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()))
  92. } else {
  93. ctx.Data["content"] = string(data)
  94. }
  95. return wikiRepo, pageName
  96. }
  97. func Wiki(ctx *middleware.Context) {
  98. ctx.Data["PageIsWiki"] = true
  99. if !ctx.Repo.Repository.HasWiki() {
  100. ctx.Data["Title"] = ctx.Tr("repo.wiki")
  101. ctx.HTML(200, WIKI_START)
  102. return
  103. }
  104. wikiRepo, pageName := renderWikiPage(ctx, true)
  105. if ctx.Written() {
  106. return
  107. }
  108. // Get last change information.
  109. lastCommit, err := wikiRepo.GetCommitByPath(pageName + ".md")
  110. if err != nil {
  111. ctx.Handle(500, "GetCommitByPath", err)
  112. return
  113. }
  114. ctx.Data["Author"] = lastCommit.Author
  115. ctx.HTML(200, WIKI_VIEW)
  116. }
  117. func WikiPages(ctx *middleware.Context) {
  118. ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
  119. ctx.Data["PageIsWiki"] = true
  120. if !ctx.Repo.Repository.HasWiki() {
  121. ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
  122. return
  123. }
  124. wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
  125. if err != nil {
  126. ctx.Handle(500, "OpenRepository", err)
  127. return
  128. }
  129. commit, err := wikiRepo.GetCommitOfBranch("master")
  130. if err != nil {
  131. ctx.Handle(500, "GetCommitOfBranch", err)
  132. return
  133. }
  134. entries, err := commit.ListEntries()
  135. if err != nil {
  136. ctx.Handle(500, "ListEntries", err)
  137. return
  138. }
  139. pages := make([]PageMeta, 0, len(entries))
  140. for i := range entries {
  141. if entries[i].Type == git.OBJECT_BLOB {
  142. c, err := wikiRepo.GetCommitByPath(entries[i].Name())
  143. if err != nil {
  144. ctx.Handle(500, "GetCommit", err)
  145. return
  146. }
  147. name := strings.TrimSuffix(entries[i].Name(), ".md")
  148. pages = append(pages, PageMeta{
  149. Name: name,
  150. URL: models.ToWikiPageURL(name),
  151. Updated: c.Author.When,
  152. })
  153. }
  154. }
  155. ctx.Data["Pages"] = pages
  156. ctx.HTML(200, WIKI_PAGES)
  157. }
  158. func NewWiki(ctx *middleware.Context) {
  159. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  160. ctx.Data["PageIsWiki"] = true
  161. ctx.Data["RequireSimpleMDE"] = true
  162. if !ctx.Repo.Repository.HasWiki() {
  163. ctx.Data["title"] = "Home"
  164. }
  165. ctx.HTML(200, WIKI_NEW)
  166. }
  167. func NewWikiPost(ctx *middleware.Context, form auth.NewWikiForm) {
  168. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  169. ctx.Data["PageIsWiki"] = true
  170. ctx.Data["RequireSimpleMDE"] = true
  171. if ctx.HasError() {
  172. ctx.HTML(200, WIKI_NEW)
  173. return
  174. }
  175. if err := ctx.Repo.Repository.AddWikiPage(ctx.User, form.Title, form.Content, form.Message); err != nil {
  176. if models.IsErrWikiAlreadyExist(err) {
  177. ctx.Data["Err_Title"] = true
  178. ctx.RenderWithErr(ctx.Tr("repo.wiki.page_already_exists"), WIKI_NEW, &form)
  179. } else {
  180. ctx.Handle(500, "AddWikiPage", err)
  181. }
  182. return
  183. }
  184. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title))
  185. }
  186. func EditWiki(ctx *middleware.Context) {
  187. ctx.Data["PageIsWiki"] = true
  188. ctx.Data["PageIsWikiEdit"] = true
  189. ctx.Data["RequireSimpleMDE"] = true
  190. if !ctx.Repo.Repository.HasWiki() {
  191. ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
  192. return
  193. }
  194. renderWikiPage(ctx, false)
  195. if ctx.Written() {
  196. return
  197. }
  198. ctx.HTML(200, WIKI_NEW)
  199. }
  200. func EditWikiPost(ctx *middleware.Context, form auth.NewWikiForm) {
  201. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  202. ctx.Data["PageIsWiki"] = true
  203. ctx.Data["RequireSimpleMDE"] = true
  204. if ctx.HasError() {
  205. ctx.HTML(200, WIKI_NEW)
  206. return
  207. }
  208. if err := ctx.Repo.Repository.EditWikiPage(ctx.User, form.OldTitle, form.Title, form.Content, form.Message); err != nil {
  209. ctx.Handle(500, "EditWikiPage", err)
  210. return
  211. }
  212. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title))
  213. }