Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Remove use of ChangeSets from RecursivePublishable #490

Closed
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
78 changes: 57 additions & 21 deletions src/RecursivePublishable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\Queries\SQLUpdate;
use SilverStripe\Model\List\SS_List;
use SilverStripe\UserForms\Model\EditableCustomRule;

/**
* Provides owns / owned_by and recursive publishing API for all objects.
Expand Down Expand Up @@ -63,37 +64,48 @@ public function publishRecursive()
$owner = $this->owner;

// get the last published version
$original = null;
$liveOwner = null;
if ($owner->hasExtension(Versioned::class) && $owner->isPublished()) {
$original = Versioned::get_by_stage($owner->baseClass(), Versioned::LIVE)
$liveOwner = Versioned::get_by_stage($owner->baseClass(), Versioned::LIVE)
->byID($owner->ID);
}

$owner->invokeWithExtensions('onBeforePublishRecursive', $original);

// Create a new changeset for this item and publish it
$changeset = ChangeSet::create();
$changeset->IsInferred = true;
$changeset->Name = _t(
__CLASS__ . '.INFERRED_TITLE',
"Generated by publish of '{title}' at {created}",
[
'title' => $owner->Title,
'created' => DBDatetime::now()->Nice()
]
);
$owner->invokeWithExtensions('onBeforePublishRecursive', $liveOwner);

$draftObjs = $this->getRecursivelyOwnedVersionedDataObjectsByStage($owner, Versioned::DRAFT);
$liveObjs = $this->getRecursivelyOwnedVersionedDataObjectsByStage($liveOwner, Versioned::LIVE);
$liveOnlyKeys = array_diff(array_keys($liveObjs), array_keys($draftObjs));

// Delete any records that were in live but not in draft
foreach ($liveOnlyKeys as $key) {
if (array_key_exists($key, $liveObjs)) {
/** @var DataObject|Versioned $liveObj */
$liveObj = $liveObjs[$key];
$liveObj->doUnpublish();
// $liveObj->suppressDeletedVersion(function () use ($liveObj) {
// $liveObj->doUnpublish();
// });
// $liveObj->deleteFromStage(Versioned::LIVE);
// $liveObj->suppressDeletedVersion(function () use ($liveObj) {
// $liveObj->deleteFromStage(Versioned::LIVE);
// });
// $liveObj->isOnLiveOnly
}
}

$changeset->write();
$changeset->addObject($owner);
// Recursively publish owned DataObjects, starting with most nested ones
foreach (array_reverse($draftObjs) as $obj) {
$obj->publishSingle();
}

$result = $changeset->publish(true);
if (!$result) {
return $result;
// Finally, publish this object
if ($owner->hasExtension(Versioned::class)) {
$owner->publishSingle();
}

$owner->invokeWithExtensions('onAfterPublishRecursive', $original);

return $result;
return true;
});
}

Expand Down Expand Up @@ -470,4 +482,28 @@ protected function onAfterDelete(): void
{
RecursiveStagesService::reset();
}

/**
* Returns a map of {dataClass}.{ID} => DataObject of DataObjectsions that are:
* - Recursively owned by the current object
* - Have the Versioned extension
* - Exist in the same stage as the current object
*/
private function getRecursivelyOwnedVersionedDataObjectsByStage(?DataObject $owner, string $stage): array
{
if (!$owner) {
return [];
}
$objs = Versioned::withVersionedMode(function () use ($owner, $stage) {
Versioned::set_stage($stage);
return array_filter(
$owner->findOwned(true)->toArray(),
fn(DataObject $obj) => $obj->hasExtension(Versioned::class)
);
});
return array_combine(
array_map(fn(DataObject $obj) => get_class($obj) . '.' . $obj->ID, $objs),
$objs
);
}
}
Loading