2017-04-21 03:18:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\FullTextSearch\Tests\SolrReindexTest;
|
|
|
|
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
2018-05-14 18:04:40 +02:00
|
|
|
use SilverStripe\FullTextSearch\Search\Indexes\SearchIndex;
|
|
|
|
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery;
|
2017-04-21 03:32:39 +02:00
|
|
|
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
|
2017-04-26 12:52:20 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-04-21 03:18:37 +02:00
|
|
|
|
|
|
|
/**
|
2018-05-14 18:04:40 +02:00
|
|
|
* Dummy variant that selects items with field Variant matching the current value
|
2017-04-21 03:18:37 +02:00
|
|
|
*
|
|
|
|
* Variant states are 0 and 1, or null if disabled
|
|
|
|
*/
|
|
|
|
class SolrReindexTest_Variant extends SearchVariant implements TestOnly
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Value of this variant (either null, 0, or 1)
|
|
|
|
*
|
|
|
|
* @var int|null
|
|
|
|
*/
|
|
|
|
protected static $current = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activate this variant
|
|
|
|
*/
|
|
|
|
public static function enable()
|
|
|
|
{
|
|
|
|
self::disable();
|
|
|
|
|
|
|
|
self::$current = 0;
|
2018-05-14 18:04:40 +02:00
|
|
|
self::$variants = [
|
2017-04-26 12:52:20 +02:00
|
|
|
self::class => singleton(self::class)
|
2018-05-14 18:04:40 +02:00
|
|
|
];
|
2017-04-21 03:18:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable this variant and reset
|
|
|
|
*/
|
|
|
|
public static function disable()
|
|
|
|
{
|
|
|
|
self::$current = null;
|
|
|
|
self::$variants = null;
|
2018-05-14 18:04:40 +02:00
|
|
|
self::$class_variants = [];
|
|
|
|
self::$call_instances = [];
|
2017-04-21 03:18:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function activateState($state)
|
|
|
|
{
|
|
|
|
self::set_current($state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current variant to the given state
|
|
|
|
*
|
|
|
|
* @param int $current 0, 1, 2, or null (disabled)
|
|
|
|
*/
|
|
|
|
public static function set_current($current)
|
|
|
|
{
|
|
|
|
self::$current = $current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current state
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public static function get_current()
|
|
|
|
{
|
|
|
|
// Always use string values for states for consistent json_encode value
|
|
|
|
if (isset(self::$current)) {
|
2018-05-14 18:04:40 +02:00
|
|
|
return (string) self::$current;
|
2017-04-21 03:18:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 18:04:40 +02:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @param SearchIndex $index
|
|
|
|
*/
|
2017-04-21 03:18:37 +02:00
|
|
|
public function alterDefinition($class, $index)
|
|
|
|
{
|
|
|
|
$self = get_class($this);
|
|
|
|
|
2018-05-14 18:04:40 +02:00
|
|
|
$this->addFilterField($index, '_testvariant', [
|
2017-04-21 03:18:37 +02:00
|
|
|
'name' => '_testvariant',
|
|
|
|
'field' => '_testvariant',
|
|
|
|
'fullfield' => '_testvariant',
|
2017-04-26 12:52:20 +02:00
|
|
|
'base' => DataObject::getSchema()->baseDataClass($class),
|
2017-04-21 03:18:37 +02:00
|
|
|
'origin' => $class,
|
|
|
|
'type' => 'Int',
|
2018-05-14 18:04:40 +02:00
|
|
|
'lookup_chain' => [
|
|
|
|
[
|
|
|
|
'call' => 'variant',
|
|
|
|
'variant' => $self,
|
|
|
|
'method' => 'currentState'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]);
|
2017-04-21 03:18:37 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 18:04:40 +02:00
|
|
|
/**
|
|
|
|
* @param SearchQuery $query
|
|
|
|
* @param SearchIndex $index
|
|
|
|
*/
|
2017-04-21 03:18:37 +02:00
|
|
|
public function alterQuery($query, $index)
|
|
|
|
{
|
|
|
|
// I guess just calling it _testvariant is ok?
|
2018-05-14 18:04:40 +02:00
|
|
|
$query->addFilter('_testvariant', $this->currentState());
|
2017-04-21 03:18:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function appliesTo($class, $includeSubclasses)
|
|
|
|
{
|
2017-04-26 12:52:20 +02:00
|
|
|
return $class === SolrReindexTest_Item::class ||
|
|
|
|
($includeSubclasses && is_subclass_of($class, SolrReindexTest_Item::class, true));
|
2017-04-21 03:18:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function appliesToEnvironment()
|
|
|
|
{
|
|
|
|
// Set to null to disable
|
|
|
|
return self::$current !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function currentState()
|
|
|
|
{
|
|
|
|
return self::get_current();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function reindexStates()
|
|
|
|
{
|
|
|
|
// Always use string values for states for consistent json_encode value
|
|
|
|
return array('0', '1', '2');
|
|
|
|
}
|
|
|
|
}
|