PageRenderTime 41ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/search.aspx.cs

#
C# | 261 lines | 203 code | 46 blank | 12 comment | 25 complexity | 1d7346d088577adc46114f81a8df83a0 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. #region Using
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Web;
  5. using System.Net;
  6. using System.Xml;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.HtmlControls;
  10. using System.Text.RegularExpressions;
  11. using BlogEngine.Core;
  12. #endregion
  13. public partial class search : BlogEngine.Core.Web.Controls.BlogBasePage
  14. {
  15. private const int PAGE_SIZE = 20;
  16. #region Event handlers
  17. protected override void OnLoad(EventArgs e)
  18. {
  19. base.OnLoad(e);
  20. rep.ItemDataBound += new RepeaterItemEventHandler(rep_ItemDataBound);
  21. var term = Request.QueryString["q"];
  22. if (!Utils.StringIsNullOrWhitespace(term))
  23. {
  24. bool includeComments = (Request.QueryString["comment"] == "true");
  25. var encodedTerm = Server.HtmlEncode(term);
  26. Page.Title = Server.HtmlEncode(Resources.labels.searchResultsFor) + " '" + encodedTerm + "'";
  27. h1Headline.InnerHtml = Resources.labels.searchResultsFor + " '" + encodedTerm + "'";
  28. Uri url;
  29. if (!Uri.TryCreate(term, UriKind.Absolute, out url))
  30. {
  31. List<IPublishable> list = Search.Hits(term, includeComments);
  32. BindSearchResult(list);
  33. }
  34. else
  35. {
  36. SearchByApml(url);
  37. }
  38. }
  39. else
  40. {
  41. Page.Title = Resources.labels.search;
  42. h1Headline.InnerHtml = Resources.labels.search;
  43. }
  44. }
  45. private void SearchByApml(Uri url)
  46. {
  47. List<IPublishable> list = new List<IPublishable>();
  48. try
  49. {
  50. Dictionary<Uri, XmlDocument> docs = Utils.FindSemanticDocuments(url, "apml");
  51. if (docs.Count > 0)
  52. {
  53. foreach (Uri key in docs.Keys)
  54. {
  55. list = Search.ApmlMatches(docs[key], 30);
  56. Page.Title = "APML matches for '" + Server.HtmlEncode(key.ToString()) + "'";
  57. break;
  58. }
  59. }
  60. else
  61. {
  62. Page.Title = "APML matches for '" + Server.HtmlEncode(Request.QueryString["q"]) + "'";
  63. }
  64. h1Headline.InnerText = Page.Title;
  65. }
  66. catch
  67. {
  68. }
  69. BindSearchResult(list);
  70. }
  71. /// <summary>
  72. /// Handles the ItemDataBound event of the rep control.
  73. /// </summary>
  74. /// <param name="sender">The source of the event.</param>
  75. /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs"/> instance containing the event data.</param>
  76. void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
  77. {
  78. HtmlGenericControl control = (HtmlGenericControl)e.Item.FindControl("type");
  79. string type = "<strong>" + Resources.labels.type + "</strong>: {0}";
  80. string categories = "<strong>" + Resources.labels.categories + "</strong> : {0}";
  81. string tags = "<strong>" + Resources.labels.tags + "</strong> : {0}";
  82. string keywords = "<strong>" + Resources.labels.keywords + "</strong> : {0} ";
  83. if (e.Item.DataItem is Comment)
  84. {
  85. control.InnerHtml = string.Format(type, Resources.labels.comment);
  86. }
  87. else if (e.Item.DataItem is Post)
  88. {
  89. Post post = (Post)e.Item.DataItem;
  90. string text = string.Format(type, Resources.labels.post);
  91. if (post.Categories.Count > 0)
  92. {
  93. string cat = string.Empty;
  94. foreach (Category category in post.Categories)
  95. {
  96. cat += category.Title + ", ";
  97. }
  98. text += "<br />" + string.Format(categories, cat.Substring(0, cat.Length - 2));
  99. }
  100. if (post.Tags.Count > 0)
  101. {
  102. string t = string.Empty;
  103. foreach (string tag in post.Tags)
  104. {
  105. t += tag + ", ";
  106. }
  107. text += "<br />" + string.Format(tags, t.Substring(0, t.Length - 2));
  108. }
  109. control.InnerHtml = text;
  110. }
  111. else if (e.Item.DataItem is BlogEngine.Core.Page)
  112. {
  113. BlogEngine.Core.Page page = (BlogEngine.Core.Page)e.Item.DataItem;
  114. string text = string.Format(type, Resources.labels.page);
  115. if (!string.IsNullOrEmpty(page.Keywords))
  116. {
  117. text += "<br />" + string.Format(keywords, page.Keywords);
  118. }
  119. control.InnerHtml = text;
  120. }
  121. }
  122. #endregion
  123. #region Data binding
  124. private void BindSearchResult(List<IPublishable> list)
  125. {
  126. int page = 1;
  127. string queryStringPage = Request.QueryString["page"];
  128. if (!Utils.StringIsNullOrWhitespace(queryStringPage))
  129. {
  130. int.TryParse(queryStringPage, out page);
  131. // Negative numbers can be passed, throwing an ArgumentOutOfRange exception.
  132. if (page < 1)
  133. {
  134. page = 1;
  135. }
  136. }
  137. int start = PAGE_SIZE * (page - 1);
  138. if (start <= list.Count - 1)
  139. {
  140. int size = PAGE_SIZE;
  141. if (start + size > list.Count)
  142. {
  143. size = list.Count - start;
  144. }
  145. rep.DataSource = list.GetRange(start, size);
  146. rep.DataBind();
  147. BindPaging(list.Count, page - 1);
  148. }
  149. }
  150. private void BindPaging(int results, int page)
  151. {
  152. if (results <= PAGE_SIZE)
  153. {
  154. return;
  155. }
  156. decimal pages = Math.Ceiling((decimal)results / (decimal)PAGE_SIZE);
  157. HtmlGenericControl ul = new HtmlGenericControl("ul");
  158. ul.Attributes.Add("class", "paging");
  159. string q = Server.HtmlEncode(Request.QueryString["q"]);
  160. string comment = Request.QueryString["comment"];
  161. for (int i = 0; i < pages; i++)
  162. {
  163. HtmlGenericControl li = new HtmlGenericControl("li");
  164. if (i == page)
  165. {
  166. li.Attributes.Add("class", "active");
  167. }
  168. HtmlAnchor a = new HtmlAnchor();
  169. a.InnerHtml = (i + 1).ToString();
  170. string comm = comment;
  171. if (comm != null)
  172. {
  173. comm = "&amp;comment=true";
  174. }
  175. a.HRef = "?q=" + q + comm + "&amp;page=" + (i + 1);
  176. li.Controls.Add(a);
  177. ul.Controls.Add(li);
  178. }
  179. Paging.Controls.Add(ul);
  180. }
  181. #endregion
  182. #region Data manipulation
  183. /// <summary>
  184. /// Removes the comment anchor from the URL
  185. /// </summary>
  186. protected string ShortenUrl(string uri)
  187. {
  188. if (!uri.Contains("#"))
  189. return uri;
  190. int index = uri.IndexOf("#");
  191. return uri.Substring(0, index);
  192. }
  193. /// <summary>
  194. /// Shortens the content to fit to a search result
  195. /// </summary>
  196. protected string GetContent(string description, string content)
  197. {
  198. string text = string.Empty;
  199. if (!string.IsNullOrEmpty(description))
  200. {
  201. text = description;
  202. }
  203. else
  204. {
  205. text = Utils.StripHtml(content);
  206. if (text.Length > 200)
  207. {
  208. text = text.Substring(0, 200) + " ...";
  209. }
  210. text = "\"" + text.Trim() + "\"";
  211. }
  212. return text;
  213. }
  214. #endregion
  215. }