wiki.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. "sync"
  14. "github.com/Unknwon/com"
  15. "github.com/gogits/git-module"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. // workingPool represents a pool of working status which makes sure
  19. // that only one instance of same task is performing at a time.
  20. // However, different type of tasks can performing at the same time.
  21. type workingPool struct {
  22. lock sync.Mutex
  23. pool map[string]*sync.Mutex
  24. count map[string]int
  25. }
  26. // CheckIn checks in a task and waits if others are running.
  27. func (p *workingPool) CheckIn(name string) {
  28. p.lock.Lock()
  29. lock, has := p.pool[name]
  30. if !has {
  31. lock = &sync.Mutex{}
  32. p.pool[name] = lock
  33. }
  34. p.count[name]++
  35. p.lock.Unlock()
  36. lock.Lock()
  37. }
  38. // CheckOut checks out a task to let other tasks run.
  39. func (p *workingPool) CheckOut(name string) {
  40. p.lock.Lock()
  41. defer p.lock.Unlock()
  42. p.pool[name].Unlock()
  43. if p.count[name] == 1 {
  44. delete(p.pool, name)
  45. delete(p.count, name)
  46. } else {
  47. p.count[name]--
  48. }
  49. }
  50. var wikiWorkingPool = &workingPool{
  51. pool: make(map[string]*sync.Mutex),
  52. count: make(map[string]int),
  53. }
  54. // ToWikiPageURL formats a string to corresponding wiki URL name.
  55. func ToWikiPageURL(name string) string {
  56. return url.QueryEscape(strings.Replace(name, " ", "-", -1))
  57. }
  58. // ToWikiPageName formats a URL back to corresponding wiki page name,
  59. // and removes leading characters './' to prevent changing files
  60. // that are not belong to wiki repository.
  61. func ToWikiPageName(urlString string) string {
  62. name, _ := url.QueryUnescape(strings.Replace(urlString, "-", " ", -1))
  63. return strings.Replace(strings.TrimLeft(name, "./"), "/", " ", -1)
  64. }
  65. // WikiCloneLink returns clone URLs of repository wiki.
  66. func (repo *Repository) WikiCloneLink() (cl *CloneLink) {
  67. return repo.cloneLink(true)
  68. }
  69. // WikiPath returns wiki data path by given user and repository name.
  70. func WikiPath(userName, repoName string) string {
  71. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  72. }
  73. func (repo *Repository) WikiPath() string {
  74. return WikiPath(repo.MustOwner().Name, repo.Name)
  75. }
  76. // HasWiki returns true if repository has wiki.
  77. func (repo *Repository) HasWiki() bool {
  78. return com.IsDir(repo.WikiPath())
  79. }
  80. // InitWiki initializes a wiki for repository,
  81. // it does nothing when repository already has wiki.
  82. func (repo *Repository) InitWiki() error {
  83. if repo.HasWiki() {
  84. return nil
  85. }
  86. if err := git.InitRepository(repo.WikiPath(), true); err != nil {
  87. return fmt.Errorf("InitRepository: %v", err)
  88. } else if err = createUpdateHook(repo.WikiPath()); err != nil {
  89. return fmt.Errorf("createUpdateHook: %v", err)
  90. }
  91. return nil
  92. }
  93. func (repo *Repository) LocalWikiPath() string {
  94. return path.Join(setting.AppDataPath, "tmp/local-wiki", com.ToStr(repo.ID))
  95. }
  96. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  97. func (repo *Repository) UpdateLocalWiki() error {
  98. return updateLocalCopy(repo.WikiPath(), repo.LocalWikiPath())
  99. }
  100. // discardLocalWikiChanges discards local commits make sure
  101. // it is even to remote branch when local copy exists.
  102. func discardLocalWikiChanges(localPath string) error {
  103. if !com.IsExist(localPath) {
  104. return nil
  105. }
  106. // No need to check if nothing in the repository.
  107. if !git.IsBranchExist(localPath, "master") {
  108. return nil
  109. }
  110. if err := git.ResetHEAD(localPath, true, "origin/master"); err != nil {
  111. return fmt.Errorf("ResetHEAD: %v", err)
  112. }
  113. return nil
  114. }
  115. // updateWikiPage adds new page to repository wiki.
  116. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  117. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  118. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  119. if err = repo.InitWiki(); err != nil {
  120. return fmt.Errorf("InitWiki: %v", err)
  121. }
  122. localPath := repo.LocalWikiPath()
  123. if err = discardLocalWikiChanges(localPath); err != nil {
  124. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  125. } else if err = repo.UpdateLocalWiki(); err != nil {
  126. return fmt.Errorf("UpdateLocalWiki: %v", err)
  127. }
  128. title = ToWikiPageName(title)
  129. filename := path.Join(localPath, title+".md")
  130. // If not a new file, show perform update not create.
  131. if isNew {
  132. if com.IsExist(filename) {
  133. return ErrWikiAlreadyExist{filename}
  134. }
  135. } else {
  136. os.Remove(path.Join(localPath, oldTitle+".md"))
  137. }
  138. // SECURITY: if new file is a symlink to non-exist critical file,
  139. // attack content can be written to the target file (e.g. authorized_keys2)
  140. // as a new page operation.
  141. // So we want to make sure the symlink is removed before write anything.
  142. // The new file we created will be in normal text format.
  143. os.Remove(filename)
  144. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  145. return fmt.Errorf("WriteFile: %v", err)
  146. }
  147. if len(message) == 0 {
  148. message = "Update page '" + title + "'"
  149. }
  150. if err = git.AddChanges(localPath, true); err != nil {
  151. return fmt.Errorf("AddChanges: %v", err)
  152. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  153. return fmt.Errorf("CommitChanges: %v", err)
  154. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  155. return fmt.Errorf("Push: %v", err)
  156. }
  157. return nil
  158. }
  159. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  160. return repo.updateWikiPage(doer, "", title, content, message, true)
  161. }
  162. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  163. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  164. }
  165. func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
  166. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  167. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  168. localPath := repo.LocalWikiPath()
  169. if err = discardLocalWikiChanges(localPath); err != nil {
  170. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  171. } else if err = repo.UpdateLocalWiki(); err != nil {
  172. return fmt.Errorf("UpdateLocalWiki: %v", err)
  173. }
  174. title = ToWikiPageName(title)
  175. filename := path.Join(localPath, title+".md")
  176. os.Remove(filename)
  177. message := "Delete page '" + title + "'"
  178. if err = git.AddChanges(localPath, true); err != nil {
  179. return fmt.Errorf("AddChanges: %v", err)
  180. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  181. return fmt.Errorf("CommitChanges: %v", err)
  182. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  183. return fmt.Errorf("Push: %v", err)
  184. }
  185. return nil
  186. }