1
0

wiki.go 5.4 KB

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