wiki.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "github.com/gogits/git-module"
  15. "github.com/gogits/gogs/modules/setting"
  16. "github.com/gogits/gogs/modules/sync"
  17. )
  18. var wikiWorkingPool = sync.NewSingleInstancePool()
  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. return strings.Replace(strings.TrimLeft(name, "./"), "/", " ", -1)
  29. }
  30. // WikiCloneLink returns clone URLs of repository wiki.
  31. func (repo *Repository) WikiCloneLink() (cl *CloneLink) {
  32. return repo.cloneLink(true)
  33. }
  34. // WikiPath returns wiki data path by given user and repository name.
  35. func WikiPath(userName, repoName string) string {
  36. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  37. }
  38. func (repo *Repository) WikiPath() string {
  39. return WikiPath(repo.MustOwner().Name, repo.Name)
  40. }
  41. // HasWiki returns true if repository has wiki.
  42. func (repo *Repository) HasWiki() bool {
  43. return com.IsDir(repo.WikiPath())
  44. }
  45. // InitWiki initializes a wiki for repository,
  46. // it does nothing when repository already has wiki.
  47. func (repo *Repository) InitWiki() error {
  48. if repo.HasWiki() {
  49. return nil
  50. }
  51. if err := git.InitRepository(repo.WikiPath(), true); err != nil {
  52. return fmt.Errorf("InitRepository: %v", err)
  53. } else if err = createUpdateHook(repo.WikiPath()); err != nil {
  54. return fmt.Errorf("createUpdateHook: %v", err)
  55. }
  56. return nil
  57. }
  58. func (repo *Repository) LocalWikiPath() string {
  59. return path.Join(setting.AppDataPath, "tmp/local-wiki", com.ToStr(repo.ID))
  60. }
  61. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  62. func (repo *Repository) UpdateLocalWiki() error {
  63. return updateLocalCopy(repo.WikiPath(), repo.LocalWikiPath(), "")
  64. }
  65. // discardLocalWikiChanges discards local commits make sure
  66. // it is even to remote branch when local copy exists.
  67. func discardLocalWikiChanges(localPath string) error {
  68. if !com.IsExist(localPath) {
  69. return nil
  70. }
  71. // No need to check if nothing in the repository.
  72. if !git.IsBranchExist(localPath, "master") {
  73. return nil
  74. }
  75. if err := git.ResetHEAD(localPath, true, "origin/master"); err != nil {
  76. return fmt.Errorf("ResetHEAD: %v", err)
  77. }
  78. return nil
  79. }
  80. // updateWikiPage adds new page to repository wiki.
  81. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  82. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  83. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  84. if err = repo.InitWiki(); err != nil {
  85. return fmt.Errorf("InitWiki: %v", err)
  86. }
  87. localPath := repo.LocalWikiPath()
  88. if err = discardLocalWikiChanges(localPath); err != nil {
  89. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  90. } else if err = repo.UpdateLocalWiki(); err != nil {
  91. return fmt.Errorf("UpdateLocalWiki: %v", err)
  92. }
  93. title = ToWikiPageName(title)
  94. filename := path.Join(localPath, title+".md")
  95. // If not a new file, show perform update not create.
  96. if isNew {
  97. if com.IsExist(filename) {
  98. return ErrWikiAlreadyExist{filename}
  99. }
  100. } else {
  101. os.Remove(path.Join(localPath, oldTitle+".md"))
  102. }
  103. // SECURITY: if new file is a symlink to non-exist critical file,
  104. // attack content can be written to the target file (e.g. authorized_keys2)
  105. // as a new page operation.
  106. // So we want to make sure the symlink is removed before write anything.
  107. // The new file we created will be in normal text format.
  108. os.Remove(filename)
  109. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  110. return fmt.Errorf("WriteFile: %v", err)
  111. }
  112. if len(message) == 0 {
  113. message = "Update page '" + title + "'"
  114. }
  115. if err = git.AddChanges(localPath, true); err != nil {
  116. return fmt.Errorf("AddChanges: %v", err)
  117. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  118. return fmt.Errorf("CommitChanges: %v", err)
  119. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  120. return fmt.Errorf("Push: %v", err)
  121. }
  122. return nil
  123. }
  124. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  125. return repo.updateWikiPage(doer, "", title, content, message, true)
  126. }
  127. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  128. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  129. }
  130. func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
  131. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  132. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  133. localPath := repo.LocalWikiPath()
  134. if err = discardLocalWikiChanges(localPath); err != nil {
  135. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  136. } else if err = repo.UpdateLocalWiki(); err != nil {
  137. return fmt.Errorf("UpdateLocalWiki: %v", err)
  138. }
  139. title = ToWikiPageName(title)
  140. filename := path.Join(localPath, title+".md")
  141. os.Remove(filename)
  142. message := "Delete page '" + title + "'"
  143. if err = git.AddChanges(localPath, true); err != nil {
  144. return fmt.Errorf("AddChanges: %v", err)
  145. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  146. return fmt.Errorf("CommitChanges: %v", err)
  147. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  148. return fmt.Errorf("Push: %v", err)
  149. }
  150. return nil
  151. }