Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

wiki_path.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package wiki
  4. import (
  5. "net/url"
  6. "path"
  7. "strings"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/modules/git"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/util"
  12. "code.gitea.io/gitea/services/convert"
  13. )
  14. // To define the wiki related concepts:
  15. // * Display Segment: the text what user see for a wiki page (aka, the title):
  16. // - "Home Page"
  17. // - "100% Free"
  18. // - "2000-01-02 meeting"
  19. // * Web Path:
  20. // - "/wiki/Home-Page"
  21. // - "/wiki/100%25+Free"
  22. // - "/wiki/2000-01-02+meeting.-"
  23. // - If a segment has a suffix "DashMarker(.-)", it means that there is no dash-space conversion for this segment.
  24. // - If a WebPath is a "*.md" pattern, then use the unescaped value directly as GitPath, to make users can access the raw file.
  25. // * Git Path (only space doesn't need to be escaped):
  26. // - "/.wiki.git/Home-Page.md"
  27. // - "/.wiki.git/100%25 Free.md"
  28. // - "/.wiki.git/2000-01-02 meeting.-.md"
  29. // TODO: support subdirectory in the future
  30. //
  31. // Although this package now has the ability to support subdirectory, but the route package doesn't:
  32. // * Double-escaping problem: the URL "/wiki/abc%2Fdef" becomes "/wiki/abc/def" by ctx.Params, which is incorrect
  33. // * This problem should have been 99% fixed, but it needs more tests.
  34. // * The old wiki code's behavior is always using %2F, instead of subdirectory, so there are a lot of legacy "%2F" files in user wikis.
  35. type WebPath string
  36. var reservedWikiNames = []string{"_pages", "_new", "_edit", "raw"}
  37. func validateWebPath(name WebPath) error {
  38. for _, s := range WebPathSegments(name) {
  39. if util.SliceContainsString(reservedWikiNames, s) {
  40. return repo_model.ErrWikiReservedName{Title: s}
  41. }
  42. }
  43. return nil
  44. }
  45. func hasDashMarker(s string) bool {
  46. return strings.HasSuffix(s, ".-")
  47. }
  48. func removeDashMarker(s string) string {
  49. return strings.TrimSuffix(s, ".-")
  50. }
  51. func addDashMarker(s string) string {
  52. return s + ".-"
  53. }
  54. func unescapeSegment(s string) (string, error) {
  55. if hasDashMarker(s) {
  56. s = removeDashMarker(s)
  57. } else {
  58. s = strings.ReplaceAll(s, "-", " ")
  59. }
  60. unescaped, err := url.QueryUnescape(s)
  61. if err != nil {
  62. return s, err // un-escaping failed, but it's still safe to return the original string, because it is only a title for end users
  63. }
  64. return unescaped, nil
  65. }
  66. func escapeSegToWeb(s string, hadDashMarker bool) string {
  67. if hadDashMarker || strings.Contains(s, "-") || strings.HasSuffix(s, ".md") {
  68. s = addDashMarker(s)
  69. } else {
  70. s = strings.ReplaceAll(s, " ", "-")
  71. }
  72. s = url.QueryEscape(s)
  73. return s
  74. }
  75. func WebPathSegments(s WebPath) []string {
  76. a := strings.Split(string(s), "/")
  77. for i := range a {
  78. a[i], _ = unescapeSegment(a[i])
  79. }
  80. return a
  81. }
  82. func WebPathToGitPath(s WebPath) string {
  83. if strings.HasSuffix(string(s), ".md") {
  84. ret, _ := url.PathUnescape(string(s))
  85. return util.PathJoinRelX(ret)
  86. }
  87. a := strings.Split(string(s), "/")
  88. for i := range a {
  89. shouldAddDashMarker := hasDashMarker(a[i])
  90. a[i], _ = unescapeSegment(a[i])
  91. a[i] = escapeSegToWeb(a[i], shouldAddDashMarker)
  92. a[i] = strings.ReplaceAll(a[i], "%20", " ") // space is safe to be kept in git path
  93. a[i] = strings.ReplaceAll(a[i], "+", " ")
  94. }
  95. return strings.Join(a, "/") + ".md"
  96. }
  97. func GitPathToWebPath(s string) (wp WebPath, err error) {
  98. if !strings.HasSuffix(s, ".md") {
  99. return "", repo_model.ErrWikiInvalidFileName{FileName: s}
  100. }
  101. s = strings.TrimSuffix(s, ".md")
  102. a := strings.Split(s, "/")
  103. for i := range a {
  104. shouldAddDashMarker := hasDashMarker(a[i])
  105. if a[i], err = unescapeSegment(a[i]); err != nil {
  106. return "", err
  107. }
  108. a[i] = escapeSegToWeb(a[i], shouldAddDashMarker)
  109. }
  110. return WebPath(strings.Join(a, "/")), nil
  111. }
  112. func WebPathToUserTitle(s WebPath) (dir, display string) {
  113. dir = path.Dir(string(s))
  114. display = path.Base(string(s))
  115. if strings.HasSuffix(display, ".md") {
  116. display = strings.TrimSuffix(display, ".md")
  117. display, _ = url.PathUnescape(display)
  118. }
  119. display, _ = unescapeSegment(display)
  120. return dir, display
  121. }
  122. func WebPathToURLPath(s WebPath) string {
  123. return string(s)
  124. }
  125. func WebPathFromRequest(s string) WebPath {
  126. s = util.PathJoinRelX(s)
  127. // The old wiki code's behavior is always using %2F, instead of subdirectory.
  128. s = strings.ReplaceAll(s, "/", "%2F")
  129. return WebPath(s)
  130. }
  131. func UserTitleToWebPath(base, title string) WebPath {
  132. // TODO: no support for subdirectory, because the old wiki code's behavior is always using %2F, instead of subdirectory.
  133. // So we do not add the support for writing slashes in title at the moment.
  134. title = strings.TrimSpace(title)
  135. title = util.PathJoinRelX(base, escapeSegToWeb(title, false))
  136. if title == "" || title == "." {
  137. title = "unnamed"
  138. }
  139. return WebPath(title)
  140. }
  141. // ToWikiPageMetaData converts meta information to a WikiPageMetaData
  142. func ToWikiPageMetaData(wikiName WebPath, lastCommit *git.Commit, repo *repo_model.Repository) *api.WikiPageMetaData {
  143. subURL := string(wikiName)
  144. _, title := WebPathToUserTitle(wikiName)
  145. return &api.WikiPageMetaData{
  146. Title: title,
  147. HTMLURL: util.URLJoin(repo.HTMLURL(), "wiki", subURL),
  148. SubURL: subURL,
  149. LastCommit: convert.ToWikiCommit(lastCommit),
  150. }
  151. }