wiki.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/gogs/git-module"
  10. "github.com/gogs/gogs/models"
  11. "github.com/gogs/gogs/pkg/context"
  12. "github.com/gogs/gogs/pkg/form"
  13. "github.com/gogs/gogs/pkg/markup"
  14. )
  15. const (
  16. WIKI_START = "repo/wiki/start"
  17. WIKI_VIEW = "repo/wiki/view"
  18. WIKI_NEW = "repo/wiki/new"
  19. WIKI_PAGES = "repo/wiki/pages"
  20. )
  21. func MustEnableWiki(c *context.Context) {
  22. if !c.Repo.Repository.EnableWiki {
  23. c.Handle(404, "MustEnableWiki", nil)
  24. return
  25. }
  26. if c.Repo.Repository.EnableExternalWiki {
  27. c.Redirect(c.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(c *context.Context, isViewPage bool) (*git.Repository, string) {
  37. wikiRepo, err := git.OpenRepository(c.Repo.Repository.WikiPath())
  38. if err != nil {
  39. c.Handle(500, "OpenRepository", err)
  40. return nil, ""
  41. }
  42. commit, err := wikiRepo.GetBranchCommit("master")
  43. if err != nil {
  44. c.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. c.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 && strings.HasSuffix(entries[i].Name(), ".md") {
  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. c.Data["Pages"] = pages
  65. }
  66. pageURL := c.Params(":page")
  67. if len(pageURL) == 0 {
  68. pageURL = "Home"
  69. }
  70. c.Data["PageURL"] = pageURL
  71. pageName := models.ToWikiPageName(pageURL)
  72. c.Data["old_title"] = pageName
  73. c.Data["Title"] = pageName
  74. c.Data["title"] = pageName
  75. c.Data["RequireHighlightJS"] = true
  76. blob, err := commit.GetBlobByPath(pageName + ".md")
  77. if err != nil {
  78. if git.IsErrNotExist(err) {
  79. c.Redirect(c.Repo.RepoLink + "/wiki/_pages")
  80. } else {
  81. c.Handle(500, "GetBlobByPath", err)
  82. }
  83. return nil, ""
  84. }
  85. r, err := blob.Data()
  86. if err != nil {
  87. c.Handle(500, "Data", err)
  88. return nil, ""
  89. }
  90. data, err := ioutil.ReadAll(r)
  91. if err != nil {
  92. c.Handle(500, "ReadAll", err)
  93. return nil, ""
  94. }
  95. if isViewPage {
  96. c.Data["content"] = string(markup.Markdown(data, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
  97. } else {
  98. c.Data["content"] = string(data)
  99. }
  100. return wikiRepo, pageName
  101. }
  102. func Wiki(c *context.Context) {
  103. c.Data["PageIsWiki"] = true
  104. if !c.Repo.Repository.HasWiki() {
  105. c.Data["Title"] = c.Tr("repo.wiki")
  106. c.HTML(200, WIKI_START)
  107. return
  108. }
  109. wikiRepo, pageName := renderWikiPage(c, true)
  110. if c.Written() {
  111. return
  112. }
  113. // Get last change information.
  114. lastCommit, err := wikiRepo.GetCommitByPath(pageName + ".md")
  115. if err != nil {
  116. c.Handle(500, "GetCommitByPath", err)
  117. return
  118. }
  119. c.Data["Author"] = lastCommit.Author
  120. c.HTML(200, WIKI_VIEW)
  121. }
  122. func WikiPages(c *context.Context) {
  123. c.Data["Title"] = c.Tr("repo.wiki.pages")
  124. c.Data["PageIsWiki"] = true
  125. if !c.Repo.Repository.HasWiki() {
  126. c.Redirect(c.Repo.RepoLink + "/wiki")
  127. return
  128. }
  129. wikiRepo, err := git.OpenRepository(c.Repo.Repository.WikiPath())
  130. if err != nil {
  131. c.Handle(500, "OpenRepository", err)
  132. return
  133. }
  134. commit, err := wikiRepo.GetBranchCommit("master")
  135. if err != nil {
  136. c.Handle(500, "GetBranchCommit", err)
  137. return
  138. }
  139. entries, err := commit.ListEntries()
  140. if err != nil {
  141. c.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 && strings.HasSuffix(entries[i].Name(), ".md") {
  147. commit, err := wikiRepo.GetCommitByPath(entries[i].Name())
  148. if err != nil {
  149. c.ServerError("GetCommitByPath", 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: commit.Author.When,
  157. })
  158. }
  159. }
  160. c.Data["Pages"] = pages
  161. c.HTML(200, WIKI_PAGES)
  162. }
  163. func NewWiki(c *context.Context) {
  164. c.Data["Title"] = c.Tr("repo.wiki.new_page")
  165. c.Data["PageIsWiki"] = true
  166. c.Data["RequireSimpleMDE"] = true
  167. if !c.Repo.Repository.HasWiki() {
  168. c.Data["title"] = "Home"
  169. }
  170. c.HTML(200, WIKI_NEW)
  171. }
  172. func NewWikiPost(c *context.Context, f form.NewWiki) {
  173. c.Data["Title"] = c.Tr("repo.wiki.new_page")
  174. c.Data["PageIsWiki"] = true
  175. c.Data["RequireSimpleMDE"] = true
  176. if c.HasError() {
  177. c.HTML(200, WIKI_NEW)
  178. return
  179. }
  180. if err := c.Repo.Repository.AddWikiPage(c.User, f.Title, f.Content, f.Message); err != nil {
  181. if models.IsErrWikiAlreadyExist(err) {
  182. c.Data["Err_Title"] = true
  183. c.RenderWithErr(c.Tr("repo.wiki.page_already_exists"), WIKI_NEW, &f)
  184. } else {
  185. c.Handle(500, "AddWikiPage", err)
  186. }
  187. return
  188. }
  189. c.Redirect(c.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(models.ToWikiPageName(f.Title)))
  190. }
  191. func EditWiki(c *context.Context) {
  192. c.Data["PageIsWiki"] = true
  193. c.Data["PageIsWikiEdit"] = true
  194. c.Data["RequireSimpleMDE"] = true
  195. if !c.Repo.Repository.HasWiki() {
  196. c.Redirect(c.Repo.RepoLink + "/wiki")
  197. return
  198. }
  199. renderWikiPage(c, false)
  200. if c.Written() {
  201. return
  202. }
  203. c.HTML(200, WIKI_NEW)
  204. }
  205. func EditWikiPost(c *context.Context, f form.NewWiki) {
  206. c.Data["Title"] = c.Tr("repo.wiki.new_page")
  207. c.Data["PageIsWiki"] = true
  208. c.Data["RequireSimpleMDE"] = true
  209. if c.HasError() {
  210. c.HTML(200, WIKI_NEW)
  211. return
  212. }
  213. if err := c.Repo.Repository.EditWikiPage(c.User, f.OldTitle, f.Title, f.Content, f.Message); err != nil {
  214. c.Handle(500, "EditWikiPage", err)
  215. return
  216. }
  217. c.Redirect(c.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(models.ToWikiPageName(f.Title)))
  218. }
  219. func DeleteWikiPagePost(c *context.Context) {
  220. pageURL := c.Params(":page")
  221. if len(pageURL) == 0 {
  222. pageURL = "Home"
  223. }
  224. pageName := models.ToWikiPageName(pageURL)
  225. if err := c.Repo.Repository.DeleteWikiPage(c.User, pageName); err != nil {
  226. c.Handle(500, "DeleteWikiPage", err)
  227. return
  228. }
  229. c.JSON(200, map[string]interface{}{
  230. "redirect": c.Repo.RepoLink + "/wiki/",
  231. })
  232. }