Source of file JsonBulkExport.php
Size: 4,358 Bytes - Last Modified: 2021-12-23T10:02:56+00:00
/var/www/docs.ssmods.com/process/src/src/Jobs/JsonBulkExport.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 | <?php namespace Marcz\Swiftype\Jobs; use Symbiote\QueuedJobs\Services\AbstractQueuedJob; use Symbiote\QueuedJobs\Services\QueuedJob; use SilverStripe\Assets\File; use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\Core\Config\Config as FileConfig; use Marcz\Swiftype\Processor\SwiftExporter; use Marcz\Swiftype\SwiftypeClient; use Exception; use Marcz\Search\Config; class JsonBulkExport extends AbstractQueuedJob implements QueuedJob { protected $bulkArray = []; protected $client; /** * Methods that corresponds to the chronological steps for this job. * All methods must return true to signal successful process * * @var array */ protected $definedSteps = [ 'stepCreateFile', 'stepSendFile', ]; /** * @param string $className * @param int $offset */ public function __construct($indexName = null, $className = null, $offset = 0) { $this->totalSteps = count($this->definedSteps); $this->currentStep = 0; $this->indexName = $indexName; $this->className = $className; $this->offset = (int) $offset; $this->fileId = 0; $this->bulk = []; } /** * @return string */ public function getTitle() { return 'Json document export: "' . $this->className . '" starting at ' . $this->offset; } /** * @return string */ public function getJobType() { return QueuedJob::QUEUED; } public function process() { if (!$this->className) { throw new Exception('Missing className defined on the constructor'); } if (!isset($this->definedSteps[$this->currentStep])) { throw new Exception('User error, unknown step defined.'); } $stepIsDone = call_user_func([$this, $this->definedSteps[$this->currentStep]]); if ($stepIsDone) { $this->currentStep++; } // and checking whether we're complete if ($this->currentStep >= $this->totalSteps) { $this->isComplete = true; } } public function stepCreateFile() { $this->addMessage('Step 1: Create File'); $file = new File(); $exporter = SwiftExporter::create(); $dateTime = DBDatetime::now(); $fileName = sprintf( '%s_export_%s_%d.json', $this->className, $dateTime->URLDatetime(), $this->offset ); $batchLength = SwiftypeClient::config()->get('batch_length') ?: Config::config()->get('batch_length'); $this->bulkArray = $exporter->bulkExport($this->className, $this->offset, $batchLength, SwiftypeClient::class); FileConfig::modify()->set(File::class, 'allowed_extensions', ['json']); $file->setFromString(json_encode($this->bulkArray), $fileName); $file->write(); $file->publishFile(); $this->fileId = $file->ID; $this->addMessage('<p><a href="' . $file->getAbsoluteURL() . '" target="_blank">' . $fileName . '</a></p>'); return true; } public function stepSendFile() { $this->addMessage('Step 2: Send File'); $method = $this->bulkArray ? 'updateByArray' : 'updateByFile'; $parameter = $this->bulkArray ? $this->bulkArray : $this->fileId; call_user_func([$this, $method], $parameter); return true; } /** * Called when the job is determined to be 'complete' * Clean-up object properties */ public function afterComplete() { $this->indexName = null; $this->className = null; $this->offset = 0; $this->fileId = 0; $this->bulk = []; } public function createClient($client = null) { if (!$client) { $this->client = SwiftypeClient::create(); } $this->client->initIndex($this->indexName); return $this->client; } public function updateByArray($bulkArray) { $this->createClient(); $this->client->bulkUpdate($bulkArray); } public function updateByFile($fileId) { $file = File::get()->byID($fileId); $bulkArray = json_decode($file->getString(), true); $this->updateByArray($bulkArray); } } |