Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions src/CLI/Commands/UUID/Duplicates.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,13 @@ protected static function check(CLI $cli, ModelWithContent $model, array &$uuids

// the UUID already exists for another model
if (isset($uuids[$uuid])) {
$duplicates[] = $uuid;

if ($cli->arg('fix') === true) {
$model->uuid()->clear();
$model->update([
'uuid' => Uuid::generate()
]);
$model->uuid()->populate(true);
$cli->out('✅ The duplicate UUID ' . $uuid . ' for ' . $model->id() . ' has been regenerated');
} else {
$cli->error('The UUID ' . $uuid . ' for ' . $model->id() . ' exists (' . $uuids[$uuid] . ')');
}
// add this model and the one with the duplicate uuid
// to the duplicates array to clean them up later
$duplicates[] = $model;
$duplicates[] = $uuids[$uuid];
}

$uuids[$uuid] = $model->id();
$uuids[$uuid] = $model;
}

public static function command(CLI $cli): void
Expand Down Expand Up @@ -79,13 +71,42 @@ public static function command(CLI $cli): void
}
}

if (count($uuids) === 0) {
// remove duplicates from duplicates array
$duplicates = array_unique($duplicates);

if (count($duplicates) === 0) {
$cli->success('There are no UUID duplicates');
} elseif ($fix === true) {
return;
}

// go through all collected models with duplicate UUIDs
// and print info or fix them.
foreach ($duplicates as $model) {
$uuid = $model->uuid()->toString();

if ($fix === true) {
static::regenerate($model);
$cli->out('✅ The duplicate UUID ' . $uuid . ' for ' . $model->id() . ' has been regenerated');
} else {
$cli->error('The UUID ' . $uuid . ' for ' . $model->id() . ' exists');
}
}

if ($fix === true) {
$cli->success(count($duplicates) . ' duplicates have been fixed');
} else {
$cli->out(count($duplicates) . ' duplicates! You can fix them with kirby uuid:duplicates --fix');
exit(1);
return;
}

$cli->out(count($duplicates) . ' duplicates! You can fix them with kirby uuid:duplicates --fix');
exit(1);
}

protected static function regenerate(ModelWithContent $model): void
{
$model->uuid()->clear();
$model->update([
'uuid' => Uuid::generate()
]);
$model->uuid()->populate(true);
}
}
Loading