archive.asp displaying unpublished posts

Topics: ASP.NET 2.0
Dec 21, 2008 at 10:50 PM
Edited Dec 21, 2008 at 10:53 PM
I noticed that the archive.aspx page displays unpublished posts. I found that in the Post.cs file there are a few methods that return a list of posts based on some criteria, but all fail to check if the post if published or not. The way I have it setup now is posts have to be flagged as published to be displayed when a user clicks on a related in tag, date range, category, and posts by author. There is one small display issue, the total comments and posts still includes the unpublished pots.

I had a few unpublished posts sitting around for a day or so and Googlebot came by and tried to index them, but was directed to a 404 error page

[Post.cs]

/// <summary>
        /// Returns all posts in the specified category
        /// </summary>
        public static List<Post> GetPostsByCategory(Guid categoryId)
        {
            List<Post> col = Posts.FindAll(delegate(Post p)
            {
                return (p.Categories.Contains(Category.GetCategory(categoryId)) && p.IsPublished == true);
            });

            col.Sort();
            col.TrimExcess();
            return col;
        }

/// <summary>
        /// Returns all posts written by the specified author.
        /// </summary>
        public static List<Post> GetPostsByAuthor(string author)
        {
            string legalAuthor = Utils.RemoveIllegalCharacters(author);
            List<Post> list = Posts.FindAll(delegate(Post p)
            {
                string legalTitle = Utils.RemoveIllegalCharacters(p.Author);                
                return (legalAuthor.Equals(legalTitle, StringComparison.OrdinalIgnoreCase) && p.IsPublished == true);
            });

            list.TrimExcess();
            return list;
        }

        /// <summary>
        /// Returns all posts tagged with the specified tag.
        /// </summary>
        public static List<Post> GetPostsByTag(string tag)
        {
            List<Post> list = Posts.FindAll(delegate(Post p)
            {
                return (p.Tags.Contains(tag) && p.IsPublished == true);
            });

            list.TrimExcess();
            return list;
        }

        /// <summary>
        /// Returns all posts published between the two dates.
        /// </summary>
        public static List<Post> GetPostsByDate(DateTime dateFrom, DateTime dateTo)
        {
            List<Post> list = Posts.FindAll(delegate(Post p)
            {
                return (p.DateCreated.Date >= dateFrom && p.DateCreated.Date <= dateTo && p.IsPublished == true);
            });

            list.TrimExcess();
            return list;
        }


[archive.aspx]

List<Post> noCatList = Post.Posts.FindAll(delegate(Post p) { return (p.Categories.Count == 0 && p.IsPublished == true); });
Coordinator
Dec 21, 2008 at 11:24 PM
Edited Dec 21, 2008 at 11:28 PM
You're digging up some good stuff.  Actually, it looks like the problem is isolated to just archive.aspx.cs.  I found a few things.

(1) If you are logged in, then you'll see all posts, unpublished and published.  Nothing new here.

(2) If you are logged out, then unpublished posts will usually not show up ... even without adding "p.IsPublished == true" in all those Post.cs functions.

(3) On the archives page, if you are logged out, unpublished posts having no categories will incorrectly show up in the Uncategoried section.  However, unpublished posts with one or more categories will not show up when you're not logged in (the correct behavior).

The fix so unpublished posts without any categories do not show up on the archives page is to modify archives.aspx.cs.

== BAD LINE ==
List<Post> noCatList = Post.Posts.FindAll(delegate(Post p) { return p.Categories.Count == 0; });

== FIXED LINE ==
List<Post> noCatList = Post.Posts.FindAll(delegate(Post p) { return p.Categories.Count == 0 && p.IsVisible; });

If you fix this line in archives.aspx.cs, then you shouldn't need all those extra "p.IsPublished == true" checks in Post.cs.  Just make sure you're not logged in when verifying whether an unpublished post is visible.
Dec 21, 2008 at 11:42 PM
That makes things a lot more simple :) Thanks for the fix!
Coordinator
Dec 21, 2008 at 11:49 PM
Nice find.  I added this to the Issue Tracker.  Vote if you'd like!

http://www.codeplex.com/blogengine/WorkItem/View.aspx?WorkItemId=8598
Coordinator
Dec 22, 2008 at 12:19 AM
It looks like this was actually fixed in changeset 16465 (BE 1.4.5.7) ... just FYI.
Dec 22, 2008 at 2:17 AM
Hi Ben, how did you find out if the issue was fixed or not? I found changeset 16465 and it never mentioned that fix specially, it's also dated Aug 21, 2008.

http://www.codeplex.com/blogengine/SourceControl/ListDownloadableCommits.aspx


Also, how do you get the newest versions? Thanks!
Coordinator
Dec 22, 2008 at 2:45 AM
You can get the newest versions by just clicking the Download link at that URL you posted.  The download is a ZIP file containing the BE core project and the web app that you deploy to your website.  The files for the BIN folder (DLL, XML, PDB) are not compiled when you download one of these releases.  All that's required is opening up the BE core project and compiling it to produce the BIN folder files.

If you're upgrading your current blog, in addition to overwriting your current BIN folder files with the new files you compile, you'll also want to copy the non-core files (in the BlogEngine.NET folder) into your blog.  This requires a little bit of attention if you've customized some of these files for your blog.  Having a file comparision utility, like Beyond Compare, will help to identify what files have changed in the new releases.  Definitely make a backup of what you have before overwriting your files with the newer files in case something happens.

One interesting thing I noticed today was that in a number of these newer releases, in the "widgets\Newsletter" folder, widget.ascx and widget.ascx.cs are not in these releases.  All the other files seem to be there.  I just noticed this today when looking into the newsletter widget problem you were looking at.  So a little strange why these two files seem to be the only ones not in these post 1.4.5 releases.  You can of course just use your current widget.ascx and widget.ascx.cs files.

I discovered this Archive page problem was fixed in 1.4.5.7 by looking at the code in the latest 1.4.5.14 release.  It was fixed in 1.4.5.14.  I then actually randomly downloaded some of the releases between 1.4.5 and 1.4.5.14 to see in which changeset the fix was put in.  Maybe there's a better way, but I did read the description of the changesets to make an "educated guess" as to which changesets this change might have occurred in.

I'm using BE 1.4.5 on my personal blog and 1.4.5.12 on another BE project.  I'll probably be upgrading my personal blog soon.  As you can see on the Source Code tab, there's been a number of fixes since 1.4.5.
Dec 25, 2008 at 12:51 AM
Just a quick update. New posts do not get displayed in the post list, or the rss feed if you start it out as unpublished using the above solution.