December 5, 2009

[silverstripe] Limiting subpages in Silverstripe

0

As a follow-up on what I wrote much earlier, let's now do this in a better way. I'm doing this for quite some time but never got to share it.

First of all, you extend Hierarchy. So let's say your doing this for an ArticleHolder type, this is what your ArticleHierarchy may look like:

class ArticleHierarchy extends Hierarchy{
   
    public function stageChildren($showAll = false)
    {
        $baseClass = ClassInfo::baseDataClass($this->owner->class);
        $extraFilter = $showAll ? '' : " AND ShowInMenus = 1";
        $filter = "`{$baseClass}`.`ParentID` = " . (int)$this->owner->ID;
        $filter .= " AND `{$baseClass}`.ID != " . (int)$this->owner->ID;
        $filter .= $extraFilter;
       
        if(Director::urlParam("Action") == "getfilteredsubtree"
        && (!empty($_REQUEST['SiteTreeSearchTerm']) || !empty($_REQUEST['SiteTreeFilterDate'])))
        {           
            $staged = DataObject::get($baseClass, $filter, "Date ASC");
            if(!$staged) $staged = new DataObjectSet();
            $this->owner->extend("augmentStageChildren", $staged, $showAll);
            return $staged;
        }
       
        else
        {
            $limit = 15;           
            $set = DataObject::get($baseClass, $filter, "Date DESC", "", $limit);
            $set->sort("Date","ASC");
            return $set;   
        }
    }
}

In your ArticleHolder class, you then add the extension like this:

static $extensions = array("ArticleHierarchy");

This will limit the amount of shown articles in the cms to the latest 15 without the need for any hacks, and gives quite a performance boost as well: before I took out all my twitter posts, I had about 1200 entries. My basic Dreamhost install couldn't handle these with Silverstripe on 2.2.2. This 'limit' was the only way that I could run the CMS. Of course, you can't expect much from a cheap shared hosting environment like Dreamhost (without VPS).

This fix also still allows you to find other pages using the 'search text' and 'edited since' filters.  You can of course improve it by checking for one of the other filters as well.

Jamie from GPMD, whom I've worked with frequently, came up with an even more useful way of handling this for blog types: you don't add in a limit, but you do override stageChildren, making sure it sorts the results 'Date DESC'. Then you override getChildrenAsUL() and add in 'dummy' year and month nodes in the tree. This will give you a nice collapsed tree of years and months instead of a huge tree.

While this last fix works great on fast and private servers, it didn't really help on the shared Dreamhost package and Silverstripe 2.2.2 I was on at that time, so the 'limit'-way was the one to go.

Today, at the time of writing, I'm on 2.3.4, which is a lot faster, and using this last 'year-month' arrangement on Dreamhost with great success. I did remove all my tweets from the database though, resulting to about one twentieth of the posts. I'll keep an eye on performance as the amount of posts grow.

This post is about , , , , filed under silverstripe.

Post your comment

You can also tweet a reply using the following hashtag: #dio5_1107

Your details and comment

Some html allowed, see code-help

Pressing ENTER 2 times makes a new paragraph. Some HTML allowed:

  • Bold: <strong>text</strong>
  • Italic: <em>text</em>
  • Link: <a href="http://www.website.com" title="My title">My website</a>

URL's with http:// are automatically converted to links.

Get the RSSFeed for the comments on this article.

Search