1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00

Updated versioning to support any field

This commit is contained in:
Laurent Cozic 2017-01-08 00:09:04 +01:00
parent cc2e01a21b
commit 794ede1e4c
8 changed files with 38 additions and 52 deletions

View File

@ -1,9 +1,12 @@
<table>
<tr><th>Title</th><th></th><th></th></tr>
<tr><th>ID</th><th>Title</th><th></th><th></th></tr>
<?php foreach ($folders as $folder): ?>
<tr>
<td>
<a href="/?action=folder&folder_id=<?php echo $folder['id']; ?>"><?php echo htmlentities($folder['title']); ?></a>
<a href="/?action=folder&folder_id=<?php echo $folder['id']; ?>"><?php echo htmlentities($folder['id']); ?></a>
</td>
<td>
<?php echo htmlentities($folder['title']); ?>
</td>
<td>
<a href="/?action=notes&folder_id=<?php echo $folder['id']; ?>">View notes</a>

View File

@ -1,28 +0,0 @@
<?php
namespace AppBundle\Model;
class Action extends BaseModel {
static protected $enums = array(
'type' => array('create', 'update', 'delete'),
);
static public function actionsDoneAfterId($userId, $clientId, $actionId) {
$limit = 100;
$items = self::where('id', '>', $actionId)
->where('user_id', '=', $userId)
->where('client_id', '!=', $clientId)
->orderBy('id')
->limit($limit + 1)
->get();
$hasMore = $limit < count($items);
if ($hasMore) array_pop($items);
return array(
'has_more' => $hasMore,
'items' => $items,
);
}
}

View File

@ -12,6 +12,7 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
public $useUuid = false;
protected $changedVersionedFieldValues = array();
protected $versionedFields = array();
protected $isVersioned = false;
private $isNew = null;
private $revId = 0;
@ -162,10 +163,6 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
$output['item_type'] = BaseItem::enumName('type', $output['item_type'], true);
}
if (isset($output['item_field'])) {
$output['item_field'] = BaseModel::enumName('field', $output['item_field'], true);
}
$maxRevId = 0;
foreach ($this->versionedFields as $field) {
$r = $this->versionedFieldValue($field, true);
@ -179,7 +176,7 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
}
public function versionedFieldValue($fieldName, $returnRevId = false) {
return Change::fullFieldText($this->id, BaseModel::enumId('field', $fieldName), null, $returnRevId);
return Change::fullFieldText($this->id, $fieldName, null, $returnRevId);
}
public function setVersionedFieldValue($fieldName, $fieldValue) {
@ -324,14 +321,21 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
$this->updated_time = time(); // TODO: maybe only update if one of the fields, or if some of versioned data has changed
if ($isNew) $this->created_time = time();
if ($this->isVersioned) {
$changedFields = array_merge($this->getDirty(), $this->changedVersionedFieldValues);
unset($changedFields['updated_time']);
}
$output = parent::save($options);
$this->isNew = null;
if (count($this->versionedFields)) {
$this->recordChanges($isNew ? 'create' : 'update', $this->changedVersionedFieldValues);
if ($this->isVersioned) {
if (count($changedFields)) {
$this->recordChanges($isNew ? 'create' : 'update', $changedFields);
}
$this->changedVersionedFieldValues = array();
}
$this->changedVersionedFieldValues = array();
return $output;
}
@ -346,17 +350,22 @@ class BaseModel extends \Illuminate\Database\Eloquent\Model {
return $output;
}
protected function recordChanges($type, $versionedData = array()) {
protected function recordChanges($type, $changedFields = array()) {
if ($type == 'delete') {
$change = $this->newChange($type);
$change->save();
} else if ($type == 'create' || $type == 'update') {
foreach ($this->versionedFields as $field) {
if (!isset($versionedData[$field])) continue;
// When recording a "create" event, we only record the versioned fields because the complete history
// is required to build their value. There's no need to record the other fields since they are
// simply new.
//
// When recording an "update" event, all the modified fields are recorded.
foreach ($changedFields as $field => $value) {
if ($type == 'create' && !in_array($field, $this->versionedFields)) continue;
$change = $this->newChange($type);
$change->item_field = BaseModel::enumId('field', $field);
$change->createDelta($versionedData[$field]);
$change->item_field = $field;
if (in_array($field, $this->versionedFields)) $change->createDelta($changedFields[$field]);
$change->save();
}
} else {

View File

@ -76,7 +76,7 @@ class Change extends BaseModel {
'type' => self::enumName('type', $this->type),
'item_id' => self::hex($this->item_id),
'item_type' => FolderItem::enumName('type', $this->item_type),
'item_field' => BaseModel::enumName('field', $this->item_field),
'item_field' => $this->item_field,
);
}

View File

@ -5,6 +5,7 @@ namespace AppBundle\Model;
class Folder extends BaseItem {
protected $versionedFields = array('title');
protected $isVersioned = true;
public function add($ids) {
$notes = Note::find($ids);

View File

@ -5,5 +5,6 @@ namespace AppBundle\Model;
class Note extends BaseItem {
protected $versionedFields = array('title', 'body');
protected $isVersioned = true;
}

View File

@ -80,7 +80,7 @@ CREATE TABLE `changes` (
`type` int(11) NOT NULL default '0',
`item_id` binary(16),
`item_type` int(11) NOT NULL default '0',
`item_field` int(11) NOT NULL default '0',
`item_field` varchar(32) NOT NULL default '',
`delta` MEDIUMTEXT,
`previous_id` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)

View File

@ -25,7 +25,7 @@ class ChangeTest extends BaseTestCase {
$change->user_id = $this->user()->id;
$change->client_id = $this->clientId();
$change->item_type = BaseItem::enumId('type', 'note');
$change->item_field = BaseModel::enumId('field', 'body');
$change->item_field = 'body';
$change->type = Change::enumId('type', 'create');
$change->item_id = $itemId;
$change->createDelta($text1);
@ -37,13 +37,13 @@ class ChangeTest extends BaseTestCase {
$change->user_id = $this->user()->id;
$change->client_id = $this->clientId();
$change->item_type = BaseItem::enumId('type', 'note');
$change->item_field = BaseModel::enumId('field', 'body');
$change->item_field = 'body';
$change->type = Change::enumId('type', 'update');
$change->item_id = $itemId;
$change->createDelta($text2);
$change->save();
$r = Change::fullFieldText($itemId, BaseModel::enumId('field', 'body'));
$r = Change::fullFieldText($itemId, 'body');
$this->assertEquals($r, $text2);
}
@ -84,7 +84,7 @@ class ChangeTest extends BaseTestCase {
$change->user_id = $this->user()->id;
$change->client_id = $this->clientId(1);
$change->item_type = BaseItem::enumId('type', 'note');
$change->item_field = BaseModel::enumId('field', 'body');
$change->item_field = 'body';
$change->type = Change::enumId('type', 'create');
$change->item_id = $itemId;
$change->createDelta($text1);
@ -98,7 +98,7 @@ class ChangeTest extends BaseTestCase {
$change->user_id = $this->user()->id;
$change->client_id = $this->clientId(2);
$change->item_type = BaseItem::enumId('type', 'note');
$change->item_field = BaseModel::enumId('field', 'body');
$change->item_field = 'body';
$change->type = Change::enumId('type', 'update');
$change->item_id = $itemId;
$change->previous_id = $changeId1;
@ -113,7 +113,7 @@ class ChangeTest extends BaseTestCase {
$change->user_id = $this->user()->id;
$change->client_id = $this->clientId(1);
$change->item_type = BaseItem::enumId('type', 'note');
$change->item_field = BaseModel::enumId('field', 'body');
$change->item_field = 'body';
$change->type = Change::enumId('type', 'update');
$change->item_id = $itemId;
$change->previous_id = $changeId1;
@ -122,7 +122,7 @@ class ChangeTest extends BaseTestCase {
$changeId3 = $change->id;
$r = Change::fullFieldText($itemId, BaseModel::enumId('field', 'body'));
$r = Change::fullFieldText($itemId, 'body');
$this->assertEquals($r, 'cd CLIENT1 efgh ijkl FROMCLIENT2');
}