2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2013-05-10 11:19:16 +02: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.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2014-03-26 06:27:07 +01:00
|
|
|
class Versioned extends DataExtension implements TemplateGlobalProvider {
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* An array of possible stages.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $stages;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* The 'default' stage.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $defaultStage;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* The 'live' stage.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $liveStage;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-11 06:14:56 +02:00
|
|
|
/**
|
|
|
|
* The default reading mode
|
|
|
|
*/
|
|
|
|
const DEFAULT_MODE = 'Stage.Live';
|
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
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
* "_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.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $prepopulate_versionnumber_cache = true;
|
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
|
|
|
* Keep track of the archive tables that have been created.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $archive_tables = array();
|
|
|
|
|
2009-05-12 06:47:56 +02:00
|
|
|
/**
|
|
|
|
* Additional database indexes for the new
|
|
|
|
* "_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
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
2013-05-10 11:19:16 +02:00
|
|
|
* Make sure your extension has a static $enabled-property that determines if it is
|
|
|
|
* processed by Versioned.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $versionableExtensions = array('Translatable' => 'lang');
|
|
|
|
|
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');
|
|
|
|
|
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 = '';
|
2009-08-11 06:45:54 +02:00
|
|
|
|
2010-04-13 05:16:57 +02:00
|
|
|
Session::clear('readingMode');
|
2009-08-11 06:45:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Construct a new Versioned object.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array $stages The different stages the versioned object can be.
|
2013-05-10 11:19:16 +02:00
|
|
|
* The first stage is considered the 'default' stage, the last stage is
|
2007-07-19 12:40:28 +02:00
|
|
|
* considered the 'live' stage.
|
|
|
|
*/
|
2013-05-10 11:19:16 +02:00
|
|
|
public function __construct($stages = array('Stage','Live')) {
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
if(!is_array($stages)) {
|
|
|
|
$stages = func_get_args();
|
|
|
|
}
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->stages = $stages;
|
|
|
|
$this->defaultStage = reset($stages);
|
|
|
|
$this->liveStage = array_pop($stages);
|
|
|
|
}
|
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.
|
|
|
|
*
|
2014-12-03 21:20:39 +01:00
|
|
|
* @param SQLQuery
|
2013-05-10 11:19:16 +02:00
|
|
|
* @param DataQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2014-12-03 21:20:39 +01:00
|
|
|
public function augmentDataQueryCreation(SQLQuery &$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]);
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
} else if($parts[0] == 'Stage' && $parts[1] != $this->defaultStage
|
2012-09-26 23:34:00 +02:00
|
|
|
&& array_search($parts[1],$this->stages) !== false) {
|
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-03 21:20:39 +01:00
|
|
|
* Augment the the SQLQuery that is created by the DataQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
* @todo Should this all go into VersionedDataQuery?
|
|
|
|
*/
|
2014-12-03 21:20:39 +01:00
|
|
|
public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) {
|
2013-10-03 16:04:55 +02:00
|
|
|
if(!$dataQuery || !$dataQuery->getQueryParam('Versioned.mode')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-24 07:56:02 +01:00
|
|
|
$baseTable = ClassInfo::baseDataClass($dataQuery->dataClass());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-03 16:04:55 +02:00
|
|
|
switch($dataQuery->getQueryParam('Versioned.mode')) {
|
2009-11-22 06:16:38 +01:00
|
|
|
// Reading a specific data from the archive
|
|
|
|
case 'archive':
|
|
|
|
$date = $dataQuery->getQueryParam('Versioned.date');
|
2012-05-03 09:34:16 +02:00
|
|
|
foreach($query->getFrom() as $table => $dummy) {
|
2013-06-21 00:32:08 +02:00
|
|
|
if(!DB::get_schema()->hasTable($table . '_versions')) {
|
2013-05-07 01:57:49 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$query->renameTable($table, $table . '_versions');
|
2012-08-10 03:54:29 +02:00
|
|
|
$query->replaceText("\"{$table}_versions\".\"ID\"", "\"{$table}_versions\".\"RecordID\"");
|
|
|
|
$query->replaceText("`{$table}_versions`.`ID`", "`{$table}_versions`.`RecordID`");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-12 06:47:56 +02:00
|
|
|
// Add all <basetable>_versions columns
|
2013-03-21 19:48:54 +01:00
|
|
|
foreach(Config::inst()->get('Versioned', 'db_for_versions_table') as $name => $type) {
|
2012-05-01 06:42:14 +02:00
|
|
|
$query->selectField(sprintf('"%s_versions"."%s"', $baseTable, $name), $name);
|
2009-05-12 06:47:56 +02:00
|
|
|
}
|
2012-05-01 06:42:14 +02:00
|
|
|
$query->selectField(sprintf('"%s_versions"."%s"', $baseTable, 'RecordID'), "ID");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($table != $baseTable) {
|
2012-08-10 02:45:37 +02:00
|
|
|
$query->addWhere("\"{$table}_versions\".\"Version\" = \"{$baseTable}_versions\".\"Version\"");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Link to the version archived on that date
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->addWhere(array(
|
2014-08-15 08:53:05 +02:00
|
|
|
"\"{$baseTable}_versions\".\"Version\" IN
|
|
|
|
(SELECT LatestVersion FROM
|
|
|
|
(SELECT
|
|
|
|
\"{$baseTable}_versions\".\"RecordID\",
|
2013-06-21 00:32:08 +02:00
|
|
|
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\"
|
|
|
|
)" => $date
|
|
|
|
));
|
2009-11-22 06:16:38 +01:00
|
|
|
break;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// Reading a specific stage (Stage or Live)
|
|
|
|
case 'stage':
|
|
|
|
$stage = $dataQuery->getQueryParam('Versioned.stage');
|
|
|
|
if($stage && ($stage != $this->defaultStage)) {
|
2012-05-03 09:34:16 +02:00
|
|
|
foreach($query->getFrom() as $table => $dummy) {
|
2009-11-22 06:16:38 +01:00
|
|
|
// Only rewrite table names that are actually part of the subclass tree
|
|
|
|
// This helps prevent rewriting of other tables that get joined in, in
|
|
|
|
// particular, many_many tables
|
2014-08-15 08:53:05 +02:00
|
|
|
if(class_exists($table) && ($table == $this->owner->class
|
|
|
|
|| is_subclass_of($table, $this->owner->class)
|
2009-11-22 06:16:38 +01:00
|
|
|
|| is_subclass_of($this->owner->class, $table))) {
|
|
|
|
$query->renameTable($table, $table . '_' . $stage);
|
|
|
|
}
|
|
|
|
}
|
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':
|
|
|
|
$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
|
|
|
|
foreach($this->stages as $excluding) {
|
|
|
|
if ($excluding == $stage) continue;
|
|
|
|
|
|
|
|
$tempName = 'ExclusionarySource_'.$excluding;
|
|
|
|
$excludingTable = $baseTable . ($excluding && $excluding != $this->defaultStage ? "_$excluding" : '');
|
|
|
|
|
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
|
2011-03-21 07:53:09 +01:00
|
|
|
case 'all_versions':
|
|
|
|
case 'latest_versions':
|
2012-05-28 11:13:42 +02:00
|
|
|
foreach($query->getFrom() as $alias => $join) {
|
|
|
|
if($alias != $baseTable) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$query->setJoinFilter($alias, "\"$alias\".\"RecordID\" = \"{$baseTable}_versions\".\"RecordID\""
|
|
|
|
. " AND \"$alias\".\"Version\" = \"{$baseTable}_versions\".\"Version\"");
|
2012-05-28 11:13:42 +02:00
|
|
|
}
|
|
|
|
$query->renameTable($alias, $alias . '_versions');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-28 11:13:42 +02:00
|
|
|
// Add all <basetable>_versions columns
|
2013-03-21 19:48:54 +01:00
|
|
|
foreach(Config::inst()->get('Versioned', 'db_for_versions_table') as $name => $type) {
|
2012-05-28 11:13:42 +02:00
|
|
|
$query->selectField(sprintf('"%s_versions"."%s"', $baseTable, $name), $name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-17 05:15:26 +01:00
|
|
|
// Alias the record ID as the row ID
|
2012-05-28 11:13:42 +02:00
|
|
|
$query->selectField(sprintf('"%s_versions"."%s"', $baseTable, 'RecordID'), "ID");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-17 05:15:26 +01:00
|
|
|
// Ensure that any sort order referring to this ID is correctly aliased
|
|
|
|
$orders = $query->getOrderBy();
|
|
|
|
foreach($orders as $order => $dir) {
|
|
|
|
if($order === "\"$baseTable\".\"ID\"") {
|
|
|
|
unset($orders[$order]);
|
|
|
|
$orders["\"{$baseTable}_versions\".\"RecordID\""] = $dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$query->setOrderBy($orders);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-28 11:13:42 +02:00
|
|
|
// latest_version has one more step
|
|
|
|
// Return latest version instances, regardless of whether they are on a particular stage
|
|
|
|
// This provides "show all, including deleted" functonality
|
|
|
|
if($dataQuery->getQueryParam('Versioned.mode') == 'latest_versions') {
|
2012-08-20 03:24:28 +02:00
|
|
|
$query->addWhere(
|
2014-08-15 08:53:05 +02:00
|
|
|
"\"{$alias}_versions\".\"Version\" IN
|
|
|
|
(SELECT LatestVersion FROM
|
|
|
|
(SELECT
|
|
|
|
\"{$alias}_versions\".\"RecordID\",
|
2012-08-21 03:46:19 +02:00
|
|
|
MAX(\"{$alias}_versions\".\"Version\") AS LatestVersion
|
|
|
|
FROM \"{$alias}_versions\"
|
|
|
|
GROUP BY \"{$alias}_versions\".\"RecordID\"
|
|
|
|
) AS \"{$alias}_versions_latest\"
|
|
|
|
WHERE \"{$alias}_versions_latest\".\"RecordID\" = \"{$alias}_versions\".\"RecordID\"
|
2012-08-20 03:24:28 +02:00
|
|
|
)");
|
2012-12-17 05:15:26 +01:00
|
|
|
} else {
|
|
|
|
// If all versions are requested, ensure that records are sorted by this field
|
|
|
|
$query->addOrderBy(sprintf('"%s_versions"."%s"', $baseTable, 'Version'));
|
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
|
|
|
|
|
|
|
/**
|
2013-05-10 11:19:16 +02:00
|
|
|
* For lazy loaded fields requiring extra sql manipulation, ie versioning.
|
|
|
|
*
|
2014-12-03 21:20:39 +01:00
|
|
|
* @param SQLQuery $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
|
|
|
*/
|
2014-12-03 21:20:39 +01:00
|
|
|
public function augmentLoadLazyFields(SQLQuery &$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
|
2013-02-12 04:44:32 +01:00
|
|
|
// accidentally querying the *_versions tables.
|
|
|
|
$versionedMode = $dataObject->getSourceQueryParam('Versioned.mode');
|
2012-10-10 03:59:45 +02:00
|
|
|
$dataClass = $dataQuery->dataClass();
|
2013-02-12 04:44:32 +01:00
|
|
|
$modesToAllowVersioning = array('all_versions', 'latest_versions', 'archive');
|
|
|
|
if(
|
|
|
|
!empty($dataObject->Version) &&
|
|
|
|
(!empty($versionedMode) && in_array($versionedMode,$modesToAllowVersioning))
|
|
|
|
) {
|
|
|
|
$dataQuery->where("\"$dataClass\".\"RecordID\" = " . $dataObject->ID);
|
|
|
|
$dataQuery->where("\"$dataClass\".\"Version\" = " . $dataObject->Version);
|
2012-10-10 03:59:45 +02:00
|
|
|
$dataQuery->setQueryParam('Versioned.mode', 'all_versions');
|
2013-02-12 04:44:32 +01:00
|
|
|
} else {
|
|
|
|
// Same behaviour as in DataObject->loadLazyFields
|
|
|
|
$dataQuery->where("\"$dataClass\".\"ID\" = {$dataObject->ID}")->limit(1);
|
2012-10-10 03:59:45 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-21 10:08:01 +02:00
|
|
|
/**
|
|
|
|
* Called by {@link SapphireTest} when the database is reset.
|
2013-05-10 11:19:16 +02:00
|
|
|
*
|
2009-05-21 10:08:01 +02:00
|
|
|
* @todo Reduce the coupling between this and SapphireTest, somehow.
|
|
|
|
*/
|
|
|
|
public static function on_db_reset() {
|
2010-04-12 05:21:30 +02:00
|
|
|
// Drop all temporary tables
|
2013-06-21 00:32:08 +02:00
|
|
|
$db = DB::get_conn();
|
2010-04-12 05:21:30 +02:00
|
|
|
foreach(self::$archive_tables as $tableName) {
|
|
|
|
if(method_exists($db, 'dropTable')) $db->dropTable($tableName);
|
|
|
|
else $db->query("DROP TABLE \"$tableName\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove references to them
|
2009-05-21 10:08:01 +02:00
|
|
|
self::$archive_tables = array();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function augmentDatabase() {
|
2007-09-16 18:15:47 +02:00
|
|
|
$classTable = $this->owner->class;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-09 08:00:07 +02:00
|
|
|
$isRootClass = ($this->owner->class == ClassInfo::baseDataClass($this->owner->class));
|
2007-09-16 18:15:47 +02:00
|
|
|
|
|
|
|
// Build a list of suffixes whose tables need versioning
|
|
|
|
$allSuffixes = array();
|
|
|
|
foreach (Versioned::$versionableExtensions as $versionableExtension => $suffixes) {
|
2009-08-11 10:49:52 +02:00
|
|
|
if ($this->owner->hasExtension($versionableExtension)) {
|
2007-09-16 18:15:47 +02:00
|
|
|
$allSuffixes = array_merge($allSuffixes, (array)$suffixes);
|
|
|
|
foreach ((array)$suffixes as $suffix) {
|
|
|
|
$allSuffixes[$suffix] = $versionableExtension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
2007-09-16 18:15:47 +02:00
|
|
|
array_push($allSuffixes,'');
|
|
|
|
|
|
|
|
foreach ($allSuffixes as $key => $suffix) {
|
|
|
|
// check that this is a valid suffix
|
|
|
|
if (!is_int($key)) continue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-16 18:15:47 +02:00
|
|
|
if ($suffix) $table = "{$classTable}_$suffix";
|
|
|
|
else $table = $classTable;
|
|
|
|
|
2009-08-11 10:49:52 +02:00
|
|
|
if($fields = DataObject::database_fields($this->owner->class)) {
|
2012-04-18 13:10:57 +02:00
|
|
|
$options = Config::inst()->get($this->owner->class, 'create_table_options', Config::FIRST_SET);
|
2007-09-16 19:24:51 +02:00
|
|
|
$indexes = $this->owner->databaseIndexes();
|
2010-05-25 05:55:30 +02:00
|
|
|
if ($suffix && ($ext = $this->owner->getExtensionInstance($allSuffixes[$suffix]))) {
|
2007-09-16 18:58:19 +02:00
|
|
|
if (!$ext->isVersionedTable($table)) continue;
|
2009-06-04 08:48:44 +02:00
|
|
|
$ext->setOwner($this->owner);
|
2007-09-16 18:58:19 +02:00
|
|
|
$fields = $ext->fieldsInExtraTables($suffix);
|
2009-06-04 08:48:44 +02:00
|
|
|
$ext->clearOwner();
|
2007-09-16 18:15:47 +02:00
|
|
|
$indexes = $fields['indexes'];
|
|
|
|
$fields = $fields['db'];
|
2007-09-16 19:24:51 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// Create tables for other stages
|
2007-09-16 19:24:51 +02:00
|
|
|
foreach($this->stages as $stage) {
|
|
|
|
// Extra tables for _Live, etc.
|
2012-09-26 23:34:00 +02:00
|
|
|
// Change unique indexes to 'index'. Versioned tables may run into unique indexing difficulties
|
|
|
|
// otherwise.
|
2013-11-26 21:57:11 +01:00
|
|
|
$indexes = $this->uniqueToIndex($indexes);
|
2007-09-16 19:24:51 +02:00
|
|
|
if($stage != $this->defaultStage) {
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::require_table("{$table}_$stage", $fields, $indexes, false, $options);
|
2007-09-16 19:24:51 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-16 19:24:51 +02:00
|
|
|
// Version fields on each root table (including Stage)
|
2009-05-07 08:00:50 +02:00
|
|
|
/*
|
2009-07-09 08:00:07 +02:00
|
|
|
if($isRootClass) {
|
2007-09-16 19:24:51 +02:00
|
|
|
$stageTable = ($stage == $this->defaultStage) ? $table : "{$table}_$stage";
|
2008-11-23 02:01:03 +01:00
|
|
|
$parts=Array('datatype'=>'int', 'precision'=>11, 'null'=>'not null', 'default'=>(int)0);
|
|
|
|
$values=Array('type'=>'int', 'parts'=>$parts);
|
|
|
|
DB::requireField($stageTable, 'Version', $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-05-07 08:00:50 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-09 08:00:07 +02:00
|
|
|
if($isRootClass) {
|
|
|
|
// Create table for all versions
|
|
|
|
$versionFields = array_merge(
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->get('Versioned', 'db_for_versions_table'),
|
2009-07-09 08:00:07 +02:00
|
|
|
(array)$fields
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-09 08:00:07 +02:00
|
|
|
$versionIndexes = array_merge(
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->get('Versioned', 'indexes_for_versions_table'),
|
2009-07-09 08:00:07 +02:00
|
|
|
(array)$indexes
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Create fields for any tables of subclasses
|
|
|
|
$versionFields = array_merge(
|
|
|
|
array(
|
|
|
|
"RecordID" => "Int",
|
|
|
|
"Version" => "Int",
|
|
|
|
),
|
|
|
|
(array)$fields
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-11-01 03:58:43 +01:00
|
|
|
//Unique indexes will not work on versioned tables, so we'll convert them to standard indexes:
|
2013-11-26 21:57:11 +01:00
|
|
|
$indexes = $this->uniqueToIndex($indexes);
|
2009-07-09 08:00:07 +02:00
|
|
|
$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,
|
|
|
|
),
|
|
|
|
(array)$indexes
|
|
|
|
);
|
2007-09-16 19:24:51 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
if(DB::get_schema()->hasTable("{$table}_versions")) {
|
2010-10-19 05:01:51 +02:00
|
|
|
// Fix data that lacks the uniqueness constraint (since this was added later and
|
|
|
|
// bugs meant that the constraint was validated)
|
2014-08-15 08:53:05 +02:00
|
|
|
$duplications = DB::query("SELECT MIN(\"ID\") AS \"ID\", \"RecordID\", \"Version\"
|
|
|
|
FROM \"{$table}_versions\" GROUP BY \"RecordID\", \"Version\"
|
2010-10-15 04:55:15 +02:00
|
|
|
HAVING COUNT(*) > 1");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:55:15 +02:00
|
|
|
foreach($duplications as $dup) {
|
|
|
|
DB::alteration_message("Removing {$table}_versions duplicate data for "
|
|
|
|
."{$dup['RecordID']}/{$dup['Version']}" ,"deleted");
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::prepared_query(
|
|
|
|
"DELETE FROM \"{$table}_versions\" WHERE \"RecordID\" = ?
|
|
|
|
AND \"Version\" = ? AND \"ID\" != ?",
|
|
|
|
array($dup['RecordID'], $dup['Version'], $dup['ID'])
|
|
|
|
);
|
2010-10-15 04:55:15 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:01:51 +02:00
|
|
|
// Remove junk which has no data in parent classes. Only needs to run the following
|
2014-08-15 08:53:05 +02:00
|
|
|
// when versioned data is spread over multiple tables
|
2010-10-19 05:01:51 +02:00
|
|
|
if(!$isRootClass && ($versionedTables = ClassInfo::dataClassesFor($table))) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:01:51 +02:00
|
|
|
foreach($versionedTables as $child) {
|
2015-06-09 03:50:09 +02:00
|
|
|
if($table === $child) break; // only need subclasses
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-09 03:50:09 +02:00
|
|
|
// Select all orphaned version records
|
|
|
|
$orphanedQuery = SQLSelect::create()
|
|
|
|
->selectField("\"{$table}_versions\".\"ID\"")
|
|
|
|
->setFrom("\"{$table}_versions\"");
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-09 03:50:09 +02:00
|
|
|
// If we have a parent table limit orphaned records
|
|
|
|
// to only those that exist in this
|
|
|
|
if(DB::get_schema()->hasTable("{$child}_versions")) {
|
|
|
|
$orphanedQuery
|
|
|
|
->addLeftJoin(
|
|
|
|
"{$child}_versions",
|
|
|
|
"\"{$child}_versions\".\"RecordID\" = \"{$table}_versions\".\"RecordID\"
|
|
|
|
AND \"{$child}_versions\".\"Version\" = \"{$table}_versions\".\"Version\""
|
|
|
|
)
|
|
|
|
->addWhere("\"{$child}_versions\".\"ID\" IS NULL");
|
|
|
|
}
|
2010-10-19 05:01:51 +02:00
|
|
|
|
2015-06-09 03:50:09 +02:00
|
|
|
$count = $orphanedQuery->count();
|
2010-10-19 05:01:51 +02:00
|
|
|
if($count > 0) {
|
2015-06-09 03:50:09 +02:00
|
|
|
DB::alteration_message("Removing {$count} orphaned versioned records", "deleted");
|
|
|
|
$ids = $orphanedQuery->execute()->column();
|
|
|
|
foreach($ids as $id) {
|
|
|
|
DB::prepared_query(
|
|
|
|
"DELETE FROM \"{$table}_versions\" WHERE \"ID\" = ?",
|
|
|
|
array($id)
|
|
|
|
);
|
2010-10-19 05:01:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-15 04:55:15 +02:00
|
|
|
}
|
2009-07-09 08:00:07 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::require_table("{$table}_versions", $versionFields, $versionIndexes, true, $options);
|
2007-09-16 19:24:51 +02:00
|
|
|
} else {
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::dont_require_table("{$table}_versions");
|
2007-09-16 19:24:51 +02:00
|
|
|
foreach($this->stages as $stage) {
|
2013-06-21 00:32:08 +02:00
|
|
|
if($stage != $this->defaultStage) DB::dont_require_table("{$table}_$stage");
|
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
|
|
|
|
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
|
|
|
|
* @param string $table Name of table
|
|
|
|
* @param int $recordID ID of record to version
|
|
|
|
*/
|
|
|
|
protected function augmentWriteVersioned(&$manipulation, $table, $recordID) {
|
|
|
|
$baseDataClass = ClassInfo::baseDataClass($table);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-08-28 01:29:38 +02:00
|
|
|
// Set up a new entry in (table)_versions
|
|
|
|
$newManipulation = array(
|
|
|
|
"command" => "insert",
|
|
|
|
"fields" => isset($manipulation[$table]['fields']) ? $manipulation[$table]['fields'] : null
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add any extra, unchanged fields to the version record.
|
|
|
|
$data = DB::prepared_query("SELECT * FROM \"$table\" WHERE \"ID\" = ?", array($recordID))->record();
|
2015-02-08 06:42:36 +01:00
|
|
|
|
|
|
|
if ($data) {
|
|
|
|
$fields = DataObject::database_fields($table);
|
|
|
|
|
|
|
|
if (is_array($fields)) {
|
|
|
|
$data = array_intersect_key($data, $fields);
|
|
|
|
|
|
|
|
foreach ($data as $k => $v) {
|
|
|
|
if (!isset($newManipulation['fields'][$k])) {
|
|
|
|
$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
|
|
|
|
FROM \"{$baseDataClass}_versions\" WHERE \"RecordID\" = ?",
|
|
|
|
array($recordID)
|
|
|
|
)->value();
|
|
|
|
}
|
|
|
|
$nextVersion = $nextVersion ?: 1;
|
|
|
|
|
|
|
|
// Add the version number to this data
|
|
|
|
$manipulation[$table]['fields']['Version'] = $nextVersion;
|
|
|
|
$newManipulation['fields']['Version'] = $nextVersion;
|
|
|
|
|
|
|
|
// Write AuthorID for baseclass
|
|
|
|
if($table === $baseDataClass) {
|
|
|
|
$userID = (Member::currentUser()) ? Member::currentUser()->ID : 0;
|
|
|
|
$newManipulation['fields']['AuthorID'] = $userID;
|
|
|
|
}
|
|
|
|
$manipulation["{$table}_versions"] = $newManipulation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$newTable = $table . '_' . Versioned::current_stage();
|
|
|
|
$manipulation[$newTable] = $manipulation[$table];
|
|
|
|
unset($manipulation[$table]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function augmentWrite(&$manipulation) {
|
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
|
2007-09-16 18:15:47 +02:00
|
|
|
if( !$this->canBeVersioned($table) ) {
|
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
|
|
|
|
$id = $manipulation[$table]['id'] ? $manipulation[$table]['id'] : $manipulation[$table]['fields']['ID'];
|
|
|
|
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
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->migratingVersion) {
|
|
|
|
$manipulation[$table]['fields']['Version'] = $this->migratingVersion;
|
|
|
|
}
|
2010-10-12 23:48:07 +02:00
|
|
|
|
2009-07-09 08:00:07 +02:00
|
|
|
// If we haven't got a version #, then we're creating a new version.
|
|
|
|
// Otherwise, we're just copying a version to another table
|
2014-09-01 23:14:08 +02:00
|
|
|
if(empty($manipulation[$table]['fields']['Version'])) {
|
2014-08-28 01:29:38 +02:00
|
|
|
$this->augmentWriteVersioned($manipulation, $table, $id);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Putting a Version of -1 is a signal to leave the version table alone, despite their being no version
|
2012-01-14 11:03:45 +01:00
|
|
|
if($manipulation[$table]['fields']['Version'] < 0 || $this->_nextWriteWithoutVersion) {
|
|
|
|
unset($manipulation[$table]['fields']['Version']);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2014-08-28 01:29:38 +02:00
|
|
|
// For base classes of versioned data objects
|
2007-09-16 18:58:19 +02:00
|
|
|
if(!$this->hasVersionField($table)) unset($manipulation[$table]['fields']['Version']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// If we're editing Live, then use (table)_Live instead of (table)
|
2013-10-24 17:09:17 +02:00
|
|
|
if(
|
2014-08-15 08:53:05 +02:00
|
|
|
Versioned::current_stage()
|
2013-10-24 17:09:17 +02:00
|
|
|
&& Versioned::current_stage() != $this->defaultStage
|
|
|
|
&& in_array(Versioned::current_stage(), $this->stages)
|
|
|
|
) {
|
2014-08-28 01:29:38 +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)) {
|
|
|
|
$this->owner->Version = str_replace("'","", $thisVersion);
|
|
|
|
}
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
$mode = $this->owner->getSourceQueryParam("Versioned.mode");
|
|
|
|
$stage = $this->owner->getSourceQueryParam("Versioned.stage");
|
|
|
|
if ($mode === 'stage' && $stage === static::get_live_stage()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bypass if site is unsecured
|
|
|
|
if (Session::get('unsecuredDraftSite')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
$latestVersion = Versioned::get_versionnumber_by_stage($this->owner->class, 'Live', $this->owner->ID);
|
|
|
|
if ($latestVersion == $this->owner->Version) {
|
|
|
|
// Even if this is loaded from a non-live stage, this is the live version
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extend versioned behaviour
|
|
|
|
$extended = $this->owner->extendedCan('canViewNonLive', $member);
|
|
|
|
if($extended !== null) {
|
|
|
|
return (bool)$extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to default permission check
|
|
|
|
$permissions = Config::inst()->get($this->owner->class, 'non_live_permissions', Config::FIRST_SET);
|
|
|
|
$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();
|
|
|
|
Versioned::reading_stage($stage);
|
|
|
|
|
2015-12-09 02:53:21 +01:00
|
|
|
$versionFromStage = DataObject::get($this->owner->class)->byID($this->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
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Determine if a table is supporting the Versioned extensions (e.g.
|
2013-05-10 11:19:16 +02:00
|
|
|
* $table_versions does exists).
|
2007-09-16 18:58:19 +02:00
|
|
|
*
|
|
|
|
* @param string $table Table name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canBeVersioned($table) {
|
2014-08-15 08:53:05 +02:00
|
|
|
return ClassInfo::exists($table)
|
2011-02-21 06:20:14 +01:00
|
|
|
&& is_subclass_of($table, 'DataObject')
|
2009-08-11 10:49:52 +02:00
|
|
|
&& DataObject::has_own_table($table);
|
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) {
|
2010-10-12 23:42:45 +02:00
|
|
|
$rPos = strrpos($table,'_');
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2010-10-12 23:42:45 +02:00
|
|
|
if(($rPos !== false) && in_array(substr($table,$rPos), $this->stages)) {
|
|
|
|
$tableWithoutStage = substr($table,0,$rPos);
|
|
|
|
} else {
|
|
|
|
$tableWithoutStage = $table;
|
|
|
|
}
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2010-10-12 23:42:45 +02:00
|
|
|
return ('DataObject' == get_parent_class($tableWithoutStage));
|
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) {
|
2007-09-16 18:15:47 +02:00
|
|
|
foreach (Versioned::$versionableExtensions as $versionableExtension => $suffixes) {
|
|
|
|
if ($this->owner->hasExtension($versionableExtension)) {
|
2010-05-25 05:55:30 +02:00
|
|
|
$ext = $this->owner->getExtensionInstance($versionableExtension);
|
2009-06-04 08:48:44 +02:00
|
|
|
$ext->setOwner($this->owner);
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the latest published DataObject.
|
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 latestPublished() {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Get the root data object class - this will have the version field
|
|
|
|
$table1 = $this->owner->class;
|
|
|
|
while( ($p = get_parent_class($table1)) != "DataObject") $table1 = $p;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$table2 = $table1 . "_$this->liveStage";
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
return DB::prepared_query("SELECT \"$table1\".\"Version\" = \"$table2\".\"Version\" FROM \"$table1\"
|
|
|
|
INNER JOIN \"$table2\" ON \"$table1\".\"ID\" = \"$table2\".\"ID\"
|
|
|
|
WHERE \"$table1\".\"ID\" = ?",
|
|
|
|
array($this->owner->ID)
|
|
|
|
)->value();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param fromStage Place to copy from. Can be either a stage name or a version number.
|
|
|
|
* @param toStage Place to copy to. Must be a stage name.
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param createNewVersion Set this to true to create a new version number. By default, the existing version
|
|
|
|
* number will be copied over.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function publish($fromStage, $toStage, $createNewVersion = false) {
|
2010-12-11 02:28:46 +01:00
|
|
|
$this->owner->extend('onBeforeVersionedPublish', $fromStage, $toStage, $createNewVersion);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$baseClass = $this->owner->class;
|
|
|
|
while( ($p = get_parent_class($baseClass)) != "DataObject") $baseClass = $p;
|
2009-08-27 06:43:40 +02:00
|
|
|
$extTable = $this->extendWithSuffix($baseClass);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_numeric($fromStage)) {
|
2009-07-03 03:21:13 +02:00
|
|
|
$from = Versioned::get_version($baseClass, $this->owner->ID, $fromStage);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
$this->owner->flushCache();
|
2012-09-26 23:34:00 +02:00
|
|
|
$from = Versioned::get_one_by_stage($baseClass, $fromStage, "\"{$baseClass}\".\"ID\"={$this->owner->ID}");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-20 01:15:05 +02:00
|
|
|
$publisherID = isset(Member::currentUser()->ID) ? Member::currentUser()->ID : 0;
|
2007-07-19 12:40:28 +02:00
|
|
|
if($from) {
|
|
|
|
$from->forceChange();
|
2010-10-13 03:04:38 +02:00
|
|
|
if($createNewVersion) {
|
|
|
|
$latest = self::get_latest_version($baseClass, $this->owner->ID);
|
|
|
|
$this->owner->Version = $latest->Version + 1;
|
|
|
|
} else {
|
|
|
|
$from->migrateVersion($from->Version);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Mark this version as having been published at some stage
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::prepared_query("UPDATE \"{$extTable}_versions\"
|
|
|
|
SET \"WasPublished\" = ?, \"PublisherID\" = ?
|
|
|
|
WHERE \"RecordID\" = ? AND \"Version\" = ?",
|
|
|
|
array(1, $publisherID, $from->ID, $from->Version)
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2010-04-13 05:16:57 +02:00
|
|
|
$oldMode = Versioned::get_reading_mode();
|
|
|
|
Versioned::reading_stage($toStage);
|
2009-07-02 00:27:18 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$conn = DB::get_conn();
|
2009-08-08 06:23:05 +02:00
|
|
|
if(method_exists($conn, 'allowPrimaryKeyEditing')) $conn->allowPrimaryKeyEditing($baseClass, true);
|
2007-07-19 12:40:28 +02:00
|
|
|
$from->write();
|
2009-08-08 06:23:05 +02:00
|
|
|
if(method_exists($conn, 'allowPrimaryKeyEditing')) $conn->allowPrimaryKeyEditing($baseClass, false);
|
2009-07-02 00:27:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$from->destroy();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 05:16:57 +02:00
|
|
|
Versioned::set_reading_mode($oldMode);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
user_error("Can't find {$this->owner->URLSegment}/{$this->owner->ID} in stage $fromStage", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*/
|
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);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!is_numeric($this->owner->ID)) {
|
|
|
|
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\" = ?",
|
|
|
|
array($this->owner->ID)
|
|
|
|
)->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
|
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
|
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
|
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)
|
|
|
|
$oldMode = self::get_reading_mode();
|
|
|
|
self::reading_stage('Stage');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-07-24 04:17:12 +02:00
|
|
|
$list = DataObject::get(get_class($this->owner), $filter, $sort, $join, $limit);
|
2011-10-29 06:12:37 +02:00
|
|
|
if($having) $having = $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
|
|
|
|
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(
|
|
|
|
$table => "LEFT JOIN \"$table\" ON \"$table\".\"RecordID\"=\"{$baseTable}_versions\".\"RecordID\""
|
|
|
|
. " AND \"$table\".\"Version\" = \"{$baseTable}_versions\".\"Version\""
|
|
|
|
));
|
2011-09-27 04:14:01 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
$query->renameTable($table, $table . '_versions');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-12 06:47:56 +02:00
|
|
|
// Add all <basetable>_versions columns
|
2013-03-21 19:48:54 +01:00
|
|
|
foreach(Config::inst()->get('Versioned', 'db_for_versions_table') as $name => $type) {
|
2012-05-01 06:42:14 +02:00
|
|
|
$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(
|
|
|
|
"\"{$baseTable}_versions\".\"RecordID\" = ?" => $this->owner->ID
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
$query->setOrderBy(($sort) ? $sort
|
2012-09-26 23:34:00 +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) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$fromRecord = Versioned::get_version($this->owner->class, $this->owner->ID, $from);
|
|
|
|
$toRecord = Versioned::get_version($this->owner->class, $this->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
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function baseTable($stage = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$tableClasses = ClassInfo::dataClassesFor($this->owner->class);
|
|
|
|
$baseClass = array_shift($tableClasses);
|
2013-05-10 11:19:16 +02:00
|
|
|
|
|
|
|
if(!$stage || $stage == $this->defaultStage) {
|
|
|
|
return $baseClass;
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
return $baseClass . "_$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
|
|
|
//-----------------------------------------------------------------------------------------------//
|
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
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function can_choose_site_stage($request) {
|
|
|
|
// Request is allowed if stage isn't being modified
|
|
|
|
if((!$request->getVar('stage') || $request->getVar('stage') === static::get_live_stage())
|
|
|
|
&& !$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']));
|
|
|
|
if(!in_array($stage, array('Stage', 'Live'))) $stage = '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 {
|
2014-06-11 06:14:56 +02:00
|
|
|
$mode = self::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)
|
|
|
|
|| (!$preexistingMode && $mode !== self::DEFAULT_MODE)
|
|
|
|
) {
|
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()) {
|
2010-10-15 03:22:25 +02:00
|
|
|
if(Versioned::current_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) {
|
2010-04-13 05:16:57 +02:00
|
|
|
Versioned::$reading_mode = $mode;
|
|
|
|
}
|
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() {
|
2010-04-13 05:16:57 +02:00
|
|
|
return Versioned::$reading_mode;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the name of the 'live' stage.
|
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 get_live_stage() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return "Live";
|
|
|
|
}
|
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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function current_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];
|
|
|
|
}
|
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());
|
|
|
|
if($parts[0] == 'Archive') return $parts[1];
|
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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function reading_stage($stage) {
|
2010-04-13 05:16:57 +02:00
|
|
|
Versioned::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.
|
|
|
|
* @param string $orderby 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
|
|
|
|
$items = self::get_by_stage($class, $stage, $filter, $sort, null, 1);
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2012-05-28 11:13:42 +02:00
|
|
|
return $items->First();
|
|
|
|
}
|
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) {
|
2008-12-17 23:38:47 +01:00
|
|
|
$baseClass = ClassInfo::baseDataClass($class);
|
|
|
|
$stageTable = ($stage == 'Stage') ? $baseClass : "{$baseClass}_{$stage}";
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2008-12-17 23:38:47 +01:00
|
|
|
// get version as performance-optimized SQL query (gets called for each page 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
|
2013-05-10 11:19:16 +02:00
|
|
|
* is null, then every page will be pre-cached.
|
|
|
|
*
|
|
|
|
* @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) {
|
2013-06-04 15:28:36 +02:00
|
|
|
if (!Config::inst()->get('Versioned', 'prepopulate_versionnumber_cache')) {
|
|
|
|
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
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
$baseClass = ClassInfo::baseDataClass($class);
|
|
|
|
$stageTable = ($stage == 'Stage') ? $baseClass : "{$baseClass}_{$stage}";
|
|
|
|
|
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
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public static function get_by_stage($class, $stage, $filter = '', $sort = '', $join = '', $limit = '',
|
|
|
|
$containerClass = 'DataList') {
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param string $stage
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
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();
|
|
|
|
Versioned::reading_stage($stage);
|
2010-10-15 04:55:45 +02:00
|
|
|
$clone = clone $this->owner;
|
|
|
|
$result = $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)
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
|
|
|
self::$cache_versionnumber[$baseClass][$stage][$this->owner->ID] = null;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $result;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
|
|
|
* @param string $stage
|
|
|
|
* @param boolean $forceInsert
|
|
|
|
*/
|
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();
|
|
|
|
Versioned::reading_stage($stage);
|
2013-05-10 11:19:16 +02:00
|
|
|
|
2014-03-04 02:48:04 +01:00
|
|
|
$this->owner->forceChange();
|
2007-07-19 12:40:28 +02:00
|
|
|
$result = $this->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
|
|
|
/**
|
|
|
|
* Roll the draft version of this page to match the published page.
|
|
|
|
* Caution: Doesn't overwrite the object properties with the rolled back version.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-10 11:19:16 +02:00
|
|
|
* @param int $version Either the string 'Live' or a version number
|
2011-03-23 04:02:10 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function doRollbackTo($version) {
|
2013-04-06 12:24:57 +02:00
|
|
|
$this->owner->extend('onBeforeRollback', $version);
|
2011-03-23 04:02:10 +01:00
|
|
|
$this->publish($version, "Stage", true);
|
2013-04-06 11:59:33 +02:00
|
|
|
|
2011-03-23 04:02:10 +01:00
|
|
|
$this->owner->writeWithoutVersion();
|
2013-04-06 11:59:33 +02:00
|
|
|
|
2013-04-06 12:24:57 +02:00
|
|
|
$this->owner->extend('onAfterRollback', $version);
|
2011-03-23 04:02:10 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-12-17 23:38:47 +01:00
|
|
|
* Return the latest version of the given page.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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) {
|
2011-03-21 07:53:09 +01:00
|
|
|
$baseClass = ClassInfo::baseDataClass($class);
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = DataList::create($baseClass)
|
|
|
|
->where("\"$baseClass\".\"RecordID\" = $id")
|
|
|
|
->setDataQueryParam("Versioned.mode", "latest_versions");
|
|
|
|
|
2012-05-28 11:13:42 +02:00
|
|
|
return $list->First();
|
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() {
|
2011-08-26 04:05:54 +02:00
|
|
|
$version = self::get_latest_version($this->owner->class, $this->owner->ID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 04:05:54 +02:00
|
|
|
return ($version->Version == $this->owner->Version);
|
|
|
|
}
|
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
|
2009-05-01 05:49:34 +02:00
|
|
|
* version of each page stored in the (class)_versions tables.
|
|
|
|
*
|
|
|
|
* 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
|
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) {
|
2011-03-21 07:53:09 +01:00
|
|
|
$baseClass = ClassInfo::baseDataClass($class);
|
2012-09-26 23:34:00 +02:00
|
|
|
$list = DataList::create($baseClass)
|
|
|
|
->where("\"$baseClass\".\"RecordID\" = $id")
|
2012-12-12 05:22:45 +01:00
|
|
|
->where("\"$baseClass\".\"Version\" = " . (int)$version)
|
|
|
|
->setDataQueryParam("Versioned.mode", 'all_versions');
|
|
|
|
|
2012-05-28 11:13:42 +02:00
|
|
|
return $list->First();
|
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-05-28 11:13:42 +02:00
|
|
|
$baseClass = ClassInfo::baseDataClass($class);
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = DataList::create($class)
|
|
|
|
->where("\"$baseClass\".\"RecordID\" = $id")
|
|
|
|
->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) {
|
2012-04-14 00:16:22 +02:00
|
|
|
$labels['Versions'] = _t('Versioned.has_many_Versions', 'Versions', 'Past Versions of this page');
|
2008-11-02 21:04:10 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:27:36 +02:00
|
|
|
/**
|
|
|
|
* @param FieldList
|
|
|
|
*/
|
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() {
|
2011-03-21 07:53:09 +01:00
|
|
|
return 'versionedmode-'.self::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() {
|
|
|
|
return $this->stages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDefaultStage() {
|
|
|
|
return $this->defaultStage;
|
|
|
|
}
|
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'
|
|
|
|
);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a single version of a record.
|
2011-08-26 04:05:54 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
2011-08-26 04:05:54 +02:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @see Versioned
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Versioned_Version extends ViewableData {
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $record;
|
2013-05-10 11:19:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DataObject
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $object;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($record) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->record = $record;
|
|
|
|
$record['ID'] = $record['RecordID'];
|
|
|
|
$className = $record['ClassName'];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-29 04:30:10 +02:00
|
|
|
$this->object = ClassInfo::exists($className) ? new $className($record) : new DataObject($record);
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->failover = $this->object;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function PublishedClass() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->record['WasPublished'] ? 'published' : 'internal';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
|
|
|
* @return Member
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Author() {
|
2013-05-10 11:19:16 +02:00
|
|
|
return Member::get()->byId($this->record['AuthorID']);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
|
|
|
* @return Member
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Publisher() {
|
2013-05-10 11:19:16 +02:00
|
|
|
if (!$this->record['WasPublished']) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return null;
|
2013-05-10 11:19:16 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
return Member::get()->byId($this->record['PublisherID']);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:19:16 +02:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Published() {
|
2013-05-10 11:19:16 +02:00
|
|
|
return !empty($this->record['WasPublished']);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-01-14 01:37:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copied from DataObject to allow access via dot notation.
|
|
|
|
*/
|
|
|
|
public function relField($fieldName) {
|
|
|
|
$component = $this;
|
|
|
|
|
|
|
|
if(strpos($fieldName, '.') !== false) {
|
|
|
|
$parts = explode('.', $fieldName);
|
|
|
|
$fieldName = array_pop($parts);
|
|
|
|
|
|
|
|
// Traverse dot syntax
|
|
|
|
foreach($parts as $relation) {
|
|
|
|
if($component instanceof SS_List) {
|
2013-05-10 11:19:16 +02:00
|
|
|
if(method_exists($component,$relation)) {
|
|
|
|
$component = $component->$relation();
|
|
|
|
} else {
|
|
|
|
$component = $component->relation($relation);
|
|
|
|
}
|
2013-01-14 01:37:12 +01:00
|
|
|
} else {
|
|
|
|
$component = $component->$relation();
|
2013-05-10 11:19:16 +02:00
|
|
|
}
|
2013-01-14 01:37:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlike has-one's, these "relations" can return false
|
|
|
|
if($component) {
|
2013-05-10 11:19:16 +02:00
|
|
|
if ($component->hasMethod($fieldName)) {
|
|
|
|
return $component->$fieldName();
|
|
|
|
}
|
|
|
|
|
2013-01-14 01:37:12 +01:00
|
|
|
return $component->$fieldName;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|