wiki.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package database
  2. import (
  3. "net/url"
  4. "os"
  5. "path"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/cockroachdb/errors"
  11. "github.com/gogs/git-module"
  12. "gogs.io/gogs/internal/conf"
  13. "gogs.io/gogs/internal/osutil"
  14. "gogs.io/gogs/internal/pathutil"
  15. "gogs.io/gogs/internal/repoutil"
  16. "gogs.io/gogs/internal/sync"
  17. )
  18. var wikiWorkingPool = sync.NewExclusivePool()
  19. // WikiBranch returns the branch name used by the wiki repository. It checks if
  20. // "main" branch exists, otherwise falls back to "master".
  21. func WikiBranch(repoPath string) string {
  22. if git.RepoHasBranch(repoPath, "main") {
  23. return "main"
  24. }
  25. return "master"
  26. }
  27. // ToWikiPageURL formats a string to corresponding wiki URL name.
  28. func ToWikiPageURL(name string) string {
  29. return url.QueryEscape(name)
  30. }
  31. // ToWikiPageName formats a URL back to corresponding wiki page name. It enforces
  32. // single-level hierarchy by replacing all "/" with spaces.
  33. func ToWikiPageName(urlString string) string {
  34. name, _ := url.QueryUnescape(urlString)
  35. name = pathutil.Clean(name)
  36. return strings.ReplaceAll(name, "/", " ")
  37. }
  38. // WikiCloneLink returns clone URLs of repository wiki.
  39. //
  40. // Deprecated: Use repoutil.NewCloneLink instead.
  41. func (r *Repository) WikiCloneLink() (cl *repoutil.CloneLink) {
  42. return r.cloneLink(true)
  43. }
  44. // WikiPath returns wiki data path by given user and repository name.
  45. func WikiPath(userName, repoName string) string {
  46. return filepath.Join(repoutil.UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  47. }
  48. func (r *Repository) WikiPath() string {
  49. return WikiPath(r.MustOwner().Name, r.Name)
  50. }
  51. // HasWiki returns true if repository has wiki.
  52. func (r *Repository) HasWiki() bool {
  53. return osutil.IsDir(r.WikiPath())
  54. }
  55. // InitWiki initializes a wiki for repository,
  56. // it does nothing when repository already has wiki.
  57. func (r *Repository) InitWiki() error {
  58. if r.HasWiki() {
  59. return nil
  60. }
  61. if err := git.Init(r.WikiPath(), git.InitOptions{Bare: true}); err != nil {
  62. return errors.Newf("init repository: %v", err)
  63. } else if err = createDelegateHooks(r.WikiPath()); err != nil {
  64. return errors.Newf("createDelegateHooks: %v", err)
  65. }
  66. return nil
  67. }
  68. func (r *Repository) LocalWikiPath() string {
  69. return filepath.Join(conf.Server.AppDataPath, "tmp", "local-wiki", strconv.FormatInt(r.ID, 10))
  70. }
  71. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  72. func (r *Repository) UpdateLocalWiki() error {
  73. wikiPath := r.WikiPath()
  74. return UpdateLocalCopyBranch(wikiPath, r.LocalWikiPath(), WikiBranch(wikiPath), true)
  75. }
  76. func discardLocalWikiChanges(localPath string) error {
  77. return discardLocalRepoBranchChanges(localPath, WikiBranch(localPath))
  78. }
  79. // updateWikiPage adds new page to repository wiki.
  80. func (r *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) error {
  81. wikiWorkingPool.CheckIn(strconv.FormatInt(r.ID, 10))
  82. defer wikiWorkingPool.CheckOut(strconv.FormatInt(r.ID, 10))
  83. if err := r.InitWiki(); err != nil {
  84. return errors.Newf("InitWiki: %v", err)
  85. }
  86. localPath := r.LocalWikiPath()
  87. if err := discardLocalWikiChanges(localPath); err != nil {
  88. return errors.Newf("discardLocalWikiChanges: %v", err)
  89. } else if err = r.UpdateLocalWiki(); err != nil {
  90. return errors.Newf("UpdateLocalWiki: %v", err)
  91. }
  92. title = ToWikiPageName(title)
  93. filename := path.Join(localPath, title+".md")
  94. // If not a new file, show perform update not create.
  95. if isNew {
  96. if osutil.Exist(filename) {
  97. return ErrWikiAlreadyExist{filename}
  98. }
  99. } else {
  100. oldTitle = ToWikiPageName(oldTitle)
  101. _ = os.Remove(path.Join(localPath, oldTitle+".md"))
  102. }
  103. // SECURITY: if new file is a symlink to non-exist critical file,
  104. // attack content can be written to the target file (e.g. authorized_keys2)
  105. // as a new page operation.
  106. // So we want to make sure the symlink is removed before write anything.
  107. // The new file we created will be in normal text format.
  108. _ = os.Remove(filename)
  109. if err := os.WriteFile(filename, []byte(content), 0o666); err != nil {
  110. return errors.Newf("WriteFile: %v", err)
  111. }
  112. if message == "" {
  113. message = "Update page '" + title + "'"
  114. }
  115. if err := git.Add(localPath, git.AddOptions{All: true}); err != nil {
  116. return errors.Newf("add all changes: %v", err)
  117. }
  118. err := git.CreateCommit(
  119. localPath,
  120. &git.Signature{
  121. Name: doer.DisplayName(),
  122. Email: doer.Email,
  123. When: time.Now(),
  124. },
  125. message,
  126. )
  127. if err != nil {
  128. return errors.Newf("commit changes: %v", err)
  129. } else if err = git.Push(localPath, "origin", WikiBranch(localPath)); err != nil {
  130. return errors.Newf("push: %v", err)
  131. }
  132. return nil
  133. }
  134. func (r *Repository) AddWikiPage(doer *User, title, content, message string) error {
  135. return r.updateWikiPage(doer, "", title, content, message, true)
  136. }
  137. func (r *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  138. return r.updateWikiPage(doer, oldTitle, title, content, message, false)
  139. }
  140. func (r *Repository) DeleteWikiPage(doer *User, title string) (err error) {
  141. wikiWorkingPool.CheckIn(strconv.FormatInt(r.ID, 10))
  142. defer wikiWorkingPool.CheckOut(strconv.FormatInt(r.ID, 10))
  143. localPath := r.LocalWikiPath()
  144. if err = discardLocalWikiChanges(localPath); err != nil {
  145. return errors.Newf("discardLocalWikiChanges: %v", err)
  146. } else if err = r.UpdateLocalWiki(); err != nil {
  147. return errors.Newf("UpdateLocalWiki: %v", err)
  148. }
  149. title = ToWikiPageName(title)
  150. _ = os.Remove(path.Join(localPath, title+".md"))
  151. message := "Delete page '" + title + "'"
  152. if err = git.Add(localPath, git.AddOptions{All: true}); err != nil {
  153. return errors.Newf("add all changes: %v", err)
  154. }
  155. err = git.CreateCommit(
  156. localPath,
  157. &git.Signature{
  158. Name: doer.DisplayName(),
  159. Email: doer.Email,
  160. When: time.Now(),
  161. },
  162. message,
  163. )
  164. if err != nil {
  165. return errors.Newf("commit changes: %v", err)
  166. } else if err = git.Push(localPath, "origin", WikiBranch(localPath)); err != nil {
  167. return errors.Newf("push: %v", err)
  168. }
  169. return nil
  170. }