📄 searchdatastore.cs
字号:
using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using DotNetNuke.Data;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework;
using DotNetNuke.Framework.Providers;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Log.EventLog;
//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2005
// by Shaun Walker ( sales@perpetualmotion.ca ) of Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
namespace DotNetNuke.Services.Search
{
/// -----------------------------------------------------------------------------
/// Namespace: DotNetNuke.Services.Search
/// Project: DotNetNuke.Search.DataStore
/// Class: SearchDataStore
/// -----------------------------------------------------------------------------
/// <summary>
/// The SearchDataStore is an implementation of the abstract SearchDataStoreProvider
/// class
/// </summary>
/// <returns></returns>
/// <remarks>
/// </remarks>
/// <history>
/// [cnurse] 11/15/2004 documented
/// </history>
/// -----------------------------------------------------------------------------
public class SearchDataStore : SearchDataStoreProvider
{
#region "Private Members"
private Hashtable settings;
private Hashtable defaultSettings;
private int maxWordLength;
private int minWordLength;
private bool includeNumbers;
private bool includeCommon;
#endregion
#region "Private Methods"
/// -----------------------------------------------------------------------------
/// <summary>
/// AddIndexWords adds the Index Words to the Data Store
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="IndexId">The Id of the SearchItem</param>
/// <param name="SearchItem">The SearchItem</param>
/// <param name="Language">The Language of the current Item</param>
/// <history>
/// [cnurse] 11/15/2004 documented
/// [cnurse] 11/16/2004 replaced calls to separate content clean-up
/// functions with new call to HtmlUtils.Clean().
/// replaced logic to determine whether word should
/// be indexed by call to CanIndexWord()
/// </history>
/// -----------------------------------------------------------------------------
private void AddIndexWords (int indexId, SearchItemInfo searchItem, string language)
{
Hashtable indexWords = new Hashtable();
Hashtable indexPositions = new Hashtable();
string setting;
//Get the Settings for this Module
settings = SearchDataStoreController.GetSearchSettings(searchItem.ModuleId);
setting = GetSetting("MaxSearchWordLength");
if (setting != null && setting.Length > 0)
{
maxWordLength = int.Parse(setting);
}
setting = GetSetting("MinSearchWordLength");
if (setting != null && setting.Length > 0)
{
minWordLength = int.Parse(setting);
}
setting = GetSetting("SearchIncludeCommon");
includeCommon = (setting == "Y");
setting = GetSetting("SearchIncludeNumeric");
includeNumbers = !(setting == "N");
string content = searchItem.Content;
// clean content
content = HtmlUtils.Clean(content, true);
content = content.ToLower();
//' split content into words
string[] contentWords = content.Split(' ');
// process each word
int intWord = 0;
foreach (string word in contentWords)
{
if (CanIndexWord(word, language))
{
intWord++;
if (!indexWords.ContainsKey(word))
{
indexWords.Add(word, 0);
indexPositions.Add(word, 1);
}
// track number of occurrences of word in content
indexWords[word] = System.Convert.ToInt32(indexWords[word])+ 1;
// track positions of word in content
indexPositions[word] = System.Convert.ToString(indexPositions[word])+ "," + intWord.ToString();
}
}
// get list of words ( non-common )
Hashtable words = GetSearchWords(); // this could be cached
int wordId;
string strWord;
//' iterate through each indexed word
foreach (object word in indexWords.Keys)
{
strWord = System.Convert.ToString(word);
if (words.ContainsKey(strWord))
{
// word is in the DataStore
wordId = System.Convert.ToInt32(words[strWord]);
}
else
{
// add the word to the DataStore
wordId = DataProvider.Instance().AddSearchWord(strWord);
words.Add(strWord, wordId);
}
// add the indexword
int searchItemWordID = DataProvider.Instance().AddSearchItemWord(indexId, wordId, System.Convert.ToInt32(indexWords[strWord]));
DataProvider.Instance().AddSearchItemWordPosition(searchItemWordID, System.Convert.ToString(indexPositions[strWord]));
}
}
/// -----------------------------------------------------------------------------
/// <summary>
/// CanIndexWord determines whether the Word should be indexed
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="strWord">The Word to validate/param>
/// <returns>True if indexable, otherwise false</returns>
/// <history>
/// [cnurse] 11/16/2004 created
/// </history>
/// -----------------------------------------------------------------------------
private bool CanIndexWord(string word, string locale)
{
// get common words for exclusion
Hashtable commonWords = GetCommonWords(locale);
//Determine if Word is actually a number
if (Regex.IsMatch(word, "", RegexOptions.Compiled))
{
//Word is Numeric
if (! includeNumbers)
{
return false;
}
}
else
{
//Word is Non-Numeric
//Determine if Word satisfies Minimum/Maximum length
if (word.Length < minWordLength || word.Length > maxWordLength)
{
return false;
}
else
{
//Determine if Word is a Common Word (and should be excluded)
if (commonWords.ContainsKey(word) && ! includeCommon)
{
return false;
}
}
}
return true;
}
/// -----------------------------------------------------------------------------
/// <summary>
/// GetCommonWords gets a list of the Common Words for the locale
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="Locale">The locale string</param>
/// <returns>A hashtable of common words</returns>
/// <history>
/// [cnurse] 11/15/2004 documented
/// </history>
/// -----------------------------------------------------------------------------
private Hashtable GetCommonWords(string locale)
{
string cacheKey = "CommonWords" + locale;
Hashtable words = DataCache.GetCache(cacheKey) as Hashtable;
if (words == null)
{
words = new Hashtable();
IDataReader dr = DataProvider.Instance().GetSearchCommonWordsByLocale(locale);
try
{
while (dr.Read())
{
words.Add(dr["CommonWord"].ToString(), dr["CommonWord"].ToString());
}
}
finally
{
dr.Close();
dr.Dispose();
}
DataCache.SetCache(cacheKey, words);
}
//End If
return words;
}
private SearchItemInfoCollection GetSearchItems(int moduleId)
{
return new SearchItemInfoCollection(CBO.FillCollection(DataProvider.Instance().GetSearchItems(Null.NullInteger, Null.NullInteger, moduleId), typeof(SearchItemInfo)));
}
/// -----------------------------------------------------------------------------
/// <summary>
/// GetSearchWords gets a list of the current Words in the Database's Index
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>A hashtable of words</returns>
/// <history>
/// [cnurse] 11/15/2004 documented
/// </history>
/// -----------------------------------------------------------------------------
private Hashtable GetSearchWords()
{
string cacheKey = "SearchWords";
Hashtable words = DataCache.GetCache(cacheKey) as Hashtable;
if (words == null)
{
words = new Hashtable();
IDataReader dr = DataProvider.Instance().GetSearchWords();
try
{
while (dr.Read())
{
words.Add(dr["Word"].ToString(), dr["SearchWordsID"]);
}
}
finally
{
dr.Close();
dr.Dispose();
}
DataCache.SetCache(cacheKey, words, TimeSpan.FromMinutes(2));
}
return words;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -