Version: current

      1 <?php
      2 /**
      3  * @package cms
      4  * @subpackage publishers
      5  */
      6 abstract class StaticPublisher extends DataObjectDecorator {
      7     /**
      8      * Defines whether to output information about publishing or not. By
      9      * default, this is off, and should be turned on when you want debugging
     10      * (for example, in a cron task)
     11      */
     12     static $echo_progress = false;
     13
     14     /**
     15      * Realtime static publishing... the second a page
     16     * is saved, it is written to the cache
     17      */
     18     static $disable_realtime = false;
     19
     20     /*
     21      * This is the current static publishing theme, which can be set at any point
     22      * If it's not set, then the last non-null theme, set via SSViewer::set_theme() is used
     23      * The obvious place to set this is in _config.php
     24      */
     25     static $static_publisher_theme=false;
     26
     27     abstract function publishPages($pages);
     28     abstract function unpublishPages($pages);
     29
     30     static function echo_progress() {
     31         return (boolean)self::$echo_progress;
     32     }
     33
     34     /**
     35      * Either turns on (boolean true) or off (boolean false) the progress indicators.
     36      * @see StaticPublisher::$echo_progress
     37      */
     38     static function set_echo_progress($progress) {
     39         self::$echo_progress = (boolean)$progress;
     40     }
     41
     42     /**
     43      * Called after a page is published.
     44      */
     45     function onAfterPublish($original) {
     46         $this->republish($original);
     47     }
     48
     49     /**
     50      * Called after link assets have been renamed, and the live site has been updated, without
     51      * an actual publish event.
     52      *
     53      * Only called if the published content exists and has been modified.
     54      */
     55     function onRenameLinkedAsset($original) {
     56         $this->republish($original);
     57     }
     58
     59     function republish($original) {
     60         if (self::$disable_realtime) return;
     61
     62         $urls = array();
     63
     64         if($this->owner->hasMethod('pagesAffectedByChanges')) {
     65             $urls = $this->owner->pagesAffectedByChanges($original);
     66         } else {
     67             $pages = Versioned::get_by_stage('SiteTree', 'Live', '', '', '', 10);
     68             if($pages) {
     69                 foreach($pages as $page) {
     70                     $urls[] = $page->AbsoluteLink();
     71                 }
     72             }
     73         }
     74
     75         // Note: Similiar to RebuildStaticCacheTask->rebuildCache()
     76         foreach($urls as $i => $url) {
     77             if(!is_string($url)) {
     78                 user_error("Bad URL: " . var_export($url, true), E_USER_WARNING);
     79                 continue;
     80             }
     81
     82             // Remove leading slashes from all URLs (apart from the homepage)
     83             if(substr($url,-1) == '/' && $url != '/') $url = substr($url,0,-1);
     84
     85             $urls[$i] = $url;
     86         }
     87
     88         $urls = array_unique($urls);
     89
     90         $this->publishPages($urls);
     91     }
     92
     93     /**
     94      * On after unpublish, get changes and hook into underlying
     95      * functionality
     96      */
     97     function onAfterUnpublish($page) {
     98         if (self::$disable_realtime) return;
     99
    100         // Get the affected URLs
    101         if($this->owner->hasMethod('pagesAffectedByUnpublishing')) {
    102             $urls = $this->owner->pagesAffectedByUnpublishing();
    103             $urls = array_unique($urls);
    104         } else {
    105             $urls = array($this->owner->AbsoluteLink());
    106         }
    107
    108         $legalPages = singleton('Page')->allPagesToCache();
    109
    110         $urlsToRepublish = array_intersect($urls, $legalPages);
    111         $urlsToUnpublish = array_diff($urls, $legalPages);
    112
    113         $this->unpublishPages($urlsToUnpublish);
    114         $this->publishPages($urlsToRepublish);
    115     }
    116
    117     /**
    118      * Get all external references to CSS, JS,
    119      */
    120     function externalReferencesFor($content) {
    121         $CLI_content = escapeshellarg($content);
    122         $tidy = `echo $CLI_content | tidy -numeric -asxhtml`;
    123         $tidy = preg_replace('/xmlns="[^"]+"/','', $tidy);
    124         $xContent = new SimpleXMLElement($tidy);
    125         //Debug::message($xContent->asXML());
    126
    127         $xlinks = array(
    128             "//link[@rel='stylesheet']/@href" => false,
    129             "//script/@src" => false,
    130             "//img/@src" => false,
    131             "//a/@href" => true,
    132         );
    133
    134         $urls = array();
    135         foreach($xlinks as $xlink => $assetsOnly) {
    136             $matches = $xContent->xpath($xlink);
    137             if($matches) foreach($matches as $item) {
    138                 $url = $item . '';
    139                 if($assetsOnly && substr($url,0,7) != 'assets/') continue;
    140
    141                 $urls[] = $url;
    142             }
    143         }
    144
    145         return $urls;
    146     }
    147
    148     function set_static_publisher_theme($theme){
    149         self::$static_publisher_theme=$theme;
    150     }
    151
    152     function static_publisher_theme(){
    153         return self::$static_publisher_theme;
    154     }
    155 }
    156
    157 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -