/** * Index all documents. * * @param bool $fullindex Whether we should reindex everything or not. * @throws \moodle_exception * @return bool Whether there was any updated document or not. */ public function index($fullindex = false) { global $CFG; // Unlimited time. \core_php_time_limit::raise(); // Notify the engine that an index starting. $this->engine->index_starting($fullindex); $sumdocs = 0; $searchareas = $this->get_search_areas_list(true); foreach ($searchareas as $areaid => $searcharea) { if (CLI_SCRIPT && !PHPUNIT_TEST) { mtrace('Processing ' . $searcharea->get_visible_name() . ' area'); } // Notify the engine that an area is starting. $this->engine->area_index_starting($searcharea, $fullindex); $indexingstart = time(); // This is used to store this component config. list($componentconfigname, $varname) = $searcharea->get_config_var_name(); $numrecords = 0; $numdocs = 0; $numdocsignored = 0; $lastindexeddoc = 0; $prevtimestart = intval(get_config($componentconfigname, $varname . '_indexingstart')); if ($fullindex === true) { $referencestarttime = 0; } else { $referencestarttime = $prevtimestart; } // Getting the recordset from the area. $recordset = $searcharea->get_recordset_by_timestamp($referencestarttime); // Pass get_document as callback. $fileindexing = $this->engine->file_indexing_enabled() && $searcharea->uses_file_indexing(); $options = array('indexfiles' => $fileindexing, 'lastindexedtime' => $prevtimestart); $iterator = new \core\dml\recordset_walk($recordset, array($searcharea, 'get_document'), $options); foreach ($iterator as $document) { if (!$document instanceof \core_search\document) { continue; } if ($prevtimestart == 0) { // If we have never indexed this area before, it must be new. $document->set_is_new(true); } if ($fileindexing) { // Attach files if we are indexing. $searcharea->attach_files($document); } if ($this->engine->add_document($document, $fileindexing)) { $numdocs++; } else { $numdocsignored++; } $lastindexeddoc = $document->get('modified'); $numrecords++; } if (CLI_SCRIPT && !PHPUNIT_TEST) { if ($numdocs > 0) { mtrace('Processed ' . $numrecords . ' records containing ' . $numdocs . ' documents for ' . $searcharea->get_visible_name() . ' area.'); } else { mtrace('No new documents to index for ' . $searcharea->get_visible_name() . ' area.'); } } // Notify the engine this area is complete, and only mark times if true. if ($this->engine->area_index_complete($searcharea, $numdocs, $fullindex)) { $sumdocs += $numdocs; // Store last index run once documents have been commited to the search engine. set_config($varname . '_indexingstart', $indexingstart, $componentconfigname); set_config($varname . '_indexingend', time(), $componentconfigname); set_config($varname . '_docsignored', $numdocsignored, $componentconfigname); set_config($varname . '_docsprocessed', $numdocs, $componentconfigname); set_config($varname . '_recordsprocessed', $numrecords, $componentconfigname); if ($lastindexeddoc > 0) { set_config($varname . '_lastindexrun', $lastindexeddoc, $componentconfigname); } } } if ($sumdocs > 0) { $event = \core\event\search_indexed::create(array('context' => \context_system::instance())); $event->trigger(); } $this->engine->index_complete($sumdocs, $fullindex); return (bool) $sumdocs; }
/** * Index all documents. * * @param bool $fullindex Whether we should reindex everything or not. * @throws \moodle_exception * @return bool Whether there was any updated document or not. */ public function index($fullindex = false) { global $CFG; // Unlimited time. \core_php_time_limit::raise(); // Notify the engine that an index starting. $this->engine->index_starting($fullindex); $sumdocs = 0; $searchareas = $this->get_search_areas_list(true); foreach ($searchareas as $areaid => $searcharea) { if (CLI_SCRIPT && !PHPUNIT_TEST) { mtrace('Processing ' . $searcharea->get_visible_name() . ' area'); } // Notify the engine that an area is starting. $this->engine->area_index_starting($searcharea, $fullindex); $indexingstart = time(); // This is used to store this component config. list($componentconfigname, $varname) = $searcharea->get_config_var_name(); $numrecords = 0; $numdocs = 0; $numdocsignored = 0; $lastindexeddoc = 0; if ($fullindex === true) { $prevtimestart = 0; } else { $prevtimestart = intval(get_config($componentconfigname, $varname . '_indexingstart')); } // Getting the recordset from the area. $recordset = $searcharea->get_recordset_by_timestamp($prevtimestart); // Pass get_document as callback. $iterator = new \core\dml\recordset_walk($recordset, array($searcharea, 'get_document')); foreach ($iterator as $document) { if (!$document instanceof \core_search\document) { continue; } $docdata = $document->export_for_engine(); switch ($docdata['type']) { case static::TYPE_TEXT: $this->engine->add_document($docdata); $numdocs++; break; default: $numdocsignored++; $iterator->close(); throw new \moodle_exception('doctypenotsupported', 'search'); } $lastindexeddoc = $document->get('modified'); $numrecords++; } if (CLI_SCRIPT && !PHPUNIT_TEST) { if ($numdocs > 0) { mtrace('Processed ' . $numrecords . ' records containing ' . $numdocs . ' documents for ' . $searcharea->get_visible_name() . ' area.'); } else { mtrace('No new documents to index for ' . $searcharea->get_visible_name() . ' area.'); } } // Notify the engine this area is complete, and only mark times if true. if ($this->engine->area_index_complete($searcharea, $numdocs, $fullindex)) { $sumdocs += $numdocs; // Store last index run once documents have been commited to the search engine. set_config($varname . '_indexingstart', $indexingstart, $componentconfigname); set_config($varname . '_indexingend', time(), $componentconfigname); set_config($varname . '_docsignored', $numdocsignored, $componentconfigname); set_config($varname . '_docsprocessed', $numdocs, $componentconfigname); set_config($varname . '_recordsprocessed', $numrecords, $componentconfigname); if ($lastindexeddoc > 0) { set_config($varname . '_lastindexrun', $lastindexeddoc, $componentconfigname); } } } if ($sumdocs > 0) { $event = \core\event\search_indexed::create(array('context' => \context_system::instance())); $event->trigger(); } $this->engine->index_complete($sumdocs, $fullindex); return (bool) $sumdocs; }