<commit_before><?php
namespace App\Model\Table;

use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Validation\Validator;

class AuditsTable
	extends AppTable
{

	public function initialize(array $config)
	{
		parent::initialize($config);

		$this->table('audits');
		$this->primaryKey('id');
	}

	public function validationDefault(Validator $validator)
	{
		$validator->allowEmpty('id', 'create');
		$validator->notEmpty('entity');
		$validator->notEmpty('key1');
		$validator->allowEmpty('key2');
		$validator->allowEmpty('data');
		$validator->allowEmpty('modified');
		$validator->allowEmpty('modifier_id');

		$validator->add('id',          'valid', ['rule' => 'numeric']);
		$validator->add('key1',        'valid', ['rule' => 'numeric']);
		$validator->add('key2',        'valid', ['rule' => 'numeric']);
		$validator->add('modifier_id', 'valid', ['rule' => 'numeric']);

		$validator->requirePresence('entity', 'create');
		$validator->requirePresence('key1',   'create');

		return $validator;
	}

	public function beforeDelete(Event $event, EntityInterface $entity, $options)
	{
	}

	public function beforeSave(Event $event, EntityInterface $entity, $options)
	{
	}

	public function logDeletion(EntityInterface $entity)
	{
		$lastChange = $this->logChange($entity);

		$audit = $this->newEntity();
		$audit->set('entity', $lastChange->get('entity'));
		$audit->set('key1', $lastChange->get('key1'));
		$audit->set('key2', $lastChange->get('key2'));
		$audit->set('data', NULL);

		$this->save($audit);

		return $audit;
	}

	public function logChange(EntityInterface $entity)
	{
		$table = TableRegistry::get($entity->source());
		$data = $entity->extractOriginal($table->getSchema()->columns());

		$primary = $table->primaryKey();
		if(!is_array($primary))
			$primary = [$primary];

		$audit = $this->newEntity();
		$audit->set('entity', $entity->getClass());
		foreach($primary as $key => $field) {
			$audit->set("key".($key+1), $data[$field]);
		}
		$audit->set('data', json_encode($data));
		$audit->set('modified', $data['modified']);
		$audit->set('modifier_id', $data['modifier_id']);

		$this->save($audit);

		return $audit;<commit_msg>audit: only save old version on actual change<commit_after><?php
namespace App\Model\Table;

use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Validation\Validator;

class AuditsTable
	extends AppTable
{

	public function initialize(array $config)
	{
		parent::initialize($config);

		$this->table('audits');
		$this->primaryKey('id');
	}

	public function validationDefault(Validator $validator)
	{
		$validator->allowEmpty('id', 'create');
		$validator->notEmpty('entity');
		$validator->notEmpty('key1');
		$validator->allowEmpty('key2');
		$validator->allowEmpty('data');
		$validator->allowEmpty('modified');
		$validator->allowEmpty('modifier_id');

		$validator->add('id',          'valid', ['rule' => 'numeric']);
		$validator->add('key1',        'valid', ['rule' => 'numeric']);
		$validator->add('key2',        'valid', ['rule' => 'numeric']);
		$validator->add('modifier_id', 'valid', ['rule' => 'numeric']);

		$validator->requirePresence('entity', 'create');
		$validator->requirePresence('key1',   'create');

		return $validator;
	}

	public function beforeDelete(Event $event, EntityInterface $entity, $options)
	{
	}

	public function beforeSave(Event $event, EntityInterface $entity, $options)
	{
	}

	public function logDeletion(EntityInterface $entity)
	{
		$lastChange = $this->logChange($entity);

		$audit = $this->newEntity();
		$audit->set('entity', $lastChange->get('entity'));
		$audit->set('key1', $lastChange->get('key1'));
		$audit->set('key2', $lastChange->get('key2'));
		$audit->set('data', NULL);

		$this->save($audit);

		return $audit;
	}

	public function logChange(EntityInterface $entity)
	{
		$table = TableRegistry::get($entity->source());
		$columns = $table->getSchema()->columns();

		if($entity->dirty('modified')
		&& count($entity->extractOriginalChanged($columns)) == 1) {
			return;
		}

		$data = $entity->extractOriginal($columns);

		$primary = $table->primaryKey();
		if(!is_array($primary))
			$primary = [$primary];

		$audit = $this->newEntity();
		$audit->set('entity', $entity->getClass());
		foreach($primary as $key => $field) {
			$audit->set("key".($key+1), $data[$field]);
		}
		$audit->set('data', json_encode($data));
		$audit->set('modified', $data['modified']);
		$audit->set('modifier_id', $data['modifier_id']);

		$this->save($audit);

		return $audit;
{'fraction_non_alphanumeric': 0.2127059843885516, 'fraction_numerical': 0.004553339115351257, 'mean_word_length': 5.406944444444444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3928766', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1534, 'n_words': 258}