Jump to:

5542 Posts in 1739 Topics by 1225 members

Customising the CMS

SilverStripe Forums » Customising the CMS » phython cronjob to publish pages

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

Page: 1
Go to End
Author Topic: 405 Views
  • Schmatze
    Avatar
    Community Member
    3 Posts

    phython cronjob to publish pages Link to this post

    Hi,

    This is my first post here and i am not sure if i am posting in the correct Sub-Forum. Sorry if it is the wrong place:)

    I am trying to run a cronjob with phyton to call a controller action "publishPages". The output on the Debian commandline is correct, but the doPublish(); doesn't seem to do anything.
    If i call the Controller-Action through a browser it gives the same output and publishes the relevant Pages correctly.

    Hope somebody can give me a hint. Here is my controller code and the phyton call below:

    Note: The echo - outputs work as well from the terminal phython call. But no publish happens...

    CronController.php

    class CronController extends Controller{

       function publishPages(){

          $pages = DataObject::get("Page");
          $today = strtotime(date('Y-m-d'));

          foreach($pages as $page) {

             if($page->PublishOnDate){

                $publishOn = strtotime($page->PublishOnDate);
                $pageSitetree = DataObject::get_by_id('SiteTree',$page->ID);
                if($publishOn <= $today){

                   if(isset($pageSitetree->ID) && $pageSitetree->Status != "Published"){
                      echo "publish:".$pageSitetree->Title."</br>";
                      $page->doPublish();
                   }

                }else{
                   echo "to early for:".$pageSitetree->Title."</br>";
                }
             }
          }
       }
    }

    phython call from debian terminal:

    phython -c "import urllib2;print urllib2.urlopen('http://someurl/CronController/publishPages').read()"

    Thanks for your help in advance

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: phython cronjob to publish pages Link to this post

    Try - $page->publish('Stage', 'Live') rather than doPublish. You may also want to write this code as a BuildTask rather than controller so it's authenticated from random access.

  • Schmatze
    Avatar
    Community Member
    3 Posts

    Re: phython cronjob to publish pages Link to this post

    that worked fine. Thanks lots!

    405 Views
Page: 1
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.