/// <summary> /// 重新获取索引 /// </summary> /// <returns></returns> public ActionResult getIndex() { HtmlAgilityPack.HtmlDocument documnet = new HtmlDocument(); BlogsBLL bloglist = new BlogsBLL(); var list = new List <SearchResult>(); list = bloglist.GetList(t => true).ToList().Select(t => new SearchResult() { blogTag = "", url = "/" + t.BlogUsersSet.UserName + "/" + t.Id + ".html", // t.BlogUrl, id = t.Id, //DocumentNode content = getText(t.BlogContent, documnet), clickQuantity = 3, title = t.BlogTitle, flag = t.UsersId }).ToList(); PanGuLuceneHelper.instance.DeleteAll(); SafetyWriteHelper <SearchResult> .logWrite(list, PanGuLuceneHelper.instance.CreateIndex); return(Content("ok")); }
/// <summary> /// 删除 文章 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Del(int?id) { var userinfo = BLLSession.UserInfoSessioin; List <BlogsDTO> blogs = new List <BlogsDTO>(); int isdelok = -1; if (null != id) { BLL.BlogsBLL blogbll = new BlogsBLL(); blogbll.Del(new ModelDB.Blogs() { Id = (int)id }, true); isdelok = blogbll.save(false); List <SearchResult> list = new List <SearchResult>(); list.Add(new SearchResult() { id = (int)id }); SafetyWriteHelper <SearchResult> .logWrite(list, PanGuLuceneHelper.instance.Delete); } return(Content((isdelok > 0).ToString())); }
/// <summary> /// 根据用户导入cnblog数据 /// </summary> /// <param name="userName"></param> /// <returns></returns> public string Import(string userName, string iszf, string isshowhome, string isshowmyhome) { userName = userName.Trim(); int blosNumber = 0; JavaScriptSerializer jss = new JavaScriptSerializer(); string url = "http://www.cnblogs.com/" + userName + @"/mvc/blog/sidecolumn.aspx"; HtmlAgilityPack.HtmlWeb htmlweb = new HtmlAgilityPack.HtmlWeb(); HtmlAgilityPack.HtmlDocument document = new HtmlDocument(); var docment = htmlweb.Load(url); string userid = GetCnblogUserId(userName); var liS = docment.DocumentNode.SelectNodes("//*[@id='sidebar_categories']/div[1]/ul/li"); foreach (var item in liS) { var tXPath = item.XPath; var href = item.SelectSingleNode(tXPath + "/a").Attributes["href"].Value; var blogtype = htmlweb.Load(href); //var entrylistItem = blogtype.DocumentNode.SelectNodes("//*[@id='mainContent']/div/div[2]/div[@class='entrylistItem']"); var entrylistItem = blogtype.DocumentNode.SelectNodes("//div[@class='entrylistItem']"); if (null == entrylistItem) //做兼容 { entrylistItem = blogtype.DocumentNode.SelectNodes("//div[@class='post post-list-item']"); // } if (null == entrylistItem) { continue; } foreach (var typeitem in entrylistItem) { var typeitemXPath = typeitem.XPath; var typeitemhrefObj = typeitem.SelectSingleNode(typeitemXPath + "/div/a"); if (null == typeitemhrefObj) //做兼容 { typeitemhrefObj = typeitem.SelectSingleNode(typeitemXPath + "/h2/a"); } var typeitemhref = typeitemhrefObj.Attributes["href"].Value; if (IsAreBlog(typeitemhref)) { continue;//说明这篇文章已经备份过了的 } var bloghtml = htmlweb.Load(typeitemhref); var blogcontextobj = bloghtml.DocumentNode.SelectSingleNode("//*[@id='cnblogs_post_body']");//.InnerHtml; if (blogcontextobj == null) { continue; //有可能是加密文章 } var blogcontext = blogcontextobj.InnerHtml; var blogNameObj = bloghtml.DocumentNode.SelectSingleNode("//*[@id='Header1_HeaderTitle']"); if (null == blogNameObj) { blogNameObj = bloghtml.DocumentNode.SelectSingleNode("//*[@id='lnkBlogTitle']"); } try { blogName = blogNameObj.InnerText; } catch (Exception) { } var blogtitle = bloghtml.DocumentNode.SelectSingleNode("//*[@id='cb_post_title_url']").InnerText; var blogurl = bloghtml.DocumentNode.SelectSingleNode("//*[@id='cb_post_title_url']").Attributes["href"].Value; var blogtypetagurl = "http://www.cnblogs.com/mvc/blog/CategoriesTags.aspx?blogApp=" + userName + "&blogId=" + userid + "&postId=" + typeitemhref.Substring(typeitemhref.LastIndexOf('/') + 1, typeitemhref.LastIndexOf('.') - typeitemhref.LastIndexOf('/') - 1); var blogtag = Blogs.Common.Helper.MyHtmlHelper.GetRequest(blogtypetagurl); var jsonobj = jss.Deserialize <Dictionary <string, string> >(blogtag); if (null == jsonobj) { continue;//如果没有 则返回 (这里只能去 数字.html 不能取那种自定义的url) } var tagSplit = jsonobj["Tags"].Split(','); var blogtagid = new List <int>(); for (int i = 0; i < tagSplit.Length; i++) { if (tagSplit[i].Length >= 1 && tagSplit[i].LastIndexOf('<') >= 1) { var blogtagname = tagSplit[i].Substring(tagSplit[i].IndexOf('>') + 1, tagSplit[i].LastIndexOf('<') - tagSplit[i].IndexOf('>') - 1); blogtagid.Add(this.GetTagId(blogtagname, userName)); } } var categoriesSplit = jsonobj["Categories"].Split(','); var blogtypeid = new List <int>(); for (int i = 0; i < categoriesSplit.Length; i++) { if (categoriesSplit[i].Length >= 1 && categoriesSplit[i].LastIndexOf('<') >= 1) { var blogtypename = categoriesSplit[i].Substring(categoriesSplit[i].IndexOf('>') + 1, categoriesSplit[i].LastIndexOf('<') - categoriesSplit[i].IndexOf('>') - 1); blogtypeid.Add(this.GetTypeId(blogtypename, userName)); } } var blogtimeobj = bloghtml.DocumentNode.SelectSingleNode("//*[@id='post-date']"); var blogtime = ""; if (null != blogtimeobj) { blogtime = blogtimeobj.InnerText; } DateTime?createtime = null; var Outcreatetime = DateTime.Now; if (DateTime.TryParse(blogtime, out Outcreatetime)) { createtime = Outcreatetime; } BlogsBLL blog = new BlogsBLL(); var myBlogTags = new BlogTagsBLL().GetList(t => blogtagid.Contains(t.Id), isAsNoTracking: false).ToList(); //.ToList(); var myBlogTypes = new BLL.BlogTypesBLL().GetList(t => blogtypeid.Contains(t.Id), isAsNoTracking: false).ToList(); //.ToList(); try { var modelMyBlogs = new ModelDB.Blogs() { BlogContent = blogcontext, BlogCreateTime = createtime, BlogTitle = blogtitle, BlogUrl = blogurl, IsDel = false, BlogTags = myBlogTags, BlogTypes = myBlogTypes, UsersId = GetUserId(userName), BlogForUrl = blogurl, IsForwarding = iszf == "true", IsShowMyHome = isshowmyhome == "true", IsShowHome = isshowhome == "true" }; blog.Add(modelMyBlogs); blog.save(); var newtag = string.Empty; try { modelMyBlogs.BlogTags.Where(t => true).ToList().ForEach(t => newtag += t.TagName + " "); var newblogurl = "/" + modelMyBlogs.BlogUsersSet.UserName + "/" + modelMyBlogs.Id + ".html"; SearchResult search = new SearchResult() { flag = modelMyBlogs.UsersId, id = modelMyBlogs.Id, title = blogtitle, clickQuantity = 0, blogTag = newtag, content = getText(blogcontext, document), url = newblogurl }; SafetyWriteHelper <SearchResult> .logWrite(search, PanGuLuceneHelper.instance.CreateIndex); } catch (Exception) { } var postid = blogurl.Substring(blogurl.LastIndexOf('/') + 1); postid = postid.Substring(0, postid.LastIndexOf('.')); testJumonyParser(modelMyBlogs.Id, postid, userName); blosNumber++; } catch (Exception) { throw; } } } if (blosNumber > 0) { Blogs.BLL.Common.GetDataHelper.GetAllTag(); Blogs.BLL.Common.CacheData.GetAllType(true); Blogs.BLL.Common.CacheData.GetAllUserInfo(true); return("成功导入" + blosNumber + "篇Blog"); } return("ok"); }
public string Release() { JSData jsdata = new JSData(); var content = Request.Form["content"]; //正文内容 var title = Request.Form["title"]; //标题 var oldtag = Request.Form["oldtag"]; //旧的标签 var newtag = Request.Form["newtag"]; //新的标签 var types = Request.Form["chk_type"]; //文章类型 var isshowhome = Request.Form["isshowhome"]; //是否显示在主页 var isshowmyhome = Request.Form["isshowmyhome"]; //是否显示在个人主页 var blogid = Request.Form["blogid"]; // int numblogid = -1; int.TryParse(blogid, out numblogid); #region 数据验证 if (null == BLL.Common.BLLSession.UserInfoSessioin) { jsdata.Messg = "您还未登录~"; } else if (BLL.Common.BLLSession.UserInfoSessioin.IsLock) { jsdata.Messg = "您的账户已经被锁定,请联系管理员~"; } else if (string.IsNullOrEmpty(content)) { jsdata.Messg = "内容不能为空~"; } else if (content.Length >= 80000) { jsdata.Messg = "发布内容过多~"; } else if (string.IsNullOrEmpty(title)) { jsdata.Messg = "标题不能为空~"; } else if (title.Length >= 100) { jsdata.Messg = "标题过长~"; } if (!string.IsNullOrEmpty(jsdata.Messg)) { jsdata.State = EnumState.失败; return(jsdata.ToJson()); } #endregion BLL.BlogsBLL blogbll = new BLL.BlogsBLL(); var blogtemp = blogbll.GetList(t => t.Id == numblogid, isAsNoTracking: false).FirstOrDefault(); var userid = numblogid > 0 ? blogtemp.UsersId : BLLSession.UserInfoSessioin.Id;//如果numblogid大于〇证明 是编辑修改 var sessionuserid = BLLSession.UserInfoSessioin.Id; //获取得 文章 类型集合 对象 var typelist = new List <int>(); foreach (string type in types.Split(',').ToList()) { if (!string.IsNullOrEmpty(type)) { typelist.Add(int.Parse(type)); } } // types.Split(',').ToList().ForEach(t => typelist.Add(int.Parse(t))); var myBlogTypes = new BLL.BlogTypesBLL().GetList(t => typelist.Contains(t.Id), isAsNoTracking: false).ToList(); //获取得 文章 tag标签集合 对象 //old var oldtaglist = oldtag.Split(',').ToList(); var myOldTagTypes = new BLL.BlogTagsBLL().GetList(t => t.UsersId == userid && oldtaglist.Contains(t.TagName), isAsNoTracking: false).ToList(); //new var newtaglist = newtag.Split(',').ToList(); AddTag(newtaglist, userid);//保存到数据库 var myNweTagTypes = new BLL.BlogTagsBLL().GetList(t => t.UsersId == userid && newtaglist.Contains(t.TagName), isAsNoTracking: false).ToList(); myNweTagTypes.ForEach(t => myOldTagTypes.Add(t)); //ModelDB.Blogs blogtemp = new ModelDB.Blogs(); if (numblogid > 0) //如果有 blogid 则修改 { //blog = blogbll.GetList(t => t.Id == numblogid, isAsNoTracking: false).FirstOrDefault(); if (sessionuserid == blogtemp.UsersId || BLLSession.UserInfoSessioin.UserName == admin) //一定要验证更新的博客是否是登陆的用户 { blogtemp.BlogContent = content; blogtemp.BlogTitle = title; //blog.BlogUpTime = DateTime.Now; //blog.BlogCreateTime = DateTime.Now; blogtemp.IsShowMyHome = isshowmyhome == "true"; blogtemp.IsShowHome = isshowhome == "true"; blogtemp.BlogTypes.Clear();//更新之前要清空 不如会存在主外键约束异常 blogtemp.BlogTypes = myBlogTypes; blogtemp.BlogTags.Clear(); blogtemp.BlogTags = myOldTagTypes; blogtemp.IsDel = false; blogtemp.IsForwarding = false; jsdata.Messg = "修改成功~"; } else { jsdata.Messg = "您没有编辑此博文的权限~"; jsdata.JSurl = "/"; jsdata.State = EnumState.失败; return(jsdata.ToJson()); } } else //否则 新增 { var blogfirst = blogbll.GetList(t => t.UsersId == sessionuserid).OrderByDescending(t => t.Id).FirstOrDefault(); //var blogtitle = blogtemp.BlogTitle; //if (blogfirst != null) // blogtitle = blogtemp.BlogTitle; if (null != blogfirst && blogfirst.BlogTitle == title) { jsdata.Messg = "不能同时发表两篇一样标题的文章~"; } else { blogtemp = new ModelDB.Blogs() { UsersId = sessionuserid, BlogContent = content, BlogTitle = title, BlogUpTime = DateTime.Now, BlogCreateTime = DateTime.Now, IsShowMyHome = isshowmyhome == "true", IsShowHome = isshowhome == "true", BlogTypes = myBlogTypes, BlogTags = myOldTagTypes, IsDel = false, IsForwarding = false }; blogbll.Add(blogtemp); jsdata.Messg = "发布成功~"; } } // if (blogbll.save(false) > 0) { #region 添加 或 修改搜索索引 try { var newtagList = string.Empty; blogtemp.BlogTags.Where(t => true).ToList().ForEach(t => newtagList += t.TagName + " "); var newblogurl = "/" + BLLSession.UserInfoSessioin.UserName + "/" + blogtemp.Id + ".html"; SearchResult search = new SearchResult() { flag = blogtemp.UsersId, id = blogtemp.Id, title = blogtemp.BlogTitle, clickQuantity = 0, blogTag = newtagList, content = Blogs.Common.Helper.MyHtmlHelper.GetHtmlText(blogtemp.BlogContent), url = newblogurl }; SafetyWriteHelper <SearchResult> .logWrite(search, PanGuLuceneHelper.instance.CreateIndex); } catch (Exception) { } #endregion jsdata.State = EnumState.成功; jsdata.JSurl = "/" + CacheData.GetAllUserInfo().Where(t => t.Id == blogtemp.UsersId).First().UserName + "/" + blogtemp.Id + ".html"; return(jsdata.ToJson()); } jsdata.Messg = string.IsNullOrEmpty(jsdata.Messg) ? "操作失败~" : jsdata.Messg; jsdata.State = EnumState.失败; return(jsdata.ToJson()); }
/// <summary> /// 提交内容的编辑或修改 /// </summary> /// <param name="input"></param> /// <returns></returns> public JSData ReleasePost(ReleaseInput input) { JSData jsdata = new JSData(); #region 数据验证 if (null == BLL.Common.BLLSession.UserInfoSessioin) { jsdata.Messg = "您还未登录~"; } else if (BLL.Common.BLLSession.UserInfoSessioin.IsLock) { jsdata.Messg = "您的账户未激活,暂只能评论。~"; } else if (string.IsNullOrEmpty(input.Content)) { jsdata.Messg = "内容不能为空~"; } if (!string.IsNullOrEmpty(jsdata.Messg)) { jsdata.State = EnumState.失败; return(jsdata); } #endregion BLL.BaseBLL <BlogInfo> blogbll = new BaseBLL <BlogInfo>(); var blogtemp = blogbll.GetList(t => t.Id == input.Blogid, isAsNoTracking: false).FirstOrDefault(); var userid = input.Blogid > 0 ? blogtemp.User.Id : BLLSession.UserInfoSessioin.Id;//如果numblogid大于〇证明 是编辑修改 var sessionuserid = BLLSession.UserInfoSessioin.Id; //获取得 文章 类型集合 对象 var typelist = new List <int>(); if (!string.IsNullOrEmpty(input.Chk_type)) { foreach (string type in input.Chk_type.Split(',').ToList()) { if (!string.IsNullOrEmpty(type)) { typelist.Add(int.Parse(type)); } } } // types.Split(',').ToList().ForEach(t => typelist.Add(int.Parse(t))); var myBlogTypes = new BLL.BaseBLL <BlogType>().GetList(t => typelist.Contains(t.Id), isAsNoTracking: false).ToList(); //获取得 文章 tag标签集合 对象 //old var oldtaglist = string.IsNullOrEmpty(input.Oldtag) ? new List <string>() : input.Oldtag.Split(',').ToList(); var myOldTagTypes = new BLL.BaseBLL <BlogTag>().GetList(t => t.BlogUser.Id == userid && oldtaglist.Contains(t.TagName), isAsNoTracking: false).ToList(); //new var newtaglist = input.Newtag.GetValueOrEmpty().Split(',').ToList(); AddTag(newtaglist, userid);//保存到数据库 var myNweTagTypes = new BLL.BaseBLL <BlogTag>().GetList(t => t.BlogUser.Id == userid && newtaglist.Contains(t.TagName), isAsNoTracking: false).ToList(); myNweTagTypes.ForEach(t => myOldTagTypes.Add(t)); if (input.Blogid > 0) //如果有 blogid 则修改 { if (sessionuserid == blogtemp.User.Id || BLLSession.UserInfoSessioin.UserName == admin) //一定要验证更新的博客是否是登陆的用户 { blogtemp.Content = input.Content; blogtemp.Title = input.Title; blogtemp.IsShowMyHome = input.Isshowmyhome; blogtemp.IsShowHome = input.Isshowhome; blogtemp.Types.Clear();//更新之前要清空 不如会存在主外键约束异常 blogtemp.Types = myBlogTypes; blogtemp.Tags.Clear(); blogtemp.Tags = myOldTagTypes; blogtemp.IsDelte = false; blogtemp.IsForwarding = false; jsdata.Messg = "修改成功~"; } else { jsdata.Messg = "您没有编辑此博文的权限~"; jsdata.JSurl = "/"; jsdata.State = EnumState.失败; return(jsdata); } } else //否则 新增 { var blogfirst = blogbll.GetList(t => t.User.Id == sessionuserid).OrderByDescending(t => t.Id).FirstOrDefault(); if (null != blogfirst && blogfirst.Title == input.Title) { jsdata.Messg = "不能同时发表两篇一样标题的文章~"; } else { var bloguser = new BLL.BaseBLL <BlogUser>().GetList(t => t.Id == BLLSession.UserInfoSessioin.Id, isAsNoTracking: false).FirstOrDefault(); blogtemp = new BlogInfo() { User = bloguser, Content = input.Content, Title = input.Title, BlogUpTime = DateTime.Now, BlogCreateTime = DateTime.Now, IsShowMyHome = input.Isshowmyhome, IsShowHome = input.Isshowhome, Types = myBlogTypes, Tags = myOldTagTypes, IsDelte = false, IsForwarding = false }; blogbll.Insert(blogtemp); jsdata.Messg = "发布成功~"; } } // if (blogbll.save(false) > 0) { #region 添加 或 修改搜索索引 try { var newtagList = string.Empty; blogtemp.Tags.Where(t => true).ToList().ForEach(t => newtagList += t.TagName + " "); var newblogurl = "/" + BLLSession.UserInfoSessioin.UserName + "/" + blogtemp.Id + ".html"; SearchResult search = new SearchResult() { flag = blogtemp.User.Id, id = blogtemp.Id, title = blogtemp.Title, clickQuantity = 0, blogTag = newtagList, content = Blogs.Common.Helper.MyHtmlHelper.GetHtmlText(blogtemp.Content), url = newblogurl }; SafetyWriteHelper <SearchResult> .logWrite(search, PanGuLuceneHelper.instance.CreateIndex); } catch (Exception) { } #endregion jsdata.State = EnumState.成功; jsdata.JSurl = "/" + GetDataHelper.GetAllUser().Where(t => t.Id == blogtemp.User.Id).First().UserName + "/" + blogtemp.Id + ".html"; return(jsdata); } jsdata.Messg = string.IsNullOrEmpty(jsdata.Messg) ? "操作失败~" : jsdata.Messg; jsdata.State = EnumState.失败; return(jsdata); }