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 |
||
| 36 | class Versioned extends DataExtension implements TemplateGlobalProvider { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Versioning mode for this object. |
||
| 40 | * Note: Not related to the current versioning mode in the state / session |
||
| 41 | * Will be one of 'StagedVersioned' or 'Versioned'; |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $mode; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The default reading mode |
||
| 49 | */ |
||
| 50 | const DEFAULT_MODE = 'Stage.Live'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor arg to specify that staging is active on this record. |
||
| 54 | * 'Staging' implies that 'Versioning' is also enabled. |
||
| 55 | */ |
||
| 56 | const STAGEDVERSIONED = 'StagedVersioned'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Constructor arg to specify that versioning only is active on this record. |
||
| 60 | */ |
||
| 61 | const VERSIONED = 'Versioned'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The Public stage. |
||
| 65 | */ |
||
| 66 | const LIVE = 'Live'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The draft (default) stage |
||
| 70 | */ |
||
| 71 | const DRAFT = 'Stage'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * A version that a DataObject should be when it is 'migrating', |
||
| 75 | * that is, when it is in the process of moving from one stage to another. |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | public $migratingVersion; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * A cache used by get_versionnumber_by_stage(). |
||
| 82 | * Clear through {@link flushCache()}. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected static $cache_versionnumber; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Current reading mode |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected static $reading_mode = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var Boolean Flag which is temporarily changed during the write() process |
||
| 97 | * to influence augmentWrite() behaviour. If set to TRUE, no new version will be created |
||
| 98 | * for the following write. Needs to be public as other classes introspect this state |
||
| 99 | * during the write process in order to adapt to this versioning behaviour. |
||
| 100 | */ |
||
| 101 | public $_nextWriteWithoutVersion = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Additional database columns for the new |
||
| 105 | * "_versions" table. Used in {@link augmentDatabase()} |
||
| 106 | * and all Versioned calls extending or creating |
||
| 107 | * SELECT statements. |
||
| 108 | * |
||
| 109 | * @var array $db_for_versions_table |
||
| 110 | */ |
||
| 111 | private static $db_for_versions_table = array( |
||
| 112 | "RecordID" => "Int", |
||
| 113 | "Version" => "Int", |
||
| 114 | "WasPublished" => "Boolean", |
||
| 115 | "AuthorID" => "Int", |
||
| 116 | "PublisherID" => "Int" |
||
| 117 | ); |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private static $db = array( |
||
| 123 | 'Version' => 'Int' |
||
| 124 | ); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Used to enable or disable the prepopulation of the version number cache. |
||
| 128 | * Defaults to true. |
||
| 129 | * |
||
| 130 | * @config |
||
| 131 | * @var boolean |
||
| 132 | */ |
||
| 133 | private static $prepopulate_versionnumber_cache = true; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Additional database indexes for the new |
||
| 137 | * "_versions" table. Used in {@link augmentDatabase()}. |
||
| 138 | * |
||
| 139 | * @var array $indexes_for_versions_table |
||
| 140 | */ |
||
| 141 | private static $indexes_for_versions_table = array( |
||
| 142 | 'RecordID_Version' => '("RecordID","Version")', |
||
| 143 | 'RecordID' => true, |
||
| 144 | 'Version' => true, |
||
| 145 | 'AuthorID' => true, |
||
| 146 | 'PublisherID' => true, |
||
| 147 | ); |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * An array of DataObject extensions that may require versioning for extra tables |
||
| 152 | * The array value is a set of suffixes to form these table names, assuming a preceding '_'. |
||
| 153 | * E.g. if Extension1 creates a new table 'Class_suffix1' |
||
| 154 | * and Extension2 the tables 'Class_suffix2' and 'Class_suffix3': |
||
| 155 | * |
||
| 156 | * $versionableExtensions = array( |
||
| 157 | * 'Extension1' => 'suffix1', |
||
| 158 | * 'Extension2' => array('suffix2', 'suffix3'), |
||
| 159 | * ); |
||
| 160 | * |
||
| 161 | * This can also be manipulated by updating the current loaded config |
||
| 162 | * |
||
| 163 | * SiteTree: |
||
| 164 | * versionableExtensions: |
||
| 165 | * - Extension1: |
||
| 166 | * - suffix1 |
||
| 167 | * - suffix2 |
||
| 168 | * - Extension2: |
||
| 169 | * - suffix1 |
||
| 170 | * - suffix2 |
||
| 171 | * |
||
| 172 | * or programatically: |
||
| 173 | * |
||
| 174 | * Config::inst()->update($this->owner->class, 'versionableExtensions', |
||
| 175 | * array('Extension1' => 'suffix1', 'Extension2' => array('suffix2', 'suffix3'))); |
||
| 176 | * |
||
| 177 | * |
||
| 178 | * Your extension must implement VersionableExtension interface in order to |
||
| 179 | * apply custom tables for versioned. |
||
| 180 | * |
||
| 181 | * @config |
||
| 182 | * @var array |
||
| 183 | */ |
||
| 184 | private static $versionableExtensions = []; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Permissions necessary to view records outside of the live stage (e.g. archive / draft stage). |
||
| 188 | * |
||
| 189 | * @config |
||
| 190 | * @var array |
||
| 191 | */ |
||
| 192 | private static $non_live_permissions = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_CMSMain', 'VIEW_DRAFT_CONTENT'); |
||
| 193 | |||
| 194 | /** |
||
| 195 | * List of relationships on this object that are "owned" by this object. |
||
| 196 | * Owership in the context of versioned objects is a relationship where |
||
| 197 | * the publishing of owning objects requires the publishing of owned objects. |
||
| 198 | * |
||
| 199 | * E.g. A page owns a set of banners, as in order for the page to be published, all |
||
| 200 | * banners on this page must also be published for it to be visible. |
||
| 201 | * |
||
| 202 | * Typically any object and its owned objects should be visible in the same edit view. |
||
| 203 | * E.g. a page and {@see GridField} of banners. |
||
| 204 | * |
||
| 205 | * Page hierarchy is typically not considered an ownership relationship. |
||
| 206 | * |
||
| 207 | * Ownership is recursive; If A owns B and B owns C then A owns C. |
||
| 208 | * |
||
| 209 | * @config |
||
| 210 | * @var array List of has_many or many_many relationships owned by this object. |
||
| 211 | */ |
||
| 212 | private static $owns = array(); |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Opposing relationship to owns config; Represents the objects which |
||
| 216 | * own the current object. |
||
| 217 | * |
||
| 218 | * @var array |
||
| 219 | */ |
||
| 220 | private static $owned_by = array(); |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Reset static configuration variables to their default values. |
||
| 224 | */ |
||
| 225 | public static function reset() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Amend freshly created DataQuery objects with versioned-specific |
||
| 232 | * information. |
||
| 233 | * |
||
| 234 | * @param SQLSelect $query |
||
| 235 | * @param DataQuery $dataQuery |
||
| 236 | */ |
||
| 237 | public function augmentDataQueryCreation(SQLSelect &$query, DataQuery &$dataQuery) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Construct a new Versioned object. |
||
| 251 | * |
||
| 252 | * @var string $mode One of "StagedVersioned" or "Versioned". |
||
| 253 | */ |
||
| 254 | public function __construct($mode = self::STAGEDVERSIONED) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Cache of version to modified dates for this objects |
||
| 277 | * |
||
| 278 | * @var array |
||
| 279 | */ |
||
| 280 | protected $versionModifiedCache = array(); |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get modified date for the given version |
||
| 284 | * |
||
| 285 | * @param int $version |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | protected function getLastEditedForVersion($version) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Updates query parameters of relations attached to versioned dataobjects |
||
| 315 | * |
||
| 316 | * @param array $params |
||
| 317 | */ |
||
| 318 | public function updateInheritableQueryParams(&$params) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Augment the the SQLSelect that is created by the DataQuery |
||
| 356 | * |
||
| 357 | * See {@see augmentLazyLoadFields} for lazy-loading applied prior to this. |
||
| 358 | * |
||
| 359 | * @param SQLSelect $query |
||
| 360 | * @param DataQuery $dataQuery |
||
| 361 | * @throws InvalidArgumentException |
||
| 362 | */ |
||
| 363 | public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Determine if the given versioned table is a part of the sub-tree of the current dataobject |
||
| 519 | * This helps prevent rewriting of other tables that get joined in, in particular, many_many tables |
||
| 520 | * |
||
| 521 | * @param string $table |
||
| 522 | * @return bool True if this table should be versioned |
||
| 523 | */ |
||
| 524 | protected function isTableVersioned($table) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * For lazy loaded fields requiring extra sql manipulation, ie versioning. |
||
| 549 | * |
||
| 550 | * @param SQLSelect $query |
||
| 551 | * @param DataQuery $dataQuery |
||
| 552 | * @param DataObject $dataObject |
||
| 553 | */ |
||
| 554 | public function augmentLoadLazyFields(SQLSelect &$query, DataQuery &$dataQuery = null, $dataObject) { |
||
| 574 | |||
| 575 | public function augmentDatabase() { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Cleanup orphaned records in the _versions table |
||
| 684 | * |
||
| 685 | * @param string $baseTable base table to use as authoritative source of records |
||
| 686 | * @param string $childTable Sub-table to clean orphans from |
||
| 687 | */ |
||
| 688 | protected function cleanupVersionedOrphans($baseTable, $childTable) { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Helper for augmentDatabase() to find unique indexes and convert them to non-unique |
||
| 727 | * |
||
| 728 | * @param array $indexes The indexes to convert |
||
| 729 | * @return array $indexes |
||
| 730 | */ |
||
| 731 | private function uniqueToIndex($indexes) { |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Generates a ($table)_version DB manipulation and injects it into the current $manipulation |
||
| 756 | * |
||
| 757 | * @param array $manipulation Source manipulation data |
||
| 758 | * @param string $class Class |
||
| 759 | * @param string $table Table Table for this class |
||
| 760 | * @param int $recordID ID of record to version |
||
| 761 | */ |
||
| 762 | protected function augmentWriteVersioned(&$manipulation, $class, $table, $recordID) { |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Rewrite the given manipulation to update the selected (non-default) stage |
||
| 821 | * |
||
| 822 | * @param array $manipulation Source manipulation data |
||
| 823 | * @param string $table Name of table |
||
| 824 | * @param int $recordID ID of record to version |
||
| 825 | */ |
||
| 826 | protected function augmentWriteStaged(&$manipulation, $table, $recordID) { |
||
| 839 | |||
| 840 | |||
| 841 | public function augmentWrite(&$manipulation) { |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Perform a write without affecting the version table. |
||
| 913 | * On objects without versioning. |
||
| 914 | * |
||
| 915 | * @return int The ID of the record |
||
| 916 | */ |
||
| 917 | public function writeWithoutVersion() { |
||
| 922 | |||
| 923 | /** |
||
| 924 | * |
||
| 925 | */ |
||
| 926 | public function onAfterWrite() { |
||
| 929 | |||
| 930 | /** |
||
| 931 | * If a write was skipped, then we need to ensure that we don't leave a |
||
| 932 | * migrateVersion() value lying around for the next write. |
||
| 933 | */ |
||
| 934 | public function onAfterSkippedWrite() { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Find all objects owned by the current object. |
||
| 940 | * Note that objects will only be searched in the same stage as the given record. |
||
| 941 | * |
||
| 942 | * @param bool $recursive True if recursive |
||
| 943 | * @param ArrayList $list Optional list to add items to |
||
| 944 | * @return ArrayList list of objects |
||
| 945 | */ |
||
| 946 | public function findOwned($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 Optional list to add items to |
||
| 958 | * @return ArrayList list of objects |
||
| 959 | */ |
||
| 960 | public function findOwners($recursive = true, $list = null) { |
||
| 972 | |||
| 973 | /** |
||
| 974 | * Find objects which own this object. |
||
| 975 | * Note that objects will only be searched in the same stage as the given record. |
||
| 976 | * |
||
| 977 | * @param bool $recursive True if recursive |
||
| 978 | * @param ArrayList $list List to add items to |
||
| 979 | * @param array $lookup List of reverse lookup rules for owned objects |
||
| 980 | * @return ArrayList list of objects |
||
| 981 | */ |
||
| 982 | public function findOwnersRecursive($recursive, $list, $lookup) { |
||
| 1014 | |||
| 1015 | /** |
||
| 1016 | * Find a list of classes, each of which with a list of methods to invoke |
||
| 1017 | * to lookup owners. |
||
| 1018 | * |
||
| 1019 | * @return array |
||
| 1020 | */ |
||
| 1021 | protected function lookupReverseOwners() { |
||
| 1022 | // Find all classes with 'owns' config |
||
| 1023 | $lookup = array(); |
||
| 1024 | foreach(ClassInfo::subclassesFor('SilverStripe\ORM\DataObject') as $class) { |
||
| 1025 | // Ensure this class is versioned |
||
| 1026 | if(!Object::has_extension($class, 'SilverStripe\ORM\Versioning\Versioned')) { |
||
| 1027 | continue; |
||
| 1028 | } |
||
| 1029 | |||
| 1030 | // Check owned objects for this class |
||
| 1031 | $owns = Config::inst()->get($class, 'owns', Config::UNINHERITED); |
||
| 1032 | if(empty($owns)) { |
||
| 1033 | continue; |
||
| 1034 | } |
||
| 1035 | |||
| 1036 | $instance = DataObject::singleton($class); |
||
| 1037 | foreach($owns as $owned) { |
||
| 1038 | // Find owned class |
||
| 1039 | $ownedClass = $instance->getRelationClass($owned); |
||
| 1040 | // Skip custom methods that don't have db relationsm |
||
| 1041 | if(!$ownedClass) { |
||
| 1042 | continue; |
||
| 1043 | } |
||
| 1044 | if($ownedClass === 'SilverStripe\ORM\DataObject') { |
||
| 1045 | throw new LogicException(sprintf( |
||
| 1046 | "Relation %s on class %s cannot be owned as it is polymorphic", |
||
| 1047 | $owned, $class |
||
| 1048 | )); |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | // Add lookup for owned class |
||
| 1052 | if(!isset($lookup[$ownedClass])) { |
||
| 1053 | $lookup[$ownedClass] = array(); |
||
| 1054 | } |
||
| 1055 | $lookup[$ownedClass][] = [ |
||
| 1056 | 'class' => $class, |
||
| 1057 | 'relation' => $owned |
||
| 1058 | ]; |
||
| 1059 | } |
||
| 1060 | } |
||
| 1061 | return $lookup; |
||
| 1062 | } |
||
| 1063 | |||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Find objects in the given relationships, merging them into the given list |
||
| 1067 | * |
||
| 1068 | * @param array $source Config property to extract relationships from |
||
| 1069 | * @param bool $recursive True if recursive |
||
| 1070 | * @param ArrayList $list Optional list to add items to |
||
| 1071 | * @return ArrayList The list |
||
| 1072 | */ |
||
| 1073 | public function findRelatedObjects($source, $recursive = true, $list = null) |
||
| 1114 | |||
| 1115 | /** |
||
| 1116 | * Helper method to merge owned/owning items into a list. |
||
| 1117 | * Items already present in the list will be skipped. |
||
| 1118 | * |
||
| 1119 | * @param ArrayList $list Items to merge into |
||
| 1120 | * @param mixed $items List of new items to merge |
||
| 1121 | * @return ArrayList List of all newly added items that did not already exist in $list |
||
| 1122 | */ |
||
| 1123 | protected function mergeRelatedObjects($list, $items) { |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * This function should return true if the current user can publish this record. |
||
| 1151 | * It can be overloaded to customise the security model for an application. |
||
| 1152 | * |
||
| 1153 | * Denies permission if any of the following conditions is true: |
||
| 1154 | * - canPublish() on any extension returns false |
||
| 1155 | * - canEdit() returns false |
||
| 1156 | * |
||
| 1157 | * @param Member $member |
||
| 1158 | * @return bool True if the current user can publish this record. |
||
| 1159 | */ |
||
| 1160 | public function canPublish($member = null) { |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Check if the current user can delete this record from live |
||
| 1187 | * |
||
| 1188 | * @param null $member |
||
| 1189 | * @return mixed |
||
| 1190 | */ |
||
| 1191 | public function canUnpublish($member = null) { |
||
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Check if the current user is allowed to archive this record. |
||
| 1218 | * If extended, ensure that both canDelete and canUnpublish are extended also |
||
| 1219 | * |
||
| 1220 | * @param Member $member |
||
| 1221 | * @return bool |
||
| 1222 | */ |
||
| 1223 | public function canArchive($member = null) { |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * Check if the user can revert this record to live |
||
| 1259 | * |
||
| 1260 | * @param Member $member |
||
| 1261 | * @return bool |
||
| 1262 | */ |
||
| 1263 | public function canRevertToLive($member = null) { |
||
| 1293 | |||
| 1294 | /** |
||
| 1295 | * Extend permissions to include additional security for objects that are not published to live. |
||
| 1296 | * |
||
| 1297 | * @param Member $member |
||
| 1298 | * @return bool|null |
||
| 1299 | */ |
||
| 1300 | public function canView($member = null) { |
||
| 1301 | // Invoke default version-gnostic canView |
||
| 1302 | if ($this->owner->canViewVersioned($member) === false) { |
||
| 1303 | return false; |
||
| 1304 | } |
||
| 1305 | return null; |
||
| 1306 | } |
||
| 1307 | |||
| 1308 | /** |
||
| 1309 | * Determine if there are any additional restrictions on this object for the given reading version. |
||
| 1310 | * |
||
| 1311 | * Override this in a subclass to customise any additional effect that Versioned applies to canView. |
||
| 1312 | * |
||
| 1313 | * This is expected to be called by canView, and thus is only responsible for denying access if |
||
| 1314 | * the default canView would otherwise ALLOW access. Thus it should not be called in isolation |
||
| 1315 | * as an authoritative permission check. |
||
| 1316 | * |
||
| 1317 | * This has the following extension points: |
||
| 1318 | * - canViewDraft is invoked if Mode = stage and Stage = stage |
||
| 1319 | * - canViewArchived is invoked if Mode = archive |
||
| 1320 | * |
||
| 1321 | * @param Member $member |
||
| 1322 | * @return bool False is returned if the current viewing mode denies visibility |
||
| 1323 | */ |
||
| 1324 | public function canViewVersioned($member = null) { |
||
| 1362 | |||
| 1363 | /** |
||
| 1364 | * Determines canView permissions for the latest version of this object on a specific stage. |
||
| 1365 | * Usually the stage is read from {@link Versioned::current_stage()}. |
||
| 1366 | * |
||
| 1367 | * This method should be invoked by user code to check if a record is visible in the given stage. |
||
| 1368 | * |
||
| 1369 | * This method should not be called via ->extend('canViewStage'), but rather should be |
||
| 1370 | * overridden in the extended class. |
||
| 1371 | * |
||
| 1372 | * @param string $stage |
||
| 1373 | * @param Member $member |
||
| 1374 | * @return bool |
||
| 1375 | */ |
||
| 1376 | public function canViewStage($stage = 'Live', $member = null) { |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Determine if a class is supporting the Versioned extensions (e.g. |
||
| 1389 | * $table_versions does exists). |
||
| 1390 | * |
||
| 1391 | * @param string $class Class name |
||
| 1392 | * @return boolean |
||
| 1393 | */ |
||
| 1394 | public function canBeVersioned($class) { |
||
| 1399 | |||
| 1400 | /** |
||
| 1401 | * Check if a certain table has the 'Version' field. |
||
| 1402 | * |
||
| 1403 | * @param string $table Table name |
||
| 1404 | * |
||
| 1405 | * @return boolean Returns false if the field isn't in the table, true otherwise |
||
| 1406 | */ |
||
| 1407 | public function hasVersionField($table) { |
||
| 1412 | |||
| 1413 | /** |
||
| 1414 | * @param string $table |
||
| 1415 | * |
||
| 1416 | * @return string |
||
| 1417 | */ |
||
| 1418 | public function extendWithSuffix($table) { |
||
| 1435 | |||
| 1436 | /** |
||
| 1437 | * Determines if the current draft version is the same as live |
||
| 1438 | * |
||
| 1439 | * @return bool |
||
| 1440 | */ |
||
| 1441 | public function latestPublished() { |
||
| 1453 | |||
| 1454 | /** |
||
| 1455 | * @deprecated 4.0..5.0 |
||
| 1456 | */ |
||
| 1457 | public function doPublish() { |
||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Publish this object and all owned objects to Live |
||
| 1464 | * |
||
| 1465 | * @return bool |
||
| 1466 | */ |
||
| 1467 | public function publishRecursive() { |
||
| 1485 | |||
| 1486 | /** |
||
| 1487 | * Publishes this object to Live, but doesn't publish owned objects. |
||
| 1488 | * |
||
| 1489 | * @return bool True if publish was successful |
||
| 1490 | */ |
||
| 1491 | public function publishSingle() { |
||
| 1503 | |||
| 1504 | /** |
||
| 1505 | * Set foreign keys of has_many objects to 0 where those objects were |
||
| 1506 | * disowned as a result of a partial publish / unpublish. |
||
| 1507 | * I.e. this object and its owned objects were recently written to $targetStage, |
||
| 1508 | * but deleted objects were not. |
||
| 1509 | * |
||
| 1510 | * Note that this operation does not create any new Versions |
||
| 1511 | * |
||
| 1512 | * @param string $sourceStage Objects in this stage will not be unlinked. |
||
| 1513 | * @param string $targetStage Objects which exist in this stage but not $sourceStage |
||
| 1514 | * will be unlinked. |
||
| 1515 | */ |
||
| 1516 | public function unlinkDisownedObjects($sourceStage, $targetStage) { |
||
| 1572 | |||
| 1573 | /** |
||
| 1574 | * Removes the record from both live and stage |
||
| 1575 | * |
||
| 1576 | * @return bool Success |
||
| 1577 | */ |
||
| 1578 | public function doArchive() { |
||
| 1591 | |||
| 1592 | /** |
||
| 1593 | * Removes this record from the live site |
||
| 1594 | * |
||
| 1595 | * @return bool Flag whether the unpublish was successful |
||
| 1596 | */ |
||
| 1597 | public function doUnpublish() { |
||
| 1627 | |||
| 1628 | /** |
||
| 1629 | * Trigger unpublish of owning objects |
||
| 1630 | */ |
||
| 1631 | public function onAfterUnpublish() { |
||
| 1640 | |||
| 1641 | |||
| 1642 | /** |
||
| 1643 | * Revert the draft changes: replace the draft content with the content on live |
||
| 1644 | * |
||
| 1645 | * @return bool True if the revert was successful |
||
| 1646 | */ |
||
| 1647 | public function doRevertToLive() { |
||
| 1658 | |||
| 1659 | /** |
||
| 1660 | * Trigger revert of all owned objects to stage |
||
| 1661 | */ |
||
| 1662 | public function onAfterRevertToLive() { |
||
| 1678 | |||
| 1679 | /** |
||
| 1680 | * @deprecated 4.0..5.0 |
||
| 1681 | */ |
||
| 1682 | public function publish($fromStage, $toStage, $createNewVersion = false) { |
||
| 1686 | |||
| 1687 | /** |
||
| 1688 | * Move a database record from one stage to the other. |
||
| 1689 | * |
||
| 1690 | * @param int|string $fromStage Place to copy from. Can be either a stage name or a version number. |
||
| 1691 | * @param string $toStage Place to copy to. Must be a stage name. |
||
| 1692 | * @param bool $createNewVersion Set this to true to create a new version number. |
||
| 1693 | * By default, the existing version number will be copied over. |
||
| 1694 | */ |
||
| 1695 | public function copyVersionToStage($fromStage, $toStage, $createNewVersion = false) { |
||
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Set the migrating version. |
||
| 1758 | * |
||
| 1759 | * @param string $version The version. |
||
| 1760 | */ |
||
| 1761 | public function migrateVersion($version) { |
||
| 1764 | |||
| 1765 | /** |
||
| 1766 | * Compare two stages to see if they're different. |
||
| 1767 | * |
||
| 1768 | * Only checks the version numbers, not the actual content. |
||
| 1769 | * |
||
| 1770 | * @param string $stage1 The first stage to check. |
||
| 1771 | * @param string $stage2 |
||
| 1772 | * @return bool |
||
| 1773 | */ |
||
| 1774 | public function stagesDiffer($stage1, $stage2) { |
||
| 1775 | $table1 = $this->baseTable($stage1); |
||
| 1776 | $table2 = $this->baseTable($stage2); |
||
| 1777 | $id = $this->owner->ID ?: $this->owner->OldID; |
||
| 1778 | if (!$id) { |
||
| 1779 | return true; |
||
| 1780 | } |
||
| 1781 | |||
| 1782 | // We test for equality - if one of the versions doesn't exist, this |
||
| 1783 | // will be false. |
||
| 1784 | |||
| 1785 | // TODO: DB Abstraction: if statement here: |
||
| 1786 | $stagesAreEqual = DB::prepared_query( |
||
| 1787 | "SELECT CASE WHEN \"$table1\".\"Version\"=\"$table2\".\"Version\" THEN 1 ELSE 0 END |
||
| 1788 | FROM \"$table1\" INNER JOIN \"$table2\" ON \"$table1\".\"ID\" = \"$table2\".\"ID\" |
||
| 1789 | AND \"$table1\".\"ID\" = ?", |
||
| 1790 | array($id) |
||
| 1791 | )->value(); |
||
| 1792 | |||
| 1793 | return !$stagesAreEqual; |
||
| 1794 | } |
||
| 1795 | |||
| 1796 | /** |
||
| 1797 | * @param string $filter |
||
| 1798 | * @param string $sort |
||
| 1799 | * @param string $limit |
||
| 1800 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1801 | * @param string $having |
||
| 1802 | * @return ArrayList |
||
| 1803 | */ |
||
| 1804 | public function Versions($filter = "", $sort = "", $limit = "", $join = "", $having = "") { |
||
| 1807 | |||
| 1808 | /** |
||
| 1809 | * Return a list of all the versions available. |
||
| 1810 | * |
||
| 1811 | * @param string $filter |
||
| 1812 | * @param string $sort |
||
| 1813 | * @param string $limit |
||
| 1814 | * @param string $join @deprecated use leftJoin($table, $joinClause) instead |
||
| 1815 | * @param string $having @deprecated |
||
| 1816 | * @return ArrayList |
||
| 1817 | */ |
||
| 1818 | public function allVersions($filter = "", $sort = "", $limit = "", $join = "", $having = "") { |
||
| 1866 | |||
| 1867 | /** |
||
| 1868 | * Compare two version, and return the diff between them. |
||
| 1869 | * |
||
| 1870 | * @param string $from The version to compare from. |
||
| 1871 | * @param string $to The version to compare to. |
||
| 1872 | * |
||
| 1873 | * @return DataObject |
||
| 1874 | */ |
||
| 1875 | public function compareVersions($from, $to) { |
||
| 1884 | |||
| 1885 | /** |
||
| 1886 | * Return the base table - the class that directly extends DataObject. |
||
| 1887 | * |
||
| 1888 | * Protected so it doesn't conflict with DataObject::baseTable() |
||
| 1889 | * |
||
| 1890 | * @param string $stage |
||
| 1891 | * @return string |
||
| 1892 | */ |
||
| 1893 | protected function baseTable($stage = null) { |
||
| 1897 | |||
| 1898 | /** |
||
| 1899 | * Given a table and stage determine the table name. |
||
| 1900 | * |
||
| 1901 | * Note: Stages this asset does not exist in will default to the draft table. |
||
| 1902 | * |
||
| 1903 | * @param string $table Main table |
||
| 1904 | * @param string $stage |
||
| 1905 | * @return string Staged table name |
||
| 1906 | */ |
||
| 1907 | public function stageTable($table, $stage) { |
||
| 1913 | |||
| 1914 | //-----------------------------------------------------------------------------------------------// |
||
| 1915 | |||
| 1916 | |||
| 1917 | /** |
||
| 1918 | * Determine if the current user is able to set the given site stage / archive |
||
| 1919 | * |
||
| 1920 | * @param HTTPRequest $request |
||
| 1921 | * @return bool |
||
| 1922 | */ |
||
| 1923 | public static function can_choose_site_stage($request) { |
||
| 1936 | |||
| 1937 | /** |
||
| 1938 | * Choose the stage the site is currently on. |
||
| 1939 | * |
||
| 1940 | * If $_GET['stage'] is set, then it will use that stage, and store it in |
||
| 1941 | * the session. |
||
| 1942 | * |
||
| 1943 | * if $_GET['archiveDate'] is set, it will use that date, and store it in |
||
| 1944 | * the session. |
||
| 1945 | * |
||
| 1946 | * If neither of these are set, it checks the session, otherwise the stage |
||
| 1947 | * is set to 'Live'. |
||
| 1948 | */ |
||
| 1949 | public static function choose_site_stage() { |
||
| 1992 | |||
| 1993 | /** |
||
| 1994 | * Set the current reading mode. |
||
| 1995 | * |
||
| 1996 | * @param string $mode |
||
| 1997 | */ |
||
| 1998 | public static function set_reading_mode($mode) { |
||
| 2001 | |||
| 2002 | /** |
||
| 2003 | * Get the current reading mode. |
||
| 2004 | * |
||
| 2005 | * @return string |
||
| 2006 | */ |
||
| 2007 | public static function get_reading_mode() { |
||
| 2010 | |||
| 2011 | /** |
||
| 2012 | * Get the current reading stage. |
||
| 2013 | * |
||
| 2014 | * @return string |
||
| 2015 | */ |
||
| 2016 | public static function get_stage() { |
||
| 2024 | |||
| 2025 | /** |
||
| 2026 | * Get the current archive date. |
||
| 2027 | * |
||
| 2028 | * @return string |
||
| 2029 | */ |
||
| 2030 | public static function current_archived_date() { |
||
| 2037 | |||
| 2038 | /** |
||
| 2039 | * Set the reading stage. |
||
| 2040 | * |
||
| 2041 | * @param string $stage New reading stage. |
||
| 2042 | * @throws InvalidArgumentException |
||
| 2043 | */ |
||
| 2044 | public static function set_stage($stage) { |
||
| 2050 | |||
| 2051 | /** |
||
| 2052 | * Set the reading archive date. |
||
| 2053 | * |
||
| 2054 | * @param string $date New reading archived date. |
||
| 2055 | */ |
||
| 2056 | public static function reading_archived_date($date) { |
||
| 2059 | |||
| 2060 | |||
| 2061 | /** |
||
| 2062 | * Get a singleton instance of a class in the given stage. |
||
| 2063 | * |
||
| 2064 | * @param string $class The name of the class. |
||
| 2065 | * @param string $stage The name of the stage. |
||
| 2066 | * @param string $filter A filter to be inserted into the WHERE clause. |
||
| 2067 | * @param boolean $cache Use caching. |
||
| 2068 | * @param string $sort A sort expression to be inserted into the ORDER BY clause. |
||
| 2069 | * |
||
| 2070 | * @return DataObject |
||
| 2071 | */ |
||
| 2072 | public static function get_one_by_stage($class, $stage, $filter = '', $cache = true, $sort = '') { |
||
| 2078 | |||
| 2079 | /** |
||
| 2080 | * Gets the current version number of a specific record. |
||
| 2081 | * |
||
| 2082 | * @param string $class |
||
| 2083 | * @param string $stage |
||
| 2084 | * @param int $id |
||
| 2085 | * @param boolean $cache |
||
| 2086 | * |
||
| 2087 | * @return int |
||
| 2088 | */ |
||
| 2089 | public static function get_versionnumber_by_stage($class, $stage, $id, $cache = true) { |
||
| 2122 | |||
| 2123 | /** |
||
| 2124 | * Pre-populate the cache for Versioned::get_versionnumber_by_stage() for |
||
| 2125 | * a list of record IDs, for more efficient database querying. If $idList |
||
| 2126 | * is null, then every record will be pre-cached. |
||
| 2127 | * |
||
| 2128 | * @param string $class |
||
| 2129 | * @param string $stage |
||
| 2130 | * @param array $idList |
||
| 2131 | */ |
||
| 2132 | public static function prepopulate_versionnumber_cache($class, $stage, $idList = null) { |
||
| 2162 | |||
| 2163 | /** |
||
| 2164 | * Get a set of class instances by the given stage. |
||
| 2165 | * |
||
| 2166 | * @param string $class The name of the class. |
||
| 2167 | * @param string $stage The name of the stage. |
||
| 2168 | * @param string $filter A filter to be inserted into the WHERE clause. |
||
| 2169 | * @param string $sort A sort expression to be inserted into the ORDER BY clause. |
||
| 2170 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 2171 | * @param int $limit A limit on the number of records returned from the database. |
||
| 2172 | * @param string $containerClass The container class for the result set (default is DataList) |
||
| 2173 | * |
||
| 2174 | * @return DataList A modified DataList designated to the specified stage |
||
| 2175 | */ |
||
| 2176 | public static function get_by_stage( |
||
| 2185 | |||
| 2186 | /** |
||
| 2187 | * Delete this record from the given stage |
||
| 2188 | * |
||
| 2189 | * @param string $stage |
||
| 2190 | */ |
||
| 2191 | public function deleteFromStage($stage) { |
||
| 2203 | |||
| 2204 | /** |
||
| 2205 | * Write the given record to the draft stage |
||
| 2206 | * |
||
| 2207 | * @param string $stage |
||
| 2208 | * @param boolean $forceInsert |
||
| 2209 | * @return int The ID of the record |
||
| 2210 | */ |
||
| 2211 | public function writeToStage($stage, $forceInsert = false) { |
||
| 2222 | |||
| 2223 | /** |
||
| 2224 | * Roll the draft version of this record to match the published record. |
||
| 2225 | * Caution: Doesn't overwrite the object properties with the rolled back version. |
||
| 2226 | * |
||
| 2227 | * {@see doRevertToLive()} to reollback to live |
||
| 2228 | * |
||
| 2229 | * @param int $version Version number |
||
| 2230 | */ |
||
| 2231 | public function doRollbackTo($version) { |
||
| 2238 | |||
| 2239 | public function onAfterRollback($version) { |
||
| 2252 | |||
| 2253 | /** |
||
| 2254 | * Return the latest version of the given record. |
||
| 2255 | * |
||
| 2256 | * @param string $class |
||
| 2257 | * @param int $id |
||
| 2258 | * @return DataObject |
||
| 2259 | */ |
||
| 2260 | public static function get_latest_version($class, $id) { |
||
| 2267 | |||
| 2268 | /** |
||
| 2269 | * Returns whether the current record is the latest one. |
||
| 2270 | * |
||
| 2271 | * @todo Performance - could do this directly via SQL. |
||
| 2272 | * |
||
| 2273 | * @see get_latest_version() |
||
| 2274 | * @see latestPublished |
||
| 2275 | * |
||
| 2276 | * @return boolean |
||
| 2277 | */ |
||
| 2278 | public function isLatestVersion() { |
||
| 2287 | |||
| 2288 | /** |
||
| 2289 | * Check if this record exists on live |
||
| 2290 | * |
||
| 2291 | * @return bool |
||
| 2292 | */ |
||
| 2293 | public function isPublished() { |
||
| 2311 | |||
| 2312 | /** |
||
| 2313 | * Check if page doesn't exist on any stage, but used to be |
||
| 2314 | * |
||
| 2315 | * @return bool |
||
| 2316 | */ |
||
| 2317 | public function isArchived() { |
||
| 2321 | |||
| 2322 | /** |
||
| 2323 | * Check if this record exists on the draft stage |
||
| 2324 | * |
||
| 2325 | * @return bool |
||
| 2326 | */ |
||
| 2327 | public function isOnDraft() { |
||
| 2340 | |||
| 2341 | /** |
||
| 2342 | * Compares current draft with live version, and returns true if no draft version of this page exists but the page |
||
| 2343 | * is still published (eg, after triggering "Delete from draft site" in the CMS). |
||
| 2344 | * |
||
| 2345 | * @return bool |
||
| 2346 | */ |
||
| 2347 | public function isOnLiveOnly() { |
||
| 2350 | |||
| 2351 | /** |
||
| 2352 | * Compares current draft with live version, and returns true if no live version exists, meaning the page was never |
||
| 2353 | * published. |
||
| 2354 | * |
||
| 2355 | * @return bool |
||
| 2356 | */ |
||
| 2357 | public function isOnDraftOnly() { |
||
| 2360 | |||
| 2361 | /** |
||
| 2362 | * Compares current draft with live version, and returns true if these versions differ, meaning there have been |
||
| 2363 | * unpublished changes to the draft site. |
||
| 2364 | * |
||
| 2365 | * @return bool |
||
| 2366 | */ |
||
| 2367 | public function isModifiedOnDraft() { |
||
| 2370 | |||
| 2371 | /** |
||
| 2372 | * Return the equivalent of a DataList::create() call, querying the latest |
||
| 2373 | * version of each record stored in the (class)_versions tables. |
||
| 2374 | * |
||
| 2375 | * In particular, this will query deleted records as well as active ones. |
||
| 2376 | * |
||
| 2377 | * @param string $class |
||
| 2378 | * @param string $filter |
||
| 2379 | * @param string $sort |
||
| 2380 | * @return DataList |
||
| 2381 | */ |
||
| 2382 | public static function get_including_deleted($class, $filter = "", $sort = "") { |
||
| 2390 | |||
| 2391 | /** |
||
| 2392 | * Return the specific version of the given id. |
||
| 2393 | * |
||
| 2394 | * Caution: The record is retrieved as a DataObject, but saving back |
||
| 2395 | * modifications via write() will create a new version, rather than |
||
| 2396 | * modifying the existing one. |
||
| 2397 | * |
||
| 2398 | * @param string $class |
||
| 2399 | * @param int $id |
||
| 2400 | * @param int $version |
||
| 2401 | * |
||
| 2402 | * @return DataObject |
||
| 2403 | */ |
||
| 2404 | public static function get_version($class, $id, $version) { |
||
| 2414 | |||
| 2415 | /** |
||
| 2416 | * Return a list of all versions for a given id. |
||
| 2417 | * |
||
| 2418 | * @param string $class |
||
| 2419 | * @param int $id |
||
| 2420 | * |
||
| 2421 | * @return DataList |
||
| 2422 | */ |
||
| 2423 | public static function get_all_versions($class, $id) { |
||
| 2430 | |||
| 2431 | /** |
||
| 2432 | * @param array $labels |
||
| 2433 | */ |
||
| 2434 | public function updateFieldLabels(&$labels) { |
||
| 2437 | |||
| 2438 | /** |
||
| 2439 | * @param FieldList $fields |
||
| 2440 | */ |
||
| 2441 | public function updateCMSFields(FieldList $fields) { |
||
| 2446 | |||
| 2447 | /** |
||
| 2448 | * Ensure version ID is reset to 0 on duplicate |
||
| 2449 | * |
||
| 2450 | * @param DataObject $source Record this was duplicated from |
||
| 2451 | * @param bool $doWrite |
||
| 2452 | */ |
||
| 2453 | public function onBeforeDuplicate($source, $doWrite) { |
||
| 2456 | |||
| 2457 | public function flushCache() { |
||
| 2460 | |||
| 2461 | /** |
||
| 2462 | * Return a piece of text to keep DataObject cache keys appropriately specific. |
||
| 2463 | * |
||
| 2464 | * @return string |
||
| 2465 | */ |
||
| 2466 | public function cacheKeyComponent() { |
||
| 2469 | |||
| 2470 | /** |
||
| 2471 | * Returns an array of possible stages. |
||
| 2472 | * |
||
| 2473 | * @return array |
||
| 2474 | */ |
||
| 2475 | public function getVersionedStages() { |
||
| 2482 | |||
| 2483 | public static function get_template_global_variables() { |
||
| 2488 | |||
| 2489 | /** |
||
| 2490 | * Check if this object has stages |
||
| 2491 | * |
||
| 2492 | * @return bool True if this object is staged |
||
| 2493 | */ |
||
| 2494 | public function hasStages() { |
||
| 2497 | } |
||
| 2498 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.