Skip to content

Commit 9c07422

Browse files
Merge branch '3.0' into 3
2 parents 2d74ee2 + 9051e86 commit 9c07422

File tree

3 files changed

+472
-18
lines changed

3 files changed

+472
-18
lines changed

src/Versioned.php

+134-17
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,12 @@ protected function augmentSQL(SQLSelect $query, ?DataQuery $dataQuery = null)
474474
case 'archive':
475475
$this->augmentSQLVersionedArchive($query, $dataQuery);
476476
break;
477+
case 'archive_only':
478+
$this->augmentSQLVersionedArchiveOnly($query, $dataQuery);
479+
break;
480+
case 'removed_from_draft':
481+
$this->augmentSQLVersionedRemovedFromDraft($query, $dataQuery);
482+
break;
477483
case 'latest_version_single':
478484
$this->augmentSQLVersionedLatestSingle($query, $dataQuery);
479485
break;
@@ -744,6 +750,42 @@ protected function augmentSQLVersionedArchive(SQLSelect $query, DataQuery $dataQ
744750
);
745751
}
746752

753+
/**
754+
* Only include records which are archived - that is they have been removed from both the draft and live stages.
755+
*/
756+
protected function augmentSQLVersionedArchiveOnly(SQLSelect $query, DataQuery $dataQuery): void
757+
{
758+
$baseTable = $this->baseTable();
759+
$liveTable = $this->stageTable($baseTable, Versioned::LIVE);
760+
761+
$query->addLeftJoin(
762+
$liveTable,
763+
"\"{$baseTable}\".\"ID\" = \"{$liveTable}\".\"ID\""
764+
);
765+
$query->addWhere("\"{$liveTable}\".\"ID\" IS NULL");
766+
767+
$this->augmentSQLVersionedRemovedFromDraft($query, $dataQuery);
768+
}
769+
770+
/**
771+
* Only include records which are removed from draft - these might be archived but might also still be in the live stage.
772+
*/
773+
protected function augmentSQLVersionedRemovedFromDraft(SQLSelect $query, DataQuery $dataQuery): void
774+
{
775+
$baseTable = $this->baseTable();
776+
777+
// Join a temporary alias BaseTable_Draft, renaming this on execution to BaseTable
778+
// See Versioned::augmentSQLVersioned() For reference on this alias
779+
$query->addLeftJoin(
780+
"{$baseTable}_Draft",
781+
"\"{$baseTable}\".\"ID\" = \"{$baseTable}_Draft\".\"ID\""
782+
);
783+
784+
$query->addWhere("\"{$baseTable}_Draft\".\"ID\" IS NULL");
785+
786+
$this->augmentSQLVersionedLatest($query, $dataQuery);
787+
}
788+
747789
/**
748790
* Return latest version instance, regardless of whether it is on a particular stage.
749791
* This is similar to augmentSQLVersionedLatest() below, except it only returns a single value
@@ -2479,24 +2521,29 @@ public static function prepopulate_versionnumber_cache($class, $stage, $idList =
24792521
* @template T of DataObject
24802522
* @param class-string<T> $class The name of the class.
24812523
* @param string $stage The name of the stage.
2482-
* @param string $filter A filter to be inserted into the WHERE clause.
2483-
* @param string $sort A sort expression to be inserted into the ORDER BY clause.
2484-
* @param int $limit A limit on the number of records returned from the database.
24852524
* @param string $containerClass The container class for the result set (default is DataList)
24862525
*
24872526
* @return DataList<T> A modified DataList designated to the specified stage
24882527
*/
24892528
public static function get_by_stage(
2490-
$class,
2491-
$stage,
2492-
$filter = '',
2493-
$sort = '',
2494-
$limit = null,
2495-
$containerClass = DataList::class
2496-
) {
2497-
ReadingMode::validateStage($stage);
2529+
string $class,
2530+
string $stage,
2531+
string|array $filter = '',
2532+
string|array|null $sort = '',
2533+
string|array|null $limit = null,
2534+
string $containerClass = DataList::class
2535+
): DataList {
24982536
$result = DataObject::get($class, $filter, $sort, $limit, $containerClass);
2499-
return $result->setDataQueryParam([
2537+
return static::updateListToAlsoIncludeStage($result, $stage);
2538+
}
2539+
2540+
/**
2541+
* Update an existing DataList to include records for a given stage.
2542+
*/
2543+
public static function updateListToAlsoIncludeStage(DataList $list, string $stage): DataList
2544+
{
2545+
ReadingMode::validateStage($stage);
2546+
return $list->setDataQueryParam([
25002547
'Versioned.mode' => 'stage',
25012548
'Versioned.stage' => $stage
25022549
]);
@@ -2784,11 +2831,11 @@ public function isModifiedOnDraft()
27842831
*
27852832
* @template T of DataObject
27862833
* @param class-string<T> $class
2787-
* @param string $filter
2788-
* @param string $sort
2834+
* @param string $filter Explicitly used in where clause as raw SQL - use with caution!
2835+
* @param string $sort Explicitly used in orderBy clause as raw SQL - use with caution!
27892836
* @return DataList<T>
27902837
*/
2791-
public static function get_including_deleted($class, $filter = "", $sort = "")
2838+
public static function get_including_deleted(string $class, string|array $filter = '', string $sort = ''): DataList
27922839
{
27932840
$list = DataList::create($class);
27942841
if (!empty($filter)) {
@@ -2797,8 +2844,78 @@ public static function get_including_deleted($class, $filter = "", $sort = "")
27972844
if (!empty($sort)) {
27982845
$list = $list->orderBy($sort);
27992846
}
2800-
$list = $list->setDataQueryParam("Versioned.mode", "latest_versions");
2801-
return $list;
2847+
return static::updateListToAlsoIncludeDeleted($list);
2848+
}
2849+
2850+
/**
2851+
* Update a DataList to query the latest version of each record stored in the (class)_Versions tables.
2852+
*
2853+
* In particular, this will query deleted records as well as active ones.
2854+
*/
2855+
public static function updateListToAlsoIncludeDeleted(DataList $list): DataList
2856+
{
2857+
return $list->setDataQueryParam('Versioned.mode', 'latest_versions');
2858+
}
2859+
2860+
/**
2861+
* Return the equivalent of a DataList::create() call, querying only records which have been removed
2862+
* from draft. This includes archived records and records which were published but have no draft record.
2863+
*
2864+
* @template T of DataObject
2865+
* @param class-string<T> $class
2866+
* @param string $where Explicitly used in where clause as raw SQL - use with caution!
2867+
* @param string $orderBy Explicitly used in orderBy clause as raw SQL - use with caution!
2868+
* @return DataList<T>
2869+
*/
2870+
public static function getRemovedFromDraft(string $class, string|array $where = '', string $orderBy = ''): DataList
2871+
{
2872+
$list = DataList::create($class);
2873+
if (!empty($where)) {
2874+
$list = $list->where($where);
2875+
}
2876+
if (!empty($orderBy)) {
2877+
$list = $list->orderBy($orderBy);
2878+
}
2879+
return static::updateListToOnlyIncludeRemovedFromDraft($list);
2880+
}
2881+
2882+
/**
2883+
* Update a DataList to query only records which have been removed from draft.
2884+
* This includes archived records and records which were published but have no draft record.
2885+
*/
2886+
public static function updateListToOnlyIncludeRemovedFromDraft(DataList $list): DataList
2887+
{
2888+
return $list->setDataQueryParam('Versioned.mode', 'removed_from_draft');
2889+
}
2890+
2891+
/**
2892+
* Return the equivalent of a DataList::create() call, querying only records which are archived
2893+
* (i.e. removed in both draft and live)
2894+
*
2895+
* @template T of DataObject
2896+
* @param class-string<T> $class
2897+
* @param string $where Explicitly used in where clause as raw SQL - use with caution!
2898+
* @param string $orderBy Explicitly used in orderBy clause as raw SQL - use with caution!
2899+
* @return DataList<T>
2900+
*/
2901+
public static function getArchivedOnly(string $class, string|array $where = '', string $orderBy = ''): DataList
2902+
{
2903+
$list = DataList::create($class);
2904+
if (!empty($where)) {
2905+
$list = $list->where($where);
2906+
}
2907+
if (!empty($orderBy)) {
2908+
$list = $list->orderBy($orderBy);
2909+
}
2910+
return static::updateListToOnlyIncludeArchived($list);
2911+
}
2912+
2913+
/**
2914+
* Update a DataList to query only records which are archived (i.e. removed in both draft and live)
2915+
*/
2916+
public static function updateListToOnlyIncludeArchived(DataList $list): DataList
2917+
{
2918+
return $list->setDataQueryParam('Versioned.mode', 'archive_only');
28022919
}
28032920

28042921
/**

0 commit comments

Comments
 (0)