Complex classes like Versioned often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Versioned, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Versioned extends DataExtension implements TemplateGlobalProvider { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Versioning mode for this object. |
||
| 20 | * Note: Not related to the current versioning mode in the state / session |
||
| 21 | * Will be one of 'StagedVersioned' or 'Versioned'; |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $mode; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The default reading mode |
||
| 29 | */ |
||
| 30 | const DEFAULT_MODE = 'Stage.Live'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Constructor arg to specify that staging is active on this record. |
||
| 34 | * 'Staging' implies that 'Versioning' is also enabled. |
||
| 35 | */ |
||
| 36 | const STAGEDVERSIONED = 'StagedVersioned'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Constructor arg to specify that versioning only is active on this record. |
||
| 40 | */ |
||
| 41 | const VERSIONED = 'Versioned'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The Public stage. |
||
| 45 | */ |
||
| 46 | const LIVE = 'Live'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The draft (default) stage |
||
| 50 | */ |
||
| 51 | const DRAFT = 'Stage'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * A version that a DataObject should be when it is 'migrating', |
||
| 55 | * that is, when it is in the process of moving from one stage to another. |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $migratingVersion; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A cache used by get_versionnumber_by_stage(). |
||
| 62 | * Clear through {@link flushCache()}. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected static $cache_versionnumber; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Current reading mode |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected static $reading_mode = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var Boolean Flag which is temporarily changed during the write() process |
||
| 77 | * to influence augmentWrite() behaviour. If set to TRUE, no new version will be created |
||
| 78 | * for the following write. Needs to be public as other classes introspect this state |
||
| 79 | * during the write process in order to adapt to this versioning behaviour. |
||
| 80 | */ |
||
| 81 | public $_nextWriteWithoutVersion = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Additional database columns for the new |
||
| 85 | * "_versions" table. Used in {@link augmentDatabase()} |
||
| 86 | * and all Versioned calls extending or creating |
||
| 87 | * SELECT statements. |
||
| 88 | * |
||
| 89 | * @var array $db_for_versions_table |
||
| 90 | */ |
||
| 91 | private static $db_for_versions_table = array( |
||
| 92 | "RecordID" => "Int", |
||
| 93 | "Version" => "Int", |
||
| 94 | "WasPublished" => "Boolean", |
||
| 95 | "AuthorID" => "Int", |
||
| 96 | "PublisherID" => "Int" |
||
| 97 | ); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | private static $db = array( |
||
| 103 | 'Version' => 'Int' |
||
| 104 | ); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Used to enable or disable the prepopulation of the version number cache. |
||
| 108 | * Defaults to true. |
||
| 109 | * |
||
| 110 | * @config |
||
| 111 | * @var boolean |
||
| 112 | */ |
||
| 113 | private static $prepopulate_versionnumber_cache = true; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Additional database indexes for the new |
||
| 117 | * "_versions" table. Used in {@link augmentDatabase()}. |
||
| 118 | * |
||
| 119 | * @var array $indexes_for_versions_table |
||
| 120 | */ |
||
| 121 | private static $indexes_for_versions_table = array( |
||
| 122 | 'RecordID_Version' => '("RecordID","Version")', |
||
| 123 | 'RecordID' => true, |
||
| 124 | 'Version' => true, |
||
| 125 | 'AuthorID' => true, |
||
| 126 | 'PublisherID' => true, |
||
| 127 | ); |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * An array of DataObject extensions that may require versioning for extra tables |
||
| 132 | * The array value is a set of suffixes to form these table names, assuming a preceding '_'. |
||
| 133 | * E.g. if Extension1 creates a new table 'Class_suffix1' |
||
| 134 | * and Extension2 the tables 'Class_suffix2' and 'Class_suffix3': |
||
| 135 | * |
||
| 136 | * $versionableExtensions = array( |
||
| 137 | * 'Extension1' => 'suffix1', |
||
| 138 | * 'Extension2' => array('suffix2', 'suffix3'), |
||
| 139 | * ); |
||
| 140 | * |
||
| 141 | * This can also be manipulated by updating the current loaded config |
||
| 142 | * |
||
| 143 | * SiteTree: |
||
| 144 | * versionableExtensions: |
||
| 145 | * - Extension1: |
||
| 146 | * - suffix1 |
||
| 147 | * - suffix2 |
||
| 148 | * - Extension2: |
||
| 149 | * - suffix1 |
||
| 150 | * - suffix2 |
||
| 151 | * |
||
| 152 | * or programatically: |
||
| 153 | * |
||
| 154 | * Config::inst()->update($this->owner->class, 'versionableExtensions', |
||
| 155 | * array('Extension1' => 'suffix1', 'Extension2' => array('suffix2', 'suffix3'))); |
||
| 156 | * |
||
| 157 | * |
||
| 158 | * Your extension must implement VersionableExtension interface in order to |
||
| 159 | * apply custom tables for versioned. |
||
| 160 | * |
||
| 161 | * @config |
||
| 162 | * @var array |
||
| 163 | */ |
||
| 164 | private static $versionableExtensions = []; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Permissions necessary to view records outside of the live stage (e.g. archive / draft stage). |
||
| 168 | * |
||
| 169 | * @config |
||
| 170 | * @var array |
||
| 171 | */ |
||
| 172 | private static $non_live_permissions = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_CMSMain', 'VIEW_DRAFT_CONTENT'); |
||
| 173 | |||
| 174 | /** |
||
| 175 | * List of relationships on this object that are "owned" by this object. |
||
| 176 | * Owership in the context of versioned objects is a relationship where |
||
| 177 | * the publishing of owning objects requires the publishing of owned objects. |
||
| 178 | * |
||
| 179 | * E.g. A page owns a set of banners, as in order for the page to be published, all |
||
| 180 | * banners on this page must also be published for it to be visible. |
||
| 181 | * |
||
| 182 | * Typically any object and its owned objects should be visible in the same edit view. |
||
| 183 | * E.g. a page and {@see GridField} of banners. |
||
| 184 | * |
||
| 185 | * Page hierarchy is typically not considered an ownership relationship. |
||
| 186 | * |
||
| 187 | * Ownership is recursive; If A owns B and B owns C then A owns C. |
||
| 188 | * |
||
| 189 | * @config |
||
| 190 | * @var array List of has_many or many_many relationships owned by this object. |
||
| 191 | */ |
||
| 192 | private static $owns = array(); |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Opposing relationship to owns config; Represents the objects which |
||
| 196 | * own the current object. |
||
| 197 | * |
||
| 198 | * @var array |
||
| 199 | */ |
||
| 200 | private static $owned_by = array(); |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Reset static configuration variables to their default values. |
||
| 204 | */ |
||
| 205 | public static function reset() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Amend freshly created DataQuery objects with versioned-specific |
||
| 212 | * information. |
||
| 213 | * |
||
| 214 | * @param SQLSelect |
||
| 215 | * @param DataQuery |
||
| 216 | */ |
||
| 217 | public function augmentDataQueryCreation(SQLSelect &$query, DataQuery &$dataQuery) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Construct a new Versioned object. |
||
| 231 | * |
||
| 232 | * @var string $mode One of "StagedVersioned" or "Versioned". |
||
| 233 | */ |
||
| 234 | public function __construct($mode = self::STAGEDVERSIONED) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Cache of version to modified dates for this objects |
||
| 257 | * |
||
| 258 | * @var array |
||
| 259 | */ |
||
| 260 | protected $versionModifiedCache = array(); |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get modified date for the given version |
||
| 264 | * |
||
| 265 | * @param int $version |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | protected function getLastEditedForVersion($version) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Updates query parameters of relations attached to versioned dataobjects |
||
| 295 | * |
||
| 296 | * @param array $params |
||
| 297 | */ |
||
| 298 | public function updateInheritableQueryParams(&$params) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Augment the the SQLSelect that is created by the DataQuery |
||
| 336 | * |
||
| 337 | * See {@see augmentLazyLoadFields} for lazy-loading applied prior to this. |
||
| 338 | * |
||
| 339 | * @param SQLSelect $query |
||
| 340 | * @param DataQuery $dataQuery |
||
| 341 | * @throws InvalidArgumentException |
||
| 342 | */ |
||
| 343 | public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Determine if the given versioned table is a part of the sub-tree of the current dataobject |
||
| 499 | * This helps prevent rewriting of other tables that get joined in, in particular, many_many tables |
||
| 500 | * |
||
| 501 | * @param string $table |
||
| 502 | * @return bool True if this table should be versioned |
||
| 503 | */ |
||
| 504 | protected function isTableVersioned($table) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * For lazy loaded fields requiring extra sql manipulation, ie versioning. |
||
| 529 | * |
||
| 530 | * @param SQLSelect $query |
||
| 531 | * @param DataQuery $dataQuery |
||
| 532 | * @param DataObject $dataObject |
||
| 533 | */ |
||
| 534 | public function augmentLoadLazyFields(SQLSelect &$query, DataQuery &$dataQuery = null, $dataObject) { |
||
| 554 | |||
| 555 | public function augmentDatabase() { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Cleanup orphaned records in the _versions table |
||
| 664 | * |
||
| 665 | * @param string $baseTable base table to use as authoritative source of records |
||
| 666 | * @param string $childTable Sub-table to clean orphans from |
||
| 667 | */ |
||
| 668 | protected function cleanupVersionedOrphans($baseTable, $childTable) { |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Helper for augmentDatabase() to find unique indexes and convert them to non-unique |
||
| 707 | * |
||
| 708 | * @param array $indexes The indexes to convert |
||
| 709 | * @return array $indexes |
||
| 710 | */ |
||
| 711 | private function uniqueToIndex($indexes) { |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Generates a ($table)_version DB manipulation and injects it into the current $manipulation |
||
| 736 | * |
||
| 737 | * @param array $manipulation Source manipulation data |
||
| 738 | * @param string $class Class |
||
| 739 | * @param string $table Table Table for this class |
||
| 740 | * @param int $recordID ID of record to version |
||
| 741 | */ |
||
| 742 | protected function augmentWriteVersioned(&$manipulation, $class, $table, $recordID) { |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Rewrite the given manipulation to update the selected (non-default) stage |
||
| 800 | * |
||
| 801 | * @param array $manipulation Source manipulation data |
||
| 802 | * @param string $table Name of table |
||
| 803 | * @param int $recordID ID of record to version |
||
| 804 | */ |
||
| 805 | protected function augmentWriteStaged(&$manipulation, $table, $recordID) { |
||
| 818 | |||
| 819 | |||
| 820 | public function augmentWrite(&$manipulation) { |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Perform a write without affecting the version table. |
||
| 892 | * On objects without versioning. |
||
| 893 | * |
||
| 894 | * @return int The ID of the record |
||
| 895 | */ |
||
| 896 | public function writeWithoutVersion() { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * |
||
| 904 | */ |
||
| 905 | public function onAfterWrite() { |
||
| 908 | |||
| 909 | /** |
||
| 910 | * If a write was skipped, then we need to ensure that we don't leave a |
||
| 911 | * migrateVersion() value lying around for the next write. |
||
| 912 | */ |
||
| 913 | public function onAfterSkippedWrite() { |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Find all objects owned by the current object. |
||
| 919 | * Note that objects will only be searched in the same stage as the given record. |
||
| 920 | * |
||
| 921 | * @param bool $recursive True if recursive |
||
| 922 | * @param ArrayList $list Optional list to add items to |
||
| 923 | * @return ArrayList list of objects |
||
| 924 | */ |
||
| 925 | public function findOwned($recursive = true, $list = null) |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Find objects which own this object. |
||
| 933 | * Note that objects will only be searched in the same stage as the given record. |
||
| 934 | * |
||
| 935 | * @param bool $recursive True if recursive |
||
| 936 | * @param ArrayList $list Optional list to add items to |
||
| 937 | * @return ArrayList list of objects |
||
| 938 | */ |
||
| 939 | public function findOwners($recursive = true, $list = null) { |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Find objects which own this object. |
||
| 954 | * Note that objects will only be searched in the same stage as the given record. |
||
| 955 | * |
||
| 956 | * @param bool $recursive True if recursive |
||
| 957 | * @param ArrayList $list List to add items to |
||
| 958 | * @param array $lookup List of reverse lookup rules for owned objects |
||
| 959 | * @return ArrayList list of objects |
||
| 960 | */ |
||
| 961 | public function findOwnersRecursive($recursive, $list, $lookup) { |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Find a list of classes, each of which with a list of methods to invoke |
||
| 996 | * to lookup owners. |
||
| 997 | * |
||
| 998 | * @return array |
||
| 999 | */ |
||
| 1000 | protected function lookupReverseOwners() { |
||
| 1043 | |||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Find objects in the given relationships, merging them into the given list |
||
| 1047 | * |
||
| 1048 | * @param array $source Config property to extract relationships from |
||
| 1049 | * @param bool $recursive True if recursive |
||
| 1050 | * @param ArrayList $list Optional list to add items to |
||
| 1051 | * @return ArrayList The list |
||
| 1052 | */ |
||
| 1053 | public function findRelatedObjects($source, $recursive = true, $list = null) |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Helper method to merge owned/owning items into a list. |
||
| 1097 | * Items already present in the list will be skipped. |
||
| 1098 | * |
||
| 1099 | * @param ArrayList $list Items to merge into |
||
| 1100 | * @param mixed $items List of new items to merge |
||
| 1101 | * @return ArrayList List of all newly added items that did not already exist in $list |
||
| 1102 | */ |
||
| 1103 | protected function mergeRelatedObjects($list, $items) { |
||
| 1128 | |||
| 1129 | /** |
||
| 1130 | * This function should return true if the current user can publish this record. |
||
| 1131 | * It can be overloaded to customise the security model for an application. |
||
| 1132 | * |
||
| 1133 | * Denies permission if any of the following conditions is true: |
||
| 1134 | * - canPublish() on any extension returns false |
||
| 1135 | * - canEdit() returns false |
||
| 1136 | * |
||
| 1137 | * @param Member $member |
||
| 1138 | * @return bool True if the current user can publish this record. |
||
| 1139 | */ |
||
| 1140 | public function canPublish($member = null) { |
||
| 1164 | |||
| 1165 | /** |
||
| 1166 | * Check if the current user can delete this record from live |
||
| 1167 | * |
||
| 1168 | * @param null $member |
||
| 1169 | * @return mixed |
||
| 1170 | */ |
||
| 1171 | public function canUnpublish($member = null) { |
||
| 1195 | |||
| 1196 | /** |
||
| 1197 | * Check if the current user is allowed to archive this record. |
||
| 1198 | * If extended, ensure that both canDelete and canUnpublish are extended also |
||
| 1199 | * |
||
| 1200 | * @param Member $member |
||
| 1201 | * @return bool |
||
| 1202 | */ |
||
| 1203 | public function canArchive($member = null) { |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Check if the user can revert this record to live |
||
| 1239 | * |
||
| 1240 | * @param Member $member |
||
| 1241 | * @return bool |
||
| 1242 | */ |
||
| 1243 | public function canRevertToLive($member = null) { |
||
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Extend permissions to include additional security for objects that are not published to live. |
||
| 1276 | * |
||
| 1277 | * @param Member $member |
||
| 1278 | * @return bool|null |
||
| 1279 | */ |
||
| 1280 | public function canView($member = null) { |
||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Determine if there are any additional restrictions on this object for the given reading version. |
||
| 1289 | * |
||
| 1290 | * Override this in a subclass to customise any additional effect that Versioned applies to canView. |
||
| 1291 | * |
||
| 1292 | * This is expected to be called by canView, and thus is only responsible for denying access if |
||
| 1293 | * the default canView would otherwise ALLOW access. Thus it should not be called in isolation |
||
| 1294 | * as an authoritative permission check. |
||
| 1295 | * |
||
| 1296 | * This has the following extension points: |
||
| 1297 | * - canViewDraft is invoked if Mode = stage and Stage = stage |
||
| 1298 | * - canViewArchived is invoked if Mode = archive |
||
| 1299 | * |
||
| 1300 | * @param Member $member |
||
| 1301 | * @return bool False is returned if the current viewing mode denies visibility |
||
| 1302 | */ |
||
| 1303 | public function canViewVersioned($member = null) { |
||
| 1341 | |||
| 1342 | /** |
||
| 1343 | * Determines canView permissions for the latest version of this object on a specific stage. |
||
| 1344 | * Usually the stage is read from {@link Versioned::current_stage()}. |
||
| 1345 | * |
||
| 1346 | * This method should be invoked by user code to check if a record is visible in the given stage. |
||
| 1347 | * |
||
| 1348 | * This method should not be called via ->extend('canViewStage'), but rather should be |
||
| 1349 | * overridden in the extended class. |
||
| 1350 | * |
||
| 1351 | * @param string $stage |
||
| 1352 | * @param Member $member |
||
| 1353 | * @return bool |
||
| 1354 | */ |
||
| 1355 | public function canViewStage($stage = 'Live', $member = null) { |
||
| 1365 | |||
| 1366 | /** |
||
| 1367 | * Determine if a class is supporting the Versioned extensions (e.g. |
||
| 1368 | * $table_versions does exists). |
||
| 1369 | * |
||
| 1370 | * @param string $class Class name |
||
| 1371 | * @return boolean |
||
| 1372 | */ |
||
| 1373 | public function canBeVersioned($class) { |
||
| 1378 | |||
| 1379 | /** |
||
| 1380 | * Check if a certain table has the 'Version' field. |
||
| 1381 | * |
||
| 1382 | * @param string $table Table name |
||
| 1383 | * |
||
| 1384 | * @return boolean Returns false if the field isn't in the table, true otherwise |
||
| 1385 | */ |
||
| 1386 | public function hasVersionField($table) { |
||
| 1391 | |||
| 1392 | /** |
||
| 1393 | * @param string $table |
||
| 1394 | * |
||
| 1395 | * @return string |
||
| 1396 | */ |
||
| 1397 | public function extendWithSuffix($table) { |
||
| 1414 | |||
| 1415 | /** |
||
| 1416 | * Get the latest published DataObject. |
||
| 1417 | * |
||
| 1418 | * @return DataObject |
||
| 1419 | */ |
||
| 1420 | public function latestPublished() { |
||
| 1432 | |||
| 1433 | /** |
||
| 1434 | * @deprecated 4.0..5.0 |
||
| 1435 | */ |
||
| 1436 | public function doPublish() { |
||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * Publish this object and all owned objects to Live |
||
| 1443 | * |
||
| 1444 | * @return bool |
||
| 1445 | */ |
||
| 1446 | public function publishRecursive() { |
||
| 1464 | |||
| 1465 | /** |
||
| 1466 | * Publishes this object to Live, but doesn't publish owned objects. |
||
| 1467 | * |
||
| 1468 | * @return bool True if publish was successful |
||
| 1469 | */ |
||
| 1470 | public function publishSingle() { |
||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * Set foreign keys of has_many objects to 0 where those objects were |
||
| 1485 | * disowned as a result of a partial publish / unpublish. |
||
| 1486 | * I.e. this object and its owned objects were recently written to $targetStage, |
||
| 1487 | * but deleted objects were not. |
||
| 1488 | * |
||
| 1489 | * Note that this operation does not create any new Versions |
||
| 1490 | * |
||
| 1491 | * @param string $sourceStage Objects in this stage will not be unlinked. |
||
| 1492 | * @param string $targetStage Objects which exist in this stage but not $sourceStage |
||
| 1493 | * will be unlinked. |
||
| 1494 | */ |
||
| 1495 | public function unlinkDisownedObjects($sourceStage, $targetStage) { |
||
| 1551 | |||
| 1552 | /** |
||
| 1553 | * Removes the record from both live and stage |
||
| 1554 | * |
||
| 1555 | * @return bool Success |
||
| 1556 | */ |
||
| 1557 | public function doArchive() { |
||
| 1570 | |||
| 1571 | /** |
||
| 1572 | * Removes this record from the live site |
||
| 1573 | * |
||
| 1574 | * @return bool Flag whether the unpublish was successful |
||
| 1575 | */ |
||
| 1576 | public function doUnpublish() { |
||
| 1577 | $owner = $this->owner; |
||
| 1578 | if(!$owner->canUnpublish()) { |
||
| 1579 | return false; |
||
| 1580 | } |
||
| 1581 | |||
| 1582 | // Skip if this record isn't saved |
||
| 1583 | if(!$owner->isInDB()) { |
||
| 1584 | return false; |
||
| 1585 | } |
||
| 1586 | |||
| 1587 | // Skip if this record isn't on live |
||
| 1588 | if(!$owner->isPublished()) { |
||
| 1589 | return false; |
||
| 1590 | } |
||
| 1591 | |||
| 1592 | $owner->invokeWithExtensions('onBeforeUnpublish'); |
||
| 1593 | |||
| 1594 | $origReadingMode = static::get_reading_mode(); |
||
| 1595 | static::set_stage(static::LIVE); |
||
| 1596 | |||
| 1597 | // This way our ID won't be unset |
||
| 1598 | $clone = clone $owner; |
||
| 1599 | $clone->delete(); |
||
| 1600 | |||
| 1601 | static::set_reading_mode($origReadingMode); |
||
| 1602 | |||
| 1603 | $owner->invokeWithExtensions('onAfterUnpublish'); |
||
| 1604 | return true; |
||
| 1605 | } |
||
| 1606 | |||
| 1607 | /** |
||
| 1608 | * Trigger unpublish of owning objects |
||
| 1609 | */ |
||
| 1610 | public function onAfterUnpublish() { |
||
| 1611 | $owner = $this->owner; |
||
| 1612 | |||
| 1613 | // Any objects which owned (and thus relied on the unpublished object) are now unpublished automatically. |
||
| 1614 | foreach ($owner->findOwners(false) as $object) { |
||
| 1615 | /** @var Versioned|DataObject $object */ |
||
| 1616 | $object->doUnpublish(); |
||
| 1617 | } |
||
| 1618 | } |
||
| 1619 | |||
| 1620 | |||
| 1621 | /** |
||
| 1622 | * Revert the draft changes: replace the draft content with the content on live |
||
| 1623 | * |
||
| 1624 | * @return bool True if the revert was successful |
||
| 1625 | */ |
||
| 1626 | public function doRevertToLive() { |
||
| 1637 | |||
| 1638 | /** |
||
| 1639 | * Trigger revert of all owned objects to stage |
||
| 1640 | */ |
||
| 1641 | public function onAfterRevertToLive() { |
||
| 1657 | |||
| 1658 | /** |
||
| 1659 | * @deprecated 4.0..5.0 |
||
| 1660 | */ |
||
| 1661 | public function publish($fromStage, $toStage, $createNewVersion = false) { |
||
| 1665 | |||
| 1666 | /** |
||
| 1667 | * Move a database record from one stage to the other. |
||
| 1668 | * |
||
| 1669 | * @param int|string $fromStage Place to copy from. Can be either a stage name or a version number. |
||
| 1670 | * @param string $toStage Place to copy to. Must be a stage name. |
||
| 1671 | * @param bool $createNewVersion Set this to true to create a new version number. |
||
| 1672 | * By default, the existing version number will be copied over. |
||
| 1673 | */ |
||
| 1674 | public function copyVersionToStage($fromStage, $toStage, $createNewVersion = false) { |
||
| 1733 | |||
| 1734 | /** |
||
| 1735 | * Set the migrating version. |
||
| 1736 | * |
||
| 1737 | * @param string $version The version. |
||
| 1738 | */ |
||
| 1739 | public function migrateVersion($version) { |
||
| 1742 | |||
| 1743 | /** |
||
| 1744 | * Compare two stages to see if they're different. |
||
| 1745 | * |
||
| 1746 | * Only checks the version numbers, not the actual content. |
||
| 1747 | * |
||
| 1748 | * @param string $stage1 The first stage to check. |
||
| 1749 | * @param string $stage2 |
||
| 1750 | * @return bool |
||
| 1751 | */ |
||
| 1752 | public function stagesDiffer($stage1, $stage2) { |
||
| 1774 | |||
| 1775 | /** |
||
| 1776 | * @param string $filter |
||
| 1777 | * @param string $sort |
||
| 1778 | * @param string $limit |
||
| 1779 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1780 | * @param string $having |
||
| 1781 | * @return ArrayList |
||
| 1782 | */ |
||
| 1783 | public function Versions($filter = "", $sort = "", $limit = "", $join = "", $having = "") { |
||
| 1786 | |||
| 1787 | /** |
||
| 1788 | * Return a list of all the versions available. |
||
| 1789 | * |
||
| 1790 | * @param string $filter |
||
| 1791 | * @param string $sort |
||
| 1792 | * @param string $limit |
||
| 1793 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1794 | * @param string $having |
||
| 1795 | * @return ArrayList |
||
| 1796 | */ |
||
| 1797 | public function allVersions($filter = "", $sort = "", $limit = "", $join = "", $having = "") { |
||
| 1843 | |||
| 1844 | /** |
||
| 1845 | * Compare two version, and return the diff between them. |
||
| 1846 | * |
||
| 1847 | * @param string $from The version to compare from. |
||
| 1848 | * @param string $to The version to compare to. |
||
| 1849 | * |
||
| 1850 | * @return DataObject |
||
| 1851 | */ |
||
| 1852 | public function compareVersions($from, $to) { |
||
| 1861 | |||
| 1862 | /** |
||
| 1863 | * Return the base table - the class that directly extends DataObject. |
||
| 1864 | * |
||
| 1865 | * Protected so it doesn't conflict with DataObject::baseTable() |
||
| 1866 | * |
||
| 1867 | * @param string $stage |
||
| 1868 | * @return string |
||
| 1869 | */ |
||
| 1870 | protected function baseTable($stage = null) { |
||
| 1874 | |||
| 1875 | /** |
||
| 1876 | * Given a table and stage determine the table name. |
||
| 1877 | * |
||
| 1878 | * Note: Stages this asset does not exist in will default to the draft table. |
||
| 1879 | * |
||
| 1880 | * @param string $table Main table |
||
| 1881 | * @param string $stage |
||
| 1882 | * @return string Staged table name |
||
| 1883 | */ |
||
| 1884 | public function stageTable($table, $stage) { |
||
| 1890 | |||
| 1891 | //-----------------------------------------------------------------------------------------------// |
||
| 1892 | |||
| 1893 | |||
| 1894 | /** |
||
| 1895 | * Determine if the current user is able to set the given site stage / archive |
||
| 1896 | * |
||
| 1897 | * @param SS_HTTPRequest $request |
||
| 1898 | * @return bool |
||
| 1899 | */ |
||
| 1900 | public static function can_choose_site_stage($request) { |
||
| 1913 | |||
| 1914 | /** |
||
| 1915 | * Choose the stage the site is currently on. |
||
| 1916 | * |
||
| 1917 | * If $_GET['stage'] is set, then it will use that stage, and store it in |
||
| 1918 | * the session. |
||
| 1919 | * |
||
| 1920 | * if $_GET['archiveDate'] is set, it will use that date, and store it in |
||
| 1921 | * the session. |
||
| 1922 | * |
||
| 1923 | * If neither of these are set, it checks the session, otherwise the stage |
||
| 1924 | * is set to 'Live'. |
||
| 1925 | */ |
||
| 1926 | public static function choose_site_stage() { |
||
| 1969 | |||
| 1970 | /** |
||
| 1971 | * Set the current reading mode. |
||
| 1972 | * |
||
| 1973 | * @param string $mode |
||
| 1974 | */ |
||
| 1975 | public static function set_reading_mode($mode) { |
||
| 1978 | |||
| 1979 | /** |
||
| 1980 | * Get the current reading mode. |
||
| 1981 | * |
||
| 1982 | * @return string |
||
| 1983 | */ |
||
| 1984 | public static function get_reading_mode() { |
||
| 1987 | |||
| 1988 | /** |
||
| 1989 | * Get the current reading stage. |
||
| 1990 | * |
||
| 1991 | * @return string |
||
| 1992 | */ |
||
| 1993 | public static function get_stage() { |
||
| 2000 | |||
| 2001 | /** |
||
| 2002 | * Get the current archive date. |
||
| 2003 | * |
||
| 2004 | * @return string |
||
| 2005 | */ |
||
| 2006 | public static function current_archived_date() { |
||
| 2012 | |||
| 2013 | /** |
||
| 2014 | * Set the reading stage. |
||
| 2015 | * |
||
| 2016 | * @param string $stage New reading stage. |
||
| 2017 | * @throws InvalidArgumentException |
||
| 2018 | */ |
||
| 2019 | public static function set_stage($stage) { |
||
| 2025 | |||
| 2026 | /** |
||
| 2027 | * Set the reading archive date. |
||
| 2028 | * |
||
| 2029 | * @param string $date New reading archived date. |
||
| 2030 | */ |
||
| 2031 | public static function reading_archived_date($date) { |
||
| 2034 | |||
| 2035 | |||
| 2036 | /** |
||
| 2037 | * Get a singleton instance of a class in the given stage. |
||
| 2038 | * |
||
| 2039 | * @param string $class The name of the class. |
||
| 2040 | * @param string $stage The name of the stage. |
||
| 2041 | * @param string $filter A filter to be inserted into the WHERE clause. |
||
| 2042 | * @param boolean $cache Use caching. |
||
| 2043 | * @param string $sort A sort expression to be inserted into the ORDER BY clause. |
||
| 2044 | * |
||
| 2045 | * @return DataObject |
||
| 2046 | */ |
||
| 2047 | public static function get_one_by_stage($class, $stage, $filter = '', $cache = true, $sort = '') { |
||
| 2053 | |||
| 2054 | /** |
||
| 2055 | * Gets the current version number of a specific record. |
||
| 2056 | * |
||
| 2057 | * @param string $class |
||
| 2058 | * @param string $stage |
||
| 2059 | * @param int $id |
||
| 2060 | * @param boolean $cache |
||
| 2061 | * |
||
| 2062 | * @return int |
||
| 2063 | */ |
||
| 2064 | public static function get_versionnumber_by_stage($class, $stage, $id, $cache = true) { |
||
| 2097 | |||
| 2098 | /** |
||
| 2099 | * Pre-populate the cache for Versioned::get_versionnumber_by_stage() for |
||
| 2100 | * a list of record IDs, for more efficient database querying. If $idList |
||
| 2101 | * is null, then every record will be pre-cached. |
||
| 2102 | * |
||
| 2103 | * @param string $class |
||
| 2104 | * @param string $stage |
||
| 2105 | * @param array $idList |
||
| 2106 | */ |
||
| 2107 | public static function prepopulate_versionnumber_cache($class, $stage, $idList = null) { |
||
| 2137 | |||
| 2138 | /** |
||
| 2139 | * Get a set of class instances by the given stage. |
||
| 2140 | * |
||
| 2141 | * @param string $class The name of the class. |
||
| 2142 | * @param string $stage The name of the stage. |
||
| 2143 | * @param string $filter A filter to be inserted into the WHERE clause. |
||
| 2144 | * @param string $sort A sort expression to be inserted into the ORDER BY clause. |
||
| 2145 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 2146 | * @param int $limit A limit on the number of records returned from the database. |
||
| 2147 | * @param string $containerClass The container class for the result set (default is DataList) |
||
| 2148 | * |
||
| 2149 | * @return DataList A modified DataList designated to the specified stage |
||
| 2150 | */ |
||
| 2151 | public static function get_by_stage( |
||
| 2160 | |||
| 2161 | /** |
||
| 2162 | * Delete this record from the given stage |
||
| 2163 | * |
||
| 2164 | * @param string $stage |
||
| 2165 | */ |
||
| 2166 | public function deleteFromStage($stage) { |
||
| 2178 | |||
| 2179 | /** |
||
| 2180 | * Write the given record to the draft stage |
||
| 2181 | * |
||
| 2182 | * @param string $stage |
||
| 2183 | * @param boolean $forceInsert |
||
| 2184 | * @return int The ID of the record |
||
| 2185 | */ |
||
| 2186 | public function writeToStage($stage, $forceInsert = false) { |
||
| 2197 | |||
| 2198 | /** |
||
| 2199 | * Roll the draft version of this record to match the published record. |
||
| 2200 | * Caution: Doesn't overwrite the object properties with the rolled back version. |
||
| 2201 | * |
||
| 2202 | * {@see doRevertToLive()} to reollback to live |
||
| 2203 | * |
||
| 2204 | * @param int $version Version number |
||
| 2205 | */ |
||
| 2206 | public function doRollbackTo($version) { |
||
| 2213 | |||
| 2214 | public function onAfterRollback($version) { |
||
| 2227 | |||
| 2228 | /** |
||
| 2229 | * Return the latest version of the given record. |
||
| 2230 | * |
||
| 2231 | * @param string $class |
||
| 2232 | * @param int $id |
||
| 2233 | * @return DataObject |
||
| 2234 | */ |
||
| 2235 | public static function get_latest_version($class, $id) { |
||
| 2242 | |||
| 2243 | /** |
||
| 2244 | * Returns whether the current record is the latest one. |
||
| 2245 | * |
||
| 2246 | * @todo Performance - could do this directly via SQL. |
||
| 2247 | * |
||
| 2248 | * @see get_latest_version() |
||
| 2249 | * @see latestPublished |
||
| 2250 | * |
||
| 2251 | * @return boolean |
||
| 2252 | */ |
||
| 2253 | public function isLatestVersion() { |
||
| 2262 | |||
| 2263 | /** |
||
| 2264 | * Check if this record exists on live |
||
| 2265 | * |
||
| 2266 | * @return bool |
||
| 2267 | */ |
||
| 2268 | public function isPublished() { |
||
| 2286 | |||
| 2287 | /** |
||
| 2288 | * Check if this record exists on the draft stage |
||
| 2289 | * |
||
| 2290 | * @return bool |
||
| 2291 | */ |
||
| 2292 | public function isOnDraft() { |
||
| 2305 | |||
| 2306 | |||
| 2307 | |||
| 2308 | /** |
||
| 2309 | * Return the equivalent of a DataList::create() call, querying the latest |
||
| 2310 | * version of each record stored in the (class)_versions tables. |
||
| 2311 | * |
||
| 2312 | * In particular, this will query deleted records as well as active ones. |
||
| 2313 | * |
||
| 2314 | * @param string $class |
||
| 2315 | * @param string $filter |
||
| 2316 | * @param string $sort |
||
| 2317 | * @return DataList |
||
| 2318 | */ |
||
| 2319 | public static function get_including_deleted($class, $filter = "", $sort = "") { |
||
| 2327 | |||
| 2328 | /** |
||
| 2329 | * Return the specific version of the given id. |
||
| 2330 | * |
||
| 2331 | * Caution: The record is retrieved as a DataObject, but saving back |
||
| 2332 | * modifications via write() will create a new version, rather than |
||
| 2333 | * modifying the existing one. |
||
| 2334 | * |
||
| 2335 | * @param string $class |
||
| 2336 | * @param int $id |
||
| 2337 | * @param int $version |
||
| 2338 | * |
||
| 2339 | * @return DataObject |
||
| 2340 | */ |
||
| 2341 | public static function get_version($class, $id, $version) { |
||
| 2351 | |||
| 2352 | /** |
||
| 2353 | * Return a list of all versions for a given id. |
||
| 2354 | * |
||
| 2355 | * @param string $class |
||
| 2356 | * @param int $id |
||
| 2357 | * |
||
| 2358 | * @return DataList |
||
| 2359 | */ |
||
| 2360 | public static function get_all_versions($class, $id) { |
||
| 2367 | |||
| 2368 | /** |
||
| 2369 | * @param array $labels |
||
| 2370 | */ |
||
| 2371 | public function updateFieldLabels(&$labels) { |
||
| 2374 | |||
| 2375 | /** |
||
| 2376 | * @param FieldList |
||
| 2377 | */ |
||
| 2378 | public function updateCMSFields(FieldList $fields) { |
||
| 2383 | |||
| 2384 | /** |
||
| 2385 | * Ensure version ID is reset to 0 on duplicate |
||
| 2386 | * |
||
| 2387 | * @param DataObject $source Record this was duplicated from |
||
| 2388 | * @param bool $doWrite |
||
| 2389 | */ |
||
| 2390 | public function onBeforeDuplicate($source, $doWrite) { |
||
| 2393 | |||
| 2394 | public function flushCache() { |
||
| 2397 | |||
| 2398 | /** |
||
| 2399 | * Return a piece of text to keep DataObject cache keys appropriately specific. |
||
| 2400 | * |
||
| 2401 | * @return string |
||
| 2402 | */ |
||
| 2403 | public function cacheKeyComponent() { |
||
| 2406 | |||
| 2407 | /** |
||
| 2408 | * Returns an array of possible stages. |
||
| 2409 | * |
||
| 2410 | * @return array |
||
| 2411 | */ |
||
| 2412 | public function getVersionedStages() { |
||
| 2419 | |||
| 2420 | public static function get_template_global_variables() { |
||
| 2425 | |||
| 2426 | /** |
||
| 2427 | * Check if this object has stages |
||
| 2428 | * |
||
| 2429 | * @return bool True if this object is staged |
||
| 2430 | */ |
||
| 2431 | public function hasStages() { |
||
| 2434 | } |
||
| 2435 | |||
| 2554 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.