wiki.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. "strings"
  7. "time"
  8. "github.com/gogs/git-module"
  9. "gogs.io/gogs/internal/context"
  10. "gogs.io/gogs/internal/db"
  11. "gogs.io/gogs/internal/form"
  12. "gogs.io/gogs/internal/gitutil"
  13. "gogs.io/gogs/internal/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.NotFound()
  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.Open(c.Repo.Repository.WikiPath())
  38. if err != nil {
  39. c.Error(err, "open repository")
  40. return nil, ""
  41. }
  42. commit, err := wikiRepo.BranchCommit("master")
  43. if err != nil {
  44. c.Error(err, "get branch commit")
  45. return nil, ""
  46. }
  47. // Get page list.
  48. if isViewPage {
  49. entries, err := commit.Entries()
  50. if err != nil {
  51. c.Error(err, "list entries")
  52. return nil, ""
  53. }
  54. pages := make([]PageMeta, 0, len(entries))
  55. for i := range entries {
  56. if entries[i].Type() == git.ObjectBlob && strings.HasSuffix(entries[i].Name(), ".md") {
  57. name := strings.TrimSuffix(entries[i].Name(), ".md")
  58. pages = append(pages, PageMeta{
  59. Name: name,
  60. URL: db.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 := db.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.Blob(pageName + ".md")
  77. if err != nil {
  78. if gitutil.IsErrRevisionNotExist(err) {
  79. c.Redirect(c.Repo.RepoLink + "/wiki/_pages")
  80. } else {
  81. c.Error(err, "get blob")
  82. }
  83. return nil, ""
  84. }
  85. p, err := blob.Bytes()
  86. if err != nil {
  87. c.Error(err, "read blob")
  88. return nil, ""
  89. }
  90. if isViewPage {
  91. c.Data["content"] = string(markup.Markdown(p, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
  92. } else {
  93. c.Data["content"] = string(p)
  94. }
  95. return wikiRepo, pageName
  96. }
  97. func Wiki(c *context.Context) {
  98. c.Data["PageIsWiki"] = true
  99. if !c.Repo.Repository.HasWiki() {
  100. c.Data["Title"] = c.Tr("repo.wiki")
  101. c.Success(WIKI_START)
  102. return
  103. }
  104. wikiRepo, pageName := renderWikiPage(c, true)
  105. if c.Written() {
  106. return
  107. }
  108. // Get last change information.
  109. commits, err := wikiRepo.Log(git.RefsHeads+"master", git.LogOptions{Path: pageName + ".md"})
  110. if err != nil {
  111. c.Error(err, "get commits by path")
  112. return
  113. }
  114. c.Data["Author"] = commits[0].Author
  115. c.Success(WIKI_VIEW)
  116. }
  117. func WikiPages(c *context.Context) {
  118. c.Data["Title"] = c.Tr("repo.wiki.pages")
  119. c.Data["PageIsWiki"] = true
  120. if !c.Repo.Repository.HasWiki() {
  121. c.Redirect(c.Repo.RepoLink + "/wiki")
  122. return
  123. }
  124. wikiRepo, err := git.Open(c.Repo.Repository.WikiPath())
  125. if err != nil {
  126. c.Error(err, "open repository")
  127. return
  128. }
  129. commit, err := wikiRepo.BranchCommit("master")
  130. if err != nil {
  131. c.Error(err, "get branch commit")
  132. return
  133. }
  134. entries, err := commit.Entries()
  135. if err != nil {
  136. c.Error(err, "list entries")
  137. return
  138. }
  139. pages := make([]PageMeta, 0, len(entries))
  140. for i := range entries {
  141. if entries[i].Type() == git.ObjectBlob && strings.HasSuffix(entries[i].Name(), ".md") {
  142. commits, err := wikiRepo.Log(git.RefsHeads+"master", git.LogOptions{Path: entries[i].Name()})
  143. if err != nil {
  144. c.Error(err, "get commits by path")
  145. return
  146. }
  147. name := strings.TrimSuffix(entries[i].Name(), ".md")
  148. pages = append(pages, PageMeta{
  149. Name: name,
  150. URL: db.ToWikiPageURL(name),
  151. Updated: commits[0].Author.When,
  152. })
  153. }
  154. }
  155. c.Data["Pages"] = pages
  156. c.Success(WIKI_PAGES)
  157. }
  158. func NewWiki(c *context.Context) {
  159. c.Data["Title"] = c.Tr("repo.wiki.new_page")
  160. c.Data["PageIsWiki"] = true
  161. c.Data["RequireSimpleMDE"] = true
  162. if !c.Repo.Repository.HasWiki() {
  163. c.Data["title"] = "Home"
  164. }
  165. c.Success(WIKI_NEW)
  166. }
  167. func NewWikiPost(c *context.Context, f form.NewWiki) {
  168. c.Data["Title"] = c.Tr("repo.wiki.new_page")
  169. c.Data["PageIsWiki"] = true
  170. c.Data["RequireSimpleMDE"] = true
  171. if c.HasError() {
  172. c.Success(WIKI_NEW)
  173. return
  174. }
  175. if err := c.Repo.Repository.AddWikiPage(c.User, f.Title, f.Content, f.Message); err != nil {
  176. if db.IsErrWikiAlreadyExist(err) {
  177. c.Data["Err_Title"] = true
  178. c.RenderWithErr(c.Tr("repo.wiki.page_already_exists"), WIKI_NEW, &f)
  179. } else {
  180. c.Error(err, "add wiki page")
  181. }
  182. return
  183. }
  184. c.Redirect(c.Repo.RepoLink + "/wiki/" + db.ToWikiPageURL(db.ToWikiPageName(f.Title)))
  185. }
  186. func EditWiki(c *context.Context) {
  187. c.Data["PageIsWiki"] = true
  188. c.Data["PageIsWikiEdit"] = true
  189. c.Data["RequireSimpleMDE"] = true
  190. if !c.Repo.Repository.HasWiki() {
  191. c.Redirect(c.Repo.RepoLink + "/wiki")
  192. return
  193. }
  194. renderWikiPage(c, false)
  195. if c.Written() {
  196. return
  197. }
  198. c.Success(WIKI_NEW)
  199. }
  200. func EditWikiPost(c *context.Context, f form.NewWiki) {
  201. c.Data["Title"] = c.Tr("repo.wiki.new_page")
  202. c.Data["PageIsWiki"] = true
  203. c.Data["RequireSimpleMDE"] = true
  204. if c.HasError() {
  205. c.Success(WIKI_NEW)
  206. return
  207. }
  208. if err := c.Repo.Repository.EditWikiPage(c.User, f.OldTitle, f.Title, f.Content, f.Message); err != nil {
  209. c.Error(err, "edit wiki page")
  210. return
  211. }
  212. c.Redirect(c.Repo.RepoLink + "/wiki/" + db.ToWikiPageURL(db.ToWikiPageName(f.Title)))
  213. }
  214. func DeleteWikiPagePost(c *context.Context) {
  215. pageURL := c.Params(":page")
  216. if len(pageURL) == 0 {
  217. pageURL = "Home"
  218. }
  219. pageName := db.ToWikiPageName(pageURL)
  220. if err := c.Repo.Repository.DeleteWikiPage(c.User, pageName); err != nil {
  221. c.Error(err, "delete wiki page")
  222. return
  223. }
  224. c.JSONSuccess(map[string]interface{}{
  225. "redirect": c.Repo.RepoLink + "/wiki/",
  226. })
  227. }