Merge branch 'MDL-63765-master' of git://github.com/damyon/moodle
[moodle.git] / search / classes / manager.php
blob007c6695b9b8fdb683990f9897a7899913f40b13
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Search subsystem manager.
20 * @package core_search
21 * @copyright Prateek Sachan {@link http://prateeksachan.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_search;
27 defined('MOODLE_INTERNAL') || die;
29 require_once($CFG->dirroot . '/lib/accesslib.php');
31 /**
32 * Search subsystem manager.
34 * @package core_search
35 * @copyright Prateek Sachan {@link http://prateeksachan.com}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class manager {
40 /**
41 * @var int Text contents.
43 const TYPE_TEXT = 1;
45 /**
46 * @var int File contents.
48 const TYPE_FILE = 2;
50 /**
51 * @var int User can not access the document.
53 const ACCESS_DENIED = 0;
55 /**
56 * @var int User can access the document.
58 const ACCESS_GRANTED = 1;
60 /**
61 * @var int The document was deleted.
63 const ACCESS_DELETED = 2;
65 /**
66 * @var int Maximum number of results that will be retrieved from the search engine.
68 const MAX_RESULTS = 100;
70 /**
71 * @var int Number of results per page.
73 const DISPLAY_RESULTS_PER_PAGE = 10;
75 /**
76 * @var int The id to be placed in owneruserid when there is no owner.
78 const NO_OWNER_ID = 0;
80 /**
81 * @var float If initial query takes longer than N seconds, this will be shown in cron log.
83 const DISPLAY_LONG_QUERY_TIME = 5.0;
85 /**
86 * @var float Adds indexing progress within one search area to cron log every N seconds.
88 const DISPLAY_INDEXING_PROGRESS_EVERY = 30.0;
90 /**
91 * @var int Context indexing: normal priority.
93 const INDEX_PRIORITY_NORMAL = 100;
95 /**
96 * @var int Context indexing: low priority for reindexing.
98 const INDEX_PRIORITY_REINDEXING = 50;
101 * @var \core_search\base[] Enabled search areas.
103 protected static $enabledsearchareas = null;
106 * @var \core_search\base[] All system search areas.
108 protected static $allsearchareas = null;
111 * @var \core_search\manager
113 protected static $instance = null;
116 * @var \core_search\engine
118 protected $engine = null;
121 * Note: This should be removed once possible (see MDL-60644).
123 * @var float Fake current time for use in PHPunit tests
125 protected static $phpunitfaketime = 0;
128 * Constructor, use \core_search\manager::instance instead to get a class instance.
130 * @param \core_search\base The search engine to use
132 public function __construct($engine) {
133 $this->engine = $engine;
137 * @var int Record time of each successful schema check, but not more than once per 10 minutes.
139 const SCHEMA_CHECK_TRACKING_DELAY = 10 * 60;
142 * @var int Require a new schema check at least every 4 hours.
144 const SCHEMA_CHECK_REQUIRED_EVERY = 4 * 3600;
147 * Returns an initialised \core_search instance.
149 * While constructing the instance, checks on the search schema may be carried out. The $fast
150 * parameter provides a way to skip those checks on pages which are used frequently. It has
151 * no effect if an instance has already been constructed in this request.
153 * @see \core_search\engine::is_installed
154 * @see \core_search\engine::is_server_ready
155 * @param bool $fast Set to true when calling on a page that requires high performance
156 * @throws \core_search\engine_exception
157 * @return \core_search\manager
159 public static function instance($fast = false) {
160 global $CFG;
162 // One per request, this should be purged during testing.
163 if (static::$instance !== null) {
164 return static::$instance;
167 if (empty($CFG->searchengine)) {
168 throw new \core_search\engine_exception('enginenotselected', 'search');
171 if (!$engine = static::search_engine_instance()) {
172 throw new \core_search\engine_exception('enginenotfound', 'search', '', $CFG->searchengine);
175 // Get time now and at last schema check.
176 $now = (int)self::get_current_time();
177 $lastschemacheck = get_config($engine->get_plugin_name(), 'lastschemacheck');
179 // On pages where performance matters, tell the engine to skip schema checks.
180 $skipcheck = false;
181 if ($fast && $now < $lastschemacheck + self::SCHEMA_CHECK_REQUIRED_EVERY) {
182 $skipcheck = true;
183 $engine->skip_schema_check();
186 if (!$engine->is_installed()) {
187 throw new \core_search\engine_exception('enginenotinstalled', 'search', '', $CFG->searchengine);
190 $serverstatus = $engine->is_server_ready();
191 if ($serverstatus !== true) {
192 // Skip this error in Behat when faking seach results.
193 if (!defined('BEHAT_SITE_RUNNING') || !get_config('core_search', 'behat_fakeresult')) {
194 // Clear the record of successful schema checks since it might have failed.
195 unset_config('lastschemacheck', $engine->get_plugin_name());
196 // Error message with no details as this is an exception that any user may find if the server crashes.
197 throw new \core_search\engine_exception('engineserverstatus', 'search');
201 // If we did a successful schema check, record this, but not more than once per 10 minutes
202 // (to avoid updating the config db table/cache too often in case it gets called frequently).
203 if (!$skipcheck && $now >= $lastschemacheck + self::SCHEMA_CHECK_TRACKING_DELAY) {
204 set_config('lastschemacheck', $now, $engine->get_plugin_name());
207 static::$instance = new \core_search\manager($engine);
208 return static::$instance;
212 * Returns whether global search is enabled or not.
214 * @return bool
216 public static function is_global_search_enabled() {
217 global $CFG;
218 return !empty($CFG->enableglobalsearch);
222 * Returns whether indexing is enabled or not (you can enable indexing even when search is not
223 * enabled at the moment, so as to have it ready for students).
225 * @return bool True if indexing is enabled.
227 public static function is_indexing_enabled() {
228 global $CFG;
229 return !empty($CFG->enableglobalsearch) || !empty($CFG->searchindexwhendisabled);
233 * Returns an instance of the search engine.
235 * @return \core_search\engine
237 public static function search_engine_instance() {
238 global $CFG;
240 $classname = '\\search_' . $CFG->searchengine . '\\engine';
241 if (!class_exists($classname)) {
242 return false;
245 return new $classname();
249 * Returns the search engine.
251 * @return \core_search\engine
253 public function get_engine() {
254 return $this->engine;
258 * Returns a search area class name.
260 * @param string $areaid
261 * @return string
263 protected static function get_area_classname($areaid) {
264 list($componentname, $areaname) = static::extract_areaid_parts($areaid);
265 return '\\' . $componentname . '\\search\\' . $areaname;
269 * Returns a new area search indexer instance.
271 * @param string $areaid
272 * @return \core_search\base|bool False if the area is not available.
274 public static function get_search_area($areaid) {
276 // We have them all here.
277 if (!empty(static::$allsearchareas[$areaid])) {
278 return static::$allsearchareas[$areaid];
281 $classname = static::get_area_classname($areaid);
283 if (class_exists($classname) && static::is_search_area($classname)) {
284 return new $classname();
287 return false;
291 * Return the list of available search areas.
293 * @param bool $enabled Return only the enabled ones.
294 * @return \core_search\base[]
296 public static function get_search_areas_list($enabled = false) {
298 // Two different arrays, we don't expect these arrays to be big.
299 if (static::$allsearchareas !== null) {
300 if (!$enabled) {
301 return static::$allsearchareas;
302 } else {
303 return static::$enabledsearchareas;
307 static::$allsearchareas = array();
308 static::$enabledsearchareas = array();
310 $plugintypes = \core_component::get_plugin_types();
311 foreach ($plugintypes as $plugintype => $unused) {
312 $plugins = \core_component::get_plugin_list($plugintype);
313 foreach ($plugins as $pluginname => $pluginfullpath) {
315 $componentname = $plugintype . '_' . $pluginname;
316 $searchclasses = \core_component::get_component_classes_in_namespace($componentname, 'search');
317 foreach ($searchclasses as $classname => $classpath) {
318 $areaname = substr(strrchr($classname, '\\'), 1);
320 if (!static::is_search_area($classname)) {
321 continue;
324 $areaid = static::generate_areaid($componentname, $areaname);
325 $searchclass = new $classname();
327 static::$allsearchareas[$areaid] = $searchclass;
328 if ($searchclass->is_enabled()) {
329 static::$enabledsearchareas[$areaid] = $searchclass;
335 $subsystems = \core_component::get_core_subsystems();
336 foreach ($subsystems as $subsystemname => $subsystempath) {
337 $componentname = 'core_' . $subsystemname;
338 $searchclasses = \core_component::get_component_classes_in_namespace($componentname, 'search');
340 foreach ($searchclasses as $classname => $classpath) {
341 $areaname = substr(strrchr($classname, '\\'), 1);
343 if (!static::is_search_area($classname)) {
344 continue;
347 $areaid = static::generate_areaid($componentname, $areaname);
348 $searchclass = new $classname();
349 static::$allsearchareas[$areaid] = $searchclass;
350 if ($searchclass->is_enabled()) {
351 static::$enabledsearchareas[$areaid] = $searchclass;
356 if ($enabled) {
357 return static::$enabledsearchareas;
359 return static::$allsearchareas;
363 * Clears all static caches.
365 * @return void
367 public static function clear_static() {
369 static::$enabledsearchareas = null;
370 static::$allsearchareas = null;
371 static::$instance = null;
373 base_block::clear_static();
374 engine::clear_users_cache();
378 * Generates an area id from the componentname and the area name.
380 * There should not be any naming conflict as the area name is the
381 * class name in component/classes/search/.
383 * @param string $componentname
384 * @param string $areaname
385 * @return void
387 public static function generate_areaid($componentname, $areaname) {
388 return $componentname . '-' . $areaname;
392 * Returns all areaid string components (component name and area name).
394 * @param string $areaid
395 * @return array Component name (Frankenstyle) and area name (search area class name)
397 public static function extract_areaid_parts($areaid) {
398 return explode('-', $areaid);
402 * Returns information about the areas which the user can access.
404 * The returned value is a stdClass object with the following fields:
405 * - everything (bool, true for admin only)
406 * - usercontexts (indexed by area identifier then context
407 * - separategroupscontexts (contexts within which group restrictions apply)
408 * - visiblegroupscontextsareas (overrides to the above when the same contexts also have
409 * 'visible groups' for certain search area ids - hopefully rare)
410 * - usergroups (groups which the current user belongs to)
412 * The areas can be limited by course id and context id. If specifying context ids, results
413 * are limited to the exact context ids specified and not their children (for example, giving
414 * the course context id would result in including search items with the course context id, and
415 * not anything from a context inside the course). For performance, you should also specify
416 * course id(s) when using context ids.
418 * @param array|false $limitcourseids An array of course ids to limit the search to. False for no limiting.
419 * @param array|false $limitcontextids An array of context ids to limit the search to. False for no limiting.
420 * @return \stdClass Object as described above
422 protected function get_areas_user_accesses($limitcourseids = false, $limitcontextids = false) {
423 global $DB, $USER;
425 // All results for admins (unless they have chosen to limit results). Eventually we could
426 // add a new capability for managers.
427 if (is_siteadmin() && !$limitcourseids && !$limitcontextids) {
428 return (object)array('everything' => true);
431 $areasbylevel = array();
433 // Split areas by context level so we only iterate only once through courses and cms.
434 $searchareas = static::get_search_areas_list(true);
435 foreach ($searchareas as $areaid => $unused) {
436 $classname = static::get_area_classname($areaid);
437 $searcharea = new $classname();
438 foreach ($classname::get_levels() as $level) {
439 $areasbylevel[$level][$areaid] = $searcharea;
443 // This will store area - allowed contexts relations.
444 $areascontexts = array();
446 // Initialise two special-case arrays for storing other information related to the contexts.
447 $separategroupscontexts = array();
448 $visiblegroupscontextsareas = array();
449 $usergroups = array();
451 if (empty($limitcourseids) && !empty($areasbylevel[CONTEXT_SYSTEM])) {
452 // We add system context to all search areas working at this level. Here each area is fully responsible of
453 // the access control as we can not automate much, we can not even check guest access as some areas might
454 // want to allow guests to retrieve data from them.
456 $systemcontextid = \context_system::instance()->id;
457 if (!$limitcontextids || in_array($systemcontextid, $limitcontextids)) {
458 foreach ($areasbylevel[CONTEXT_SYSTEM] as $areaid => $searchclass) {
459 $areascontexts[$areaid][$systemcontextid] = $systemcontextid;
464 if (!empty($areasbylevel[CONTEXT_USER])) {
465 if ($usercontext = \context_user::instance($USER->id, IGNORE_MISSING)) {
466 if (!$limitcontextids || in_array($usercontext->id, $limitcontextids)) {
467 // Extra checking although only logged users should reach this point, guest users have a valid context id.
468 foreach ($areasbylevel[CONTEXT_USER] as $areaid => $searchclass) {
469 $areascontexts[$areaid][$usercontext->id] = $usercontext->id;
475 if (is_siteadmin()) {
476 // Admins have access to all courses regardless of enrolment.
477 if ($limitcourseids) {
478 list ($coursesql, $courseparams) = $DB->get_in_or_equal($limitcourseids);
479 $coursesql = 'id ' . $coursesql;
480 } else {
481 $coursesql = '';
482 $courseparams = [];
484 // Get courses using the same list of fields from enrol_get_my_courses.
485 $courses = $DB->get_records_select('course', $coursesql, $courseparams, '',
486 'id, category, sortorder, shortname, fullname, idnumber, startdate, visible, ' .
487 'groupmode, groupmodeforce, cacherev');
488 } else {
489 // Get the courses where the current user has access.
490 $courses = enrol_get_my_courses(array('id', 'cacherev'), 'id', 0, [],
491 (bool)get_config('core', 'searchallavailablecourses'));
494 if (empty($limitcourseids) || in_array(SITEID, $limitcourseids)) {
495 $courses[SITEID] = get_course(SITEID);
498 // Keep a list of included course context ids (needed for the block calculation below).
499 $coursecontextids = [];
500 $modulecms = [];
502 foreach ($courses as $course) {
503 if (!empty($limitcourseids) && !in_array($course->id, $limitcourseids)) {
504 // Skip non-included courses.
505 continue;
508 $coursecontext = \context_course::instance($course->id);
509 $coursecontextids[] = $coursecontext->id;
510 $hasgrouprestrictions = false;
512 // Info about the course modules.
513 $modinfo = get_fast_modinfo($course);
515 if (!empty($areasbylevel[CONTEXT_COURSE]) &&
516 (!$limitcontextids || in_array($coursecontext->id, $limitcontextids))) {
517 // Add the course contexts the user can view.
518 foreach ($areasbylevel[CONTEXT_COURSE] as $areaid => $searchclass) {
519 if ($course->visible || has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
520 $areascontexts[$areaid][$coursecontext->id] = $coursecontext->id;
525 if (!empty($areasbylevel[CONTEXT_MODULE])) {
526 // Add the module contexts the user can view (cm_info->uservisible).
528 foreach ($areasbylevel[CONTEXT_MODULE] as $areaid => $searchclass) {
530 // Removing the plugintype 'mod_' prefix.
531 $modulename = substr($searchclass->get_component_name(), 4);
533 $modinstances = $modinfo->get_instances_of($modulename);
534 foreach ($modinstances as $modinstance) {
535 // Skip module context if not included in list of context ids.
536 if ($limitcontextids && !in_array($modinstance->context->id, $limitcontextids)) {
537 continue;
539 if ($modinstance->uservisible) {
540 $contextid = $modinstance->context->id;
541 $areascontexts[$areaid][$contextid] = $contextid;
542 $modulecms[$modinstance->id] = $modinstance;
544 if (!has_capability('moodle/site:accessallgroups', $modinstance->context) &&
545 ($searchclass instanceof base_mod) &&
546 $searchclass->supports_group_restriction()) {
547 if ($searchclass->restrict_cm_access_by_group($modinstance)) {
548 $separategroupscontexts[$contextid] = $contextid;
549 $hasgrouprestrictions = true;
550 } else {
551 // Track a list of anything that has a group id (so might get
552 // filtered) and doesn't want to be, in this context.
553 if (!array_key_exists($contextid, $visiblegroupscontextsareas)) {
554 $visiblegroupscontextsareas[$contextid] = array();
556 $visiblegroupscontextsareas[$contextid][$areaid] = $areaid;
564 // Insert group information for course (unless there aren't any modules restricted by
565 // group for this user in this course, in which case don't bother).
566 if ($hasgrouprestrictions) {
567 $groups = groups_get_all_groups($course->id, $USER->id, 0, 'g.id');
568 foreach ($groups as $group) {
569 $usergroups[$group->id] = $group->id;
574 // Chuck away all the 'visible groups contexts' data unless there is actually something
575 // that does use separate groups in the same context (this data is only used as an
576 // 'override' in cases where the search is restricting to separate groups).
577 foreach ($visiblegroupscontextsareas as $contextid => $areas) {
578 if (!array_key_exists($contextid, $separategroupscontexts)) {
579 unset($visiblegroupscontextsareas[$contextid]);
583 // Add all supported block contexts, in a single query for performance.
584 if (!empty($areasbylevel[CONTEXT_BLOCK])) {
585 // Get list of all block types we care about.
586 $blocklist = [];
587 foreach ($areasbylevel[CONTEXT_BLOCK] as $areaid => $searchclass) {
588 $blocklist[$searchclass->get_block_name()] = true;
590 list ($blocknamesql, $blocknameparams) = $DB->get_in_or_equal(array_keys($blocklist));
592 // Get list of course contexts.
593 list ($contextsql, $contextparams) = $DB->get_in_or_equal($coursecontextids);
595 // Get list of block context (if limited).
596 $blockcontextwhere = '';
597 $blockcontextparams = [];
598 if ($limitcontextids) {
599 list ($blockcontextsql, $blockcontextparams) = $DB->get_in_or_equal($limitcontextids);
600 $blockcontextwhere = 'AND x.id ' . $blockcontextsql;
603 // Query all blocks that are within an included course, and are set to be visible, and
604 // in a supported page type (basically just course view). This query could be
605 // extended (or a second query added) to support blocks that are within a module
606 // context as well, and we could add more page types if required.
607 $blockrecs = $DB->get_records_sql("
608 SELECT x.*, bi.blockname AS blockname, bi.id AS blockinstanceid
609 FROM {block_instances} bi
610 JOIN {context} x ON x.instanceid = bi.id AND x.contextlevel = ?
611 LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id
612 AND bp.contextid = bi.parentcontextid
613 AND bp.pagetype LIKE 'course-view-%'
614 AND bp.subpage = ''
615 AND bp.visible = 0
616 WHERE bi.parentcontextid $contextsql
617 $blockcontextwhere
618 AND bi.blockname $blocknamesql
619 AND bi.subpagepattern IS NULL
620 AND (bi.pagetypepattern = 'site-index'
621 OR bi.pagetypepattern LIKE 'course-view-%'
622 OR bi.pagetypepattern = 'course-*'
623 OR bi.pagetypepattern = '*')
624 AND bp.id IS NULL",
625 array_merge([CONTEXT_BLOCK], $contextparams, $blockcontextparams, $blocknameparams));
626 $blockcontextsbyname = [];
627 foreach ($blockrecs as $blockrec) {
628 if (empty($blockcontextsbyname[$blockrec->blockname])) {
629 $blockcontextsbyname[$blockrec->blockname] = [];
631 \context_helper::preload_from_record($blockrec);
632 $blockcontextsbyname[$blockrec->blockname][] = \context_block::instance(
633 $blockrec->blockinstanceid);
636 // Add the block contexts the user can view.
637 foreach ($areasbylevel[CONTEXT_BLOCK] as $areaid => $searchclass) {
638 if (empty($blockcontextsbyname[$searchclass->get_block_name()])) {
639 continue;
641 foreach ($blockcontextsbyname[$searchclass->get_block_name()] as $context) {
642 if (has_capability('moodle/block:view', $context)) {
643 $areascontexts[$areaid][$context->id] = $context->id;
649 // Return all the data.
650 return (object)array('everything' => false, 'usercontexts' => $areascontexts,
651 'separategroupscontexts' => $separategroupscontexts, 'usergroups' => $usergroups,
652 'visiblegroupscontextsareas' => $visiblegroupscontextsareas);
656 * Returns requested page of documents plus additional information for paging.
658 * This function does not perform any kind of security checking for access, the caller code
659 * should check that the current user have moodle/search:query capability.
661 * If a page is requested that is beyond the last result, the last valid page is returned in
662 * results, and actualpage indicates which page was returned.
664 * @param stdClass $formdata
665 * @param int $pagenum The 0 based page number.
666 * @return object An object with 3 properties:
667 * results => An array of \core_search\documents for the actual page.
668 * totalcount => Number of records that are possibly available, to base paging on.
669 * actualpage => The actual page returned.
671 public function paged_search(\stdClass $formdata, $pagenum) {
672 $out = new \stdClass();
674 $perpage = static::DISPLAY_RESULTS_PER_PAGE;
676 // Make sure we only allow request up to max page.
677 $pagenum = min($pagenum, (static::MAX_RESULTS / $perpage) - 1);
679 // Calculate the first and last document number for the current page, 1 based.
680 $mindoc = ($pagenum * $perpage) + 1;
681 $maxdoc = ($pagenum + 1) * $perpage;
683 // Get engine documents, up to max.
684 $docs = $this->search($formdata, $maxdoc);
686 $resultcount = count($docs);
687 if ($resultcount < $maxdoc) {
688 // This means it couldn't give us results to max, so the count must be the max.
689 $out->totalcount = $resultcount;
690 } else {
691 // Get the possible count reported by engine, and limit to our max.
692 $out->totalcount = $this->engine->get_query_total_count();
693 $out->totalcount = min($out->totalcount, static::MAX_RESULTS);
696 // Determine the actual page.
697 if ($resultcount < $mindoc) {
698 // We couldn't get the min docs for this page, so determine what page we can get.
699 $out->actualpage = floor(($resultcount - 1) / $perpage);
700 } else {
701 $out->actualpage = $pagenum;
704 // Split the results to only return the page.
705 $out->results = array_slice($docs, $out->actualpage * $perpage, $perpage, true);
707 return $out;
711 * Returns documents from the engine based on the data provided.
713 * This function does not perform any kind of security checking, the caller code
714 * should check that the current user have moodle/search:query capability.
716 * It might return the results from the cache instead.
718 * Valid formdata options include:
719 * - q (query text)
720 * - courseids (optional list of course ids to restrict)
721 * - contextids (optional list of context ids to restrict)
722 * - context (Moodle context object for location user searched from)
723 * - order (optional ordering, one of the types supported by the search engine e.g. 'relevance')
724 * - userids (optional list of user ids to restrict)
726 * @param \stdClass $formdata Query input data (usually from search form)
727 * @param int $limit The maximum number of documents to return
728 * @return \core_search\document[]
730 public function search(\stdClass $formdata, $limit = 0) {
731 // For Behat testing, the search results can be faked using a special step.
732 if (defined('BEHAT_SITE_RUNNING')) {
733 $fakeresult = get_config('core_search', 'behat_fakeresult');
734 if ($fakeresult) {
735 // Clear config setting.
736 unset_config('core_search', 'behat_fakeresult');
738 // Check query matches expected value.
739 $details = json_decode($fakeresult);
740 if ($formdata->q !== $details->query) {
741 throw new \coding_exception('Unexpected search query: ' . $formdata->q);
744 // Create search documents from the JSON data.
745 $docs = [];
746 foreach ($details->results as $result) {
747 $doc = new \core_search\document($result->itemid, $result->componentname,
748 $result->areaname);
749 foreach ((array)$result->fields as $field => $value) {
750 $doc->set($field, $value);
752 foreach ((array)$result->extrafields as $field => $value) {
753 $doc->set_extra($field, $value);
755 $area = $this->get_search_area($doc->get('areaid'));
756 $doc->set_doc_url($area->get_doc_url($doc));
757 $doc->set_context_url($area->get_context_url($doc));
758 $docs[] = $doc;
761 return $docs;
765 $limitcourseids = false;
766 if (!empty($formdata->courseids)) {
767 $limitcourseids = $formdata->courseids;
770 $limitcontextids = false;
771 if (!empty($formdata->contextids)) {
772 $limitcontextids = $formdata->contextids;
775 // Clears previous query errors.
776 $this->engine->clear_query_error();
778 $contextinfo = $this->get_areas_user_accesses($limitcourseids, $limitcontextids);
779 if (!$contextinfo->everything && !$contextinfo->usercontexts) {
780 // User can not access any context.
781 $docs = array();
782 } else {
783 // If engine does not support groups, remove group information from the context info -
784 // use the old format instead (true = admin, array = user contexts).
785 if (!$this->engine->supports_group_filtering()) {
786 $contextinfo = $contextinfo->everything ? true : $contextinfo->usercontexts;
789 // Execute the actual query.
790 $docs = $this->engine->execute_query($formdata, $contextinfo, $limit);
793 return $docs;
797 * Merge separate index segments into one.
799 public function optimize_index() {
800 $this->engine->optimize();
804 * Index all documents.
806 * @param bool $fullindex Whether we should reindex everything or not.
807 * @param float $timelimit Time limit in seconds (0 = no time limit)
808 * @param \progress_trace|null $progress Optional class for tracking progress
809 * @throws \moodle_exception
810 * @return bool Whether there was any updated document or not.
812 public function index($fullindex = false, $timelimit = 0, \progress_trace $progress = null) {
813 global $DB;
815 // Cannot combine time limit with reindex.
816 if ($timelimit && $fullindex) {
817 throw new \coding_exception('Cannot apply time limit when reindexing');
819 if (!$progress) {
820 $progress = new \null_progress_trace();
823 // Unlimited time.
824 \core_php_time_limit::raise();
826 // Notify the engine that an index starting.
827 $this->engine->index_starting($fullindex);
829 $sumdocs = 0;
831 $searchareas = $this->get_search_areas_list(true);
833 if ($timelimit) {
834 // If time is limited (and therefore we're not just indexing everything anyway), select
835 // an order for search areas. The intention here is to avoid a situation where a new
836 // large search area is enabled, and this means all our other search areas go out of
837 // date while that one is being indexed. To do this, we order by the time we spent
838 // indexing them last time we ran, meaning anything that took a very long time will be
839 // done last.
840 uasort($searchareas, function(\core_search\base $area1, \core_search\base $area2) {
841 return (int)$area1->get_last_indexing_duration() - (int)$area2->get_last_indexing_duration();
844 // Decide time to stop.
845 $stopat = self::get_current_time() + $timelimit;
848 foreach ($searchareas as $areaid => $searcharea) {
850 $progress->output('Processing area: ' . $searcharea->get_visible_name());
852 // Notify the engine that an area is starting.
853 $this->engine->area_index_starting($searcharea, $fullindex);
855 $indexingstart = (int)self::get_current_time();
856 $elapsed = self::get_current_time();
858 // This is used to store this component config.
859 list($componentconfigname, $varname) = $searcharea->get_config_var_name();
861 $prevtimestart = intval(get_config($componentconfigname, $varname . '_indexingstart'));
863 if ($fullindex === true) {
864 $referencestarttime = 0;
866 // For full index, we delete any queued context index requests, as those will
867 // obviously be met by the full index.
868 $DB->delete_records('search_index_requests');
869 } else {
870 $partial = get_config($componentconfigname, $varname . '_partial');
871 if ($partial) {
872 // When the previous index did not complete all data, we start from the time of the
873 // last document that was successfully indexed. (Note this will result in
874 // re-indexing that one document, but we can't avoid that because there may be
875 // other documents in the same second.)
876 $referencestarttime = intval(get_config($componentconfigname, $varname . '_lastindexrun'));
877 } else {
878 $referencestarttime = $prevtimestart;
882 // Getting the recordset from the area.
883 $recordset = $searcharea->get_recordset_by_timestamp($referencestarttime);
884 $initialquerytime = self::get_current_time() - $elapsed;
885 if ($initialquerytime > self::DISPLAY_LONG_QUERY_TIME) {
886 $progress->output('Initial query took ' . round($initialquerytime, 1) .
887 ' seconds.', 1);
890 // Pass get_document as callback.
891 $fileindexing = $this->engine->file_indexing_enabled() && $searcharea->uses_file_indexing();
892 $options = array('indexfiles' => $fileindexing, 'lastindexedtime' => $prevtimestart);
893 if ($timelimit) {
894 $options['stopat'] = $stopat;
896 $options['progress'] = $progress;
897 $iterator = new skip_future_documents_iterator(new \core\dml\recordset_walk(
898 $recordset, array($searcharea, 'get_document'), $options));
899 $result = $this->engine->add_documents($iterator, $searcharea, $options);
900 $recordset->close();
901 if (count($result) === 5) {
902 list($numrecords, $numdocs, $numdocsignored, $lastindexeddoc, $partial) = $result;
903 } else {
904 // Backward compatibility for engines that don't support partial adding.
905 list($numrecords, $numdocs, $numdocsignored, $lastindexeddoc) = $result;
906 debugging('engine::add_documents() should return $partial (4-value return is deprecated)',
907 DEBUG_DEVELOPER);
908 $partial = false;
911 if ($numdocs > 0) {
912 $elapsed = round((self::get_current_time() - $elapsed), 1);
914 $partialtext = '';
915 if ($partial) {
916 $partialtext = ' (not complete; done to ' . userdate($lastindexeddoc,
917 get_string('strftimedatetimeshort', 'langconfig')) . ')';
920 $progress->output('Processed ' . $numrecords . ' records containing ' . $numdocs .
921 ' documents, in ' . $elapsed . ' seconds' . $partialtext . '.', 1);
922 } else {
923 $progress->output('No new documents to index.', 1);
926 // Notify the engine this area is complete, and only mark times if true.
927 if ($this->engine->area_index_complete($searcharea, $numdocs, $fullindex)) {
928 $sumdocs += $numdocs;
930 // Store last index run once documents have been committed to the search engine.
931 set_config($varname . '_indexingstart', $indexingstart, $componentconfigname);
932 set_config($varname . '_indexingend', (int)self::get_current_time(), $componentconfigname);
933 set_config($varname . '_docsignored', $numdocsignored, $componentconfigname);
934 set_config($varname . '_docsprocessed', $numdocs, $componentconfigname);
935 set_config($varname . '_recordsprocessed', $numrecords, $componentconfigname);
936 if ($lastindexeddoc > 0) {
937 set_config($varname . '_lastindexrun', $lastindexeddoc, $componentconfigname);
939 if ($partial) {
940 set_config($varname . '_partial', 1, $componentconfigname);
941 } else {
942 unset_config($varname . '_partial', $componentconfigname);
944 } else {
945 $progress->output('Engine reported error.');
948 if ($timelimit && (self::get_current_time() >= $stopat)) {
949 $progress->output('Stopping indexing due to time limit.');
950 break;
954 if ($sumdocs > 0) {
955 $event = \core\event\search_indexed::create(
956 array('context' => \context_system::instance()));
957 $event->trigger();
960 $this->engine->index_complete($sumdocs, $fullindex);
962 return (bool)$sumdocs;
966 * Indexes or reindexes a specific context of the system, e.g. one course.
968 * The function returns an object with field 'complete' (true or false).
970 * This function supports partial indexing via the time limit parameter. If the time limit
971 * expires, it will return values for $startfromarea and $startfromtime which can be passed
972 * next time to continue indexing.
974 * @param \context $context Context to restrict index.
975 * @param string $singleareaid If specified, indexes only the given area.
976 * @param float $timelimit Time limit in seconds (0 = no time limit)
977 * @param \progress_trace|null $progress Optional class for tracking progress
978 * @param string $startfromarea Area to start from
979 * @param int $startfromtime Timestamp to start from
980 * @return \stdClass Object indicating success
982 public function index_context($context, $singleareaid = '', $timelimit = 0,
983 \progress_trace $progress = null, $startfromarea = '', $startfromtime = 0) {
984 if (!$progress) {
985 $progress = new \null_progress_trace();
988 // Work out time to stop, if limited.
989 if ($timelimit) {
990 // Decide time to stop.
991 $stopat = self::get_current_time() + $timelimit;
994 // No PHP time limit.
995 \core_php_time_limit::raise();
997 // Notify the engine that an index starting.
998 $this->engine->index_starting(false);
1000 $sumdocs = 0;
1002 // Get all search areas, in consistent order.
1003 $searchareas = $this->get_search_areas_list(true);
1004 ksort($searchareas);
1006 // Are we skipping past some that were handled previously?
1007 $skipping = $startfromarea ? true : false;
1009 foreach ($searchareas as $areaid => $searcharea) {
1010 // If we're only processing one area id, skip all the others.
1011 if ($singleareaid && $singleareaid !== $areaid) {
1012 continue;
1015 // If we're skipping to a later area, continue through the loop.
1016 $referencestarttime = 0;
1017 if ($skipping) {
1018 if ($areaid !== $startfromarea) {
1019 continue;
1021 // Stop skipping and note the reference start time.
1022 $skipping = false;
1023 $referencestarttime = $startfromtime;
1026 $progress->output('Processing area: ' . $searcharea->get_visible_name());
1028 $elapsed = self::get_current_time();
1030 // Get the recordset of all documents from the area for this context.
1031 $recordset = $searcharea->get_document_recordset($referencestarttime, $context);
1032 if (!$recordset) {
1033 if ($recordset === null) {
1034 $progress->output('Skipping (not relevant to context).', 1);
1035 } else {
1036 $progress->output('Skipping (does not support context indexing).', 1);
1038 continue;
1041 // Notify the engine that an area is starting.
1042 $this->engine->area_index_starting($searcharea, false);
1044 // Work out search options.
1045 $options = [];
1046 $options['indexfiles'] = $this->engine->file_indexing_enabled() &&
1047 $searcharea->uses_file_indexing();
1048 if ($timelimit) {
1049 $options['stopat'] = $stopat;
1052 // Construct iterator which will use get_document on the recordset results.
1053 $iterator = new \core\dml\recordset_walk($recordset,
1054 array($searcharea, 'get_document'), $options);
1056 // Use this iterator to add documents.
1057 $result = $this->engine->add_documents($iterator, $searcharea, $options);
1058 if (count($result) === 5) {
1059 list($numrecords, $numdocs, $numdocsignored, $lastindexeddoc, $partial) = $result;
1060 } else {
1061 // Backward compatibility for engines that don't support partial adding.
1062 list($numrecords, $numdocs, $numdocsignored, $lastindexeddoc) = $result;
1063 debugging('engine::add_documents() should return $partial (4-value return is deprecated)',
1064 DEBUG_DEVELOPER);
1065 $partial = false;
1068 if ($numdocs > 0) {
1069 $elapsed = round((self::get_current_time() - $elapsed), 3);
1070 $progress->output('Processed ' . $numrecords . ' records containing ' . $numdocs .
1071 ' documents, in ' . $elapsed . ' seconds' .
1072 ($partial ? ' (not complete)' : '') . '.', 1);
1073 } else {
1074 $progress->output('No documents to index.', 1);
1077 // Notify the engine this area is complete, but don't store any times as this is not
1078 // part of the 'normal' search index.
1079 if (!$this->engine->area_index_complete($searcharea, $numdocs, false)) {
1080 $progress->output('Engine reported error.', 1);
1083 if ($partial && $timelimit && (self::get_current_time() >= $stopat)) {
1084 $progress->output('Stopping indexing due to time limit.');
1085 break;
1089 if ($sumdocs > 0) {
1090 $event = \core\event\search_indexed::create(
1091 array('context' => $context));
1092 $event->trigger();
1095 $this->engine->index_complete($sumdocs, false);
1097 // Indicate in result whether we completed indexing, or only part of it.
1098 $result = new \stdClass();
1099 if ($partial) {
1100 $result->complete = false;
1101 $result->startfromarea = $areaid;
1102 $result->startfromtime = $lastindexeddoc;
1103 } else {
1104 $result->complete = true;
1106 return $result;
1110 * Resets areas config.
1112 * @throws \moodle_exception
1113 * @param string $areaid
1114 * @return void
1116 public function reset_config($areaid = false) {
1118 if (!empty($areaid)) {
1119 $searchareas = array();
1120 if (!$searchareas[$areaid] = static::get_search_area($areaid)) {
1121 throw new \moodle_exception('errorareanotavailable', 'search', '', $areaid);
1123 } else {
1124 // Only the enabled ones.
1125 $searchareas = static::get_search_areas_list(true);
1128 foreach ($searchareas as $searcharea) {
1129 list($componentname, $varname) = $searcharea->get_config_var_name();
1130 $config = $searcharea->get_config();
1132 foreach ($config as $key => $value) {
1133 // We reset them all but the enable/disabled one.
1134 if ($key !== $varname . '_enabled') {
1135 set_config($key, 0, $componentname);
1142 * Deletes an area's documents or all areas documents.
1144 * @param string $areaid The area id or false for all
1145 * @return void
1147 public function delete_index($areaid = false) {
1148 if (!empty($areaid)) {
1149 $this->engine->delete($areaid);
1150 $this->reset_config($areaid);
1151 } else {
1152 $this->engine->delete();
1153 $this->reset_config();
1158 * Deletes index by id.
1160 * @param int Solr Document string $id
1162 public function delete_index_by_id($id) {
1163 $this->engine->delete_by_id($id);
1167 * Returns search areas configuration.
1169 * @param \core_search\base[] $searchareas
1170 * @return \stdClass[] $configsettings
1172 public function get_areas_config($searchareas) {
1174 $vars = array('indexingstart', 'indexingend', 'lastindexrun', 'docsignored',
1175 'docsprocessed', 'recordsprocessed', 'partial');
1177 $configsettings = [];
1178 foreach ($searchareas as $searcharea) {
1180 $areaid = $searcharea->get_area_id();
1182 $configsettings[$areaid] = new \stdClass();
1183 list($componentname, $varname) = $searcharea->get_config_var_name();
1185 if (!$searcharea->is_enabled()) {
1186 // We delete all indexed data on disable so no info.
1187 foreach ($vars as $var) {
1188 $configsettings[$areaid]->{$var} = 0;
1190 } else {
1191 foreach ($vars as $var) {
1192 $configsettings[$areaid]->{$var} = get_config($componentname, $varname .'_' . $var);
1196 // Formatting the time.
1197 if (!empty($configsettings[$areaid]->lastindexrun)) {
1198 $configsettings[$areaid]->lastindexrun = userdate($configsettings[$areaid]->lastindexrun);
1199 } else {
1200 $configsettings[$areaid]->lastindexrun = get_string('never');
1203 return $configsettings;
1207 * Triggers search_results_viewed event
1209 * Other data required:
1210 * - q: The query string
1211 * - page: The page number
1212 * - title: Title filter
1213 * - areaids: Search areas filter
1214 * - courseids: Courses filter
1215 * - timestart: Time start filter
1216 * - timeend: Time end filter
1218 * @since Moodle 3.2
1219 * @param array $other Other info for the event.
1220 * @return \core\event\search_results_viewed
1222 public static function trigger_search_results_viewed($other) {
1223 $event = \core\event\search_results_viewed::create([
1224 'context' => \context_system::instance(),
1225 'other' => $other
1227 $event->trigger();
1229 return $event;
1233 * Checks whether a classname is of an actual search area.
1235 * @param string $classname
1236 * @return bool
1238 protected static function is_search_area($classname) {
1239 if (is_subclass_of($classname, 'core_search\base')) {
1240 return (new \ReflectionClass($classname))->isInstantiable();
1243 return false;
1247 * Requests that a specific context is indexed by the scheduled task. The context will be
1248 * added to a queue which is processed by the task.
1250 * This is used after a restore to ensure that restored items are indexed, even though their
1251 * modified time will be older than the latest indexed. It is also used by the 'Gradual reindex'
1252 * admin feature from the search areas screen.
1254 * @param \context $context Context to index within
1255 * @param string $areaid Area to index, '' = all areas
1256 * @param int $priority Priority (INDEX_PRIORITY_xx constant)
1258 public static function request_index(\context $context, $areaid = '',
1259 $priority = self::INDEX_PRIORITY_NORMAL) {
1260 global $DB;
1262 // Check through existing requests for this context or any parent context.
1263 list ($contextsql, $contextparams) = $DB->get_in_or_equal(
1264 $context->get_parent_context_ids(true));
1265 $existing = $DB->get_records_select('search_index_requests',
1266 'contextid ' . $contextsql, $contextparams, '',
1267 'id, searcharea, partialarea, indexpriority');
1268 foreach ($existing as $rec) {
1269 // If we haven't started processing the existing request yet, and it covers the same
1270 // area (or all areas) then that will be sufficient so don't add anything else.
1271 if ($rec->partialarea === '' && ($rec->searcharea === $areaid || $rec->searcharea === '')) {
1272 // If the existing request has the same (or higher) priority, no need to add anything.
1273 if ($rec->indexpriority >= $priority) {
1274 return;
1276 // The existing request has lower priority. If it is exactly the same, then just
1277 // adjust the priority of the existing request.
1278 if ($rec->searcharea === $areaid) {
1279 $DB->set_field('search_index_requests', 'indexpriority', $priority,
1280 ['id' => $rec->id]);
1281 return;
1283 // The existing request would cover this area but is a lower priority. We need to
1284 // add the new request even though that means we will index part of it twice.
1288 // No suitable existing request, so add a new one.
1289 $newrecord = [ 'contextid' => $context->id, 'searcharea' => $areaid,
1290 'timerequested' => (int)self::get_current_time(),
1291 'partialarea' => '', 'partialtime' => 0,
1292 'indexpriority' => $priority ];
1293 $DB->insert_record('search_index_requests', $newrecord);
1297 * Processes outstanding index requests. This will take the first item from the queue (taking
1298 * account the indexing priority) and process it, continuing until an optional time limit is
1299 * reached.
1301 * If there are no index requests, the function will do nothing.
1303 * @param float $timelimit Time limit (0 = none)
1304 * @param \progress_trace|null $progress Optional progress indicator
1306 public function process_index_requests($timelimit = 0.0, \progress_trace $progress = null) {
1307 global $DB;
1309 if (!$progress) {
1310 $progress = new \null_progress_trace();
1313 $before = self::get_current_time();
1314 if ($timelimit) {
1315 $stopat = $before + $timelimit;
1317 while (true) {
1318 // Retrieve first request, using fully defined ordering.
1319 $requests = $DB->get_records('search_index_requests', null,
1320 'indexpriority DESC, timerequested, contextid, searcharea',
1321 'id, contextid, searcharea, partialarea, partialtime', 0, 1);
1322 if (!$requests) {
1323 // If there are no more requests, stop.
1324 break;
1326 $request = reset($requests);
1328 // Calculate remaining time.
1329 $remainingtime = 0;
1330 $beforeindex = self::get_current_time();
1331 if ($timelimit) {
1332 $remainingtime = $stopat - $beforeindex;
1334 // If the time limit expired already, stop now. (Otherwise we might accidentally
1335 // index with no time limit or a negative time limit.)
1336 if ($remainingtime <= 0) {
1337 break;
1341 // Show a message before each request, indicating what will be indexed.
1342 $context = \context::instance_by_id($request->contextid, IGNORE_MISSING);
1343 if (!$context) {
1344 $DB->delete_records('search_index_requests', ['id' => $request->id]);
1345 $progress->output('Skipped deleted context: ' . $request->contextid);
1346 continue;
1348 $contextname = $context->get_context_name();
1349 if ($request->searcharea) {
1350 $contextname .= ' (search area: ' . $request->searcharea . ')';
1352 $progress->output('Indexing requested context: ' . $contextname);
1354 // Actually index the context.
1355 $result = $this->index_context($context, $request->searcharea, $remainingtime,
1356 $progress, $request->partialarea, $request->partialtime);
1358 // Work out shared part of message.
1359 $endmessage = $contextname . ' (' . round(self::get_current_time() - $beforeindex, 1) . 's)';
1361 // Update database table and continue/stop as appropriate.
1362 if ($result->complete) {
1363 // If we completed the request, remove it from the table.
1364 $DB->delete_records('search_index_requests', ['id' => $request->id]);
1365 $progress->output('Completed requested context: ' . $endmessage);
1366 } else {
1367 // If we didn't complete the request, store the partial details (how far it got).
1368 $DB->update_record('search_index_requests', ['id' => $request->id,
1369 'partialarea' => $result->startfromarea,
1370 'partialtime' => $result->startfromtime]);
1371 $progress->output('Ending requested context: ' . $endmessage);
1373 // The time limit must have expired, so stop looping.
1374 break;
1380 * Gets information about the request queue, in the form of a plain object suitable for passing
1381 * to a template for rendering.
1383 * @return \stdClass Information about queued index requests
1385 public function get_index_requests_info() {
1386 global $DB;
1388 $result = new \stdClass();
1390 $result->total = $DB->count_records('search_index_requests');
1391 $result->topten = $DB->get_records('search_index_requests', null,
1392 'indexpriority DESC, timerequested, contextid, searcharea',
1393 'id, contextid, timerequested, searcharea, partialarea, partialtime, indexpriority',
1394 0, 10);
1395 foreach ($result->topten as $item) {
1396 $context = \context::instance_by_id($item->contextid);
1397 $item->contextlink = \html_writer::link($context->get_url(),
1398 s($context->get_context_name()));
1399 if ($item->searcharea) {
1400 $item->areaname = $this->get_search_area($item->searcharea)->get_visible_name();
1402 if ($item->partialarea) {
1403 $item->partialareaname = $this->get_search_area($item->partialarea)->get_visible_name();
1405 switch ($item->indexpriority) {
1406 case self::INDEX_PRIORITY_REINDEXING :
1407 $item->priorityname = get_string('priority_reindexing', 'search');
1408 break;
1409 case self::INDEX_PRIORITY_NORMAL :
1410 $item->priorityname = get_string('priority_normal', 'search');
1411 break;
1415 // Normalise array indices.
1416 $result->topten = array_values($result->topten);
1418 if ($result->total > 10) {
1419 $result->ellipsis = true;
1422 return $result;
1426 * Gets current time for use in search system.
1428 * Note: This should be replaced with generic core functionality once possible (see MDL-60644).
1430 * @return float Current time in seconds (with decimals)
1432 public static function get_current_time() {
1433 if (PHPUNIT_TEST && self::$phpunitfaketime) {
1434 return self::$phpunitfaketime;
1436 return microtime(true);