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 15 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
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
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. tplWikiRevision base.TplName = "repo/wiki/revision"
  25. tplWikiNew base.TplName = "repo/wiki/new"
  26. tplWikiPages base.TplName = "repo/wiki/pages"
  27. )
  28. // MustEnableWiki check if wiki is enabled, if external then redirect
  29. func MustEnableWiki(ctx *context.Context) {
  30. if !ctx.Repo.CanRead(models.UnitTypeWiki) &&
  31. !ctx.Repo.CanRead(models.UnitTypeExternalWiki) {
  32. if log.IsTrace() {
  33. log.Trace("Permission Denied: User %-v cannot read %-v or %-v of repo %-v\n"+
  34. "User in repo has Permissions: %-+v",
  35. ctx.User,
  36. models.UnitTypeWiki,
  37. models.UnitTypeExternalWiki,
  38. ctx.Repo.Repository,
  39. ctx.Repo.Permission)
  40. }
  41. ctx.NotFound("MustEnableWiki", nil)
  42. return
  43. }
  44. unit, err := ctx.Repo.Repository.GetUnit(models.UnitTypeExternalWiki)
  45. if err == nil {
  46. ctx.Redirect(unit.ExternalWikiConfig().ExternalWikiURL)
  47. return
  48. }
  49. }
  50. // PageMeta wiki page meat information
  51. type PageMeta struct {
  52. Name string
  53. SubURL string
  54. UpdatedUnix util.TimeStamp
  55. }
  56. // findEntryForFile finds the tree entry for a target filepath.
  57. func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error) {
  58. entries, err := commit.ListEntries()
  59. if err != nil {
  60. return nil, err
  61. }
  62. for _, entry := range entries {
  63. if entry.IsRegular() && entry.Name() == target {
  64. return entry, nil
  65. }
  66. }
  67. return nil, nil
  68. }
  69. func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
  70. wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
  71. if err != nil {
  72. ctx.ServerError("OpenRepository", err)
  73. return nil, nil, err
  74. }
  75. commit, err := wikiRepo.GetBranchCommit("master")
  76. if err != nil {
  77. return wikiRepo, nil, err
  78. }
  79. return wikiRepo, commit, nil
  80. }
  81. // wikiContentsByEntry returns the contents of the wiki page referenced by the
  82. // given tree entry. Writes to ctx if an error occurs.
  83. func wikiContentsByEntry(ctx *context.Context, entry *git.TreeEntry) []byte {
  84. reader, err := entry.Blob().DataAsync()
  85. if err != nil {
  86. ctx.ServerError("Blob.Data", err)
  87. return nil
  88. }
  89. defer reader.Close()
  90. content, err := ioutil.ReadAll(reader)
  91. if err != nil {
  92. ctx.ServerError("ReadAll", err)
  93. return nil
  94. }
  95. return content
  96. }
  97. // wikiContentsByName returns the contents of a wiki page, along with a boolean
  98. // indicating whether the page exists. Writes to ctx if an error occurs.
  99. func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName string) ([]byte, *git.TreeEntry, string, bool) {
  100. var entry *git.TreeEntry
  101. var err error
  102. pageFilename := models.WikiNameToFilename(wikiName)
  103. if entry, err = findEntryForFile(commit, pageFilename); err != nil {
  104. ctx.ServerError("findEntryForFile", err)
  105. return nil, nil, "", false
  106. } else if entry == nil {
  107. return nil, nil, "", true
  108. }
  109. return wikiContentsByEntry(ctx, entry), entry, pageFilename, false
  110. }
  111. func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
  112. wikiRepo, commit, err := findWikiRepoCommit(ctx)
  113. if err != nil {
  114. if !git.IsErrNotExist(err) {
  115. ctx.ServerError("GetBranchCommit", err)
  116. }
  117. return nil, nil
  118. }
  119. // Get page list.
  120. entries, err := commit.ListEntries()
  121. if err != nil {
  122. ctx.ServerError("ListEntries", err)
  123. return nil, nil
  124. }
  125. pages := make([]PageMeta, 0, len(entries))
  126. for _, entry := range entries {
  127. if !entry.IsRegular() {
  128. continue
  129. }
  130. wikiName, err := models.WikiFilenameToName(entry.Name())
  131. if err != nil {
  132. if models.IsErrWikiInvalidFileName(err) {
  133. continue
  134. }
  135. ctx.ServerError("WikiFilenameToName", err)
  136. return nil, nil
  137. } else if wikiName == "_Sidebar" || wikiName == "_Footer" {
  138. continue
  139. }
  140. pages = append(pages, PageMeta{
  141. Name: wikiName,
  142. SubURL: models.WikiNameToSubURL(wikiName),
  143. })
  144. }
  145. ctx.Data["Pages"] = pages
  146. // get requested pagename
  147. pageName := models.NormalizeWikiName(ctx.Params(":page"))
  148. if len(pageName) == 0 {
  149. pageName = "Home"
  150. }
  151. ctx.Data["PageURL"] = models.WikiNameToSubURL(pageName)
  152. ctx.Data["old_title"] = pageName
  153. ctx.Data["Title"] = pageName
  154. ctx.Data["title"] = pageName
  155. ctx.Data["RequireHighlightJS"] = true
  156. //lookup filename in wiki - get filecontent, gitTree entry , real filename
  157. data, entry, pageFilename, noEntry := wikiContentsByName(ctx, commit, pageName)
  158. if noEntry {
  159. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
  160. }
  161. if entry == nil || ctx.Written() {
  162. return nil, nil
  163. }
  164. sidebarContent, _, _, _ := wikiContentsByName(ctx, commit, "_Sidebar")
  165. if ctx.Written() {
  166. return nil, nil
  167. }
  168. footerContent, _, _, _ := wikiContentsByName(ctx, commit, "_Footer")
  169. if ctx.Written() {
  170. return nil, nil
  171. }
  172. metas := ctx.Repo.Repository.ComposeMetas()
  173. ctx.Data["content"] = markdown.RenderWiki(data, ctx.Repo.RepoLink, metas)
  174. ctx.Data["sidebarPresent"] = sidebarContent != nil
  175. ctx.Data["sidebarContent"] = markdown.RenderWiki(sidebarContent, ctx.Repo.RepoLink, metas)
  176. ctx.Data["footerPresent"] = footerContent != nil
  177. ctx.Data["footerContent"] = markdown.RenderWiki(footerContent, ctx.Repo.RepoLink, metas)
  178. // get commit count - wiki revisions
  179. commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename)
  180. ctx.Data["CommitCount"] = commitsCount
  181. return wikiRepo, entry
  182. }
  183. func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
  184. wikiRepo, commit, err := findWikiRepoCommit(ctx)
  185. if err != nil {
  186. if !git.IsErrNotExist(err) {
  187. ctx.ServerError("GetBranchCommit", err)
  188. }
  189. return nil, nil
  190. }
  191. // get requested pagename
  192. pageName := models.NormalizeWikiName(ctx.Params(":page"))
  193. if len(pageName) == 0 {
  194. pageName = "Home"
  195. }
  196. ctx.Data["PageURL"] = models.WikiNameToSubURL(pageName)
  197. ctx.Data["old_title"] = pageName
  198. ctx.Data["Title"] = pageName
  199. ctx.Data["title"] = pageName
  200. ctx.Data["RequireHighlightJS"] = true
  201. //lookup filename in wiki - get filecontent, gitTree entry , real filename
  202. data, entry, pageFilename, noEntry := wikiContentsByName(ctx, commit, pageName)
  203. if noEntry {
  204. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
  205. }
  206. if entry == nil || ctx.Written() {
  207. return nil, nil
  208. }
  209. ctx.Data["content"] = string(data)
  210. ctx.Data["sidebarPresent"] = false
  211. ctx.Data["sidebarContent"] = ""
  212. ctx.Data["footerPresent"] = false
  213. ctx.Data["footerContent"] = ""
  214. // get commit count - wiki revisions
  215. commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename)
  216. ctx.Data["CommitCount"] = commitsCount
  217. // get page
  218. page := ctx.QueryInt("page")
  219. if page <= 1 {
  220. page = 1
  221. }
  222. // get Commit Count
  223. commitsHistory, err := wikiRepo.CommitsByFileAndRangeNoFollow("master", pageFilename, page)
  224. if err != nil {
  225. ctx.ServerError("CommitsByFileAndRangeNoFollow", err)
  226. return nil, nil
  227. }
  228. commitsHistory = models.ValidateCommitsWithEmails(commitsHistory)
  229. commitsHistory = models.ParseCommitsWithSignature(commitsHistory)
  230. ctx.Data["Commits"] = commitsHistory
  231. pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
  232. pager.SetDefaultParams(ctx)
  233. ctx.Data["Page"] = pager
  234. return wikiRepo, entry
  235. }
  236. func renderEditPage(ctx *context.Context) {
  237. _, commit, err := findWikiRepoCommit(ctx)
  238. if err != nil {
  239. if !git.IsErrNotExist(err) {
  240. ctx.ServerError("GetBranchCommit", err)
  241. }
  242. return
  243. }
  244. // get requested pagename
  245. pageName := models.NormalizeWikiName(ctx.Params(":page"))
  246. if len(pageName) == 0 {
  247. pageName = "Home"
  248. }
  249. ctx.Data["PageURL"] = models.WikiNameToSubURL(pageName)
  250. ctx.Data["old_title"] = pageName
  251. ctx.Data["Title"] = pageName
  252. ctx.Data["title"] = pageName
  253. ctx.Data["RequireHighlightJS"] = true
  254. //lookup filename in wiki - get filecontent, gitTree entry , real filename
  255. data, entry, _, noEntry := wikiContentsByName(ctx, commit, pageName)
  256. if noEntry {
  257. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
  258. }
  259. if entry == nil || ctx.Written() {
  260. return
  261. }
  262. ctx.Data["content"] = string(data)
  263. ctx.Data["sidebarPresent"] = false
  264. ctx.Data["sidebarContent"] = ""
  265. ctx.Data["footerPresent"] = false
  266. ctx.Data["footerContent"] = ""
  267. }
  268. // Wiki renders single wiki page
  269. func Wiki(ctx *context.Context) {
  270. ctx.Data["PageIsWiki"] = true
  271. ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(models.UnitTypeWiki) && !ctx.Repo.Repository.IsArchived
  272. if !ctx.Repo.Repository.HasWiki() {
  273. ctx.Data["Title"] = ctx.Tr("repo.wiki")
  274. ctx.HTML(200, tplWikiStart)
  275. return
  276. }
  277. wikiRepo, entry := renderViewPage(ctx)
  278. if ctx.Written() {
  279. return
  280. }
  281. if entry == nil {
  282. ctx.Data["Title"] = ctx.Tr("repo.wiki")
  283. ctx.HTML(200, tplWikiStart)
  284. return
  285. }
  286. wikiPath := entry.Name()
  287. if markup.Type(wikiPath) != markdown.MarkupName {
  288. ext := strings.ToUpper(filepath.Ext(wikiPath))
  289. ctx.Data["FormatWarning"] = fmt.Sprintf("%s rendering is not supported at the moment. Rendered as Markdown.", ext)
  290. }
  291. // Get last change information.
  292. lastCommit, err := wikiRepo.GetCommitByPath(wikiPath)
  293. if err != nil {
  294. ctx.ServerError("GetCommitByPath", err)
  295. return
  296. }
  297. ctx.Data["Author"] = lastCommit.Author
  298. ctx.HTML(200, tplWikiView)
  299. }
  300. // WikiRevision renders file revision list of wiki page
  301. func WikiRevision(ctx *context.Context) {
  302. ctx.Data["PageIsWiki"] = true
  303. ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(models.UnitTypeWiki) && !ctx.Repo.Repository.IsArchived
  304. if !ctx.Repo.Repository.HasWiki() {
  305. ctx.Data["Title"] = ctx.Tr("repo.wiki")
  306. ctx.HTML(200, tplWikiStart)
  307. return
  308. }
  309. wikiRepo, entry := renderRevisionPage(ctx)
  310. if ctx.Written() {
  311. return
  312. }
  313. if entry == nil {
  314. ctx.Data["Title"] = ctx.Tr("repo.wiki")
  315. ctx.HTML(200, tplWikiStart)
  316. return
  317. }
  318. // Get last change information.
  319. wikiPath := entry.Name()
  320. lastCommit, err := wikiRepo.GetCommitByPath(wikiPath)
  321. if err != nil {
  322. ctx.ServerError("GetCommitByPath", err)
  323. return
  324. }
  325. ctx.Data["Author"] = lastCommit.Author
  326. ctx.HTML(200, tplWikiRevision)
  327. }
  328. // WikiPages render wiki pages list page
  329. func WikiPages(ctx *context.Context) {
  330. if !ctx.Repo.Repository.HasWiki() {
  331. ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
  332. return
  333. }
  334. ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
  335. ctx.Data["PageIsWiki"] = true
  336. ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(models.UnitTypeWiki) && !ctx.Repo.Repository.IsArchived
  337. wikiRepo, commit, err := findWikiRepoCommit(ctx)
  338. if err != nil {
  339. return
  340. }
  341. entries, err := commit.ListEntries()
  342. if err != nil {
  343. ctx.ServerError("ListEntries", err)
  344. return
  345. }
  346. pages := make([]PageMeta, 0, len(entries))
  347. for _, entry := range entries {
  348. if !entry.IsRegular() {
  349. continue
  350. }
  351. c, err := wikiRepo.GetCommitByPath(entry.Name())
  352. if err != nil {
  353. ctx.ServerError("GetCommit", err)
  354. return
  355. }
  356. wikiName, err := models.WikiFilenameToName(entry.Name())
  357. if err != nil {
  358. if models.IsErrWikiInvalidFileName(err) {
  359. continue
  360. }
  361. ctx.ServerError("WikiFilenameToName", err)
  362. return
  363. }
  364. pages = append(pages, PageMeta{
  365. Name: wikiName,
  366. SubURL: models.WikiNameToSubURL(wikiName),
  367. UpdatedUnix: util.TimeStamp(c.Author.When.Unix()),
  368. })
  369. }
  370. ctx.Data["Pages"] = pages
  371. ctx.HTML(200, tplWikiPages)
  372. }
  373. // WikiRaw outputs raw blob requested by user (image for example)
  374. func WikiRaw(ctx *context.Context) {
  375. wikiRepo, commit, err := findWikiRepoCommit(ctx)
  376. if err != nil {
  377. if wikiRepo != nil {
  378. return
  379. }
  380. }
  381. providedPath := ctx.Params("*")
  382. var entry *git.TreeEntry
  383. if commit != nil {
  384. // Try to find a file with that name
  385. entry, err = findEntryForFile(commit, providedPath)
  386. if err != nil {
  387. ctx.ServerError("findFile", err)
  388. return
  389. }
  390. if entry == nil {
  391. // Try to find a wiki page with that name
  392. if strings.HasSuffix(providedPath, ".md") {
  393. providedPath = providedPath[:len(providedPath)-3]
  394. }
  395. wikiPath := models.WikiNameToFilename(providedPath)
  396. entry, err = findEntryForFile(commit, wikiPath)
  397. if err != nil {
  398. ctx.ServerError("findFile", err)
  399. return
  400. }
  401. }
  402. }
  403. if entry != nil {
  404. if err = ServeBlob(ctx, entry.Blob()); err != nil {
  405. ctx.ServerError("ServeBlob", err)
  406. }
  407. return
  408. }
  409. ctx.NotFound("findEntryForFile", nil)
  410. }
  411. // NewWiki render wiki create page
  412. func NewWiki(ctx *context.Context) {
  413. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  414. ctx.Data["PageIsWiki"] = true
  415. ctx.Data["RequireSimpleMDE"] = true
  416. if !ctx.Repo.Repository.HasWiki() {
  417. ctx.Data["title"] = "Home"
  418. }
  419. ctx.HTML(200, tplWikiNew)
  420. }
  421. // NewWikiPost response for wiki create request
  422. func NewWikiPost(ctx *context.Context, form auth.NewWikiForm) {
  423. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  424. ctx.Data["PageIsWiki"] = true
  425. ctx.Data["RequireSimpleMDE"] = true
  426. if ctx.HasError() {
  427. ctx.HTML(200, tplWikiNew)
  428. return
  429. }
  430. if util.IsEmptyString(form.Title) {
  431. ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplWikiNew, form)
  432. return
  433. }
  434. wikiName := models.NormalizeWikiName(form.Title)
  435. if err := ctx.Repo.Repository.AddWikiPage(ctx.User, wikiName, form.Content, form.Message); err != nil {
  436. if models.IsErrWikiReservedName(err) {
  437. ctx.Data["Err_Title"] = true
  438. ctx.RenderWithErr(ctx.Tr("repo.wiki.reserved_page", wikiName), tplWikiNew, &form)
  439. } else if models.IsErrWikiAlreadyExist(err) {
  440. ctx.Data["Err_Title"] = true
  441. ctx.RenderWithErr(ctx.Tr("repo.wiki.page_already_exists"), tplWikiNew, &form)
  442. } else {
  443. ctx.ServerError("AddWikiPage", err)
  444. }
  445. return
  446. }
  447. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.WikiNameToSubURL(wikiName))
  448. }
  449. // EditWiki render wiki modify page
  450. func EditWiki(ctx *context.Context) {
  451. ctx.Data["PageIsWiki"] = true
  452. ctx.Data["PageIsWikiEdit"] = true
  453. ctx.Data["RequireSimpleMDE"] = true
  454. if !ctx.Repo.Repository.HasWiki() {
  455. ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
  456. return
  457. }
  458. renderEditPage(ctx)
  459. if ctx.Written() {
  460. return
  461. }
  462. ctx.HTML(200, tplWikiNew)
  463. }
  464. // EditWikiPost response for wiki modify request
  465. func EditWikiPost(ctx *context.Context, form auth.NewWikiForm) {
  466. ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
  467. ctx.Data["PageIsWiki"] = true
  468. ctx.Data["RequireSimpleMDE"] = true
  469. if ctx.HasError() {
  470. ctx.HTML(200, tplWikiNew)
  471. return
  472. }
  473. oldWikiName := models.NormalizeWikiName(ctx.Params(":page"))
  474. newWikiName := models.NormalizeWikiName(form.Title)
  475. if err := ctx.Repo.Repository.EditWikiPage(ctx.User, oldWikiName, newWikiName, form.Content, form.Message); err != nil {
  476. ctx.ServerError("EditWikiPage", err)
  477. return
  478. }
  479. ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.WikiNameToSubURL(newWikiName))
  480. }
  481. // DeleteWikiPagePost delete wiki page
  482. func DeleteWikiPagePost(ctx *context.Context) {
  483. wikiName := models.NormalizeWikiName(ctx.Params(":page"))
  484. if len(wikiName) == 0 {
  485. wikiName = "Home"
  486. }
  487. if err := ctx.Repo.Repository.DeleteWikiPage(ctx.User, wikiName); err != nil {
  488. ctx.ServerError("DeleteWikiPage", err)
  489. return
  490. }
  491. ctx.JSON(200, map[string]interface{}{
  492. "redirect": ctx.Repo.RepoLink + "/wiki/",
  493. })
  494. }