SearchPageBase.cs
上传用户:huiyue
上传日期:2022-04-08
资源大小:1429k
文件大小:8k
源码类别:

搜索引擎

开发平台:

ASP/ASPX

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Collections;
  11. using System.Collections.Specialized;
  12. using System.Xml.Serialization;
  13. using Searcharoo.Common;
  14. namespace Searcharoo.WebApplication
  15. {
  16.     /// <summary>
  17.     /// Search.aspx base class
  18.     /// </summary>
  19.     public class SearchPageBase : Page
  20.     {
  21.         protected SearchControlBase ucSearchPanelHeader;
  22.         protected SearchControlBase ucSearchPanelFooter;
  23.         protected Repeater SearchResults;
  24.         protected Panel lblNoSearchResults;
  25.         #region Private Fields: _WordCount, _ErrorMessage, _Catalog, _SearchTerm, _PagedResults, _DisplayTime, _Matches, _NumberOfMatches
  26.         /// <summary>Displayed in HTML - count of words IN CATALOG (not results)</summary>
  27.         protected int _WordCount;
  28.         /// <summary>Displayed in HTML - error message IF an error occurred</summary>
  29.         protected string _ErrorMessage = String.Empty;
  30.         /// <summary>Get from Cache</summary>
  31.         protected Catalog _Catalog = null;
  32.         /// <summary>Get from Cache [v7]</summary>
  33.         protected Cache _Cache = null;
  34.         protected string _SearchTerm = String.Empty;
  35.         /// <summary>Datasource to bind the results collection to, for paged display</summary>
  36.         protected PagedDataSource _PagedResults = new PagedDataSource();
  37.         /// <summary>Display string: time the search too</summary>
  38.         protected string _DisplayTime;
  39.         /// <summary>Display string: matches (links and number of)</summary>
  40.         protected string _Matches = "";
  41.         /// <summary>Display string: Number of pages that match the query</summary>
  42.         protected string _NumberOfMatches;
  43.         protected int _Geocoded;
  44.         #endregion
  45.         /// <summary>
  46.         /// Available to override in the Kml page which filters out non-geocoded results
  47.         /// </summary>
  48.         protected virtual SortedList GetSearchResults (Searcharoo.Engine.Search se)
  49.         {
  50.             return se.GetResults(this.SearchQuery, _Catalog);
  51.         }
  52.         /// <summary>
  53.         /// Available to override in the Kml page, which doesn't support 'paging'
  54.         /// </summary>
  55.         protected virtual int MaxResultsPerPage
  56.         {
  57.             get
  58.             {
  59.                 return Preferences.ResultsPerPage;
  60.             }
  61.         }
  62.         protected string SearchQuery
  63.         {
  64.             get
  65.             {
  66.                 if (string.IsNullOrEmpty(Request.QueryString[Preferences.QuerystringParameterName]))
  67.                 {
  68.                     return string.Empty;
  69.                 }
  70.                 else
  71.                 {
  72.                     return Request.QueryString[Preferences.QuerystringParameterName].ToString().Trim(' ');
  73.                 }
  74.             }
  75.         }
  76.         /// <summary>
  77.         /// ALL processing happens here, since we are not using ASP.NET controls or events.
  78.         /// Page_Load will:
  79.         /// * check the Cache for a catalog to use 
  80.         /// * if not, check the filesystem for a serialized cache
  81.         /// * and if STILL not, Server.Transfer to the Spider to build a new cache
  82.         /// * check the QueryString for search arguments (and if so, do a search)
  83.         /// * otherwise just show the HTML of this page - a blank search form
  84.         /// </summary>
  85.         public void Page_Load()
  86.         {
  87.             // prevent Searcharoo from indexing itself (ie. it's own results page)
  88.             if (Request.UserAgent.ToLower().IndexOf("searcharoo") > 0) { Response.Clear(); Response.End(); return; }
  89.             bool getCatalog = false;
  90.             try
  91.             {   // see if there is a catalog object in the cache
  92.                 _Catalog = (Catalog)Cache["Searcharoo_Catalog"];
  93.                 _WordCount = _Catalog.Length; // if so, get the _WordCount
  94.                 _Cache = (Searcharoo.Common.Cache)Cache["Searcharoo_Catalog"];
  95.             }
  96.             catch (Exception ex)
  97.             {
  98.                 // otherwise, we'll need to build the catalog
  99.                 Trace.Write("Catalog object unavailable : building a new one ! " + ex.ToString() );
  100.                 _Catalog = null; // in case
  101.                 _Cache = null;
  102.             }
  103.             ucSearchPanelHeader.WordCount = _WordCount;
  104.             ucSearchPanelFooter.WordCount = _WordCount;
  105.             if (null == _Catalog)
  106.             {
  107.                 getCatalog = true;
  108.             }
  109.             else if (_Catalog.Length == 0)
  110.             {
  111.                 getCatalog = true;
  112.             }
  113.             if (getCatalog)
  114.             {
  115.                 // No catalog 'in memory', so let's look for one
  116.                 // First, for a serialized version on disk
  117.                 _Catalog = Catalog.Load(); // returns null if not found
  118.                 _Cache = Searcharoo.Common.Cache.Load(); // [v7]
  119.                 _Catalog.FileCache = _Cache;
  120.                 // Still no Catalog, so we have to start building a new one
  121.                 if (null == _Catalog)
  122.                 {
  123.                     //    Server.Transfer("SearchSpider.aspx");
  124.                     _Catalog = (Catalog)Cache["Searcharoo_Catalog"];
  125.                     _Cache = (Searcharoo.Common.Cache)Cache["Searcharoo_Cache"];
  126.                     Trace.Write("Catalog retrieved from Cache[] " + _Catalog.Words);
  127.                 }
  128.                 else
  129.                 { // Yep, there was a serialized catalog file
  130.                     // Don't forget to add to cache for next time (the Spider does this too)
  131.                     Cache["Searcharoo_Catalog"] = _Catalog;
  132.                     Cache["Searcharoo_Cache"] = _Cache;
  133.                     Trace.Write("Deserialized catalog and put in Cache[] " + _Catalog.Words);
  134.                 }
  135.             }
  136.             if (this.SearchQuery == "")
  137.             {
  138.                 //ucSearchPanelHeader.ErrorMessage = "Please type a word (or words) to search for<br>";
  139.                 ucSearchPanelFooter.Visible = false;
  140.                 ucSearchPanelFooter.IsFooter = true;
  141.                 ucSearchPanelHeader.IsSearchResultsPage = false;
  142.             }
  143.             else
  144.             {
  145.                 //refactored into class - catalog can be build via a console application as well as the SearchSpider.aspx page
  146.                 Searcharoo.Engine.Search se = new Searcharoo.Engine.Search();
  147.                 SortedList output = this.GetSearchResults(se); // se.GetResults(this.SearchQuery, _Catalog);
  148.                 _NumberOfMatches = output.Count.ToString();
  149.                 if (output.Count > 0)
  150.                 {
  151.                     _PagedResults.DataSource = output.GetValueList();
  152.                     _PagedResults.AllowPaging = true;
  153.                     _PagedResults.PageSize = MaxResultsPerPage; //;Preferences.ResultsPerPage; //10;
  154.                     _PagedResults.CurrentPageIndex = Request.QueryString["page"] == null ? 0 : Convert.ToInt32(Request.QueryString["page"]) - 1;
  155.                     _Matches = se.SearchQueryMatchHtml;
  156.                     _DisplayTime = se.DisplayTime;
  157.                     _Geocoded = se.GeocodedMatches;
  158.                     SearchResults.DataSource = _PagedResults;
  159.                     SearchResults.DataBind();
  160.                 }
  161.                 else
  162.                 {
  163.                     lblNoSearchResults.Visible = true;
  164.                 }
  165.                 // Set the display info in the top & bottom user controls
  166.                 ucSearchPanelHeader.Word = ucSearchPanelFooter.Word = this.SearchQuery;
  167.                 ucSearchPanelFooter.Visible = true;
  168.                 ucSearchPanelFooter.IsFooter = true;
  169.                 ucSearchPanelHeader.IsSearchResultsPage = true;
  170.             }
  171.         } // Page_Load
  172.         public string CreatePageUrl(string searchFor, int pageNumber)
  173.         {
  174.             return "Search.aspx?" + Preferences.QuerystringParameterName + "=" + this.SearchQuery + "&page=" + pageNumber;
  175.         }
  176.     }
  177. }