2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM\Versioning;
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Control\Cookie;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Object;
|
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataQuery;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
2016-10-12 01:19:07 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Queries\SQLSelect;
|
|
|
|
use SilverStripe\ORM\Queries\SQLUpdate;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\TemplateGlobalProvider;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use LogicException;
|
2016-01-26 04:56:07 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* The Versioned extension allows your DataObjects to have several versions,
|
|
|
|
* allowing you to rollback changes and view history. An example of this is
|
2013-05-10 11:19:16 +02:00
|
|
|
* the pages used in the CMS.
|
|
|
|
*
|
2016-02-22 06:42:48 +01:00
|
|
|
* @property int $Version
|
2016-02-25 05:32:41 +01:00
|
|
|
* @property DataObject|Versioned $owner
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2014-03-26 06:27:07 +01:00
|
|
|
class Versioned extends DataExtension implements TemplateGlobalProvider {
|
2016-03-02 06:18:10 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-03-02 06:18:10 +01:00
|
|
|
* Versioning mode for this object.
|
|
|
|
* Note: Not related to the current versioning mode in the state / session
|
|
|
|
* Will be one of 'StagedVersioned' or 'Versioned';
|
|
|
|
*
|
|
|
|
* @var string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-03-02 06:18:10 +01:00
|
|
|
protected $mode;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-03-02 06:18:10 +01:00
|
|
|
* The default reading mode
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-03-02 06:18:10 +01:00
|
|
|
const DEFAULT_MODE = 'Stage.Live';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-03-02 06:18:10 +01:00
|
|
|
* Constructor arg to specify that staging is active on this record.
|
|
|
|
* 'Staging' implies that 'Versioning' is also enabled.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-03-02 06:18:10 +01:00
|
|
|
const STAGEDVERSIONED = 'StagedVersioned';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-11 06:14:56 +02:00
|
|
|
/**
|
2016-03-02 06:18:10 +01:00
|
|
|
* Constructor arg to specify that versioning only is active on this record.
|
2014-06-11 06:14:56 +02:00
|
|
|
*/
|
2016-03-02 06:18:10 +01:00
|
|
|
const VERSIONED = 'Versioned';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Public stage.
|
|
|
|
*/
|
|
|
|
const LIVE = 'Live';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The draft (default) stage
|
|
|
|
*/
|
|
|
|
const DRAFT = 'Stage';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* A version that a DataObject should be when it is 'migrating',
|
|
|
|
* that is, when it is in the process of moving from one stage to another.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $migratingVersion;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-17 23:38:47 +01:00
|
|
|
/**
|
|
|
|
* A cache used by get_versionnumber_by_stage().
|
|
|
|
* Clear through {@link flushCache()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-12-17 23:38:47 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $cache_versionnumber;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
2016-03-02 06:18:10 +01:00
|
|
|
* Current reading mode
|
|
|
|
*
|
2013-05-10 11:19:16 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $reading_mode = null;
|
|
|
|
|
2012-01-14 11:03:45 +01:00
|
|
|
/**
|
|
|
|
* @var Boolean Flag which is temporarily changed during the write() process
|
|
|
|
* to influence augmentWrite() behaviour. If set to TRUE, no new version will be created
|
|
|
|
* for the following write. Needs to be public as other classes introspect this state
|
|
|
|
* during the write process in order to adapt to this versioning behaviour.
|
|
|
|
*/
|
|
|
|
public $_nextWriteWithoutVersion = false;
|
|
|
|
|
2009-05-12 06:47:56 +02:00
|
|
|
/**
|
|
|
|
* Additional database columns for the new
|
2016-10-20 03:49:22 +02:00
|
|
|
* "_Versions" table. Used in {@link augmentDatabase()}
|
2011-04-15 11:35:30 +02:00
|
|
|
* and all Versioned calls extending or creating
|
2009-05-12 06:47:56 +02:00
|
|
|
* SELECT statements.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-12 06:47:56 +02:00
|
|
|
* @var array $db_for_versions_table
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db_for_versions_table = array(
|
2009-05-12 06:47:56 +02:00
|
|
|
"RecordID" => "Int",
|
|
|
|
"Version" => "Int",
|
|
|
|
"WasPublished" => "Boolean",
|
|
|
|
"AuthorID" => "Int",
|
2012-05-28 11:13:42 +02:00
|
|
|
"PublisherID" => "Int"
|
2009-05-12 06:47:56 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $db = array(
|
|
|
|
'Version' => 'Int'
|
|
|
|
);
|
|
|
|
|
2013-06-04 15:28:36 +02:00
|
|
|
/**
|
|
|
|
* Used to enable or disable the prepopulation of the version number cache.
|
|
|
|
* Defaults to true.
|
|
|
|
*
|
2016-03-02 06:18:10 +01:00
|
|
|
* @config
|
2013-06-04 15:28:36 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $prepopulate_versionnumber_cache = true;
|
|
|
|
|
2009-05-12 06:47:56 +02:00
|
|
|
/**
|
|
|
|
* Additional database indexes for the new
|
2016-10-20 03:49:22 +02:00
|
|
|
* "_Versions" table. Used in {@link augmentDatabase()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-12 06:47:56 +02:00
|
|
|
* @var array $indexes_for_versions_table
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $indexes_for_versions_table = array(
|
2012-09-11 02:53:08 +02:00
|
|
|
'RecordID_Version' => '("RecordID","Version")',
|
2009-05-12 06:47:56 +02:00
|
|
|
'RecordID' => true,
|
|
|
|
'Version' => true,
|
|
|
|
'AuthorID' => true,
|
|
|
|
'PublisherID' => true,
|
|
|
|
);
|
2009-08-11 06:45:54 +02:00
|
|
|
|
2016-02-23 21:53:52 +01:00
|
|
|
|
2009-08-11 06:45:54 +02:00
|
|
|
/**
|
2013-05-10 11:19:16 +02:00
|
|
|
* An array of DataObject extensions that may require versioning for extra tables
|
|
|
|
* The array value is a set of suffixes to form these table names, assuming a preceding '_'.
|
2014-08-15 08:53:05 +02:00
|
|
|
* E.g. if Extension1 creates a new table 'Class_suffix1'
|
2013-05-10 11:19:16 +02:00
|
|
|
* and Extension2 the tables 'Class_suffix2' and 'Class_suffix3':
|
|
|
|
*
|
|
|
|
* $versionableExtensions = array(
|
|
|
|
* 'Extension1' => 'suffix1',
|
|
|
|
* 'Extension2' => array('suffix2', 'suffix3'),
|
|
|
|
* );
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-02-23 21:53:52 +01:00
|
|
|
* This can also be manipulated by updating the current loaded config
|
|
|
|
*
|
|
|
|
* SiteTree:
|
|
|
|
* versionableExtensions:
|
|
|
|
* - Extension1:
|
|
|
|
* - suffix1
|
|
|
|
* - suffix2
|
|
|
|
* - Extension2:
|
|
|
|
* - suffix1
|
|
|
|
* - suffix2
|
|
|
|
*
|
|
|
|
* or programatically:
|
|
|
|
*
|
|
|
|
* Config::inst()->update($this->owner->class, 'versionableExtensions',
|
|
|
|
* array('Extension1' => 'suffix1', 'Extension2' => array('suffix2', 'suffix3')));
|
|
|
|
*
|
|
|
|
*
|
2016-05-25 07:09:29 +02:00
|
|
|
* Your extension must implement VersionableExtension interface in order to
|
|
|
|
* apply custom tables for versioned.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2016-03-02 06:18:10 +01:00
|
|
|
* @config
|
2013-05-10 11:19:16 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-05-25 07:09:29 +02:00
|
|
|
private static $versionableExtensions = [];
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2015-12-02 05:33:25 +01:00
|
|
|
/**
|
|
|
|
* Permissions necessary to view records outside of the live stage (e.g. archive / draft stage).
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $non_live_permissions = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_CMSMain', 'VIEW_DRAFT_CONTENT');
|
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
/**
|
|
|
|
* List of relationships on this object that are "owned" by this object.
|
|
|
|
* Owership in the context of versioned objects is a relationship where
|
|
|
|
* the publishing of owning objects requires the publishing of owned objects.
|
|
|
|
*
|
|
|
|
* E.g. A page owns a set of banners, as in order for the page to be published, all
|
|
|
|
* banners on this page must also be published for it to be visible.
|
|
|
|
*
|
|
|
|
* Typically any object and its owned objects should be visible in the same edit view.
|
|
|
|
* E.g. a page and {@see GridField} of banners.
|
|
|
|
*
|
|
|
|
* Page hierarchy is typically not considered an ownership relationship.
|
|
|
|
*
|
|
|
|
* Ownership is recursive; If A owns B and B owns C then A owns C.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array List of has_many or many_many relationships owned by this object.
|
|
|
|
*/
|
|
|
|
private static $owns = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opposing relationship to owns config; Represents the objects which
|
|
|
|
* own the current object.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $owned_by = array();
|
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
|
|
|
* Reset static configuration variables to their default values.
|
2009-08-11 06:45:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function reset() {
|
2010-04-13 05:16:57 +02:00
|
|
|
self::$reading_mode = '';
|
|
|
|
Session::clear('readingMode');
|
2009-08-11 06:45:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Amend freshly created DataQuery objects with versioned-specific
|
2013-05-10 11:19:16 +02:00
|
|
|
* information.
|
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param SQLSelect $query
|
|
|
|
* @param DataQuery $dataQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public function augmentDataQueryCreation(SQLSelect &$query, DataQuery &$dataQuery) {
|
2011-03-21 07:53:09 +01:00
|
|
|
$parts = explode('.', Versioned::get_reading_mode());
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2011-03-21 07:53:09 +01:00
|
|
|
if($parts[0] == 'Archive') {
|
2009-11-22 06:16:38 +01:00
|
|
|
$dataQuery->setQueryParam('Versioned.mode', 'archive');
|
2011-03-21 07:53:09 +01:00
|
|
|
$dataQuery->setQueryParam('Versioned.date', $parts[1]);
|
2016-03-02 06:18:10 +01:00
|
|
|
} else if($parts[0] == 'Stage' && $this->hasStages()) {
|
2009-11-22 06:16:38 +01:00
|
|
|
$dataQuery->setQueryParam('Versioned.mode', 'stage');
|
2011-03-21 07:53:09 +01:00
|
|
|
$dataQuery->setQueryParam('Versioned.stage', $parts[1]);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2016-02-25 05:32:41 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
/**
|
|
|
|
* Construct a new Versioned object.
|
|
|
|
*
|
|
|
|
* @var string $mode One of "StagedVersioned" or "Versioned".
|
|
|
|
*/
|
|
|
|
public function __construct($mode = self::STAGEDVERSIONED) {
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
// Handle deprecated behaviour
|
|
|
|
if($mode === 'Stage' && func_num_args() === 1) {
|
|
|
|
Deprecation::notice("5.0", "Versioned now takes a mode as a single parameter");
|
|
|
|
$mode = static::VERSIONED;
|
|
|
|
} elseif(is_array($mode) || func_num_args() > 1) {
|
|
|
|
Deprecation::notice("5.0", "Versioned now takes a mode as a single parameter");
|
|
|
|
$mode = func_num_args() > 1 || count($mode) > 1
|
|
|
|
? static::STAGEDVERSIONED
|
|
|
|
: static::VERSIONED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!in_array($mode, array(static::STAGEDVERSIONED, static::VERSIONED))) {
|
|
|
|
throw new InvalidArgumentException("Invalid mode: {$mode}");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->mode = $mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache of version to modified dates for this objects
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $versionModifiedCache = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get modified date for the given version
|
|
|
|
*
|
|
|
|
* @param int $version
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getLastEditedForVersion($version) {
|
|
|
|
// Cache key
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseTable = $this->baseTable();
|
2016-03-02 06:18:10 +01:00
|
|
|
$id = $this->owner->ID;
|
|
|
|
$key = "{$baseTable}#{$id}/{$version}";
|
|
|
|
|
|
|
|
// Check cache
|
|
|
|
if(isset($this->versionModifiedCache[$key])) {
|
|
|
|
return $this->versionModifiedCache[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build query
|
2016-10-20 03:49:22 +02:00
|
|
|
$table = "\"{$baseTable}_Versions\"";
|
2016-03-02 06:18:10 +01:00
|
|
|
$query = SQLSelect::create('"LastEdited"', $table)
|
|
|
|
->addWhere([
|
|
|
|
"{$table}.\"RecordID\"" => $id,
|
|
|
|
"{$table}.\"Version\"" => $version
|
|
|
|
]);
|
|
|
|
$date = $query->execute()->value();
|
|
|
|
if($date) {
|
|
|
|
$this->versionModifiedCache[$key] = $date;
|
|
|
|
}
|
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
/**
|
|
|
|
* Updates query parameters of relations attached to versioned dataobjects
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
2016-02-25 05:32:41 +01:00
|
|
|
public function updateInheritableQueryParams(&$params) {
|
2016-03-02 06:18:10 +01:00
|
|
|
// Skip if versioned isn't set
|
|
|
|
if(!isset($params['Versioned.mode'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust query based on original selection criterea
|
|
|
|
switch($params['Versioned.mode']) {
|
|
|
|
case 'all_versions': {
|
|
|
|
// Versioned.mode === all_versions doesn't inherit very well, so default to stage
|
|
|
|
$params['Versioned.mode'] = 'stage';
|
|
|
|
$params['Versioned.stage'] = static::DRAFT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'version': {
|
|
|
|
// If we selected this object from a specific version, we need
|
|
|
|
// to find the date this version was published, and ensure
|
|
|
|
// inherited queries select from that date.
|
|
|
|
$version = $params['Versioned.version'];
|
|
|
|
$date = $this->getLastEditedForVersion($version);
|
|
|
|
|
|
|
|
// Filter related objects at the same date as this version
|
|
|
|
unset($params['Versioned.version']);
|
|
|
|
if($date) {
|
|
|
|
$params['Versioned.mode'] = 'archive';
|
|
|
|
$params['Versioned.date'] = $date;
|
|
|
|
} else {
|
|
|
|
// Fallback to default
|
|
|
|
$params['Versioned.mode'] = 'stage';
|
|
|
|
$params['Versioned.stage'] = static::DRAFT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-02-25 05:32:41 +01:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Augment the the SQLSelect that is created by the DataQuery
|
2015-06-09 01:31:07 +02:00
|
|
|
*
|
2016-03-02 06:18:10 +01:00
|
|
|
* See {@see augmentLazyLoadFields} for lazy-loading applied prior to this.
|
|
|
|
*
|
2015-06-09 01:31:07 +02:00
|
|
|
* @param SQLSelect $query
|
|
|
|
* @param DataQuery $dataQuery
|
|
|
|
* @throws InvalidArgumentException
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) {
|
2013-10-03 16:04:55 +02:00
|
|
|
if(!$dataQuery || !$dataQuery->getQueryParam('Versioned.mode')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseTable = $this->baseTable();
|
2016-03-02 06:18:10 +01:00
|
|
|
$versionedMode = $dataQuery->getQueryParam('Versioned.mode');
|
|
|
|
switch($versionedMode) {
|
2009-11-22 06:16:38 +01:00
|
|
|
// Reading a specific stage (Stage or Live)
|
|
|
|
case 'stage':
|
2016-03-02 06:18:10 +01:00
|
|
|
// Check if we need to rewrite this table
|
2009-11-22 06:16:38 +01:00
|
|
|
$stage = $dataQuery->getQueryParam('Versioned.stage');
|
2016-03-02 06:18:10 +01:00
|
|
|
if(!$this->hasStages() || $stage === static::DRAFT) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Rewrite all tables to select from the live version
|
|
|
|
foreach($query->getFrom() as $table => $dummy) {
|
|
|
|
if(!$this->isTableVersioned($table)) {
|
|
|
|
continue;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2016-03-02 06:18:10 +01:00
|
|
|
$stageTable = $this->stageTable($table, $stage);
|
|
|
|
$query->renameTable($table, $stageTable);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
break;
|
2012-08-21 05:52:12 +02:00
|
|
|
|
|
|
|
// Reading a specific stage, but only return items that aren't in any other stage
|
|
|
|
case 'stage_unique':
|
2016-03-02 06:18:10 +01:00
|
|
|
if(!$this->hasStages()) {
|
|
|
|
break;
|
|
|
|
}
|
2012-08-21 05:52:12 +02:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
$stage = $dataQuery->getQueryParam('Versioned.stage');
|
2012-09-26 23:34:00 +02:00
|
|
|
// Recurse to do the default stage behavior (must be first, we rely on stage renaming happening before
|
|
|
|
// below)
|
2012-08-21 05:52:12 +02:00
|
|
|
$dataQuery->setQueryParam('Versioned.mode', 'stage');
|
|
|
|
$this->augmentSQL($query, $dataQuery);
|
2012-12-17 22:11:30 +01:00
|
|
|
$dataQuery->setQueryParam('Versioned.mode', 'stage_unique');
|
2012-08-21 05:52:12 +02:00
|
|
|
|
|
|
|
// Now exclude any ID from any other stage. Note that we double rename to avoid the regular stage rename
|
|
|
|
// renaming all subquery references to be Versioned.stage
|
2016-03-02 06:18:10 +01:00
|
|
|
foreach([static::DRAFT, static::LIVE] as $excluding) {
|
|
|
|
if ($excluding == $stage) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-21 05:52:12 +02:00
|
|
|
|
|
|
|
$tempName = 'ExclusionarySource_'.$excluding;
|
2016-05-25 07:09:29 +02:00
|
|
|
$excludingTable = $this->baseTable($excluding);
|
2012-08-21 05:52:12 +02:00
|
|
|
|
2012-08-23 02:56:06 +02:00
|
|
|
$query->addWhere('"'.$baseTable.'"."ID" NOT IN (SELECT "ID" FROM "'.$tempName.'")');
|
2012-08-21 05:52:12 +02:00
|
|
|
$query->renameTable($tempName, $excludingTable);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// Return all version instances
|
2016-03-02 06:18:10 +01:00
|
|
|
case 'archive':
|
2011-03-21 07:53:09 +01:00
|
|
|
case 'all_versions':
|
|
|
|
case 'latest_versions':
|
2016-03-02 06:18:10 +01:00
|
|
|
case 'version':
|
2012-05-28 11:13:42 +02:00
|
|
|
foreach($query->getFrom() as $alias => $join) {
|
2016-03-07 00:47:04 +01:00
|
|
|
if(!$this->isTableVersioned($alias)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-05-28 11:13:42 +02:00
|
|
|
if($alias != $baseTable) {
|
2016-02-25 05:32:41 +01:00
|
|
|
// Make sure join includes version as well
|
|
|
|
$query->setJoinFilter(
|
|
|
|
$alias,
|
2016-10-20 03:49:22 +02:00
|
|
|
"\"{$alias}_Versions\".\"RecordID\" = \"{$baseTable}_Versions\".\"RecordID\""
|
|
|
|
. " AND \"{$alias}_Versions\".\"Version\" = \"{$baseTable}_Versions\".\"Version\""
|
2016-02-25 05:32:41 +01:00
|
|
|
);
|
2012-05-28 11:13:42 +02:00
|
|
|
}
|
2016-10-20 03:49:22 +02:00
|
|
|
$query->renameTable($alias, $alias . '_Versions');
|
2012-05-28 11:13:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-20 03:49:22 +02:00
|
|
|
// Add all <basetable>_Versions columns
|
|
|
|
foreach(Config::inst()->get(static::class, 'db_for_versions_table') as $name => $type) {
|
|
|
|
$query->selectField(sprintf('"%s_Versions"."%s"', $baseTable, $name), $name);
|
2012-05-28 11:13:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
// Alias the record ID as the row ID, and ensure ID filters are aliased correctly
|
2016-10-20 03:49:22 +02:00
|
|
|
$query->selectField("\"{$baseTable}_Versions\".\"RecordID\"", "ID");
|
|
|
|
$query->replaceText("\"{$baseTable}_Versions\".\"ID\"", "\"{$baseTable}_Versions\".\"RecordID\"");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
// However, if doing count, undo rewrite of "ID" column
|
|
|
|
$query->replaceText(
|
2016-10-20 03:49:22 +02:00
|
|
|
"count(DISTINCT \"{$baseTable}_Versions\".\"RecordID\")",
|
|
|
|
"count(DISTINCT \"{$baseTable}_Versions\".\"ID\")"
|
2016-02-25 05:32:41 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
// Add additional versioning filters
|
|
|
|
switch($versionedMode) {
|
|
|
|
case 'archive': {
|
|
|
|
$date = $dataQuery->getQueryParam('Versioned.date');
|
|
|
|
if(!$date) {
|
|
|
|
throw new InvalidArgumentException("Invalid archive date");
|
|
|
|
}
|
|
|
|
// Link to the version archived on that date
|
|
|
|
$query->addWhere([
|
2016-10-20 03:49:22 +02:00
|
|
|
"\"{$baseTable}_Versions\".\"Version\" IN
|
2016-03-02 06:18:10 +01:00
|
|
|
(SELECT LatestVersion FROM
|
|
|
|
(SELECT
|
2016-10-20 03:49:22 +02:00
|
|
|
\"{$baseTable}_Versions\".\"RecordID\",
|
|
|
|
MAX(\"{$baseTable}_Versions\".\"Version\") AS LatestVersion
|
|
|
|
FROM \"{$baseTable}_Versions\"
|
|
|
|
WHERE \"{$baseTable}_Versions\".\"LastEdited\" <= ?
|
|
|
|
GROUP BY \"{$baseTable}_Versions\".\"RecordID\"
|
|
|
|
) AS \"{$baseTable}_Versions_Latest\"
|
|
|
|
WHERE \"{$baseTable}_Versions_Latest\".\"RecordID\" = \"{$baseTable}_Versions\".\"RecordID\"
|
2016-03-02 06:18:10 +01:00
|
|
|
)" => $date
|
|
|
|
]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'latest_versions': {
|
|
|
|
// Return latest version instances, regardless of whether they are on a particular stage
|
|
|
|
// This provides "show all, including deleted" functonality
|
|
|
|
$query->addWhere(
|
2016-10-20 03:49:22 +02:00
|
|
|
"\"{$baseTable}_Versions\".\"Version\" IN
|
2016-03-02 06:18:10 +01:00
|
|
|
(SELECT LatestVersion FROM
|
|
|
|
(SELECT
|
2016-10-20 03:49:22 +02:00
|
|
|
\"{$baseTable}_Versions\".\"RecordID\",
|
|
|
|
MAX(\"{$baseTable}_Versions\".\"Version\") AS LatestVersion
|
|
|
|
FROM \"{$baseTable}_Versions\"
|
|
|
|
GROUP BY \"{$baseTable}_Versions\".\"RecordID\"
|
|
|
|
) AS \"{$baseTable}_Versions_Latest\"
|
|
|
|
WHERE \"{$baseTable}_Versions_Latest\".\"RecordID\" = \"{$baseTable}_Versions\".\"RecordID\"
|
2016-03-02 06:18:10 +01:00
|
|
|
)"
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'version': {
|
|
|
|
// If selecting a specific version, filter it here
|
|
|
|
$version = $dataQuery->getQueryParam('Versioned.version');
|
|
|
|
if(!$version) {
|
|
|
|
throw new InvalidArgumentException("Invalid version");
|
|
|
|
}
|
|
|
|
$query->addWhere([
|
2016-10-20 03:49:22 +02:00
|
|
|
"\"{$baseTable}_Versions\".\"Version\"" => $version
|
2016-03-02 06:18:10 +01:00
|
|
|
]);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
case 'all_versions':
|
2016-03-02 06:18:10 +01:00
|
|
|
default: {
|
|
|
|
// If all versions are requested, ensure that records are sorted by this field
|
2016-10-20 03:49:22 +02:00
|
|
|
$query->addOrderBy(sprintf('"%s_Versions"."%s"', $baseTable, 'Version'));
|
2016-03-02 06:18:10 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-05-28 11:13:42 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2012-09-26 23:34:00 +02:00
|
|
|
throw new InvalidArgumentException("Bad value for query parameter Versioned.mode: "
|
|
|
|
. $dataQuery->getQueryParam('Versioned.mode'));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-10 03:59:45 +02:00
|
|
|
|
2016-03-07 00:47:04 +01:00
|
|
|
/**
|
|
|
|
* Determine if the given versioned table is a part of the sub-tree of the current dataobject
|
|
|
|
* This helps prevent rewriting of other tables that get joined in, in particular, many_many tables
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @return bool True if this table should be versioned
|
|
|
|
*/
|
|
|
|
protected function isTableVersioned($table) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
$tableClass = $schema->tableClass($table);
|
|
|
|
if(empty($tableClass)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that this class belongs to the same tree
|
|
|
|
$baseClass = $schema->baseDataClass($this->owner);
|
|
|
|
if(!is_a($tableClass, $baseClass, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that this isn't a derived table
|
|
|
|
// (e.g. _Live, or a many_many table)
|
|
|
|
$mainTable = $schema->tableName($tableClass);
|
|
|
|
if($mainTable !== $table) {
|
2016-03-07 00:47:04 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
|
|
|
|
return true;
|
2016-03-07 00:47:04 +01:00
|
|
|
}
|
|
|
|
|
2012-10-10 03:59:45 +02:00
|
|
|
/**
|
2013-05-10 11:19:16 +02:00
|
|
|
* For lazy loaded fields requiring extra sql manipulation, ie versioning.
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param SQLSelect $query
|
2012-10-10 03:59:45 +02:00
|
|
|
* @param DataQuery $dataQuery
|
2013-02-12 04:44:32 +01:00
|
|
|
* @param DataObject $dataObject
|
2012-10-10 03:59:45 +02:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public function augmentLoadLazyFields(SQLSelect &$query, DataQuery &$dataQuery = null, $dataObject) {
|
2014-08-15 08:53:05 +02:00
|
|
|
// The VersionedMode local variable ensures that this decorator only applies to
|
|
|
|
// queries that have originated from the Versioned object, and have the Versioned
|
|
|
|
// metadata set on the query object. This prevents regular queries from
|
2016-10-20 03:49:22 +02:00
|
|
|
// accidentally querying the *_Versions tables.
|
2013-02-12 04:44:32 +01:00
|
|
|
$versionedMode = $dataObject->getSourceQueryParam('Versioned.mode');
|
2016-03-02 06:18:10 +01:00
|
|
|
$modesToAllowVersioning = array('all_versions', 'latest_versions', 'archive', 'version');
|
2013-02-12 04:44:32 +01:00
|
|
|
if(
|
|
|
|
!empty($dataObject->Version) &&
|
|
|
|
(!empty($versionedMode) && in_array($versionedMode,$modesToAllowVersioning))
|
|
|
|
) {
|
2016-03-02 06:18:10 +01:00
|
|
|
// This will ensure that augmentSQL will select only the same version as the owner,
|
|
|
|
// regardless of how this object was initially selected
|
2016-05-25 07:09:29 +02:00
|
|
|
$versionColumn = $this->owner->getSchema()->sqlColumnForField($this->owner, 'Version');
|
2016-03-02 06:18:10 +01:00
|
|
|
$dataQuery->where([
|
2016-05-25 07:09:29 +02:00
|
|
|
$versionColumn => $dataObject->Version
|
2016-03-02 06:18:10 +01:00
|
|
|
]);
|
2012-10-10 03:59:45 +02:00
|
|
|
$dataQuery->setQueryParam('Versioned.mode', 'all_versions');
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function augmentDatabase() {
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
2016-05-25 07:09:29 +02:00
|
|
|
$class = get_class($owner);
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = $owner->getSchema();
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseTable = $this->baseTable();
|
2016-10-06 06:31:38 +02:00
|
|
|
$classTable = $schema->tableName($owner);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
$isRootClass = $class === $owner->baseClass();
|
2007-09-16 18:15:47 +02:00
|
|
|
|
|
|
|
// Build a list of suffixes whose tables need versioning
|
|
|
|
$allSuffixes = array();
|
2016-03-02 06:18:10 +01:00
|
|
|
$versionableExtensions = $owner->config()->versionableExtensions;
|
2016-02-23 21:53:52 +01:00
|
|
|
if(count($versionableExtensions)){
|
|
|
|
foreach ($versionableExtensions as $versionableExtension => $suffixes) {
|
2016-03-02 06:18:10 +01:00
|
|
|
if ($owner->hasExtension($versionableExtension)) {
|
|
|
|
foreach ((array)$suffixes as $suffix) {
|
|
|
|
$allSuffixes[$suffix] = $versionableExtension;
|
|
|
|
}
|
2007-09-16 18:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-09-16 19:24:51 +02:00
|
|
|
// Add the default table with an empty suffix to the list (table name = class name)
|
2016-05-25 07:09:29 +02:00
|
|
|
$allSuffixes[''] = null;
|
|
|
|
|
|
|
|
foreach ($allSuffixes as $suffix => $extension) {
|
|
|
|
// Check tables for this build
|
|
|
|
if ($suffix) {
|
|
|
|
$suffixBaseTable = "{$baseTable}_{$suffix}";
|
|
|
|
$suffixTable = "{$classTable}_{$suffix}";
|
|
|
|
} else {
|
|
|
|
$suffixBaseTable = $baseTable;
|
|
|
|
$suffixTable = $classTable;
|
|
|
|
}
|
2007-09-16 18:15:47 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
$fields = $schema->databaseFields($class, false);
|
2015-09-10 05:46:23 +02:00
|
|
|
unset($fields['ID']);
|
|
|
|
if($fields) {
|
2016-10-06 06:31:38 +02:00
|
|
|
$options = Config::inst()->get($class, 'create_table_options', Config::FIRST_SET);
|
2016-03-02 06:18:10 +01:00
|
|
|
$indexes = $owner->databaseIndexes();
|
2016-05-25 07:09:29 +02:00
|
|
|
$extensionClass = $allSuffixes[$suffix];
|
|
|
|
if ($suffix && ($extension = $owner->getExtensionInstance($extensionClass))) {
|
|
|
|
if (!$extension instanceof VersionableExtension) {
|
|
|
|
throw new LogicException(
|
|
|
|
"Extension {$extensionClass} must implement VersionableExtension"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Allow versionable extension to customise table fields and indexes
|
|
|
|
$extension->setOwner($owner);
|
|
|
|
if ($extension->isVersionedTable($suffixTable)) {
|
|
|
|
$extension->updateVersionableFields($suffix, $fields, $indexes);
|
|
|
|
}
|
|
|
|
$extension->clearOwner();
|
2007-09-16 19:24:51 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
// Build _Live table
|
2016-03-02 06:18:10 +01:00
|
|
|
if($this->hasStages()) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$liveTable = $this->stageTable($suffixTable, static::LIVE);
|
2016-03-02 06:18:10 +01:00
|
|
|
DB::require_table($liveTable, $fields, $indexes, false, $options);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-20 03:49:22 +02:00
|
|
|
// Build _Versions table
|
2016-05-25 07:09:29 +02:00
|
|
|
//Unique indexes will not work on versioned tables, so we'll convert them to standard indexes:
|
|
|
|
$nonUniqueIndexes = $this->uniqueToIndex($indexes);
|
2009-07-09 08:00:07 +02:00
|
|
|
if($isRootClass) {
|
|
|
|
// Create table for all versions
|
|
|
|
$versionFields = array_merge(
|
2016-10-20 03:49:22 +02:00
|
|
|
Config::inst()->get(static::class, 'db_for_versions_table'),
|
2009-07-09 08:00:07 +02:00
|
|
|
(array)$fields
|
|
|
|
);
|
|
|
|
$versionIndexes = array_merge(
|
2016-10-20 03:49:22 +02:00
|
|
|
Config::inst()->get(static::class, 'indexes_for_versions_table'),
|
2016-05-25 07:09:29 +02:00
|
|
|
(array)$nonUniqueIndexes
|
2009-07-09 08:00:07 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Create fields for any tables of subclasses
|
|
|
|
$versionFields = array_merge(
|
|
|
|
array(
|
|
|
|
"RecordID" => "Int",
|
|
|
|
"Version" => "Int",
|
|
|
|
),
|
|
|
|
(array)$fields
|
|
|
|
);
|
|
|
|
$versionIndexes = array_merge(
|
|
|
|
array(
|
2012-08-21 06:24:05 +02:00
|
|
|
'RecordID_Version' => array('type' => 'unique', 'value' => '"RecordID","Version"'),
|
2009-07-09 08:00:07 +02:00
|
|
|
'RecordID' => true,
|
|
|
|
'Version' => true,
|
|
|
|
),
|
2016-05-25 07:09:29 +02:00
|
|
|
(array)$nonUniqueIndexes
|
2009-07-09 08:00:07 +02:00
|
|
|
);
|
2007-09-16 19:24:51 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
// Cleanup any orphans
|
2016-10-20 03:49:22 +02:00
|
|
|
$this->cleanupVersionedOrphans("{$suffixBaseTable}_Versions", "{$suffixTable}_Versions");
|
2009-07-09 08:00:07 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
// Build versions table
|
2016-10-20 03:49:22 +02:00
|
|
|
DB::require_table("{$suffixTable}_Versions", $versionFields, $versionIndexes, true, $options);
|
2007-09-16 19:24:51 +02:00
|
|
|
} else {
|
2016-10-20 03:49:22 +02:00
|
|
|
DB::dont_require_table("{$suffixTable}_Versions");
|
2016-03-02 06:18:10 +01:00
|
|
|
if($this->hasStages()) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$liveTable = $this->stageTable($suffixTable, static::LIVE);
|
2016-03-02 06:18:10 +01:00
|
|
|
DB::dont_require_table($liveTable);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:15:47 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
/**
|
2016-10-20 03:49:22 +02:00
|
|
|
* Cleanup orphaned records in the _Versions table
|
2016-05-25 07:09:29 +02:00
|
|
|
*
|
|
|
|
* @param string $baseTable base table to use as authoritative source of records
|
|
|
|
* @param string $childTable Sub-table to clean orphans from
|
|
|
|
*/
|
|
|
|
protected function cleanupVersionedOrphans($baseTable, $childTable) {
|
|
|
|
// Skip if child table doesn't exist
|
|
|
|
if(!DB::get_schema()->hasTable($childTable)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Skip if tables are the same
|
|
|
|
if($childTable === $baseTable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select all orphaned version records
|
|
|
|
$orphanedQuery = SQLSelect::create()
|
|
|
|
->selectField("\"{$childTable}\".\"ID\"")
|
|
|
|
->setFrom("\"{$childTable}\"");
|
|
|
|
|
|
|
|
// If we have a parent table limit orphaned records
|
|
|
|
// to only those that exist in this
|
|
|
|
if(DB::get_schema()->hasTable($baseTable)) {
|
|
|
|
$orphanedQuery
|
|
|
|
->addLeftJoin(
|
|
|
|
$baseTable,
|
|
|
|
"\"{$childTable}\".\"RecordID\" = \"{$baseTable}\".\"RecordID\"
|
|
|
|
AND \"{$childTable}\".\"Version\" = \"{$baseTable}\".\"Version\""
|
|
|
|
)
|
|
|
|
->addWhere("\"{$baseTable}\".\"ID\" IS NULL");
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = $orphanedQuery->count();
|
|
|
|
if($count > 0) {
|
|
|
|
DB::alteration_message("Removing {$count} orphaned versioned records", "deleted");
|
|
|
|
$ids = $orphanedQuery->execute()->column();
|
|
|
|
foreach($ids as $id) {
|
|
|
|
DB::prepared_query("DELETE FROM \"{$childTable}\" WHERE \"ID\" = ?", array($id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-26 21:57:11 +01:00
|
|
|
/**
|
|
|
|
* Helper for augmentDatabase() to find unique indexes and convert them to non-unique
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-26 21:57:11 +01:00
|
|
|
* @param array $indexes The indexes to convert
|
|
|
|
* @return array $indexes
|
|
|
|
*/
|
|
|
|
private function uniqueToIndex($indexes) {
|
|
|
|
$unique_regex = '/unique/i';
|
|
|
|
$results = array();
|
|
|
|
foreach ($indexes as $key => $index) {
|
|
|
|
$results[$key] = $index;
|
|
|
|
|
|
|
|
// support string descriptors
|
|
|
|
if (is_string($index)) {
|
|
|
|
if (preg_match($unique_regex, $index)) {
|
|
|
|
$results[$key] = preg_replace($unique_regex, 'index', $index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// canonical, array-based descriptors
|
|
|
|
elseif (is_array($index)) {
|
|
|
|
if (strtolower($index['type']) == 'unique') {
|
|
|
|
$results[$key]['type'] = 'index';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2014-08-28 01:29:38 +02:00
|
|
|
/**
|
|
|
|
* Generates a ($table)_version DB manipulation and injects it into the current $manipulation
|
|
|
|
*
|
|
|
|
* @param array $manipulation Source manipulation data
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param string $class Class
|
|
|
|
* @param string $table Table Table for this class
|
2014-08-28 01:29:38 +02:00
|
|
|
* @param int $recordID ID of record to version
|
|
|
|
*/
|
2016-05-25 07:09:29 +02:00
|
|
|
protected function augmentWriteVersioned(&$manipulation, $class, $table, $recordID) {
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
$baseDataClass = $schema->baseDataClass($class);
|
|
|
|
$baseDataTable = $schema->tableName($baseDataClass);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-10-20 03:49:22 +02:00
|
|
|
// Set up a new entry in (table)_Versions
|
2014-08-28 01:29:38 +02:00
|
|
|
$newManipulation = array(
|
|
|
|
"command" => "insert",
|
2016-07-13 07:07:55 +02:00
|
|
|
"fields" => isset($manipulation[$table]['fields']) ? $manipulation[$table]['fields'] : [],
|
2016-05-25 07:09:29 +02:00
|
|
|
"class" => $class,
|
2014-08-28 01:29:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Add any extra, unchanged fields to the version record.
|
2016-05-25 07:09:29 +02:00
|
|
|
$data = DB::prepared_query("SELECT * FROM \"{$table}\" WHERE \"ID\" = ?", array($recordID))->record();
|
2015-02-08 06:42:36 +01:00
|
|
|
|
|
|
|
if ($data) {
|
2016-10-06 06:31:38 +02:00
|
|
|
$fields = $schema->databaseFields($class, false);
|
2015-02-08 06:42:36 +01:00
|
|
|
if (is_array($fields)) {
|
|
|
|
$data = array_intersect_key($data, $fields);
|
|
|
|
|
|
|
|
foreach ($data as $k => $v) {
|
2016-06-24 06:41:35 +02:00
|
|
|
// If the value is not set at all in the manipulation currently, use the existing value from the database
|
2016-07-13 07:07:55 +02:00
|
|
|
if (!array_key_exists($k, $newManipulation['fields'])) {
|
2015-02-08 06:42:36 +01:00
|
|
|
$newManipulation['fields'][$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 01:29:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the ID is instead written to the RecordID field
|
|
|
|
$newManipulation['fields']['RecordID'] = $recordID;
|
|
|
|
unset($newManipulation['fields']['ID']);
|
|
|
|
|
|
|
|
// Generate next version ID to use
|
|
|
|
$nextVersion = 0;
|
|
|
|
if($recordID) {
|
|
|
|
$nextVersion = DB::prepared_query("SELECT MAX(\"Version\") + 1
|
2016-10-20 03:49:22 +02:00
|
|
|
FROM \"{$baseDataTable}_Versions\" WHERE \"RecordID\" = ?",
|
2014-08-28 01:29:38 +02:00
|
|
|
array($recordID)
|
|
|
|
)->value();
|
|
|
|
}
|
|
|
|
$nextVersion = $nextVersion ?: 1;
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
if($class === $baseDataClass) {
|
2016-03-02 06:18:10 +01:00
|
|
|
// Write AuthorID for baseclass
|
2014-08-28 01:29:38 +02:00
|
|
|
$userID = (Member::currentUser()) ? Member::currentUser()->ID : 0;
|
|
|
|
$newManipulation['fields']['AuthorID'] = $userID;
|
2016-02-22 06:42:48 +01:00
|
|
|
|
|
|
|
// Update main table version if not previously known
|
|
|
|
$manipulation[$table]['fields']['Version'] = $nextVersion;
|
2014-08-28 01:29:38 +02:00
|
|
|
}
|
2016-02-22 06:42:48 +01:00
|
|
|
|
2016-10-20 03:49:22 +02:00
|
|
|
// Update _Versions table manipulation
|
2016-02-22 06:42:48 +01:00
|
|
|
$newManipulation['fields']['Version'] = $nextVersion;
|
2016-10-20 03:49:22 +02:00
|
|
|
$manipulation["{$table}_Versions"] = $newManipulation;
|
2014-08-28 01:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rewrite the given manipulation to update the selected (non-default) stage
|
|
|
|
*
|
|
|
|
* @param array $manipulation Source manipulation data
|
|
|
|
* @param string $table Name of table
|
|
|
|
* @param int $recordID ID of record to version
|
|
|
|
*/
|
|
|
|
protected function augmentWriteStaged(&$manipulation, $table, $recordID) {
|
|
|
|
// If the record has already been inserted in the (table), get rid of it.
|
|
|
|
if($manipulation[$table]['command'] == 'insert') {
|
|
|
|
DB::prepared_query(
|
|
|
|
"DELETE FROM \"{$table}\" WHERE \"ID\" = ?",
|
|
|
|
array($recordID)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
$newTable = $this->stageTable($table, Versioned::get_stage());
|
2014-08-28 01:29:38 +02:00
|
|
|
$manipulation[$newTable] = $manipulation[$table];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function augmentWrite(&$manipulation) {
|
2016-02-22 06:42:48 +01:00
|
|
|
// get Version number from base data table on write
|
|
|
|
$version = null;
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseDataTable = DataObject::getSchema()->baseDataTable($owner);
|
|
|
|
if(isset($manipulation[$baseDataTable]['fields'])) {
|
2016-02-22 06:42:48 +01:00
|
|
|
if ($this->migratingVersion) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$manipulation[$baseDataTable]['fields']['Version'] = $this->migratingVersion;
|
2016-02-22 06:42:48 +01:00
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
if (isset($manipulation[$baseDataTable]['fields']['Version'])) {
|
|
|
|
$version = $manipulation[$baseDataTable]['fields']['Version'];
|
2016-02-22 06:42:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update all tables
|
2007-07-19 12:40:28 +02:00
|
|
|
$tables = array_keys($manipulation);
|
|
|
|
foreach($tables as $table) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Make sure that the augmented write is being applied to a table that can be versioned
|
2016-05-25 07:09:29 +02:00
|
|
|
$class = isset($manipulation[$table]['class']) ? $manipulation[$table]['class'] : null;
|
|
|
|
if(!$class || !$this->canBeVersioned($class) ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
unset($manipulation[$table]);
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-08-28 01:29:38 +02:00
|
|
|
// Get ID field
|
2016-02-22 06:42:48 +01:00
|
|
|
$id = $manipulation[$table]['id']
|
|
|
|
? $manipulation[$table]['id']
|
|
|
|
: $manipulation[$table]['fields']['ID'];
|
2016-01-22 02:45:05 +01:00
|
|
|
if(!$id) {
|
|
|
|
user_error("Couldn't find ID in " . var_export($manipulation[$table], true), E_USER_ERROR);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-01-22 02:45:05 +01:00
|
|
|
if($version < 0 || $this->_nextWriteWithoutVersion) {
|
|
|
|
// Putting a Version of -1 is a signal to leave the version table alone, despite their being no version
|
|
|
|
unset($manipulation[$table]['fields']['Version']);
|
|
|
|
} elseif(empty($version)) {
|
|
|
|
// If we haven't got a version #, then we're creating a new version.
|
|
|
|
// Otherwise, we're just copying a version to another table
|
2016-05-25 07:09:29 +02:00
|
|
|
$this->augmentWriteVersioned($manipulation, $class, $table, $id);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-02-22 06:42:48 +01:00
|
|
|
// Remove "Version" column from subclasses of baseDataClass
|
2016-01-22 02:45:05 +01:00
|
|
|
if(!$this->hasVersionField($table)) {
|
2012-01-14 11:03:45 +01:00
|
|
|
unset($manipulation[$table]['fields']['Version']);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Grab a version number - it should be the same across all tables.
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($manipulation[$table]['fields']['Version'])) {
|
|
|
|
$thisVersion = $manipulation[$table]['fields']['Version'];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-18 07:19:59 +02:00
|
|
|
// If we're editing Live, then write to (table)_Live as well as (table)
|
2016-03-02 06:18:10 +01:00
|
|
|
if($this->hasStages() && static::get_stage() === static::LIVE) {
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->augmentWriteStaged($manipulation, $table, $id);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:55:15 +02:00
|
|
|
// Clear the migration flag
|
2013-05-10 11:19:16 +02:00
|
|
|
if($this->migratingVersion) {
|
|
|
|
$this->migrateVersion(null);
|
|
|
|
}
|
2010-10-13 03:04:38 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// Add the new version # back into the data object, for accessing
|
2013-05-10 11:19:16 +02:00
|
|
|
// after this write
|
|
|
|
if(isset($thisVersion)) {
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner->Version = str_replace("'","", $thisVersion);
|
2013-05-10 11:19:16 +02:00
|
|
|
}
|
2007-09-16 18:15:47 +02:00
|
|
|
}
|
2010-10-12 23:48:07 +02:00
|
|
|
|
2012-01-14 11:03:45 +01:00
|
|
|
/**
|
|
|
|
* Perform a write without affecting the version table.
|
|
|
|
* On objects without versioning.
|
|
|
|
*
|
|
|
|
* @return int The ID of the record
|
|
|
|
*/
|
|
|
|
public function writeWithoutVersion() {
|
|
|
|
$this->_nextWriteWithoutVersion = true;
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2012-01-14 11:03:45 +01:00
|
|
|
return $this->owner->write();
|
|
|
|
}
|
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onAfterWrite() {
|
2012-01-14 11:03:45 +01:00
|
|
|
$this->_nextWriteWithoutVersion = false;
|
|
|
|
}
|
|
|
|
|
2010-10-12 23:48:07 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* If a write was skipped, then we need to ensure that we don't leave a
|
2013-05-10 11:19:16 +02:00
|
|
|
* migrateVersion() value lying around for the next write.
|
2010-10-12 23:48:07 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onAfterSkippedWrite() {
|
2010-10-12 23:48:07 +02:00
|
|
|
$this->migrateVersion(null);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
/**
|
|
|
|
* Find all objects owned by the current object.
|
|
|
|
* Note that objects will only be searched in the same stage as the given record.
|
|
|
|
*
|
|
|
|
* @param bool $recursive True if recursive
|
|
|
|
* @param ArrayList $list Optional list to add items to
|
|
|
|
* @return ArrayList list of objects
|
|
|
|
*/
|
|
|
|
public function findOwned($recursive = true, $list = null)
|
|
|
|
{
|
|
|
|
// Find objects in these relationships
|
|
|
|
return $this->findRelatedObjects('owns', $recursive, $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find objects which own this object.
|
|
|
|
* Note that objects will only be searched in the same stage as the given record.
|
|
|
|
*
|
|
|
|
* @param bool $recursive True if recursive
|
|
|
|
* @param ArrayList $list Optional list to add items to
|
|
|
|
* @return ArrayList list of objects
|
|
|
|
*/
|
2016-03-22 02:39:25 +01:00
|
|
|
public function findOwners($recursive = true, $list = null) {
|
|
|
|
if (!$list) {
|
|
|
|
$list = new ArrayList();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build reverse lookup for ownership
|
|
|
|
// @todo - Cache this more intelligently
|
|
|
|
$rules = $this->lookupReverseOwners();
|
|
|
|
|
|
|
|
// Hand off to recursive method
|
|
|
|
return $this->findOwnersRecursive($recursive, $list, $rules);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find objects which own this object.
|
|
|
|
* Note that objects will only be searched in the same stage as the given record.
|
|
|
|
*
|
|
|
|
* @param bool $recursive True if recursive
|
|
|
|
* @param ArrayList $list List to add items to
|
|
|
|
* @param array $lookup List of reverse lookup rules for owned objects
|
|
|
|
* @return ArrayList list of objects
|
|
|
|
*/
|
|
|
|
public function findOwnersRecursive($recursive, $list, $lookup) {
|
|
|
|
// First pass: find objects that are explicitly owned_by (e.g. custom relationships)
|
|
|
|
$owners = $this->findRelatedObjects('owned_by', false);
|
|
|
|
|
|
|
|
// Second pass: Find owners via reverse lookup list
|
|
|
|
foreach($lookup as $ownedClass => $classLookups) {
|
|
|
|
// Skip owners of other objects
|
|
|
|
if(!is_a($this->owner, $ownedClass)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach($classLookups as $classLookup) {
|
|
|
|
// Merge new owners into this object's owners
|
|
|
|
$ownerClass = $classLookup['class'];
|
|
|
|
$ownerRelation = $classLookup['relation'];
|
|
|
|
$result = $this->owner->inferReciprocalComponent($ownerClass, $ownerRelation);
|
|
|
|
$this->mergeRelatedObjects($owners, $result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge all objects into the main list
|
|
|
|
$newItems = $this->mergeRelatedObjects($list, $owners);
|
|
|
|
|
|
|
|
// If recursing, iterate over all newly added items
|
|
|
|
if($recursive) {
|
|
|
|
foreach($newItems as $item) {
|
|
|
|
/** @var Versioned|DataObject $item */
|
|
|
|
$item->findOwnersRecursive(true, $list, $lookup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a list of classes, each of which with a list of methods to invoke
|
|
|
|
* to lookup owners.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function lookupReverseOwners() {
|
|
|
|
// Find all classes with 'owns' config
|
|
|
|
$lookup = array();
|
2016-06-15 06:03:16 +02:00
|
|
|
foreach(ClassInfo::subclassesFor('SilverStripe\ORM\DataObject') as $class) {
|
2016-03-22 02:39:25 +01:00
|
|
|
// Ensure this class is versioned
|
2016-10-20 03:49:22 +02:00
|
|
|
if(!Object::has_extension($class, static::class)) {
|
2016-03-22 02:39:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check owned objects for this class
|
|
|
|
$owns = Config::inst()->get($class, 'owns', Config::UNINHERITED);
|
|
|
|
if(empty($owns)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$instance = DataObject::singleton($class);
|
2016-03-22 02:39:25 +01:00
|
|
|
foreach($owns as $owned) {
|
|
|
|
// Find owned class
|
|
|
|
$ownedClass = $instance->getRelationClass($owned);
|
|
|
|
// Skip custom methods that don't have db relationsm
|
|
|
|
if(!$ownedClass) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-26 07:22:19 +02:00
|
|
|
if($ownedClass === DataObject::class) {
|
2016-03-22 02:39:25 +01:00
|
|
|
throw new LogicException(sprintf(
|
|
|
|
"Relation %s on class %s cannot be owned as it is polymorphic",
|
|
|
|
$owned, $class
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add lookup for owned class
|
|
|
|
if(!isset($lookup[$ownedClass])) {
|
|
|
|
$lookup[$ownedClass] = array();
|
|
|
|
}
|
|
|
|
$lookup[$ownedClass][] = [
|
|
|
|
'class' => $class,
|
|
|
|
'relation' => $owned
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $lookup;
|
2016-02-25 05:32:41 +01:00
|
|
|
}
|
|
|
|
|
2016-03-22 02:39:25 +01:00
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
/**
|
|
|
|
* Find objects in the given relationships, merging them into the given list
|
|
|
|
*
|
|
|
|
* @param array $source Config property to extract relationships from
|
|
|
|
* @param bool $recursive True if recursive
|
|
|
|
* @param ArrayList $list Optional list to add items to
|
|
|
|
* @return ArrayList The list
|
|
|
|
*/
|
|
|
|
public function findRelatedObjects($source, $recursive = true, $list = null)
|
|
|
|
{
|
|
|
|
if (!$list) {
|
|
|
|
$list = new ArrayList();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip search for unsaved records
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
if(!$owner->isInDB()) {
|
2016-02-25 05:32:41 +01:00
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
$relationships = $owner->config()->{$source};
|
2016-02-25 05:32:41 +01:00
|
|
|
foreach($relationships as $relationship) {
|
|
|
|
// Warn if invalid config
|
2016-03-02 06:18:10 +01:00
|
|
|
if(!$owner->hasMethod($relationship)) {
|
2016-02-25 05:32:41 +01:00
|
|
|
trigger_error(sprintf(
|
|
|
|
"Invalid %s config value \"%s\" on object on class \"%s\"",
|
|
|
|
$source,
|
|
|
|
$relationship,
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner->class
|
2016-02-25 05:32:41 +01:00
|
|
|
), E_USER_WARNING);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inspect value of this relationship
|
2016-03-02 06:18:10 +01:00
|
|
|
$items = $owner->{$relationship}();
|
2016-02-25 05:32:41 +01:00
|
|
|
|
2016-03-22 02:39:25 +01:00
|
|
|
// Merge any new item
|
|
|
|
$newItems = $this->mergeRelatedObjects($list, $items);
|
2016-02-25 05:32:41 +01:00
|
|
|
|
2016-03-22 02:39:25 +01:00
|
|
|
// Recurse if necessary
|
|
|
|
if($recursive) {
|
|
|
|
foreach($newItems as $item) {
|
|
|
|
/** @var Versioned|DataObject $item */
|
2016-02-25 05:32:41 +01:00
|
|
|
$item->findRelatedObjects($source, true, $list);
|
2016-03-22 02:39:25 +01:00
|
|
|
}
|
2016-02-25 05:32:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:39:25 +01:00
|
|
|
/**
|
|
|
|
* Helper method to merge owned/owning items into a list.
|
|
|
|
* Items already present in the list will be skipped.
|
|
|
|
*
|
|
|
|
* @param ArrayList $list Items to merge into
|
|
|
|
* @param mixed $items List of new items to merge
|
|
|
|
* @return ArrayList List of all newly added items that did not already exist in $list
|
|
|
|
*/
|
|
|
|
protected function mergeRelatedObjects($list, $items) {
|
|
|
|
$added = new ArrayList();
|
|
|
|
if(!$items) {
|
|
|
|
return $added;
|
|
|
|
}
|
|
|
|
if($items instanceof DataObject) {
|
|
|
|
$items = array($items);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Versioned|DataObject $item */
|
|
|
|
foreach($items as $item) {
|
2016-09-26 07:22:19 +02:00
|
|
|
$this->mergeRelatedObject($list, $added, $item);
|
2016-03-22 02:39:25 +01:00
|
|
|
}
|
|
|
|
return $added;
|
|
|
|
}
|
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
/**
|
|
|
|
* This function should return true if the current user can publish this record.
|
|
|
|
* It can be overloaded to customise the security model for an application.
|
|
|
|
*
|
|
|
|
* Denies permission if any of the following conditions is true:
|
|
|
|
* - canPublish() on any extension returns false
|
|
|
|
* - canEdit() returns false
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool True if the current user can publish this record.
|
|
|
|
*/
|
|
|
|
public function canPublish($member = null) {
|
|
|
|
// Skip if invoked by extendedCan()
|
|
|
|
if(func_num_args() > 4) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Permission::checkMember($member, "ADMIN")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standard mechanism for accepting permission changes from extensions
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
$extended = $owner->extendedCan('canPublish', $member);
|
2016-01-26 04:56:07 +01:00
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to relying on edit permission
|
2016-03-02 06:18:10 +01:00
|
|
|
return $owner->canEdit($member);
|
2016-01-26 04:56:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the current user can delete this record from live
|
|
|
|
*
|
|
|
|
* @param null $member
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function canUnpublish($member = null) {
|
|
|
|
// Skip if invoked by extendedCan()
|
|
|
|
if(func_num_args() > 4) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Permission::checkMember($member, "ADMIN")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standard mechanism for accepting permission changes from extensions
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
$extended = $owner->extendedCan('canUnpublish', $member);
|
2016-01-26 04:56:07 +01:00
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to relying on canPublish
|
2016-03-02 06:18:10 +01:00
|
|
|
return $owner->canPublish($member);
|
2016-01-26 04:56:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the current user is allowed to archive this record.
|
|
|
|
* If extended, ensure that both canDelete and canUnpublish are extended also
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canArchive($member = null) {
|
|
|
|
// Skip if invoked by extendedCan()
|
|
|
|
if(func_num_args() > 4) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standard mechanism for accepting permission changes from extensions
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
$extended = $owner->extendedCan('canArchive', $member);
|
2016-01-26 04:56:07 +01:00
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
2016-10-18 07:19:59 +02:00
|
|
|
// Admin permissions allow
|
|
|
|
if(Permission::checkMember($member, "ADMIN")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
// Check if this record can be deleted from stage
|
2016-03-02 06:18:10 +01:00
|
|
|
if(!$owner->canDelete($member)) {
|
2016-01-26 04:56:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we can delete from live
|
2016-03-02 06:18:10 +01:00
|
|
|
if(!$owner->canUnpublish($member)) {
|
2016-01-26 04:56:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
/**
|
|
|
|
* Check if the user can revert this record to live
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canRevertToLive($member = null) {
|
|
|
|
$owner = $this->owner;
|
|
|
|
|
|
|
|
// Skip if invoked by extendedCan()
|
|
|
|
if(func_num_args() > 4) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't revert if not on live
|
|
|
|
if(!$owner->isPublished()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Permission::checkMember($member, "ADMIN")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standard mechanism for accepting permission changes from extensions
|
|
|
|
$extended = $owner->extendedCan('canRevertToLive', $member);
|
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to canEdit
|
|
|
|
return $owner->canEdit($member);
|
|
|
|
}
|
|
|
|
|
2015-12-02 05:33:25 +01:00
|
|
|
/**
|
|
|
|
* Extend permissions to include additional security for objects that are not published to live.
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool|null
|
|
|
|
*/
|
|
|
|
public function canView($member = null) {
|
|
|
|
// Invoke default version-gnostic canView
|
|
|
|
if ($this->owner->canViewVersioned($member) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2015-12-02 05:33:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if there are any additional restrictions on this object for the given reading version.
|
|
|
|
*
|
|
|
|
* Override this in a subclass to customise any additional effect that Versioned applies to canView.
|
|
|
|
*
|
|
|
|
* This is expected to be called by canView, and thus is only responsible for denying access if
|
|
|
|
* the default canView would otherwise ALLOW access. Thus it should not be called in isolation
|
|
|
|
* as an authoritative permission check.
|
|
|
|
*
|
|
|
|
* This has the following extension points:
|
|
|
|
* - canViewDraft is invoked if Mode = stage and Stage = stage
|
|
|
|
* - canViewArchived is invoked if Mode = archive
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool False is returned if the current viewing mode denies visibility
|
|
|
|
*/
|
|
|
|
public function canViewVersioned($member = null) {
|
|
|
|
// Bypass when live stage
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
$mode = $owner->getSourceQueryParam("Versioned.mode");
|
|
|
|
$stage = $owner->getSourceQueryParam("Versioned.stage");
|
|
|
|
if ($mode === 'stage' && $stage === static::LIVE) {
|
2015-12-02 05:33:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bypass if site is unsecured
|
|
|
|
if (Session::get('unsecuredDraftSite')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
// Bypass if record doesn't have a live stage
|
2016-03-02 06:18:10 +01:00
|
|
|
if(!$this->hasStages()) {
|
2016-01-26 04:56:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-02 05:33:25 +01:00
|
|
|
// If we weren't definitely loaded from live, and we can't view non-live content, we need to
|
|
|
|
// check to make sure this version is the live version and so can be viewed
|
2016-03-02 06:18:10 +01:00
|
|
|
$latestVersion = Versioned::get_versionnumber_by_stage($owner->class, static::LIVE, $owner->ID);
|
|
|
|
if ($latestVersion == $owner->Version) {
|
2015-12-02 05:33:25 +01:00
|
|
|
// Even if this is loaded from a non-live stage, this is the live version
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extend versioned behaviour
|
2016-03-02 06:18:10 +01:00
|
|
|
$extended = $owner->extendedCan('canViewNonLive', $member);
|
2015-12-02 05:33:25 +01:00
|
|
|
if($extended !== null) {
|
|
|
|
return (bool)$extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to default permission check
|
2016-03-02 06:18:10 +01:00
|
|
|
$permissions = Config::inst()->get($owner->class, 'non_live_permissions', Config::FIRST_SET);
|
2015-12-02 05:33:25 +01:00
|
|
|
$check = Permission::checkMember($member, $permissions);
|
|
|
|
return (bool)$check;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines canView permissions for the latest version of this object on a specific stage.
|
|
|
|
* Usually the stage is read from {@link Versioned::current_stage()}.
|
|
|
|
*
|
|
|
|
* This method should be invoked by user code to check if a record is visible in the given stage.
|
|
|
|
*
|
|
|
|
* This method should not be called via ->extend('canViewStage'), but rather should be
|
|
|
|
* overridden in the extended class.
|
|
|
|
*
|
|
|
|
* @param string $stage
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canViewStage($stage = 'Live', $member = null) {
|
|
|
|
$oldMode = Versioned::get_reading_mode();
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_stage($stage);
|
2015-12-02 05:33:25 +01:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
$versionFromStage = DataObject::get($owner->class)->byID($owner->ID);
|
2015-12-02 05:33:25 +01:00
|
|
|
|
|
|
|
Versioned::set_reading_mode($oldMode);
|
|
|
|
return $versionFromStage ? $versionFromStage->canView($member) : false;
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:58:19 +02:00
|
|
|
/**
|
2016-05-25 07:09:29 +02:00
|
|
|
* Determine if a class is supporting the Versioned extensions (e.g.
|
2016-10-20 03:49:22 +02:00
|
|
|
* $table_Versions does exists).
|
2007-09-16 18:58:19 +02:00
|
|
|
*
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param string $class Class name
|
2007-09-16 18:58:19 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-05-25 07:09:29 +02:00
|
|
|
public function canBeVersioned($class) {
|
|
|
|
return ClassInfo::exists($class)
|
2016-10-06 06:31:38 +02:00
|
|
|
&& is_subclass_of($class, DataObject::class)
|
|
|
|
&& DataObject::getSchema()->classHasTable($class);
|
2007-09-16 18:15:47 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-16 18:58:19 +02:00
|
|
|
/**
|
2013-05-10 11:19:16 +02:00
|
|
|
* Check if a certain table has the 'Version' field.
|
2007-09-16 18:58:19 +02:00
|
|
|
*
|
|
|
|
* @param string $table Table name
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-09-16 18:58:19 +02:00
|
|
|
* @return boolean Returns false if the field isn't in the table, true otherwise
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function hasVersionField($table) {
|
2016-03-02 06:18:10 +01:00
|
|
|
// Base table has version field
|
2016-05-25 07:09:29 +02:00
|
|
|
$class = DataObject::getSchema()->tableClass($table);
|
|
|
|
return $class === DataObject::getSchema()->baseDataClass($class);
|
2007-09-16 18:58:19 +02:00
|
|
|
}
|
2013-05-10 11:19:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $table
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function extendWithSuffix($table) {
|
2016-02-25 02:51:59 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
$versionableExtensions = $owner->config()->versionableExtensions;
|
2016-02-23 21:53:52 +01:00
|
|
|
|
|
|
|
if(count($versionableExtensions)){
|
|
|
|
foreach ($versionableExtensions as $versionableExtension => $suffixes) {
|
2016-02-25 02:51:59 +01:00
|
|
|
if ($owner->hasExtension($versionableExtension)) {
|
|
|
|
$ext = $owner->getExtensionInstance($versionableExtension);
|
|
|
|
$ext->setOwner($owner);
|
2016-05-25 07:09:29 +02:00
|
|
|
$table = $ext->extendWithSuffix($table);
|
|
|
|
$ext->clearOwner();
|
|
|
|
}
|
2007-09-16 18:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2007-09-16 18:15:47 +02:00
|
|
|
return $table;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* Determines if the current draft version is the same as live
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2016-09-07 10:31:23 +02:00
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function latestPublished() {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Get the root data object class - this will have the version field
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
2016-05-25 07:09:29 +02:00
|
|
|
$draftTable = $this->baseTable();
|
|
|
|
$liveTable = $this->stageTable($draftTable, static::LIVE);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
return DB::prepared_query("SELECT \"$draftTable\".\"Version\" = \"$liveTable\".\"Version\" FROM \"$draftTable\"
|
|
|
|
INNER JOIN \"$liveTable\" ON \"$draftTable\".\"ID\" = \"$liveTable\".\"ID\"
|
|
|
|
WHERE \"$draftTable\".\"ID\" = ?",
|
2016-03-02 06:18:10 +01:00
|
|
|
array($owner->ID)
|
2013-06-21 00:32:08 +02:00
|
|
|
)->value();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
/**
|
2016-04-01 05:27:59 +02:00
|
|
|
* @deprecated 4.0..5.0
|
2016-01-26 04:56:07 +01:00
|
|
|
*/
|
|
|
|
public function doPublish() {
|
2016-04-01 05:27:59 +02:00
|
|
|
Deprecation::notice('5.0', 'Use publishRecursive instead');
|
|
|
|
return $this->owner->publishRecursive();
|
2016-01-26 04:56:07 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
/**
|
2016-04-01 05:27:59 +02:00
|
|
|
* Publish this object and all owned objects to Live
|
|
|
|
*
|
|
|
|
* @return bool
|
2016-03-02 06:18:10 +01:00
|
|
|
*/
|
2016-04-01 05:27:59 +02:00
|
|
|
public function publishRecursive() {
|
2016-10-12 01:19:07 +02:00
|
|
|
// Create a new changeset for this item and publish it
|
|
|
|
$changeset = ChangeSet::create();
|
|
|
|
$changeset->IsInferred = true;
|
|
|
|
$changeset->Name = _t(
|
|
|
|
'Versioned.INFERRED_TITLE',
|
|
|
|
"Generated by publish of '{title}' at {created}",
|
|
|
|
[
|
|
|
|
'title' => $this->owner->Title,
|
|
|
|
'created' => DBDatetime::now()->Nice()
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$changeset->write();
|
|
|
|
$changeset->addObject($this->owner);
|
|
|
|
return $changeset->publish();
|
2016-04-01 05:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publishes this object to Live, but doesn't publish owned objects.
|
|
|
|
*
|
|
|
|
* @return bool True if publish was successful
|
|
|
|
*/
|
|
|
|
public function publishSingle() {
|
|
|
|
$owner = $this->owner;
|
|
|
|
if(!$owner->canPublish()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$owner->invokeWithExtensions('onBeforePublish');
|
|
|
|
$owner->write();
|
|
|
|
$owner->copyVersionToStage(static::DRAFT, static::LIVE);
|
|
|
|
$owner->invokeWithExtensions('onAfterPublish');
|
|
|
|
return true;
|
2016-03-02 06:18:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set foreign keys of has_many objects to 0 where those objects were
|
|
|
|
* disowned as a result of a partial publish / unpublish.
|
|
|
|
* I.e. this object and its owned objects were recently written to $targetStage,
|
|
|
|
* but deleted objects were not.
|
|
|
|
*
|
|
|
|
* Note that this operation does not create any new Versions
|
|
|
|
*
|
|
|
|
* @param string $sourceStage Objects in this stage will not be unlinked.
|
|
|
|
* @param string $targetStage Objects which exist in this stage but not $sourceStage
|
|
|
|
* will be unlinked.
|
|
|
|
*/
|
|
|
|
public function unlinkDisownedObjects($sourceStage, $targetStage) {
|
|
|
|
$owner = $this->owner;
|
|
|
|
|
|
|
|
// after publishing, objects which used to be owned need to be
|
|
|
|
// dis-connected from this object (set ForeignKeyID = 0)
|
|
|
|
$owns = $owner->config()->owns;
|
|
|
|
$hasMany = $owner->config()->has_many;
|
|
|
|
if(empty($owns) || empty($hasMany)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = DataObject::getSchema();
|
2016-03-02 06:18:10 +01:00
|
|
|
$ownedHasMany = array_intersect($owns, array_keys($hasMany));
|
|
|
|
foreach($ownedHasMany as $relationship) {
|
|
|
|
// Find metadata on relationship
|
2016-10-06 06:31:38 +02:00
|
|
|
$joinClass = $schema->hasManyComponent(get_class($owner), $relationship);
|
|
|
|
$joinField = $schema->getRemoteJoinField(get_class($owner), $relationship, 'has_many', $polymorphic);
|
2016-03-02 06:18:10 +01:00
|
|
|
$idField = $polymorphic ? "{$joinField}ID" : $joinField;
|
2016-05-25 07:09:29 +02:00
|
|
|
$joinTable = DataObject::getSchema()->tableForField($joinClass, $idField);
|
2016-03-02 06:18:10 +01:00
|
|
|
|
|
|
|
// Generate update query which will unlink disowned objects
|
|
|
|
$targetTable = $this->stageTable($joinTable, $targetStage);
|
|
|
|
$disowned = new SQLUpdate("\"{$targetTable}\"");
|
|
|
|
$disowned->assign("\"{$idField}\"", 0);
|
|
|
|
$disowned->addWhere(array(
|
|
|
|
"\"{$targetTable}\".\"{$idField}\"" => $owner->ID
|
|
|
|
));
|
|
|
|
|
|
|
|
// Build exclusion list (items to owned objects we need to keep)
|
|
|
|
$sourceTable = $this->stageTable($joinTable, $sourceStage);
|
|
|
|
$owned = new SQLSelect("\"{$sourceTable}\".\"ID\"", "\"{$sourceTable}\"");
|
|
|
|
$owned->addWhere(array(
|
|
|
|
"\"{$sourceTable}\".\"{$idField}\"" => $owner->ID
|
|
|
|
));
|
|
|
|
|
|
|
|
// Apply class condition if querying on polymorphic has_one
|
|
|
|
if($polymorphic) {
|
|
|
|
$disowned->assign("\"{$joinField}Class\"", null);
|
|
|
|
$disowned->addWhere(array(
|
|
|
|
"\"{$targetTable}\".\"{$joinField}Class\"" => get_class($owner)
|
|
|
|
));
|
|
|
|
$owned->addWhere(array(
|
|
|
|
"\"{$sourceTable}\".\"{$joinField}Class\"" => get_class($owner)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge queries and perform unlink
|
|
|
|
$ownedSQL = $owned->sql($ownedParams);
|
|
|
|
$disowned->addWhere(array(
|
|
|
|
"\"{$targetTable}\".\"ID\" NOT IN ({$ownedSQL})" => $ownedParams
|
|
|
|
));
|
2016-01-26 04:56:07 +01:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner->extend('updateDisownershipQuery', $disowned, $sourceStage, $targetStage, $relationship);
|
|
|
|
|
|
|
|
$disowned->execute();
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 04:56:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the record from both live and stage
|
|
|
|
*
|
|
|
|
* @return bool Success
|
|
|
|
*/
|
|
|
|
public function doArchive() {
|
|
|
|
$owner = $this->owner;
|
2016-03-02 06:18:10 +01:00
|
|
|
if(!$owner->canArchive()) {
|
|
|
|
return false;
|
2016-01-26 04:56:07 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner->invokeWithExtensions('onBeforeArchive', $this);
|
|
|
|
$owner->doUnpublish();
|
|
|
|
$owner->delete();
|
|
|
|
$owner->invokeWithExtensions('onAfterArchive', $this);
|
|
|
|
|
|
|
|
return true;
|
2016-01-26 04:56:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes this record from the live site
|
|
|
|
*
|
|
|
|
* @return bool Flag whether the unpublish was successful
|
|
|
|
*/
|
|
|
|
public function doUnpublish() {
|
|
|
|
$owner = $this->owner;
|
2016-03-02 06:18:10 +01:00
|
|
|
if(!$owner->canUnpublish()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if this record isn't saved
|
2016-01-26 04:56:07 +01:00
|
|
|
if(!$owner->isInDB()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
// Skip if this record isn't on live
|
|
|
|
if(!$owner->isPublished()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
$owner->invokeWithExtensions('onBeforeUnpublish');
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
$origReadingMode = static::get_reading_mode();
|
2016-03-02 06:18:10 +01:00
|
|
|
static::set_stage(static::LIVE);
|
2016-01-26 04:56:07 +01:00
|
|
|
|
|
|
|
// This way our ID won't be unset
|
|
|
|
$clone = clone $owner;
|
|
|
|
$clone->delete();
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
static::set_reading_mode($origReadingMode);
|
2016-03-02 06:18:10 +01:00
|
|
|
|
|
|
|
$owner->invokeWithExtensions('onAfterUnpublish');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger unpublish of owning objects
|
|
|
|
*/
|
|
|
|
public function onAfterUnpublish() {
|
|
|
|
$owner = $this->owner;
|
2016-01-26 04:56:07 +01:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
// Any objects which owned (and thus relied on the unpublished object) are now unpublished automatically.
|
|
|
|
foreach ($owner->findOwners(false) as $object) {
|
|
|
|
/** @var Versioned|DataObject $object */
|
|
|
|
$object->doUnpublish();
|
2016-01-26 04:56:07 +01:00
|
|
|
}
|
2016-03-02 06:18:10 +01:00
|
|
|
}
|
2016-01-26 04:56:07 +01:00
|
|
|
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
/**
|
|
|
|
* Revert the draft changes: replace the draft content with the content on live
|
|
|
|
*
|
|
|
|
* @return bool True if the revert was successful
|
|
|
|
*/
|
|
|
|
public function doRevertToLive() {
|
|
|
|
$owner = $this->owner;
|
|
|
|
if(!$owner->canRevertToLive()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$owner->invokeWithExtensions('onBeforeRevertToLive');
|
2016-04-01 05:27:59 +02:00
|
|
|
$owner->copyVersionToStage(static::LIVE, static::DRAFT, false);
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner->invokeWithExtensions('onAfterRevertToLive');
|
2016-01-26 04:56:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
/**
|
|
|
|
* Trigger revert of all owned objects to stage
|
|
|
|
*/
|
|
|
|
public function onAfterRevertToLive() {
|
|
|
|
$owner = $this->owner;
|
|
|
|
/** @var Versioned|DataObject $liveOwner */
|
|
|
|
$liveOwner = static::get_by_stage(get_class($owner), static::LIVE)
|
|
|
|
->byID($owner->ID);
|
|
|
|
|
|
|
|
// Revert any owned objects from the live stage only
|
|
|
|
foreach ($liveOwner->findOwned(false) as $object) {
|
|
|
|
/** @var Versioned|DataObject $object */
|
|
|
|
$object->doRevertToLive();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlink any objects disowned as a result of this action
|
|
|
|
// I.e. objects which aren't owned anymore by this record, but are by the old draft record
|
|
|
|
$owner->unlinkDisownedObjects(Versioned::LIVE, Versioned::DRAFT);
|
|
|
|
}
|
|
|
|
|
2016-04-01 05:27:59 +02:00
|
|
|
/**
|
|
|
|
* @deprecated 4.0..5.0
|
|
|
|
*/
|
|
|
|
public function publish($fromStage, $toStage, $createNewVersion = false) {
|
|
|
|
Deprecation::notice('5.0', 'Use copyVersionToStage instead');
|
|
|
|
$this->owner->copyVersionToStage($fromStage, $toStage, $createNewVersion);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Move a database record from one stage to the other.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2016-02-22 06:42:48 +01:00
|
|
|
* @param int|string $fromStage Place to copy from. Can be either a stage name or a version number.
|
2015-12-09 22:19:23 +01:00
|
|
|
* @param string $toStage Place to copy to. Must be a stage name.
|
|
|
|
* @param bool $createNewVersion Set this to true to create a new version number.
|
2016-10-18 07:19:59 +02:00
|
|
|
* By default, the existing version number will be copied over. Note if copying
|
|
|
|
* to the live stage, the draft stage will also be updated with the new version.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-04-01 05:27:59 +02:00
|
|
|
public function copyVersionToStage($fromStage, $toStage, $createNewVersion = false) {
|
2016-01-26 04:56:07 +01:00
|
|
|
$owner = $this->owner;
|
2016-02-25 05:32:41 +01:00
|
|
|
$owner->invokeWithExtensions('onBeforeVersionedPublish', $fromStage, $toStage, $createNewVersion);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseClass = $owner->baseClass();
|
2016-08-04 05:30:04 +02:00
|
|
|
$baseTable = $owner->baseTable();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-02-22 06:42:48 +01:00
|
|
|
/** @var Versioned|DataObject $from */
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_numeric($fromStage)) {
|
2016-01-26 04:56:07 +01:00
|
|
|
$from = Versioned::get_version($baseClass, $owner->ID, $fromStage);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner->flushCache();
|
2016-02-22 06:42:48 +01:00
|
|
|
$from = Versioned::get_one_by_stage($baseClass, $fromStage, array(
|
2016-08-04 05:30:04 +02:00
|
|
|
"\"{$baseTable}\".\"ID\" = ?" => $owner->ID
|
2016-02-22 06:42:48 +01:00
|
|
|
));
|
2016-01-26 04:56:07 +01:00
|
|
|
}
|
|
|
|
if(!$from) {
|
|
|
|
throw new InvalidArgumentException("Can't find {$baseClass}#{$owner->ID} in stage {$fromStage}");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
$from->forceChange();
|
|
|
|
if($createNewVersion) {
|
2016-02-22 06:42:48 +01:00
|
|
|
// Clear version to be automatically created on write
|
|
|
|
$from->Version = null;
|
2016-01-26 04:56:07 +01:00
|
|
|
} else {
|
|
|
|
$from->migrateVersion($from->Version);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
// Mark this version as having been published at some stage
|
2016-02-22 06:42:48 +01:00
|
|
|
$publisherID = isset(Member::currentUser()->ID) ? Member::currentUser()->ID : 0;
|
2016-08-04 05:30:04 +02:00
|
|
|
$extTable = $this->extendWithSuffix($baseTable);
|
2016-10-20 03:49:22 +02:00
|
|
|
DB::prepared_query("UPDATE \"{$extTable}_Versions\"
|
2016-03-02 06:18:10 +01:00
|
|
|
SET \"WasPublished\" = ?, \"PublisherID\" = ?
|
|
|
|
WHERE \"RecordID\" = ? AND \"Version\" = ?",
|
|
|
|
array(1, $publisherID, $from->ID, $from->Version)
|
|
|
|
);
|
2016-02-22 06:42:48 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2016-02-22 06:42:48 +01:00
|
|
|
// Change to new stage, write, and revert state
|
2016-01-26 04:56:07 +01:00
|
|
|
$oldMode = Versioned::get_reading_mode();
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_stage($toStage);
|
2009-07-02 00:27:18 +02:00
|
|
|
|
2016-02-25 03:41:32 +01:00
|
|
|
// Migrate stage prior to write
|
|
|
|
$from->setSourceQueryParam('Versioned.mode', 'stage');
|
|
|
|
$from->setSourceQueryParam('Versioned.stage', $toStage);
|
|
|
|
|
|
|
|
$conn = DB::get_conn();
|
2016-02-22 06:42:48 +01:00
|
|
|
if(method_exists($conn, 'allowPrimaryKeyEditing')) {
|
2016-08-04 05:30:04 +02:00
|
|
|
$conn->allowPrimaryKeyEditing($baseTable, true);
|
2007-07-19 12:40:28 +02:00
|
|
|
$from->write();
|
2016-08-04 05:30:04 +02:00
|
|
|
$conn->allowPrimaryKeyEditing($baseTable, false);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2016-02-22 06:42:48 +01:00
|
|
|
$from->write();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-07-02 00:27:18 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
$from->destroy();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
Versioned::set_reading_mode($oldMode);
|
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
$owner->invokeWithExtensions('onAfterVersionedPublish', $fromStage, $toStage, $createNewVersion);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the migrating version.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $version The version.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function migrateVersion($version) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->migratingVersion = $version;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Compare two stages to see if they're different.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* Only checks the version numbers, not the actual content.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $stage1 The first stage to check.
|
|
|
|
* @param string $stage2
|
2016-03-02 06:18:10 +01:00
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function stagesDiffer($stage1, $stage2) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$table1 = $this->baseTable($stage1);
|
|
|
|
$table2 = $this->baseTable($stage2);
|
2016-10-05 03:16:09 +02:00
|
|
|
$id = $this->owner->ID ?: $this->owner->OldID;
|
|
|
|
if (!$id) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-05-28 11:13:42 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// We test for equality - if one of the versions doesn't exist, this
|
2013-05-10 11:19:16 +02:00
|
|
|
// will be false.
|
|
|
|
|
|
|
|
// TODO: DB Abstraction: if statement here:
|
2013-06-21 00:32:08 +02:00
|
|
|
$stagesAreEqual = DB::prepared_query(
|
|
|
|
"SELECT CASE WHEN \"$table1\".\"Version\"=\"$table2\".\"Version\" THEN 1 ELSE 0 END
|
|
|
|
FROM \"$table1\" INNER JOIN \"$table2\" ON \"$table1\".\"ID\" = \"$table2\".\"ID\"
|
|
|
|
AND \"$table1\".\"ID\" = ?",
|
2016-10-05 03:16:09 +02:00
|
|
|
array($id)
|
2013-06-21 00:32:08 +02:00
|
|
|
)->value();
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return !$stagesAreEqual;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-09 18:20:19 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $filter
|
|
|
|
* @param string $sort
|
|
|
|
* @param string $limit
|
2013-01-09 18:20:19 +01:00
|
|
|
* @param string $join Deprecated, use leftJoin($table, $joinClause) instead
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $having
|
2016-03-02 06:18:10 +01:00
|
|
|
* @return ArrayList
|
2013-01-09 18:20:19 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Versions($filter = "", $sort = "", $limit = "", $join = "", $having = "") {
|
2009-09-14 07:15:49 +02:00
|
|
|
return $this->allVersions($filter, $sort, $limit, $join, $having);
|
2008-08-11 01:35:11 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return a list of all the versions available.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @param string $filter
|
|
|
|
* @param string $sort
|
|
|
|
* @param string $limit
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $join @deprecated use leftJoin($table, $joinClause) instead
|
|
|
|
* @param string $having @deprecated
|
2016-02-25 05:32:41 +01:00
|
|
|
* @return ArrayList
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2011-09-27 04:14:01 +02:00
|
|
|
public function allVersions($filter = "", $sort = "", $limit = "", $join = "", $having = "") {
|
2010-10-13 02:50:48 +02:00
|
|
|
// Make sure the table names are not postfixed (e.g. _Live)
|
2016-03-02 06:18:10 +01:00
|
|
|
$oldMode = static::get_reading_mode();
|
2016-04-01 05:27:59 +02:00
|
|
|
static::set_stage(static::DRAFT);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
$list = DataObject::get(get_class($owner), $filter, $sort, $join, $limit);
|
|
|
|
if($having) {
|
2016-08-19 00:51:35 +02:00
|
|
|
// @todo - This method doesn't exist on DataList
|
2016-03-02 06:18:10 +01:00
|
|
|
$list->having($having);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-10-29 06:12:37 +02:00
|
|
|
$query = $list->dataQuery()->query();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$baseTable = null;
|
2012-05-03 09:34:16 +02:00
|
|
|
foreach($query->getFrom() as $table => $tableJoin) {
|
2011-09-27 04:14:01 +02:00
|
|
|
if(is_string($tableJoin) && $tableJoin[0] == '"') {
|
|
|
|
$baseTable = str_replace('"','',$tableJoin);
|
|
|
|
} elseif(is_string($tableJoin) && substr($tableJoin,0,5) != 'INNER') {
|
2012-09-26 23:34:00 +02:00
|
|
|
$query->setFrom(array(
|
2016-10-20 03:49:22 +02:00
|
|
|
$table => "LEFT JOIN \"$table\" ON \"$table\".\"RecordID\"=\"{$baseTable}_Versions\".\"RecordID\""
|
|
|
|
. " AND \"$table\".\"Version\" = \"{$baseTable}_Versions\".\"Version\""
|
2012-09-26 23:34:00 +02:00
|
|
|
));
|
2011-09-27 04:14:01 +02:00
|
|
|
}
|
2016-10-20 03:49:22 +02:00
|
|
|
$query->renameTable($table, $table . '_Versions');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-20 03:49:22 +02:00
|
|
|
// Add all <basetable>_Versions columns
|
|
|
|
foreach(Config::inst()->get(static::class, 'db_for_versions_table') as $name => $type) {
|
|
|
|
$query->selectField(sprintf('"%s_Versions"."%s"', $baseTable, $name), $name);
|
2009-05-12 06:47:56 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->addWhere(array(
|
2016-10-20 03:49:22 +02:00
|
|
|
"\"{$baseTable}_Versions\".\"RecordID\" = ?" => $owner->ID
|
2013-06-21 00:32:08 +02:00
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
$query->setOrderBy(($sort) ? $sort
|
2016-10-20 03:49:22 +02:00
|
|
|
: "\"{$baseTable}_Versions\".\"LastEdited\" DESC, \"{$baseTable}_Versions\".\"Version\" DESC");
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$records = $query->execute();
|
2011-05-05 12:40:24 +02:00
|
|
|
$versions = new ArrayList();
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($records as $record) {
|
|
|
|
$versions->push(new Versioned_Version($record));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 02:50:48 +02:00
|
|
|
Versioned::set_reading_mode($oldMode);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $versions;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Compare two version, and return the diff between them.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $from The version to compare from.
|
|
|
|
* @param string $to The version to compare to.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return DataObject
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function compareVersions($from, $to) {
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
$fromRecord = Versioned::get_version($owner->class, $owner->ID, $from);
|
|
|
|
$toRecord = Versioned::get_version($owner->class, $owner->ID, $to);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-23 05:29:33 +02:00
|
|
|
$diff = new DataDifferencer($fromRecord, $toRecord);
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2009-05-23 05:29:33 +02:00
|
|
|
return $diff->diffedData();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the base table - the class that directly extends DataObject.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2016-05-25 07:09:29 +02:00
|
|
|
* Protected so it doesn't conflict with DataObject::baseTable()
|
|
|
|
*
|
2016-02-25 05:32:41 +01:00
|
|
|
* @param string $stage
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-05-25 07:09:29 +02:00
|
|
|
protected function baseTable($stage = null) {
|
|
|
|
$baseTable = $this->owner->baseTable();
|
|
|
|
return $this->stageTable($baseTable, $stage);
|
2016-03-02 06:18:10 +01:00
|
|
|
}
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
/**
|
2016-05-25 07:09:29 +02:00
|
|
|
* Given a table and stage determine the table name.
|
2016-03-02 06:18:10 +01:00
|
|
|
*
|
|
|
|
* Note: Stages this asset does not exist in will default to the draft table.
|
|
|
|
*
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param string $table Main table
|
2016-03-02 06:18:10 +01:00
|
|
|
* @param string $stage
|
2016-05-25 07:09:29 +02:00
|
|
|
* @return string Staged table name
|
2016-03-02 06:18:10 +01:00
|
|
|
*/
|
2016-05-25 07:09:29 +02:00
|
|
|
public function stageTable($table, $stage) {
|
2016-03-02 06:18:10 +01:00
|
|
|
if($this->hasStages() && $stage === static::LIVE) {
|
2016-05-25 07:09:29 +02:00
|
|
|
return "{$table}_{$stage}";
|
2013-05-10 11:19:16 +02:00
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
return $table;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
//-----------------------------------------------------------------------------------------------//
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2015-12-07 05:25:27 +01:00
|
|
|
/**
|
|
|
|
* Determine if the current user is able to set the given site stage / archive
|
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
2015-12-07 05:25:27 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function can_choose_site_stage($request) {
|
|
|
|
// Request is allowed if stage isn't being modified
|
2016-03-02 06:18:10 +01:00
|
|
|
if((!$request->getVar('stage') || $request->getVar('stage') === static::LIVE)
|
2015-12-07 05:25:27 +01:00
|
|
|
&& !$request->getVar('archiveDate')
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check permissions with member ID in session.
|
|
|
|
$member = Member::currentUser();
|
|
|
|
$permissions = Config::inst()->get(get_called_class(), 'non_live_permissions');
|
|
|
|
return $member && Permission::checkMember($member, $permissions);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Choose the stage the site is currently on.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* If $_GET['stage'] is set, then it will use that stage, and store it in
|
2013-05-10 11:19:16 +02:00
|
|
|
* the session.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* if $_GET['archiveDate'] is set, it will use that date, and store it in
|
2013-05-10 11:19:16 +02:00
|
|
|
* the session.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* If neither of these are set, it checks the session, otherwise the stage
|
2013-05-10 11:19:16 +02:00
|
|
|
* is set to 'Live'.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-12-07 05:25:27 +01:00
|
|
|
public static function choose_site_stage() {
|
2014-06-11 06:14:56 +02:00
|
|
|
// Check any pre-existing session mode
|
2015-12-07 05:25:27 +01:00
|
|
|
$preexistingMode = Session::get('readingMode');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-11 06:14:56 +02:00
|
|
|
// Determine the reading mode
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($_GET['stage'])) {
|
2010-10-19 03:32:39 +02:00
|
|
|
$stage = ucfirst(strtolower($_GET['stage']));
|
2016-03-02 06:18:10 +01:00
|
|
|
if(!in_array($stage, array(static::DRAFT, static::LIVE))) {
|
|
|
|
$stage = static::LIVE;
|
|
|
|
}
|
2014-05-19 23:47:34 +02:00
|
|
|
$mode = 'Stage.' . $stage;
|
|
|
|
} elseif (isset($_GET['archiveDate']) && strtotime($_GET['archiveDate'])) {
|
|
|
|
$mode = 'Archive.' . $_GET['archiveDate'];
|
2014-06-11 06:14:56 +02:00
|
|
|
} elseif($preexistingMode) {
|
|
|
|
$mode = $preexistingMode;
|
2014-05-19 23:47:34 +02:00
|
|
|
} else {
|
2016-03-02 06:18:10 +01:00
|
|
|
$mode = static::DEFAULT_MODE;
|
2007-11-09 04:40:05 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-19 23:47:34 +02:00
|
|
|
// Save reading mode
|
|
|
|
Versioned::set_reading_mode($mode);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-11 06:14:56 +02:00
|
|
|
// Try not to store the mode in the session if not needed
|
|
|
|
if(($preexistingMode && $preexistingMode !== $mode)
|
2016-03-02 06:18:10 +01:00
|
|
|
|| (!$preexistingMode && $mode !== static::DEFAULT_MODE)
|
2014-06-11 06:14:56 +02:00
|
|
|
) {
|
2015-12-07 05:25:27 +01:00
|
|
|
Session::set('readingMode', $mode);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-04-12 04:33:46 +02:00
|
|
|
|
2011-08-30 08:38:03 +02:00
|
|
|
if(!headers_sent() && !Director::is_cli()) {
|
2016-03-02 06:18:10 +01:00
|
|
|
if(Versioned::get_stage() == 'Live') {
|
2012-04-26 06:43:58 +02:00
|
|
|
// clear the cookie if it's set
|
2014-05-04 15:34:58 +02:00
|
|
|
if(Cookie::get('bypassStaticCache')) {
|
|
|
|
Cookie::force_expiry('bypassStaticCache', null, null, false, true /* httponly */);
|
2012-04-26 06:43:58 +02:00
|
|
|
}
|
2010-10-15 03:22:25 +02:00
|
|
|
} else {
|
2012-04-26 06:43:58 +02:00
|
|
|
// set the cookie if it's cleared
|
2014-05-04 15:34:58 +02:00
|
|
|
if(!Cookie::get('bypassStaticCache')) {
|
2012-04-26 06:43:58 +02:00
|
|
|
Cookie::set('bypassStaticCache', '1', 0, null, null, false, true /* httponly */);
|
|
|
|
}
|
2010-10-15 03:22:25 +02:00
|
|
|
}
|
2010-04-12 04:33:46 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 05:16:57 +02:00
|
|
|
/**
|
|
|
|
* Set the current reading mode.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
|
|
|
* @param string $mode
|
2010-04-13 05:16:57 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_reading_mode($mode) {
|
2016-03-02 06:18:10 +01:00
|
|
|
self::$reading_mode = $mode;
|
2010-04-13 05:16:57 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 05:16:57 +02:00
|
|
|
/**
|
|
|
|
* Get the current reading mode.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2010-04-13 05:16:57 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_reading_mode() {
|
2016-03-02 06:18:10 +01:00
|
|
|
return self::$reading_mode;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the current reading stage.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-02 06:18:10 +01:00
|
|
|
public static function get_stage() {
|
2010-04-13 05:16:57 +02:00
|
|
|
$parts = explode('.', Versioned::get_reading_mode());
|
2013-05-10 11:19:16 +02:00
|
|
|
|
|
|
|
if($parts[0] == 'Stage') {
|
|
|
|
return $parts[1];
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the current archive date.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function current_archived_date() {
|
2010-04-13 05:16:57 +02:00
|
|
|
$parts = explode('.', Versioned::get_reading_mode());
|
2016-03-02 06:18:10 +01:00
|
|
|
if($parts[0] == 'Archive') {
|
|
|
|
return $parts[1];
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the reading stage.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $stage New reading stage.
|
2016-05-25 07:09:29 +02:00
|
|
|
* @throws InvalidArgumentException
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-03-02 06:18:10 +01:00
|
|
|
public static function set_stage($stage) {
|
2016-05-25 07:09:29 +02:00
|
|
|
if(!in_array($stage, [static::LIVE, static::DRAFT])) {
|
|
|
|
throw new \InvalidArgumentException("Invalid stage name \"{$stage}\"");
|
|
|
|
}
|
2016-03-02 06:18:10 +01:00
|
|
|
static::set_reading_mode('Stage.' . $stage);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the reading archive date.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $date New reading archived date.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function reading_archived_date($date) {
|
2010-04-13 05:16:57 +02:00
|
|
|
Versioned::set_reading_mode('Archive.' . $date);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get a singleton instance of a class in the given stage.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $class The name of the class.
|
|
|
|
* @param string $stage The name of the stage.
|
|
|
|
* @param string $filter A filter to be inserted into the WHERE clause.
|
2008-10-16 12:14:47 +02:00
|
|
|
* @param boolean $cache Use caching.
|
2016-02-25 05:32:41 +01:00
|
|
|
* @param string $sort A sort expression to be inserted into the ORDER BY clause.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return DataObject
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_one_by_stage($class, $stage, $filter = '', $cache = true, $sort = '') {
|
2012-05-28 11:13:42 +02:00
|
|
|
// TODO: No identity cache operating
|
2016-03-02 06:18:10 +01:00
|
|
|
$items = static::get_by_stage($class, $stage, $filter, $sort, null, 1);
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
return $items->first();
|
2012-05-28 11:13:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-17 23:38:47 +01:00
|
|
|
/**
|
|
|
|
* Gets the current version number of a specific record.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-12-17 23:38:47 +01:00
|
|
|
* @param string $class
|
|
|
|
* @param string $stage
|
|
|
|
* @param int $id
|
|
|
|
* @param boolean $cache
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2008-12-17 23:38:47 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_versionnumber_by_stage($class, $stage, $id, $cache = true) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseClass = DataObject::getSchema()->baseDataClass($class);
|
|
|
|
$stageTable = DataObject::getSchema()->tableName($baseClass);
|
|
|
|
if($stage === static::LIVE) {
|
|
|
|
$stageTable .= "_{$stage}";
|
|
|
|
}
|
2008-12-17 23:38:47 +01:00
|
|
|
|
|
|
|
// cached call
|
|
|
|
if($cache && isset(self::$cache_versionnumber[$baseClass][$stage][$id])) {
|
|
|
|
return self::$cache_versionnumber[$baseClass][$stage][$id];
|
|
|
|
}
|
2011-03-21 07:53:09 +01:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
// get version as performance-optimized SQL query (gets called for each record in the sitetree)
|
2013-06-21 00:32:08 +02:00
|
|
|
$version = DB::prepared_query(
|
|
|
|
"SELECT \"Version\" FROM \"$stageTable\" WHERE \"ID\" = ?",
|
|
|
|
array($id)
|
|
|
|
)->value();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-17 23:38:47 +01:00
|
|
|
// cache value (if required)
|
|
|
|
if($cache) {
|
2013-05-10 11:19:16 +02:00
|
|
|
if(!isset(self::$cache_versionnumber[$baseClass])) {
|
|
|
|
self::$cache_versionnumber[$baseClass] = array();
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!isset(self::$cache_versionnumber[$baseClass][$stage])) {
|
|
|
|
self::$cache_versionnumber[$baseClass][$stage] = array();
|
|
|
|
}
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2008-12-17 23:38:47 +01:00
|
|
|
self::$cache_versionnumber[$baseClass][$stage][$id] = $version;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-17 23:38:47 +01:00
|
|
|
return $version;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Pre-populate the cache for Versioned::get_versionnumber_by_stage() for
|
|
|
|
* a list of record IDs, for more efficient database querying. If $idList
|
2016-01-26 04:56:07 +01:00
|
|
|
* is null, then every record will be pre-cached.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param string $stage
|
|
|
|
* @param array $idList
|
2009-02-02 00:49:53 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function prepopulate_versionnumber_cache($class, $stage, $idList = null) {
|
2016-10-20 03:49:22 +02:00
|
|
|
if (!Config::inst()->get(static::class, 'prepopulate_versionnumber_cache')) {
|
2013-06-04 15:28:36 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
$filter = "";
|
2013-06-21 00:32:08 +02:00
|
|
|
$parameters = array();
|
2009-02-02 00:49:53 +01:00
|
|
|
if($idList) {
|
|
|
|
// Validate the ID list
|
2013-05-10 11:19:16 +02:00
|
|
|
foreach($idList as $id) {
|
|
|
|
if(!is_numeric($id)) {
|
|
|
|
user_error("Bad ID passed to Versioned::prepopulate_versionnumber_cache() in \$idList: " . $id,
|
2012-09-26 23:34:00 +02:00
|
|
|
E_USER_ERROR);
|
2013-05-10 11:19:16 +02:00
|
|
|
}
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
$filter = 'WHERE "ID" IN ('.DB::placeholders($idList).')';
|
|
|
|
$parameters = $idList;
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
/** @var Versioned|DataObject $singleton */
|
|
|
|
$singleton = DataObject::singleton($class);
|
|
|
|
$baseClass = $singleton->baseClass();
|
|
|
|
$baseTable = $singleton->baseTable();
|
|
|
|
$stageTable = $singleton->stageTable($baseTable, $stage);
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$versions = DB::prepared_query("SELECT \"ID\", \"Version\" FROM \"$stageTable\" $filter", $parameters)->map();
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
foreach($versions as $id => $version) {
|
|
|
|
self::$cache_versionnumber[$baseClass][$stage][$id] = $version;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-16 12:14:47 +02:00
|
|
|
/**
|
|
|
|
* Get a set of class instances by the given stage.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-16 12:14:47 +02:00
|
|
|
* @param string $class The name of the class.
|
|
|
|
* @param string $stage The name of the stage.
|
|
|
|
* @param string $filter A filter to be inserted into the WHERE clause.
|
|
|
|
* @param string $sort A sort expression to be inserted into the ORDER BY clause.
|
2013-01-09 18:20:19 +01:00
|
|
|
* @param string $join Deprecated, use leftJoin($table, $joinClause) instead
|
2008-10-16 12:14:47 +02:00
|
|
|
* @param int $limit A limit on the number of records returned from the database.
|
2011-10-26 08:09:04 +02:00
|
|
|
* @param string $containerClass The container class for the result set (default is DataList)
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return DataList A modified DataList designated to the specified stage
|
2008-10-16 12:14:47 +02:00
|
|
|
*/
|
2016-02-25 05:32:41 +01:00
|
|
|
public static function get_by_stage(
|
2016-06-15 06:03:16 +02:00
|
|
|
$class, $stage, $filter = '', $sort = '', $join = '', $limit = null, $containerClass = 'SilverStripe\ORM\DataList'
|
2016-02-25 05:32:41 +01:00
|
|
|
) {
|
2008-10-16 12:14:47 +02:00
|
|
|
$result = DataObject::get($class, $filter, $sort, $join, $limit, $containerClass);
|
2012-12-12 05:22:45 +01:00
|
|
|
return $result->setDataQueryParam(array(
|
|
|
|
'Versioned.mode' => 'stage',
|
|
|
|
'Versioned.stage' => $stage
|
|
|
|
));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
2016-02-25 05:32:41 +01:00
|
|
|
* Delete this record from the given stage
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2016-02-25 05:32:41 +01:00
|
|
|
* @param string $stage
|
2013-05-10 11:19:16 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function deleteFromStage($stage) {
|
2010-04-13 05:16:57 +02:00
|
|
|
$oldMode = Versioned::get_reading_mode();
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_stage($stage);
|
|
|
|
$owner = $this->owner;
|
|
|
|
$clone = clone $owner;
|
2016-02-25 05:32:41 +01:00
|
|
|
$clone->delete();
|
2010-04-13 05:16:57 +02:00
|
|
|
Versioned::set_reading_mode($oldMode);
|
2010-10-15 04:56:05 +02:00
|
|
|
|
|
|
|
// Fix the version number cache (in case you go delete from stage and then check ExistsOnLive)
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseClass = $owner->baseClass();
|
2016-03-02 06:18:10 +01:00
|
|
|
self::$cache_versionnumber[$baseClass][$stage][$owner->ID] = null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
2016-10-18 07:19:59 +02:00
|
|
|
* Write the given record to the given stage.
|
|
|
|
* Note: If writing to live, this will write to stage as well.
|
2016-02-25 05:32:41 +01:00
|
|
|
*
|
2013-05-10 11:19:16 +02:00
|
|
|
* @param string $stage
|
|
|
|
* @param boolean $forceInsert
|
2016-02-25 05:32:41 +01:00
|
|
|
* @return int The ID of the record
|
2013-05-10 11:19:16 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function writeToStage($stage, $forceInsert = false) {
|
2010-04-13 05:16:57 +02:00
|
|
|
$oldMode = Versioned::get_reading_mode();
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_stage($stage);
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
$owner->forceChange();
|
|
|
|
$result = $owner->write(false, $forceInsert);
|
2010-04-13 05:16:57 +02:00
|
|
|
Versioned::set_reading_mode($oldMode);
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $result;
|
|
|
|
}
|
2011-03-21 07:53:09 +01:00
|
|
|
|
2011-03-23 04:02:10 +01:00
|
|
|
/**
|
2016-01-26 04:56:07 +01:00
|
|
|
* Roll the draft version of this record to match the published record.
|
2011-03-23 04:02:10 +01:00
|
|
|
* Caution: Doesn't overwrite the object properties with the rolled back version.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-03-02 06:18:10 +01:00
|
|
|
* {@see doRevertToLive()} to reollback to live
|
|
|
|
*
|
|
|
|
* @param int $version Version number
|
2011-03-23 04:02:10 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function doRollbackTo($version) {
|
2016-01-26 04:56:07 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
$owner->extend('onBeforeRollback', $version);
|
2016-04-01 05:27:59 +02:00
|
|
|
$owner->copyVersionToStage($version, static::DRAFT, true);
|
2016-01-26 04:56:07 +01:00
|
|
|
$owner->writeWithoutVersion();
|
|
|
|
$owner->extend('onAfterRollback', $version);
|
2011-03-23 04:02:10 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
public function onAfterRollback($version) {
|
|
|
|
// Find record at this version
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseClass = DataObject::getSchema()->baseDataClass($this->owner);
|
2016-03-22 02:39:25 +01:00
|
|
|
/** @var Versioned|DataObject $recordVersion */
|
2016-03-02 06:18:10 +01:00
|
|
|
$recordVersion = static::get_version($baseClass, $this->owner->ID, $version);
|
|
|
|
|
|
|
|
// Note that unlike other publishing actions, rollback is NOT recursive;
|
|
|
|
// The owner collects all objects and writes them back using writeToStage();
|
|
|
|
foreach ($recordVersion->findOwned() as $object) {
|
|
|
|
/** @var Versioned|DataObject $object */
|
|
|
|
$object->writeToStage(static::DRAFT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-01-26 04:56:07 +01:00
|
|
|
* Return the latest version of the given record.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-02-25 05:32:41 +01:00
|
|
|
* @param string $class
|
|
|
|
* @param int $id
|
2008-12-17 23:38:47 +01:00
|
|
|
* @return DataObject
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_latest_version($class, $id) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseClass = DataObject::getSchema()->baseDataClass($class);
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = DataList::create($baseClass)
|
|
|
|
->setDataQueryParam("Versioned.mode", "latest_versions");
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
return $list->byID($id);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 04:05:54 +02:00
|
|
|
/**
|
|
|
|
* Returns whether the current record is the latest one.
|
|
|
|
*
|
|
|
|
* @todo Performance - could do this directly via SQL.
|
|
|
|
*
|
|
|
|
* @see get_latest_version()
|
|
|
|
* @see latestPublished
|
|
|
|
*
|
2013-05-10 11:19:16 +02:00
|
|
|
* @return boolean
|
2011-08-26 04:05:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isLatestVersion() {
|
2016-03-02 06:18:10 +01:00
|
|
|
$owner = $this->owner;
|
|
|
|
if(!$owner->isInDB()) {
|
2016-01-26 04:56:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
$version = static::get_latest_version($owner->class, $owner->ID);
|
|
|
|
return ($version->Version == $owner->Version);
|
2011-08-26 04:05:54 +02:00
|
|
|
}
|
2009-05-01 05:49:34 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
/**
|
|
|
|
* Check if this record exists on live
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPublished() {
|
2016-10-05 03:16:09 +02:00
|
|
|
$id = $this->owner->ID ?: $this->owner->OldID;
|
|
|
|
if(!$id) {
|
2016-01-26 04:56:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
// Non-staged objects are considered "published" if saved
|
|
|
|
if(!$this->hasStages()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
$table = $this->baseTable(static::LIVE);
|
2016-01-26 04:56:07 +01:00
|
|
|
$result = DB::prepared_query(
|
|
|
|
"SELECT COUNT(*) FROM \"{$table}\" WHERE \"{$table}\".\"ID\" = ?",
|
2016-10-05 03:16:09 +02:00
|
|
|
array($id)
|
2016-01-26 04:56:07 +01:00
|
|
|
);
|
|
|
|
return (bool)$result->value();
|
|
|
|
}
|
|
|
|
|
2016-10-05 03:16:09 +02:00
|
|
|
/**
|
|
|
|
* Check if page doesn't exist on any stage, but used to be
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isArchived() {
|
|
|
|
$id = $this->owner->ID ?: $this->owner->OldID;
|
|
|
|
return $id && !$this->isOnDraft() && !$this->isPublished();
|
|
|
|
}
|
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
/**
|
|
|
|
* Check if this record exists on the draft stage
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isOnDraft() {
|
2016-10-05 03:16:09 +02:00
|
|
|
$id = $this->owner->ID ?: $this->owner->OldID;
|
|
|
|
if(!$id) {
|
2016-01-26 04:56:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
$table = $this->baseTable();
|
2016-01-26 04:56:07 +01:00
|
|
|
$result = DB::prepared_query(
|
|
|
|
"SELECT COUNT(*) FROM \"{$table}\" WHERE \"{$table}\".\"ID\" = ?",
|
2016-10-05 03:16:09 +02:00
|
|
|
array($id)
|
2016-01-26 04:56:07 +01:00
|
|
|
);
|
|
|
|
return (bool)$result->value();
|
|
|
|
}
|
2016-10-05 03:16:09 +02:00
|
|
|
|
2016-10-04 06:09:11 +02:00
|
|
|
/**
|
|
|
|
* Compares current draft with live version, and returns true if no draft version of this page exists but the page
|
|
|
|
* is still published (eg, after triggering "Delete from draft site" in the CMS).
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-05 03:16:09 +02:00
|
|
|
public function isOnLiveOnly() {
|
|
|
|
return $this->isPublished() && !$this->isOnDraft();
|
2016-10-04 06:09:11 +02:00
|
|
|
}
|
2016-10-05 03:16:09 +02:00
|
|
|
|
2016-10-04 06:09:11 +02:00
|
|
|
/**
|
2016-10-05 03:16:09 +02:00
|
|
|
* Compares current draft with live version, and returns true if no live version exists, meaning the page was never
|
|
|
|
* published.
|
2016-10-04 06:09:11 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-05 03:16:09 +02:00
|
|
|
public function isOnDraftOnly() {
|
|
|
|
return $this->isOnDraft() && !$this->isPublished();
|
2016-10-04 06:09:11 +02:00
|
|
|
}
|
2016-10-05 03:16:09 +02:00
|
|
|
|
2016-10-04 06:09:11 +02:00
|
|
|
/**
|
2016-10-05 03:16:09 +02:00
|
|
|
* Compares current draft with live version, and returns true if these versions differ, meaning there have been
|
|
|
|
* unpublished changes to the draft site.
|
2016-10-04 06:09:11 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-05 03:16:09 +02:00
|
|
|
public function isModifiedOnDraft() {
|
|
|
|
return $this->isOnDraft() && $this->stagesDiffer(Versioned::DRAFT, Versioned::LIVE);
|
2016-10-04 06:09:11 +02:00
|
|
|
}
|
2016-01-26 04:56:07 +01:00
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
/**
|
2012-03-19 03:27:52 +01:00
|
|
|
* Return the equivalent of a DataList::create() call, querying the latest
|
2016-10-20 03:49:22 +02:00
|
|
|
* version of each record stored in the (class)_Versions tables.
|
2009-05-01 05:49:34 +02:00
|
|
|
*
|
|
|
|
* In particular, this will query deleted records as well as active ones.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param string $filter
|
|
|
|
* @param string $sort
|
2016-02-25 05:32:41 +01:00
|
|
|
* @return DataList
|
2009-05-01 05:49:34 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_including_deleted($class, $filter = "", $sort = "") {
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = DataList::create($class)
|
|
|
|
->where($filter)
|
|
|
|
->sort($sort)
|
|
|
|
->setDataQueryParam("Versioned.mode", "latest_versions");
|
|
|
|
|
2012-05-28 11:13:42 +02:00
|
|
|
return $list;
|
2009-05-01 05:49:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-17 23:38:47 +01:00
|
|
|
/**
|
2012-06-27 16:03:08 +02:00
|
|
|
* Return the specific version of the given id.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Caution: The record is retrieved as a DataObject, but saving back
|
|
|
|
* modifications via write() will create a new version, rather than
|
2013-05-10 11:19:16 +02:00
|
|
|
* modifying the existing one.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-10 11:19:16 +02:00
|
|
|
* @param string $class
|
|
|
|
* @param int $id
|
|
|
|
* @param int $version
|
|
|
|
*
|
2008-12-17 23:38:47 +01:00
|
|
|
* @return DataObject
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_version($class, $id, $version) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseClass = DataObject::getSchema()->baseDataClass($class);
|
2012-09-26 23:34:00 +02:00
|
|
|
$list = DataList::create($baseClass)
|
2016-03-02 06:18:10 +01:00
|
|
|
->setDataQueryParam([
|
|
|
|
"Versioned.mode" => 'version',
|
|
|
|
"Versioned.version" => $version
|
|
|
|
]);
|
2012-12-12 05:22:45 +01:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
return $list->byID($id);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-12-17 23:38:47 +01:00
|
|
|
/**
|
2013-05-10 11:19:16 +02:00
|
|
|
* Return a list of all versions for a given id.
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param int $id
|
|
|
|
*
|
2011-03-21 07:53:09 +01:00
|
|
|
* @return DataList
|
2008-12-17 23:38:47 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_all_versions($class, $id) {
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = DataList::create($class)
|
2016-02-25 05:32:41 +01:00
|
|
|
->filter('ID', $id)
|
2012-12-12 05:22:45 +01:00
|
|
|
->setDataQueryParam('Versioned.mode', 'all_versions');
|
|
|
|
|
2012-05-28 11:13:42 +02:00
|
|
|
return $list;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-05-10 11:19:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $labels
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function updateFieldLabels(&$labels) {
|
2016-01-26 04:56:07 +01:00
|
|
|
$labels['Versions'] = _t('Versioned.has_many_Versions', 'Versions', 'Past Versions of this record');
|
2008-11-02 21:04:10 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:27:36 +02:00
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param FieldList $fields
|
2013-05-10 11:27:36 +02:00
|
|
|
*/
|
2013-05-21 22:08:11 +02:00
|
|
|
public function updateCMSFields(FieldList $fields) {
|
2014-08-15 08:53:05 +02:00
|
|
|
// remove the version field from the CMS as this should be left
|
|
|
|
// entirely up to the extension (not the cms user).
|
2013-05-10 11:27:36 +02:00
|
|
|
$fields->removeByName('Version');
|
|
|
|
}
|
|
|
|
|
2015-08-09 23:54:30 +02:00
|
|
|
/**
|
|
|
|
* Ensure version ID is reset to 0 on duplicate
|
|
|
|
*
|
|
|
|
* @param DataObject $source Record this was duplicated from
|
|
|
|
* @param bool $doWrite
|
|
|
|
*/
|
|
|
|
public function onBeforeDuplicate($source, $doWrite) {
|
|
|
|
$this->owner->Version = 0;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function flushCache() {
|
2008-12-17 23:38:47 +01:00
|
|
|
self::$cache_versionnumber = array();
|
|
|
|
}
|
2010-04-12 04:40:50 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-10 11:19:16 +02:00
|
|
|
* Return a piece of text to keep DataObject cache keys appropriately specific.
|
|
|
|
*
|
|
|
|
* @return string
|
2010-04-12 04:40:50 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function cacheKeyComponent() {
|
2016-03-02 06:18:10 +01:00
|
|
|
return 'versionedmode-'.static::get_reading_mode();
|
2010-04-12 04:40:50 +02:00
|
|
|
}
|
2014-01-30 01:59:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of possible stages.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getVersionedStages() {
|
2016-03-02 06:18:10 +01:00
|
|
|
if($this->hasStages()) {
|
|
|
|
return [static::DRAFT, static::LIVE];
|
|
|
|
} else {
|
|
|
|
return [static::DRAFT];
|
|
|
|
}
|
2014-01-30 01:59:10 +01:00
|
|
|
}
|
2014-03-30 07:15:12 +02:00
|
|
|
|
2014-03-26 06:27:07 +01:00
|
|
|
public static function get_template_global_variables() {
|
|
|
|
return array(
|
|
|
|
'CurrentReadingMode' => 'get_reading_mode'
|
|
|
|
);
|
|
|
|
}
|
2016-03-02 06:18:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this object has stages
|
|
|
|
*
|
|
|
|
* @return bool True if this object is staged
|
|
|
|
*/
|
|
|
|
public function hasStages() {
|
|
|
|
return $this->mode === static::STAGEDVERSIONED;
|
|
|
|
}
|
2016-09-26 07:22:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge single object into a list
|
|
|
|
*
|
|
|
|
* @param ArrayList $list Global list. Object will not be added if already added to this list.
|
|
|
|
* @param ArrayList $added Additional list to insert into
|
|
|
|
* @param DataObject $item Item to add
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function mergeRelatedObject($list, $added, $item)
|
|
|
|
{
|
|
|
|
// Identify item
|
|
|
|
$itemKey = get_class($item) . '/' . $item->ID;
|
|
|
|
|
|
|
|
// Write if saved, versioned, and not already added
|
2016-10-20 03:49:22 +02:00
|
|
|
if ($item->isInDB() && $item->has_extension(static::class) && !isset($list[$itemKey])) {
|
2016-09-26 07:22:19 +02:00
|
|
|
$list[$itemKey] = $item;
|
|
|
|
$added[$itemKey] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add joined record (from many_many through) automatically
|
|
|
|
$joined = $item->getJoin();
|
|
|
|
if ($joined) {
|
|
|
|
$this->mergeRelatedObject($list, $added, $joined);
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|