ENHANCEMENT Added Versioned::get_versionnumber_by_stage() (used in new CMSMain/SiteTree state checking)

ENHANCEMENT Added Versioned->flushCache() to empty get_versionnumber_by_stage() caches
ENHANCEMENT Making DataObject->flushCache() decoratable

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@69409 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-12-17 22:38:47 +00:00
parent fe0a640c39
commit bc430a96df
3 changed files with 60 additions and 1 deletions

View File

@ -2329,6 +2329,8 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
if(isset(self::$cache_get_one[$class])) unset(self::$cache_get_one[$class]);
}
$this->extend('flushCache');
$this->componentCache = array();
}

View File

@ -193,6 +193,12 @@ abstract class DataObjectDecorator extends Extension {
if($field_labels) $lables = array_merge($lables, $field_labels);
}
}
/**
* Clear any internal caches.
*/
function flushCache() {
}
}

View File

@ -31,6 +31,14 @@ class Versioned extends DataObjectDecorator {
*/
public $migratingVersion;
/**
* A cache used by get_versionnumber_by_stage().
* Clear through {@link flushCache()}.
*
* @var array
*/
protected static $cache_versionnumber;
/**
* Construct a new Versioned object.
* @var array $stages The different stages the versioned object can be.
@ -592,6 +600,37 @@ class Versioned extends DataObjectDecorator {
return $result;
}
/**
* Gets the current version number of a specific record.
*
* @param string $class
* @param string $stage
* @param int $id
* @param boolean $cache
* @return int
*/
static function get_versionnumber_by_stage($class, $stage, $id, $cache = true) {
$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];
}
// get version as performance-optimized SQL query (gets called for each page in the sitetree)
$version = DB::query("SELECT Version FROM `$stageTable` WHERE ID = $id")->value();
// cache value (if required)
if($cache) {
if(!isset(self::$cache_versionnumber[$baseClass])) self::$cache_versionnumber[$baseClass] = array();
if(!isset(self::$cache_versionnumber[$baseClass][$stage])) self::$cache_versionnumber[$baseClass][$stage] = array();
self::$cache_versionnumber[$baseClass][$stage][$id] = $version;
}
return $version;
}
/**
* Get a set of class instances by the given stage.
*
@ -655,7 +694,9 @@ class Versioned extends DataObjectDecorator {
}
/**
* Return the latest version of the given page
* Return the latest version of the given page.
*
* @return DataObject
*/
static function get_latest_version($class, $id) {
$oldStage = Versioned::$reading_stage;
@ -677,6 +718,9 @@ class Versioned extends DataObjectDecorator {
return new $className($record);
}
/**
* @return DataObject
*/
static function get_version($class, $id, $version) {
$oldStage = Versioned::$reading_stage;
Versioned::$reading_stage = null;
@ -696,6 +740,9 @@ class Versioned extends DataObjectDecorator {
return new $className($record);
}
/**
* @return DataObject
*/
static function get_all_versions($class, $id, $version) {
$baseTable = ClassInfo::baseDataClass($class);
$query = singleton($class)->buildVersionSQL("\"{$baseTable}\".RecordID = $id AND \"{$baseTable}\".Version = $version");
@ -722,6 +769,10 @@ class Versioned extends DataObjectDecorator {
function updateFieldLabels(&$labels) {
$labels['Versions'] = _t('Versioned.has_many_Versions', 'Versions', PR_MEDIUM, 'Past Versions of this page');
}
function flushCache() {
self::$cache_versionnumber = array();
}
}
/**