Versioned
class Versioned extends DataExtension implements TemplateGlobalProvider, Resettable (View source)
The Versioned extension allows your DataObjects to have several versions, allowing you to rollback changes and view history. An example of this is the pages used in the CMS.
Note: This extension relies on the object also having the {see \SilverStripe\Versioned\Ownership} extension applied.
Constants
DEFAULT_MODE |
The default reading mode |
STAGEDVERSIONED |
Constructor arg to specify that staging is active on this record. 'Staging' implies that 'Versioning' is also enabled. |
VERSIONED |
Constructor arg to specify that versioning only is active on this record. |
LIVE |
The Public stage. |
DRAFT |
The draft (default) stage |
MIGRATING_VERSION |
Field used to hold the migrating version |
NEXT_WRITE_WITHOUT_VERSIONED |
Field used to hold flag indicating the next write should be without a new version |
DELETE_WRITES_VERSION_DISABLED |
Prevents delete() from creating a _Versions record (in case this must be deferred)
Best used with suppressDeleteVersion() |
Properties
DataObject|RecursivePublishable|Versioned | $owner | ||
static private bool | $draft_site_secured | Default config for $is_draft_site_secured |
|
static private array | $non_virtual_fields | Ensure versioned page doesn't attempt to virtualise these non-db fields |
|
static private array | $casting | Ensure versioned records cast extra fields properly |
|
static private array | $db | ||
static private bool | $prepopulate_versionnumber_cache | Used to enable or disable the prepopulation of the version number cache. |
|
static private bool | $use_conditions_over_inner_joins | Indicates whether augmentSQL operations should add subselects as WHERE conditions instead of INNER JOIN intersections. Performance of the INNER JOIN scales on the size of _Versions tables where as the condition scales on the number of records being returned from the base query. |
|
static private array | $versionableExtensions | An array of DataObject extensions that may require versioning for extra tables The array value is a set of suffixes to form these table names, assuming a preceding '_'. |
|
static private array | $non_live_permissions | Permissions necessary to view records outside of the live stage (e.g. archive / draft stage). |
|
static private bool | $use_session | Use PHP's session storage for the "reading mode" and "unsecuredDraftSite", instead of explicitly relying on the "stage" query parameter. |
|
int | $Version |
Methods
Construct a new Versioned object.
Called when this extension is added to a particular class
Temporarily modify the owner. The original owner is ensured to be restored
Clear the current owner, and restore extension to the state prior to the last setOwner()
Helper method to strip eval'ed arguments from a string that's passed to {@link DataObject::$extensions} or {@link Object::add_extension()}.
Invoke extension point. This will prefer explicit extend
prefixed
methods.
Augment the the SQLSelect that is created by the DataQuery
Update the database schema as required by this extension.
Augment a write-record request.
No description
No description
Define extra database fields
This function is used to provide modifications to the form used for front end forms. {@link DataObject->getFrontEndFields()}
This is used to provide modifications to the form actions used in the CMS. {@link DataObject->getCMSActions()}.
this function is used to provide modifications to the summary fields in CMS by the extension By default, the summaryField() of its owner will merge more fields defined in the extension's $extra_fields['summary_fields']
No description
Reset static configuration variables to their default values.
Amend freshly created DataQuery objects with versioned-specific information.
Get this record at a specific version
Updates query parameters of relations attached to versioned dataobjects
For lazy loaded fields requiring extra sql manipulation, ie versioning.
Perform a write without affecting the version table.
Check if next write is without version
Set if next write should be without version or not
Check if delete() should write _Version rows or not
Set if delete() should write _Version rows
If a write was skipped, then we need to ensure that we don't leave a migrateVersion() value lying around for the next write.
This function should return true if the current user can publish this record.
Check if the current user can delete this record from live
Determine if there are any additional restrictions on this object for the given reading version.
Determines canView permissions for the latest version of this object on a specific stage.
Determine if a class is supporting the Versioned extensions (e.g.
Check if a certain table has the 'Version' field.
No description
Determines if the current draft version is the same as live or rather, that there are no outstanding draft changes
Publishes this object to Live, but doesn't publish owned objects.
Removes the record from both live and stage
Removes this record from the live site
Determine if this object is published, and has any published owners.
Revert the draft changes: replace the draft content with the content on live
Move a database record from one stage to the other.
Get version migrated to
Set the migrating version.
Compare two stages to see if they're different.
Compare two version, and return the diff between them.
Given a table and stage determine the table name.
Determine if the current user is able to set the given site stage / archive
Set the current reading mode.
Get the current reading mode.
Get the current reading stage.
Get the current archive date.
Get the current archive stage.
Set the reading stage.
Replace default mode.
Get default reading mode
Check if draft site should be secured.
Set if the draft site should be secured or not
Set the reading archive date.
Get a singleton instance of a class in the given stage.
Gets the current version number of a specific record.
Hook into {@link Hierarchy::prepopulateTreeDataCache}.
Pre-populate the cache for Versioned::get_versionnumber_by_stage() for a list of record IDs, for more efficient database querying. If $idList is null, then every record will be pre-cached.
Get a set of class instances by the given stage.
Delete this record from the given stage
Write the given record to the given stage.
Roll the draft version of this record to match the published record.
Recursively rollback draft to the given version. This will also rollback any owned objects at that point in time to the same date. Objects which didn't exist (or weren't attached) to the record at the target point in time will be "unlinked", which dis-associates the record without requiring a hard deletion.
Rollback draft to a given version
Return the latest version of the given record.
Returns whether the current record is the latest one.
Returns whether the current record's version is the current live/published version
Returns whether the current record's version is the current draft/modified version
Check if this record exists on live
Check if page doesn't exist on any stage, but used to be
Check if this record exists on the draft stage
Compares current draft with live version, and returns true if no draft version of this page exists but the page is still published (eg, after triggering "Delete from draft site" in the CMS).
Compares current draft with live version, and returns true if no live version exists, meaning the page was never published.
Compares current draft with live version, and returns true if these versions differ, meaning there have been unpublished changes to the draft site.
Return the equivalent of a DataList::create() call, querying the latest version of each record stored in the (class)_Versions tables.
Return the specific version of the given id.
Return a list of all versions for a given id.
No description
Return a piece of text to keep DataObject cache keys appropriately specific.
Returns an array of possible stages.
Called by SSViewer to get a list of global variables to expose to the template, the static method to call on this class to get the value for those variables, and the class to use for casting the returned value for use in a template
Check if this object has stages
Invoke a callback which may modify reading mode, but ensures this mode is restored after completion, without modifying global state.
Details
__construct($mode = self::STAGEDVERSIONED)
Construct a new Versioned object.
static
add_to_class(string $class, string $extensionClass, mixed $args = null)
Called when this extension is added to a particular class
setOwner(object $owner)
Set the owner of this extension.
mixed
withOwner(mixed $owner, callable $callback, array $args = [])
Temporarily modify the owner. The original owner is ensured to be restored
clearOwner()
Clear the current owner, and restore extension to the state prior to the last setOwner()
object
getOwner()
Returns the owner of this extension.
static string
get_classname_without_arguments(string $extensionStr)
Helper method to strip eval'ed arguments from a string that's passed to {@link DataObject::$extensions} or {@link Object::add_extension()}.
mixed
invokeExtension(object $owner, string $method, array ...$arguments)
Invoke extension point. This will prefer explicit extend
prefixed
methods.
static
unload_extra_statics($class, $extension)
validate(ValidationResult $validationResult)
Hook for extension-specific validation.
augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
Augment the the SQLSelect that is created by the DataQuery
See {see \SilverStripe\Versioned\augmentLazyLoadFields} for lazy-loading applied prior to this.
augmentDatabase()
Update the database schema as required by this extension.
When duplicating a table's structure, remember to duplicate the create options as well. See {@link Versioned->augmentDatabase} for an example.
augmentWrite(array $manipulation)
Augment a write-record request.
onBeforeWrite()
onAfterWrite()
onBeforeDelete()
onAfterDelete()
requireDefaultRecords()
populateDefaults()
can($member)
canEdit($member)
canDelete($member)
canCreate($member)
array
extraStatics(string $class = null, string $extension = null)
Define extra database fields
Return a map where the keys are db, has_one, etc, and the values are additional fields/relations to be defined.
updateCMSFields(FieldList $fields)
updateFrontEndFields(FieldList $fields)
This function is used to provide modifications to the form used for front end forms. {@link DataObject->getFrontEndFields()}
Caution: Use {@link FieldList->push()} to add fields.
updateCMSActions(FieldList $actions)
This is used to provide modifications to the form actions used in the CMS. {@link DataObject->getCMSActions()}.
updateSummaryFields(array $fields)
this function is used to provide modifications to the summary fields in CMS by the extension By default, the summaryField() of its owner will merge more fields defined in the extension's $extra_fields['summary_fields']
updateFieldLabels(array $labels)
static
reset()
Reset static configuration variables to their default values.
augmentDataQueryCreation(SQLSelect $query, DataQuery $dataQuery)
Amend freshly created DataQuery objects with versioned-specific information.
Versioned|DataObject
getAtVersion(int|string|null $from)
Get this record at a specific version
updateInheritableQueryParams(array $params)
Updates query parameters of relations attached to versioned dataobjects
augmentLoadLazyFields(SQLSelect $query, DataQuery $dataQuery = null, DataObject $dataObject)
For lazy loaded fields requiring extra sql manipulation, ie versioning.
int
writeWithoutVersion()
Perform a write without affecting the version table.
bool
getNextWriteWithoutVersion()
Check if next write is without version
DataObject
setNextWriteWithoutVersion(bool $flag)
Set if next write should be without version or not
bool
getDeleteWritesVersion()
Check if delete() should write _Version rows or not
DataObject
setDeleteWritesVersion(bool $flag)
Set if delete() should write _Version rows
onAfterSkippedWrite()
If a write was skipped, then we need to ensure that we don't leave a migrateVersion() value lying around for the next write.
bool
canPublish(Member $member = null)
This function should return true if the current user can publish this record.
It can be overloaded to customise the security model for an application.
Denies permission if any of the following conditions is true:
- canPublish() on any extension returns false
- canEdit() returns false
mixed
canUnpublish(null $member = null)
Check if the current user can delete this record from live
bool
canArchive(Member $member = null)
Check if the current user is allowed to archive this record.
If extended, ensure that both canDelete and canUnpublish are extended also
bool
canRevertToLive(Member $member = null)
Check if the user can revert this record to live
bool
canRestoreToDraft(Member $member = null)
Check if the user can restore this record to draft
bool|null
canView(Member $member = null)
Extend permissions to include additional security for objects that are not published to live.
bool
canViewVersioned(Member $member = null)
Determine if there are any additional restrictions on this object for the given reading version.
Override this in a subclass to customise any additional effect that Versioned applies to canView.
This is expected to be called by canView, and thus is only responsible for denying access if the default canView would otherwise ALLOW access. Thus it should not be called in isolation as an authoritative permission check.
This has the following extension points:
- canViewDraft is invoked if Mode = stage and Stage = stage
- canViewArchived is invoked if Mode = archive
bool
canViewStage(string $stage = self::LIVE, Member $member = null)
Determines canView permissions for the latest version of this object on a specific stage.
Usually the stage is read from {@link Versioned::current_stage()}.
This method should be invoked by user code to check if a record is visible in the given stage.
This method should not be called via ->extend('canViewStage'), but rather should be overridden in the extended class.
bool
canBeVersioned(string $class)
Determine if a class is supporting the Versioned extensions (e.g.
$table_Versions does exists).
bool
hasVersionField(string $table)
Check if a certain table has the 'Version' field.
string
extendWithSuffix(string $table)
bool
latestPublished()
Determines if the current draft version is the same as live or rather, that there are no outstanding draft changes
bool
publishSingle()
Publishes this object to Live, but doesn't publish owned objects.
User code should call {see \SilverStripe\Versioned\canPublish()} prior to invoking this method.
bool
doArchive()
Removes the record from both live and stage
User code should call {see \SilverStripe\Versioned\canArchive()} prior to invoking this method.
bool
doUnpublish()
Removes this record from the live site
User code should call {see \SilverStripe\Versioned\canUnpublish()} prior to invoking this method.
bool
hasPublishedOwners()
Determine if this object is published, and has any published owners.
If this is true, a warning should be shown before this is published.
Note: This method returns false if the object itself is unpublished, since owners are only considered on the same stage as the record itself.
bool
doRevertToLive()
Revert the draft changes: replace the draft content with the content on live
User code should call {see \SilverStripe\Versioned\canRevertToLive()} prior to invoking this method.
onAfterRevertToLive()
deprecated
deprecated
copyVersionToStage(int|string|null $fromStage, string $toStage, bool $createNewVersion = true)
Move a database record from one stage to the other.
int|null
getMigratingVersion()
Get version migrated to
DataObject
setMigratingVersion(string $version)
Set the migrating version.
bool
stagesDiffer()
Compare two stages to see if they're different.
Only checks the version numbers, not the actual content.
DataList
Versions()
Return all versions of the current object
DataObject
compareVersions(string $from, string $to)
Compare two version, and return the diff between them.
string
stageTable(string $table, string $stage)
Given a table and stage determine the table name.
Note: Stages this asset does not exist in will default to the draft table.
static bool
can_choose_site_stage(HTTPRequest $request)
Determine if the current user is able to set the given site stage / archive
static
choose_site_stage(HTTPRequest $request)
Choose the stage the site is currently on.
If $_GET['stage'] is set, then it will use that stage, and store it in the session.
if $_GET['archiveDate'] is set, it will use that date, and store it in the session.
If neither of these are set, it checks the session, otherwise the stage is set to 'Live'.
static
set_reading_mode(string $mode)
Set the current reading mode.
static string
get_reading_mode()
Get the current reading mode.
static string
get_stage()
Get the current reading stage.
static string
current_archived_date()
Get the current archive date.
static string
current_archived_stage()
Get the current archive stage.
static
set_stage(string $stage)
Set the reading stage.
static
set_default_reading_mode(string $mode)
Replace default mode.
An non-default mode should be specified via querystring arguments.
static string
get_default_reading_mode()
Get default reading mode
static bool
get_draft_site_secured()
Check if draft site should be secured.
Can be turned off if draft site unauthenticated
static
set_draft_site_secured(bool $secured)
Set if the draft site should be secured or not
static
reading_archived_date(string $date, string $stage = self::DRAFT)
Set the reading archive date.
static DataObject
get_one_by_stage(string $class, string $stage, string $filter = '', bool $cache = true, string $sort = '')
Get a singleton instance of a class in the given stage.
static int|null
get_versionnumber_by_stage(string $class, string $stage, int $id, bool $cache = true)
Gets the current version number of a specific record.
onPrepopulateTreeDataCache(DataList|array $recordList = null, array $options = [])
Hook into {@link Hierarchy::prepopulateTreeDataCache}.
static
prepopulate_versionnumber_cache(string $class, string $stage, array $idList = null)
Pre-populate the cache for Versioned::get_versionnumber_by_stage() for a list of record IDs, for more efficient database querying. If $idList is null, then every record will be pre-cached.
static DataList
get_by_stage(string $class, string $stage, string $filter = '', string $sort = '', string $join = '', int $limit = null, string $containerClass = DataList::class)
Get a set of class instances by the given stage.
deleteFromStage(string $stage)
Delete this record from the given stage
int
writeToStage(string $stage, bool $forceInsert = false)
Write the given record to the given stage.
Note: If writing to live, this will write to stage as well.
doRollbackTo(int $version)
deprecated
deprecated
Roll the draft version of this record to match the published record.
Caution: Doesn't overwrite the object properties with the rolled back version.
{see \SilverStripe\Versioned\doRevertToLive()} to reollback to live
onAfterRollback()
deprecated
deprecated
DataObject|Versioned
rollbackRecursive(int|string|null $version = null)
Recursively rollback draft to the given version. This will also rollback any owned objects at that point in time to the same date. Objects which didn't exist (or weren't attached) to the record at the target point in time will be "unlinked", which dis-associates the record without requiring a hard deletion.
rollbackSingle(int|string|null $version)
Rollback draft to a given version
static DataObject
get_latest_version(string $class, int $id)
Return the latest version of the given record.
bool
isLatestVersion()
Returns whether the current record is the latest one.
bool
isLiveVersion()
Returns whether the current record's version is the current live/published version
bool
isLatestDraftVersion()
Returns whether the current record's version is the current draft/modified version
bool
isPublished()
Check if this record exists on live
bool
isArchived()
Check if page doesn't exist on any stage, but used to be
bool
isOnDraft()
Check if this record exists on the draft stage
bool
isOnLiveOnly()
Compares current draft with live version, and returns true if no draft version of this page exists but the page is still published (eg, after triggering "Delete from draft site" in the CMS).
bool
isOnDraftOnly()
Compares current draft with live version, and returns true if no live version exists, meaning the page was never published.
bool
isModifiedOnDraft()
Compares current draft with live version, and returns true if these versions differ, meaning there have been unpublished changes to the draft site.
static DataList
get_including_deleted(string $class, string $filter = "", string $sort = "")
Return the equivalent of a DataList::create() call, querying the latest version of each record stored in the (class)_Versions tables.
In particular, this will query deleted records as well as active ones.
static DataObject
get_version(string $class, int $id, int $version)
Return the specific version of the given id.
Caution: The record is retrieved as a DataObject, but saving back modifications via write() will create a new version, rather than modifying the existing one.
static DataList
get_all_versions(string $class, int $id)
Return a list of all versions for a given id.
onBeforeDuplicate(DataObject $source, bool $doWrite)
Ensure version ID is reset to 0 on duplicate
flushCache()
string
cacheKeyComponent()
Return a piece of text to keep DataObject cache keys appropriately specific.
array
getVersionedStages()
Returns an array of possible stages.
static array
get_template_global_variables()
Called by SSViewer to get a list of global variables to expose to the template, the static method to call on this class to get the value for those variables, and the class to use for casting the returned value for use in a template
If the method to call is not included for a particular template variable, a method named the same as the template variable will be called
If the casting class is not specified for a particular template variable, ViewableData::$default_cast is used
The first letter of the template variable is case-insensitive. However the method name is always case sensitive.
bool
hasStages()
Check if this object has stages
Member|null
Author()
Get author of this record.
Note: Only works on records selected via Versions()
Member|null
Publisher()
Get publisher of this record.
Note: Only works on records selected via Versions()
static mixed
withVersionedMode(callable $callback)
Invoke a callback which may modify reading mode, but ensures this mode is restored after completion, without modifying global state.
The desired reading mode should be set by the callback directly