Jump to:

23001 Posts in 11854 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Remove static pages from cache on unpublish

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 2968 Views
  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Remove static pages from cache on unpublish Link to this post

    pagesAffectedByChanges() creates the array of links that you want to update - or exclude - depending on what page(type) is being published. The other one, allPagesToCache(), is called from /dev/buildcache, it will be slightly different...

    Suppose you have a sitemap on your site - you would probably want to update that one on any publishing action - or have it stay dynamic, just like forms and such...

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Remove static pages from cache on unpublish Link to this post

    Reviving this thread... Hi Willr,

    Are you guys by chance still working on upgrading the StaticPublisher? I'm asking this since I've been doing some things on my own, but I don't know if it's any use to continue like this, if the whole module is going to change shortly...

    Right now I've thrown together a batch version that will create static files for all published pages in the selected tree. It seems to be working fine but it's still in beta.

    I'll gladly publish it here if anyone wants it - as soon as I get it to work without having to hack it into the existing CMSBatchActions. I can't get the CMSBatchActionHandler::register function to work from any _config file as it keeps complaining about my batchaction class not existing... If anyone can please help me here ???

    Other things I'd still like to accomplish: to remove a static file on removal from the published site - but that would probably mean altering existing code?

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Remove static pages from cache on unpublish Link to this post

    One more: when a page is published with a different URLSegment, the old static file still remains, just like on delete (I'm thinking bookmarked pages)

  • infoclipper
    Avatar
    Community Member
    5 Posts

    Re: Remove static pages from cache on unpublish Link to this post

    For what its worth, I, for one, would be quite interested in seeing your batch system.

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Remove static pages from cache on unpublish Link to this post

    Took me a while to realize that register couldn't find my class because I used an underscore in the classname/file I'll not make that mistake again. OK, this is still very beta, all comments are very welcome. So for my first StaticPublisher batch action here goes:

    <?php
    /**
    * Batch action to create static pages. Pages are not republished to database
    *
    * Register the batchaction by adding this to your _config.php:
    * CMSBatchActionHandler::register('createstatic', 'CMSBatchActionCreateStatic');
    *
    * @author M. Bloem, Balbus Design
    *
    * @TODO remove (selected) pages from cache that are no longer valid
    */
    class CMSBatchActionCreateStatic extends CMSBatchAction {

       function getActionTitle() {
          return _t('CMSBatchActionCreateStatic.CREATE_STATIC_PAGES', 'Create static pages');
       }

       function getDoingText() {
          return _t('CMSBatchActionCreateStatic.CREATING_STATIC_PAGES', 'Creating static pages');
       }

       function run(DataObjectSet $pages) {
       return $this->batchaction(
             $pages,
             null,
             '');
       }

       public function batchaction(DataObjectSet $pages, $helperMethod, $successMessage)
       {
          // start with a page object so you can use its methods
          $pageObject = singleton('Page');

          // check if the StaticPublisher extension is enabled
          if (!$pageObject->hasMethod('publishPages')) {
             $message = _t('CMSBatchActionCreateStatic.NO_STATICPUBLISHER','StaticPublisher not enabled');
          }

          // check if the allPagesToCache method exists in the page, that defines
          // which url's may be cached
          elseif (!$pageObject->hasMethod('allPagesToCache')) {
             $message = _t('CMSBatchActionCreateStatic.NO_ALLPAGESTOCACHE',
                'AllPagesToCache method not defined');
          }

          else {
             $urls = array();
             $cacheableUrls = $pageObject->allPagesToCache();

             // check if the page can be cached
             foreach($pages as $page) {

                // Need the details of the live page
                $livePage = Versioned::get_one_by_stage(
                 'SiteTree', 'Live', "`SiteTree_Live`.`ID` = {$page->ID}");

                if (!empty($livePage) && $livePage->Status == 'Published' &&
                    $livePage->canView()) {

                   $url = $livePage->Link();

                   // check if this page belongs to the cacheable ones
                   if (in_array($url, $cacheableUrls)) {

                      // make it a relative url without trailing slash
                      $url = Director::makeRelative($url);
                      if(substr($url,-1) == '/') $url = substr($url,0,-1);
                      $urls[] = $url;
                   }
                }
                $page->destroy();
                unset($page);
             }

             // if there's still some urls left to process...
             if (!empty($urls)) {

                // call FileSystemPublisher::publishPages
                $pageObject->publishPages($urls);

                $pageObject->destroy();
                unset($pageObject);

                // this message assumes that nothing went wrong during publishing
                // must check this
                $message = sprintf(_t('CMSBatchActionCreateStatic.CREATED_STATIC_PAGES',
                   'Processed %d page(s) and created %d static file(s)'),
                   $pages->Count(),count($urls));

                FormResponse::add('statusMessage("'.$message.'","good");');
             }
             else {   
                $message = _t('CMSBatchActionCreateStatic.NO_STATICPAGES','No static pages to create');
                FormResponse::add('statusMessage("'.$message.'","good");');
             }
          }
          return FormResponse::respond();
       }
    }
    ?>

    I have a dutch translation for whoever might want it...

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Remove static pages from cache on unpublish Link to this post

    I have a few more things I'd like to tackle from the BatchAction as well as from publishing and unpublishing pages from the CMS:

    - publish only pages that can be viewed
    - remove pages that can not ( this includes pages that are for registered users only)

    If anyone's already done/doing this, please tell me so before I go and reinvent the wheel...

  • Soccerphile
    Avatar
    Community Member
    4 Posts

    Re: Remove static pages from cache on unpublish Link to this post

    I am having a problem not being able to fully delete a page and as a result a duplicate is created as in this example
    http://www.soccerphile.com/metalist-stadium there is a duplicate page with -2 on the end of the previous url which shows in the site map

    I tried to delete the original http://www.soccerphile.com/metalist-stadium and replace it with new content but all I got was a duplicate.

    How is it possible to delete pages which don't show in the tree?

    2968 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.