From 6c170da48f8f39d9cc3577191bfa4cab2adad366 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sat, 12 Jun 2021 16:45:26 +0800 Subject: [PATCH 1/6] fix: not able to update local created non-urlencoded wiki pages --- services/wiki/wiki.go | 48 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 75b9d1d1f574b..b6a96b239fe2a 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -43,6 +43,12 @@ func NormalizeWikiName(name string) string { return strings.ReplaceAll(name, "-", " ") } +// NameToUnescapedFilename converts a wiki name to its corresponding filename without url query escape. +func NameToUnescapedFilename(name string) string { + name = strings.ReplaceAll(name, " ", "-") + return name + ".md" +} + // NameToFilename converts a wiki name to its corresponding filename. func NameToFilename(name string) string { name = strings.ReplaceAll(name, " ", "-") @@ -81,6 +87,19 @@ func InitWiki(repo *models.Repository) error { return nil } +func checkFileExistence(gitRepo *git.Repository, filePath string) (bool, error) { + filesInIndex, err := gitRepo.LsFiles(filePath) + if err != nil { + log.Error("%v", err) + return false, err + } + if util.IsStringInSlice(filePath, filesInIndex) { + return true, nil + } + + return false, nil +} + // updateWikiPage adds a new page to the repository wiki. func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) { if err = nameAllowed(newWikiName); err != nil { @@ -133,27 +152,40 @@ func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, new } } - newWikiPath := NameToFilename(newWikiName) + newWikiPath := NameToUnescapedFilename(newWikiName) + isWikiExist, err := checkFileExistence(gitRepo, newWikiPath) + if err != nil { + return err + } + if !isWikiExist { + newWikiPath = NameToFilename(newWikiName) + } + if isNew { - filesInIndex, err := gitRepo.LsFiles(newWikiPath) + isNewWikiExist, err := checkFileExistence(gitRepo, newWikiPath) if err != nil { - log.Error("%v", err) return err } - if util.IsStringInSlice(newWikiPath, filesInIndex) { + if isNewWikiExist { return models.ErrWikiAlreadyExist{ Title: newWikiPath, } } } else { - oldWikiPath := NameToFilename(oldWikiName) - filesInIndex, err := gitRepo.LsFiles(oldWikiPath) + oldWikiPath := NameToUnescapedFilename(oldWikiName) + isOldWikiExist, err := checkFileExistence(gitRepo, oldWikiPath) if err != nil { - log.Error("%v", err) return err } + if !isOldWikiExist { + oldWikiPath = NameToFilename(oldWikiName) + isOldWikiExist, err = checkFileExistence(gitRepo, oldWikiPath) + if err != nil { + return err + } + } - if util.IsStringInSlice(oldWikiPath, filesInIndex) { + if isOldWikiExist { err := gitRepo.RemoveFilesFromIndex(oldWikiPath) if err != nil { log.Error("%v", err) From f831290137939b52abc0d28a6df3cb08d726ad46 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Wed, 23 Jun 2021 13:34:10 +0800 Subject: [PATCH 2/6] tidy code --- services/wiki/wiki.go | 57 +++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index b6a96b239fe2a..eb840a1e2f1cd 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -100,6 +100,38 @@ func checkFileExistence(gitRepo *git.Repository, filePath string) (bool, error) return false, nil } +// prepareWikiFileName try to find a suitable file path with file name by the given raw wiki name. +// return: existence, prepared file path with name, error +func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string, error) { + // Pass in the raw unchanged name + // Add ".md" suffix if not there. + newWikiPath := NameToUnescapedFilename(wikiName) + // Look for this raw suffixed name within the stringslice - that works return true, rawSuffixed, nil + isWikiExist, err := checkFileExistence(gitRepo, newWikiPath) + if err != nil { + return isWikiExist, newWikiPath, err + } + + if isWikiExist { + return true, newWikiPath, nil + } + + // If not normalize and look for that - if that works returns true, normalized path, nil + newWikiPath = NameToFilename(wikiName) + isWikiExist, err = checkFileExistence(gitRepo, newWikiPath) + if err != nil { + return isWikiExist, newWikiPath, err + } + + if isWikiExist { + return true, newWikiPath, nil + } + + // If not return false, raw suffixed, nil + // blumia: we prefer use the normalized path here to keep compatible with old version. + return false, newWikiPath, nil +} + // updateWikiPage adds a new page to the repository wiki. func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) { if err = nameAllowed(newWikiName); err != nil { @@ -152,34 +184,23 @@ func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, new } } - newWikiPath := NameToUnescapedFilename(newWikiName) - isWikiExist, err := checkFileExistence(gitRepo, newWikiPath) + isWikiExist, newWikiPath, err := prepareWikiFileName(gitRepo, newWikiName) if err != nil { return err } - if !isWikiExist { - newWikiPath = NameToFilename(newWikiName) - } if isNew { - isNewWikiExist, err := checkFileExistence(gitRepo, newWikiPath) - if err != nil { - return err - } - if isNewWikiExist { + if isWikiExist { return models.ErrWikiAlreadyExist{ Title: newWikiPath, } } } else { - oldWikiPath := NameToUnescapedFilename(oldWikiName) - isOldWikiExist, err := checkFileExistence(gitRepo, oldWikiPath) - if err != nil { - return err - } - if !isOldWikiExist { - oldWikiPath = NameToFilename(oldWikiName) - isOldWikiExist, err = checkFileExistence(gitRepo, oldWikiPath) + // avoid check existence again if wiki name is not changed since gitRepo.LsFiles(...) is not free. + isOldWikiExist := true + oldWikiPath := newWikiPath + if oldWikiName != newWikiName { + isOldWikiExist, oldWikiPath, err = prepareWikiFileName(gitRepo, oldWikiName) if err != nil { return err } From 66afdad074e345abafa681b38e821b01ba8cf594 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 26 Jun 2021 01:22:08 +0100 Subject: [PATCH 3/6] as per suggestion Signed-off-by: Andrew Thornton --- services/wiki/wiki.go | 49 +++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index eb840a1e2f1cd..a5cc45d4bc064 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -87,49 +87,34 @@ func InitWiki(repo *models.Repository) error { return nil } -func checkFileExistence(gitRepo *git.Repository, filePath string) (bool, error) { - filesInIndex, err := gitRepo.LsFiles(filePath) - if err != nil { - log.Error("%v", err) - return false, err - } - if util.IsStringInSlice(filePath, filesInIndex) { - return true, nil - } - - return false, nil -} - // prepareWikiFileName try to find a suitable file path with file name by the given raw wiki name. // return: existence, prepared file path with name, error func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string, error) { // Pass in the raw unchanged name // Add ".md" suffix if not there. - newWikiPath := NameToUnescapedFilename(wikiName) - // Look for this raw suffixed name within the stringslice - that works return true, rawSuffixed, nil - isWikiExist, err := checkFileExistence(gitRepo, newWikiPath) - if err != nil { - return isWikiExist, newWikiPath, err - } - - if isWikiExist { - return true, newWikiPath, nil - } + unescaped := NameToUnescapedFilename(wikiName) + escaped := NameToFilename(wikiName) - // If not normalize and look for that - if that works returns true, normalized path, nil - newWikiPath = NameToFilename(wikiName) - isWikiExist, err = checkFileExistence(gitRepo, newWikiPath) + // Look for both files + filesInIndex, err := gitRepo.LsFiles(unescaped, escaped) if err != nil { - return isWikiExist, newWikiPath, err + log.Error("%v", err) + return false, escaped, err } - if isWikiExist { - return true, newWikiPath, nil + foundEscaped := false + for _, filename := range filesInIndex { + switch filename { + case unescaped: + // if we find the unescaped file return it + return true, unescaped, nil + case escaped: + foundEscaped = true + } } - // If not return false, raw suffixed, nil - // blumia: we prefer use the normalized path here to keep compatible with old version. - return false, newWikiPath, nil + // If not return whether the escaped file exists, and the escaped filename to keep backwards compatibility. + return foundEscaped, escaped, nil } // updateWikiPage adds a new page to the repository wiki. From 888060039b946e1c7dbbc530640ec0ed7173fe2e Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sat, 26 Jun 2021 22:14:41 +0800 Subject: [PATCH 4/6] Don't replace space to dash for unescaped wiki filename Co-authored-by: zeripath --- services/wiki/wiki.go | 1 - 1 file changed, 1 deletion(-) diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index a5cc45d4bc064..ba8e1f177cbda 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -45,7 +45,6 @@ func NormalizeWikiName(name string) string { // NameToUnescapedFilename converts a wiki name to its corresponding filename without url query escape. func NameToUnescapedFilename(name string) string { - name = strings.ReplaceAll(name, " ", "-") return name + ".md" } From 00ed6be6a7859367b441b4fe0a0c2b17a5b3f7e2 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Thu, 1 Jul 2021 19:05:36 +0800 Subject: [PATCH 5/6] Remove incorrect comment --- services/wiki/wiki.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index ba8e1f177cbda..e54c4ca1308c7 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -89,8 +89,6 @@ func InitWiki(repo *models.Repository) error { // prepareWikiFileName try to find a suitable file path with file name by the given raw wiki name. // return: existence, prepared file path with name, error func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string, error) { - // Pass in the raw unchanged name - // Add ".md" suffix if not there. unescaped := NameToUnescapedFilename(wikiName) escaped := NameToFilename(wikiName) From 2aa6f97e55cb46d13b688d5188a66fc1c98cc65b Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Thu, 1 Jul 2021 23:55:48 +0800 Subject: [PATCH 6/6] Remove NameToUnescapedFilename() --- services/wiki/wiki.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index e54c4ca1308c7..16301034da15e 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -43,11 +43,6 @@ func NormalizeWikiName(name string) string { return strings.ReplaceAll(name, "-", " ") } -// NameToUnescapedFilename converts a wiki name to its corresponding filename without url query escape. -func NameToUnescapedFilename(name string) string { - return name + ".md" -} - // NameToFilename converts a wiki name to its corresponding filename. func NameToFilename(name string) string { name = strings.ReplaceAll(name, " ", "-") @@ -89,7 +84,7 @@ func InitWiki(repo *models.Repository) error { // prepareWikiFileName try to find a suitable file path with file name by the given raw wiki name. // return: existence, prepared file path with name, error func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string, error) { - unescaped := NameToUnescapedFilename(wikiName) + unescaped := wikiName + ".md" escaped := NameToFilename(wikiName) // Look for both files