Source of file SolrReindexJob.php
Size: 2,769 Bytes - Last Modified: 2021-12-23T10:34:35+00:00
/var/www/docs.ssmods.com/process/src/code/tasks/SolrReindexJob.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 | <?php /** * A queued job used for reindexing content * * * @author Marcus Nyeholt <marcus@silverstripe.com.au> * @license http://silverstripe.org/bsd-license/ */ if (class_exists('AbstractQueuedJob')) { class SolrReindexJob extends AbstractQueuedJob { private static $at_a_time = 100; public function __construct($type = null) { if (!$type && isset($_GET['type'])) { $type = $_GET['type']; } if ($type) { $this->reindexType = $type; } } public function getTitle() { return "Reindex $this->reindexType content in Solr"; } public function setup() { $this->lastIndexedID = 0; $service = singleton('SolrSearchService'); $service->getSolr()->deleteByQuery('ClassNameHierarchy_ms:' . $this->reindexType); } /** * To process this job, we need to get the next page whose ID is the next greater than the last * processed. This way we don't need to remember a bunch of data about what we've processed */ public function process() { if (ClassInfo::exists('Subsite')) { Subsite::disable_subsite_filter(); } $class = $this->reindexType; $pages = $class::get(); $pages = $pages->filter(array('ID:GreaterThan' => $this->lastIndexedID)); $pages = $pages->limit(Config::inst()->get(__CLASS__, 'at_a_time')); $pages = $pages->sort('ID ASC'); if (ClassInfo::exists('Subsite')) { Subsite::$disable_subsite_filter = false; } if (!$pages || !$pages->count()) { $this->isComplete = true; return; } $mode = Versioned::get_reading_mode(); Versioned::reading_stage('Stage'); // index away $service = singleton('SolrSearchService'); $live = array(); $stage = array(); $all = array(); foreach ($pages as $page) { // Make sure the current page is not orphaned. if($page->ParentID > 0) { $parent = $page->getParent(); if(is_null($parent) || ($parent === false)) { continue; } } // Appropriately index the current page, taking versioning into account. if ($page->hasMethod('publish')) { $stage[] = $page; $base = $page->baseTable(); $idField = '"' . $base . '_Live"."ID"'; $livePage = Versioned::get_one_by_stage($page->ClassName, 'Live', $idField . ' = ' . $page->ID); if ($livePage) { $live[] = $livePage; } } else { $all[] = $page; } $this->lastIndexedID = $page->ID; } if (count($all)) { $service->indexMultiple($all); } if (count($stage)) { $service->indexMultiple($stage, 'Stage'); } if (count($live)) { $service->indexMultiple($live, 'Live'); } Versioned::set_reading_mode($mode); $this->lastIndexedID = $page->ID; $this->currentStep += $pages->count(); } } } |