@ -81,6 +81,34 @@ func InitWiki(repo *models.Repository) error {
return nil
return 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 ) {
unescaped := wikiName + ".md"
escaped := NameToFilename ( wikiName )
// Look for both files
filesInIndex , err := gitRepo . LsFiles ( unescaped , escaped )
if err != nil {
log . Error ( "%v" , err )
return false , escaped , err
}
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 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.
// 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 ) {
func updateWikiPage ( doer * models . User , repo * models . Repository , oldWikiName , newWikiName , content , message string , isNew bool ) ( err error ) {
if err = nameAllowed ( newWikiName ) ; err != nil {
if err = nameAllowed ( newWikiName ) ; err != nil {
@ -133,27 +161,29 @@ func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, new
}
}
}
}
newWikiPath := NameToFilename ( newWikiName )
isWikiExist , newWikiPath , err := prepareWikiFileName ( gitRepo , newWikiName )
if err != nil {
return err
}
if isNew {
if isNew {
filesInIndex , err := gitRepo . LsFiles ( newWikiPath )
if isWikiExist {
if err != nil {
log . Error ( "%v" , err )
return err
}
if util . IsStringInSlice ( newWikiPath , filesInIndex ) {
return models . ErrWikiAlreadyExist {
return models . ErrWikiAlreadyExist {
Title : newWikiPath ,
Title : newWikiPath ,
}
}
}
}
} else {
} else {
oldWikiPath := NameToFilename ( oldWikiName )
// avoid check existence again if wiki name is not changed since gitRepo.LsFiles(...) is not free.
filesInIndex , err := gitRepo . LsFiles ( oldWikiPath )
isOldWikiExist := true
if err != nil {
oldWikiPath := newWikiPath
log . Error ( "%v" , err )
if oldWikiName != newWikiName {
return err
isOldWikiExist , oldWikiPath , err = prepareWikiFileName ( gitRepo , oldWikiName )
if err != nil {
return err
}
}
}
if util . IsStringInSlice ( oldWikiPath , filesInIndex ) {
if isOldWikiExist {
err := gitRepo . RemoveFilesFromIndex ( oldWikiPath )
err := gitRepo . RemoveFilesFromIndex ( oldWikiPath )
if err != nil {
if err != nil {
log . Error ( "%v" , err )
log . Error ( "%v" , err )