2009-05-23 05:28:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Utility class to render views of the differences between two data objects (or two versions of the
|
|
|
|
* same data object).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-23 05:28:18 +02:00
|
|
|
* Construcing a diff object is done as follows:
|
|
|
|
* <code>
|
|
|
|
* $fromRecord = Versioned::get_version('SiteTree', $pageID, $fromVersion);
|
|
|
|
* $toRecord = Versioned::get_version('SiteTree, $pageID, $toVersion);
|
|
|
|
* $diff = new DataDifferencer($fromRecord, $toRecord);
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-23 05:28:18 +02:00
|
|
|
* And then it can be used in a number of ways. You can use the ChangedFields() method in a template:
|
|
|
|
* <pre>
|
|
|
|
* <dl class="diff">
|
2012-05-11 03:49:20 +02:00
|
|
|
* <% with Diff %>
|
|
|
|
* <% loop ChangedFields %>
|
2009-05-23 05:28:18 +02:00
|
|
|
* <dt>$Title</dt>
|
|
|
|
* <dd>$Diff</dd>
|
2012-05-11 03:49:20 +02:00
|
|
|
* <% end_loop %>
|
|
|
|
* <% end_with %>
|
2009-05-23 05:28:18 +02:00
|
|
|
* </dl>
|
|
|
|
* </pre>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-23 05:28:18 +02:00
|
|
|
* Or you can get the diff'ed content as another DataObject, that you can insert into a form.
|
|
|
|
* <code>
|
|
|
|
* $form->loadDataFrom($diff->diffedData());
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-23 05:28:18 +02:00
|
|
|
* If there are fields whose changes you aren't interested in, you can ignore them like so:
|
|
|
|
* <code>
|
|
|
|
* $diff->ignoreFields('AuthorID', 'Status');
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-04-23 02:11:41 +02:00
|
|
|
* @subpackage misc
|
2009-05-23 05:28:18 +02:00
|
|
|
*/
|
|
|
|
class DataDifferencer extends ViewableData {
|
|
|
|
protected $fromRecord;
|
|
|
|
protected $toRecord;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-23 05:28:18 +02:00
|
|
|
protected $ignoredFields = array("ID","Version","RecordID");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 05:17:29 +02:00
|
|
|
/**
|
|
|
|
* Construct a DataDifferencer to show the changes between $fromRecord and $toRecord.
|
|
|
|
* If $fromRecord is null, this will represent a "creation".
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-29 08:35:10 +02:00
|
|
|
* @param DataObject (Optional)
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param DataObject
|
2009-06-17 05:17:29 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($fromRecord, DataObject $toRecord) {
|
2009-06-17 05:17:29 +02:00
|
|
|
if(!$toRecord) user_error("DataDifferencer constructed without a toRecord", E_USER_WARNING);
|
2009-05-23 05:28:18 +02:00
|
|
|
$this->fromRecord = $fromRecord;
|
|
|
|
$this->toRecord = $toRecord;
|
2009-09-18 05:02:19 +02:00
|
|
|
parent::__construct();
|
2009-05-23 05:28:18 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-23 05:28:18 +02:00
|
|
|
/**
|
|
|
|
* Specify some fields to ignore changes from. Repeated calls are cumulative.
|
|
|
|
* @param $ignoredFields An array of field names to ignore. Alternatively, pass the field names as
|
|
|
|
* separate args.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function ignoreFields($ignoredFields) {
|
2009-05-23 05:28:18 +02:00
|
|
|
if(!is_array($ignoredFields)) $ignoredFields = func_get_args();
|
|
|
|
$this->ignoredFields = array_merge($this->ignoredFields, $ignoredFields);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-03 11:45:05 +02:00
|
|
|
/**
|
|
|
|
* Get a DataObject with altered values replaced with HTML diff strings, incorporating
|
|
|
|
* <ins> and <del> tags.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function diffedData() {
|
2009-06-17 05:17:29 +02:00
|
|
|
if($this->fromRecord) {
|
|
|
|
$diffed = clone $this->fromRecord;
|
2012-05-11 01:23:24 +02:00
|
|
|
$fields = array_keys($diffed->toMap() + $this->toRecord->toMap());
|
2009-06-17 05:17:29 +02:00
|
|
|
} else {
|
|
|
|
$diffed = clone $this->toRecord;
|
2012-05-11 01:23:24 +02:00
|
|
|
$fields = array_keys($this->toRecord->toMap());
|
2009-06-17 05:17:29 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-25 16:21:33 +01:00
|
|
|
$hasOnes = array_merge($this->fromRecord->hasOne(), $this->toRecord->hasOne());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-29 22:23:22 +02:00
|
|
|
// Loop through properties
|
2009-05-23 05:28:18 +02:00
|
|
|
foreach($fields as $field) {
|
|
|
|
if(in_array($field, $this->ignoredFields)) continue;
|
2011-08-29 22:23:22 +02:00
|
|
|
if(in_array($field, array_keys($hasOnes))) continue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-10 06:01:19 +01:00
|
|
|
// Check if a field from-value is comparable
|
|
|
|
$toField = $this->toRecord->obj($field);
|
|
|
|
if(!($toField instanceof DBField)) continue;
|
|
|
|
$toValue = $toField->forTemplate();
|
|
|
|
|
|
|
|
// Show only to value
|
2009-06-17 05:17:29 +02:00
|
|
|
if(!$this->fromRecord) {
|
2015-02-10 06:01:19 +01:00
|
|
|
$diffed->setField($field, "<ins>{$toValue}</ins>");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a field to-value is comparable
|
|
|
|
$fromField = $this->fromRecord->obj($field);
|
|
|
|
if(!($fromField instanceof DBField)) continue;
|
|
|
|
$fromValue = $fromField->forTemplate();
|
|
|
|
|
|
|
|
// Show changes between the two, if any exist
|
|
|
|
if($fromValue != $toValue) {
|
2017-05-29 00:59:00 +02:00
|
|
|
$diffed->setField($field, DBField::create_field('HTMLText', Diff::compareHTML($fromValue, $toValue)));
|
2009-05-23 05:28:18 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-29 22:23:22 +02:00
|
|
|
// Loop through has_one
|
|
|
|
foreach($hasOnes as $relName => $relSpec) {
|
|
|
|
if(in_array($relName, $this->ignoredFields)) continue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-11-24 23:45:21 +01:00
|
|
|
// Create the actual column name
|
2011-08-29 22:23:22 +02:00
|
|
|
$relField = "{$relName}ID";
|
2014-11-24 23:45:21 +01:00
|
|
|
$toTitle = '';
|
|
|
|
if($this->toRecord->hasMethod($relName)) {
|
|
|
|
$relObjTo = $this->toRecord->$relName();
|
2015-04-19 23:11:57 +02:00
|
|
|
if($relObjTo) {
|
|
|
|
$toTitle = ($relObjTo->hasMethod('Title') || $relObjTo->hasField('Title')) ? $relObjTo->Title : '';
|
|
|
|
} else {
|
|
|
|
$toTitle = '';
|
|
|
|
}
|
2014-11-24 23:45:21 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-29 22:23:22 +02:00
|
|
|
if(!$this->fromRecord) {
|
|
|
|
if($relObjTo) {
|
|
|
|
if($relObjTo instanceof Image) {
|
|
|
|
// Using relation name instead of database column name, because of FileField etc.
|
|
|
|
// TODO Use CMSThumbnail instead to limit max size, blocked by DataDifferencerTest and GC
|
|
|
|
// not playing nice with mocked images
|
|
|
|
$diffed->setField($relName, "<ins>" . $relObjTo->getTag() . "</ins>");
|
|
|
|
} else {
|
2014-11-24 23:45:21 +01:00
|
|
|
$diffed->setField($relField, "<ins>" . $toTitle . "</ins>");
|
2011-08-29 22:23:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if($this->fromRecord->$relField != $this->toRecord->$relField) {
|
2014-11-24 23:45:21 +01:00
|
|
|
$fromTitle = '';
|
|
|
|
if($this->fromRecord->hasMethod($relName)) {
|
|
|
|
$relObjFrom = $this->fromRecord->$relName();
|
2015-04-19 23:11:57 +02:00
|
|
|
if($relObjFrom) {
|
2016-01-06 00:34:58 +01:00
|
|
|
$fromTitle = ($relObjFrom->hasMethod('Title') || $relObjFrom->hasField('Title')) ? $relObjFrom->Title : '';
|
2015-04-19 23:11:57 +02:00
|
|
|
} else {
|
|
|
|
$fromTitle = '';
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-11-24 23:45:21 +01:00
|
|
|
}
|
|
|
|
if(isset($relObjFrom) && $relObjFrom instanceof Image) {
|
2011-08-29 22:23:22 +02:00
|
|
|
// TODO Use CMSThumbnail (see above)
|
|
|
|
$diffed->setField(
|
|
|
|
// Using relation name instead of database column name, because of FileField etc.
|
2014-08-15 08:53:05 +02:00
|
|
|
$relName,
|
2011-08-29 22:23:22 +02:00
|
|
|
Diff::compareHTML($relObjFrom->getTag(), $relObjTo->getTag())
|
|
|
|
);
|
|
|
|
} else {
|
2014-11-24 23:45:21 +01:00
|
|
|
// Set the field.
|
2011-08-29 22:23:22 +02:00
|
|
|
$diffed->setField(
|
2014-08-15 08:53:05 +02:00
|
|
|
$relField,
|
2014-11-24 23:45:21 +01:00
|
|
|
Diff::compareHTML($fromTitle, $toTitle)
|
2011-08-29 22:23:22 +02:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-29 22:23:22 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-23 05:28:18 +02:00
|
|
|
return $diffed;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-03 11:45:05 +02:00
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* Get a SS_List of the changed fields.
|
2009-06-03 11:45:05 +02:00
|
|
|
* Each element is an array data containing
|
|
|
|
* - Name: The field name
|
|
|
|
* - Title: The human-readable field title
|
|
|
|
* - Diff: An HTML diff showing the changes
|
|
|
|
* - From: The older version of the field
|
|
|
|
* - To: The newer version of the field
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function ChangedFields() {
|
2011-05-05 12:40:24 +02:00
|
|
|
$changedFields = new ArrayList();
|
2009-06-17 05:17:29 +02:00
|
|
|
|
|
|
|
if($this->fromRecord) {
|
|
|
|
$base = $this->fromRecord;
|
2012-05-11 01:23:24 +02:00
|
|
|
$fields = array_keys($this->fromRecord->toMap());
|
2009-06-17 05:17:29 +02:00
|
|
|
} else {
|
|
|
|
$base = $this->toRecord;
|
2012-05-11 01:23:24 +02:00
|
|
|
$fields = array_keys($this->toRecord->toMap());
|
2009-06-17 05:17:29 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-23 05:28:18 +02:00
|
|
|
foreach($fields as $field) {
|
|
|
|
if(in_array($field, $this->ignoredFields)) continue;
|
|
|
|
|
2011-09-15 09:20:01 +02:00
|
|
|
if(!$this->fromRecord || $this->fromRecord->$field != $this->toRecord->$field) {
|
|
|
|
// Only show HTML diffs for fields which allow HTML values in the first place
|
|
|
|
$fieldObj = $this->toRecord->dbObject($field);
|
|
|
|
if($this->fromRecord) {
|
|
|
|
$fieldDiff = Diff::compareHTML(
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->fromRecord->$field,
|
|
|
|
$this->toRecord->$field,
|
2011-09-15 09:20:01 +02:00
|
|
|
(!$fieldObj || $fieldObj->stat('escape_type') != 'xml')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if($fieldObj && $fieldObj->stat('escape_type') == 'xml') {
|
|
|
|
$fieldDiff = "<ins>" . $this->toRecord->$field . "</ins>";
|
|
|
|
} else {
|
|
|
|
$fieldDiff = "<ins>" . Convert::raw2xml($this->toRecord->$field) . "</ins>";
|
|
|
|
}
|
|
|
|
}
|
2009-05-23 05:28:18 +02:00
|
|
|
$changedFields->push(new ArrayData(array(
|
2009-06-03 11:45:05 +02:00
|
|
|
'Name' => $field,
|
2009-06-17 05:17:29 +02:00
|
|
|
'Title' => $base->fieldLabel($field),
|
2010-05-25 05:15:54 +02:00
|
|
|
'Diff' => $this->fromRecord
|
|
|
|
? Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field)
|
|
|
|
: "<ins>" . $this->toRecord->$field . "</ins>",
|
2009-06-17 05:42:24 +02:00
|
|
|
'From' => $this->fromRecord ? $this->fromRecord->$field : null,
|
|
|
|
'To' => $this->toRecord ? $this->toRecord->$field : null,
|
2009-05-23 05:28:18 +02:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-23 05:28:18 +02:00
|
|
|
return $changedFields;
|
|
|
|
}
|
2009-06-03 11:45:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of the names of every fields that has changed.
|
|
|
|
* This is simpler than {@link ChangedFields()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function changedFieldNames() {
|
2009-06-03 11:45:05 +02:00
|
|
|
$diffed = clone $this->fromRecord;
|
2012-05-11 01:23:24 +02:00
|
|
|
$fields = array_keys($diffed->toMap());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-03 11:45:05 +02:00
|
|
|
$changedFields = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-03 11:45:05 +02:00
|
|
|
foreach($fields as $field) {
|
|
|
|
if(in_array($field, $this->ignoredFields)) continue;
|
2012-05-11 01:23:24 +02:00
|
|
|
if($this->fromRecord->$field != $this->toRecord->$field) {
|
2009-06-03 11:45:05 +02:00
|
|
|
$changedFields[] = $field;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-03 11:45:05 +02:00
|
|
|
return $changedFields;
|
|
|
|
}
|
2009-06-17 05:17:29 +02:00
|
|
|
}
|