Example #1
0
 /**
  * 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;
 }
Example #2
0
 public function test_extra_params_callback()
 {
     global $DB;
     $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     $courses = array();
     for ($i = 0; $i < 10; $i++) {
         $courses[$i] = $generator->create_instance(array('course' => SITEID));
     }
     // Iteration with extra callback arguments.
     $recordset = $DB->get_recordset('assign');
     $walker = new \core\dml\recordset_walk($recordset, array($this, 'extra_callback'), array('brown' => 'onions'));
     $count = 0;
     foreach ($walker as $data) {
         // Checking that the callback is being executed on each
         // iteration and the param is being passed.
         $this->assertEquals('onions', $data->brown);
         $count++;
     }
     $this->assertEquals(10, $count);
     $walker->close();
 }