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

wiki.go 12 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
10 years ago
10 years ago
10 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
10 years ago
10 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "fmt"
  8. "io/ioutil"
  9. "path/filepath"
  10. "strings"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/auth"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/markup"
  18. "code.gitea.io/gitea/modules/markup/markdown"
  19. "code.gitea.io/gitea/modules/util"
  20. )
  21. const (
  22. tplWikiStart base.TplName = "repo/wiki/start"
  23. tplWikiView base.TplName = "repo/wiki/view"
  24. tplWikiNew base.TplName = "repo/wiki/new"
  25. tplWikiPages base.TplName = "repo/wiki/pages"
  26. )
  27. // MustEnableWiki check if wiki is enabled, if external then redirect
  28. func MustEnableWiki(ctx *context.Context) {
  29. if !ctx.Repo.CanRead(models.UnitTypeWiki) &&
  30. !ctx.Repo.CanRead(models.UnitTypeExternalWiki) {
  31. if log.IsTrace() {
  32. log.Trace("Permission Denied: User %-v cannot read %-v or %-v of repo %-v\n"+
  33. "User in repo has Permissions: %-+v",
  34. ctx.User,
  35. models.UnitTypeWiki,
  36. models.UnitTypeExternalWiki,
  37. ctx.Repo.Repository,
  38. ctx.Repo.Permission)
  39. }
  40. ctx.NotFound("MustEnableWiki", nil)
  41. return
  42. }
  43. unit, err := ctx.Repo.Repository.GetUnit(models.UnitTypeExternalWiki)
  44. if err == nil {
  45. ctx.Redirect(unit.ExternalWikiConfig().ExternalWikiURL)
  46. return
  47. }
  48. }
  49. // PageMeta wiki page meat information
  50. type PageMeta struct {
  51. Name string
  52. SubURL string
  53. UpdatedUnix util.TimeStamp
  54. }
  55. // findEntryForFile finds the tree entry for a target filepath.
  56. func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error) {
  57. entries, err := commit.ListEntries()
  58. if err != nil {
  59. return nil, err
  60. }
  61. for _, entry := range entries {
  62. if entry.IsRegular() && entry.Name() == target {
  63. return entry, nil
  64. }
  65. }
  66. return nil, nil
  67. }
  68. func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
  69. wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
  70. if err != nil {
  71. ctx.ServerError("OpenRepository", err)
  72. return nil, nil, err
  73. }
  74. commit, err := wikiRepo.GetBranchCommit("master")
  75. if err != nil {
  76. return wikiRepo, nil, err
  77. }
  78. return wikiRepo, commit, nil
  79. }
  80. // wikiContentsByEntry returns the contents of the wiki page referenced by the
  81. // given tree entry. Writes to ctx if an error occurs.
  82. func wikiContentsByEntry(ctx *context.Context, entry *git.TreeEntry) []byte {
  83. reader, err := entry.Blob().DataAsync()
  84. if err != nil {
  85. ctx.ServerError("Blob.Data", err)
  86. return nil
  87. }
  88. defer reader.Close()
  89. content, err := ioutil.ReadAll(reader)
  90. if err != nil {
  91. ctx.ServerError("ReadAll", err)
  92. return nil
  93. }
  94. return content
  95. }
  96. // wikiContentsByName returns the contents of a wiki page, along with a boolean
  97. // indicating whether the page exists. Writes to ctx if an error occurs.
  98. func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName string) ([]byte, bool) {
  99. entry, err := findEntryForFile(commit, models.WikiNameToFilename(wikiName))
  100. if err != nil {
  101. ctx.ServerError("findEntryForFile", err)
  102. return nil, false
  103. } else if entry == nil {
  104. return nil, false
  105. }
  106. return wikiContentsByEntry(ctx, entry), true
  107. }
  108. func renderWikiPage(ctx *context.Context, isViewPage bool) (*git.Repository, *git.TreeEntry) {
  109. wikiRepo, commit, err := findWikiRepoCommit(ctx)
  110. if err != nil {
  111. if !git.IsErrNotExist(err) {
  112. ctx.ServerError("GetBranchCommit", err)
  113. }
  114. return nil, nil
  115. }
  116. // Get page list.
  117. if isViewPage {
  118. entries, err := commit.ListEntries()
  119. if err != nil {
  120. ctx.ServerError("ListEntries", err)
  121. return nil, nil
  122. }
  123. pages := make([]PageMeta, 0, len(entries))
  124. for _, entry := range entries {
  125. if !entry.IsRegular() {
  126. continue
  127. }
  128. wikiName, err := models.WikiFilenameToName(entry.Name())
  129. if err != nil {
  130. if models.IsErrWikiInvalidFileName(err) {
  131. continue
  132. }
  133. ctx.ServerError("WikiFilenameToName", err)
  134. return nil, nil
  135. } else if wikiName == "_Sidebar" || wikiName == "_Footer" {
  136. continue
  137. }
  138. pages = append(pages, PageMeta{
  139. Name: wikiName,
  140. SubURL: models.WikiNameToSubURL(wikiName),
  141. })
  142. }
  143. ctx.Data["Pages"] = pages
  144. }
  145. pageName := models.NormalizeWikiName(ctx.Params(":page"))
  146. if len(pageName) == 0 {
  147. pageName = "Home"
  148. }
  149. ctx.Data["PageURL"] = models.WikiNameToSubURL(pageName)
  150. ctx.Data["old_title"] = pageName
  151. ctx.Data["Title"] = pageName
  152. ctx.Data["title"] = pageName
  153. ctx.Data["RequireHighlightJS"] = true
  154. pageFilename := models.WikiNameToFilename(pageName)
  155. var entry *git.TreeEntry
  156. if entry, err = findEntryForFile(commit, pageFilename); err != nil {
  157. ctx.ServerError("findEntryForFile", err)
  158. return nil, nil
  159. } else if entry == nil {
  160. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
  161. return nil, nil
  162. }
  163. data := wikiContentsByEntry(ctx, entry)
  164. if ctx.Written() {
  165. return nil, nil
  166. }
  167. if isViewPage {
  168. sidebarContent, sidebarPresent := wikiContentsByName(ctx, commit, "_Sidebar")
  169. if ctx.Written() {
  170. return nil, nil
  171. }
  172. footerContent, footerPresent := wikiContentsByName(ctx, commit, "_Footer")
  173. if ctx.Written() {
  174. return nil, nil
  175. }
  176. metas := ctx.Repo.Repository.ComposeMetas()
  177. ctx.Data["content"] = markdown.RenderWiki(data, ctx.Repo.RepoLink, metas)
  178. ctx.Data["sidebarPresent"] = sidebarPresent
  179. ctx.Data["sidebarContent"] = markdown.RenderWiki(sidebarContent, ctx.Repo.RepoLink, metas)
  180. ctx.Data["footerPresent"] = footerPresent
  181. ctx.Data["footerContent"] = markdown.RenderWiki(footerContent, ctx.Repo.RepoLink, metas)
  182. } else {
  183. ctx.Data["content"] = string(data)
  184. ctx.Data["sidebarPresent"] = false
  185. ctx.Data["sidebarContent"] = ""
  186. ctx.Data["footerPresent"] = false
  187. ctx.Data["footerContent"] = ""
  188. }
  189. return wikiRepo, entry
  190. }
  191. // Wiki renders single wiki page
  192. func Wiki(ctx *context.Context) {
  193. ctx.Data["PageIsWiki"] = true
  194. ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(models.UnitTypeWiki) && !ctx.Repo.Repository.IsArchived
  195. if !ctx.Repo.Repository.HasWiki() {
  196. ctx.Data["Title"] = ctx.Tr("repo.wiki")
  197. ctx.HTML(200, tplWikiStart)
  198. return
  199. }
  200. wikiRepo, entry := renderWikiPage(ctx, true)
  201. if ctx.Written() {
  202. return
  203. }
  204. if entry == nil {
  205. ctx.Data["Title"] = ctx.Tr("repo.wiki")
  206. ctx.HTML(200, tplWikiStart)
  207. return
  208. }
  209. wikiPath := entry.Name()
  210. if markup.Type(wikiPath) != markdown.MarkupName {
  211. ext := strings.ToUpper(filepath.Ext(wikiPath))
  212. ctx.Data["FormatWarning"] = fmt.Sprintf("%s rendering is not supported at the moment. Rendered as Markdown.", ext)
  213. }
  214. // Get last change information.
  215. lastCommit, err := wikiRepo.GetCommitByPath(wikiPath)
  216. if err != nil {
  217. ctx.ServerError("GetCommitByPath", err)
  218. return
  219. }
  220. ctx.Data["Author"] = lastCommit.Author
  221. ctx.HTML(200, tplWikiView)
  222. }
  223. // WikiPages render wiki pages list page
  224. func WikiPages(ctx *context.Context) {
  225. if !ctx.Repo.Repository.HasWiki() {
  226. ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
  227. return
  228. }
  229. ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
  230. ctx.Data["PageIsWiki"] = true
  231. ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(models.UnitTypeWiki) && !ctx.Repo.Repository.IsArchived
  232. wikiRepo, commit, err := findWikiRepoCommit(ctx)
  233. if err != nil {
  234. return
  235. }
  236. entries, err := commit.ListEntries()
  237. if err != nil {
  238. ctx.ServerError("ListEntries", err)
  239. return
  240. }
  241. pages := make([]PageMeta, 0, len(entries))
  242. for _, entry := range entries {
  243. if !entry.IsRegular() {
  244. continue
  245. }
  246. c, err := wikiRepo.GetCommitByPath(entry.Name())
  247. if err != nil {
  248. ctx.ServerError("GetCommit", err)
  249. return
  250. }
  251. wikiName, err := models.WikiFilenameToName(entry.Name())
  252. if err != nil {
  253. if models.IsErrWikiInvalidFileName(err) {
  254. continue
  255. }
  256. ctx.ServerError("WikiFilenameToName", err)
  257. return
  258. }
  259. pages = append(pages, PageMeta{
  260. Name: wikiName,
  261. SubURL: models.WikiNameToSubURL(wikiName),
  262. UpdatedUnix: util.TimeStamp(c.Author.When.Unix()),
  263. })
  264. }
  265. ctx.Data["Pages"] = pages
  266. ctx.HTML(200, tplWikiPages)
  267. }
  268. // WikiRaw outputs raw blob requested by user (image for example)
  269. func WikiRaw(ctx *context.Context) {
  270. wikiRepo, commit, err := findWikiRepoCommit(ctx)
  271. if err != nil {
  272. if wikiRepo != nil {
  273. return
  274. }
  275. }
  276. providedPath := ctx.Params("*")
  277. var entry *git.TreeEntry
  278. if commit != nil {
  279. // Try to find a file with that name
  280. entry, err = findEntryForFile(commit, providedPath)
  281. if err != nil {
  282. ctx.ServerError("findFile", err)
  283. return
  284. }
  285. if entry == nil {
  286. // Try to find a wiki page with that name
  287. if strings.HasSuffix(providedPath, ".md") {
  288. providedPath = providedPath[:len(providedPath)-3]
  289. }
  290. wikiPath := models.WikiNameToFilename(providedPath)
  291. entry, err = findEntryForFile(commit, wikiPath)
  292. if err != nil {
  293. ctx.ServerError("findFile", err)
  294. return
  295. }
  296. }
  297. }
  298. if entry != nil {
  299. if err = ServeBlob(ctx, entry.Blob()); err != nil {
  300. ctx.ServerError("ServeBlob", err)
  301. }
  302. return
  303. }
  304. ctx.NotFound("findEntryForFile", nil)
  305. }
  306. // NewWiki render wiki create page
  307. func NewWiki(ctx *context.Context) {
  308. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  309. ctx.Data["PageIsWiki"] = true
  310. ctx.Data["RequireSimpleMDE"] = true
  311. if !ctx.Repo.Repository.HasWiki() {
  312. ctx.Data["title"] = "Home"
  313. }
  314. ctx.HTML(200, tplWikiNew)
  315. }
  316. // NewWikiPost response for wiki create request
  317. func NewWikiPost(ctx *context.Context, form auth.NewWikiForm) {
  318. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  319. ctx.Data["PageIsWiki"] = true
  320. ctx.Data["RequireSimpleMDE"] = true
  321. if ctx.HasError() {
  322. ctx.HTML(200, tplWikiNew)
  323. return
  324. }
  325. if util.IsEmptyString(form.Title) {
  326. ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplWikiNew, form)
  327. return
  328. }
  329. wikiName := models.NormalizeWikiName(form.Title)
  330. if err := ctx.Repo.Repository.AddWikiPage(ctx.User, wikiName, form.Content, form.Message); err != nil {
  331. if models.IsErrWikiReservedName(err) {
  332. ctx.Data["Err_Title"] = true
  333. ctx.RenderWithErr(ctx.Tr("repo.wiki.reserved_page", wikiName), tplWikiNew, &form)
  334. } else if models.IsErrWikiAlreadyExist(err) {
  335. ctx.Data["Err_Title"] = true
  336. ctx.RenderWithErr(ctx.Tr("repo.wiki.page_already_exists"), tplWikiNew, &form)
  337. } else {
  338. ctx.ServerError("AddWikiPage", err)
  339. }
  340. return
  341. }
  342. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.WikiNameToSubURL(wikiName))
  343. }
  344. // EditWiki render wiki modify page
  345. func EditWiki(ctx *context.Context) {
  346. ctx.Data["PageIsWiki"] = true
  347. ctx.Data["PageIsWikiEdit"] = true
  348. ctx.Data["RequireSimpleMDE"] = true
  349. if !ctx.Repo.Repository.HasWiki() {
  350. ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
  351. return
  352. }
  353. renderWikiPage(ctx, false)
  354. if ctx.Written() {
  355. return
  356. }
  357. ctx.HTML(200, tplWikiNew)
  358. }
  359. // EditWikiPost response for wiki modify request
  360. func EditWikiPost(ctx *context.Context, form auth.NewWikiForm) {
  361. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  362. ctx.Data["PageIsWiki"] = true
  363. ctx.Data["RequireSimpleMDE"] = true
  364. if ctx.HasError() {
  365. ctx.HTML(200, tplWikiNew)
  366. return
  367. }
  368. oldWikiName := models.NormalizeWikiName(ctx.Params(":page"))
  369. newWikiName := models.NormalizeWikiName(form.Title)
  370. if err := ctx.Repo.Repository.EditWikiPage(ctx.User, oldWikiName, newWikiName, form.Content, form.Message); err != nil {
  371. ctx.ServerError("EditWikiPage", err)
  372. return
  373. }
  374. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.WikiNameToSubURL(newWikiName))
  375. }
  376. // DeleteWikiPagePost delete wiki page
  377. func DeleteWikiPagePost(ctx *context.Context) {
  378. wikiName := models.NormalizeWikiName(ctx.Params(":page"))
  379. if len(wikiName) == 0 {
  380. wikiName = "Home"
  381. }
  382. if err := ctx.Repo.Repository.DeleteWikiPage(ctx.User, wikiName); err != nil {
  383. ctx.ServerError("DeleteWikiPage", err)
  384. return
  385. }
  386. ctx.JSON(200, map[string]interface{}{
  387. "redirect": ctx.Repo.RepoLink + "/wiki/",
  388. })
  389. }