1
0

wiki.go 5.7 KB

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