You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiki.go 7.5 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "", ErrWikiInvalidFileName{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(x, 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.MustOwnerName(), 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 local wiki repository copy path.
  77. func LocalWikiPath() string {
  78. if filepath.IsAbs(setting.Repository.Local.LocalWikiPath) {
  79. return setting.Repository.Local.LocalWikiPath
  80. }
  81. return path.Join(setting.AppDataPath, setting.Repository.Local.LocalWikiPath)
  82. }
  83. // LocalWikiPath returns the path to the local wiki repository (?).
  84. func (repo *Repository) LocalWikiPath() string {
  85. return path.Join(LocalWikiPath(), com.ToStr(repo.ID))
  86. }
  87. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  88. func (repo *Repository) updateLocalWiki() error {
  89. // Don't pass branch name here because it fails to clone and
  90. // checkout to a specific branch when wiki is an empty repository.
  91. var branch = ""
  92. if com.IsExist(repo.LocalWikiPath()) {
  93. branch = "master"
  94. }
  95. return UpdateLocalCopyBranch(repo.WikiPath(), repo.LocalWikiPath(), branch)
  96. }
  97. func discardLocalWikiChanges(localPath string) error {
  98. return discardLocalRepoBranchChanges(localPath, "master")
  99. }
  100. // nameAllowed checks if a wiki name is allowed
  101. func nameAllowed(name string) error {
  102. for _, reservedName := range reservedWikiNames {
  103. if name == reservedName {
  104. return ErrWikiReservedName{name}
  105. }
  106. }
  107. return nil
  108. }
  109. // updateWikiPage adds a new page to the repository wiki.
  110. func (repo *Repository) updateWikiPage(doer *User, oldWikiName, newWikiName, content, message string, isNew bool) (err error) {
  111. if err = nameAllowed(newWikiName); err != nil {
  112. return err
  113. }
  114. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  115. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  116. if err = repo.InitWiki(); err != nil {
  117. return fmt.Errorf("InitWiki: %v", err)
  118. }
  119. localPath := repo.LocalWikiPath()
  120. if err = discardLocalWikiChanges(localPath); err != nil {
  121. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  122. } else if err = repo.updateLocalWiki(); err != nil {
  123. return fmt.Errorf("UpdateLocalWiki: %v", err)
  124. }
  125. newWikiPath := path.Join(localPath, WikiNameToFilename(newWikiName))
  126. // If not a new file, show perform update not create.
  127. if isNew {
  128. if com.IsExist(newWikiPath) {
  129. return ErrWikiAlreadyExist{newWikiPath}
  130. }
  131. } else {
  132. oldWikiPath := path.Join(localPath, WikiNameToFilename(oldWikiName))
  133. if err := os.Remove(oldWikiPath); err != nil {
  134. return fmt.Errorf("Failed to remove %s: %v", oldWikiPath, err)
  135. }
  136. }
  137. // SECURITY: if new file is a symlink to non-exist critical file,
  138. // attack content can be written to the target file (e.g. authorized_keys2)
  139. // as a new page operation.
  140. // So we want to make sure the symlink is removed before write anything.
  141. // The new file we created will be in normal text format.
  142. if err = os.RemoveAll(newWikiPath); err != nil {
  143. return err
  144. }
  145. if err = ioutil.WriteFile(newWikiPath, []byte(content), 0666); err != nil {
  146. return fmt.Errorf("WriteFile: %v", err)
  147. }
  148. if len(message) == 0 {
  149. message = "Update page '" + newWikiName + "'"
  150. }
  151. if err = git.AddChanges(localPath, true); err != nil {
  152. return fmt.Errorf("AddChanges: %v", err)
  153. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  154. Committer: doer.NewGitSig(),
  155. Message: message,
  156. }); err != nil {
  157. return fmt.Errorf("CommitChanges: %v", err)
  158. } else if err = git.Push(localPath, git.PushOptions{
  159. Remote: "origin",
  160. Branch: "master",
  161. }); err != nil {
  162. return fmt.Errorf("Push: %v", err)
  163. }
  164. return nil
  165. }
  166. // AddWikiPage adds a new wiki page with a given wikiPath.
  167. func (repo *Repository) AddWikiPage(doer *User, wikiName, content, message string) error {
  168. return repo.updateWikiPage(doer, "", wikiName, content, message, true)
  169. }
  170. // EditWikiPage updates a wiki page identified by its wikiPath,
  171. // optionally also changing wikiPath.
  172. func (repo *Repository) EditWikiPage(doer *User, oldWikiName, newWikiName, content, message string) error {
  173. return repo.updateWikiPage(doer, oldWikiName, newWikiName, content, message, false)
  174. }
  175. // DeleteWikiPage deletes a wiki page identified by its path.
  176. func (repo *Repository) DeleteWikiPage(doer *User, wikiName string) (err error) {
  177. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  178. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  179. localPath := repo.LocalWikiPath()
  180. if err = discardLocalWikiChanges(localPath); err != nil {
  181. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  182. } else if err = repo.updateLocalWiki(); err != nil {
  183. return fmt.Errorf("UpdateLocalWiki: %v", err)
  184. }
  185. filename := path.Join(localPath, WikiNameToFilename(wikiName))
  186. if err := os.Remove(filename); err != nil {
  187. return fmt.Errorf("Failed to remove %s: %v", filename, err)
  188. }
  189. message := "Delete page '" + wikiName + "'"
  190. if err = git.AddChanges(localPath, true); err != nil {
  191. return fmt.Errorf("AddChanges: %v", err)
  192. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  193. Committer: doer.NewGitSig(),
  194. Message: message,
  195. }); err != nil {
  196. return fmt.Errorf("CommitChanges: %v", err)
  197. } else if err = git.Push(localPath, git.PushOptions{
  198. Remote: "origin",
  199. Branch: "master",
  200. }); err != nil {
  201. return fmt.Errorf("Push: %v", err)
  202. }
  203. return nil
  204. }