@@ -474,6 +474,12 @@ protected function augmentSQL(SQLSelect $query, ?DataQuery $dataQuery = null)
474
474
case 'archive ' :
475
475
$ this ->augmentSQLVersionedArchive ($ query , $ dataQuery );
476
476
break ;
477
+ case 'archive_only ' :
478
+ $ this ->augmentSQLVersionedArchiveOnly ($ query , $ dataQuery );
479
+ break ;
480
+ case 'removed_from_draft ' :
481
+ $ this ->augmentSQLVersionedRemovedFromDraft ($ query , $ dataQuery );
482
+ break ;
477
483
case 'latest_version_single ' :
478
484
$ this ->augmentSQLVersionedLatestSingle ($ query , $ dataQuery );
479
485
break ;
@@ -744,6 +750,42 @@ protected function augmentSQLVersionedArchive(SQLSelect $query, DataQuery $dataQ
744
750
);
745
751
}
746
752
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
+
747
789
/**
748
790
* Return latest version instance, regardless of whether it is on a particular stage.
749
791
* 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 =
2479
2521
* @template T of DataObject
2480
2522
* @param class-string<T> $class The name of the class.
2481
2523
* @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.
2485
2524
* @param string $containerClass The container class for the result set (default is DataList)
2486
2525
*
2487
2526
* @return DataList<T> A modified DataList designated to the specified stage
2488
2527
*/
2489
2528
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 {
2498
2536
$ 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 ([
2500
2547
'Versioned.mode ' => 'stage ' ,
2501
2548
'Versioned.stage ' => $ stage
2502
2549
]);
@@ -2784,11 +2831,11 @@ public function isModifiedOnDraft()
2784
2831
*
2785
2832
* @template T of DataObject
2786
2833
* @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!
2789
2836
* @return DataList<T>
2790
2837
*/
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
2792
2839
{
2793
2840
$ list = DataList::create ($ class );
2794
2841
if (!empty ($ filter )) {
@@ -2797,8 +2844,78 @@ public static function get_including_deleted($class, $filter = "", $sort = "")
2797
2844
if (!empty ($ sort )) {
2798
2845
$ list = $ list ->orderBy ($ sort );
2799
2846
}
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 ' );
2802
2919
}
2803
2920
2804
2921
/**
0 commit comments