Passed
Pull Request — master (#42)
by
unknown
04:10
created

VersionFeed::getDefaultRSSLink()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace SilverStripe\VersionFeed;
4
5
use SilverStripe\ORM\FieldType\DBField;
6
use SilverStripe\View\Parsers\Diff;
7
use SilverStripe\ORM\ArrayList;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Forms\CheckboxField;
11
use SilverStripe\Forms\FieldGroup;
12
use SilverStripe\Forms\LiteralField;
13
use SilverStripe\SiteConfig\SiteConfig;
14
use SilverStripe\CMS\Model\SiteTreeExtension;
15
16
class VersionFeed extends SiteTreeExtension {
17
	
18
	private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
		'PublicHistory' => 'Boolean(true)'
20
	);
21
22
	private static $defaults = array(
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
23
		'PublicHistory' => true
24
	);
25
26
	public function updateFieldLabels(&$labels) {
27
		$labels['PublicHistory'] = _t('RSSHistory.LABEL', 'Make history public');
28
	}
29
30
	/**
31
	 * Enable the allchanges feed
32
	 *
33
	 * @config
34
	 * @var bool
35
	 */
36
	private static $allchanges_enabled = true;
0 ignored issues
show
introduced by
The private property $allchanges_enabled is not used, and could be removed.
Loading history...
37
38
	/**
39
	 * Allchanges feed limit of items.
40
	 *
41
	 * @config
42
	 * @var int
43
	 */
44
	private static $allchanges_limit = 20;
0 ignored issues
show
introduced by
The private property $allchanges_limit is not used, and could be removed.
Loading history...
45
46
	/**
47
	 * Enables RSS feed for page-specific changes
48
	 * 
49
	 * @config
50
	 * @var bool
51
	 */
52
	private static $changes_enabled = true;
0 ignored issues
show
introduced by
The private property $changes_enabled is not used, and could be removed.
Loading history...
53
54
	/**
55
	 * Changes feed limit of items.
56
	 *
57
	 * @config
58
	 * @var int
59
	 */
60
	private static $changes_limit = 100;
0 ignored issues
show
introduced by
The private property $changes_limit is not used, and could be removed.
Loading history...
61
62
	/**
63
	 * Compile a list of changes to the current page, excluding non-published and explicitly secured versions.
64
	 *
65
	 * @param int $highestVersion Top version number to consider.
66
	 * @param int $limit Limit to the amount of items returned.
67
	 *
68
	 * @returns ArrayList List of cleaned records.
69
	 */
70
	public function getDiffList($highestVersion = null, $limit = 100) {
71
		// This can leak secured content if it was protected via inherited setting.
72
		// For now the users will need to be aware about this shortcoming.
73
		$offset = $highestVersion ? "AND \"SiteTree_versions\".\"Version\"<='".(int)$highestVersion."'" : '';
74
		// Get just enough elements for diffing. We need one more than desired to have something to compare to.
75
		$qLimit = (int)$limit + 1;
76
		$versions = $this->owner->allVersions(
77
			"\"WasPublished\"='1' AND \"CanViewType\" IN ('Anyone', 'Inherit') $offset",
78
			"\"SiteTree\".\"LastEdited\" DESC, \"SiteTree\".\"ID\" DESC",
79
			$qLimit
80
		);
81
82
		// Process the list to add the comparisons.
83
		$changeList = new ArrayList();
84
		$previous = null;
85
		$count = 0;
86
		foreach ($versions as $version) {
87
			$changed = false;
88
89
			// Check if we have something to compare with.
90
			if (isset($previous)) {
91
92
				// Produce the diff fields for use in the template.
93
				if ($version->Title != $previous->Title) {
94
					$diffTitle = Diff::compareHTML($version->Title, $previous->Title);
95
96
					$version->DiffTitle = DBField::create_field('HTMLText', null);
97
					$version->DiffTitle->setValue(
98
						sprintf(
99
							'<div><em>%s</em> ' . $diffTitle . '</div>',
100
							_t('RSSHistory.TITLECHANGED', 'Title has changed:')
101
						)
102
					);
103
					$changed = true;
104
				}
105
106
				if ($version->Content != $previous->Content) {
107
					$diffContent = Diff::compareHTML($version->Content, $previous->Content);
108
109
					$version->DiffContent = DBField::create_field('HTMLText', null);
110
					$version->DiffContent->setValue('<div>'.$diffContent.'</div>');
111
					$changed = true;
112
				}
113
114
				// Copy the link so it can be cached.
115
				$version->GeneratedLink = $version->AbsoluteLink();
116
			}
117
118
			// Omit the versions that haven't been visibly changed (only takes the above fields into consideration).
119
			if ($changed) {
120
				$changeList->push($version);
121
				$count++;
122
			}
123
124
			// Store the last version for comparison.
125
			$previous = $version;
126
		}
127
128
		// Make sure enough diff items have been generated to satisfy the $limit. If we ran out, add the final,
129
		// non-diffed item (the initial version). This will also work for a single-diff request: if we are requesting
130
		// a diff on the initial version we will just get that version, verbatim.
131
		if ($previous && $versions->count()<$qLimit) {
132
			$first = clone($previous);
133
			$first->DiffContent = DBField::create_field('HTMLText', null);
134
			$first->DiffContent->setValue('<div>' . $first->Content . '</div>');
135
			// Copy the link so it can be cached.
136
			$first->GeneratedLink = $first->AbsoluteLink();
137
			$changeList->push($first);
138
		}
139
140
		return $changeList;
141
	}
142
143
	/**
144
	 * Return a single diff representing this version.
145
	 * Returns the initial version if there is nothing to compare to.
146
	 *
147
	 * @returns DataObject Object with relevant fields diffed.
148
	 */
149
	public function getDiff() {
150
		$changes = $this->getDiffList($this->owner->Version, 1);
151
		if ($changes && $changes->Count()) {
152
			return $changes->First();
153
		}
154
155
		return null;
156
	}
157
158
	/**
159
	 * Compile a list of changes to the current page, excluding non-published and explicitly secured versions.
160
	 *
161
	 * @deprecated 2.0.0 Use VersionFeed::getDiffList instead
162
	 *
163
	 * @param int $highestVersion Top version number to consider.
164
	 * @param boolean $fullHistory Set to true to get the full change history, set to false for a single diff.
165
	 * @param int $limit Limit to the amount of items returned.
166
	 *
167
	 * @returns ArrayList List of cleaned records.
168
	 */
169
	public function getDiffedChanges($highestVersion = null, $fullHistory = true, $limit = 100) {
170
		return $this->getDiffList(
171
			$highestVersion,
172
			$fullHistory ? $limit : 1
173
		);
174
	}
175
176
	public function updateSettingsFields(FieldList $fields) {
177
		if(!Config::inst()->get(get_class(), 'changes_enabled')) return;
178
		
179
		// Add public history field.
180
		$fields->addFieldToTab('Root.Settings', $publicHistory = new FieldGroup(
181
			new CheckboxField('PublicHistory', $this->owner->fieldLabel('PublicHistory')
182
		)));
183
184
		$warning = _t(
185
			'VersionFeed.Warning',
186
			"Publicising the history will also disclose the changes that have at the time been protected " .
187
			"from the public view."
188
		);
189
190
		$fields->addFieldToTab('Root.Settings', new LiteralField('PublicHistoryWarning', $warning), 'PublicHistory');
191
192
		if ($this->owner->CanViewType!='Anyone') {
193
			$warning = _t(
194
				'VersionFeed.Warning2',
195
				"Changing access settings in such a way that this page or pages under it become publicly<br>" .
196
				"accessible may result in publicising all historical changes on these pages too. Please review<br>" .
197
				"this section's \"Public history\" settings to ascertain only intended information is disclosed."
198
			);
199
200
			$fields->addFieldToTab('Root.Settings', new LiteralField('PublicHistoryWarning2', $warning), 'CanViewType');
201
		}
202
	}
203
204
	public function getSiteRSSLink() {
205
		// TODO: This link should be from the homepage, not this page.
206
		if(Config::inst()->get(get_class(), 'allchanges_enabled')
207
			&& SiteConfig::current_site_config()->AllChangesEnabled
208
		) {
209
			return $this->owner->Link('allchanges');
210
		}
211
	}
212
213
	public function getDefaultRSSLink() {
214
		if(Config::inst()->get(get_class(), 'changes_enabled') && $this->owner->PublicHistory) {
215
			return $this->owner->Link('changes');
216
		}
217
	}
218
}
219