wiki.go 5.6 KB

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