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 | * Keep track of the archive tables that have been created. |
||
| 117 | * |
||
| 118 | * @config |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | private static $archive_tables = array(); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Additional database indexes for the new |
||
| 125 | * "_versions" table. Used in {@link augmentDatabase()}. |
||
| 126 | * |
||
| 127 | * @var array $indexes_for_versions_table |
||
| 128 | */ |
||
| 129 | private static $indexes_for_versions_table = array( |
||
| 130 | 'RecordID_Version' => '("RecordID","Version")', |
||
| 131 | 'RecordID' => true, |
||
| 132 | 'Version' => true, |
||
| 133 | 'AuthorID' => true, |
||
| 134 | 'PublisherID' => true, |
||
| 135 | ); |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * An array of DataObject extensions that may require versioning for extra tables |
||
| 140 | * The array value is a set of suffixes to form these table names, assuming a preceding '_'. |
||
| 141 | * E.g. if Extension1 creates a new table 'Class_suffix1' |
||
| 142 | * and Extension2 the tables 'Class_suffix2' and 'Class_suffix3': |
||
| 143 | * |
||
| 144 | * $versionableExtensions = array( |
||
| 145 | * 'Extension1' => 'suffix1', |
||
| 146 | * 'Extension2' => array('suffix2', 'suffix3'), |
||
| 147 | * ); |
||
| 148 | * |
||
| 149 | * This can also be manipulated by updating the current loaded config |
||
| 150 | * |
||
| 151 | * SiteTree: |
||
| 152 | * versionableExtensions: |
||
| 153 | * - Extension1: |
||
| 154 | * - suffix1 |
||
| 155 | * - suffix2 |
||
| 156 | * - Extension2: |
||
| 157 | * - suffix1 |
||
| 158 | * - suffix2 |
||
| 159 | * |
||
| 160 | * or programatically: |
||
| 161 | * |
||
| 162 | * Config::inst()->update($this->owner->class, 'versionableExtensions', |
||
| 163 | * array('Extension1' => 'suffix1', 'Extension2' => array('suffix2', 'suffix3'))); |
||
| 164 | * |
||
| 165 | * |
||
| 166 | * Make sure your extension has a static $enabled-property that determines if it is |
||
| 167 | * processed by Versioned. |
||
| 168 | * |
||
| 169 | * @config |
||
| 170 | * @var array |
||
| 171 | */ |
||
| 172 | private static $versionableExtensions = array('Translatable' => 'lang'); |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Permissions necessary to view records outside of the live stage (e.g. archive / draft stage). |
||
| 176 | * |
||
| 177 | * @config |
||
| 178 | * @var array |
||
| 179 | */ |
||
| 180 | private static $non_live_permissions = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_CMSMain', 'VIEW_DRAFT_CONTENT'); |
||
| 181 | |||
| 182 | /** |
||
| 183 | * List of relationships on this object that are "owned" by this object. |
||
| 184 | * Owership in the context of versioned objects is a relationship where |
||
| 185 | * the publishing of owning objects requires the publishing of owned objects. |
||
| 186 | * |
||
| 187 | * E.g. A page owns a set of banners, as in order for the page to be published, all |
||
| 188 | * banners on this page must also be published for it to be visible. |
||
| 189 | * |
||
| 190 | * Typically any object and its owned objects should be visible in the same edit view. |
||
| 191 | * E.g. a page and {@see GridField} of banners. |
||
| 192 | * |
||
| 193 | * Page hierarchy is typically not considered an ownership relationship. |
||
| 194 | * |
||
| 195 | * Ownership is recursive; If A owns B and B owns C then A owns C. |
||
| 196 | * |
||
| 197 | * @config |
||
| 198 | * @var array List of has_many or many_many relationships owned by this object. |
||
| 199 | */ |
||
| 200 | private static $owns = array(); |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Opposing relationship to owns config; Represents the objects which |
||
| 204 | * own the current object. |
||
| 205 | * |
||
| 206 | * @var array |
||
| 207 | */ |
||
| 208 | private static $owned_by = array(); |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Reset static configuration variables to their default values. |
||
| 212 | */ |
||
| 213 | public static function reset() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Amend freshly created DataQuery objects with versioned-specific |
||
| 220 | * information. |
||
| 221 | * |
||
| 222 | * @param SQLSelect |
||
| 223 | * @param DataQuery |
||
| 224 | */ |
||
| 225 | public function augmentDataQueryCreation(SQLSelect &$query, DataQuery &$dataQuery) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Construct a new Versioned object. |
||
| 239 | * |
||
| 240 | * @var string $mode One of "StagedVersioned" or "Versioned". |
||
| 241 | */ |
||
| 242 | public function __construct($mode = self::STAGEDVERSIONED) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Cache of version to modified dates for this objects |
||
| 265 | * |
||
| 266 | * @var array |
||
| 267 | */ |
||
| 268 | protected $versionModifiedCache = array(); |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get modified date for the given version |
||
| 272 | * |
||
| 273 | * @param int $version |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | protected function getLastEditedForVersion($version) { |
||
| 277 | // Cache key |
||
| 278 | $baseTable = ClassInfo::baseDataClass($this->owner); |
||
| 279 | $id = $this->owner->ID; |
||
| 280 | $key = "{$baseTable}#{$id}/{$version}"; |
||
| 281 | |||
| 282 | // Check cache |
||
| 283 | if(isset($this->versionModifiedCache[$key])) { |
||
| 284 | return $this->versionModifiedCache[$key]; |
||
| 285 | } |
||
| 286 | |||
| 287 | // Build query |
||
| 288 | $table = "\"{$baseTable}_versions\""; |
||
| 289 | $query = SQLSelect::create('"LastEdited"', $table) |
||
| 290 | ->addWhere([ |
||
| 291 | "{$table}.\"RecordID\"" => $id, |
||
| 292 | "{$table}.\"Version\"" => $version |
||
| 293 | ]); |
||
| 294 | $date = $query->execute()->value(); |
||
| 295 | if($date) { |
||
| 296 | $this->versionModifiedCache[$key] = $date; |
||
| 297 | } |
||
| 298 | return $date; |
||
| 299 | } |
||
| 300 | |||
| 301 | |||
| 302 | public function updateInheritableQueryParams(&$params) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Augment the the SQLSelect that is created by the DataQuery |
||
| 341 | * |
||
| 342 | * See {@see augmentLazyLoadFields} for lazy-loading applied prior to this. |
||
| 343 | * |
||
| 344 | * @param SQLSelect $query |
||
| 345 | * @param DataQuery $dataQuery |
||
| 346 | * @throws InvalidArgumentException |
||
| 347 | */ |
||
| 348 | public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Determine if the given versioned table is a part of the sub-tree of the current dataobject |
||
| 504 | * This helps prevent rewriting of other tables that get joined in, in particular, many_many tables |
||
| 505 | * |
||
| 506 | * @param string $table |
||
| 507 | * @return bool True if this table should be versioned |
||
| 508 | */ |
||
| 509 | protected function isTableVersioned($table) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * For lazy loaded fields requiring extra sql manipulation, ie versioning. |
||
| 519 | * |
||
| 520 | * @param SQLSelect $query |
||
| 521 | * @param DataQuery $dataQuery |
||
| 522 | * @param DataObject $dataObject |
||
| 523 | */ |
||
| 524 | public function augmentLoadLazyFields(SQLSelect &$query, DataQuery &$dataQuery = null, $dataObject) { |
||
| 544 | |||
| 545 | |||
| 546 | /** |
||
| 547 | * Called by {@link SapphireTest} when the database is reset. |
||
| 548 | * |
||
| 549 | * @todo Reduce the coupling between this and SapphireTest, somehow. |
||
| 550 | */ |
||
| 551 | public static function on_db_reset() { |
||
| 562 | |||
| 563 | public function augmentDatabase() { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Helper for augmentDatabase() to find unique indexes and convert them to non-unique |
||
| 719 | * |
||
| 720 | * @param array $indexes The indexes to convert |
||
| 721 | * @return array $indexes |
||
| 722 | */ |
||
| 723 | private function uniqueToIndex($indexes) { |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Generates a ($table)_version DB manipulation and injects it into the current $manipulation |
||
| 748 | * |
||
| 749 | * @param array $manipulation Source manipulation data |
||
| 750 | * @param string $table Name of table |
||
| 751 | * @param int $recordID ID of record to version |
||
| 752 | */ |
||
| 753 | protected function augmentWriteVersioned(&$manipulation, $table, $recordID) { |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Rewrite the given manipulation to update the selected (non-default) stage |
||
| 809 | * |
||
| 810 | * @param array $manipulation Source manipulation data |
||
| 811 | * @param string $table Name of table |
||
| 812 | * @param int $recordID ID of record to version |
||
| 813 | */ |
||
| 814 | protected function augmentWriteStaged(&$manipulation, $table, $recordID) { |
||
| 827 | |||
| 828 | |||
| 829 | public function augmentWrite(&$manipulation) { |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Perform a write without affecting the version table. |
||
| 900 | * On objects without versioning. |
||
| 901 | * |
||
| 902 | * @return int The ID of the record |
||
| 903 | */ |
||
| 904 | public function writeWithoutVersion() { |
||
| 909 | |||
| 910 | /** |
||
| 911 | * |
||
| 912 | */ |
||
| 913 | public function onAfterWrite() { |
||
| 916 | |||
| 917 | /** |
||
| 918 | * If a write was skipped, then we need to ensure that we don't leave a |
||
| 919 | * migrateVersion() value lying around for the next write. |
||
| 920 | */ |
||
| 921 | public function onAfterSkippedWrite() { |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Find all objects owned by the current object. |
||
| 927 | * Note that objects will only be searched in the same stage as the given record. |
||
| 928 | * |
||
| 929 | * @param bool $recursive True if recursive |
||
| 930 | * @param ArrayList $list Optional list to add items to |
||
| 931 | * @return ArrayList list of objects |
||
| 932 | */ |
||
| 933 | public function findOwned($recursive = true, $list = null) |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Find objects which own this object. |
||
| 941 | * Note that objects will only be searched in the same stage as the given record. |
||
| 942 | * |
||
| 943 | * @param bool $recursive True if recursive |
||
| 944 | * @param ArrayList $list Optional list to add items to |
||
| 945 | * @return ArrayList list of objects |
||
| 946 | */ |
||
| 947 | public function findOwners($recursive = true, $list = null) |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Find objects in the given relationships, merging them into the given list |
||
| 955 | * |
||
| 956 | * @param array $source Config property to extract relationships from |
||
| 957 | * @param bool $recursive True if recursive |
||
| 958 | * @param ArrayList $list Optional list to add items to |
||
| 959 | * @return ArrayList The list |
||
| 960 | */ |
||
| 961 | public function findRelatedObjects($source, $recursive = true, $list = null) |
||
| 1014 | |||
| 1015 | /** |
||
| 1016 | * This function should return true if the current user can publish this record. |
||
| 1017 | * It can be overloaded to customise the security model for an application. |
||
| 1018 | * |
||
| 1019 | * Denies permission if any of the following conditions is true: |
||
| 1020 | * - canPublish() on any extension returns false |
||
| 1021 | * - canEdit() returns false |
||
| 1022 | * |
||
| 1023 | * @param Member $member |
||
| 1024 | * @return bool True if the current user can publish this record. |
||
| 1025 | */ |
||
| 1026 | public function canPublish($member = null) { |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Check if the current user can delete this record from live |
||
| 1053 | * |
||
| 1054 | * @param null $member |
||
| 1055 | * @return mixed |
||
| 1056 | */ |
||
| 1057 | public function canUnpublish($member = null) { |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Check if the current user is allowed to archive this record. |
||
| 1084 | * If extended, ensure that both canDelete and canUnpublish are extended also |
||
| 1085 | * |
||
| 1086 | * @param Member $member |
||
| 1087 | * @return bool |
||
| 1088 | */ |
||
| 1089 | public function canArchive($member = null) { |
||
| 1122 | |||
| 1123 | /** |
||
| 1124 | * Check if the user can revert this record to live |
||
| 1125 | * |
||
| 1126 | * @param Member $member |
||
| 1127 | * @return bool |
||
| 1128 | */ |
||
| 1129 | public function canRevertToLive($member = null) { |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Extend permissions to include additional security for objects that are not published to live. |
||
| 1162 | * |
||
| 1163 | * @param Member $member |
||
| 1164 | * @return bool|null |
||
| 1165 | */ |
||
| 1166 | public function canView($member = null) { |
||
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Determine if there are any additional restrictions on this object for the given reading version. |
||
| 1175 | * |
||
| 1176 | * Override this in a subclass to customise any additional effect that Versioned applies to canView. |
||
| 1177 | * |
||
| 1178 | * This is expected to be called by canView, and thus is only responsible for denying access if |
||
| 1179 | * the default canView would otherwise ALLOW access. Thus it should not be called in isolation |
||
| 1180 | * as an authoritative permission check. |
||
| 1181 | * |
||
| 1182 | * This has the following extension points: |
||
| 1183 | * - canViewDraft is invoked if Mode = stage and Stage = stage |
||
| 1184 | * - canViewArchived is invoked if Mode = archive |
||
| 1185 | * |
||
| 1186 | * @param Member $member |
||
| 1187 | * @return bool False is returned if the current viewing mode denies visibility |
||
| 1188 | */ |
||
| 1189 | public function canViewVersioned($member = null) { |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Determines canView permissions for the latest version of this object on a specific stage. |
||
| 1230 | * Usually the stage is read from {@link Versioned::current_stage()}. |
||
| 1231 | * |
||
| 1232 | * This method should be invoked by user code to check if a record is visible in the given stage. |
||
| 1233 | * |
||
| 1234 | * This method should not be called via ->extend('canViewStage'), but rather should be |
||
| 1235 | * overridden in the extended class. |
||
| 1236 | * |
||
| 1237 | * @param string $stage |
||
| 1238 | * @param Member $member |
||
| 1239 | * @return bool |
||
| 1240 | */ |
||
| 1241 | public function canViewStage($stage = 'Live', $member = null) { |
||
| 1251 | |||
| 1252 | /** |
||
| 1253 | * Determine if a table is supporting the Versioned extensions (e.g. |
||
| 1254 | * $table_versions does exists). |
||
| 1255 | * |
||
| 1256 | * @param string $table Table name |
||
| 1257 | * @return boolean |
||
| 1258 | */ |
||
| 1259 | public function canBeVersioned($table) { |
||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Check if a certain table has the 'Version' field. |
||
| 1267 | * |
||
| 1268 | * @param string $table Table name |
||
| 1269 | * |
||
| 1270 | * @return boolean Returns false if the field isn't in the table, true otherwise |
||
| 1271 | */ |
||
| 1272 | public function hasVersionField($table) { |
||
| 1282 | |||
| 1283 | /** |
||
| 1284 | * @param string $table |
||
| 1285 | * |
||
| 1286 | * @return string |
||
| 1287 | */ |
||
| 1288 | public function extendWithSuffix($table) { |
||
| 1305 | |||
| 1306 | /** |
||
| 1307 | * Get the latest published DataObject. |
||
| 1308 | * |
||
| 1309 | * @return DataObject |
||
| 1310 | */ |
||
| 1311 | public function latestPublished() { |
||
| 1323 | |||
| 1324 | /** |
||
| 1325 | * Provides a simple doPublish action for Versioned dataobjects |
||
| 1326 | * |
||
| 1327 | * @return bool True if publish was successful |
||
| 1328 | */ |
||
| 1329 | public function doPublish() { |
||
| 1341 | |||
| 1342 | /** |
||
| 1343 | * Trigger publishing of owned objects |
||
| 1344 | */ |
||
| 1345 | public function onAfterPublish() { |
||
| 1358 | |||
| 1359 | /** |
||
| 1360 | * Set foreign keys of has_many objects to 0 where those objects were |
||
| 1361 | * disowned as a result of a partial publish / unpublish. |
||
| 1362 | * I.e. this object and its owned objects were recently written to $targetStage, |
||
| 1363 | * but deleted objects were not. |
||
| 1364 | * |
||
| 1365 | * Note that this operation does not create any new Versions |
||
| 1366 | * |
||
| 1367 | * @param string $sourceStage Objects in this stage will not be unlinked. |
||
| 1368 | * @param string $targetStage Objects which exist in this stage but not $sourceStage |
||
| 1369 | * will be unlinked. |
||
| 1370 | */ |
||
| 1371 | public function unlinkDisownedObjects($sourceStage, $targetStage) { |
||
| 1427 | |||
| 1428 | /** |
||
| 1429 | * Removes the record from both live and stage |
||
| 1430 | * |
||
| 1431 | * @return bool Success |
||
| 1432 | */ |
||
| 1433 | public function doArchive() { |
||
| 1446 | |||
| 1447 | /** |
||
| 1448 | * Removes this record from the live site |
||
| 1449 | * |
||
| 1450 | * @return bool Flag whether the unpublish was successful |
||
| 1451 | */ |
||
| 1452 | public function doUnpublish() { |
||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * Trigger unpublish of owning objects |
||
| 1485 | */ |
||
| 1486 | public function onAfterUnpublish() { |
||
| 1495 | |||
| 1496 | |||
| 1497 | /** |
||
| 1498 | * Revert the draft changes: replace the draft content with the content on live |
||
| 1499 | * |
||
| 1500 | * @return bool True if the revert was successful |
||
| 1501 | */ |
||
| 1502 | public function doRevertToLive() { |
||
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Trigger revert of all owned objects to stage |
||
| 1516 | */ |
||
| 1517 | public function onAfterRevertToLive() { |
||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Move a database record from one stage to the other. |
||
| 1536 | * |
||
| 1537 | * @param int|string $fromStage Place to copy from. Can be either a stage name or a version number. |
||
| 1538 | * @param string $toStage Place to copy to. Must be a stage name. |
||
| 1539 | * @param bool $createNewVersion Set this to true to create a new version number. |
||
| 1540 | * By default, the existing version number will be copied over. |
||
| 1541 | */ |
||
| 1542 | public function publish($fromStage, $toStage, $createNewVersion = false) { |
||
| 1601 | |||
| 1602 | /** |
||
| 1603 | * Set the migrating version. |
||
| 1604 | * |
||
| 1605 | * @param string $version The version. |
||
| 1606 | */ |
||
| 1607 | public function migrateVersion($version) { |
||
| 1610 | |||
| 1611 | /** |
||
| 1612 | * Compare two stages to see if they're different. |
||
| 1613 | * |
||
| 1614 | * Only checks the version numbers, not the actual content. |
||
| 1615 | * |
||
| 1616 | * @param string $stage1 The first stage to check. |
||
| 1617 | * @param string $stage2 |
||
| 1618 | * @return bool |
||
| 1619 | */ |
||
| 1620 | public function stagesDiffer($stage1, $stage2) { |
||
| 1642 | |||
| 1643 | /** |
||
| 1644 | * @param string $filter |
||
| 1645 | * @param string $sort |
||
| 1646 | * @param string $limit |
||
| 1647 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1648 | * @param string $having |
||
| 1649 | * @return ArrayList |
||
| 1650 | */ |
||
| 1651 | public function Versions($filter = "", $sort = "", $limit = "", $join = "", $having = "") { |
||
| 1654 | |||
| 1655 | /** |
||
| 1656 | * Return a list of all the versions available. |
||
| 1657 | * |
||
| 1658 | * @param string $filter |
||
| 1659 | * @param string $sort |
||
| 1660 | * @param string $limit |
||
| 1661 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1662 | * @param string $having |
||
| 1663 | * @return ArrayList |
||
| 1664 | */ |
||
| 1665 | public function allVersions($filter = "", $sort = "", $limit = "", $join = "", $having = "") { |
||
| 1711 | |||
| 1712 | /** |
||
| 1713 | * Compare two version, and return the diff between them. |
||
| 1714 | * |
||
| 1715 | * @param string $from The version to compare from. |
||
| 1716 | * @param string $to The version to compare to. |
||
| 1717 | * |
||
| 1718 | * @return DataObject |
||
| 1719 | */ |
||
| 1720 | public function compareVersions($from, $to) { |
||
| 1729 | |||
| 1730 | /** |
||
| 1731 | * Return the base table - the class that directly extends DataObject. |
||
| 1732 | * |
||
| 1733 | * @param string $stage |
||
| 1734 | * @return string |
||
| 1735 | */ |
||
| 1736 | public function baseTable($stage = null) { |
||
| 1740 | |||
| 1741 | /** |
||
| 1742 | * Given a class and stage determine the table name. |
||
| 1743 | * |
||
| 1744 | * Note: Stages this asset does not exist in will default to the draft table. |
||
| 1745 | * |
||
| 1746 | * @param string $class |
||
| 1747 | * @param string $stage |
||
| 1748 | * @return string Table name |
||
| 1749 | */ |
||
| 1750 | public function stageTable($class, $stage) { |
||
| 1756 | |||
| 1757 | //-----------------------------------------------------------------------------------------------// |
||
| 1758 | |||
| 1759 | |||
| 1760 | /** |
||
| 1761 | * Determine if the current user is able to set the given site stage / archive |
||
| 1762 | * |
||
| 1763 | * @param SS_HTTPRequest $request |
||
| 1764 | * @return bool |
||
| 1765 | */ |
||
| 1766 | public static function can_choose_site_stage($request) { |
||
| 1779 | |||
| 1780 | /** |
||
| 1781 | * Choose the stage the site is currently on. |
||
| 1782 | * |
||
| 1783 | * If $_GET['stage'] is set, then it will use that stage, and store it in |
||
| 1784 | * the session. |
||
| 1785 | * |
||
| 1786 | * if $_GET['archiveDate'] is set, it will use that date, and store it in |
||
| 1787 | * the session. |
||
| 1788 | * |
||
| 1789 | * If neither of these are set, it checks the session, otherwise the stage |
||
| 1790 | * is set to 'Live'. |
||
| 1791 | */ |
||
| 1792 | public static function choose_site_stage() { |
||
| 1835 | |||
| 1836 | /** |
||
| 1837 | * Set the current reading mode. |
||
| 1838 | * |
||
| 1839 | * @param string $mode |
||
| 1840 | */ |
||
| 1841 | public static function set_reading_mode($mode) { |
||
| 1844 | |||
| 1845 | /** |
||
| 1846 | * Get the current reading mode. |
||
| 1847 | * |
||
| 1848 | * @return string |
||
| 1849 | */ |
||
| 1850 | public static function get_reading_mode() { |
||
| 1853 | |||
| 1854 | /** |
||
| 1855 | * Get the current reading stage. |
||
| 1856 | * |
||
| 1857 | * @return string |
||
| 1858 | */ |
||
| 1859 | public static function get_stage() { |
||
| 1866 | |||
| 1867 | /** |
||
| 1868 | * Get the current archive date. |
||
| 1869 | * |
||
| 1870 | * @return string |
||
| 1871 | */ |
||
| 1872 | public static function current_archived_date() { |
||
| 1878 | |||
| 1879 | /** |
||
| 1880 | * Set the reading stage. |
||
| 1881 | * |
||
| 1882 | * @param string $stage New reading stage. |
||
| 1883 | */ |
||
| 1884 | public static function set_stage($stage) { |
||
| 1887 | |||
| 1888 | /** |
||
| 1889 | * Set the reading archive date. |
||
| 1890 | * |
||
| 1891 | * @param string $date New reading archived date. |
||
| 1892 | */ |
||
| 1893 | public static function reading_archived_date($date) { |
||
| 1896 | |||
| 1897 | |||
| 1898 | /** |
||
| 1899 | * Get a singleton instance of a class in the given stage. |
||
| 1900 | * |
||
| 1901 | * @param string $class The name of the class. |
||
| 1902 | * @param string $stage The name of the stage. |
||
| 1903 | * @param string $filter A filter to be inserted into the WHERE clause. |
||
| 1904 | * @param boolean $cache Use caching. |
||
| 1905 | * @param string $sort A sort expression to be inserted into the ORDER BY clause. |
||
| 1906 | * |
||
| 1907 | * @return DataObject |
||
| 1908 | */ |
||
| 1909 | public static function get_one_by_stage($class, $stage, $filter = '', $cache = true, $sort = '') { |
||
| 1915 | |||
| 1916 | /** |
||
| 1917 | * Gets the current version number of a specific record. |
||
| 1918 | * |
||
| 1919 | * @param string $class |
||
| 1920 | * @param string $stage |
||
| 1921 | * @param int $id |
||
| 1922 | * @param boolean $cache |
||
| 1923 | * |
||
| 1924 | * @return int |
||
| 1925 | */ |
||
| 1926 | public static function get_versionnumber_by_stage($class, $stage, $id, $cache = true) { |
||
| 1956 | |||
| 1957 | /** |
||
| 1958 | * Pre-populate the cache for Versioned::get_versionnumber_by_stage() for |
||
| 1959 | * a list of record IDs, for more efficient database querying. If $idList |
||
| 1960 | * is null, then every record will be pre-cached. |
||
| 1961 | * |
||
| 1962 | * @param string $class |
||
| 1963 | * @param string $stage |
||
| 1964 | * @param array $idList |
||
| 1965 | */ |
||
| 1966 | public static function prepopulate_versionnumber_cache($class, $stage, $idList = null) { |
||
| 1993 | |||
| 1994 | /** |
||
| 1995 | * Get a set of class instances by the given stage. |
||
| 1996 | * |
||
| 1997 | * @param string $class The name of the class. |
||
| 1998 | * @param string $stage The name of the stage. |
||
| 1999 | * @param string $filter A filter to be inserted into the WHERE clause. |
||
| 2000 | * @param string $sort A sort expression to be inserted into the ORDER BY clause. |
||
| 2001 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 2002 | * @param int $limit A limit on the number of records returned from the database. |
||
| 2003 | * @param string $containerClass The container class for the result set (default is DataList) |
||
| 2004 | * |
||
| 2005 | * @return DataList A modified DataList designated to the specified stage |
||
| 2006 | */ |
||
| 2007 | public static function get_by_stage( |
||
| 2016 | |||
| 2017 | /** |
||
| 2018 | * Delete this record from the given stage |
||
| 2019 | * |
||
| 2020 | * @param string $stage |
||
| 2021 | */ |
||
| 2022 | public function deleteFromStage($stage) { |
||
| 2034 | |||
| 2035 | /** |
||
| 2036 | * Write the given record to the draft stage |
||
| 2037 | * |
||
| 2038 | * @param string $stage |
||
| 2039 | * @param boolean $forceInsert |
||
| 2040 | * @return int The ID of the record |
||
| 2041 | */ |
||
| 2042 | public function writeToStage($stage, $forceInsert = false) { |
||
| 2053 | |||
| 2054 | /** |
||
| 2055 | * Roll the draft version of this record to match the published record. |
||
| 2056 | * Caution: Doesn't overwrite the object properties with the rolled back version. |
||
| 2057 | * |
||
| 2058 | * {@see doRevertToLive()} to reollback to live |
||
| 2059 | * |
||
| 2060 | * @param int $version Version number |
||
| 2061 | */ |
||
| 2062 | public function doRollbackTo($version) { |
||
| 2069 | |||
| 2070 | public function onAfterRollback($version) { |
||
| 2082 | |||
| 2083 | /** |
||
| 2084 | * Return the latest version of the given record. |
||
| 2085 | * |
||
| 2086 | * @param string $class |
||
| 2087 | * @param int $id |
||
| 2088 | * @return DataObject |
||
| 2089 | */ |
||
| 2090 | public static function get_latest_version($class, $id) { |
||
| 2097 | |||
| 2098 | /** |
||
| 2099 | * Returns whether the current record is the latest one. |
||
| 2100 | * |
||
| 2101 | * @todo Performance - could do this directly via SQL. |
||
| 2102 | * |
||
| 2103 | * @see get_latest_version() |
||
| 2104 | * @see latestPublished |
||
| 2105 | * |
||
| 2106 | * @return boolean |
||
| 2107 | */ |
||
| 2108 | public function isLatestVersion() { |
||
| 2117 | |||
| 2118 | /** |
||
| 2119 | * Check if this record exists on live |
||
| 2120 | * |
||
| 2121 | * @return bool |
||
| 2122 | */ |
||
| 2123 | public function isPublished() { |
||
| 2142 | |||
| 2143 | /** |
||
| 2144 | * Check if this record exists on the draft stage |
||
| 2145 | * |
||
| 2146 | * @return bool |
||
| 2147 | */ |
||
| 2148 | public function isOnDraft() { |
||
| 2161 | |||
| 2162 | |||
| 2163 | |||
| 2164 | /** |
||
| 2165 | * Return the equivalent of a DataList::create() call, querying the latest |
||
| 2166 | * version of each record stored in the (class)_versions tables. |
||
| 2167 | * |
||
| 2168 | * In particular, this will query deleted records as well as active ones. |
||
| 2169 | * |
||
| 2170 | * @param string $class |
||
| 2171 | * @param string $filter |
||
| 2172 | * @param string $sort |
||
| 2173 | * @return DataList |
||
| 2174 | */ |
||
| 2175 | public static function get_including_deleted($class, $filter = "", $sort = "") { |
||
| 2183 | |||
| 2184 | /** |
||
| 2185 | * Return the specific version of the given id. |
||
| 2186 | * |
||
| 2187 | * Caution: The record is retrieved as a DataObject, but saving back |
||
| 2188 | * modifications via write() will create a new version, rather than |
||
| 2189 | * modifying the existing one. |
||
| 2190 | * |
||
| 2191 | * @param string $class |
||
| 2192 | * @param int $id |
||
| 2193 | * @param int $version |
||
| 2194 | * |
||
| 2195 | * @return DataObject |
||
| 2196 | */ |
||
| 2197 | public static function get_version($class, $id, $version) { |
||
| 2207 | |||
| 2208 | /** |
||
| 2209 | * Return a list of all versions for a given id. |
||
| 2210 | * |
||
| 2211 | * @param string $class |
||
| 2212 | * @param int $id |
||
| 2213 | * |
||
| 2214 | * @return DataList |
||
| 2215 | */ |
||
| 2216 | public static function get_all_versions($class, $id) { |
||
| 2223 | |||
| 2224 | /** |
||
| 2225 | * @param array $labels |
||
| 2226 | */ |
||
| 2227 | public function updateFieldLabels(&$labels) { |
||
| 2230 | |||
| 2231 | /** |
||
| 2232 | * @param FieldList |
||
| 2233 | */ |
||
| 2234 | public function updateCMSFields(FieldList $fields) { |
||
| 2239 | |||
| 2240 | /** |
||
| 2241 | * Ensure version ID is reset to 0 on duplicate |
||
| 2242 | * |
||
| 2243 | * @param DataObject $source Record this was duplicated from |
||
| 2244 | * @param bool $doWrite |
||
| 2245 | */ |
||
| 2246 | public function onBeforeDuplicate($source, $doWrite) { |
||
| 2249 | |||
| 2250 | public function flushCache() { |
||
| 2253 | |||
| 2254 | /** |
||
| 2255 | * Return a piece of text to keep DataObject cache keys appropriately specific. |
||
| 2256 | * |
||
| 2257 | * @return string |
||
| 2258 | */ |
||
| 2259 | public function cacheKeyComponent() { |
||
| 2262 | |||
| 2263 | /** |
||
| 2264 | * Returns an array of possible stages. |
||
| 2265 | * |
||
| 2266 | * @return array |
||
| 2267 | */ |
||
| 2268 | public function getVersionedStages() { |
||
| 2275 | |||
| 2276 | public static function get_template_global_variables() { |
||
| 2281 | |||
| 2282 | /** |
||
| 2283 | * Check if this object has stages |
||
| 2284 | * |
||
| 2285 | * @return bool True if this object is staged |
||
| 2286 | */ |
||
| 2287 | public function hasStages() { |
||
| 2290 | } |
||
| 2291 | |||
| 2410 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.