Skip to content

Commit

Permalink
Clean up old lock files in the changes controller
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Nov 26, 2024
1 parent 6a005e9 commit 5d45af2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/Api/Controller/Changes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Kirby\Api\Controller;

use Kirby\Cms\ModelWithContent;
use Kirby\Content\Lock;
use Kirby\Content\VersionId;
use Kirby\Filesystem\F;
use Kirby\Form\Form;

/**
Expand All @@ -18,13 +20,23 @@
*/
class Changes
{
/**
* Cleans up legacy lock files
*/
protected static function cleanup(ModelWithContent $model): void
{
F::remove(Lock::legacyFile($model));
}

/**
* Discards unsaved changes by deleting the changes version
*/
public static function discard(ModelWithContent $model): array
{
$model->version(VersionId::changes())->delete('current');

static::cleanup($model);

return [
'status' => 'ok'
];
Expand All @@ -41,6 +53,8 @@ public static function publish(ModelWithContent $model, array $input): array
input: $input
);

static::cleanup($model);

// get the changes version
$changes = $model->version(VersionId::changes());

Expand Down Expand Up @@ -77,6 +91,8 @@ public static function save(ModelWithContent $model, array $input): array
$changes = $model->version(VersionId::changes());
$latest = $model->version(VersionId::latest());

static::cleanup($model);

// combine the new field changes with the
// last published state
$changes->save(
Expand Down
18 changes: 13 additions & 5 deletions src/Content/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Kirby\Cms\App;
use Kirby\Cms\Language;
use Kirby\Cms\Languages;
use Kirby\Cms\ModelWithContent;
use Kirby\Cms\User;
use Kirby\Data\Data;
use Kirby\Toolkit\Str;
Expand Down Expand Up @@ -42,7 +43,7 @@ public static function for(
Language|string $language = 'default'
): static {

if ($legacy = static::legacy($version)) {
if ($legacy = static::legacy($version->model())) {
return $legacy;
}

Expand Down Expand Up @@ -143,12 +144,10 @@ public function isLocked(): bool
* Looks for old .lock files and tries to create a
* usable lock instance from them
*/
public static function legacy(Version $version): static|null
public static function legacy(ModelWithContent $model): static|null
{
$model = $version->model();
$kirby = $model->kirby();
$root = $model::CLASS_ALIAS === 'file' ? dirname($model->root()) : $model->root();
$file = $root . '/.lock';
$file = static::legacyFile($model);
$id = '/' . $model->id();

// no legacy lock file? no lock.
Expand All @@ -175,6 +174,15 @@ public static function legacy(Version $version): static|null
);
}

/**
* Returns the absolute path to a legacy lock file
*/
public static function legacyFile(ModelWithContent $model): string
{
$root = $model::CLASS_ALIAS === 'file' ? dirname($model->root()) : $model->root();
return $root . '/.lock';
}

/**
* Returns the timestamp when the locked content has
* been updated. You can pass a format to get a useful,
Expand Down

0 comments on commit 5d45af2

Please sign in to comment.