mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-20 10:47:56 +08:00
Sanitizing RoutableService
--HG-- branch : 1.x
This commit is contained in:
@@ -53,12 +53,12 @@ namespace Orchard.Core.Routable.Services {
|
|||||||
|
|
||||||
public static string RemoveDiacritics(string slug) {
|
public static string RemoveDiacritics(string slug) {
|
||||||
string stFormD = slug.Normalize(NormalizationForm.FormD);
|
string stFormD = slug.Normalize(NormalizationForm.FormD);
|
||||||
StringBuilder sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
for (int ich = 0; ich < stFormD.Length; ich++) {
|
foreach (char t in stFormD) {
|
||||||
UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
|
UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(t);
|
||||||
if (uc != UnicodeCategory.NonSpacingMark) {
|
if (uc != UnicodeCategory.NonSpacingMark) {
|
||||||
sb.Append(stFormD[ich]);
|
sb.Append(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ namespace Orchard.Core.Routable.Services {
|
|||||||
if ((model.Slug != null && !string.IsNullOrEmpty(model.Slug.Trim())) || string.IsNullOrEmpty(model.Title))
|
if ((model.Slug != null && !string.IsNullOrEmpty(model.Slug.Trim())) || string.IsNullOrEmpty(model.Title))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
FillSlugContext slugContext = new FillSlugContext(model.Title);
|
var slugContext = new FillSlugContext(model.Title);
|
||||||
|
|
||||||
foreach (ISlugEventHandler slugEventHandler in _slugEventHandlers) {
|
foreach (ISlugEventHandler slugEventHandler in _slugEventHandlers) {
|
||||||
slugEventHandler.FillingSlugFromTitle(slugContext);
|
slugEventHandler.FillingSlugFromTitle(slugContext);
|
||||||
@@ -95,8 +95,17 @@ namespace Orchard.Core.Routable.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public string GenerateUniqueSlug(IRoutableAspect part, IEnumerable<string> existingPaths) {
|
public string GenerateUniqueSlug(IRoutableAspect part, IEnumerable<string> existingPaths) {
|
||||||
if (existingPaths == null || !existingPaths.Contains(part.Path))
|
|
||||||
|
if (existingPaths == null) {
|
||||||
return part.Slug;
|
return part.Slug;
|
||||||
|
}
|
||||||
|
|
||||||
|
// materializing the enumeration
|
||||||
|
existingPaths = existingPaths.ToArray();
|
||||||
|
|
||||||
|
if(!existingPaths.Contains(part.Path)) {
|
||||||
|
return part.Slug;
|
||||||
|
}
|
||||||
|
|
||||||
int? version = existingPaths.Select(s => GetSlugVersion(part.Path, s)).OrderBy(i => i).LastOrDefault();
|
int? version = existingPaths.Select(s => GetSlugVersion(part.Path, s)).OrderBy(i => i).LastOrDefault();
|
||||||
|
|
||||||
@@ -117,8 +126,7 @@ namespace Orchard.Core.Routable.Services {
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<IRoutableAspect> GetSimilarPaths(string path)
|
public IEnumerable<IRoutableAspect> GetSimilarPaths(string path) {
|
||||||
{
|
|
||||||
return
|
return
|
||||||
_contentManager.Query<RoutePart, RoutePartRecord>()
|
_contentManager.Query<RoutePart, RoutePartRecord>()
|
||||||
.Where(routable => routable.Path != null && routable.Path.StartsWith(path, StringComparison.OrdinalIgnoreCase))
|
.Where(routable => routable.Path != null && routable.Path.StartsWith(path, StringComparison.OrdinalIgnoreCase))
|
||||||
@@ -138,13 +146,13 @@ namespace Orchard.Core.Routable.Services {
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
part.Path = part.GetPathWithSlug(part.Slug);
|
part.Path = part.GetPathWithSlug(part.Slug);
|
||||||
var pathsLikeThis = GetSimilarPaths(part.Path);
|
var pathsLikeThis = GetSimilarPaths(part.Path).ToArray();
|
||||||
|
|
||||||
// Don't include *this* part in the list
|
// Don't include *this* part in the list
|
||||||
// of slugs to consider for conflict detection
|
// of slugs to consider for conflict detection
|
||||||
pathsLikeThis = pathsLikeThis.Where(p => p.ContentItem.Id != part.ContentItem.Id);
|
pathsLikeThis = pathsLikeThis.Where(p => p.ContentItem.Id != part.ContentItem.Id).ToArray();
|
||||||
|
|
||||||
if (pathsLikeThis.Count() > 0) {
|
if (pathsLikeThis.Any()) {
|
||||||
var originalSlug = part.Slug;
|
var originalSlug = part.Slug;
|
||||||
var newSlug = GenerateUniqueSlug(part, pathsLikeThis.Select(p => p.Path));
|
var newSlug = GenerateUniqueSlug(part, pathsLikeThis.Select(p => p.Path));
|
||||||
part.Path = part.GetPathWithSlug(newSlug);
|
part.Path = part.GetPathWithSlug(newSlug);
|
||||||
|
Reference in New Issue
Block a user