wiki.go 6.4 KB

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