wiki.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 db
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "net/url"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "github.com/unknwon/com"
  14. "github.com/gogs/git-module"
  15. "gogs.io/gogs/internal/conf"
  16. "gogs.io/gogs/internal/repoutil"
  17. "gogs.io/gogs/internal/sync"
  18. )
  19. var wikiWorkingPool = sync.NewExclusivePool()
  20. // ToWikiPageURL formats a string to corresponding wiki URL name.
  21. func ToWikiPageURL(name string) string {
  22. return url.QueryEscape(name)
  23. }
  24. // ToWikiPageName formats a URL back to corresponding wiki page name,
  25. // and removes leading characters './' to prevent changing files
  26. // that are not belong to wiki repository.
  27. func ToWikiPageName(urlString string) string {
  28. name, _ := url.QueryUnescape(urlString)
  29. return strings.ReplaceAll(strings.TrimLeft(path.Clean("/"+name), "/"), "/", " ")
  30. }
  31. // WikiCloneLink returns clone URLs of repository wiki.
  32. //
  33. // Deprecated: Use repoutil.NewCloneLink instead.
  34. func (repo *Repository) WikiCloneLink() (cl *repoutil.CloneLink) {
  35. return repo.cloneLink(true)
  36. }
  37. // WikiPath returns wiki data path by given user and repository name.
  38. func WikiPath(userName, repoName string) string {
  39. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  40. }
  41. func (repo *Repository) WikiPath() string {
  42. return WikiPath(repo.MustOwner().Name, repo.Name)
  43. }
  44. // HasWiki returns true if repository has wiki.
  45. func (repo *Repository) HasWiki() bool {
  46. return com.IsDir(repo.WikiPath())
  47. }
  48. // InitWiki initializes a wiki for repository,
  49. // it does nothing when repository already has wiki.
  50. func (repo *Repository) InitWiki() error {
  51. if repo.HasWiki() {
  52. return nil
  53. }
  54. if err := git.Init(repo.WikiPath(), git.InitOptions{Bare: true}); err != nil {
  55. return fmt.Errorf("init repository: %v", err)
  56. } else if err = createDelegateHooks(repo.WikiPath()); err != nil {
  57. return fmt.Errorf("createDelegateHooks: %v", err)
  58. }
  59. return nil
  60. }
  61. func (repo *Repository) LocalWikiPath() string {
  62. return filepath.Join(conf.Server.AppDataPath, "tmp", "local-wiki", com.ToStr(repo.ID))
  63. }
  64. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  65. func (repo *Repository) UpdateLocalWiki() error {
  66. return UpdateLocalCopyBranch(repo.WikiPath(), repo.LocalWikiPath(), "master", true)
  67. }
  68. func discardLocalWikiChanges(localPath string) error {
  69. return discardLocalRepoBranchChanges(localPath, "master")
  70. }
  71. // updateWikiPage adds new page to repository wiki.
  72. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  73. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  74. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  75. if err = repo.InitWiki(); err != nil {
  76. return fmt.Errorf("InitWiki: %v", err)
  77. }
  78. localPath := repo.LocalWikiPath()
  79. if err = discardLocalWikiChanges(localPath); err != nil {
  80. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  81. } else if err = repo.UpdateLocalWiki(); err != nil {
  82. return fmt.Errorf("UpdateLocalWiki: %v", err)
  83. }
  84. title = ToWikiPageName(title)
  85. filename := path.Join(localPath, title+".md")
  86. // If not a new file, show perform update not create.
  87. if isNew {
  88. if com.IsExist(filename) {
  89. return ErrWikiAlreadyExist{filename}
  90. }
  91. } else {
  92. os.Remove(path.Join(localPath, oldTitle+".md"))
  93. }
  94. // SECURITY: if new file is a symlink to non-exist critical file,
  95. // attack content can be written to the target file (e.g. authorized_keys2)
  96. // as a new page operation.
  97. // So we want to make sure the symlink is removed before write anything.
  98. // The new file we created will be in normal text format.
  99. os.Remove(filename)
  100. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  101. return fmt.Errorf("WriteFile: %v", err)
  102. }
  103. if message == "" {
  104. message = "Update page '" + title + "'"
  105. }
  106. if err = git.Add(localPath, git.AddOptions{All: true}); err != nil {
  107. return fmt.Errorf("add all changes: %v", err)
  108. } else if err = git.CreateCommit(localPath, doer.NewGitSig(), message); err != nil {
  109. return fmt.Errorf("commit changes: %v", err)
  110. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  111. return fmt.Errorf("push: %v", err)
  112. }
  113. return nil
  114. }
  115. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  116. return repo.updateWikiPage(doer, "", title, content, message, true)
  117. }
  118. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  119. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  120. }
  121. func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
  122. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  123. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  124. localPath := repo.LocalWikiPath()
  125. if err = discardLocalWikiChanges(localPath); err != nil {
  126. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  127. } else if err = repo.UpdateLocalWiki(); err != nil {
  128. return fmt.Errorf("UpdateLocalWiki: %v", err)
  129. }
  130. title = ToWikiPageName(title)
  131. filename := path.Join(localPath, title+".md")
  132. os.Remove(filename)
  133. message := "Delete page '" + title + "'"
  134. if err = git.Add(localPath, git.AddOptions{All: true}); err != nil {
  135. return fmt.Errorf("add all changes: %v", err)
  136. } else if err = git.CreateCommit(localPath, doer.NewGitSig(), message); err != nil {
  137. return fmt.Errorf("commit changes: %v", err)
  138. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  139. return fmt.Errorf("push: %v", err)
  140. }
  141. return nil
  142. }