Repository navigation
Expand file tree
/
Copy pathuQuery.cs
More file actions
274 lines (238 loc) · 7.53 KB
/
Copy pathuQuery.cs
File metadata and controls
274 lines (238 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;
using uComponents.Core.uQueryExtensions;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.DataLayer;
namespace uComponents.Core
{
/// <summary>
/// Static helper methods, previously this class was UmbracoHelper
/// </summary>
[Obsolete(Constants.uQueryLegacyObsoleteMessage)]
public static partial class uQuery
{
/// <summary>
/// Gets the SqlHelper used by Umbraco
/// </summary>
public static ISqlHelper SqlHelper
{
get
{
return Application.SqlHelper;
}
}
/// <summary>
/// Constructs the XML source from the cmsContentXml table used for Media and Members.
/// </summary>
/// <param name="umbracoObjectType">an UmbracoObjectType value</param>
/// <returns>XML built from the cmsContentXml table</returns>
public static XmlDocument GetPublishedXml(UmbracoObjectType umbracoObjectType)
{
try
{
var hierarchy = new Dictionary<int, List<int>>();
var nodeIndex = new Dictionary<int, XmlNode>();
var xmlDoc = new XmlDocument();
var sql = "SELECT umbracoNode.id, umbracoNode.parentId, umbracoNode.sortOrder, cmsContentXml.xml FROM umbracoNode INNER JOIN cmsContentXml ON cmsContentXml.nodeId = umbracoNode.id AND umbracoNode.nodeObjectType = @nodeObjectType ORDER BY umbracoNode.level, umbracoNode.sortOrder";
using (var dr = uQuery.SqlHelper.ExecuteReader(sql, uQuery.SqlHelper.CreateParameter("@nodeObjectType", umbracoObjectType.GetGuid())))
{
while (dr.Read())
{
var currentId = dr.GetInt("id");
var parentId = dr.GetInt("parentId");
var xml = dr.GetString("xml");
// and parse it into a DOM node
xmlDoc.LoadXml(xml);
nodeIndex.Add(currentId, xmlDoc.FirstChild);
// Build the content hierarchy
List<int> children;
if (!hierarchy.TryGetValue(parentId, out children))
{
// No children for this parent, so add one
children = new List<int>();
hierarchy.Add(parentId, children);
}
children.Add(currentId);
}
// Set top level wrapper element
switch (umbracoObjectType)
{
case UmbracoObjectType.Media:
xmlDoc.LoadXml("<Media id=\"-1\"/>");
break;
case UmbracoObjectType.Member:
xmlDoc.LoadXml("<Members id=\"-1\"/>");
break;
default:
xmlDoc.LoadXml("<Nodes id=\"-1\"/>");
break;
}
// Start building the content tree recursively from the root (-1) node
GenerateXmlDocument(hierarchy, nodeIndex, -1, xmlDoc.DocumentElement);
return xmlDoc;
}
}
catch (Exception ex)
{
Log.Add(LogTypes.Error, -1, ex.Message);
}
return null;
}
/// <summary>
/// Checks the Umbraco XML Schema version in use
/// </summary>
/// <returns>true if using the old XML schema, else false if using the new XML schema</returns>
public static bool IsLegacyXmlSchema()
{
bool isLegacyXmlSchema;
try
{
isLegacyXmlSchema = UmbracoSettings.UseLegacyXmlSchema;
}
catch (MissingMethodException)
{
// Method doens't exist so must be using the legacy schema
isLegacyXmlSchema = true;
}
return isLegacyXmlSchema;
}
/// <summary>
/// build a string array from a csv
/// </summary>
/// <param name="csv">string of comma seperated values</param>
/// <returns>An array of node ids as string.</returns>
internal static string[] GetCsvIds(string csv)
{
string[] ids = null;
if (!string.IsNullOrEmpty(csv))
{
ids = csv.Split(Constants.Common.COMMA).Select(s => s.Trim()).ToArray();
}
return ids;
}
/// <summary>
/// Gets Ids from known XML fragments (as saved by the MNTP / XPath CheckBoxList)
/// </summary>
/// <param name="xml">The Xml</param>
/// <returns>An array of node ids as integer.</returns>
internal static int[] GetXmlIds(string xml)
{
var ids = new List<int>();
if (!string.IsNullOrEmpty(xml))
{
using (var xmlReader = XmlReader.Create(new StringReader(xml)))
{
try
{
xmlReader.Read();
// Check name of first element
switch (xmlReader.Name)
{
case "MultiNodePicker":
case "XPathCheckBoxList":
case "CheckBoxTree":
// Position on first <nodeId>
xmlReader.ReadStartElement();
while (!xmlReader.EOF)
{
if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "nodeId")
{
int id;
if (int.TryParse(xmlReader.ReadElementContentAsString(), out id))
{
ids.Add(id);
}
}
else
{
// Step the reader on
xmlReader.Read();
}
}
break;
}
}
catch
{
// Failed to read as Xml
}
}
}
return ids.ToArray();
}
/// <summary>
/// Gets an Id value from the QueryString
/// </summary>
/// <returns>an id as a string or string.empty</returns>
internal static string GetIdFromQueryString()
{
var queryStringId = string.Empty;
if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["id"]))
{
queryStringId = HttpContext.Current.Request.QueryString["id"];
}
else if (HttpContext.Current.Request.CurrentExecutionFilePathExtension == ".asmx"
&& HttpContext.Current.Request.UrlReferrer != null
&& !string.IsNullOrEmpty(HttpContext.Current.Request.UrlReferrer.Query))
{
// Special case for MNTP CustomTreeService.asmx
queryStringId = HttpUtility.ParseQueryString(HttpContext.Current.Request.UrlReferrer.Query)["id"];
}
return queryStringId;
}
/// <summary>
/// Converts a string array into an integer array.
/// </summary>
/// <param name="items">The string array.</param>
/// <returns>Returns an integer array.</returns>
internal static int[] ConvertToIntArray(string[] items)
{
if (items == null)
return new int[] { };
int n;
return items.Select(s => int.TryParse(s, out n) ? n : 0).ToArray();
}
/// <summary>
/// Generates an XML document.
/// </summary>
/// <param name="hierarchy">The hierarchy.</param>
/// <param name="nodeIndex">Index of the node.</param>
/// <param name="parentId">The parent id.</param>
/// <param name="parentNode">The parent node.</param>
private static void GenerateXmlDocument(IDictionary<int, List<int>> hierarchy, IDictionary<int, XmlNode> nodeIndex, int parentId, XmlNode parentNode)
{
List<int> children;
if (hierarchy.TryGetValue(parentId, out children))
{
var childContainer = uQuery.IsLegacyXmlSchema() || string.IsNullOrEmpty(UmbracoSettings.TEMP_FRIENDLY_XML_CHILD_CONTAINER_NODENAME) ? parentNode : parentNode.SelectSingleNode(UmbracoSettings.TEMP_FRIENDLY_XML_CHILD_CONTAINER_NODENAME);
if (!uQuery.IsLegacyXmlSchema() && !string.IsNullOrEmpty(UmbracoSettings.TEMP_FRIENDLY_XML_CHILD_CONTAINER_NODENAME))
{
if (childContainer == null)
{
childContainer = xmlHelper.addTextNode(parentNode.OwnerDocument, UmbracoSettings.TEMP_FRIENDLY_XML_CHILD_CONTAINER_NODENAME, string.Empty);
parentNode.AppendChild(childContainer);
}
}
foreach (int childId in children)
{
var childNode = nodeIndex[childId];
if (uQuery.IsLegacyXmlSchema() || string.IsNullOrEmpty(UmbracoSettings.TEMP_FRIENDLY_XML_CHILD_CONTAINER_NODENAME))
{
parentNode.AppendChild(childNode);
}
else
{
childContainer.AppendChild(childNode);
}
// Recursively build the content tree under the current child
GenerateXmlDocument(hierarchy, nodeIndex, childId, childNode);
}
}
}
}
}