Beispiel #1
0
        private static List <Post> ItemsToIndex()
        {
            DataBuddy.Query q  = PostCollection.DefaultQuery();
            PostCollection  pc = new PostCollection();

            pc.LoadAndCloseReader(q.ExecuteReader());

            return(pc);
        }
Beispiel #2
0
        public PostCollection PostsByCategory(Category category, int numberOfPosts, bool filterHome)
        {
            if (category == null)
            {
                return(null);
            }

            const string CacheKey = "Posts-Categories-P:{0}-C:{1}-T:{2}-PS:{3}";

            PostCollection pc =
                ZCache.Get <PostCollection>(string.Format(CacheKey, 1, category.Id, category.SortOrder, numberOfPosts));

            if (pc == null)
            {
                pc = new PostCollection();
                Query q = PostCollection.DefaultQuery(1, numberOfPosts, category.SortOrder);

                if (Category.IncludeChildPosts)
                {
                    if (category.ParentId > 0)
                    {
                        q.AndWhere(Post.Columns.CategoryId, category.Id);
                    }
                    else
                    {
                        var ids = new List <int>(category.Children.Count + 1);
                        foreach (Category child in category.Children)
                        {
                            ids.Add(child.Id);
                        }

                        ids.Add(category.Id);

                        q.AndInWhere(Post.Columns.CategoryId, ids.ToArray());
                    }
                }
                else
                {
                    q.AndWhere(Post.Columns.CategoryId, category.Id);
                }

                if (filterHome)
                {
                    string where = GraffitiContext.Current["where"] as string;
                    if (!string.IsNullOrEmpty(where) && where == "home" && Site.UseCustomHomeList)
                    {
                        q.AndWhere(Post.Columns.IsHome, true);
                    }
                }

                pc.LoadAndCloseReader(q.ExecuteReader());
                ZCache.InsertCache(string.Format(CacheKey, 1, category.Id, category.SortOrder, numberOfPosts), pc, 60);
            }

            return(pc);
        }
Beispiel #3
0
        /// <summary>
        /// Gets x amount of popular posts from the specified category Id
        /// </summary>
        /// <param name="numberOfPosts"></param>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public PostCollection PopularPosts(int numberOfPosts, int categoryId)
        {
            PostCollection pc = ZCache.Get <PostCollection>("Posts-Popular-" + numberOfPosts + "c:" + categoryId);

            if (pc == null)
            {
                Query q = PostCollection.DefaultQuery();

                if (categoryId > 0)
                {
                    Category category = new CategoryController().GetCachedCategory(categoryId, true);
                    if (category != null)
                    {
                        if (category.ParentId > 0)
                        {
                            q.AndWhere(Post.Columns.CategoryId, categoryId);
                        }
                        else
                        {
                            List <int> ids = new List <int>(category.Children.Count + 1);
                            foreach (Category child in category.Children)
                            {
                                ids.Add(child.Id);
                            }

                            ids.Add(category.Id);

                            q.AndInWhere(Post.Columns.CategoryId, ids.ToArray());
                        }
                    }
                    else
                    {
                        //this should result in no data, but it will signal to
                        //the end user to edit/remove this widget
                        q.AndWhere(Post.Columns.CategoryId, categoryId);
                    }
                }

                q.Orders.Clear();
                q.OrderByDesc(Post.Columns.Views);
                q.Top = numberOfPosts.ToString();
                pc    = PostCollection.FetchByQuery(q);
                ZCache.InsertCache("Posts-Popular-" + numberOfPosts + "c:" + categoryId, pc, 60);
            }
            return(pc);
        }
Beispiel #4
0
        protected virtual object GetCategoryPosts(string key, GraffitiContext graffitiContext)
        {
            Category       category = new CategoryController().GetCachedCategory(CategoryID, false);
            int            pageSize = SiteSettings.Get().PageSize;
            PostCollection pc       =
                ZCache.Get <PostCollection>(string.Format(CacheKey, PageIndex, CategoryID, category.SortOrder, pageSize));

            if (pc == null)
            {
                pc = new PostCollection();
                Query q = PostCollection.DefaultQuery(PageIndex, pageSize, category.SortOrder);

                if (Category.IncludeChildPosts)
                {
                    if (category.ParentId > 0)
                    {
                        q.AndWhere(Post.Columns.CategoryId, CategoryID);
                    }
                    else
                    {
                        var ids = new List <int>(category.Children.Count + 1);
                        foreach (Category child in category.Children)
                        {
                            ids.Add(child.Id);
                        }

                        ids.Add(category.Id);

                        q.AndInWhere(Post.Columns.CategoryId, ids.ToArray());
                    }
                }
                else
                {
                    q.AndWhere(Post.Columns.CategoryId, CategoryID);
                }
                pc.LoadAndCloseReader(q.ExecuteReader());
                ZCache.InsertCache(string.Format(CacheKey, PageIndex, CategoryID, category.SortOrder, pageSize), pc, 60);
            }

            graffitiContext.TotalRecords = category.PostCount;
            graffitiContext.PageIndex    = PageIndex;
            graffitiContext.PageSize     = SiteSettings.Get().PageSize;

            return(pc);
        }
Beispiel #5
0
        /// <summary>
        ///     Gets all posts by the specified user in the specified category name
        /// </summary>
        /// <param name="user"></param>
        /// <param name="category"></param>
        /// <param name="numberOfPosts"></param>
        public PostCollection PostsByUserAndCategory(IGraffitiUser user, Category category, int numberOfPosts)
        {
            if (category == null || user == null)
            {
                return(null);
            }

            const string CacheKey = "Posts-Users-Categories-P:{0}-U:{1}-C:{2}-T:{3}-PS:{4}";

            PostCollection pc =
                ZCache.Get <PostCollection>(string.Format(CacheKey, 1, user.UniqueId, category.Id, category.SortOrder, numberOfPosts));

            if (pc == null)
            {
                pc = new PostCollection();
                Query q = PostCollection.DefaultQuery(1, numberOfPosts, category.SortOrder);
                q.AndWhere(Post.Columns.UserName, user.Name);
                if (Category.IncludeChildPosts)
                {
                    if (category.ParentId > 0)
                    {
                        q.AndWhere(Post.Columns.CategoryId, category.Id);
                    }
                    else
                    {
                        var ids = new List <int>(category.Children.Count + 1);
                        foreach (Category child in category.Children)
                        {
                            ids.Add(child.Id);
                        }
                        ids.Add(category.Id);
                        q.AndInWhere(Post.Columns.CategoryId, ids.ToArray());
                    }
                }
                else
                {
                    q.AndWhere(Post.Columns.CategoryId, category.Id);
                }
                pc.LoadAndCloseReader(q.ExecuteReader());
                ZCache.InsertCache(string.Format(CacheKey, 1, user.UniqueId, category.Id, category.SortOrder, numberOfPosts), pc, 60);
            }

            return(pc);
        }
Beispiel #6
0
        protected override void OnLoad(EventArgs e)
        {
            Initialize();

            SiteSettings settings = SiteSettings.Get();

            string baseUrl = SiteSettings.BaseUrl;

            if (string.IsNullOrEmpty(TagName))
            {
                Category category = null;
                if (CategoryID > -1)
                {
                    category = new CategoryController().GetCachedCategory(CategoryID, false);
                }

                if (category == null)
                {
                    if (!string.IsNullOrEmpty(settings.ExternalFeedUrl) &&
                        Request.UserAgent.IndexOf("FeedBurner", StringComparison.InvariantCultureIgnoreCase) == -1)
                    {
                        Context.Response.RedirectLocation = settings.ExternalFeedUrl;
                        Context.Response.StatusCode       = 301;
                        Context.Response.End();
                    }
                }
                else if (!string.IsNullOrEmpty(category.FeedUrlOverride) &&
                         Request.UserAgent.IndexOf("FeedBurner", StringComparison.InvariantCultureIgnoreCase) == -1)
                {
                    Context.Response.RedirectLocation = category.FeedUrlOverride;
                    Context.Response.StatusCode       = 301;
                    Context.Response.End();
                }
                else if (CategoryName != null && !Util.AreEqualIgnoreCase(CategoryName, category.LinkName))
                {
                    Context.Response.RedirectLocation = new Uri(Context.Request.Url, category.Url).ToString();
                    Context.Response.StatusCode       = 301;
                    Context.Response.End();
                }

                string cacheKey = CategoryID > -1
                                                          ? "Posts-Index-" + Util.PageSize + "-" + CategoryID.ToString()
                                                          : string.Format("Posts-Categories-P:{0}-C:{1}-T:{2}-PS:{3}", 1, CategoryID,
                                                                          SortOrderType.Descending, Util.PageSize);

                PostCollection pc = ZCache.Get <PostCollection>(cacheKey);

                if (pc == null)
                {
                    Query q = PostCollection.DefaultQuery();
                    q.Top = Util.PageSize.ToString();

                    if (SiteSettings.Get().IncludeChildPosts&& macros.IsNotNull(category))
                    {
                        if (category.ParentId > 0)
                        {
                            q.AndWhere(Post.Columns.CategoryId, CategoryID);
                        }
                        else
                        {
                            var ids = new List <int>(category.Children.Count + 1);
                            foreach (Category child in category.Children)
                            {
                                ids.Add(child.Id);
                            }

                            ids.Add(category.Id);

                            q.AndInWhere(Post.Columns.CategoryId, ids.ToArray());
                        }
                    }
                    else
                    {
                        if (CategoryID > 0)
                        {
                            q.AndWhere(Post.Columns.CategoryId, CategoryID);
                        }
                    }

                    pc = new PostCollection();
                    pc.LoadAndCloseReader(q.ExecuteReader());

                    PostCollection permissionsFiltered = new PostCollection();

                    permissionsFiltered.AddRange(pc);

                    foreach (Post p in pc)
                    {
                        if (!RolePermissionManager.GetPermissions(p.CategoryId, GraffitiUsers.Current).Read)
                        {
                            permissionsFiltered.Remove(p);
                        }
                    }

                    ZCache.InsertCache(cacheKey, permissionsFiltered, 90);
                    pc = permissionsFiltered;
                }

                ValidateAndSetHeaders(pc, settings, Context);

                StringWriter sw = new StringWriter();
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
                XmlTextWriter writer = new XmlTextWriter(sw);

                writer.WriteStartElement("rss");
                writer.WriteAttributeString("version", "2.0");
                writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
                writer.WriteAttributeString("xmlns:slash", "http://purl.org/rss/1.0/modules/slash/");

                // Allow plugins to add additional xml namespaces
                Core.Events.Instance().ExecuteRssNamespace(writer);

                writer.WriteStartElement("channel");
                WriteChannel(writer, category, settings);

                // Allow plugins to add additional xml to the <channel>
                Core.Events.Instance().ExecuteRssChannel(writer);

                foreach (Post p in pc)
                {
                    writer.WriteStartElement("item");
                    WriteItem(writer, p, settings, baseUrl);

                    // Allow plugins to add additional xml to the <item>
                    Core.Events.Instance().ExecuteRssItem(writer, p);

                    writer.WriteEndElement();                     // End Item
                }

                writer.WriteEndElement();                 // End Channel
                writer.WriteEndElement();                 // End Document

                // save XML into response
                Context.Response.ContentEncoding = Encoding.UTF8;
                Context.Response.ContentType     = "application/rss+xml";
                Context.Response.Write(sw.ToString());
            }
            else
            {
                PostCollection pc = GetTaggedPosts(TagName);

                ValidateAndSetHeaders(pc, settings, Context);

                StringWriter sw = new StringWriter();
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
                XmlTextWriter writer = new XmlTextWriter(sw);

                writer.WriteStartElement("rss");
                writer.WriteAttributeString("version", "2.0");
                writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
                writer.WriteAttributeString("xmlns:slash", "http://purl.org/rss/1.0/modules/slash/");

                Core.Events.Instance().ExecuteRssNamespace(writer);

                writer.WriteStartElement("channel");
                WriteChannel(writer, TagName, settings);

                // Allow plugins to add additional xml to the <channel>
                Core.Events.Instance().ExecuteRssChannel(writer);

                foreach (Post p in pc)
                {
                    writer.WriteStartElement("item");
                    WriteItem(writer, p, settings, baseUrl);

                    Core.Events.Instance().ExecuteRssItem(writer, p);

                    writer.WriteEndElement();                     // End Item
                }

                writer.WriteEndElement();                 // End Channel
                writer.WriteEndElement();                 // End Document

                Context.Response.ContentEncoding = Encoding.UTF8;
                Context.Response.ContentType     = "application/rss+xml";
                Context.Response.Write(sw.ToString());
            }
        }