2015-08-21 04:01:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate an Aggregate on a particular field of a particular DataObject type (possibly with
|
|
|
|
* an additional filter before the aggregate)
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* Implemented as a class to provide a semi-DSL method of calculating Aggregates. DataObject has a function
|
|
|
|
* that will create & return an instance of this class with the DataObject type and filter set,
|
|
|
|
* but at that point we don't yet know the aggregate function or field
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* This class captures any XML_val or unknown call, and uses that to get the field & aggregate function &
|
|
|
|
* then return the result
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* Two ways of calling
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* $aggregate->XML_val(aggregate_function, array(field)) - For templates
|
|
|
|
* $aggregate->aggregate_function(field) - For PHP
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* Aggregate functions are uppercased by this class, but are otherwise assumed to be valid SQL functions. Some
|
|
|
|
* examples: Min, Max, Avg
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* Aggregates are often used as portions of a cacheblock key. They are therefore cached themselves, in the 'aggregate'
|
2016-01-06 00:34:58 +01:00
|
|
|
* cache, although the invalidation logic prefers speed over keeping valid data.
|
2015-08-21 04:01:10 +02:00
|
|
|
* The aggregate cache is cleared through {@link DataObject::flushCache()}, which in turn is called on
|
2016-01-06 00:34:58 +01:00
|
|
|
* {@link DataObject->write()} and other write operations.
|
2015-08-21 04:01:10 +02:00
|
|
|
* This means most write operations to the database will invalidate the cache correctly.
|
|
|
|
* Use {@link Aggregate::flushCache()} to manually clear.
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* NOTE: The cache logic uses tags, and so a backend that supports tags is required. Currently only the File
|
|
|
|
* backend (and the two-level backend with the File backend as the slow store) meets this requirement
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* @deprecated 3.1 Use DataList to aggregate data
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* @author hfried
|
|
|
|
* @package framework
|
|
|
|
* @subpackage core
|
|
|
|
*/
|
|
|
|
class Aggregate extends ViewableData {
|
|
|
|
|
|
|
|
private static $cache = null;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
/** Build & cache the cache object */
|
|
|
|
protected static function cache() {
|
|
|
|
return self::$cache ? self::$cache : (self::$cache = SS_Cache::factory('aggregate'));
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:34:58 +01:00
|
|
|
/**
|
2015-08-21 04:01:10 +02:00
|
|
|
* Clear the aggregate cache for a given type, or pass nothing to clear all aggregate caches.
|
|
|
|
* {@link $class} is just effective if the cache backend supports tags.
|
|
|
|
*/
|
|
|
|
public static function flushCache($class=null) {
|
|
|
|
$cache = self::cache();
|
|
|
|
$capabilities = $cache->getBackend()->getCapabilities();
|
|
|
|
if($capabilities['tags'] && (!$class || $class == 'DataObject')) {
|
|
|
|
$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('aggregate'));
|
|
|
|
} elseif($capabilities['tags']) {
|
|
|
|
$tags = ClassInfo::ancestry($class);
|
|
|
|
foreach($tags as &$tag) {
|
|
|
|
$tag = preg_replace('/[^a-zA-Z0-9_]/', '_', $tag);
|
|
|
|
}
|
|
|
|
$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags);
|
|
|
|
} else {
|
|
|
|
$cache->clean(Zend_Cache::CLEANING_MODE_ALL);
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* @deprecated 3.1 Use DataList to aggregate data
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* @param string $type The DataObject type we are building an aggregate for
|
|
|
|
* @param string $filter (optional) An SQL filter to apply to the selected rows before calculating the aggregate
|
|
|
|
*/
|
|
|
|
public function __construct($type, $filter = '') {
|
|
|
|
Deprecation::notice('4.0', 'Call aggregate methods on a DataList directly instead. In templates'
|
|
|
|
. ' an example of the new syntax is <% cached List(Member).max(LastEdited) %> instead'
|
|
|
|
. ' (check partial-caching.md documentation for more details.)');
|
|
|
|
$this->type = $type;
|
|
|
|
$this->filter = $filter;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the SQLSelect to calculate the aggregate
|
|
|
|
* This is a seperate function so that subtypes of Aggregate can change just this bit
|
|
|
|
* @param string $attr - the SQL field statement for selection (i.e. "MAX(LastUpdated)")
|
|
|
|
* @return SQLSelect
|
|
|
|
*/
|
|
|
|
protected function query($attr) {
|
|
|
|
$query = DataList::create($this->type)->where($this->filter);
|
|
|
|
$query->setSelect($attr);
|
2016-01-06 00:34:58 +01:00
|
|
|
$query->setOrderBy(array());
|
2015-08-21 04:01:10 +02:00
|
|
|
$singleton->extend('augmentSQL', $query);
|
|
|
|
return $query;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
/**
|
|
|
|
* Entry point for being called from a template.
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
|
|
|
* This gets the aggregate function
|
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
*/
|
|
|
|
public function XML_val($name, $args = null, $cache = false) {
|
|
|
|
$func = strtoupper( strpos($name, 'get') === 0 ? substr($name, 3) : $name );
|
|
|
|
$attribute = $args ? $args[0] : 'ID';
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
$table = null;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
foreach (ClassInfo::ancestry($this->type, true) as $class) {
|
|
|
|
$fields = DataObject::database_fields($class, false);
|
|
|
|
if (array_key_exists($attribute, $fields)) { $table = $class; break; }
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
if (!$table) user_error("Couldn't find table for field $attribute in type {$this->type}", E_USER_ERROR);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
$query = $this->query("$func(\"$table\".\"$attribute\")");
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
// Cache results of this specific SQL query until flushCache() is triggered.
|
|
|
|
$sql = $query->sql($parameters);
|
|
|
|
$cachekey = sha1($sql.'-'.var_export($parameters, true));
|
|
|
|
$cache = self::cache();
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
if (!($result = $cache->load($cachekey))) {
|
|
|
|
$result = (string)$query->execute()->value(); if (!$result) $result = '0';
|
|
|
|
$cache->save($result, null, array('aggregate', preg_replace('/[^a-zA-Z0-9_]/', '_', $this->type)));
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
return $result;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
/**
|
|
|
|
* Entry point for being called from PHP.
|
|
|
|
*/
|
|
|
|
public function __call($method, $arguments) {
|
|
|
|
return $this->XML_val($method, $arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A subclass of Aggregate that calculates aggregates for the result of a has_many query.
|
|
|
|
*
|
|
|
|
* @deprecated
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* @author hfried
|
|
|
|
* @package framework
|
|
|
|
* @subpackage core
|
|
|
|
*/
|
|
|
|
class Aggregate_Relationship extends Aggregate {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
|
|
|
* @param DataObject $object The object that has_many somethings that we're calculating the aggregate for
|
2015-08-21 04:01:10 +02:00
|
|
|
* @param string $relationship The name of the relationship
|
|
|
|
* @param string $filter (optional) An SQL filter to apply to the relationship rows before calculating the
|
|
|
|
* aggregate
|
|
|
|
*/
|
|
|
|
public function __construct($object, $relationship, $filter = '') {
|
|
|
|
$this->object = $object;
|
|
|
|
$this->relationship = $relationship;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
$this->has_many = $object->has_many($relationship);
|
|
|
|
$this->many_many = $object->many_many($relationship);
|
|
|
|
|
|
|
|
if (!$this->has_many && !$this->many_many) {
|
|
|
|
user_error("Could not find relationship $relationship on object class {$object->class} in"
|
|
|
|
. " Aggregate Relationship", E_USER_ERROR);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
parent::__construct($this->has_many ? $this->has_many : $this->many_many[1], $filter);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
protected function query($attr) {
|
|
|
|
if ($this->has_many) {
|
|
|
|
$query = $this->object->getComponentsQuery($this->relationship, $this->filter);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$query = $this->object->getManyManyComponentsQuery($this->relationship, $this->filter);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
$query->setSelect($attr);
|
|
|
|
$query->setGroupBy(array());
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
$singleton = singleton($this->type);
|
|
|
|
$singleton->extend('augmentSQL', $query);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}
|