Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 20 | class Versioned extends DataExtension implements TemplateGlobalProvider { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * An array of possible stages. |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $stages; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The 'default' stage. |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $defaultStage; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The 'live' stage. |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $liveStage; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The default reading mode |
||
| 42 | */ |
||
| 43 | const DEFAULT_MODE = 'Stage.Live'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * A version that a DataObject should be when it is 'migrating', that is, when it is in the process of moving from |
||
| 47 | * one stage to another. |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public $migratingVersion; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * A cache used by get_versionnumber_by_stage(). Clear through {@link flushCache()}. |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected static $cache_versionnumber; |
||
| 57 | |||
| 58 | /** @var string */ |
||
| 59 | protected static $reading_mode = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Flag which is temporarily changed during the write() process to influence augmentWrite() behaviour. If set to |
||
| 63 | * true, no new version will be created for the following write. Needs to be public as other classes introspect this |
||
| 64 | * state during the write process in order to adapt to this versioning behaviour. |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | public $_nextWriteWithoutVersion = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Additional database columns for the new "_versions" table. Used in {@link augmentDatabase()} and all Versioned |
||
| 71 | * calls extending or creating SELECT statements. |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private static $db_for_versions_table = array( |
||
| 75 | "RecordID" => "Int", |
||
| 76 | "Version" => "Int", |
||
| 77 | "WasPublished" => "Boolean", |
||
| 78 | "AuthorID" => "Int", |
||
| 79 | "PublisherID" => "Int" |
||
| 80 | ); |
||
| 81 | |||
| 82 | private static $db = array( |
||
| 83 | 'Version' => 'Int' |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Used to enable or disable the prepopulation of the version number cache. Defaults to true. |
||
| 88 | * @var bool |
||
| 89 | */ |
||
| 90 | private static $prepopulate_versionnumber_cache = true; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Keep track of the archive tables that have been created. |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | private static $archive_tables = array(); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Additional database indexes for the new "_versions" table. Used in {@link augmentDatabase()}. |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | private static $indexes_for_versions_table = array( |
||
| 103 | 'RecordID_Version' => '("RecordID","Version")', |
||
| 104 | 'RecordID' => true, |
||
| 105 | 'Version' => true, |
||
| 106 | 'AuthorID' => true, |
||
| 107 | 'PublisherID' => true, |
||
| 108 | ); |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * An array of DataObject extensions that may require versioning for extra tables. The array value is a set of |
||
| 113 | * suffixes to form these table names, assuming a preceding '_'. E.g. if Extension1 creates a new table |
||
| 114 | * 'Class_suffix1' and Extension2 the tables 'Class_suffix2' and 'Class_suffix3': |
||
| 115 | * |
||
| 116 | * $versionableExtensions = array( |
||
| 117 | * 'Extension1' => 'suffix1', |
||
| 118 | * 'Extension2' => array('suffix2', 'suffix3'), |
||
| 119 | * ); |
||
| 120 | * |
||
| 121 | * This can also be manipulated by updating the current loaded config |
||
| 122 | * |
||
| 123 | * SiteTree: |
||
| 124 | * versionableExtensions: |
||
| 125 | * - Extension1: |
||
| 126 | * - suffix1 |
||
| 127 | * - suffix2 |
||
| 128 | * - Extension2: |
||
| 129 | * - suffix1 |
||
| 130 | * - suffix2 |
||
| 131 | * |
||
| 132 | * or programatically: |
||
| 133 | * |
||
| 134 | * Config::inst()->update($this->owner->class, 'versionableExtensions', |
||
| 135 | * array('Extension1' => 'suffix1', 'Extension2' => array('suffix2', 'suffix3'))); |
||
| 136 | * |
||
| 137 | * Make sure your extension has a static $enabled-property that determines if it is processed by Versioned. |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | protected static $versionableExtensions = array('Translatable' => 'lang'); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Permissions necessary to view records outside of the live stage (e.g. archive / draft stage). |
||
| 145 | * |
||
| 146 | * @config |
||
| 147 | * @var array |
||
| 148 | */ |
||
| 149 | private static $non_live_permissions = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_CMSMain', 'VIEW_DRAFT_CONTENT'); |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Reset static configuration variables to their default values. |
||
| 153 | */ |
||
| 154 | public static function reset() { |
||
| 155 | self::$reading_mode = ''; |
||
| 156 | |||
| 157 | Session::clear('readingMode'); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Construct a new Versioned object. |
||
| 162 | * |
||
| 163 | * @param array $stages The different stages the versioned object can be. The first stage is considered the |
||
| 164 | * 'default' stage, the last stage is considered the 'live' stage. |
||
| 165 | */ |
||
| 166 | public function __construct($stages = array('Stage','Live')) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Amend freshly created DataQuery objects with versioned-specific information. |
||
| 180 | * |
||
| 181 | * @param SQLQuery $query |
||
| 182 | * @param DataQuery $dataQuery |
||
| 183 | */ |
||
| 184 | public function augmentDataQueryCreation(SQLQuery &$query, DataQuery &$dataQuery) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Augment the the SQLQuery that is created by the DataQuery. |
||
| 201 | * @todo Should this all go into VersionedDataQuery? |
||
| 202 | * |
||
| 203 | * @param SQLQuery $query |
||
| 204 | * @param DataQuery $dataQuery |
||
| 205 | */ |
||
| 206 | public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * For lazy loaded fields requiring extra SQL manipulation, ie versioning. |
||
| 348 | * |
||
| 349 | * @param SQLQuery $query |
||
| 350 | * @param DataQuery $dataQuery |
||
| 351 | * @param DataObject $dataObject |
||
| 352 | */ |
||
| 353 | public function augmentLoadLazyFields(SQLQuery &$query, DataQuery &$dataQuery = null, $dataObject) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Called by {@link SapphireTest} when the database is reset. |
||
| 375 | * |
||
| 376 | * @todo Reduce the coupling between this and SapphireTest, somehow. |
||
| 377 | */ |
||
| 378 | public static function on_db_reset() { |
||
| 389 | |||
| 390 | public function augmentDatabase() { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Helper for augmentDatabase() to find unique indexes and convert them to non-unique |
||
| 555 | * |
||
| 556 | * @param array $indexes The indexes to convert |
||
| 557 | * @return array $indexes |
||
| 558 | */ |
||
| 559 | private function uniqueToIndex($indexes) { |
||
| 560 | $unique_regex = '/unique/i'; |
||
| 561 | $results = array(); |
||
| 562 | foreach ($indexes as $key => $index) { |
||
| 563 | $results[$key] = $index; |
||
| 564 | |||
| 565 | // support string descriptors |
||
| 566 | if (is_string($index)) { |
||
| 567 | if (preg_match($unique_regex, $index)) { |
||
| 568 | $results[$key] = preg_replace($unique_regex, 'index', $index); |
||
| 569 | } |
||
| 570 | } |
||
| 571 | |||
| 572 | // canonical, array-based descriptors |
||
| 573 | elseif (is_array($index)) { |
||
| 574 | if (strtolower($index['type']) == 'unique') { |
||
| 575 | $results[$key]['type'] = 'index'; |
||
| 576 | } |
||
| 577 | } |
||
| 578 | } |
||
| 579 | return $results; |
||
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Generates a ($table)_version DB manipulation and injects it into the current $manipulation |
||
| 584 | * |
||
| 585 | * @param SQLQuery $manipulation The query to augment |
||
| 586 | */ |
||
| 587 | protected function augmentWriteVersioned(&$manipulation, $table, $recordID) { |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Rewrite the given manipulation to update the selected (non-default) stage |
||
| 643 | * |
||
| 644 | * @param array $manipulation Source manipulation data |
||
| 645 | * @param string $table Name of table |
||
| 646 | * @param int $recordID ID of record to version |
||
| 647 | */ |
||
| 648 | protected function augmentWriteStaged(&$manipulation, $table, $recordID) { |
||
| 649 | // If the record has already been inserted in the (table), get rid of it. |
||
| 650 | if($manipulation[$table]['command'] == 'insert') { |
||
| 651 | DB::prepared_query( |
||
| 652 | "DELETE FROM \"{$table}\" WHERE \"ID\" = ?", |
||
| 653 | array($recordID) |
||
| 654 | ); |
||
| 655 | } |
||
| 656 | |||
| 657 | $newTable = $table . '_' . Versioned::current_stage(); |
||
| 658 | $manipulation[$newTable] = $manipulation[$table]; |
||
| 659 | unset($manipulation[$table]); |
||
| 660 | } |
||
| 661 | |||
| 662 | |||
| 663 | public function augmentWrite(&$manipulation) { |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Perform a write without affecting the version table. |
||
| 736 | * |
||
| 737 | * @return int The ID of the written record |
||
| 738 | */ |
||
| 739 | public function writeWithoutVersion() { |
||
| 744 | |||
| 745 | public function onAfterWrite() { |
||
| 746 | $this->_nextWriteWithoutVersion = false; |
||
| 747 | } |
||
| 748 | |||
| 749 | /** |
||
| 750 | * If a write was skipped, then we need to ensure that we don't leave a migrateVersion() value lying around for the |
||
| 751 | * next write. |
||
| 752 | */ |
||
| 753 | public function onAfterSkippedWrite() { |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Extend permissions to include additional security for objects that are not published to live. |
||
| 759 | * |
||
| 760 | * @param Member $member |
||
| 761 | * @return bool|null |
||
| 762 | */ |
||
| 763 | public function canView($member = null) { |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Determine if there are any additional restrictions on this object for the given reading version. |
||
| 772 | * |
||
| 773 | * Override this in a subclass to customise any additional effect that Versioned applies to canView. |
||
| 774 | * |
||
| 775 | * This is expected to be called by canView, and thus is only responsible for denying access if |
||
| 776 | * the default canView would otherwise ALLOW access. Thus it should not be called in isolation |
||
| 777 | * as an authoritative permission check. |
||
| 778 | * |
||
| 779 | * This has the following extension points: |
||
| 780 | * - canViewDraft is invoked if Mode = stage and Stage = stage |
||
| 781 | * - canViewArchived is invoked if Mode = archive |
||
| 782 | * |
||
| 783 | * @param Member $member |
||
| 784 | * @return bool False is returned if the current viewing mode denies visibility |
||
| 785 | */ |
||
| 786 | public function canViewVersioned($member = null) { |
||
| 787 | // Bypass when live stage |
||
| 788 | $mode = $this->owner->getSourceQueryParam("Versioned.mode"); |
||
| 789 | $stage = $this->owner->getSourceQueryParam("Versioned.stage"); |
||
| 790 | if ($mode === 'stage' && $stage === static::get_live_stage()) { |
||
| 791 | return true; |
||
| 792 | } |
||
| 793 | |||
| 794 | // Bypass if site is unsecured |
||
| 795 | if (Session::get('unsecuredDraftSite')) { |
||
| 796 | return true; |
||
| 797 | } |
||
| 798 | |||
| 799 | // If there are less than 2 stages, we can exit early since comparing stages is not needed |
||
| 800 | if(count($this->stages) < 2){ |
||
| 801 | return true; |
||
| 802 | } |
||
| 803 | |||
| 804 | // If we weren't definitely loaded from live, and we can't view non-live content, we need to |
||
| 805 | // check to make sure this version is the live version and so can be viewed. |
||
| 806 | $latestVersion = Versioned::get_versionnumber_by_stage($this->owner->class, $this->liveStage, $this->owner->ID); |
||
| 807 | if ($latestVersion == $this->owner->Version) { |
||
| 808 | // Even if this is loaded from a non-live stage, this is the live version |
||
| 809 | return true; |
||
| 810 | } |
||
| 811 | |||
| 812 | // Extend versioned behaviour |
||
| 813 | $extended = $this->owner->extendedCan('canViewNonLive', $member); |
||
| 814 | if($extended !== null) { |
||
| 815 | return (bool)$extended; |
||
| 816 | } |
||
| 817 | |||
| 818 | // Fall back to default permission check |
||
| 819 | $permissions = Config::inst()->get($this->owner->class, 'non_live_permissions', Config::FIRST_SET); |
||
| 820 | $check = Permission::checkMember($member, $permissions); |
||
| 821 | return (bool)$check; |
||
| 822 | } |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Determines canView permissions for the latest version of this object on a specific stage. |
||
| 826 | * Usually the stage is read from {@link Versioned::current_stage()}. |
||
| 827 | * |
||
| 828 | * This method should be invoked by user code to check if a record is visible in the given stage. |
||
| 829 | * |
||
| 830 | * This method should not be called via ->extend('canViewStage'), but rather should be |
||
| 831 | * overridden in the extended class. |
||
| 832 | * |
||
| 833 | * @param string $stage |
||
| 834 | * @param Member $member |
||
| 835 | * @return bool |
||
| 836 | */ |
||
| 837 | View Code Duplication | public function canViewStage($stage = 'Live', $member = null) { |
|
| 838 | $oldMode = Versioned::get_reading_mode(); |
||
| 839 | Versioned::reading_stage($stage); |
||
| 840 | |||
| 841 | $versionFromStage = DataObject::get($this->owner->class)->byID($this->owner->ID); |
||
| 842 | |||
| 843 | Versioned::set_reading_mode($oldMode); |
||
| 844 | return $versionFromStage ? $versionFromStage->canView($member) : false; |
||
| 845 | } |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Determine if a table supports the Versioned extensions (e.g. $table_versions does exists). |
||
| 849 | * |
||
| 850 | * @param string $table Table name |
||
| 851 | * @return bool |
||
| 852 | */ |
||
| 853 | public function canBeVersioned($table) { |
||
| 854 | return ClassInfo::exists($table) |
||
| 855 | && is_subclass_of($table, 'DataObject') |
||
| 856 | && DataObject::has_own_table($table); |
||
| 857 | } |
||
| 858 | |||
| 859 | /** |
||
| 860 | * Check if a certain table has the 'Version' field. |
||
| 861 | * |
||
| 862 | * @param string $table Table name |
||
| 863 | * @return bool |
||
| 864 | */ |
||
| 865 | public function hasVersionField($table) { |
||
| 876 | |||
| 877 | /** |
||
| 878 | * @param string $table |
||
| 879 | * @return string |
||
| 880 | */ |
||
| 881 | public function extendWithSuffix($table) { |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Get the latest published version of this object. |
||
| 900 | * |
||
| 901 | * @return DataObject |
||
| 902 | */ |
||
| 903 | public function latestPublished() { |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Move a database record from one stage to the other. |
||
| 919 | * |
||
| 920 | * @param string $fromStage Place to copy from. Can be either a stage name or a version number. |
||
| 921 | * @param string $toStage Place to copy to. Must be a stage name. |
||
| 922 | * @param bool $createNewVersion Set this to true to create a new version number. By default, the existing |
||
| 923 | * version number will be copied over. |
||
| 924 | */ |
||
| 925 | public function publish($fromStage, $toStage, $createNewVersion = false) { |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Set the migrating version. |
||
| 982 | * |
||
| 983 | * @param string $version |
||
| 984 | */ |
||
| 985 | public function migrateVersion($version) { |
||
| 986 | $this->migratingVersion = $version; |
||
| 987 | } |
||
| 988 | |||
| 989 | /** |
||
| 990 | * Compare two stages to see if they're different. Only checks the version numbers, not the actual content. |
||
| 991 | * |
||
| 992 | * @param string $stage1 The first stage to check |
||
| 993 | * @param string $stage2 The second stage to check |
||
| 994 | * @return bool |
||
| 995 | */ |
||
| 996 | public function stagesDiffer($stage1, $stage2) { |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Get a list of versions for this object, optionally with additional SQL parameters |
||
| 1019 | * |
||
| 1020 | * @param string $filter |
||
| 1021 | * @param string $sort |
||
| 1022 | * @param string $limit |
||
| 1023 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1024 | * @param string $having |
||
| 1025 | * @return DataList |
||
| 1026 | */ |
||
| 1027 | public function Versions($filter = "", $sort = "", $limit = "", $join = "", $having = "") { |
||
| 1028 | return $this->allVersions($filter, $sort, $limit, $join, $having); |
||
| 1029 | } |
||
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Get a list of versions for this object, optionally with additional SQL parameters |
||
| 1033 | * |
||
| 1034 | * @param string $filter |
||
| 1035 | * @param string $sort |
||
| 1036 | * @param string $limit |
||
| 1037 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1038 | * @param string $having |
||
| 1039 | * @return DataList |
||
| 1040 | */ |
||
| 1041 | public function allVersions($filter = "", $sort = "", $limit = "", $join = "", $having = "") { |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * Compare two version, and return the differences between them. |
||
| 1087 | * |
||
| 1088 | * @param string $from The version to compare from |
||
| 1089 | * @param string $to The version to compare to |
||
| 1090 | * @return DataObject |
||
| 1091 | */ |
||
| 1092 | public function compareVersions($from, $to) { |
||
| 1093 | $fromRecord = Versioned::get_version($this->owner->class, $this->owner->ID, $from); |
||
| 1094 | $toRecord = Versioned::get_version($this->owner->class, $this->owner->ID, $to); |
||
| 1095 | |||
| 1096 | $diff = new DataDifferencer($fromRecord, $toRecord); |
||
| 1097 | |||
| 1098 | return $diff->diffedData(); |
||
| 1099 | } |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * Return the base table - the class that directly extends DataObject. |
||
| 1103 | * |
||
| 1104 | * @param string $stage Override the stage used |
||
| 1105 | * @return string |
||
| 1106 | */ |
||
| 1107 | public function baseTable($stage = null) { |
||
| 1117 | |||
| 1118 | //-----------------------------------------------------------------------------------------------// |
||
| 1119 | |||
| 1120 | |||
| 1121 | /** |
||
| 1122 | * Determine if the current user is able to set the given site stage / archive |
||
| 1123 | * |
||
| 1124 | * @param SS_HTTPRequest $request |
||
| 1125 | * @return bool |
||
| 1126 | */ |
||
| 1127 | public static function can_choose_site_stage($request) { |
||
| 1128 | // Request is allowed if stage isn't being modified |
||
| 1129 | if((!$request->getVar('stage') || $request->getVar('stage') === static::get_live_stage()) |
||
| 1130 | && !$request->getVar('archiveDate') |
||
| 1131 | ) { |
||
| 1132 | return true; |
||
| 1133 | } |
||
| 1134 | |||
| 1135 | // Check permissions with member ID in session. |
||
| 1136 | $member = Member::currentUser(); |
||
| 1137 | $permissions = Config::inst()->get(get_called_class(), 'non_live_permissions'); |
||
| 1138 | return $member && Permission::checkMember($member, $permissions); |
||
| 1139 | } |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * Choose the stage the site is currently on: |
||
| 1143 | * - If $_GET['stage'] is set, then it will use that stage, and store it in the session. |
||
| 1144 | * - If $_GET['archiveDate'] is set, it will use that date, and store it in the session. |
||
| 1145 | * - If neither of these are set, it checks the session, otherwise the stage is set to 'Live'. |
||
| 1146 | * |
||
| 1147 | * @param Session $session Optional session within which to store the resulting stage |
||
| 1148 | */ |
||
| 1149 | public static function choose_site_stage() { |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Set the current reading mode. |
||
| 1193 | * |
||
| 1194 | * @param string $mode |
||
| 1195 | */ |
||
| 1196 | public static function set_reading_mode($mode) { |
||
| 1197 | Versioned::$reading_mode = $mode; |
||
| 1198 | } |
||
| 1199 | |||
| 1200 | /** |
||
| 1201 | * Get the current reading mode. |
||
| 1202 | * |
||
| 1203 | * @return string |
||
| 1204 | */ |
||
| 1205 | public static function get_reading_mode() { |
||
| 1206 | return Versioned::$reading_mode; |
||
| 1207 | } |
||
| 1208 | |||
| 1209 | /** |
||
| 1210 | * Get the name of the 'live' stage. |
||
| 1211 | * |
||
| 1212 | * @return string |
||
| 1213 | */ |
||
| 1214 | public static function get_live_stage() { |
||
| 1215 | return "Live"; |
||
| 1216 | } |
||
| 1217 | |||
| 1218 | /** |
||
| 1219 | * Get the current reading stage. |
||
| 1220 | * |
||
| 1221 | * @return string |
||
| 1222 | */ |
||
| 1223 | public static function current_stage() { |
||
| 1224 | $parts = explode('.', Versioned::get_reading_mode()); |
||
| 1225 | |||
| 1226 | if($parts[0] == 'Stage') { |
||
| 1227 | return $parts[1]; |
||
| 1228 | } |
||
| 1229 | } |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Get the current archive date. |
||
| 1233 | * |
||
| 1234 | * @return string |
||
| 1235 | */ |
||
| 1236 | public static function current_archived_date() { |
||
| 1237 | $parts = explode('.', Versioned::get_reading_mode()); |
||
| 1238 | if($parts[0] == 'Archive') return $parts[1]; |
||
| 1239 | } |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Set the reading stage. |
||
| 1243 | * |
||
| 1244 | * @param string $stage |
||
| 1245 | */ |
||
| 1246 | public static function reading_stage($stage) { |
||
| 1247 | Versioned::set_reading_mode('Stage.' . $stage); |
||
| 1248 | } |
||
| 1249 | |||
| 1250 | /** |
||
| 1251 | * Set the reading archive date. |
||
| 1252 | * |
||
| 1253 | * @param string $date |
||
| 1254 | */ |
||
| 1255 | public static function reading_archived_date($date) { |
||
| 1256 | Versioned::set_reading_mode('Archive.' . $date); |
||
| 1257 | } |
||
| 1258 | |||
| 1259 | /** |
||
| 1260 | * Get a singleton instance of a class in the given stage. |
||
| 1261 | * |
||
| 1262 | * @param string $class The name of the class |
||
| 1263 | * @param string $stage The name of the stage |
||
| 1264 | * @param string $filter A filter to be inserted into the WHERE clause |
||
| 1265 | * @param bool $cache Whether to load from the cache instead of fresh from the database |
||
| 1266 | * @param string $sort A sort expression to be inserted into the ORDER BY clause. |
||
| 1267 | * |
||
| 1268 | * @return DataObject |
||
| 1269 | */ |
||
| 1270 | public static function get_one_by_stage($class, $stage, $filter = '', $cache = true, $sort = '') { |
||
| 1276 | |||
| 1277 | /** |
||
| 1278 | * Gets the current version number of a specific record. |
||
| 1279 | * |
||
| 1280 | * @param string $class The classname of the desired object |
||
| 1281 | * @param string $stage The name of the stage to load from |
||
| 1282 | * @param int $id The object's ID |
||
| 1283 | * @param bool $cache Whether to load from the cache instead of fresh from the database |
||
| 1284 | * |
||
| 1285 | * @return int |
||
| 1286 | */ |
||
| 1287 | public static function get_versionnumber_by_stage($class, $stage, $id, $cache = true) { |
||
| 1314 | |||
| 1315 | /** |
||
| 1316 | * Prepopulate the cache for Versioned::get_versionnumber_by_stage() for a list of record IDs, for more efficient |
||
| 1317 | * database querying. If $idList is null, then every object will be pre-cached. |
||
| 1318 | * |
||
| 1319 | * @param string $class The object class to prepopulate version numbers for |
||
| 1320 | * @param string $stage The stage to prepopulate version numbers from |
||
| 1321 | * @param array $idList A whitelist of IDs to use when prepopulating |
||
| 1322 | */ |
||
| 1323 | public static function prepopulate_versionnumber_cache($class, $stage, $idList = null) { |
||
| 1350 | |||
| 1351 | /** |
||
| 1352 | * Get a set of class instances by the given stage. |
||
| 1353 | * |
||
| 1354 | * @param string $class The name of the class. |
||
| 1355 | * @param string $stage The name of the stage. |
||
| 1356 | * @param string $filter A filter to be inserted into the WHERE clause. |
||
| 1357 | * @param string $sort A sort expression to be inserted into the ORDER BY clause. |
||
| 1358 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1359 | * @param string|int $limit A limit on the number of records returned from the database. |
||
| 1360 | * @param string $containerClass The container class for the result set (default is DataList) |
||
| 1361 | * |
||
| 1362 | * @return DataList A modified DataList designated to the specified stage |
||
| 1363 | */ |
||
| 1364 | public static function get_by_stage($class, $stage, $filter = '', $sort = '', $join = '', $limit = '', |
||
| 1365 | $containerClass = 'DataList') { |
||
| 1366 | |||
| 1367 | $result = DataObject::get($class, $filter, $sort, $join, $limit, $containerClass); |
||
| 1368 | return $result->setDataQueryParam(array( |
||
| 1369 | 'Versioned.mode' => 'stage', |
||
| 1370 | 'Versioned.stage' => $stage |
||
| 1371 | )); |
||
| 1372 | } |
||
| 1373 | |||
| 1374 | /** |
||
| 1375 | * Delete this item from the specified stage. |
||
| 1376 | * |
||
| 1377 | * @param string $stage |
||
| 1378 | */ |
||
| 1379 | public function deleteFromStage($stage) { |
||
| 1392 | |||
| 1393 | /** |
||
| 1394 | * Write this item to the specified stage. |
||
| 1395 | * |
||
| 1396 | * @param string $stage The stage to write this item to |
||
| 1397 | * @param bool $forceInsert Whether to force an INSERT query over an UPDATE query |
||
| 1398 | * @return int The ID of the item being written |
||
| 1399 | */ |
||
| 1400 | View Code Duplication | public function writeToStage($stage, $forceInsert = false) { |
|
| 1410 | |||
| 1411 | /** |
||
| 1412 | * Roll the draft version of this object to match the published one. |
||
| 1413 | * |
||
| 1414 | * Caution: Doesn't overwrite the object properties with the rolled back version. |
||
| 1415 | * |
||
| 1416 | * @param string|int $version Either the string 'Live' or a version number |
||
| 1417 | */ |
||
| 1418 | public function doRollbackTo($version) { |
||
| 1419 | $this->owner->extend('onBeforeRollback', $version); |
||
| 1420 | $this->publish($version, "Stage", true); |
||
| 1421 | |||
| 1422 | $this->owner->writeWithoutVersion(); |
||
| 1423 | |||
| 1424 | $this->owner->extend('onAfterRollback', $version); |
||
| 1425 | } |
||
| 1426 | |||
| 1427 | /** |
||
| 1428 | * Return the latest version of the given object. |
||
| 1429 | * |
||
| 1430 | * @param string $class The classname of the object to lookup |
||
| 1431 | * @param string $id The object of the ID to retrieve |
||
| 1432 | * @return DataObject |
||
| 1433 | */ |
||
| 1434 | View Code Duplication | public static function get_latest_version($class, $id) { |
|
| 1442 | |||
| 1443 | /** |
||
| 1444 | * Returns whether the current record is the latest one. |
||
| 1445 | * |
||
| 1446 | * @todo Performance - could do this directly via SQL. |
||
| 1447 | * |
||
| 1448 | * @see get_latest_version() |
||
| 1449 | * @see latestPublished |
||
| 1450 | * |
||
| 1451 | * @return bool |
||
| 1452 | */ |
||
| 1453 | public function isLatestVersion() { |
||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Return the equivalent of a DataList::create() call, querying the latest version of each object stored in the |
||
| 1464 | * (class)_versions tables. In particular, this will query deleted records as well as active ones. |
||
| 1465 | * |
||
| 1466 | * @param string $class The type of object to lookup |
||
| 1467 | * @param string $filter An optional SQL comparison to add to the WHERE clause |
||
| 1468 | * @param string $sort An optional SQL statement to add to the SORT clause |
||
| 1469 | */ |
||
| 1470 | public static function get_including_deleted($class, $filter = "", $sort = "") { |
||
| 1478 | |||
| 1479 | /** |
||
| 1480 | * Return the specific version of the given ID. |
||
| 1481 | * |
||
| 1482 | * Caution: The record is retrieved as a DataObject, but saving back modifications via write() will create a new |
||
| 1483 | * version, rather than modifying the existing one. |
||
| 1484 | * |
||
| 1485 | * @param string $class The type of object to lookup |
||
| 1486 | * @param int $id The ID of the object to retrieve |
||
| 1487 | * @param int $version The desired version of the object |
||
| 1488 | * @return DataObject |
||
| 1489 | */ |
||
| 1490 | View Code Duplication | public static function get_version($class, $id, $version) { |
|
| 1499 | |||
| 1500 | /** |
||
| 1501 | * Return a list of all versions for a given id. |
||
| 1502 | * |
||
| 1503 | * @param string $class The type of object to lookup |
||
| 1504 | * @param int $id The ID of the object to retrieve |
||
| 1505 | * |
||
| 1506 | * @return DataList |
||
| 1507 | */ |
||
| 1508 | public static function get_all_versions($class, $id) { |
||
| 1516 | |||
| 1517 | /** |
||
| 1518 | * @param array $labels |
||
| 1519 | */ |
||
| 1520 | public function updateFieldLabels(&$labels) { |
||
| 1521 | $labels['Versions'] = _t('Versioned.has_many_Versions', 'Versions', 'Past Versions of this page'); |
||
| 1522 | } |
||
| 1523 | |||
| 1524 | public function updateCMSFields(FieldList $fields) { |
||
| 1525 | // remove the version field from the CMS as this should be left |
||
| 1526 | // entirely up to the extension (not the cms user). |
||
| 1527 | $fields->removeByName('Version'); |
||
| 1528 | } |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Ensure version ID is reset to 0 on duplicate |
||
| 1532 | * |
||
| 1533 | * @param DataObject $source Record this was duplicated from |
||
| 1534 | * @param bool $doWrite |
||
| 1535 | */ |
||
| 1536 | public function onBeforeDuplicate($source, $doWrite) { |
||
| 1537 | $this->owner->Version = 0; |
||
| 1538 | } |
||
| 1539 | |||
| 1540 | /** |
||
| 1541 | * Clear the cached version numbers from previous queries. |
||
| 1542 | */ |
||
| 1543 | public function flushCache() { |
||
| 1544 | self::$cache_versionnumber = array(); |
||
| 1545 | } |
||
| 1546 | |||
| 1547 | /** |
||
| 1548 | * Returns a piece of text to keep DataObject cache keys appropriately specific. |
||
| 1549 | * |
||
| 1550 | * @return string |
||
| 1551 | */ |
||
| 1552 | public function cacheKeyComponent() { |
||
| 1555 | |||
| 1556 | /** |
||
| 1557 | * Returns an array of possible stages. |
||
| 1558 | * |
||
| 1559 | * @return array |
||
| 1560 | */ |
||
| 1561 | public function getVersionedStages() { |
||
| 1562 | return $this->stages; |
||
| 1563 | } |
||
| 1564 | |||
| 1565 | /** |
||
| 1566 | * @return string |
||
| 1567 | */ |
||
| 1568 | public function getDefaultStage() { |
||
| 1569 | return $this->defaultStage; |
||
| 1570 | } |
||
| 1571 | |||
| 1572 | public static function get_template_global_variables() { |
||
| 1573 | return array( |
||
| 1574 | 'CurrentReadingMode' => 'get_reading_mode' |
||
| 1575 | ); |
||
| 1576 | } |
||
| 1577 | } |
||
| 1578 | |||
| 1579 | /** |
||
| 1580 | * Represents a single version of a record. |
||
| 1581 | * |
||
| 1582 | * @package framework |
||
| 1683 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.