Skip to content

Commit

Permalink
Added ability to configure revision limit
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Sep 22, 2018
1 parent 0931ff3 commit 3f58800
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
11 changes: 7 additions & 4 deletions app/Repos/EntityRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,13 @@ public function savePageRevision(Page $page, $summary = null)
$revision->revision_number = $page->revision_count;
$revision->save();

// Clear old revisions
if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
$this->pageRevision->where('page_id', '=', $page->id)
->orderBy('created_at', 'desc')->skip(50)->take(5)->delete();
$revisionLimit = config('app.revision_limit');
if ($revisionLimit !== false) {
$revisionsToDelete = $this->pageRevision->where('page_id', '=', $page->id)
->orderBy('created_at', 'desc')->skip(intval($revisionLimit))->take(10)->get(['id']);
if ($revisionsToDelete->count() > 0) {
$this->pageRevision->whereIn('id', $revisionsToDelete->pluck('id'))->delete();
}
}

return $revision;
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"type": "project",
"require": {
"php": ">=7.0.0",
"ext-tidy": "*",
"ext-dom": "*",
"laravel/framework": "~5.5.42",
"fideloper/proxy": "~3.3",
"ext-tidy": "*",
"intervention/image": "^2.4",
"laravel/socialite": "^3.0",
"league/flysystem-aws-s3-v3": "^1.0",
Expand Down
7 changes: 7 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
'books' => env('APP_VIEWS_BOOKS', 'list')
],

/**
* The number of revisions to keep in the database.
* Once this limit is reached older revisions will be deleted.
* If set to false then a limit will not be enforced.
*/
'revision_limit' => env('REVISION_LIMIT', 50),

/**
* Allow <script> tags to entered within page content.
* <script> tags are escaped by default.
Expand Down
30 changes: 30 additions & 0 deletions tests/Entity/PageRevisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,34 @@ public function test_revision_deletion() {
$afterRevisionCount = $page->revisions->count();
$this->assertTrue($beforeRevisionCount === $afterRevisionCount);
}

public function test_revision_limit_enforced()
{
config()->set('app.revision_limit', 2);
$page = Page::first();
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
$page = Page::find($page->id);
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
for ($i = 0; $i < 10; $i++) {
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
}

$revisionCount = $page->revisions()->count();
$this->assertEquals(2, $revisionCount);
}

public function test_false_revision_limit_allows_many_revisions()
{
config()->set('app.revision_limit', false);
$page = Page::first();
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
$page = Page::find($page->id);
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
for ($i = 0; $i < 10; $i++) {
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
}

$revisionCount = $page->revisions()->count();
$this->assertEquals(12, $revisionCount);
}
}

0 comments on commit 3f58800

Please sign in to comment.