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

217 lines
6.6 KiB

9 years ago
9 years ago
  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. reservedWikiPaths = []string{"_pages", "_new", "_edit"}
  20. wikiWorkingPool = sync.NewExclusivePool()
  21. )
  22. // ToWikiPageURL formats a string to corresponding wiki URL name.
  23. func ToWikiPageURL(name string) string {
  24. return url.QueryEscape(strings.Replace(name, " ", "-", -1))
  25. }
  26. // ToWikiPageName formats a URL back to corresponding wiki page name,
  27. // and removes leading characters './' to prevent changing files
  28. // that are not belong to wiki repository.
  29. func ToWikiPageName(urlString string) string {
  30. name, _ := url.QueryUnescape(strings.Replace(urlString, "-", " ", -1))
  31. name = strings.Replace(name, "\t", " ", -1)
  32. return strings.Replace(strings.TrimLeft(name, "./"), "/", " ", -1)
  33. }
  34. // WikiCloneLink returns clone URLs of repository wiki.
  35. func (repo *Repository) WikiCloneLink() *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(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  41. }
  42. // WikiPath returns wiki data path for given repository.
  43. func (repo *Repository) WikiPath() string {
  44. return WikiPath(repo.MustOwner().Name, repo.Name)
  45. }
  46. // HasWiki returns true if repository has wiki.
  47. func (repo *Repository) HasWiki() bool {
  48. return com.IsDir(repo.WikiPath())
  49. }
  50. // InitWiki initializes a wiki for repository,
  51. // it does nothing when repository already has wiki.
  52. func (repo *Repository) InitWiki() error {
  53. if repo.HasWiki() {
  54. return nil
  55. }
  56. if err := git.InitRepository(repo.WikiPath(), true); err != nil {
  57. return fmt.Errorf("InitRepository: %v", err)
  58. } else if err = createDelegateHooks(repo.WikiPath()); err != nil {
  59. return fmt.Errorf("createDelegateHooks: %v", err)
  60. }
  61. return nil
  62. }
  63. // LocalWikiPath returns the path to the local wiki repository (?).
  64. func (repo *Repository) LocalWikiPath() string {
  65. return path.Join(setting.AppDataPath, "tmp/local-wiki", com.ToStr(repo.ID))
  66. }
  67. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  68. func (repo *Repository) UpdateLocalWiki() error {
  69. // Don't pass branch name here because it fails to clone and
  70. // checkout to a specific branch when wiki is an empty repository.
  71. var branch = ""
  72. if com.IsExist(repo.LocalWikiPath()) {
  73. branch = "master"
  74. }
  75. return UpdateLocalCopyBranch(repo.WikiPath(), repo.LocalWikiPath(), branch)
  76. }
  77. func discardLocalWikiChanges(localPath string) error {
  78. return discardLocalRepoBranchChanges(localPath, "master")
  79. }
  80. // pathAllowed checks if a wiki path is allowed
  81. func pathAllowed(path string) error {
  82. for i := range reservedWikiPaths {
  83. if path == reservedWikiPaths[i] {
  84. return ErrWikiAlreadyExist{path}
  85. }
  86. }
  87. return nil
  88. }
  89. // updateWikiPage adds new page to repository wiki.
  90. func (repo *Repository) updateWikiPage(doer *User, oldWikiPath, wikiPath, content, message string, isNew bool) (err error) {
  91. if err = pathAllowed(wikiPath); err != nil {
  92. return err
  93. }
  94. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  95. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  96. if err = repo.InitWiki(); err != nil {
  97. return fmt.Errorf("InitWiki: %v", err)
  98. }
  99. localPath := repo.LocalWikiPath()
  100. if err = discardLocalWikiChanges(localPath); err != nil {
  101. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  102. } else if err = repo.UpdateLocalWiki(); err != nil {
  103. return fmt.Errorf("UpdateLocalWiki: %v", err)
  104. }
  105. title := ToWikiPageName(wikiPath)
  106. filename := path.Join(localPath, wikiPath+".md")
  107. // If not a new file, show perform update not create.
  108. if isNew {
  109. if com.IsExist(filename) {
  110. return ErrWikiAlreadyExist{filename}
  111. }
  112. } else {
  113. file := path.Join(localPath, oldWikiPath+".md")
  114. if err := os.Remove(file); err != nil {
  115. return fmt.Errorf("Failed to remove %s: %v", file, err)
  116. }
  117. }
  118. // SECURITY: if new file is a symlink to non-exist critical file,
  119. // attack content can be written to the target file (e.g. authorized_keys2)
  120. // as a new page operation.
  121. // So we want to make sure the symlink is removed before write anything.
  122. // The new file we created will be in normal text format.
  123. _ = os.Remove(filename)
  124. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  125. return fmt.Errorf("WriteFile: %v", err)
  126. }
  127. if len(message) == 0 {
  128. message = "Update page '" + title + "'"
  129. }
  130. if err = git.AddChanges(localPath, true); err != nil {
  131. return fmt.Errorf("AddChanges: %v", err)
  132. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  133. Committer: doer.NewGitSig(),
  134. Message: message,
  135. }); err != nil {
  136. return fmt.Errorf("CommitChanges: %v", err)
  137. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  138. return fmt.Errorf("Push: %v", err)
  139. }
  140. return nil
  141. }
  142. // AddWikiPage adds a new wiki page with a given wikiPath.
  143. func (repo *Repository) AddWikiPage(doer *User, wikiPath, content, message string) error {
  144. return repo.updateWikiPage(doer, "", wikiPath, content, message, true)
  145. }
  146. // EditWikiPage updates a wiki page identified by its wikiPath,
  147. // optionally also changing wikiPath.
  148. func (repo *Repository) EditWikiPage(doer *User, oldWikiPath, wikiPath, content, message string) error {
  149. return repo.updateWikiPage(doer, oldWikiPath, wikiPath, content, message, false)
  150. }
  151. // DeleteWikiPage deletes a wiki page identified by its wikiPath.
  152. func (repo *Repository) DeleteWikiPage(doer *User, wikiPath string) (err error) {
  153. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  154. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  155. localPath := repo.LocalWikiPath()
  156. if err = discardLocalWikiChanges(localPath); err != nil {
  157. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  158. } else if err = repo.UpdateLocalWiki(); err != nil {
  159. return fmt.Errorf("UpdateLocalWiki: %v", err)
  160. }
  161. filename := path.Join(localPath, wikiPath+".md")
  162. if err := os.Remove(filename); err != nil {
  163. return fmt.Errorf("Failed to remove %s: %v", filename, err)
  164. }
  165. title := ToWikiPageName(wikiPath)
  166. message := "Delete page '" + title + "'"
  167. if err = git.AddChanges(localPath, true); err != nil {
  168. return fmt.Errorf("AddChanges: %v", err)
  169. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  170. Committer: doer.NewGitSig(),
  171. Message: message,
  172. }); err != nil {
  173. return fmt.Errorf("CommitChanges: %v", err)
  174. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  175. return fmt.Errorf("Push: %v", err)
  176. }
  177. return nil
  178. }