Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better wildcard file search support #3962

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DNN Platform/Library/Services/FileSystem/FolderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1845,11 +1845,11 @@ private static Regex WildcardToRegex(string pattern)
{
if (!pattern.Contains("*") && !pattern.Contains("?"))
{
pattern = "^" + pattern + ".*$";
pattern = ".*" + Regex.Escape(pattern) + ".*";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't have the start and end tokens, you wouldn't need the .* pattern, either, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I too am curious on this one, the "old way" was requires full string match, starting with the pattern at the beginning. For example

TestF as an input

Would match TestF, TestFile, TestFilters, TestF Test, etc

This new pattern would match those, but also would match

MyTestF, etc.

Is this a desired behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I too am curious on this one, the "old way" was requires full string match, starting with the pattern at the beginning. For example

TestF as an input

Would match TestF, TestFile, TestFilters, TestF Test, etc

This new pattern would match those, but also would match

MyTestF, etc.

Is this a desired behavior?

Yes, this is the desired behavior. It is a much more powerful wildcard search.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't have the start and end tokens, you wouldn't need the .* pattern, either, right?

I believe you would in order to achieve the "contains" search results.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regular Expression matching is a "contains" match by default, here's an example:
LINQPad screenshot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this was merged already, so I'll do another PR to change this. Thanks @bdukes

}
else
{
pattern = "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$";
pattern = Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".");
}

return RegexUtils.GetCachedRegex(pattern, RegexOptions.IgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,14 @@ public IList<IFileInfo> SearchFolderContent(int moduleId, IFolderInfo folder, bo

search = (search ?? string.Empty).Trim();

// Lucene does not support wildcards as the beginning of the search
bdukes marked this conversation as resolved.
Show resolved Hide resolved
// For file names we can remove any existing wildcard at the beginning
var cleanedKeywords = TrimStartWildCards(search);

var files = FolderManager.Instance.SearchFiles(folder, cleanedKeywords, recursive);
var files = FolderManager.Instance.SearchFiles(folder, search, recursive);
var sortProperties = SortProperties.Parse(sorting);
var sortedFiles = SortFiles(files, sortProperties).ToList();
totalCount = sortedFiles.Count;

return sortedFiles.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}

private static string TrimStartWildCards(string search)
{
var keywords = from keyword in search.Split(' ')
select keyword.TrimStart('*', '?');
search = string.Join(" ", keywords);
return search;
}

private static IEnumerable<IFileInfo> SortFiles(IEnumerable<IFileInfo> files, SortProperties sortProperties)
{
switch (sortProperties.Column)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
margin: 0px;
list-style: none;
box-sizing: border-box;
height: 28px;
}
}

Expand Down