protected void PrintPage() { long t1 = Environment.TickCount; DateTime sBegin, sEnd; string newKey, sAuthor; Hashtable hnick = Functions.hashNick; Hashtable hrating = Functions.hashRating; string chk = checkQuery(hnick, out sBegin, out sEnd, out sAuthor, out newKey); if (chk != "") { Response.Write("<br />" + chk); return; } if (search == null) //第一次搜索,加载搜索模块 { search = new SearchMessage(Server.MapPath("Data") + "\\"); } SearchResult result = search.Search(newKey); SqlConnection conn = new SqlConnection(connstr); conn.Open(); if (result.documentID.Length == 0) { Response.Write(string.Format("<br />" + "找不到和您的查询 \"<strong>{0}</strong>\" 相符的内容或信息。", key)); } else { StringBuilder content = new StringBuilder(); if (sBegin != begin || sEnd != end) { initMinMaxID(conn, sBegin, sEnd);//第一次搜索时间,加载时间加速模块 int min = minID[sBegin]; int max = maxID[sEnd]; List <int> res = new List <int>(); List <double> tfidf = new List <double>(); for (int i = 0; i < result.documentID.Length; i++) { if (result.documentID[i] >= min && result.documentID[i] <= max) { res.Add(result.documentID[i]); tfidf.Add(result.tfidf[i]); } } result.documentID = res.ToArray(); result.tfidf = tfidf.ToArray(); } int cnt = 0; int pageNum = (result.documentID.Length - 1) / 10 + 1; if (pageNum > maxPage) { pageNum = maxPage; } if (page > pageNum) { page = pageNum; } if (rank >= 2) { Array.Sort(result.documentID); if (rank == 3) { Array.Reverse(result.documentID); } } for (int j = (page - 1) * 10; j < result.documentID.Length && cnt < 10; j++) { SqlCommand cmd = new SqlCommand("SELECT [Message],[qq],[time],[qun],[groupnum] FROM [data] WHERE [id]=@id", conn); cmd.Parameters.Add("id", SqlDbType.Int).Value = result.documentID[j]; SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); string msg = PreProcessUtility.HTML2Text((string)dr["message"]); msg = search.getSnippet(result.words, msg); string qq = ((string)dr["qq"]).Trim(); int qun = (int)dr["qun"]; DateTime time = (DateTime)dr["time"]; int msgCount = (int)dr["groupnum"]; dr.Close(); content.Append(string.Format("<tr><td><div class='a'><div class='b'><a href=\"Rating.aspx?qq={0}\" class=\"{4}\">{3}({0})</a></div>{1} <a href=\"Message.aspx?date={6}&qun={7}#m{5}\">查看</a>{8}</div><div class='c'>{2}</div></td></tr>\n", qq, time, msg, hnick[qq], Functions.GetColor((double)hrating[qq]), result.documentID[j], time.ToShortDateString(), qun, (msgCount == 1 ? "" : " " + msgCount + " 条相同消息"))); cnt++; } if (pageNum != 1) { content.Append("<tr><th class='sub'>"); int pb = Math.Max(1, page - 5); int pe = Math.Min(pageNum, page + 5); if (pb > 1) { content.Append(string.Format(" <a href=\"Search.aspx?q={0}&p={1}{2}\"><strong>...</strong></a> ", key, Math.Max(pb - 5, 1), getRankStr(rank))); } for (int i = pb; i <= pe; i++) { if (i == page) { content.Append(string.Format(" [{0}] ", i)); } else { content.Append(string.Format(" <a href=\"Search.aspx?q={0}&p={1}{2}\">{1}</a> ", key, i, getRankStr(rank))); } } if (pe < pageNum) { content.Append(string.Format(" <a href=\"Search.aspx?q={0}&p={1}{2}\"><strong>...</strong></a> ", key, Math.Min(pe + 5, maxPage), getRankStr(rank))); } content.Append("</th></tr>"); } content.Append("</table>"); string strDate = ""; if (sBegin != begin || sEnd != end) { if (sBegin == begin) { strDate = string.Format("从开始到 <strong>{0}</strong> ", sEnd.ToShortDateString()); } else if (sEnd == end) { strDate = string.Format("从 <strong>{0}</strong> 到现在", sBegin.ToShortDateString()); } else { if (sBegin == sEnd) { strDate = string.Format("在 <strong>{0}</strong> ", sBegin.ToShortDateString()); } else { strDate = string.Format("从 <strong>{0}</strong> 到 <strong>{1}</strong> ", sBegin.ToShortDateString(), sEnd.ToShortDateString()); } } } string strAuthor = ""; if (sAuthor != "") { strAuthor = string.Format("由 <a href=\"Rating.aspx?qq={1}\" class=\"{2}\">{0}({1})</a> ", (string)hnick[sAuthor], sAuthor, Functions.GetColor((double)hrating[sAuthor])); } string strDescription = strDate + strAuthor; if (strDescription != "") { strDescription += " 发表的"; } string strSort = ""; if (rank == 1) { strSort = string.Format("<strong>默认排序</strong> | <a href=\"Search.aspx?q={0}&p={1}&r=asc\">时间排序↑</a>", key, page); } else if (rank == 2) { strSort = string.Format("<a href=\"Search.aspx?q={0}&p={1}\">默认排序</a> | <a href=\"Search.aspx?q={0}&p={1}&r=dec\"><strong>时间排序↑</strong></a>", key, page); } else { strSort = string.Format("<a href=\"Search.aspx?q={0}&p={1}\">默认排序</a> | <a href=\"Search.aspx?q={0}&p={1}&r=asc\"><strong>时间排序↓</strong></a>", key, page); } Response.Write(string.Format("<table class='full'><thead><tr><th style=\"font-weight:normal\">搜索 {3} <strong>{0}</strong>,获得 {1} 条搜索结果 (用时 {2:F3} 秒) | {4}</th></tr></thead>\n", newKey, result.documentID.Length, (Environment.TickCount - t1) / 1000.0, strDescription, strSort)); Response.Write(content.ToString()); } SqlCommand cmdRec = new SqlCommand("INSERT INTO [SearchHistory] ([keyword],[IP],[time],[sort],[page]) VALUES (@keyword, @ip, @time, @sort, @page)", conn); cmdRec.Parameters.Add("keyword", SqlDbType.Char).Value = key; cmdRec.Parameters.Add("ip", SqlDbType.Char).Value = Request.ServerVariables["REMOTE_ADDR"]; cmdRec.Parameters.Add("time", SqlDbType.DateTime).Value = DateTime.Now; cmdRec.Parameters.Add("sort", SqlDbType.TinyInt).Value = rank; cmdRec.Parameters.Add("page", SqlDbType.TinyInt).Value = page; cmdRec.ExecuteNonQuery(); conn.Close(); }
protected void PrintPage() { SqlConnection conn = new SqlConnection(connstr); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT TOP 11 [Message],[groupNum] FROM [data] WHERE [time]>=@t1 and [time]<@t2 AND [groupNum]>1 ORDER BY [groupNum] DESC", conn); cmd.Parameters.Add("t1", SqlDbType.DateTime).Value = dt; cmd.Parameters.Add("t2", SqlDbType.DateTime).Value = dt.AddDays(1); Response.Write("<table class='full'>\n"); Response.Write(string.Format("<thead><tr><th colspan='3'>热门消息</th></tr></thead>\n")); Response.Write("<tbody><tr><th>排名</th><th>消息</th><th>次数</th></tr>\n"); SqlDataReader dr = cmd.ExecuteReader(); int cnt = 0; while (dr.Read()) { string s = PreProcessUtility.HTML2Text((string)dr["Message"]); if (s != "") { cnt++; Response.Write(string.Format("<tr><th class='sub'>{0}</th><td>{1}</td><td>{2}</td></tr>\n", cnt, dr["Message"], dr["groupNum"])); } } Response.Write("</tbody></table>"); dr.Close(); cmd = new SqlCommand("SELECT [Message] FROM [data] WHERE [time]>=@t1 and [time]<@t2", conn); cmd.Parameters.Add("t1", SqlDbType.DateTime).Value = dt; cmd.Parameters.Add("t2", SqlDbType.DateTime).Value = dt.AddDays(1); dr = cmd.ExecuteReader(); Dictionary <string, int> picCount = new Dictionary <string, int>(); while (dr.Read()) { string txt = (string)dr["Message"]; int p = txt.IndexOf("<IMG"); while (p != -1) { int e = txt.IndexOf(">", p); string s = txt.Substring(p, e - p + 1); txt = txt.Remove(p, e - p + 1); int st = s.IndexOf("\""); int ed = s.IndexOf("\"", st + 1); string url = s.Substring(st + 1, ed - st - 1); if (url.Contains("msg")) { if (url == "msg/{3829986E-A56D-B399-818B-F5F378ACFBF1}.gif") { url = "msg/{CC350580-F5C6-7354-D337-29AD641E0C54}.gif"; } if (picCount.ContainsKey(url)) { picCount[url]++; } else { picCount.Add(url, 1); } } p = txt.IndexOf("<IMG"); } } dr.Close(); conn.Close(); int[] cnts = new int[picCount.Count]; string[] paths = new string[picCount.Count]; cnt = 0; foreach (var o in picCount) { cnts[cnt] = o.Value; paths[cnt] = o.Key; cnt++; } Array.Sort(cnts, paths); Array.Reverse(cnts); Array.Reverse(paths); cnt = 0; Response.Write("<hr /><table class='full'>\n"); Response.Write(string.Format("<thead><tr><th colspan='3'>热门图片</th></tr></thead>\n")); Response.Write("<tbody><tr><th>排名</th><th>图片</th><th>次数</th></tr>\n"); for (int i = 0; i < 5 && i < cnts.Length && cnts[i] > 1; i++) { cnt++; Response.Write(string.Format("<tr><th class='sub'>{0}</th><td><img src=\"{1}\" onload=\"javascript:if(480<this.width){{this.width=480}}\" onerror=\"javascript:this.src='img/e.gif'\" /></td><td>{2}</td></tr>\n", cnt, paths[i], cnts[i])); } Response.Write("</tbody></table>"); }