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.

284 lines
6.7 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. "code.gitea.io/git"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/auth"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/markdown"
  15. )
  16. const (
  17. tplWikiStart base.TplName = "repo/wiki/start"
  18. tplWikiView base.TplName = "repo/wiki/view"
  19. tplWikiNew base.TplName = "repo/wiki/new"
  20. tplWikiPages base.TplName = "repo/wiki/pages"
  21. )
  22. // MustEnableWiki check if wiki is enabled, if external then redirect
  23. func MustEnableWiki(ctx *context.Context) {
  24. if !ctx.Repo.Repository.EnableWiki {
  25. ctx.Handle(404, "MustEnableWiki", nil)
  26. return
  27. }
  28. if ctx.Repo.Repository.EnableExternalWiki {
  29. ctx.Redirect(ctx.Repo.Repository.ExternalWikiURL)
  30. return
  31. }
  32. }
  33. // PageMeta wiki page meat information
  34. type PageMeta struct {
  35. Name string
  36. URL string
  37. Updated time.Time
  38. }
  39. func renderWikiPage(ctx *context.Context, isViewPage bool) (*git.Repository, string) {
  40. wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
  41. if err != nil {
  42. ctx.Handle(500, "OpenRepository", err)
  43. return nil, ""
  44. }
  45. commit, err := wikiRepo.GetBranchCommit("master")
  46. if err != nil {
  47. ctx.Handle(500, "GetBranchCommit", err)
  48. return nil, ""
  49. }
  50. // Get page list.
  51. if isViewPage {
  52. entries, err := commit.ListEntries()
  53. if err != nil {
  54. ctx.Handle(500, "ListEntries", err)
  55. return nil, ""
  56. }
  57. pages := make([]PageMeta, 0, len(entries))
  58. for i := range entries {
  59. if entries[i].Type == git.OBJECT_BLOB && strings.HasSuffix(entries[i].Name(), ".md") {
  60. name := strings.TrimSuffix(entries[i].Name(), ".md")
  61. pages = append(pages, PageMeta{
  62. Name: name,
  63. URL: models.ToWikiPageURL(name),
  64. })
  65. }
  66. }
  67. ctx.Data["Pages"] = pages
  68. }
  69. pageURL := ctx.Params(":page")
  70. if len(pageURL) == 0 {
  71. pageURL = "Home"
  72. }
  73. ctx.Data["PageURL"] = pageURL
  74. pageName := models.ToWikiPageName(pageURL)
  75. ctx.Data["old_title"] = pageName
  76. ctx.Data["Title"] = pageName
  77. ctx.Data["title"] = pageName
  78. ctx.Data["RequireHighlightJS"] = true
  79. blob, err := commit.GetBlobByPath(pageName + ".md")
  80. if err != nil {
  81. if git.IsErrNotExist(err) {
  82. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
  83. } else {
  84. ctx.Handle(500, "GetBlobByPath", err)
  85. }
  86. return nil, ""
  87. }
  88. r, err := blob.Data()
  89. if err != nil {
  90. ctx.Handle(500, "Data", err)
  91. return nil, ""
  92. }
  93. data, err := ioutil.ReadAll(r)
  94. if err != nil {
  95. ctx.Handle(500, "ReadAll", err)
  96. return nil, ""
  97. }
  98. if isViewPage {
  99. ctx.Data["content"] = string(markdown.Render(data, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()))
  100. } else {
  101. ctx.Data["content"] = string(data)
  102. }
  103. return wikiRepo, pageName
  104. }
  105. // Wiki render wiki page
  106. func Wiki(ctx *context.Context) {
  107. ctx.Data["PageIsWiki"] = true
  108. if !ctx.Repo.Repository.HasWiki() {
  109. ctx.Data["Title"] = ctx.Tr("repo.wiki")
  110. ctx.HTML(200, tplWikiStart)
  111. return
  112. }
  113. wikiRepo, pageName := renderWikiPage(ctx, true)
  114. if ctx.Written() {
  115. return
  116. }
  117. // Get last change information.
  118. lastCommit, err := wikiRepo.GetCommitByPath(pageName + ".md")
  119. if err != nil {
  120. ctx.Handle(500, "GetCommitByPath", err)
  121. return
  122. }
  123. ctx.Data["Author"] = lastCommit.Author
  124. ctx.HTML(200, tplWikiView)
  125. }
  126. // WikiPages render wiki pages list page
  127. func WikiPages(ctx *context.Context) {
  128. ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
  129. ctx.Data["PageIsWiki"] = true
  130. if !ctx.Repo.Repository.HasWiki() {
  131. ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
  132. return
  133. }
  134. wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
  135. if err != nil {
  136. ctx.Handle(500, "OpenRepository", err)
  137. return
  138. }
  139. commit, err := wikiRepo.GetBranchCommit("master")
  140. if err != nil {
  141. ctx.Handle(500, "GetBranchCommit", err)
  142. return
  143. }
  144. entries, err := commit.ListEntries()
  145. if err != nil {
  146. ctx.Handle(500, "ListEntries", err)
  147. return
  148. }
  149. pages := make([]PageMeta, 0, len(entries))
  150. for i := range entries {
  151. if entries[i].Type == git.OBJECT_BLOB && strings.HasSuffix(entries[i].Name(), ".md") {
  152. c, err := wikiRepo.GetCommitByPath(entries[i].Name())
  153. if err != nil {
  154. ctx.Handle(500, "GetCommit", err)
  155. return
  156. }
  157. name := strings.TrimSuffix(entries[i].Name(), ".md")
  158. pages = append(pages, PageMeta{
  159. Name: name,
  160. URL: models.ToWikiPageURL(name),
  161. Updated: c.Author.When,
  162. })
  163. }
  164. }
  165. ctx.Data["Pages"] = pages
  166. ctx.HTML(200, tplWikiPages)
  167. }
  168. // NewWiki render wiki create page
  169. func NewWiki(ctx *context.Context) {
  170. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  171. ctx.Data["PageIsWiki"] = true
  172. ctx.Data["RequireSimpleMDE"] = true
  173. if !ctx.Repo.Repository.HasWiki() {
  174. ctx.Data["title"] = "Home"
  175. }
  176. ctx.HTML(200, tplWikiNew)
  177. }
  178. // NewWikiPost response fro wiki create request
  179. func NewWikiPost(ctx *context.Context, form auth.NewWikiForm) {
  180. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  181. ctx.Data["PageIsWiki"] = true
  182. ctx.Data["RequireSimpleMDE"] = true
  183. if ctx.HasError() {
  184. ctx.HTML(200, tplWikiNew)
  185. return
  186. }
  187. if err := ctx.Repo.Repository.AddWikiPage(ctx.User, form.Title, form.Content, form.Message); err != nil {
  188. if models.IsErrWikiAlreadyExist(err) {
  189. ctx.Data["Err_Title"] = true
  190. ctx.RenderWithErr(ctx.Tr("repo.wiki.page_already_exists"), tplWikiNew, &form)
  191. } else {
  192. ctx.Handle(500, "AddWikiPage", err)
  193. }
  194. return
  195. }
  196. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title))
  197. }
  198. // EditWiki render wiki modify page
  199. func EditWiki(ctx *context.Context) {
  200. ctx.Data["PageIsWiki"] = true
  201. ctx.Data["PageIsWikiEdit"] = true
  202. ctx.Data["RequireSimpleMDE"] = true
  203. if !ctx.Repo.Repository.HasWiki() {
  204. ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
  205. return
  206. }
  207. renderWikiPage(ctx, false)
  208. if ctx.Written() {
  209. return
  210. }
  211. ctx.HTML(200, tplWikiNew)
  212. }
  213. // EditWikiPost response fro wiki modify request
  214. func EditWikiPost(ctx *context.Context, form auth.NewWikiForm) {
  215. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  216. ctx.Data["PageIsWiki"] = true
  217. ctx.Data["RequireSimpleMDE"] = true
  218. if ctx.HasError() {
  219. ctx.HTML(200, tplWikiNew)
  220. return
  221. }
  222. if err := ctx.Repo.Repository.EditWikiPage(ctx.User, form.OldTitle, form.Title, form.Content, form.Message); err != nil {
  223. ctx.Handle(500, "EditWikiPage", err)
  224. return
  225. }
  226. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title))
  227. }
  228. // DeleteWikiPagePost delete wiki page
  229. func DeleteWikiPagePost(ctx *context.Context) {
  230. pageURL := ctx.Params(":page")
  231. if len(pageURL) == 0 {
  232. pageURL = "Home"
  233. }
  234. pageName := models.ToWikiPageName(pageURL)
  235. if err := ctx.Repo.Repository.DeleteWikiPage(ctx.User, pageName); err != nil {
  236. ctx.Handle(500, "DeleteWikiPage", err)
  237. return
  238. }
  239. ctx.JSON(200, map[string]interface{}{
  240. "redirect": ctx.Repo.RepoLink + "/wiki/",
  241. })
  242. }