silverstripe-versionfeed/code/VersionFeed_Controller.php

172 lines
4.9 KiB
PHP
Raw Normal View History

2012-11-26 01:22:30 +01:00
<?php
class VersionFeed_Controller extends Extension {
2013-10-31 10:38:08 +01:00
private static $allowed_actions = array(
2012-11-26 01:22:30 +01:00
'changes',
'allchanges'
);
2014-12-11 22:26:33 +01:00
/**
* Content handler
*
* @var \VersionFeed\Filters\ContentFilter
*/
protected $contentFilter;
2014-12-11 22:26:33 +01:00
/**
* Sets the content filter
2014-12-11 22:26:33 +01:00
*
* @param \VersionFeed\Filters\ContentFilter $contentFilter
*/
public function setContentFilter(\VersionFeed\Filters\ContentFilter $contentFilter) {
$this->contentFilter = $contentFilter;
}
2014-12-11 22:26:33 +01:00
/**
* Evaluates the result of the given callback
2014-12-11 22:26:33 +01:00
*
* @param string $key Unique key for this
* @param callable $callback Callback for evaluating the content
* @return mixed Result of $callback()
*/
protected function filterContent($key, $callback) {
if($this->contentFilter) {
return $this->contentFilter->getContent($key, $callback);
} else {
return call_user_func($callback);
}
}
2012-11-26 01:22:30 +01:00
public function onAfterInit() {
$this->linkToPageRSSFeed();
2012-11-26 01:22:30 +01:00
$this->linkToAllSiteRSSFeed();
}
/**
* Get page-specific changes in a RSS feed.
*/
public function changes() {
// Check viewability of changes
2014-12-11 22:26:33 +01:00
if(!Config::inst()->get('VersionFeed', 'changes_enabled')
|| !$this->owner->PublicHistory
|| $this->owner->ID == -1
) {
return $this->owner->httpError(404, 'Page history not viewable');
}
2012-11-26 01:22:30 +01:00
// Cache the diffs to remove DOS possibility.
$target = $this->owner;
$key = implode('_', array('changes', $this->owner->ID, $this->owner->Version));
$entries = $this->filterContent($key, function() use ($target) {
return $target->getDiffedChanges();
});
2012-11-26 01:22:30 +01:00
// Generate the output.
2014-12-11 22:26:33 +01:00
$title = sprintf(_t('RSSHistory.SINGLEPAGEFEEDTITLE', 'Updates to %s page'), $this->owner->Title);
$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.
*/
public function allchanges() {
// Check viewability of allchanges
if(!Config::inst()->get('VersionFeed', 'allchanges_enabled')
|| !SiteConfig::current_site_config()->AllChangesEnabled
) {
return $this->owner->httpError(404, 'Global history not viewable');
}
$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'
);
$lastChange = $latestChanges->record();
$latestChanges->rewind();
if ($lastChange) {
// Cache the diffs to remove DOS possibility.
$key = 'allchanges'
. preg_replace('#[^a-zA-Z0-9_]#', '', $lastChange['LastEdited'])
. (Member::currentUserID() ?: 'public');
$changeList = $this->filterContent($key, function() use ($latestChanges) {
$changeList = new ArrayList();
$canView = array();
foreach ($latestChanges as $record) {
2014-12-11 22:26:33 +01:00
// Check if the page should be visible.
// WARNING: although we are providing historical details, we check the current configuration.
$id = $record['RecordID'];
if(!isset($canView[$id])) {
$page = SiteTree::get()->byID($id);
$canView[$id] = $page && $page->canView(new Member());
}
if (!$canView[$id]) continue;
// Get the diff to the previous version.
$version = new Versioned_Version($record);
$changes = $version->getDiffedChanges($version->Version, false);
if ($changes && $changes->Count()) $changeList->push($changes->First());
}
return $changeList;
});
} 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();
}
2014-12-11 22:26:33 +01:00
/**
* Generates and embeds the RSS header link for the page-specific version rss feed
*/
public function linkToPageRSSFeed() {
if (!Config::inst()->get('VersionFeed', 'changes_enabled') || !$this->owner->PublicHistory) {
return;
}
2014-12-11 22:26:33 +01:00
RSSFeed::linkToFeed(
$this->owner->Link('changes'),
sprintf(
_t('RSSHistory.SINGLEPAGEFEEDTITLE', 'Updates to %s page'),
$this->owner->Title
)
);
}
2012-11-26 01:22:30 +01:00
/**
* Generates and embeds the RSS header link for the global version rss feed
*/
public function linkToAllSiteRSSFeed() {
if(!Config::inst()->get('VersionFeed', 'allchanges_enabled')
|| !SiteConfig::current_site_config()->AllChangesEnabled
) {
return;
}
2014-12-11 22:26:33 +01:00
2012-11-26 01:22:30 +01:00
// RSS feed to all-site changes.
$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
}
public function linkToAllSitesRSSFeedTitle() {
2012-11-26 01:22:30 +01:00
return sprintf(_t('RSSHistory.SITEFEEDTITLE', 'Updates to %s'), SiteConfig::current_site_config()->Title);
}
}