wiki.go 5.6 KB

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