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-04-17 07:44:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Content handler
|
|
|
|
*
|
2014-04-30 02:31:18 +02:00
|
|
|
* @var \VersionFeed\Filters\ContentFilter
|
2014-04-17 07:44:38 +02:00
|
|
|
*/
|
|
|
|
protected $contentFilter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the content filter
|
|
|
|
*
|
2014-04-30 02:31:18 +02:00
|
|
|
* @param \VersionFeed\Filters\ContentFilter $contentFilter
|
2014-04-17 07:44:38 +02:00
|
|
|
*/
|
2014-04-30 02:31:18 +02:00
|
|
|
public function setContentFilter(\VersionFeed\Filters\ContentFilter $contentFilter) {
|
2014-04-17 07:44:38 +02:00
|
|
|
$this->contentFilter = $contentFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates the result of the given callback
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
2014-04-17 07:44:38 +02:00
|
|
|
public function onAfterInit() {
|
2014-05-14 06:14:15 +02:00
|
|
|
$this->linkToPageRSSFeed();
|
2012-11-26 01:22:30 +01:00
|
|
|
$this->linkToAllSiteRSSFeed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get page-specific changes in a RSS feed.
|
|
|
|
*/
|
2014-04-17 07:44:38 +02:00
|
|
|
public function changes() {
|
2014-05-14 06:14:15 +02:00
|
|
|
// Check viewability of changes
|
2015-07-23 12:56:26 +02:00
|
|
|
if(!Config::inst()->get('VersionFeed', 'changes_enabled')
|
|
|
|
|| !$this->owner->PublicHistory
|
|
|
|
|| $this->owner->Version == ''
|
|
|
|
) {
|
2014-05-14 06:14:15 +02:00
|
|
|
return $this->owner->httpError(404, 'Page history not viewable');
|
|
|
|
}
|
2012-11-26 01:22:30 +01:00
|
|
|
|
2013-07-23 00:07:32 +02:00
|
|
|
// Cache the diffs to remove DOS possibility.
|
2014-04-17 07:44:38 +02:00
|
|
|
$target = $this->owner;
|
2013-10-29 12:06:26 +01:00
|
|
|
$key = implode('_', array('changes', $this->owner->ID, $this->owner->Version));
|
2014-04-17 07:44:38 +02:00
|
|
|
$entries = $this->filterContent($key, function() use ($target) {
|
2015-03-30 00:28:42 +02:00
|
|
|
return $target->getDiffList(null, Config::inst()->get('VersionFeed', 'changes_limit'));
|
2014-04-17 07:44:38 +02:00
|
|
|
});
|
2013-07-22 06:27:52 +02:00
|
|
|
|
2012-11-26 01:22:30 +01:00
|
|
|
// Generate the output.
|
2015-03-30 00:28:42 +02:00
|
|
|
$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.
|
|
|
|
*/
|
2014-04-17 07:44:38 +02:00
|
|
|
public function allchanges() {
|
2014-05-14 06:14:15 +02:00
|
|
|
// 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');
|
|
|
|
}
|
2014-04-17 07:44:38 +02:00
|
|
|
|
2015-03-30 00:28:42 +02:00
|
|
|
$limit = (int)Config::inst()->get('VersionFeed', 'allchanges_limit');
|
2014-04-17 07:44:38 +02: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\')
|
2015-03-30 00:28:42 +02:00
|
|
|
ORDER BY "LastEdited" DESC LIMIT ' . $limit
|
2014-04-17 07:44:38 +02:00
|
|
|
);
|
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.
|
2014-04-17 07:44:38 +02:00
|
|
|
$key = 'allchanges'
|
|
|
|
. preg_replace('#[^a-zA-Z0-9_]#', '', $lastChange['LastEdited'])
|
|
|
|
. (Member::currentUserID() ?: 'public');
|
|
|
|
$changeList = $this->filterContent($key, function() use ($latestChanges) {
|
2013-07-22 06:27:52 +02:00
|
|
|
$changeList = new ArrayList();
|
2014-04-17 07:44:38 +02:00
|
|
|
$canView = array();
|
2013-07-22 06:27:52 +02:00
|
|
|
foreach ($latestChanges as $record) {
|
2014-04-17 07:44:38 +02:00
|
|
|
|
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.
|
2014-04-17 07:44:38 +02:00
|
|
|
$id = $record['RecordID'];
|
|
|
|
if(!isset($canView[$id])) {
|
|
|
|
$page = SiteTree::get()->byID($id);
|
|
|
|
$canView[$id] = $page && $page->canView(new Member());
|
|
|
|
}
|
|
|
|
if (!$canView[$id]) continue;
|
2013-07-23 00:07:32 +02:00
|
|
|
|
2013-07-22 06:27:52 +02:00
|
|
|
// Get the diff to the previous version.
|
|
|
|
$version = new Versioned_Version($record);
|
2015-03-30 00:28:42 +02:00
|
|
|
if ($diff = $version->getDiff()) {
|
|
|
|
$changeList->push($diff);
|
|
|
|
}
|
2013-07-22 06:27:52 +02:00
|
|
|
}
|
|
|
|
|
2014-04-17 07:44:38 +02:00
|
|
|
return $changeList;
|
|
|
|
});
|
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();
|
|
|
|
}
|
2014-05-14 06:14:15 +02: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-05-14 06:14:15 +02: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;
|
|
|
|
}
|
|
|
|
|
2012-11-26 01:22:30 +01:00
|
|
|
// 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(
|
2015-10-20 05:58:45 +02:00
|
|
|
'<link rel="alternate" type="application/rss+xml" title="' . $title .
|
2013-10-23 23:15:22 +02:00
|
|
|
'" href="' . $url . '" />');
|
2012-11-26 01:22:30 +01:00
|
|
|
}
|
|
|
|
|
2014-05-14 06:14:15 +02: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);
|
|
|
|
}
|
2013-07-22 06:27:52 +02:00
|
|
|
}
|