Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

Remove static pages from cache on unpublish


Reply

15 Posts   3142 Views

Avatar
martimiz

15 August 2009 at 1:42am Forum Moderator, 1091 Posts

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...

Avatar
martimiz

24 September 2009 at 9:13pm Forum Moderator, 1091 Posts

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?

Avatar
martimiz

24 September 2009 at 11:00pm Forum Moderator, 1091 Posts

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)

Avatar
infoclipper

25 September 2009 at 8:02pm Community Member, 5 Posts

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

Avatar
martimiz

8 October 2009 at 10:42pm Forum Moderator, 1091 Posts

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...

Avatar
martimiz

9 October 2009 at 3:13am Forum Moderator, 1091 Posts

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...

Avatar
Soccerphile

18 October 2011 at 11:56pm Community Member, 4 Posts

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?