2012-11-26 01:22:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class VersionFeed extends SiteTreeExtension {
|
2013-10-31 10:38:08 +01:00
|
|
|
|
|
|
|
private static $db = array(
|
2014-04-17 07:44:38 +02:00
|
|
|
'PublicHistory' => 'Boolean(true)'
|
2012-11-26 01:22:30 +01:00
|
|
|
);
|
|
|
|
|
2013-10-31 10:38:08 +01:00
|
|
|
private static $defaults = array(
|
2012-11-26 01:22:30 +01:00
|
|
|
'PublicHistory' => true
|
|
|
|
);
|
|
|
|
|
2013-10-24 23:33:20 +02:00
|
|
|
public function updateFieldLabels(&$labels) {
|
|
|
|
$labels['PublicHistory'] = _t('RSSHistory.LABEL', 'Make history public');
|
|
|
|
}
|
2015-03-30 00:28:42 +02:00
|
|
|
|
2014-05-14 06:14:15 +02:00
|
|
|
/**
|
|
|
|
* Enable the allchanges feed
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $allchanges_enabled = true;
|
2015-03-30 00:28:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allchanges feed limit of items.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $allchanges_limit = 20;
|
|
|
|
|
2014-05-14 06:14:15 +02:00
|
|
|
/**
|
|
|
|
* Enables RSS feed for page-specific changes
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $changes_enabled = true;
|
2013-10-24 23:33:20 +02:00
|
|
|
|
2015-03-30 00:28:42 +02:00
|
|
|
/**
|
|
|
|
* Changes feed limit of items.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $changes_limit = 100;
|
|
|
|
|
2012-11-26 01:22:30 +01:00
|
|
|
/**
|
|
|
|
* Compile a list of changes to the current page, excluding non-published and explicitly secured versions.
|
|
|
|
*
|
|
|
|
* @param int $highestVersion Top version number to consider.
|
2015-03-30 00:28:42 +02:00
|
|
|
* @param int $limit Limit to the amount of items returned.
|
2012-11-26 01:22:30 +01:00
|
|
|
*
|
|
|
|
* @returns ArrayList List of cleaned records.
|
|
|
|
*/
|
2015-03-30 00:28:42 +02:00
|
|
|
public function getDiffList($highestVersion = null, $limit = 100) {
|
2012-11-26 01:22:30 +01:00
|
|
|
// This can leak secured content if it was protected via inherited setting.
|
|
|
|
// For now the users will need to be aware about this shortcoming.
|
|
|
|
$offset = $highestVersion ? "AND \"SiteTree_versions\".\"Version\"<='".(int)$highestVersion."'" : '';
|
2015-03-30 00:28:42 +02:00
|
|
|
// Get just enough elements for diffing. We need one more than desired to have something to compare to.
|
|
|
|
$qLimit = (int)$limit + 1;
|
|
|
|
$versions = $this->owner->allVersions(
|
2016-05-19 23:27:37 +02:00
|
|
|
"\"WasPublished\"='1' AND \"CanViewType\" IN ('Anyone', 'Inherit') $offset",
|
|
|
|
"\"SiteTree\".\"LastEdited\" DESC, \"SiteTree\".\"ID\" DESC",
|
|
|
|
$qLimit
|
2015-03-30 00:28:42 +02:00
|
|
|
);
|
2012-11-26 01:22:30 +01:00
|
|
|
|
|
|
|
// Process the list to add the comparisons.
|
|
|
|
$changeList = new ArrayList();
|
|
|
|
$previous = null;
|
|
|
|
$count = 0;
|
|
|
|
foreach ($versions as $version) {
|
|
|
|
$changed = false;
|
|
|
|
|
2015-03-30 00:29:33 +02:00
|
|
|
// Check if we have something to compare with.
|
2012-11-26 01:22:30 +01:00
|
|
|
if (isset($previous)) {
|
|
|
|
|
|
|
|
// Produce the diff fields for use in the template.
|
|
|
|
if ($version->Title != $previous->Title) {
|
2015-03-30 00:29:33 +02:00
|
|
|
$diffTitle = Diff::compareHTML($version->Title, $previous->Title);
|
|
|
|
|
2012-11-26 01:22:30 +01:00
|
|
|
$version->DiffTitle = new HTMLText();
|
|
|
|
$version->DiffTitle->setValue(
|
|
|
|
sprintf(
|
2015-03-30 00:29:33 +02:00
|
|
|
'<div><em>%s</em> ' . $diffTitle . '</div>',
|
2012-11-26 01:22:30 +01:00
|
|
|
_t('RSSHistory.TITLECHANGED', 'Title has changed:')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$changed = true;
|
|
|
|
}
|
2015-03-30 00:29:33 +02:00
|
|
|
|
2012-11-26 01:22:30 +01:00
|
|
|
if ($version->Content != $previous->Content) {
|
2015-03-30 00:29:33 +02:00
|
|
|
$diffContent = Diff::compareHTML($version->Content, $previous->Content);
|
|
|
|
|
2012-11-26 01:22:30 +01:00
|
|
|
$version->DiffContent = new HTMLText();
|
2015-03-30 00:29:33 +02:00
|
|
|
$version->DiffContent->setValue('<div>'.$diffContent.'</div>');
|
2012-11-26 01:22:30 +01:00
|
|
|
$changed = true;
|
|
|
|
}
|
2013-07-22 06:27:52 +02:00
|
|
|
|
|
|
|
// Copy the link so it can be cached by SS_Cache.
|
|
|
|
$version->GeneratedLink = $version->AbsoluteLink();
|
2012-11-26 01:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Omit the versions that haven't been visibly changed (only takes the above fields into consideration).
|
|
|
|
if ($changed) {
|
|
|
|
$changeList->push($version);
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the last version for comparison.
|
|
|
|
$previous = $version;
|
|
|
|
}
|
|
|
|
|
2015-03-30 00:28:42 +02:00
|
|
|
// Make sure enough diff items have been generated to satisfy the $limit. If we ran out, add the final,
|
|
|
|
// non-diffed item (the initial version). This will also work for a single-diff request: if we are requesting
|
|
|
|
// a diff on the initial version we will just get that version, verbatim.
|
|
|
|
if ($previous && $versions->count()<$qLimit) {
|
2012-11-26 01:22:30 +01:00
|
|
|
$first = clone($previous);
|
|
|
|
$first->DiffContent = new HTMLText();
|
2015-03-30 00:29:33 +02:00
|
|
|
$first->DiffContent->setValue('<div>' . $first->Content . '</div>');
|
2015-03-30 00:28:42 +02:00
|
|
|
// Copy the link so it can be cached by SS_Cache.
|
|
|
|
$first->GeneratedLink = $first->AbsoluteLink();
|
2012-11-26 01:22:30 +01:00
|
|
|
$changeList->push($first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $changeList;
|
|
|
|
}
|
|
|
|
|
2015-03-30 00:28:42 +02:00
|
|
|
/**
|
|
|
|
* Return a single diff representing this version.
|
|
|
|
* Returns the initial version if there is nothing to compare to.
|
|
|
|
*
|
|
|
|
* @returns DataObject Object with relevant fields diffed.
|
|
|
|
*/
|
|
|
|
public function getDiff() {
|
|
|
|
$changes = $this->getDiffList($this->owner->Version, 1);
|
|
|
|
if ($changes && $changes->Count()) {
|
|
|
|
return $changes->First();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile a list of changes to the current page, excluding non-published and explicitly secured versions.
|
|
|
|
*
|
|
|
|
* @deprecated 2.0.0 Use VersionFeed::getDiffList instead
|
|
|
|
*
|
|
|
|
* @param int $highestVersion Top version number to consider.
|
|
|
|
* @param boolean $fullHistory Set to true to get the full change history, set to false for a single diff.
|
|
|
|
* @param int $limit Limit to the amount of items returned.
|
|
|
|
*
|
|
|
|
* @returns ArrayList List of cleaned records.
|
|
|
|
*/
|
|
|
|
public function getDiffedChanges($highestVersion = null, $fullHistory = true, $limit = 100) {
|
|
|
|
return $this->getDiffList(
|
|
|
|
$highestVersion,
|
|
|
|
$fullHistory ? $limit : 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-11-26 01:22:30 +01:00
|
|
|
public function updateSettingsFields(FieldList $fields) {
|
2014-05-14 06:14:15 +02:00
|
|
|
if(!Config::inst()->get(get_class(), 'changes_enabled')) return;
|
|
|
|
|
2012-11-26 01:22:30 +01:00
|
|
|
// Add public history field.
|
|
|
|
$fields->addFieldToTab('Root.Settings', $publicHistory = new FieldGroup(
|
2013-10-24 23:33:20 +02:00
|
|
|
new CheckboxField('PublicHistory', $this->owner->fieldLabel('PublicHistory')
|
2012-11-26 01:22:30 +01:00
|
|
|
)));
|
2013-07-23 00:07:32 +02:00
|
|
|
|
2013-10-24 23:33:20 +02:00
|
|
|
$warning = _t(
|
|
|
|
'VersionFeed.Warning',
|
2013-07-23 00:07:32 +02:00
|
|
|
"Publicising the history will also disclose the changes that have at the time been protected " .
|
2013-10-24 23:33:20 +02:00
|
|
|
"from the public view."
|
|
|
|
);
|
2013-07-23 00:07:32 +02:00
|
|
|
|
|
|
|
$fields->addFieldToTab('Root.Settings', new LiteralField('PublicHistoryWarning', $warning), 'PublicHistory');
|
|
|
|
|
|
|
|
if ($this->owner->CanViewType!='Anyone') {
|
2013-10-24 23:33:20 +02:00
|
|
|
$warning = _t(
|
|
|
|
'VersionFeed.Warning2',
|
2013-07-23 00:07:32 +02:00
|
|
|
"Changing access settings in such a way that this page or pages under it become publicly<br>" .
|
|
|
|
"accessible may result in publicising all historical changes on these pages too. Please review<br>" .
|
2013-10-24 23:33:20 +02:00
|
|
|
"this section's \"Public history\" settings to ascertain only intended information is disclosed."
|
|
|
|
);
|
2013-07-23 00:07:32 +02:00
|
|
|
|
|
|
|
$fields->addFieldToTab('Root.Settings', new LiteralField('PublicHistoryWarning2', $warning), 'CanViewType');
|
|
|
|
}
|
2012-11-26 01:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getSiteRSSLink() {
|
|
|
|
// TODO: This link should be from the homepage, not this page.
|
2014-05-14 06:14:15 +02:00
|
|
|
if(Config::inst()->get(get_class(), 'allchanges_enabled')
|
|
|
|
&& SiteConfig::current_site_config()->AllChangesEnabled
|
|
|
|
) {
|
|
|
|
return $this->owner->Link('allchanges');
|
|
|
|
}
|
2012-11-26 01:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDefaultRSSLink() {
|
2014-05-14 06:14:15 +02:00
|
|
|
if(Config::inst()->get(get_class(), 'changes_enabled') && $this->owner->PublicHistory) {
|
|
|
|
return $this->owner->Link('changes');
|
|
|
|
}
|
2012-11-26 01:22:30 +01:00
|
|
|
}
|
2013-07-22 06:27:52 +02:00
|
|
|
}
|