wiki.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. "net/url"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "time"
  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. // WikiBranch returns the branch name used by the wiki repository. It checks if
  21. // "main" branch exists, otherwise falls back to "master".
  22. func WikiBranch(repoPath string) string {
  23. if git.RepoHasBranch(repoPath, "main") {
  24. return "main"
  25. }
  26. return "master"
  27. }
  28. // ToWikiPageURL formats a string to corresponding wiki URL name.
  29. func ToWikiPageURL(name string) string {
  30. return url.QueryEscape(name)
  31. }
  32. // ToWikiPageName formats a URL back to corresponding wiki page name,
  33. // and removes leading characters './' to prevent changing files
  34. // that are not belong to wiki repository.
  35. func ToWikiPageName(urlString string) string {
  36. name, _ := url.QueryUnescape(urlString)
  37. return strings.ReplaceAll(strings.TrimLeft(path.Clean("/"+name), "/"), "/", " ")
  38. }
  39. // WikiCloneLink returns clone URLs of repository wiki.
  40. //
  41. // Deprecated: Use repoutil.NewCloneLink instead.
  42. func (repo *Repository) WikiCloneLink() (cl *repoutil.CloneLink) {
  43. return repo.cloneLink(true)
  44. }
  45. // WikiPath returns wiki data path by given user and repository name.
  46. func WikiPath(userName, repoName string) string {
  47. return filepath.Join(repoutil.UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  48. }
  49. func (repo *Repository) WikiPath() string {
  50. return WikiPath(repo.MustOwner().Name, repo.Name)
  51. }
  52. // HasWiki returns true if repository has wiki.
  53. func (repo *Repository) HasWiki() bool {
  54. return com.IsDir(repo.WikiPath())
  55. }
  56. // InitWiki initializes a wiki for repository,
  57. // it does nothing when repository already has wiki.
  58. func (repo *Repository) InitWiki() error {
  59. if repo.HasWiki() {
  60. return nil
  61. }
  62. if err := git.Init(repo.WikiPath(), git.InitOptions{Bare: true}); err != nil {
  63. return fmt.Errorf("init repository: %v", err)
  64. } else if err = createDelegateHooks(repo.WikiPath()); err != nil {
  65. return fmt.Errorf("createDelegateHooks: %v", err)
  66. }
  67. return nil
  68. }
  69. func (repo *Repository) LocalWikiPath() string {
  70. return filepath.Join(conf.Server.AppDataPath, "tmp", "local-wiki", com.ToStr(repo.ID))
  71. }
  72. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  73. func (repo *Repository) UpdateLocalWiki() error {
  74. wikiPath := repo.WikiPath()
  75. return UpdateLocalCopyBranch(wikiPath, repo.LocalWikiPath(), WikiBranch(wikiPath), true)
  76. }
  77. func discardLocalWikiChanges(localPath string) error {
  78. return discardLocalRepoBranchChanges(localPath, WikiBranch(localPath))
  79. }
  80. // updateWikiPage adds new page to repository wiki.
  81. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  82. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  83. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  84. if err = repo.InitWiki(); err != nil {
  85. return fmt.Errorf("InitWiki: %v", err)
  86. }
  87. localPath := repo.LocalWikiPath()
  88. if err = discardLocalWikiChanges(localPath); err != nil {
  89. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  90. } else if err = repo.UpdateLocalWiki(); err != nil {
  91. return fmt.Errorf("UpdateLocalWiki: %v", err)
  92. }
  93. title = ToWikiPageName(title)
  94. filename := path.Join(localPath, title+".md")
  95. // If not a new file, show perform update not create.
  96. if isNew {
  97. if com.IsExist(filename) {
  98. return ErrWikiAlreadyExist{filename}
  99. }
  100. } else {
  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), 0666); err != nil {
  110. return fmt.Errorf("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 fmt.Errorf("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 fmt.Errorf("commit changes: %v", err)
  129. } else if err = git.Push(localPath, "origin", WikiBranch(localPath)); err != nil {
  130. return fmt.Errorf("push: %v", err)
  131. }
  132. return nil
  133. }
  134. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  135. return repo.updateWikiPage(doer, "", title, content, message, true)
  136. }
  137. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  138. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  139. }
  140. func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
  141. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  142. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  143. localPath := repo.LocalWikiPath()
  144. if err = discardLocalWikiChanges(localPath); err != nil {
  145. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  146. } else if err = repo.UpdateLocalWiki(); err != nil {
  147. return fmt.Errorf("UpdateLocalWiki: %v", err)
  148. }
  149. title = ToWikiPageName(title)
  150. filename := path.Join(localPath, title+".md")
  151. os.Remove(filename)
  152. message := "Delete page '" + title + "'"
  153. if err = git.Add(localPath, git.AddOptions{All: true}); err != nil {
  154. return fmt.Errorf("add all changes: %v", err)
  155. }
  156. err = git.CreateCommit(
  157. localPath,
  158. &git.Signature{
  159. Name: doer.DisplayName(),
  160. Email: doer.Email,
  161. When: time.Now(),
  162. },
  163. message,
  164. )
  165. if err != nil {
  166. return fmt.Errorf("commit changes: %v", err)
  167. } else if err = git.Push(localPath, "origin", WikiBranch(localPath)); err != nil {
  168. return fmt.Errorf("push: %v", err)
  169. }
  170. return nil
  171. }