Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

wiki.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 models
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "net/url"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "github.com/Unknwon/com"
  14. "code.gitea.io/git"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/sync"
  17. )
  18. var (
  19. reservedWikiNames = []string{"_pages", "_new", "_edit"}
  20. wikiWorkingPool = sync.NewExclusivePool()
  21. )
  22. // NormalizeWikiName normalizes a wiki name
  23. func NormalizeWikiName(name string) string {
  24. return strings.Replace(name, "-", " ", -1)
  25. }
  26. // WikiNameToSubURL converts a wiki name to its corresponding sub-URL.
  27. func WikiNameToSubURL(name string) string {
  28. return url.QueryEscape(strings.Replace(name, " ", "-", -1))
  29. }
  30. // WikiNameToFilename converts a wiki name to its corresponding filename.
  31. func WikiNameToFilename(name string) string {
  32. name = strings.Replace(name, " ", "-", -1)
  33. return url.QueryEscape(name) + ".md"
  34. }
  35. // WikiFilenameToName converts a wiki filename to its corresponding page name.
  36. func WikiFilenameToName(filename string) (string, error) {
  37. if !strings.HasSuffix(filename, ".md") {
  38. return "", fmt.Errorf("Invalid wiki filename: %s", filename)
  39. }
  40. basename := filename[:len(filename)-3]
  41. unescaped, err := url.QueryUnescape(basename)
  42. if err != nil {
  43. return "", err
  44. }
  45. return NormalizeWikiName(unescaped), nil
  46. }
  47. // WikiCloneLink returns clone URLs of repository wiki.
  48. func (repo *Repository) WikiCloneLink() *CloneLink {
  49. return repo.cloneLink(true)
  50. }
  51. // WikiPath returns wiki data path by given user and repository name.
  52. func WikiPath(userName, repoName string) string {
  53. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  54. }
  55. // WikiPath returns wiki data path for given repository.
  56. func (repo *Repository) WikiPath() string {
  57. return WikiPath(repo.MustOwner().Name, repo.Name)
  58. }
  59. // HasWiki returns true if repository has wiki.
  60. func (repo *Repository) HasWiki() bool {
  61. return com.IsDir(repo.WikiPath())
  62. }
  63. // InitWiki initializes a wiki for repository,
  64. // it does nothing when repository already has wiki.
  65. func (repo *Repository) InitWiki() error {
  66. if repo.HasWiki() {
  67. return nil
  68. }
  69. if err := git.InitRepository(repo.WikiPath(), true); err != nil {
  70. return fmt.Errorf("InitRepository: %v", err)
  71. } else if err = createDelegateHooks(repo.WikiPath()); err != nil {
  72. return fmt.Errorf("createDelegateHooks: %v", err)
  73. }
  74. return nil
  75. }
  76. // LocalWikiPath returns the path to the local wiki repository (?).
  77. func (repo *Repository) LocalWikiPath() string {
  78. return path.Join(setting.AppDataPath, "tmp/local-wiki", com.ToStr(repo.ID))
  79. }
  80. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  81. func (repo *Repository) updateLocalWiki() error {
  82. // Don't pass branch name here because it fails to clone and
  83. // checkout to a specific branch when wiki is an empty repository.
  84. var branch = ""
  85. if com.IsExist(repo.LocalWikiPath()) {
  86. branch = "master"
  87. }
  88. return UpdateLocalCopyBranch(repo.WikiPath(), repo.LocalWikiPath(), branch)
  89. }
  90. func discardLocalWikiChanges(localPath string) error {
  91. return discardLocalRepoBranchChanges(localPath, "master")
  92. }
  93. // nameAllowed checks if a wiki name is allowed
  94. func nameAllowed(name string) error {
  95. for _, reservedName := range reservedWikiNames {
  96. if name == reservedName {
  97. return ErrWikiReservedName{name}
  98. }
  99. }
  100. return nil
  101. }
  102. // updateWikiPage adds a new page to the repository wiki.
  103. func (repo *Repository) updateWikiPage(doer *User, oldWikiName, newWikiName, content, message string, isNew bool) (err error) {
  104. if err = nameAllowed(newWikiName); err != nil {
  105. return err
  106. }
  107. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  108. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  109. if err = repo.InitWiki(); err != nil {
  110. return fmt.Errorf("InitWiki: %v", err)
  111. }
  112. localPath := repo.LocalWikiPath()
  113. if err = discardLocalWikiChanges(localPath); err != nil {
  114. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  115. } else if err = repo.updateLocalWiki(); err != nil {
  116. return fmt.Errorf("UpdateLocalWiki: %v", err)
  117. }
  118. newWikiPath := path.Join(localPath, WikiNameToFilename(newWikiName))
  119. // If not a new file, show perform update not create.
  120. if isNew {
  121. if com.IsExist(newWikiPath) {
  122. return ErrWikiAlreadyExist{newWikiPath}
  123. }
  124. } else {
  125. oldWikiPath := path.Join(localPath, WikiNameToFilename(oldWikiName))
  126. if err := os.Remove(oldWikiPath); err != nil {
  127. return fmt.Errorf("Failed to remove %s: %v", oldWikiPath, err)
  128. }
  129. }
  130. // SECURITY: if new file is a symlink to non-exist critical file,
  131. // attack content can be written to the target file (e.g. authorized_keys2)
  132. // as a new page operation.
  133. // So we want to make sure the symlink is removed before write anything.
  134. // The new file we created will be in normal text format.
  135. if err = os.RemoveAll(newWikiPath); err != nil {
  136. return err
  137. }
  138. if err = ioutil.WriteFile(newWikiPath, []byte(content), 0666); err != nil {
  139. return fmt.Errorf("WriteFile: %v", err)
  140. }
  141. if len(message) == 0 {
  142. message = "Update page '" + newWikiName + "'"
  143. }
  144. if err = git.AddChanges(localPath, true); err != nil {
  145. return fmt.Errorf("AddChanges: %v", err)
  146. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  147. Committer: doer.NewGitSig(),
  148. Message: message,
  149. }); err != nil {
  150. return fmt.Errorf("CommitChanges: %v", err)
  151. } else if err = git.Push(localPath, git.PushOptions{
  152. Remote: "origin",
  153. Branch: "master",
  154. }); err != nil {
  155. return fmt.Errorf("Push: %v", err)
  156. }
  157. return nil
  158. }
  159. // AddWikiPage adds a new wiki page with a given wikiPath.
  160. func (repo *Repository) AddWikiPage(doer *User, wikiName, content, message string) error {
  161. return repo.updateWikiPage(doer, "", wikiName, content, message, true)
  162. }
  163. // EditWikiPage updates a wiki page identified by its wikiPath,
  164. // optionally also changing wikiPath.
  165. func (repo *Repository) EditWikiPage(doer *User, oldWikiName, newWikiName, content, message string) error {
  166. return repo.updateWikiPage(doer, oldWikiName, newWikiName, content, message, false)
  167. }
  168. // DeleteWikiPage deletes a wiki page identified by its path.
  169. func (repo *Repository) DeleteWikiPage(doer *User, wikiName string) (err error) {
  170. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  171. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  172. localPath := repo.LocalWikiPath()
  173. if err = discardLocalWikiChanges(localPath); err != nil {
  174. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  175. } else if err = repo.updateLocalWiki(); err != nil {
  176. return fmt.Errorf("UpdateLocalWiki: %v", err)
  177. }
  178. filename := path.Join(localPath, WikiNameToFilename(wikiName))
  179. if err := os.Remove(filename); err != nil {
  180. return fmt.Errorf("Failed to remove %s: %v", filename, err)
  181. }
  182. message := "Delete page '" + wikiName + "'"
  183. if err = git.AddChanges(localPath, true); err != nil {
  184. return fmt.Errorf("AddChanges: %v", err)
  185. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  186. Committer: doer.NewGitSig(),
  187. Message: message,
  188. }); err != nil {
  189. return fmt.Errorf("CommitChanges: %v", err)
  190. } else if err = git.Push(localPath, git.PushOptions{
  191. Remote: "origin",
  192. Branch: "master",
  193. }); err != nil {
  194. return fmt.Errorf("Push: %v", err)
  195. }
  196. return nil
  197. }