2012-11-26 01:22:30 +01:00
< ? php
class VersionFeed_Controller extends Extension {
static $allowed_actions = array (
'changes' ,
'allchanges'
);
function onAfterInit () {
// RSS feed for per-page changes.
if ( $this -> owner -> PublicHistory ) {
RSSFeed :: linkToFeed ( $this -> owner -> Link () . 'changes' ,
sprintf (
_t ( 'RSSHistory.SINGLEPAGEFEEDTITLE' , 'Updates to %s page' ),
$this -> owner -> Title
)
);
}
$this -> linkToAllSiteRSSFeed ();
return $this ;
}
/**
* Get page - specific changes in a RSS feed .
*/
function changes () {
if ( ! $this -> owner -> PublicHistory ) throw new SS_HTTPResponse_Exception ( 'Page history not viewable' , 404 );;
2013-07-23 00:07:32 +02:00
// Cache the diffs to remove DOS possibility.
2013-07-22 06:27:52 +02:00
$cache = SS_Cache :: factory ( 'VersionFeed_Controller' );
$cache -> setOption ( 'automatic_serialization' , true );
2013-10-29 12:06:26 +01:00
$key = implode ( '_' , array ( 'changes' , $this -> owner -> ID , $this -> owner -> Version ));
2013-07-22 06:27:52 +02:00
$entries = $cache -> load ( $key );
2013-07-23 00:07:32 +02:00
if ( ! $entries || isset ( $_GET [ 'flush' ])) {
2013-07-22 06:27:52 +02:00
$entries = $this -> owner -> getDiffedChanges ();
$cache -> save ( $entries , $key );
}
2012-11-26 01:22:30 +01:00
// Generate the output.
$title = sprintf ( _t ( 'RSSHistory.SINGLEPAGEFEEDTITLE' , 'Updates to %s page' ), $this -> owner -> Title );
2013-07-22 06:27:52 +02:00
$rss = new RSSFeed ( $entries , $this -> owner -> request -> getURL (), $title , '' , 'Title' , '' , null );
2012-11-26 01:22:30 +01:00
$rss -> setTemplate ( 'Page_changes_rss' );
return $rss -> outputToBrowser ();
}
/**
* Get all changes from the site in a RSS feed .
*/
function allchanges () {
2013-07-22 06:27:52 +02:00
2012-11-26 01:22:30 +01:00
$latestChanges = DB :: query ( 'SELECT * FROM "SiteTree_versions" WHERE "WasPublished"=\'1\' AND "CanViewType" IN (\'Anyone\', \'Inherit\') AND "ShowInSearch"=1 AND ("PublicHistory" IS NULL OR "PublicHistory" = \'1\') ORDER BY "LastEdited" DESC LIMIT 20' );
2013-07-22 06:27:52 +02:00
$lastChange = $latestChanges -> record ();
$latestChanges -> rewind ();
if ( $lastChange ) {
2013-07-23 00:07:32 +02:00
// Cache the diffs to remove DOS possibility.
$member = Member :: currentUser ();
2013-07-22 06:27:52 +02:00
$cache = SS_Cache :: factory ( 'VersionFeed_Controller' );
$cache -> setOption ( 'automatic_serialization' , true );
2013-07-23 00:07:32 +02:00
$key = 'allchanges' . preg_replace ( '#[^a-zA-Z0-9_]#' , '' , $lastChange [ 'LastEdited' ]) .
( $member ? $member -> ID : 'public' );
2013-07-22 06:27:52 +02:00
$changeList = $cache -> load ( $key );
2013-07-23 00:07:32 +02:00
if ( ! $changeList || isset ( $_GET [ 'flush' ])) {
2013-07-22 06:27:52 +02:00
$changeList = new ArrayList ();
foreach ( $latestChanges as $record ) {
2013-07-23 00:07:32 +02:00
// Check if the page should be visible.
// WARNING: although we are providing historical details, we check the current configuration.
$page = SiteTree :: get () -> filter ( array ( 'ID' => $record [ 'RecordID' ])) -> First ();
if ( ! $page -> canView ( new Member ())) continue ;
2013-07-22 06:27:52 +02:00
// Get the diff to the previous version.
$version = new Versioned_Version ( $record );
2013-07-23 00:07:32 +02:00
2013-07-22 06:27:52 +02:00
$changes = $version -> getDiffedChanges ( $version -> Version , false );
if ( $changes && $changes -> Count ()) $changeList -> push ( $changes -> First ());
}
$cache -> save ( $changeList , $key );
}
2012-11-26 01:22:30 +01:00
2013-10-29 12:06:26 +01:00
} else {
$changeList = new ArrayList ();
2012-11-26 01:22:30 +01:00
}
// Produce output
$rss = new RSSFeed ( $changeList , $this -> owner -> request -> getURL (), $this -> linkToAllSitesRSSFeedTitle (), '' , 'Title' , '' , null );
$rss -> setTemplate ( 'Page_allchanges_rss' );
return $rss -> outputToBrowser ();
}
function linkToAllSiteRSSFeed () {
// RSS feed to all-site changes.
2013-10-23 23:15:22 +02:00
$title = Convert :: raw2xml ( $this -> linkToAllSitesRSSFeedTitle ());
$url = $this -> owner -> getSiteRSSLink ();
Requirements :: insertHeadTags (
'<link rel="alternate nofollow" type="application/rss+xml" title="' . $title .
'" href="' . $url . '" />' );
2012-11-26 01:22:30 +01:00
}
function linkToAllSitesRSSFeedTitle () {
return sprintf ( _t ( 'RSSHistory.SITEFEEDTITLE' , 'Updates to %s' ), SiteConfig :: current_site_config () -> Title );
}
2013-07-22 06:27:52 +02:00
}