You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
5.9 KiB

  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-module"
  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. return
  25. }
  26. if ctx.Repo.Repository.EnableExternalWiki {
  27. ctx.Redirect(ctx.Repo.Repository.ExternalWikiURL)
  28. return
  29. }
  30. }
  31. type PageMeta struct {
  32. Name string
  33. URL string
  34. Updated time.Time
  35. }
  36. func renderWikiPage(ctx *middleware.Context, isViewPage bool) (*git.Repository, string) {
  37. wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
  38. if err != nil {
  39. ctx.Handle(500, "OpenRepository", err)
  40. return nil, ""
  41. }
  42. commit, err := wikiRepo.GetBranchCommit("master")
  43. if err != nil {
  44. ctx.Handle(500, "GetBranchCommit", err)
  45. return nil, ""
  46. }
  47. // Get page list.
  48. if isViewPage {
  49. entries, err := commit.ListEntries()
  50. if err != nil {
  51. ctx.Handle(500, "ListEntries", err)
  52. return nil, ""
  53. }
  54. pages := make([]PageMeta, 0, len(entries))
  55. for i := range entries {
  56. if entries[i].Type == git.OBJECT_BLOB {
  57. name := strings.TrimSuffix(entries[i].Name(), ".md")
  58. pages = append(pages, PageMeta{
  59. Name: name,
  60. URL: models.ToWikiPageURL(name),
  61. })
  62. }
  63. }
  64. ctx.Data["Pages"] = pages
  65. }
  66. pageURL := ctx.Params(":page")
  67. if len(pageURL) == 0 {
  68. pageURL = "Home"
  69. }
  70. ctx.Data["PageURL"] = pageURL
  71. pageName := models.ToWikiPageName(pageURL)
  72. ctx.Data["old_title"] = pageName
  73. ctx.Data["Title"] = pageName
  74. ctx.Data["title"] = pageName
  75. ctx.Data["RequireHighlightJS"] = true
  76. blob, err := commit.GetBlobByPath(pageName + ".md")
  77. if err != nil {
  78. if git.IsErrNotExist(err) {
  79. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
  80. } else {
  81. ctx.Handle(500, "GetBlobByPath", err)
  82. }
  83. return nil, ""
  84. }
  85. r, err := blob.Data()
  86. if err != nil {
  87. ctx.Handle(500, "Data", err)
  88. return nil, ""
  89. }
  90. data, err := ioutil.ReadAll(r)
  91. if err != nil {
  92. ctx.Handle(500, "ReadAll", err)
  93. return nil, ""
  94. }
  95. if isViewPage {
  96. ctx.Data["content"] = string(base.RenderMarkdown(data, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()))
  97. } else {
  98. ctx.Data["content"] = string(data)
  99. }
  100. return wikiRepo, pageName
  101. }
  102. func Wiki(ctx *middleware.Context) {
  103. ctx.Data["PageIsWiki"] = true
  104. if !ctx.Repo.Repository.HasWiki() {
  105. ctx.Data["Title"] = ctx.Tr("repo.wiki")
  106. ctx.HTML(200, WIKI_START)
  107. return
  108. }
  109. wikiRepo, pageName := renderWikiPage(ctx, true)
  110. if ctx.Written() {
  111. return
  112. }
  113. // Get last change information.
  114. lastCommit, err := wikiRepo.GetCommitByPath(pageName + ".md")
  115. if err != nil {
  116. ctx.Handle(500, "GetCommitByPath", err)
  117. return
  118. }
  119. ctx.Data["Author"] = lastCommit.Author
  120. ctx.HTML(200, WIKI_VIEW)
  121. }
  122. func WikiPages(ctx *middleware.Context) {
  123. ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
  124. ctx.Data["PageIsWiki"] = true
  125. if !ctx.Repo.Repository.HasWiki() {
  126. ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
  127. return
  128. }
  129. wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
  130. if err != nil {
  131. ctx.Handle(500, "OpenRepository", err)
  132. return
  133. }
  134. commit, err := wikiRepo.GetBranchCommit("master")
  135. if err != nil {
  136. ctx.Handle(500, "GetBranchCommit", err)
  137. return
  138. }
  139. entries, err := commit.ListEntries()
  140. if err != nil {
  141. ctx.Handle(500, "ListEntries", err)
  142. return
  143. }
  144. pages := make([]PageMeta, 0, len(entries))
  145. for i := range entries {
  146. if entries[i].Type == git.OBJECT_BLOB {
  147. c, err := wikiRepo.GetCommitByPath(entries[i].Name())
  148. if err != nil {
  149. ctx.Handle(500, "GetCommit", err)
  150. return
  151. }
  152. name := strings.TrimSuffix(entries[i].Name(), ".md")
  153. pages = append(pages, PageMeta{
  154. Name: name,
  155. URL: models.ToWikiPageURL(name),
  156. Updated: c.Author.When,
  157. })
  158. }
  159. }
  160. ctx.Data["Pages"] = pages
  161. ctx.HTML(200, WIKI_PAGES)
  162. }
  163. func NewWiki(ctx *middleware.Context) {
  164. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  165. ctx.Data["PageIsWiki"] = true
  166. ctx.Data["RequireSimpleMDE"] = true
  167. if !ctx.Repo.Repository.HasWiki() {
  168. ctx.Data["title"] = "Home"
  169. }
  170. ctx.HTML(200, WIKI_NEW)
  171. }
  172. func NewWikiPost(ctx *middleware.Context, form auth.NewWikiForm) {
  173. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  174. ctx.Data["PageIsWiki"] = true
  175. ctx.Data["RequireSimpleMDE"] = true
  176. if ctx.HasError() {
  177. ctx.HTML(200, WIKI_NEW)
  178. return
  179. }
  180. if err := ctx.Repo.Repository.AddWikiPage(ctx.User, form.Title, form.Content, form.Message); err != nil {
  181. if models.IsErrWikiAlreadyExist(err) {
  182. ctx.Data["Err_Title"] = true
  183. ctx.RenderWithErr(ctx.Tr("repo.wiki.page_already_exists"), WIKI_NEW, &form)
  184. } else {
  185. ctx.Handle(500, "AddWikiPage", err)
  186. }
  187. return
  188. }
  189. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title))
  190. }
  191. func EditWiki(ctx *middleware.Context) {
  192. ctx.Data["PageIsWiki"] = true
  193. ctx.Data["PageIsWikiEdit"] = true
  194. ctx.Data["RequireSimpleMDE"] = true
  195. if !ctx.Repo.Repository.HasWiki() {
  196. ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
  197. return
  198. }
  199. renderWikiPage(ctx, false)
  200. if ctx.Written() {
  201. return
  202. }
  203. ctx.HTML(200, WIKI_NEW)
  204. }
  205. func EditWikiPost(ctx *middleware.Context, form auth.NewWikiForm) {
  206. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  207. ctx.Data["PageIsWiki"] = true
  208. ctx.Data["RequireSimpleMDE"] = true
  209. if ctx.HasError() {
  210. ctx.HTML(200, WIKI_NEW)
  211. return
  212. }
  213. if err := ctx.Repo.Repository.EditWikiPage(ctx.User, form.OldTitle, form.Title, form.Content, form.Message); err != nil {
  214. ctx.Handle(500, "EditWikiPage", err)
  215. return
  216. }
  217. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title))
  218. }