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

XPath "in-use" language retrieval issues on unpublished nodes #95

Merged
Changes from 2 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
64 changes: 35 additions & 29 deletions src/Our.Umbraco.Vorto/Web/Controllers/VortoApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,42 @@ public IEnumerable<object> GetLanguages(string section, int id, int parentId, Gu

if (languageSource == "inuse")
{
var xpath = preValues.ContainsKey("xpath") ? preValues["xpath"].Value : "";

// Grab languages by xpath (only if in content section)
if (!string.IsNullOrWhiteSpace(xpath) && section == "content")
{
xpath = xpath.Replace("$currentPage",
string.Format("//*[@id={0} and @isDoc]", id)).Replace("$parentPage",
string.Format("//*[@id={0} and @isDoc]", parentId)).Replace("$ancestorOrSelf",
string.Format("//*[@id={0} and @isDoc]", id != 0 ? id : parentId));

// Lookup language nodes
var nodeIds = uQuery.GetNodesByXPath(xpath).Select(x => x.Id).ToArray();
if (nodeIds.Any())
{
var db = ApplicationContext.Current.DatabaseContext.Database;
languages.AddRange(db.Query<string>(
string.Format(
"SELECT DISTINCT [languageISOCode] FROM [umbracoLanguage] JOIN [umbracoDomains] ON [umbracoDomains].[domainDefaultLanguage] = [umbracoLanguage].[id] WHERE [umbracoDomains].[domainRootStructureID] in ({0})",
string.Join(",", nodeIds)))
.Select(CultureInfo.GetCultureInfo)
.Select(x => new Language
{
IsoCode = x.Name,
Name = x.DisplayName,
NativeName = x.NativeName,
var currentNode = id != 0 ? ApplicationContext.Services.ContentService.GetById(id) : null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd be tempted to refactor out the use of the ContentService here as this will be performing a database hit. You could probably go one of two approaches. 1) given you are really only checking to see if the content is actually published, you could check the published content cache and if it's not their, assume it's unpublished or 2) perform a lucene query on the internal lucene index which contains unpublished nodes and check there. I reckon option 1 would be easier but should do the trick I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, thanks for taking a look Matt - I'll update and re-commit just now :)

var currentNodeIsUnpublished = currentNode != null && currentNode.Published;

//trying to add/publish a home node, so no "in use" languages have been defined/are accessible - display all installed in the interim
var currentNodeIsUnpublishedRootNode = currentNodeIsUnpublished && parentId == -1;

var xpath = preValues.ContainsKey("xpath") ? preValues["xpath"].Value : "";

// Grab languages by xpath (only if in content section)
if (!currentNodeIsUnpublishedRootNode && !string.IsNullOrWhiteSpace(xpath) && section == "content")
{
xpath = xpath.Replace("$currentPage",
string.Format("//*[@id={0} and @isDoc]", id)).Replace("$parentPage",
string.Format("//*[@id={0} and @isDoc]", parentId)).Replace("$ancestorOrSelf",
string.Format("//*[@id={0} and @isDoc]", currentNodeIsUnpublished ? parentId : id));

// Lookup language nodes
var nodeIds = uQuery.GetNodesByXPath(xpath).Select(x => x.Id).ToArray();
if (nodeIds.Any())
{
var db = ApplicationContext.Current.DatabaseContext.Database;
languages.AddRange(db.Query<string>(
string.Format(
"SELECT DISTINCT [languageISOCode] FROM [umbracoLanguage] JOIN [umbracoDomains] ON [umbracoDomains].[domainDefaultLanguage] = [umbracoLanguage].[id] WHERE [umbracoDomains].[domainRootStructureID] in ({0})",
string.Join(",", nodeIds)))
.Select(CultureInfo.GetCultureInfo)
.Select(x => new Language
{
IsoCode = x.Name,
Name = x.DisplayName,
NativeName = x.NativeName,
IsRightToLeft = x.TextInfo.IsRightToLeft
}));
}
}
else
}));
}
}
else
{
// No language node xpath so just return a list of all languages in use
var db = ApplicationContext.Current.DatabaseContext.Database;
Expand Down