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 |
||
| 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() { |
||
| 214 | self::$reading_mode = ''; |
||
| 215 | Session::clear('readingMode'); |
||
| 216 | } |
||
| 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) { |
||
|
|
|||
| 226 | $parts = explode('.', Versioned::get_reading_mode()); |
||
| 227 | |||
| 228 | if($parts[0] == 'Archive') { |
||
| 229 | $dataQuery->setQueryParam('Versioned.mode', 'archive'); |
||
| 230 | $dataQuery->setQueryParam('Versioned.date', $parts[1]); |
||
| 231 | } else if($parts[0] == 'Stage' && $this->hasStages()) { |
||
| 232 | $dataQuery->setQueryParam('Versioned.mode', 'stage'); |
||
| 233 | $dataQuery->setQueryParam('Versioned.stage', $parts[1]); |
||
| 234 | } |
||
| 235 | } |
||
| 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) { |
||
| 243 | parent::__construct(); |
||
| 244 | |||
| 245 | // Handle deprecated behaviour |
||
| 246 | if($mode === 'Stage' && func_num_args() === 1) { |
||
| 247 | Deprecation::notice("5.0", "Versioned now takes a mode as a single parameter"); |
||
| 248 | $mode = static::VERSIONED; |
||
| 249 | } elseif(is_array($mode) || func_num_args() > 1) { |
||
| 250 | Deprecation::notice("5.0", "Versioned now takes a mode as a single parameter"); |
||
| 251 | $mode = func_num_args() > 1 || count($mode) > 1 |
||
| 252 | ? static::STAGEDVERSIONED |
||
| 253 | : static::VERSIONED; |
||
| 254 | } |
||
| 255 | |||
| 256 | if(!in_array($mode, array(static::STAGEDVERSIONED, static::VERSIONED))) { |
||
| 257 | throw new InvalidArgumentException("Invalid mode: {$mode}"); |
||
| 258 | } |
||
| 259 | |||
| 260 | $this->mode = $mode; |
||
| 261 | } |
||
| 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) { |
||
| 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) { |
||
| 349 | if(!$dataQuery || !$dataQuery->getQueryParam('Versioned.mode')) { |
||
| 350 | return; |
||
| 351 | } |
||
| 352 | |||
| 353 | $baseTable = ClassInfo::baseDataClass($dataQuery->dataClass()); |
||
| 354 | |||
| 355 | $versionedMode = $dataQuery->getQueryParam('Versioned.mode'); |
||
| 356 | switch($versionedMode) { |
||
| 357 | // Reading a specific stage (Stage or Live) |
||
| 358 | case 'stage': |
||
| 359 | // Check if we need to rewrite this table |
||
| 360 | $stage = $dataQuery->getQueryParam('Versioned.stage'); |
||
| 361 | if(!$this->hasStages() || $stage === static::DRAFT) { |
||
| 362 | break; |
||
| 363 | } |
||
| 364 | // Rewrite all tables to select from the live version |
||
| 365 | foreach($query->getFrom() as $table => $dummy) { |
||
| 366 | if(!$this->isTableVersioned($table)) { |
||
| 367 | continue; |
||
| 368 | } |
||
| 369 | $stageTable = $this->stageTable($table, $stage); |
||
| 370 | $query->renameTable($table, $stageTable); |
||
| 371 | } |
||
| 372 | break; |
||
| 373 | |||
| 374 | // Reading a specific stage, but only return items that aren't in any other stage |
||
| 375 | case 'stage_unique': |
||
| 376 | if(!$this->hasStages()) { |
||
| 377 | break; |
||
| 378 | } |
||
| 379 | |||
| 380 | $stage = $dataQuery->getQueryParam('Versioned.stage'); |
||
| 381 | // Recurse to do the default stage behavior (must be first, we rely on stage renaming happening before |
||
| 382 | // below) |
||
| 383 | $dataQuery->setQueryParam('Versioned.mode', 'stage'); |
||
| 384 | $this->augmentSQL($query, $dataQuery); |
||
| 385 | $dataQuery->setQueryParam('Versioned.mode', 'stage_unique'); |
||
| 386 | |||
| 387 | // Now exclude any ID from any other stage. Note that we double rename to avoid the regular stage rename |
||
| 388 | // renaming all subquery references to be Versioned.stage |
||
| 389 | foreach([static::DRAFT, static::LIVE] as $excluding) { |
||
| 390 | if ($excluding == $stage) { |
||
| 391 | continue; |
||
| 392 | } |
||
| 393 | |||
| 394 | $tempName = 'ExclusionarySource_'.$excluding; |
||
| 395 | $excludingTable = $baseTable . ($excluding && $excluding != static::DRAFT ? "_$excluding" : ''); |
||
| 396 | |||
| 397 | $query->addWhere('"'.$baseTable.'"."ID" NOT IN (SELECT "ID" FROM "'.$tempName.'")'); |
||
| 398 | $query->renameTable($tempName, $excludingTable); |
||
| 399 | } |
||
| 400 | break; |
||
| 401 | |||
| 402 | // Return all version instances |
||
| 403 | case 'archive': |
||
| 404 | case 'all_versions': |
||
| 405 | case 'latest_versions': |
||
| 406 | case 'version': |
||
| 407 | foreach($query->getFrom() as $alias => $join) { |
||
| 408 | if(!$this->isTableVersioned($alias)) { |
||
| 409 | continue; |
||
| 410 | } |
||
| 411 | |||
| 412 | if($alias != $baseTable) { |
||
| 413 | // Make sure join includes version as well |
||
| 414 | $query->setJoinFilter( |
||
| 415 | $alias, |
||
| 416 | "\"{$alias}_versions\".\"RecordID\" = \"{$baseTable}_versions\".\"RecordID\"" |
||
| 417 | . " AND \"{$alias}_versions\".\"Version\" = \"{$baseTable}_versions\".\"Version\"" |
||
| 418 | ); |
||
| 419 | } |
||
| 420 | $query->renameTable($alias, $alias . '_versions'); |
||
| 421 | } |
||
| 422 | |||
| 423 | // Add all <basetable>_versions columns |
||
| 424 | View Code Duplication | foreach(Config::inst()->get('Versioned', 'db_for_versions_table') as $name => $type) { |
|
| 425 | $query->selectField(sprintf('"%s_versions"."%s"', $baseTable, $name), $name); |
||
| 426 | } |
||
| 427 | |||
| 428 | // Alias the record ID as the row ID, and ensure ID filters are aliased correctly |
||
| 429 | $query->selectField("\"{$baseTable}_versions\".\"RecordID\"", "ID"); |
||
| 430 | $query->replaceText("\"{$baseTable}_versions\".\"ID\"", "\"{$baseTable}_versions\".\"RecordID\""); |
||
| 431 | |||
| 432 | // However, if doing count, undo rewrite of "ID" column |
||
| 433 | $query->replaceText( |
||
| 434 | "count(DISTINCT \"{$baseTable}_versions\".\"RecordID\")", |
||
| 435 | "count(DISTINCT \"{$baseTable}_versions\".\"ID\")" |
||
| 436 | ); |
||
| 437 | |||
| 438 | // Add additional versioning filters |
||
| 439 | switch($versionedMode) { |
||
| 440 | case 'archive': { |
||
| 441 | $date = $dataQuery->getQueryParam('Versioned.date'); |
||
| 442 | if(!$date) { |
||
| 443 | throw new InvalidArgumentException("Invalid archive date"); |
||
| 444 | } |
||
| 445 | // Link to the version archived on that date |
||
| 446 | $query->addWhere([ |
||
| 447 | "\"{$baseTable}_versions\".\"Version\" IN |
||
| 448 | (SELECT LatestVersion FROM |
||
| 449 | (SELECT |
||
| 450 | \"{$baseTable}_versions\".\"RecordID\", |
||
| 451 | MAX(\"{$baseTable}_versions\".\"Version\") AS LatestVersion |
||
| 452 | FROM \"{$baseTable}_versions\" |
||
| 453 | WHERE \"{$baseTable}_versions\".\"LastEdited\" <= ? |
||
| 454 | GROUP BY \"{$baseTable}_versions\".\"RecordID\" |
||
| 455 | ) AS \"{$baseTable}_versions_latest\" |
||
| 456 | WHERE \"{$baseTable}_versions_latest\".\"RecordID\" = \"{$baseTable}_versions\".\"RecordID\" |
||
| 457 | )" => $date |
||
| 458 | ]); |
||
| 459 | break; |
||
| 460 | } |
||
| 461 | case 'latest_versions': { |
||
| 462 | // Return latest version instances, regardless of whether they are on a particular stage |
||
| 463 | // This provides "show all, including deleted" functonality |
||
| 464 | $query->addWhere( |
||
| 465 | "\"{$baseTable}_versions\".\"Version\" IN |
||
| 466 | (SELECT LatestVersion FROM |
||
| 467 | (SELECT |
||
| 468 | \"{$baseTable}_versions\".\"RecordID\", |
||
| 469 | MAX(\"{$baseTable}_versions\".\"Version\") AS LatestVersion |
||
| 470 | FROM \"{$baseTable}_versions\" |
||
| 471 | GROUP BY \"{$baseTable}_versions\".\"RecordID\" |
||
| 472 | ) AS \"{$baseTable}_versions_latest\" |
||
| 473 | WHERE \"{$baseTable}_versions_latest\".\"RecordID\" = \"{$baseTable}_versions\".\"RecordID\" |
||
| 474 | )" |
||
| 475 | ); |
||
| 476 | break; |
||
| 477 | } |
||
| 478 | case 'version': { |
||
| 479 | // If selecting a specific version, filter it here |
||
| 480 | $version = $dataQuery->getQueryParam('Versioned.version'); |
||
| 481 | if(!$version) { |
||
| 482 | throw new InvalidArgumentException("Invalid version"); |
||
| 483 | } |
||
| 484 | $query->addWhere([ |
||
| 485 | "\"{$baseTable}_versions\".\"Version\"" => $version |
||
| 486 | ]); |
||
| 487 | break; |
||
| 488 | } |
||
| 489 | default: { |
||
| 490 | // If all versions are requested, ensure that records are sorted by this field |
||
| 491 | $query->addOrderBy(sprintf('"%s_versions"."%s"', $baseTable, 'Version')); |
||
| 492 | break; |
||
| 493 | } |
||
| 494 | } |
||
| 495 | break; |
||
| 496 | default: |
||
| 497 | throw new InvalidArgumentException("Bad value for query parameter Versioned.mode: " |
||
| 498 | . $dataQuery->getQueryParam('Versioned.mode')); |
||
| 499 | } |
||
| 500 | } |
||
| 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) { |
||
| 525 | // The VersionedMode local variable ensures that this decorator only applies to |
||
| 526 | // queries that have originated from the Versioned object, and have the Versioned |
||
| 527 | // metadata set on the query object. This prevents regular queries from |
||
| 528 | // accidentally querying the *_versions tables. |
||
| 529 | $versionedMode = $dataObject->getSourceQueryParam('Versioned.mode'); |
||
| 530 | $dataClass = ClassInfo::baseDataClass($dataQuery->dataClass()); |
||
| 531 | $modesToAllowVersioning = array('all_versions', 'latest_versions', 'archive', 'version'); |
||
| 532 | if( |
||
| 533 | !empty($dataObject->Version) && |
||
| 534 | (!empty($versionedMode) && in_array($versionedMode,$modesToAllowVersioning)) |
||
| 535 | ) { |
||
| 536 | // This will ensure that augmentSQL will select only the same version as the owner, |
||
| 537 | // regardless of how this object was initially selected |
||
| 538 | $dataQuery->where([ |
||
| 539 | "\"$dataClass\".\"Version\"" => $dataObject->Version |
||
| 540 | ]); |
||
| 541 | $dataQuery->setQueryParam('Versioned.mode', 'all_versions'); |
||
| 542 | } |
||
| 543 | } |
||
| 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() { |
||
| 552 | // Drop all temporary tables |
||
| 553 | $db = DB::get_conn(); |
||
| 554 | foreach(static::$archive_tables as $tableName) { |
||
| 555 | if(method_exists($db, 'dropTable')) $db->dropTable($tableName); |
||
| 556 | else $db->query("DROP TABLE \"$tableName\""); |
||
| 557 | } |
||
| 558 | |||
| 559 | // Remove references to them |
||
| 560 | static::$archive_tables = array(); |
||
| 561 | } |
||
| 562 | |||
| 563 | public function augmentDatabase() { |
||
| 564 | $owner = $this->owner; |
||
| 565 | $classTable = $owner->class; |
||
| 566 | |||
| 567 | $isRootClass = ($owner->class == ClassInfo::baseDataClass($owner->class)); |
||
| 568 | |||
| 569 | // Build a list of suffixes whose tables need versioning |
||
| 570 | $allSuffixes = array(); |
||
| 571 | $versionableExtensions = $owner->config()->versionableExtensions; |
||
| 572 | if(count($versionableExtensions)){ |
||
| 573 | foreach ($versionableExtensions as $versionableExtension => $suffixes) { |
||
| 574 | if ($owner->hasExtension($versionableExtension)) { |
||
| 575 | $allSuffixes = array_merge($allSuffixes, (array)$suffixes); |
||
| 576 | foreach ((array)$suffixes as $suffix) { |
||
| 577 | $allSuffixes[$suffix] = $versionableExtension; |
||
| 578 | } |
||
| 579 | } |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 583 | // Add the default table with an empty suffix to the list (table name = class name) |
||
| 584 | array_push($allSuffixes,''); |
||
| 585 | |||
| 586 | foreach ($allSuffixes as $key => $suffix) { |
||
| 587 | // check that this is a valid suffix |
||
| 588 | if (!is_int($key)) continue; |
||
| 589 | |||
| 590 | if ($suffix) $table = "{$classTable}_$suffix"; |
||
| 591 | else $table = $classTable; |
||
| 592 | |||
| 593 | $fields = DataObject::database_fields($owner->class); |
||
| 594 | unset($fields['ID']); |
||
| 595 | if($fields) { |
||
| 596 | $options = Config::inst()->get($owner->class, 'create_table_options', Config::FIRST_SET); |
||
| 597 | $indexes = $owner->databaseIndexes(); |
||
| 598 | if ($suffix && ($ext = $owner->getExtensionInstance($allSuffixes[$suffix]))) { |
||
| 599 | if (!$ext->isVersionedTable($table)) continue; |
||
| 600 | $ext->setOwner($owner); |
||
| 601 | $fields = $ext->fieldsInExtraTables($suffix); |
||
| 602 | $ext->clearOwner(); |
||
| 603 | $indexes = $fields['indexes']; |
||
| 604 | $fields = $fields['db']; |
||
| 605 | } |
||
| 606 | |||
| 607 | // Create tables for other stages |
||
| 608 | if($this->hasStages()) { |
||
| 609 | // Extra tables for _Live, etc. |
||
| 610 | // Change unique indexes to 'index'. Versioned tables may run into unique indexing difficulties |
||
| 611 | // otherwise. |
||
| 612 | $liveTable = $this->stageTable($table, static::LIVE); |
||
| 613 | $indexes = $this->uniqueToIndex($indexes); |
||
| 614 | DB::require_table($liveTable, $fields, $indexes, false, $options); |
||
| 615 | } |
||
| 616 | |||
| 617 | if($isRootClass) { |
||
| 618 | // Create table for all versions |
||
| 619 | $versionFields = array_merge( |
||
| 620 | Config::inst()->get('Versioned', 'db_for_versions_table'), |
||
| 621 | (array)$fields |
||
| 622 | ); |
||
| 623 | |||
| 624 | $versionIndexes = array_merge( |
||
| 625 | Config::inst()->get('Versioned', 'indexes_for_versions_table'), |
||
| 626 | (array)$indexes |
||
| 627 | ); |
||
| 628 | } else { |
||
| 629 | // Create fields for any tables of subclasses |
||
| 630 | $versionFields = array_merge( |
||
| 631 | array( |
||
| 632 | "RecordID" => "Int", |
||
| 633 | "Version" => "Int", |
||
| 634 | ), |
||
| 635 | (array)$fields |
||
| 636 | ); |
||
| 637 | |||
| 638 | //Unique indexes will not work on versioned tables, so we'll convert them to standard indexes: |
||
| 639 | $indexes = $this->uniqueToIndex($indexes); |
||
| 640 | $versionIndexes = array_merge( |
||
| 641 | array( |
||
| 642 | 'RecordID_Version' => array('type' => 'unique', 'value' => '"RecordID","Version"'), |
||
| 643 | 'RecordID' => true, |
||
| 644 | 'Version' => true, |
||
| 645 | ), |
||
| 646 | (array)$indexes |
||
| 647 | ); |
||
| 648 | } |
||
| 649 | |||
| 650 | if(DB::get_schema()->hasTable("{$table}_versions")) { |
||
| 651 | // Fix data that lacks the uniqueness constraint (since this was added later and |
||
| 652 | // bugs meant that the constraint was validated) |
||
| 653 | $duplications = DB::query("SELECT MIN(\"ID\") AS \"ID\", \"RecordID\", \"Version\" |
||
| 654 | FROM \"{$table}_versions\" GROUP BY \"RecordID\", \"Version\" |
||
| 655 | HAVING COUNT(*) > 1"); |
||
| 656 | |||
| 657 | foreach($duplications as $dup) { |
||
| 658 | DB::alteration_message("Removing {$table}_versions duplicate data for " |
||
| 659 | ."{$dup['RecordID']}/{$dup['Version']}" ,"deleted"); |
||
| 660 | DB::prepared_query( |
||
| 661 | "DELETE FROM \"{$table}_versions\" WHERE \"RecordID\" = ? |
||
| 662 | AND \"Version\" = ? AND \"ID\" != ?", |
||
| 663 | array($dup['RecordID'], $dup['Version'], $dup['ID']) |
||
| 664 | ); |
||
| 665 | } |
||
| 666 | |||
| 667 | // Remove junk which has no data in parent classes. Only needs to run the following |
||
| 668 | // when versioned data is spread over multiple tables |
||
| 669 | if(!$isRootClass && ($versionedTables = ClassInfo::dataClassesFor($table))) { |
||
| 670 | |||
| 671 | foreach($versionedTables as $child) { |
||
| 672 | if($table === $child) break; // only need subclasses |
||
| 673 | |||
| 674 | // Select all orphaned version records |
||
| 675 | $orphanedQuery = SQLSelect::create() |
||
| 676 | ->selectField("\"{$table}_versions\".\"ID\"") |
||
| 677 | ->setFrom("\"{$table}_versions\""); |
||
| 678 | |||
| 679 | // If we have a parent table limit orphaned records |
||
| 680 | // to only those that exist in this |
||
| 681 | if(DB::get_schema()->hasTable("{$child}_versions")) { |
||
| 682 | $orphanedQuery |
||
| 683 | ->addLeftJoin( |
||
| 684 | "{$child}_versions", |
||
| 685 | "\"{$child}_versions\".\"RecordID\" = \"{$table}_versions\".\"RecordID\" |
||
| 686 | AND \"{$child}_versions\".\"Version\" = \"{$table}_versions\".\"Version\"" |
||
| 687 | ) |
||
| 688 | ->addWhere("\"{$child}_versions\".\"ID\" IS NULL"); |
||
| 689 | } |
||
| 690 | |||
| 691 | $count = $orphanedQuery->count(); |
||
| 692 | if($count > 0) { |
||
| 693 | DB::alteration_message("Removing {$count} orphaned versioned records", "deleted"); |
||
| 694 | $ids = $orphanedQuery->execute()->column(); |
||
| 695 | foreach($ids as $id) { |
||
| 696 | DB::prepared_query( |
||
| 697 | "DELETE FROM \"{$table}_versions\" WHERE \"ID\" = ?", |
||
| 698 | array($id) |
||
| 699 | ); |
||
| 700 | } |
||
| 701 | } |
||
| 702 | } |
||
| 703 | } |
||
| 704 | } |
||
| 705 | |||
| 706 | DB::require_table("{$table}_versions", $versionFields, $versionIndexes, true, $options); |
||
| 707 | } else { |
||
| 708 | DB::dont_require_table("{$table}_versions"); |
||
| 709 | if($this->hasStages()) { |
||
| 710 | $liveTable = $this->stageTable($table, static::LIVE); |
||
| 711 | DB::dont_require_table($liveTable); |
||
| 712 | } |
||
| 713 | } |
||
| 714 | } |
||
| 715 | } |
||
| 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) { |
||
| 724 | $unique_regex = '/unique/i'; |
||
| 725 | $results = array(); |
||
| 726 | foreach ($indexes as $key => $index) { |
||
| 727 | $results[$key] = $index; |
||
| 728 | |||
| 729 | // support string descriptors |
||
| 730 | if (is_string($index)) { |
||
| 731 | if (preg_match($unique_regex, $index)) { |
||
| 732 | $results[$key] = preg_replace($unique_regex, 'index', $index); |
||
| 733 | } |
||
| 734 | } |
||
| 735 | |||
| 736 | // canonical, array-based descriptors |
||
| 737 | elseif (is_array($index)) { |
||
| 738 | if (strtolower($index['type']) == 'unique') { |
||
| 739 | $results[$key]['type'] = 'index'; |
||
| 740 | } |
||
| 741 | } |
||
| 742 | } |
||
| 743 | return $results; |
||
| 744 | } |
||
| 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) { |
||
| 754 | $baseDataClass = ClassInfo::baseDataClass($table); |
||
| 755 | |||
| 756 | // Set up a new entry in (table)_versions |
||
| 757 | $newManipulation = array( |
||
| 758 | "command" => "insert", |
||
| 759 | "fields" => isset($manipulation[$table]['fields']) ? $manipulation[$table]['fields'] : null |
||
| 760 | ); |
||
| 761 | |||
| 762 | // Add any extra, unchanged fields to the version record. |
||
| 763 | $data = DB::prepared_query("SELECT * FROM \"$table\" WHERE \"ID\" = ?", array($recordID))->record(); |
||
| 764 | |||
| 765 | if ($data) { |
||
| 766 | $fields = DataObject::database_fields($table); |
||
| 767 | |||
| 768 | if (is_array($fields)) { |
||
| 769 | $data = array_intersect_key($data, $fields); |
||
| 770 | |||
| 771 | foreach ($data as $k => $v) { |
||
| 772 | if (!isset($newManipulation['fields'][$k])) { |
||
| 773 | $newManipulation['fields'][$k] = $v; |
||
| 774 | } |
||
| 775 | } |
||
| 776 | } |
||
| 777 | } |
||
| 778 | |||
| 779 | // Ensure that the ID is instead written to the RecordID field |
||
| 780 | $newManipulation['fields']['RecordID'] = $recordID; |
||
| 781 | unset($newManipulation['fields']['ID']); |
||
| 782 | |||
| 783 | // Generate next version ID to use |
||
| 784 | $nextVersion = 0; |
||
| 785 | if($recordID) { |
||
| 786 | $nextVersion = DB::prepared_query("SELECT MAX(\"Version\") + 1 |
||
| 787 | FROM \"{$baseDataClass}_versions\" WHERE \"RecordID\" = ?", |
||
| 788 | array($recordID) |
||
| 789 | )->value(); |
||
| 790 | } |
||
| 791 | $nextVersion = $nextVersion ?: 1; |
||
| 792 | |||
| 793 | if($table === $baseDataClass) { |
||
| 794 | // Write AuthorID for baseclass |
||
| 795 | $userID = (Member::currentUser()) ? Member::currentUser()->ID : 0; |
||
| 796 | $newManipulation['fields']['AuthorID'] = $userID; |
||
| 797 | |||
| 798 | // Update main table version if not previously known |
||
| 799 | $manipulation[$table]['fields']['Version'] = $nextVersion; |
||
| 800 | } |
||
| 801 | |||
| 802 | // Update _versions table manipulation |
||
| 803 | $newManipulation['fields']['Version'] = $nextVersion; |
||
| 804 | $manipulation["{$table}_versions"] = $newManipulation; |
||
| 805 | } |
||
| 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) { |
||
| 815 | // If the record has already been inserted in the (table), get rid of it. |
||
| 816 | if($manipulation[$table]['command'] == 'insert') { |
||
| 817 | DB::prepared_query( |
||
| 818 | "DELETE FROM \"{$table}\" WHERE \"ID\" = ?", |
||
| 819 | array($recordID) |
||
| 820 | ); |
||
| 821 | } |
||
| 822 | |||
| 823 | $newTable = $this->stageTable($table, Versioned::get_stage()); |
||
| 824 | $manipulation[$newTable] = $manipulation[$table]; |
||
| 825 | unset($manipulation[$table]); |
||
| 826 | } |
||
| 827 | |||
| 828 | |||
| 829 | public function augmentWrite(&$manipulation) { |
||
| 830 | // get Version number from base data table on write |
||
| 831 | $version = null; |
||
| 832 | $owner = $this->owner; |
||
| 833 | $baseDataClass = ClassInfo::baseDataClass($owner->class); |
||
| 834 | if(isset($manipulation[$baseDataClass]['fields'])) { |
||
| 835 | if ($this->migratingVersion) { |
||
| 836 | $manipulation[$baseDataClass]['fields']['Version'] = $this->migratingVersion; |
||
| 837 | } |
||
| 838 | if (isset($manipulation[$baseDataClass]['fields']['Version'])) { |
||
| 839 | $version = $manipulation[$baseDataClass]['fields']['Version']; |
||
| 840 | } |
||
| 841 | } |
||
| 842 | |||
| 843 | // Update all tables |
||
| 844 | $tables = array_keys($manipulation); |
||
| 845 | foreach($tables as $table) { |
||
| 846 | |||
| 847 | // Make sure that the augmented write is being applied to a table that can be versioned |
||
| 848 | if( !$this->canBeVersioned($table) ) { |
||
| 849 | unset($manipulation[$table]); |
||
| 850 | continue; |
||
| 851 | } |
||
| 852 | |||
| 853 | // Get ID field |
||
| 854 | $id = $manipulation[$table]['id'] |
||
| 855 | ? $manipulation[$table]['id'] |
||
| 856 | : $manipulation[$table]['fields']['ID']; |
||
| 857 | if(!$id) { |
||
| 858 | user_error("Couldn't find ID in " . var_export($manipulation[$table], true), E_USER_ERROR); |
||
| 859 | } |
||
| 860 | |||
| 861 | if($version < 0 || $this->_nextWriteWithoutVersion) { |
||
| 862 | // Putting a Version of -1 is a signal to leave the version table alone, despite their being no version |
||
| 863 | unset($manipulation[$table]['fields']['Version']); |
||
| 864 | } elseif(empty($version)) { |
||
| 865 | // If we haven't got a version #, then we're creating a new version. |
||
| 866 | // Otherwise, we're just copying a version to another table |
||
| 867 | $this->augmentWriteVersioned($manipulation, $table, $id); |
||
| 868 | } |
||
| 869 | |||
| 870 | // Remove "Version" column from subclasses of baseDataClass |
||
| 871 | if(!$this->hasVersionField($table)) { |
||
| 872 | unset($manipulation[$table]['fields']['Version']); |
||
| 873 | } |
||
| 874 | |||
| 875 | // Grab a version number - it should be the same across all tables. |
||
| 876 | if(isset($manipulation[$table]['fields']['Version'])) { |
||
| 877 | $thisVersion = $manipulation[$table]['fields']['Version']; |
||
| 878 | } |
||
| 879 | |||
| 880 | // If we're editing Live, then use (table)_Live instead of (table) |
||
| 881 | if($this->hasStages() && static::get_stage() === static::LIVE) { |
||
| 882 | $this->augmentWriteStaged($manipulation, $table, $id); |
||
| 883 | } |
||
| 884 | } |
||
| 885 | |||
| 886 | // Clear the migration flag |
||
| 887 | if($this->migratingVersion) { |
||
| 888 | $this->migrateVersion(null); |
||
| 889 | } |
||
| 890 | |||
| 891 | // Add the new version # back into the data object, for accessing |
||
| 892 | // after this write |
||
| 893 | if(isset($thisVersion)) { |
||
| 894 | $owner->Version = str_replace("'","", $thisVersion); |
||
| 895 | } |
||
| 896 | } |
||
| 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() { |
||
| 905 | $this->_nextWriteWithoutVersion = true; |
||
| 906 | |||
| 907 | return $this->owner->write(); |
||
| 908 | } |
||
| 909 | |||
| 910 | /** |
||
| 911 | * |
||
| 912 | */ |
||
| 913 | public function onAfterWrite() { |
||
| 914 | $this->_nextWriteWithoutVersion = false; |
||
| 915 | } |
||
| 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() { |
||
| 922 | $this->migrateVersion(null); |
||
| 923 | } |
||
| 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) { |
||
| 948 | if (!$list) { |
||
| 949 | $list = new ArrayList(); |
||
| 950 | } |
||
| 951 | |||
| 952 | // Build reverse lookup for ownership |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Find objects which own this object. |
||
| 962 | * Note that objects will only be searched in the same stage as the given record. |
||
| 963 | * |
||
| 964 | * @param bool $recursive True if recursive |
||
| 965 | * @param ArrayList $list List to add items to |
||
| 966 | * @param array $lookup List of reverse lookup rules for owned objects |
||
| 967 | * @return ArrayList list of objects |
||
| 968 | */ |
||
| 969 | public function findOwnersRecursive($recursive, $list, $lookup) { |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Find a list of classes, each of which with a list of methods to invoke |
||
| 1004 | * to lookup owners. |
||
| 1005 | * |
||
| 1006 | * @return array |
||
| 1007 | */ |
||
| 1008 | protected function lookupReverseOwners() { |
||
| 1051 | |||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Find objects in the given relationships, merging them into the given list |
||
| 1055 | * |
||
| 1056 | * @param array $source Config property to extract relationships from |
||
| 1057 | * @param bool $recursive True if recursive |
||
| 1058 | * @param ArrayList $list Optional list to add items to |
||
| 1059 | * @return ArrayList The list |
||
| 1060 | */ |
||
| 1061 | public function findRelatedObjects($source, $recursive = true, $list = null) |
||
| 1102 | |||
| 1103 | /** |
||
| 1104 | * Helper method to merge owned/owning items into a list. |
||
| 1105 | * Items already present in the list will be skipped. |
||
| 1106 | * |
||
| 1107 | * @param ArrayList $list Items to merge into |
||
| 1108 | * @param mixed $items List of new items to merge |
||
| 1109 | * @return ArrayList List of all newly added items that did not already exist in $list |
||
| 1110 | */ |
||
| 1111 | protected function mergeRelatedObjects($list, $items) { |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * This function should return true if the current user can publish this record. |
||
| 1139 | * It can be overloaded to customise the security model for an application. |
||
| 1140 | * |
||
| 1141 | * Denies permission if any of the following conditions is true: |
||
| 1142 | * - canPublish() on any extension returns false |
||
| 1143 | * - canEdit() returns false |
||
| 1144 | * |
||
| 1145 | * @param Member $member |
||
| 1146 | * @return bool True if the current user can publish this record. |
||
| 1147 | */ |
||
| 1148 | View Code Duplication | public function canPublish($member = null) { |
|
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Check if the current user can delete this record from live |
||
| 1175 | * |
||
| 1176 | * @param null $member |
||
| 1177 | * @return mixed |
||
| 1178 | */ |
||
| 1179 | View Code Duplication | public function canUnpublish($member = null) { |
|
| 1203 | |||
| 1204 | /** |
||
| 1205 | * Check if the current user is allowed to archive this record. |
||
| 1206 | * If extended, ensure that both canDelete and canUnpublish are extended also |
||
| 1207 | * |
||
| 1208 | * @param Member $member |
||
| 1209 | * @return bool |
||
| 1210 | */ |
||
| 1211 | public function canArchive($member = null) { |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * Check if the user can revert this record to live |
||
| 1247 | * |
||
| 1248 | * @param Member $member |
||
| 1249 | * @return bool |
||
| 1250 | */ |
||
| 1251 | View Code Duplication | public function canRevertToLive($member = null) { |
|
| 1281 | |||
| 1282 | /** |
||
| 1283 | * Extend permissions to include additional security for objects that are not published to live. |
||
| 1284 | * |
||
| 1285 | * @param Member $member |
||
| 1286 | * @return bool|null |
||
| 1287 | */ |
||
| 1288 | public function canView($member = null) { |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Determine if there are any additional restrictions on this object for the given reading version. |
||
| 1297 | * |
||
| 1298 | * Override this in a subclass to customise any additional effect that Versioned applies to canView. |
||
| 1299 | * |
||
| 1300 | * This is expected to be called by canView, and thus is only responsible for denying access if |
||
| 1301 | * the default canView would otherwise ALLOW access. Thus it should not be called in isolation |
||
| 1302 | * as an authoritative permission check. |
||
| 1303 | * |
||
| 1304 | * This has the following extension points: |
||
| 1305 | * - canViewDraft is invoked if Mode = stage and Stage = stage |
||
| 1306 | * - canViewArchived is invoked if Mode = archive |
||
| 1307 | * |
||
| 1308 | * @param Member $member |
||
| 1309 | * @return bool False is returned if the current viewing mode denies visibility |
||
| 1310 | */ |
||
| 1311 | public function canViewVersioned($member = null) { |
||
| 1349 | |||
| 1350 | /** |
||
| 1351 | * Determines canView permissions for the latest version of this object on a specific stage. |
||
| 1352 | * Usually the stage is read from {@link Versioned::current_stage()}. |
||
| 1353 | * |
||
| 1354 | * This method should be invoked by user code to check if a record is visible in the given stage. |
||
| 1355 | * |
||
| 1356 | * This method should not be called via ->extend('canViewStage'), but rather should be |
||
| 1357 | * overridden in the extended class. |
||
| 1358 | * |
||
| 1359 | * @param string $stage |
||
| 1360 | * @param Member $member |
||
| 1361 | * @return bool |
||
| 1362 | */ |
||
| 1363 | View Code Duplication | public function canViewStage($stage = 'Live', $member = null) { |
|
| 1373 | |||
| 1374 | /** |
||
| 1375 | * Determine if a table is supporting the Versioned extensions (e.g. |
||
| 1376 | * $table_versions does exists). |
||
| 1377 | * |
||
| 1378 | * @param string $table Table name |
||
| 1379 | * @return boolean |
||
| 1380 | */ |
||
| 1381 | public function canBeVersioned($table) { |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Check if a certain table has the 'Version' field. |
||
| 1389 | * |
||
| 1390 | * @param string $table Table name |
||
| 1391 | * |
||
| 1392 | * @return boolean Returns false if the field isn't in the table, true otherwise |
||
| 1393 | */ |
||
| 1394 | public function hasVersionField($table) { |
||
| 1404 | |||
| 1405 | /** |
||
| 1406 | * @param string $table |
||
| 1407 | * |
||
| 1408 | * @return string |
||
| 1409 | */ |
||
| 1410 | public function extendWithSuffix($table) { |
||
| 1427 | |||
| 1428 | /** |
||
| 1429 | * Get the latest published DataObject. |
||
| 1430 | * |
||
| 1431 | * @return DataObject |
||
| 1432 | */ |
||
| 1433 | public function latestPublished() { |
||
| 1445 | |||
| 1446 | /** |
||
| 1447 | * @deprecated 4.0..5.0 |
||
| 1448 | */ |
||
| 1449 | public function doPublish() { |
||
| 1453 | |||
| 1454 | /** |
||
| 1455 | * Publish this object and all owned objects to Live |
||
| 1456 | * |
||
| 1457 | * @return bool |
||
| 1458 | */ |
||
| 1459 | public function publishRecursive() { |
||
| 1477 | |||
| 1478 | /** |
||
| 1479 | * Publishes this object to Live, but doesn't publish owned objects. |
||
| 1480 | * |
||
| 1481 | * @return bool True if publish was successful |
||
| 1482 | */ |
||
| 1483 | View Code Duplication | public function publishSingle() { |
|
| 1495 | |||
| 1496 | /** |
||
| 1497 | * Set foreign keys of has_many objects to 0 where those objects were |
||
| 1498 | * disowned as a result of a partial publish / unpublish. |
||
| 1499 | * I.e. this object and its owned objects were recently written to $targetStage, |
||
| 1500 | * but deleted objects were not. |
||
| 1501 | * |
||
| 1502 | * Note that this operation does not create any new Versions |
||
| 1503 | * |
||
| 1504 | * @param string $sourceStage Objects in this stage will not be unlinked. |
||
| 1505 | * @param string $targetStage Objects which exist in this stage but not $sourceStage |
||
| 1506 | * will be unlinked. |
||
| 1507 | */ |
||
| 1508 | public function unlinkDisownedObjects($sourceStage, $targetStage) { |
||
| 1564 | |||
| 1565 | /** |
||
| 1566 | * Removes the record from both live and stage |
||
| 1567 | * |
||
| 1568 | * @return bool Success |
||
| 1569 | */ |
||
| 1570 | public function doArchive() { |
||
| 1583 | |||
| 1584 | /** |
||
| 1585 | * Removes this record from the live site |
||
| 1586 | * |
||
| 1587 | * @return bool Flag whether the unpublish was successful |
||
| 1588 | */ |
||
| 1589 | public function doUnpublish() { |
||
| 1619 | |||
| 1620 | /** |
||
| 1621 | * Trigger unpublish of owning objects |
||
| 1622 | */ |
||
| 1623 | public function onAfterUnpublish() { |
||
| 1632 | |||
| 1633 | |||
| 1634 | /** |
||
| 1635 | * Revert the draft changes: replace the draft content with the content on live |
||
| 1636 | * |
||
| 1637 | * @return bool True if the revert was successful |
||
| 1638 | */ |
||
| 1639 | View Code Duplication | public function doRevertToLive() { |
|
| 1650 | |||
| 1651 | /** |
||
| 1652 | * Trigger revert of all owned objects to stage |
||
| 1653 | */ |
||
| 1654 | public function onAfterRevertToLive() { |
||
| 1670 | |||
| 1671 | /** |
||
| 1672 | * @deprecated 4.0..5.0 |
||
| 1673 | */ |
||
| 1674 | public function publish($fromStage, $toStage, $createNewVersion = false) { |
||
| 1678 | |||
| 1679 | /** |
||
| 1680 | * Move a database record from one stage to the other. |
||
| 1681 | * |
||
| 1682 | * @param int|string $fromStage Place to copy from. Can be either a stage name or a version number. |
||
| 1683 | * @param string $toStage Place to copy to. Must be a stage name. |
||
| 1684 | * @param bool $createNewVersion Set this to true to create a new version number. |
||
| 1685 | * By default, the existing version number will be copied over. |
||
| 1686 | */ |
||
| 1687 | public function copyVersionToStage($fromStage, $toStage, $createNewVersion = false) { |
||
| 1746 | |||
| 1747 | /** |
||
| 1748 | * Set the migrating version. |
||
| 1749 | * |
||
| 1750 | * @param string $version The version. |
||
| 1751 | */ |
||
| 1752 | public function migrateVersion($version) { |
||
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Compare two stages to see if they're different. |
||
| 1758 | * |
||
| 1759 | * Only checks the version numbers, not the actual content. |
||
| 1760 | * |
||
| 1761 | * @param string $stage1 The first stage to check. |
||
| 1762 | * @param string $stage2 |
||
| 1763 | * @return bool |
||
| 1764 | */ |
||
| 1765 | public function stagesDiffer($stage1, $stage2) { |
||
| 1787 | |||
| 1788 | /** |
||
| 1789 | * @param string $filter |
||
| 1790 | * @param string $sort |
||
| 1791 | * @param string $limit |
||
| 1792 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1793 | * @param string $having |
||
| 1794 | * @return ArrayList |
||
| 1795 | */ |
||
| 1796 | public function Versions($filter = "", $sort = "", $limit = "", $join = "", $having = "") { |
||
| 1799 | |||
| 1800 | /** |
||
| 1801 | * Return a list of all the versions available. |
||
| 1802 | * |
||
| 1803 | * @param string $filter |
||
| 1804 | * @param string $sort |
||
| 1805 | * @param string $limit |
||
| 1806 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1807 | * @param string $having |
||
| 1808 | * @return ArrayList |
||
| 1809 | */ |
||
| 1810 | public function allVersions($filter = "", $sort = "", $limit = "", $join = "", $having = "") { |
||
| 1856 | |||
| 1857 | /** |
||
| 1858 | * Compare two version, and return the diff between them. |
||
| 1859 | * |
||
| 1860 | * @param string $from The version to compare from. |
||
| 1861 | * @param string $to The version to compare to. |
||
| 1862 | * |
||
| 1863 | * @return DataObject |
||
| 1864 | */ |
||
| 1865 | public function compareVersions($from, $to) { |
||
| 1874 | |||
| 1875 | /** |
||
| 1876 | * Return the base table - the class that directly extends DataObject. |
||
| 1877 | * |
||
| 1878 | * @param string $stage |
||
| 1879 | * @return string |
||
| 1880 | */ |
||
| 1881 | public function baseTable($stage = null) { |
||
| 1885 | |||
| 1886 | /** |
||
| 1887 | * Given a class and stage determine the table name. |
||
| 1888 | * |
||
| 1889 | * Note: Stages this asset does not exist in will default to the draft table. |
||
| 1890 | * |
||
| 1891 | * @param string $class |
||
| 1892 | * @param string $stage |
||
| 1893 | * @return string Table name |
||
| 1894 | */ |
||
| 1895 | public function stageTable($class, $stage) { |
||
| 1901 | |||
| 1902 | //-----------------------------------------------------------------------------------------------// |
||
| 1903 | |||
| 1904 | |||
| 1905 | /** |
||
| 1906 | * Determine if the current user is able to set the given site stage / archive |
||
| 1907 | * |
||
| 1908 | * @param SS_HTTPRequest $request |
||
| 1909 | * @return bool |
||
| 1910 | */ |
||
| 1911 | public static function can_choose_site_stage($request) { |
||
| 1924 | |||
| 1925 | /** |
||
| 1926 | * Choose the stage the site is currently on. |
||
| 1927 | * |
||
| 1928 | * If $_GET['stage'] is set, then it will use that stage, and store it in |
||
| 1929 | * the session. |
||
| 1930 | * |
||
| 1931 | * if $_GET['archiveDate'] is set, it will use that date, and store it in |
||
| 1932 | * the session. |
||
| 1933 | * |
||
| 1934 | * If neither of these are set, it checks the session, otherwise the stage |
||
| 1935 | * is set to 'Live'. |
||
| 1936 | */ |
||
| 1937 | public static function choose_site_stage() { |
||
| 1980 | |||
| 1981 | /** |
||
| 1982 | * Set the current reading mode. |
||
| 1983 | * |
||
| 1984 | * @param string $mode |
||
| 1985 | */ |
||
| 1986 | public static function set_reading_mode($mode) { |
||
| 1989 | |||
| 1990 | /** |
||
| 1991 | * Get the current reading mode. |
||
| 1992 | * |
||
| 1993 | * @return string |
||
| 1994 | */ |
||
| 1995 | public static function get_reading_mode() { |
||
| 1998 | |||
| 1999 | /** |
||
| 2000 | * Get the current reading stage. |
||
| 2001 | * |
||
| 2002 | * @return string |
||
| 2003 | */ |
||
| 2004 | public static function get_stage() { |
||
| 2011 | |||
| 2012 | /** |
||
| 2013 | * Get the current archive date. |
||
| 2014 | * |
||
| 2015 | * @return string |
||
| 2016 | */ |
||
| 2017 | public static function current_archived_date() { |
||
| 2023 | |||
| 2024 | /** |
||
| 2025 | * Set the reading stage. |
||
| 2026 | * |
||
| 2027 | * @param string $stage New reading stage. |
||
| 2028 | */ |
||
| 2029 | public static function set_stage($stage) { |
||
| 2032 | |||
| 2033 | /** |
||
| 2034 | * Set the reading archive date. |
||
| 2035 | * |
||
| 2036 | * @param string $date New reading archived date. |
||
| 2037 | */ |
||
| 2038 | public static function reading_archived_date($date) { |
||
| 2041 | |||
| 2042 | |||
| 2043 | /** |
||
| 2044 | * Get a singleton instance of a class in the given stage. |
||
| 2045 | * |
||
| 2046 | * @param string $class The name of the class. |
||
| 2047 | * @param string $stage The name of the stage. |
||
| 2048 | * @param string $filter A filter to be inserted into the WHERE clause. |
||
| 2049 | * @param boolean $cache Use caching. |
||
| 2050 | * @param string $sort A sort expression to be inserted into the ORDER BY clause. |
||
| 2051 | * |
||
| 2052 | * @return DataObject |
||
| 2053 | */ |
||
| 2054 | public static function get_one_by_stage($class, $stage, $filter = '', $cache = true, $sort = '') { |
||
| 2060 | |||
| 2061 | /** |
||
| 2062 | * Gets the current version number of a specific record. |
||
| 2063 | * |
||
| 2064 | * @param string $class |
||
| 2065 | * @param string $stage |
||
| 2066 | * @param int $id |
||
| 2067 | * @param boolean $cache |
||
| 2068 | * |
||
| 2069 | * @return int |
||
| 2070 | */ |
||
| 2071 | public static function get_versionnumber_by_stage($class, $stage, $id, $cache = true) { |
||
| 2101 | |||
| 2102 | /** |
||
| 2103 | * Pre-populate the cache for Versioned::get_versionnumber_by_stage() for |
||
| 2104 | * a list of record IDs, for more efficient database querying. If $idList |
||
| 2105 | * is null, then every record will be pre-cached. |
||
| 2106 | * |
||
| 2107 | * @param string $class |
||
| 2108 | * @param string $stage |
||
| 2109 | * @param array $idList |
||
| 2110 | */ |
||
| 2111 | public static function prepopulate_versionnumber_cache($class, $stage, $idList = null) { |
||
| 2138 | |||
| 2139 | /** |
||
| 2140 | * Get a set of class instances by the given stage. |
||
| 2141 | * |
||
| 2142 | * @param string $class The name of the class. |
||
| 2143 | * @param string $stage The name of the stage. |
||
| 2144 | * @param string $filter A filter to be inserted into the WHERE clause. |
||
| 2145 | * @param string $sort A sort expression to be inserted into the ORDER BY clause. |
||
| 2146 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 2147 | * @param int $limit A limit on the number of records returned from the database. |
||
| 2148 | * @param string $containerClass The container class for the result set (default is DataList) |
||
| 2149 | * |
||
| 2150 | * @return DataList A modified DataList designated to the specified stage |
||
| 2151 | */ |
||
| 2152 | public static function get_by_stage( |
||
| 2161 | |||
| 2162 | /** |
||
| 2163 | * Delete this record from the given stage |
||
| 2164 | * |
||
| 2165 | * @param string $stage |
||
| 2166 | */ |
||
| 2167 | public function deleteFromStage($stage) { |
||
| 2179 | |||
| 2180 | /** |
||
| 2181 | * Write the given record to the draft stage |
||
| 2182 | * |
||
| 2183 | * @param string $stage |
||
| 2184 | * @param boolean $forceInsert |
||
| 2185 | * @return int The ID of the record |
||
| 2186 | */ |
||
| 2187 | View Code Duplication | public function writeToStage($stage, $forceInsert = false) { |
|
| 2198 | |||
| 2199 | /** |
||
| 2200 | * Roll the draft version of this record to match the published record. |
||
| 2201 | * Caution: Doesn't overwrite the object properties with the rolled back version. |
||
| 2202 | * |
||
| 2203 | * {@see doRevertToLive()} to reollback to live |
||
| 2204 | * |
||
| 2205 | * @param int $version Version number |
||
| 2206 | */ |
||
| 2207 | public function doRollbackTo($version) { |
||
| 2214 | |||
| 2215 | public function onAfterRollback($version) { |
||
| 2228 | |||
| 2229 | /** |
||
| 2230 | * Return the latest version of the given record. |
||
| 2231 | * |
||
| 2232 | * @param string $class |
||
| 2233 | * @param int $id |
||
| 2234 | * @return DataObject |
||
| 2235 | */ |
||
| 2236 | public static function get_latest_version($class, $id) { |
||
| 2243 | |||
| 2244 | /** |
||
| 2245 | * Returns whether the current record is the latest one. |
||
| 2246 | * |
||
| 2247 | * @todo Performance - could do this directly via SQL. |
||
| 2248 | * |
||
| 2249 | * @see get_latest_version() |
||
| 2250 | * @see latestPublished |
||
| 2251 | * |
||
| 2252 | * @return boolean |
||
| 2253 | */ |
||
| 2254 | public function isLatestVersion() { |
||
| 2263 | |||
| 2264 | /** |
||
| 2265 | * Check if this record exists on live |
||
| 2266 | * |
||
| 2267 | * @return bool |
||
| 2268 | */ |
||
| 2269 | public function isPublished() { |
||
| 2288 | |||
| 2289 | /** |
||
| 2290 | * Check if this record exists on the draft stage |
||
| 2291 | * |
||
| 2292 | * @return bool |
||
| 2293 | */ |
||
| 2294 | public function isOnDraft() { |
||
| 2307 | |||
| 2308 | |||
| 2309 | |||
| 2310 | /** |
||
| 2311 | * Return the equivalent of a DataList::create() call, querying the latest |
||
| 2312 | * version of each record stored in the (class)_versions tables. |
||
| 2313 | * |
||
| 2314 | * In particular, this will query deleted records as well as active ones. |
||
| 2315 | * |
||
| 2316 | * @param string $class |
||
| 2317 | * @param string $filter |
||
| 2318 | * @param string $sort |
||
| 2319 | * @return DataList |
||
| 2320 | */ |
||
| 2321 | public static function get_including_deleted($class, $filter = "", $sort = "") { |
||
| 2329 | |||
| 2330 | /** |
||
| 2331 | * Return the specific version of the given id. |
||
| 2332 | * |
||
| 2333 | * Caution: The record is retrieved as a DataObject, but saving back |
||
| 2334 | * modifications via write() will create a new version, rather than |
||
| 2335 | * modifying the existing one. |
||
| 2336 | * |
||
| 2337 | * @param string $class |
||
| 2338 | * @param int $id |
||
| 2339 | * @param int $version |
||
| 2340 | * |
||
| 2341 | * @return DataObject |
||
| 2342 | */ |
||
| 2343 | public static function get_version($class, $id, $version) { |
||
| 2353 | |||
| 2354 | /** |
||
| 2355 | * Return a list of all versions for a given id. |
||
| 2356 | * |
||
| 2357 | * @param string $class |
||
| 2358 | * @param int $id |
||
| 2359 | * |
||
| 2360 | * @return DataList |
||
| 2361 | */ |
||
| 2362 | public static function get_all_versions($class, $id) { |
||
| 2369 | |||
| 2370 | /** |
||
| 2371 | * @param array $labels |
||
| 2372 | */ |
||
| 2373 | public function updateFieldLabels(&$labels) { |
||
| 2376 | |||
| 2377 | /** |
||
| 2378 | * @param FieldList |
||
| 2379 | */ |
||
| 2380 | public function updateCMSFields(FieldList $fields) { |
||
| 2385 | |||
| 2386 | /** |
||
| 2387 | * Ensure version ID is reset to 0 on duplicate |
||
| 2388 | * |
||
| 2389 | * @param DataObject $source Record this was duplicated from |
||
| 2390 | * @param bool $doWrite |
||
| 2391 | */ |
||
| 2392 | public function onBeforeDuplicate($source, $doWrite) { |
||
| 2395 | |||
| 2396 | public function flushCache() { |
||
| 2399 | |||
| 2400 | /** |
||
| 2401 | * Return a piece of text to keep DataObject cache keys appropriately specific. |
||
| 2402 | * |
||
| 2403 | * @return string |
||
| 2404 | */ |
||
| 2405 | public function cacheKeyComponent() { |
||
| 2408 | |||
| 2409 | /** |
||
| 2410 | * Returns an array of possible stages. |
||
| 2411 | * |
||
| 2412 | * @return array |
||
| 2413 | */ |
||
| 2414 | public function getVersionedStages() { |
||
| 2421 | |||
| 2422 | public static function get_template_global_variables() { |
||
| 2427 | |||
| 2428 | /** |
||
| 2429 | * Check if this object has stages |
||
| 2430 | * |
||
| 2431 | * @return bool True if this object is staged |
||
| 2432 | */ |
||
| 2433 | public function hasStages() { |
||
| 2436 | } |
||
| 2437 | |||
| 2556 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.