Skip to content

Commit

Permalink
Make wiki title supports dashes and improve wiki name related features (
Browse files Browse the repository at this point in the history
#24143)

Close #7570


1. Clearly define the wiki path behaviors, see
`services/wiki/wiki_path.go` and tests
2. Keep compatibility with old contents
3. Allow to use dashes in titles, eg: "2000-01-02 Meeting record"
4. Add a "Pages" link in the dropdown, otherwise users can't go to the
Pages page easily.
5. Add a "View original git file" link in the Pages list, even if some
file names are broken, users still have a chance to edit or remove it,
without cloning the wiki repo to local.
6. Fix 500 error when the name contains prefix spaces.


This PR also introduces the ability to support sub-directories, but it
can't be done at the moment due to there are a lot of legacy wiki data,
which use "%2F" in file names.



![image](https://user-images.githubusercontent.com/2114189/232239004-3359d7b9-7bf3-4ff3-8446-bfb0e79645dd.png)


![image](https://user-images.githubusercontent.com/2114189/232239020-74b92c72-bf73-4377-a319-1c85609f82b1.png)

Co-authored-by: Giteabot <teabot@gitea.io>
  • Loading branch information
wxiaoguang and GiteaBot committed Apr 19, 2023
1 parent 738f2af commit b39a5bb
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 256 deletions.
3 changes: 3 additions & 0 deletions modules/git/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
if err != nil {
return nil, err
}
if len(commits) == 0 {
return nil, ErrNotExist{ID: relpath}
}
return commits[0], nil
}

Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,7 @@ wiki.reserved_page = The wiki page name "%s" is reserved.
wiki.pages = Pages
wiki.last_updated = Last updated %s
wiki.page_name_desc = Enter a name for this Wiki page. Some special names are: 'Home', '_Sidebar' and '_Footer'.
wiki.original_git_entry_tooltip = View original Git file instead of using friendly link.

activity = Activity
activity.period.filter_label = Period:
Expand Down
40 changes: 19 additions & 21 deletions routers/api/v1/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func NewWikiPage(ctx *context.APIContext) {
return
}

wikiName := wiki_service.NormalizeWikiName(form.Title)
wikiName := wiki_service.UserTitleToWebPath("", form.Title)

if len(form.Message) == 0 {
form.Message = fmt.Sprintf("Add '%s'", form.Title)
form.Message = fmt.Sprintf("Add %q", form.Title)
}

content, err := base64.StdEncoding.DecodeString(form.ContentBase64)
Expand All @@ -85,7 +85,7 @@ func NewWikiPage(ctx *context.APIContext) {
wikiPage := getWikiPage(ctx, wikiName)

if !ctx.Written() {
notification.NotifyNewWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, wikiName, form.Message)
notification.NotifyNewWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, string(wikiName), form.Message)
ctx.JSON(http.StatusCreated, wikiPage)
}
}
Expand Down Expand Up @@ -127,15 +127,15 @@ func EditWikiPage(ctx *context.APIContext) {

form := web.GetForm(ctx).(*api.CreateWikiPageOptions)

oldWikiName := wiki_service.NormalizeWikiName(ctx.Params(":pageName"))
newWikiName := wiki_service.NormalizeWikiName(form.Title)
oldWikiName := wiki_service.WebPathFromRequest(ctx.Params(":pageName"))
newWikiName := wiki_service.UserTitleToWebPath("", form.Title)

if len(newWikiName) == 0 {
newWikiName = oldWikiName
}

if len(form.Message) == 0 {
form.Message = fmt.Sprintf("Update '%s'", newWikiName)
form.Message = fmt.Sprintf("Update %q", newWikiName)
}

content, err := base64.StdEncoding.DecodeString(form.ContentBase64)
Expand All @@ -153,14 +153,12 @@ func EditWikiPage(ctx *context.APIContext) {
wikiPage := getWikiPage(ctx, newWikiName)

if !ctx.Written() {
notification.NotifyEditWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, newWikiName, form.Message)
notification.NotifyEditWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, string(newWikiName), form.Message)
ctx.JSON(http.StatusOK, wikiPage)
}
}

func getWikiPage(ctx *context.APIContext, title string) *api.WikiPage {
title = wiki_service.NormalizeWikiName(title)

func getWikiPage(ctx *context.APIContext, wikiName wiki_service.WebPath) *api.WikiPage {
wikiRepo, commit := findWikiRepoCommit(ctx)
if wikiRepo != nil {
defer wikiRepo.Close()
Expand All @@ -170,7 +168,7 @@ func getWikiPage(ctx *context.APIContext, title string) *api.WikiPage {
}

// lookup filename in wiki - get filecontent, real filename
content, pageFilename := wikiContentsByName(ctx, commit, title, false)
content, pageFilename := wikiContentsByName(ctx, commit, wikiName, false)
if ctx.Written() {
return nil
}
Expand All @@ -196,7 +194,7 @@ func getWikiPage(ctx *context.APIContext, title string) *api.WikiPage {
}

return &api.WikiPage{
WikiPageMetaData: convert.ToWikiPageMetaData(title, lastCommit, ctx.Repo.Repository),
WikiPageMetaData: convert.ToWikiPageMetaData(wikiName, lastCommit, ctx.Repo.Repository),
ContentBase64: content,
CommitCount: commitsCount,
Sidebar: sidebarContent,
Expand Down Expand Up @@ -233,7 +231,7 @@ func DeleteWikiPage(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"

wikiName := wiki_service.NormalizeWikiName(ctx.Params(":pageName"))
wikiName := wiki_service.WebPathFromRequest(ctx.Params(":pageName"))

if err := wiki_service.DeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, wikiName); err != nil {
if err.Error() == "file does not exist" {
Expand All @@ -244,7 +242,7 @@ func DeleteWikiPage(ctx *context.APIContext) {
return
}

notification.NotifyDeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, wikiName)
notification.NotifyDeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, string(wikiName))

ctx.Status(http.StatusNoContent)
}
Expand Down Expand Up @@ -316,7 +314,7 @@ func ListWikiPages(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
}
wikiName, err := wiki_service.FilenameToName(entry.Name())
wikiName, err := wiki_service.GitPathToWebPath(entry.Name())
if err != nil {
if repo_model.IsErrWikiInvalidFileName(err) {
continue
Expand Down Expand Up @@ -361,7 +359,7 @@ func GetWikiPage(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"

// get requested pagename
pageName := wiki_service.NormalizeWikiName(ctx.Params(":pageName"))
pageName := wiki_service.WebPathFromRequest(ctx.Params(":pageName"))

wikiPage := getWikiPage(ctx, pageName)
if !ctx.Written() {
Expand Down Expand Up @@ -411,7 +409,7 @@ func ListPageRevisions(ctx *context.APIContext) {
}

// get requested pagename
pageName := wiki_service.NormalizeWikiName(ctx.Params(":pageName"))
pageName := wiki_service.WebPathFromRequest(ctx.Params(":pageName"))
if len(pageName) == 0 {
pageName = "Home"
}
Expand Down Expand Up @@ -502,9 +500,9 @@ func wikiContentsByEntry(ctx *context.APIContext, entry *git.TreeEntry) string {

// wikiContentsByName returns the contents of a wiki page, along with a boolean
// indicating whether the page exists. Writes to ctx if an error occurs.
func wikiContentsByName(ctx *context.APIContext, commit *git.Commit, wikiName string, isSidebarOrFooter bool) (string, string) {
pageFilename := wiki_service.NameToFilename(wikiName)
entry, err := findEntryForFile(commit, pageFilename)
func wikiContentsByName(ctx *context.APIContext, commit *git.Commit, wikiName wiki_service.WebPath, isSidebarOrFooter bool) (string, string) {
gitFilename := wiki_service.WebPathToGitPath(wikiName)
entry, err := findEntryForFile(commit, gitFilename)
if err != nil {
if git.IsErrNotExist(err) {
if !isSidebarOrFooter {
Expand All @@ -515,5 +513,5 @@ func wikiContentsByName(ctx *context.APIContext, commit *git.Commit, wikiName st
}
return "", ""
}
return wikiContentsByEntry(ctx, entry), pageFilename
return wikiContentsByEntry(ctx, entry), gitFilename
}
Loading

0 comments on commit b39a5bb

Please sign in to comment.