wiki.go 5.8 KB

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