Source of file ReindexAfterWriteJob.php
Size: 3,733 Bytes - Last Modified: 2021-12-24T06:51:11+00:00
/var/www/docs.ssmods.com/process/src/src/Jobs/ReindexAfterWriteJob.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?php namespace Heyday\Elastica\Jobs; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use SilverStripe\Versioned\Versioned; use Symbiote\QueuedJobs\Services\AbstractQueuedJob; use Symbiote\QueuedJobs\Services\QueuedJob; /** * Class ReindexAfterWriteJob * @package Heyday\Elastica\Jobs */ class ReindexAfterWriteJob extends AbstractQueuedJob implements QueuedJob { /** * * get the instance to reindex and the service * ReindexAfterWriteJob constructor. * @param int $id * @param string $class */ public function __construct($id = null, $class = null) { if ($id) { $this->id = $id; } if ($class) { $this->class = $class; } } /** * Defines the title of the job * * @return string */ public function getTitle() { return "Reindexing " . $this->class . " ID " . $this->id; } /** * Indicate to the system which queue we think we should be in based * on how many objects we're going to touch on while processing. * * We want to make sure we also set how many steps we think we might need to take to * process everything - note that this does not need to be 100% accurate, but it's nice * to give a reasonable approximation * * @return int */ public function getJobType() { $this->totalSteps = 'Lots'; return QueuedJob::QUEUED; } /** * Lets process */ public function process() { if ($this->id && $this->class) { $service = Injector::inst()->get('Heyday\Elastica\ElasticaService'); $reading_mode = Versioned::get_reading_mode(); Versioned::set_reading_mode('Stage.Live'); $versionToIndex = DataObject::get($this->class)->byID($this->id); if (!$versionToIndex->hasField('ShowInSearch') || $versionToIndex->ShowInSearch) { $service->index($versionToIndex); } else { $service->remove($versionToIndex); } $this->updateDependentClasses($versionToIndex, $service); Versioned::set_reading_mode($reading_mode); $this->isComplete = true; return; } else { return; } } /** * Updates the records of all instances of dependent classes. */ protected function updateDependentClasses($versionToIndex, $service) { $classes = $this->dependentClasses($versionToIndex); if ($classes) { foreach ($classes as $class) { $list = DataList::create($class); foreach ($list as $object) { if ($object instanceof DataObject && $object->hasExtension('Heyday\\Elastica\\Searchable') ) { if (($object instanceof SiteTree && $object->ShowInSearch) || (!$object instanceof SiteTree) ) { $service->index($object); } else { $service->remove($object); } } } } } } /** * Return an array of dependant class names. These are classes that need to be reindexed when an instance of the * extended class is updated or when a relationship to it changes. * @return array|\scalar */ public function dependentClasses($versionToIndex) { return $versionToIndex->config()->get('dependent_classes'); } } |