/// <summary> /// Gets an umbraco node, based on its node id. /// </summary> /// <param name="nodeId">The node id.</param> /// <returns></returns> /// <remarks></remarks> public virtual UmbracoNode GetByNodeId(int nodeId) { var nativeNode = new Node(nodeId); var node = new UmbracoNode() { Name = nativeNode.Name.Replace(" ", "-").Replace(".", "_"), Url = GetUrlPath(nodeId), UpdateDate = nativeNode.UpdateDate}; LoadChildrenIds(nativeNode, node); LoadParentId(nativeNode, node); LoadProperties(nativeNode, node); LoadDocumentTypeId(nativeNode, node); return node; }
/// <summary> /// Extracts the images from the node's properties. /// </summary> /// <param name="html">The property HTML.</param> /// <param name="node">The node.</param> private static void ExtractImages(string html, UmbracoNode node) { var source = html; var imageTags = Regex.Matches(source, "<img(.|\n)+?>", RegexOptions.IgnoreCase); foreach (var tag in imageTags) { var url = Regex.Match(tag.ToString(), "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value; if (!url.ToLower().StartsWith("http:") && !url.ToLower().StartsWith("https:")) { url = url.ToLower().StartsWith("/") ? Configuration.Configuration.SitePreferredUrl + url : Configuration.Configuration.SitePreferredUrl + "/" + url; } node.ImageUris.Add(url); } }
/// <summary> /// Loads the properties of the node. /// </summary> /// <remarks></remarks> private static void LoadProperties(Node nativeNode, UmbracoNode node) { node.Properties.Clear(); for (var idx = 0; idx < nativeNode.Properties.Count; idx++) { var propertyValue = nativeNode.Properties[idx].Value; if (!string.IsNullOrEmpty(propertyValue)) { var localLinks = Regex.Matches(propertyValue, "(/\\{localLink\\:[0-9]*\\})"); foreach (var linkExpr in localLinks) { var linkNodeId = linkExpr.ToString().Substring(linkExpr.ToString().IndexOf(":", System.StringComparison.Ordinal) + 1); linkNodeId = linkNodeId.Substring(0, linkNodeId.Length - 1); var linkPath = GetUrlPath(int.Parse(linkNodeId)); propertyValue = propertyValue.Replace(linkExpr.ToString(), linkPath); } } var extractionsOk = true; try { ExtractImages(propertyValue, node); } catch (Exception) { extractionsOk = false; // Abort extraction and continue } try { ExtractLinks(propertyValue, node); } catch (Exception) { extractionsOk = false; // Abort extraction and continue } if (extractionsOk) { // Clear HTML tags propertyValue = Regex.Replace(propertyValue, "<(.|\n)+?>", string.Empty, RegexOptions.IgnoreCase).Trim(); } if (!string.IsNullOrEmpty(propertyValue)) { var property = new UmbracoProperty { Name = nativeNode.Properties[idx].Alias, Value = propertyValue }; node.Properties.Add(property); } } }
private static void LoadParentId(INode nativeNode, UmbracoNode node) { if (nativeNode.Parent != null) { node.ParentId = nativeNode.Parent.Id; } else { node.ParentId = -1; } }
/// <summary> /// Loads the document type id. /// </summary> /// <param name="nativeNode">The native node.</param> /// <param name="node">The node.</param> /// <remarks></remarks> private static void LoadDocumentTypeId(Node nativeNode, UmbracoNode node) { var h = UmbracoSQLHelper.Get(); using ( var reader = h.ExecuteReader("select contentType from [cmsContent] where nodeId = @nodeId", h.CreateParameter("nodeId", nativeNode.Id))) { if (!reader.Read()) { throw new Exception(String.Format("Document type id not found for node id '{0}'", nativeNode.Id)); } node.DocumentTypeId = reader.GetInt("contentType"); } }
private static void LoadChildrenIds(Node nativeNode, UmbracoNode node) { node.ChildrenIds = new int[nativeNode.Children.Count]; for (var idx = 0; idx < nativeNode.Children.Count; idx++) { node.ChildrenIds[idx] = nativeNode.Children[idx].Id; } }
public void GenerateNode() { var focusedNode = new UmbracoNode { Name = "FocusedNode", ChildrenIds = new int[] { 2 }, DocumentTypeId = 15, ParentId = 3 }; focusedNode.Properties.Add(new UmbracoProperty() { Name = "title", Value = "Focused node" }); focusedNode.Properties.Add(new UmbracoProperty() { Name = "content4testing", Value = "Test content" }); var childNode = new UmbracoNode { Name = "ChildNode" }; var parentNode = new UmbracoNode { Name = "ParentNode" }; var mockNodeRepo = new Mock<IUmbracoNodeRepository>(); mockNodeRepo.Setup(x => x.GetByNodeId(1)).Returns(focusedNode); mockNodeRepo.Setup(x => x.GetByNodeId(2)).Returns(childNode); mockNodeRepo.Setup(x => x.GetByNodeId(3)).Returns(parentNode); var titleDt = new UmbracoPropertyType { DatabaseType = "nvarchar", Name = "title" }; var contentDt = new UmbracoPropertyType { DatabaseType = "nvarchar", Name = "content" }; var contentTestNameDt = new UmbracoPropertyType { DatabaseType = "nvarchar", Name = "content4testing" }; var mockPropTypeRepo = new Mock<IUmbracoPropertyTypeRepository>(); mockPropTypeRepo.Setup(x => x.GetForNativeDocumentTypePropertyAlias(15, "title")) .Returns(titleDt); mockPropTypeRepo.Setup(x => x.GetForNativeDocumentTypePropertyAlias(15, "content")) .Returns(contentDt); mockPropTypeRepo.Setup(x => x.GetForNativeDocumentTypePropertyAlias(15, "content4testing")) .Returns(contentTestNameDt); var dt15 = new UmbracoDocumentType() { AllowedContentTypeChildrenIds = new int[] { }, InherritanceParentId = -1, Name = "TestDocumentType", PropertyTypes = new List<UmbracoPropertyType>() { new UmbracoPropertyType() {DatabaseType = "nvarchar", Name = "title"}, new UmbracoPropertyType() {DatabaseType = "nvarchar", Name = "content"} } }; var mockDocTypeRepo = new Mock<IUmbracoDocumentTypeRepository>(); mockDocTypeRepo.Setup(x => x.GetById(15)) .Returns(dt15); var resourceFactory = new ResourceFactory(mockNodeRepo.Object, mockPropTypeRepo.Object, mockDocTypeRepo.Object); var node = resourceFactory.GenerateNode(1); Assert.AreEqual("http://localhost/rdf/resource/FocusedNode", node.Iri); Assert.AreEqual("http://localhost/rdf/resource/ParentNode", node.ParentIri); Assert.AreEqual("http://localhost/rdf/resource/ChildNode", node.ChildrenIris.First()); Assert.IsTrue(node.Properties.Any(x => x.PropertyType.Iri == "http://localhost/rdf/ontology#hasTitle" && x.PropertyType.DatabaseType == "nvarchar" && x.Value == "Focused node")); Assert.IsTrue(node.Properties.Any(x => x.PropertyType.Iri == "http://localhost/rdf/ontology#hasTestedContent" && x.PropertyType.DatabaseType == "nvarchar" && x.Value == "Test content")); }