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