Console
Console copied to clipboard
Invalid language parameter is treated as all languages
Expected Behavior
If I load item(s) via Get-Item, Get-ChildItem etc. and accidentally provide an incorrect language parameter, I'd like SPE to give an error message and/or not return any results. Maybe the current behavior is by design, and I understand if this fix/change isn't worth doing. If an invalid language is provided to the -Language parameter, it is silently accepted and is treated as * (any language).
Actual Behavior
If I provide an invalid language, it is treated as * or all languages. So if I want to perform an action on a set of items on a specific language layer and mistype the language parameter, the action will be performed on all languages. So basically, this problem only occurs when there is a typo/error in the provided script, but the consequence can be quite sever.
Steps to Reproduce the Problem
I've tested this on 6.0 and 6.1.1, but haven't yet been able to test this on the latest version. To reproduce the problem, an item with more than one language layer is required.
Get-Item -Path 'master:/sitecore/content/home -Language 'some-typo' | Format-Table will return all language versions of that item, basically the same as providing -Language *. I'd expect zero versions to be returned and/or that it gives an error. Right now I've tested this on Get-Item and Get-ChildItem, but briefly browsing the SPE code I assume this applies to more cmdlets.
Browsing the code, it seems like the handling of LanguageWildcardPatterns in BaseItemCommand.cs could be the source of this, but I may be completely wrong. A wild guess of the root cause could be the WildcardUtils.GetWildcardPattern returning * if name is null or empty. Is it as simple as name becoming null/empty if the input value cannot be parsed into a CultureInfo object?
Sample tests:
Get-Item ... -Language en,*returns "en" plus all languages, so the "en" version is listed twice. Makes sense.Get-Item ... -Language en,en-US,*returns "en", "en-US" plus all languages, so the "en" and "en-US" versions are listed twice. Basically the same as above. Makes sense.Get-Item ... -Language foowhere foo is not a registered culture, returns the same as *. This feels scary.Get-Item ... -Language en,foowhere foo is not a registered culture, returns exactly the same as en,*. This feels scary.Get-Item ...without the language parameter returns data on the context language as expected.
So basically, when a provided language string cannot be parsed into a culture, it treats it as a wildcard.
- [ ] Tested issue with clean install of Sitecore and the latest available version of SPE.
- [X] Asked questions on the Sitecore Slack Chat channel.
- [X] Reviewed questions and answers on the Sitecore Stack Exchange.
From what I can see, there is a call to LanguageManager.GetLanguage("foo") which ultimately returns an array of one item with a null value... SPE turns that into the wildcard *.
I would expect a language unknown to the LanguageManager to result in no items for that language.
Write-Host 'Good language + wildcard: Get-Item -Path "." -Language "en",*'
Get-Item -Path "." -Language "en",* | Format-Table
Write-Host 'Missing language: Get-Item -Path "." -Language "foo"'
Get-Item -Path "." -Language "foo" | Format-Table
Write-Host 'Good and bad language: Get-Item -Path "." -Language "en","foo"'
Get-Item -Path "." -Language "en","foo" | Format-Table
Write-Host 'No language: Get-Item -Path "."'
Get-Item -Path "." | Format-Table
Good language + wildcard: Get-Item -Path "." -Language "en",*
Name Children Language Version Id TemplateName
---- -------- -------- ------- -- ------------
Home False en 2 {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9} Sample Item
Home False en 2 {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9} Sample Item
Home False en-CA 2 {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9} Sample Item
Missing language: Get-Item -Path "." -Language "foo"
Good and bad language: Get-Item -Path "." -Language "en","foo"
Name Children Language Version Id TemplateName
---- -------- -------- ------- -- ------------
Home False en 2 {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9} Sample Item
No language: Get-Item -Path "."
Name Children Language Version Id TemplateName
---- -------- -------- ------- -- ------------
Home False en 2 {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9} Sample Item