Skip to content

Commit

Permalink
Merge pull request #36 from xini/pull-ss5
Browse files Browse the repository at this point in the history
Upgrade to SS5 and PHP 8.3
  • Loading branch information
nyeholt authored Sep 13, 2024
2 parents 1782f81 + 483db32 commit f4089ea
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 92 deletions.
8 changes: 2 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Changelog

All notable changes to this project will be documented in this file.
For more information on changes and releases, please visit [Releases](https://github.com/symbiote/silverstripe-datachange-tracker/releases).

This project adheres to [Semantic Versioning](http://semver.org/).

## [5.0.0]

* First SilverStripe 4 compatible version.
This project adheres to [Semantic Versioning](http://semver.org/)
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Total Downloads](https://poser.pugx.org/symbiote/silverstripe-datachange-tracker/downloads.svg)](https://packagist.org/packages/symbiote/silverstripe-datachange-tracker)
[![License](https://poser.pugx.org/symbiote/silverstripe-datachange-tracker/license.svg)](https://github.com/symbiote/silverstripe-datachange-tracker/blob/master/LICENSE.md)

Compatible with SilverStripe 4
Compatible with SilverStripe 4 and 5

## Maintainers

Expand All @@ -28,21 +28,10 @@ composer require symbiote/silverstripe-datachange-tracker:~5.0

## Requirements

* SilverStripe 4
* SilverStripe 4.12 || 5

## Documentation

* [Quick Start](docs/en/quick-start.md)
* [License](LICENSE.md)
* [Contributing](CONTRIBUTING.md)

## Version details

*3.2.0*

* Added pruning of data via queuedjobs

*3.0.0*

* Removed static DataChangeRecord::track() method

5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
],

"require": {
"silverstripe/framework": "~4.0"
"silverstripe/framework": "^4.12 || ^5"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^9.6"
},
"extra": {
"expose": [
"client"
],
"installer-name": "datachange-tracker",
"branch-alias": {
"dev-master": "5.0.x-dev"
}
Expand Down
5 changes: 3 additions & 2 deletions src/Extension/ChangeRecordable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ChangeRecordable extends DataExtension
*/
public $dataChangeTrackService;

private static $ignored_fields = array();
private static $ignored_fields = [];

protected $isNewObject = false;

Expand Down Expand Up @@ -80,7 +80,8 @@ public function onBeforeVersionedPublish($from, $to)
*
* @return \SilverStripe\ORM\DataList
*/
public function getDataChangesList() {
public function getDataChangesList()
{
return DataChangeRecord::get()->filter([
'ChangeRecordID' => $this->owner->ID,
'ChangeRecordClass' => $this->owner->ClassName
Expand Down
8 changes: 4 additions & 4 deletions src/Extension/SignificantChangeRecordable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
class SignificantChangeRecordable extends DataExtension
{

private static $ignored_fields = array();
private static $ignored_fields = [];

private static $significant_fields = array();
private static $significant_fields = [];

private static $db = array(
private static $db = [
'LastSignificantChange' => 'DBDatetime',
'ChangeDescription' => 'Text'
);
];

public function updateCMSFields(FieldList $fields)
{
Expand Down
15 changes: 7 additions & 8 deletions src/Extension/SiteTreeChangeRecordable.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@ public function updateCMSFields(FieldList $fields)
//create a gridfield out of them
$gridFieldConfig = GridFieldConfig_RecordViewer::create();
$publishedGrid = new GridField('PublishStates', 'Published States', $dataChanges, $gridFieldConfig);
$dataColumns = $publishedGrid->getConfig()->getComponentByType('SilverStripe\Forms\GridField\GridFieldDataColumns');
$dataColumns->setDisplayFields(
array('ChangeType' => 'Change Type',
'ObjectTitle' => 'Page Title',
'ChangedBy.Title' => 'User',
'Created' => 'Modification Date',
)
);
$dataColumns = $publishedGrid->getConfig()->getComponentByType(\SilverStripe\Forms\GridField\GridFieldDataColumns::class);
$dataColumns->setDisplayFields([
'ChangeType' => 'Change Type',
'ObjectTitle' => 'Page Title',
'ChangedBy.Title' => 'User',
'Created' => 'Modification Date'
]);

//linking through to the datachanges modeladmin

Expand Down
2 changes: 1 addition & 1 deletion src/Job/CleanupDataChangeHistoryTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function run($request)
return;
}

$since = strtotime($since);
$since = strtotime((string) $since);
if (!$since) {
echo "Please specify an 'older' param with a date older than which to prune (in strtotime friendly format)<br/>\n";
return;
Expand Down
3 changes: 1 addition & 2 deletions src/Job/PruneChangesBeforeJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct($priorTo = null)
{
$ts = 0;
if ($priorTo) {
$ts = strtotime($priorTo);
$ts = strtotime((string) $priorTo);
}
if ($ts <= 0) {
$ts = time() - 90 * 86400;
Expand Down Expand Up @@ -69,4 +69,3 @@ public function process()
Injector::inst()->get(QueuedJobService::class)->queueJob($job, $next);
}
}

76 changes: 38 additions & 38 deletions src/Model/DataChangeRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Symbiote\DataChange\Model;

use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Security;
use SilverStripe\View\Requirements;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\ToggleCompositeField;
Expand Down Expand Up @@ -36,24 +37,24 @@ class DataChangeRecord extends DataObject
'GetVars' => 'Text',
'PostVars' => 'Text',
];
private static $has_one = array(
'ChangedBy' => 'SilverStripe\Security\Member',
'ChangeRecord' => 'SilverStripe\ORM\DataObject',
);
private static $summary_fields = array(
private static $has_one = [
'ChangedBy' => Member::class,
'ChangeRecord' => DataObject::class
];
private static $summary_fields = [
'ChangeType' => 'Change Type',
'ChangeRecordClass' => 'Record Class',
'ChangeRecordID' => 'Record ID',
'ObjectTitle' => 'Record Title',
'ChangedBy.Title' => 'User',
'Created' => 'Modification Date',
);
private static $searchable_fields = array(
'Created' => 'Modification Date'
];
private static $searchable_fields = [
'ChangeType',
'ObjectTitle',
'ChangeRecordClass',
'ChangeRecordID',
);
'ChangeRecordID'
];
private static $default_sort = 'ID DESC';

/**
Expand All @@ -62,8 +63,8 @@ class DataChangeRecord extends DataObject
* @var boolean
*/
private static $save_request_vars = false;
private static $field_blacklist = array('Password');
private static $request_vars_blacklist = array('url', 'SecurityID');
private static $field_blacklist = ['Password'];
private static $request_vars_blacklist = ['url', 'SecurityID'];

public function getCMSFields($params = null)
{
Expand All @@ -73,7 +74,7 @@ public function getCMSFields($params = null)
ToggleCompositeField::create(
'Details',
'Details',
array(
[
ReadonlyField::create('ChangeType', 'Type of change'),
ReadonlyField::create('ChangeRecordClass', 'Record Class'),
ReadonlyField::create('ChangeRecordID', 'Record ID'),
Expand All @@ -84,18 +85,18 @@ public function getCMSFields($params = null)
ReadonlyField::create('CurrentURL', 'URL'),
ReadonlyField::create('Referer', 'Referer'),
ReadonlyField::create('RemoteIP', 'Remote IP'),
ReadonlyField::create('Agent', 'Agent'),
)
ReadonlyField::create('Agent', 'Agent')
]
)->setStartClosed(false)->addExtraClass('datachange-field'),
ToggleCompositeField::create(
'RawData',
'Raw Data',
array(
[
ReadonlyField::create('Before'),
ReadonlyField::create('After'),
ReadonlyField::create('GetVars'),
ReadonlyField::create('PostVars'),
)
ReadonlyField::create('PostVars')
]
)->setStartClosed(false)->addExtraClass('datachange-field')
);

Expand All @@ -106,11 +107,11 @@ public function getCMSFields($params = null)

// The solr search service injector dependency causes issues with comparison, since it has public variables that are stored in an array.

$diff->ignoreFields(array('searchService'));
$diff->ignoreFields(['searchService']);
$diffed = $diff->diffedData();
$diffText = '';

$changedFields = array();
$changedFields = [];
foreach ($diffed->toMap() as $field => $prop) {
if (is_object($prop)) {
continue;
Expand All @@ -119,33 +120,33 @@ public function getCMSFields($params = null)
$prop = json_encode($prop);
}
$changedFields[] = $readOnly = \SilverStripe\Forms\ReadonlyField::create(
'ChangedField'.$field,
'ChangedField' . $field,
$field,
$prop
);
$readOnly->addExtraClass('datachange-field');
}

$fields->insertBefore(
'RawData',
ToggleCompositeField::create('FieldChanges', 'Changed Fields', $changedFields)
->setStartClosed(false)
->addExtraClass('datachange-field'),
'RawData'
->addExtraClass('datachange-field')
);
}

// Flags fields that cannot be rendered with 'forTemplate'. This prevents bugs where
// WorkflowService (of AdvancedWorkflow Module) and BlockManager (of Sheadawson/blocks module) get put
// into a field and break the page.
$fieldsToRemove = array();
$fieldsToRemove = [];
foreach ($fields->dataFields() as $field) {
$value = $field->Value();
if ($value && is_object($value)) {
if ((method_exists($value, 'hasMethod') && !$value->hasMethod('forTemplate')) || !method_exists(
$value,
'forTemplate'
)) {
$field->setValue('[Missing '.get_class($value).'::forTemplate]');
$field->setValue('[Missing ' . $value::class . '::forTemplate]');
}
}
}
Expand Down Expand Up @@ -198,8 +199,8 @@ public function track(DataObject $changedObject, $type = 'Change')
$this->ObjectTitle = $changedObject->Title;
$this->Stage = Versioned::get_reading_mode();

$before = array();
$after = array();
$before = [];
$after = [];

if ($type != 'Change' && $type != 'New') { // If we are (un)publishing we want to store the entire object
$before = ($type === 'Unpublish') ? $changedObject->toMap() : null;
Expand Down Expand Up @@ -237,27 +238,26 @@ public function track(DataObject $changedObject, $type = 'Change')
$this->PostVars = isset($_POST) ? json_encode($_POST) : null;
}

$this->ChangedByID = Member::currentUserID();

if (Member::currentUserID() && Member::currentUser()) {
$this->CurrentEmail = Member::currentUser()->Email;
if ($member = Security::getCurrentUser()) {
$this->ChangedByID = $member->ID;
$this->CurrentEmail = $member->Email;
}

if (isset($_SERVER['SERVER_NAME'])) {
$protocol = 'http';
$protocol = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? 'https://' : 'http://';
$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : '80';
$port = $_SERVER['SERVER_PORT'] ?? '80';

$this->CurrentURL = $protocol.$_SERVER["SERVER_NAME"].":".$port.$_SERVER["REQUEST_URI"];
$this->CurrentURL = $protocol . $_SERVER["SERVER_NAME"] . ":" . $port . $_SERVER["REQUEST_URI"];
} elseif (Director::is_cli()) {
$this->CurrentURL = 'CLI';
} else {
$this->CurrentURL = 'Could not determine current URL';
}

$this->RemoteIP = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : (Director::is_cli() ? 'CLI' : 'Unknown remote addr');
$this->Referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$this->Agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$this->RemoteIP = $_SERVER['REMOTE_ADDR'] ?? (Director::is_cli() ? 'CLI' : 'Unknown remote addr');
$this->Referer = $_SERVER['HTTP_REFERER'] ?? '';
$this->Agent = $_SERVER['HTTP_USER_AGENT'] ?? '';

$this->write();
return $this;
Expand All @@ -276,7 +276,7 @@ public function canDelete($member = null)
* */
public function getTitle()
{
return $this->ChangeRecordClass.' #'.$this->ChangeRecordID;
return $this->ChangeRecordClass . ' #' . $this->ChangeRecordID;
}

/**
Expand Down Expand Up @@ -306,7 +306,7 @@ private function prepareForDataDifferencer($jsonData)
//
// So solve this, we simply only decode to a depth of 1. (rather than the 512 default)
//
$resultJsonData = json_decode($jsonData, true, 1);
$resultJsonData = json_decode((string) $jsonData, true, 1);
return $resultJsonData;
}
}
6 changes: 3 additions & 3 deletions src/Model/TrackedManyManyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
*/
class TrackedManyManyList extends ManyManyList
{
public $trackedRelationships = array();
public $trackedRelationships = [];

public function add($item, $extraFields = array())
public function add($item, $extraFields = [])
{
$this->recordManyManyChange(__FUNCTION__, $item);
$result = parent::add($item, $extraFields);
Expand Down Expand Up @@ -58,7 +58,7 @@ protected function recordManyManyChange($type, $item)
$item = $class::get()->byID($item);
}
$join = $type === 'add' ? ' to ' : ' from ';
$type = ucfirst($type) . ' "' . $item->Title . '"' . $join . $relationName;
$type = ucfirst((string) $type) . ' "' . $item->Title . '"' . $join . $relationName;
$onItem->RelatedItem = $item->ClassName . ' #' . $item->ID;
singleton('DataChangeTrackService')->track($onItem, $type);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Service/DataChangeTrackService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
/**
* @author Stephen McMahon <stephen@symbiote.com.au>
*/
class DataChangeTrackService
class DataChangeTrackService implements \Stringable
{

protected $dcr_cache = array();
protected $dcr_cache = [];

public $disabled = false;

Expand All @@ -30,11 +30,12 @@ public function track(DataObject $object, $type = 'Change')
$this->dcr_cache["{$object->ID}-{$object->Classname}"]->track($object, $type);
}

public function resetChangeCache() {
public function resetChangeCache()
{
$this->dcr_cache = [];
}

public function __toString()
public function __toString(): string
{
return '';
}
Expand Down
Loading

0 comments on commit f4089ea

Please sign in to comment.