diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs index 152db6ec97..91989b2496 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs @@ -192,6 +192,10 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache } } + LogHelper.Debug( + "Could not retrieve media {0} from Examine index, reverting to looking up media via legacy library.GetMedia method", + () => id); + var media = global::umbraco.library.GetMedia(id, true); if (media != null && media.Current != null) { @@ -213,6 +217,10 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache return ConvertFromXPathNavigator(media.Current); } + LogHelper.Debug( + "Could not retrieve media {0} from Examine index or from legacy library.GetMedia method", + () => id); + return null; } diff --git a/src/Umbraco.Web/PublishedContentQuery.cs b/src/Umbraco.Web/PublishedContentQuery.cs index fca773390a..33469c0c4b 100644 --- a/src/Umbraco.Web/PublishedContentQuery.cs +++ b/src/Umbraco.Web/PublishedContentQuery.cs @@ -38,7 +38,7 @@ namespace Umbraco.Web public IEnumerable TypedContent(IEnumerable ids) { - return TypedDocumentsbyIds(_contentCache, ids); + return TypedDocumentsByIds(_contentCache, ids); } public IEnumerable TypedContentAtXPath(string xpath, params XPathVariable[] vars) @@ -102,7 +102,7 @@ namespace Umbraco.Web public IEnumerable TypedMedia(IEnumerable ids) { - return TypedDocumentsbyIds(_mediaCache, ids); + return TypedDocumentsByIds(_mediaCache, ids); } public IEnumerable TypedMediaAtRoot() @@ -148,7 +148,7 @@ namespace Umbraco.Web // return doc; //} - private IEnumerable TypedDocumentsbyIds(ContextualPublishedCache cache, IEnumerable ids) + private IEnumerable TypedDocumentsByIds(ContextualPublishedCache cache, IEnumerable ids) { return ids.Select(eachId => TypedDocumentById(eachId, cache)); } diff --git a/src/Umbraco.Web/umbraco.presentation/library.cs b/src/Umbraco.Web/umbraco.presentation/library.cs index f029fc9a25..0415bff9c8 100644 --- a/src/Umbraco.Web/umbraco.presentation/library.cs +++ b/src/Umbraco.Web/umbraco.presentation/library.cs @@ -9,6 +9,7 @@ using System.Text.RegularExpressions; using System.Web; using System.Web.UI; using System.Xml; +using System.Xml.Linq; using System.Xml.XPath; using Newtonsoft.Json; using Umbraco.Core; @@ -16,21 +17,23 @@ using Umbraco.Core.Cache; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Logging; +using Umbraco.Core.Models; +using Umbraco.Core.Services; using Umbraco.Web; using Umbraco.Web.Cache; using Umbraco.Web.Templates; using umbraco.BusinessLogic; using umbraco.cms.businesslogic; -using umbraco.cms.businesslogic.media; -using umbraco.cms.businesslogic.member; -using umbraco.cms.businesslogic.propertytype; -using umbraco.cms.businesslogic.relation; using umbraco.cms.businesslogic.web; using umbraco.cms.helpers; using umbraco.scripting; using umbraco.DataLayer; -using umbraco.cms.businesslogic.language; using Umbraco.Core.IO; +using Language = umbraco.cms.businesslogic.language.Language; +using Media = umbraco.cms.businesslogic.media.Media; +using Member = umbraco.cms.businesslogic.member.Member; +using PropertyType = umbraco.cms.businesslogic.propertytype.PropertyType; +using Relation = umbraco.cms.businesslogic.relation.Relation; using UmbracoContext = umbraco.presentation.UmbracoContext; namespace umbraco @@ -490,46 +493,54 @@ namespace umbraco { if (UmbracoConfig.For.UmbracoSettings().Content.UmbracoLibraryCacheDuration > 0) { - XPathNodeIterator retVal = ApplicationContext.Current.ApplicationCache.GetCacheItem( + var xml = ApplicationContext.Current.ApplicationCache.GetCacheItem( string.Format( "{0}_{1}_{2}", CacheKeys.MediaCacheKey, MediaId, Deep), TimeSpan.FromSeconds(UmbracoConfig.For.UmbracoSettings().Content.UmbracoLibraryCacheDuration), () => GetMediaDo(MediaId, Deep)); - if (retVal != null) - return retVal; + if (xml != null) + { + return FromXElement(xml, xml.Attribute("nodeTypeAlias").Value); + } + } else { - return GetMediaDo(MediaId, Deep); + var xml = GetMediaDo(MediaId, Deep); + return FromXElement(xml, xml.Attribute("nodeTypeAlias").Value); } - } - catch + catch(Exception ex) { + LogHelper.Error("An error occurred looking up media", ex); } - XmlDocument xd = new XmlDocument(); + LogHelper.Debug("No media result for id {0}", () => MediaId); + + var xd = new XmlDocument(); xd.LoadXml(string.Format("No media is maching '{0}'", MediaId)); return xd.CreateNavigator().Select("/"); } - private static XPathNodeIterator GetMediaDo(int mediaId, bool deep) + private static XElement GetMediaDo(int mediaId, bool deep) { - var m = new Media(mediaId); - if (m.nodeObjectType == Media._objectType) - { - var mXml = new XmlDocument(); - var xml = m.ToXml(mXml, deep); - //This will be null if the media isn't public (meaning it is in the trash) - if (xml == null) return null; - //TODO: This is an aweful way of loading in XML - it is very slow. - mXml.LoadXml(xml.OuterXml); - var xp = mXml.CreateNavigator(); - var xpath = UmbracoConfig.For.UmbracoSettings().Content.UseLegacyXmlSchema ? "/node" : String.Format("/{0}", Casing.SafeAliasWithForcingCheck(m.ContentType.Alias)); - return xp.Select(xpath); - } - return null; + var media = ApplicationContext.Current.Services.MediaService.GetById(mediaId); + if (media == null) return null; + var serializer = new EntityXmlSerializer(); + var serialized = serializer.Serialize( + ApplicationContext.Current.Services.MediaService, ApplicationContext.Current.Services.DataTypeService, + ApplicationContext.Current.Services.UserService, media, deep); + return serialized; + } + + private static XPathNodeIterator FromXElement(XNode xml, string mediaContentType) + { + var xp = xml.CreateNavigator(); + var xpath = UmbracoConfig.For.UmbracoSettings().Content.UseLegacyXmlSchema + ? "/node" + : String.Format("/{0}", Casing.SafeAliasWithForcingCheck(mediaContentType)); + return xp.Select(xpath); } /// @@ -545,35 +556,42 @@ namespace umbraco { if (UmbracoConfig.For.UmbracoSettings().Content.UmbracoLibraryCacheDuration > 0) { - var retVal = ApplicationContext.Current.ApplicationCache.GetCacheItem( + var xml = ApplicationContext.Current.ApplicationCache.GetCacheItem( string.Format( "{0}_{1}", CacheKeys.MemberLibraryCacheKey, MemberId), TimeSpan.FromSeconds(UmbracoConfig.For.UmbracoSettings().Content.UmbracoLibraryCacheDuration), () => GetMemberDo(MemberId)); - if (retVal != null) - return retVal.CreateNavigator().Select("/"); + if (xml != null) + { + return xml.CreateNavigator().Select("/"); + } } else { return GetMemberDo(MemberId).CreateNavigator().Select("/"); } - } - catch + catch (Exception ex) { + LogHelper.Error("An error occurred looking up member", ex); } - XmlDocument xd = new XmlDocument(); + + LogHelper.Debug("No member result for id {0}", () => MemberId); + + var xd = new XmlDocument(); xd.LoadXml(string.Format("No member is maching '{0}'", MemberId)); return xd.CreateNavigator().Select("/"); } - private static XmlDocument GetMemberDo(int MemberId) + private static XElement GetMemberDo(int MemberId) { - Member m = new Member(MemberId); - XmlDocument mXml = new XmlDocument(); - mXml.LoadXml(m.ToXml(mXml, false).OuterXml); - return mXml; + var member = ApplicationContext.Current.Services.MemberService.GetById(MemberId); + if (member == null) return null; + var serializer = new EntityXmlSerializer(); + var serialized = serializer.Serialize( + ApplicationContext.Current.Services.DataTypeService, member); + return serialized; } /// @@ -1365,8 +1383,9 @@ namespace umbraco nav.MoveToId(HttpContext.Current.Items["pageID"].ToString()); return nav.Select("."); } - catch + catch (Exception ex) { + LogHelper.Error("Could not retrieve current xml node", ex); } XmlDocument xd = new XmlDocument();