1
0

wiki.go 6.0 KB

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