PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

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

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