Determine BlogEngine.NET comments that haven't been published - with LINQPad
At the beginning of the month I wrote a post on how to find BlogEngine.NET comments that had not yet been published/approved.
Having purchased a copy of LINQPad a short while ago (autocompletion costs, the program with all other functionality does not; give it a try if you develop in .NET - it's very cool), and having got slammed this morning with some spammer who had an hour to kill, I decided to adapt my code for LINQPad.
string postsDirectory = @"C:\posts";
string[] postFiles = System.IO.Directory.GetFiles(postsDirectory);
DataTable comments = new DataTable();
comments.Columns.Add(“Post”);
comments.Columns.Add(“CommentApproved”);
comments.Columns.Add(“FileId”);
comments.Columns.Add(“IpAddress”);
XDocument postXml;
foreach (string postFile in postFiles) {
postXml = XDocument.Load(postFile);
var posts = from postData in postXml.Descendants("post")
select new {
Title = postData.Element("title").Value,
CommentItems = (from commentItems in postData.Element("comments").Elements("comment")
select commentItems).ToList()
};
foreach (var post in posts) {
if (post.CommentItems.Count > 0) {
foreach (var commentItem in post.CommentItems) {
if (commentItem.Attribute("approved") != null && commentItem.Attribute("approved").Value == "False") {
DataRow comment = comments.NewRow();
comment["Post"] = post.Title;
comment["CommentApproved"] = commentItem.Attribute("approved").Value;
comment["IpAddress"] = commentItem.Element("ip").Value;
comment["FileId"] = "/post.aspx?id=" + System.IO.Path.GetFileNameWithoutExtension(postFile);
comments.Rows.Add(comment);
}
}
}
}
}
postXml = null;
comments.Dump();
You'll want to change postsDirectory accordingly.
This will output all unapproved comments, the post they are associated with, the post GUID, and the commentor's IP address.
It's relatively easy to expand this to display more or less information, as desired. For example, website and author can be swapped in in place of ip in the last foreach.
Comments/questions/etcetera welcome and appreciated.
Search
Links of Note
Support This Site
If my blog was helpful to you, then please consider visiting my Amazon Wishlist.