Merge branch 'MDL-62899-search-icons-master' of https://github.com/dmitriim/moodle
[moodle.git] / search / classes / manager.php
CommitLineData
db48207e
DM
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.
13//
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/>.
16
17/**
18 * Search subsystem manager.
19 *
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
23 */
24
25namespace core_search;
26
27defined('MOODLE_INTERNAL') || die;
28
29require_once($CFG->dirroot . '/lib/accesslib.php');
30
31/**
32 * Search subsystem manager.
33 *
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
37 */
38class manager {
39
40 /**
41 * @var int Text contents.
42 */
43 const TYPE_TEXT = 1;
44
091973db
EM
45 /**
46 * @var int File contents.
47 */
48 const TYPE_FILE = 2;
49
db48207e
DM
50 /**
51 * @var int User can not access the document.
52 */
53 const ACCESS_DENIED = 0;
54
55 /**
56 * @var int User can access the document.
57 */
58 const ACCESS_GRANTED = 1;
59
60 /**
61 * @var int The document was deleted.
62 */
63 const ACCESS_DELETED = 2;
64
65 /**
66 * @var int Maximum number of results that will be retrieved from the search engine.
67 */
68 const MAX_RESULTS = 100;
69
70 /**
71 * @var int Number of results per page.
72 */
73 const DISPLAY_RESULTS_PER_PAGE = 10;
74
f6b425e2
EM
75 /**
76 * @var int The id to be placed in owneruserid when there is no owner.
77 */
78 const NO_OWNER_ID = 0;
79
1b8cf12a 80 /**
81 * @var float If initial query takes longer than N seconds, this will be shown in cron log.
82 */
83 const DISPLAY_LONG_QUERY_TIME = 5.0;
84
85 /**
86 * @var float Adds indexing progress within one search area to cron log every N seconds.
87 */
88 const DISPLAY_INDEXING_PROGRESS_EVERY = 30.0;
89
8736fbc1 90 /**
91 * @var int Context indexing: normal priority.
92 */
93 const INDEX_PRIORITY_NORMAL = 100;
94
95 /**
96 * @var int Context indexing: low priority for reindexing.
97 */
98 const INDEX_PRIORITY_REINDEXING = 50;
99
db48207e 100 /**
0bd8383a 101 * @var \core_search\base[] Enabled search areas.
db48207e
DM
102 */
103 protected static $enabledsearchareas = null;
104
105 /**
0bd8383a 106 * @var \core_search\base[] All system search areas.
db48207e
DM
107 */
108 protected static $allsearchareas = null;
109
110 /**
111 * @var \core_search\manager
112 */
113 protected static $instance = null;
114
115 /**
116 * @var \core_search\engine
117 */
118 protected $engine = null;
119
82735dec 120 /**
121 * Note: This should be removed once possible (see MDL-60644).
122 *
123 * @var float Fake current time for use in PHPunit tests
124 */
125 protected static $phpunitfaketime = 0;
126
db48207e
DM
127 /**
128 * Constructor, use \core_search\manager::instance instead to get a class instance.
129 *
0bd8383a 130 * @param \core_search\base The search engine to use
db48207e
DM
131 */
132 public function __construct($engine) {
133 $this->engine = $engine;
134 }
135
73fd5666 136 /**
137 * @var int Record time of each successful schema check, but not more than once per 10 minutes.
138 */
139 const SCHEMA_CHECK_TRACKING_DELAY = 10 * 60;
140
141 /**
142 * @var int Require a new schema check at least every 4 hours.
143 */
144 const SCHEMA_CHECK_REQUIRED_EVERY = 4 * 3600;
145
db48207e
DM
146 /**
147 * Returns an initialised \core_search instance.
148 *
73fd5666 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.
152 *
23fc1be8
DM
153 * @see \core_search\engine::is_installed
154 * @see \core_search\engine::is_server_ready
73fd5666 155 * @param bool $fast Set to true when calling on a page that requires high performance
db48207e
DM
156 * @throws \core_search\engine_exception
157 * @return \core_search\manager
158 */
73fd5666 159 public static function instance($fast = false) {
db48207e
DM
160 global $CFG;
161
162 // One per request, this should be purged during testing.
163 if (static::$instance !== null) {
164 return static::$instance;
165 }
166
379ca986
DM
167 if (empty($CFG->searchengine)) {
168 throw new \core_search\engine_exception('enginenotselected', 'search');
169 }
170
db48207e
DM
171 if (!$engine = static::search_engine_instance()) {
172 throw new \core_search\engine_exception('enginenotfound', 'search', '', $CFG->searchengine);
173 }
174
73fd5666 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');
178
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();
184 }
185
db48207e
DM
186 if (!$engine->is_installed()) {
187 throw new \core_search\engine_exception('enginenotinstalled', 'search', '', $CFG->searchengine);
188 }
189
190 $serverstatus = $engine->is_server_ready();
191 if ($serverstatus !== true) {
e36eefae 192 // Skip this error in Behat when faking seach results.
193 if (!defined('BEHAT_SITE_RUNNING') || !get_config('core_search', 'behat_fakeresult')) {
73fd5666 194 // Clear the record of successful schema checks since it might have failed.
195 unset_config('lastschemacheck', $engine->get_plugin_name());
e36eefae 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');
198 }
db48207e
DM
199 }
200
73fd5666 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());
205 }
206
db48207e
DM
207 static::$instance = new \core_search\manager($engine);
208 return static::$instance;
209 }
210
211 /**
212 * Returns whether global search is enabled or not.
213 *
214 * @return bool
215 */
216 public static function is_global_search_enabled() {
217 global $CFG;
218 return !empty($CFG->enableglobalsearch);
219 }
220
d761b3fb 221 /**
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).
224 *
225 * @return bool True if indexing is enabled.
226 */
227 public static function is_indexing_enabled() {
228 global $CFG;
229 return !empty($CFG->enableglobalsearch) || !empty($CFG->searchindexwhendisabled);
230 }
231
db48207e
DM
232 /**
233 * Returns an instance of the search engine.
234 *
235 * @return \core_search\engine
236 */
237 public static function search_engine_instance() {
238 global $CFG;
239
240 $classname = '\\search_' . $CFG->searchengine . '\\engine';
241 if (!class_exists($classname)) {
242 return false;
243 }
244
245 return new $classname();
246 }
247
248 /**
249 * Returns the search engine.
250 *
251 * @return \core_search\engine
252 */
253 public function get_engine() {
254 return $this->engine;
255 }
256
257 /**
258 * Returns a search area class name.
259 *
260 * @param string $areaid
261 * @return string
262 */
263 protected static function get_area_classname($areaid) {
264 list($componentname, $areaname) = static::extract_areaid_parts($areaid);
265 return '\\' . $componentname . '\\search\\' . $areaname;
266 }
267
268 /**
269 * Returns a new area search indexer instance.
270 *
271 * @param string $areaid
0bd8383a 272 * @return \core_search\base|bool False if the area is not available.
db48207e
DM
273 */
274 public static function get_search_area($areaid) {
275
b805d3f8 276 // We have them all here.
db48207e
DM
277 if (!empty(static::$allsearchareas[$areaid])) {
278 return static::$allsearchareas[$areaid];
279 }
db48207e
DM
280
281 $classname = static::get_area_classname($areaid);
f3d38863
DM
282
283 if (class_exists($classname) && static::is_search_area($classname)) {
db48207e
DM
284 return new $classname();
285 }
286
287 return false;
288 }
289
290 /**
291 * Return the list of available search areas.
292 *
293 * @param bool $enabled Return only the enabled ones.
0bd8383a 294 * @return \core_search\base[]
db48207e
DM
295 */
296 public static function get_search_areas_list($enabled = false) {
297
298 // Two different arrays, we don't expect these arrays to be big.
b805d3f8
DM
299 if (static::$allsearchareas !== null) {
300 if (!$enabled) {
301 return static::$allsearchareas;
302 } else {
303 return static::$enabledsearchareas;
304 }
db48207e
DM
305 }
306
b805d3f8
DM
307 static::$allsearchareas = array();
308 static::$enabledsearchareas = array();
db48207e
DM
309
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) {
314
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);
396d6f0a
DG
319
320 if (!static::is_search_area($classname)) {
321 continue;
322 }
323
db48207e
DM
324 $areaid = static::generate_areaid($componentname, $areaname);
325 $searchclass = new $classname();
b805d3f8
DM
326
327 static::$allsearchareas[$areaid] = $searchclass;
328 if ($searchclass->is_enabled()) {
329 static::$enabledsearchareas[$areaid] = $searchclass;
db48207e
DM
330 }
331 }
332 }
333 }
334
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');
339
340 foreach ($searchclasses as $classname => $classpath) {
341 $areaname = substr(strrchr($classname, '\\'), 1);
396d6f0a
DG
342
343 if (!static::is_search_area($classname)) {
344 continue;
345 }
346
db48207e
DM
347 $areaid = static::generate_areaid($componentname, $areaname);
348 $searchclass = new $classname();
b805d3f8
DM
349 static::$allsearchareas[$areaid] = $searchclass;
350 if ($searchclass->is_enabled()) {
351 static::$enabledsearchareas[$areaid] = $searchclass;
db48207e
DM
352 }
353 }
354 }
355
db48207e 356 if ($enabled) {
b805d3f8 357 return static::$enabledsearchareas;
db48207e 358 }
b805d3f8 359 return static::$allsearchareas;
db48207e
DM
360 }
361
362 /**
363 * Clears all static caches.
364 *
365 * @return void
366 */
367 public static function clear_static() {
368
369 static::$enabledsearchareas = null;
370 static::$allsearchareas = null;
371 static::$instance = null;
a96faa49 372
373 base_block::clear_static();
26b86f31 374 engine::clear_users_cache();
db48207e
DM
375 }
376
377 /**
378 * Generates an area id from the componentname and the area name.
379 *
380 * There should not be any naming conflict as the area name is the
381 * class name in component/classes/search/.
382 *
383 * @param string $componentname
384 * @param string $areaname
385 * @return void
386 */
387 public static function generate_areaid($componentname, $areaname) {
388 return $componentname . '-' . $areaname;
389 }
390
391 /**
392 * Returns all areaid string components (component name and area name).
393 *
394 * @param string $areaid
395 * @return array Component name (Frankenstyle) and area name (search area class name)
396 */
397 public static function extract_areaid_parts($areaid) {
398 return explode('-', $areaid);
399 }
400
401 /**
4359ef18 402 * Returns information about the areas which the user can access.
db48207e 403 *
4359ef18 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)
db48207e 411 *
cfa00fc5 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.
417 *
427e3cbc 418 * @param array|false $limitcourseids An array of course ids to limit the search to. False for no limiting.
cfa00fc5 419 * @param array|false $limitcontextids An array of context ids to limit the search to. False for no limiting.
4359ef18 420 * @return \stdClass Object as described above
db48207e 421 */
cfa00fc5 422 protected function get_areas_user_accesses($limitcourseids = false, $limitcontextids = false) {
a96faa49 423 global $DB, $USER;
db48207e 424
cfa00fc5 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) {
4359ef18 428 return (object)array('everything' => true);
db48207e
DM
429 }
430
431 $areasbylevel = array();
432
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;
440 }
441 }
442
443 // This will store area - allowed contexts relations.
444 $areascontexts = array();
445
4359ef18 446 // Initialise two special-case arrays for storing other information related to the contexts.
447 $separategroupscontexts = array();
448 $visiblegroupscontextsareas = array();
449 $usergroups = array();
450
427e3cbc 451 if (empty($limitcourseids) && !empty($areasbylevel[CONTEXT_SYSTEM])) {
db48207e
DM
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.
455
456 $systemcontextid = \context_system::instance()->id;
cfa00fc5 457 if (!$limitcontextids || in_array($systemcontextid, $limitcontextids)) {
458 foreach ($areasbylevel[CONTEXT_SYSTEM] as $areaid => $searchclass) {
459 $areascontexts[$areaid][$systemcontextid] = $systemcontextid;
460 }
25ba053f
DM
461 }
462 }
463
464 if (!empty($areasbylevel[CONTEXT_USER])) {
465 if ($usercontext = \context_user::instance($USER->id, IGNORE_MISSING)) {
cfa00fc5 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;
470 }
25ba053f 471 }
db48207e
DM
472 }
473 }
474
cfa00fc5 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 = [];
483 }
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'));
492 }
427e3cbc
EM
493
494 if (empty($limitcourseids) || in_array(SITEID, $limitcourseids)) {
495 $courses[SITEID] = get_course(SITEID);
496 }
497
a96faa49 498 // Keep a list of included course context ids (needed for the block calculation below).
499 $coursecontextids = [];
4359ef18 500 $modulecms = [];
a96faa49 501
db48207e 502 foreach ($courses as $course) {
427e3cbc
EM
503 if (!empty($limitcourseids) && !in_array($course->id, $limitcourseids)) {
504 // Skip non-included courses.
505 continue;
506 }
db48207e 507
a96faa49 508 $coursecontext = \context_course::instance($course->id);
509 $coursecontextids[] = $coursecontext->id;
4359ef18 510 $hasgrouprestrictions = false;
a96faa49 511
db48207e
DM
512 // Info about the course modules.
513 $modinfo = get_fast_modinfo($course);
514
cfa00fc5 515 if (!empty($areasbylevel[CONTEXT_COURSE]) &&
516 (!$limitcontextids || in_array($coursecontext->id, $limitcontextids))) {
db48207e 517 // Add the course contexts the user can view.
db48207e
DM
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;
521 }
522 }
523 }
524
525 if (!empty($areasbylevel[CONTEXT_MODULE])) {
526 // Add the module contexts the user can view (cm_info->uservisible).
527
528 foreach ($areasbylevel[CONTEXT_MODULE] as $areaid => $searchclass) {
529
530 // Removing the plugintype 'mod_' prefix.
531 $modulename = substr($searchclass->get_component_name(), 4);
532
533 $modinstances = $modinfo->get_instances_of($modulename);
534 foreach ($modinstances as $modinstance) {
cfa00fc5 535 // Skip module context if not included in list of context ids.
536 if ($limitcontextids && !in_array($modinstance->context->id, $limitcontextids)) {
537 continue;
538 }
db48207e 539 if ($modinstance->uservisible) {
4359ef18 540 $contextid = $modinstance->context->id;
541 $areascontexts[$areaid][$contextid] = $contextid;
542 $modulecms[$modinstance->id] = $modinstance;
543
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();
555 }
556 $visiblegroupscontextsareas[$contextid][$areaid] = $areaid;
557 }
558 }
db48207e
DM
559 }
560 }
561 }
562 }
4359ef18 563
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;
570 }
571 }
572 }
573
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]);
580 }
db48207e
DM
581 }
582
a96faa49 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;
589 }
590 list ($blocknamesql, $blocknameparams) = $DB->get_in_or_equal(array_keys($blocklist));
591
592 // Get list of course contexts.
593 list ($contextsql, $contextparams) = $DB->get_in_or_equal($coursecontextids);
594
cfa00fc5 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;
601 }
602
a96faa49 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
cfa00fc5 617 $blockcontextwhere
a96faa49 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",
cfa00fc5 625 array_merge([CONTEXT_BLOCK], $contextparams, $blockcontextparams, $blocknameparams));
a96faa49 626 $blockcontextsbyname = [];
627 foreach ($blockrecs as $blockrec) {
628 if (empty($blockcontextsbyname[$blockrec->blockname])) {
629 $blockcontextsbyname[$blockrec->blockname] = [];
630 }
631 \context_helper::preload_from_record($blockrec);
632 $blockcontextsbyname[$blockrec->blockname][] = \context_block::instance(
633 $blockrec->blockinstanceid);
634 }
635
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;
640 }
641 foreach ($blockcontextsbyname[$searchclass->get_block_name()] as $context) {
642 if (has_capability('moodle/block:view', $context)) {
643 $areascontexts[$areaid][$context->id] = $context->id;
644 }
645 }
646 }
647 }
648
4359ef18 649 // Return all the data.
650 return (object)array('everything' => false, 'usercontexts' => $areascontexts,
651 'separategroupscontexts' => $separategroupscontexts, 'usergroups' => $usergroups,
652 'visiblegroupscontextsareas' => $visiblegroupscontextsareas);
db48207e
DM
653 }
654
053118a1
EM
655 /**
656 * Returns requested page of documents plus additional information for paging.
657 *
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.
660 *
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.
663 *
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.
670 */
671 public function paged_search(\stdClass $formdata, $pagenum) {
672 $out = new \stdClass();
673
674 $perpage = static::DISPLAY_RESULTS_PER_PAGE;
675
676 // Make sure we only allow request up to max page.
677 $pagenum = min($pagenum, (static::MAX_RESULTS / $perpage) - 1);
678
679 // Calculate the first and last document number for the current page, 1 based.
680 $mindoc = ($pagenum * $perpage) + 1;
681 $maxdoc = ($pagenum + 1) * $perpage;
682
683 // Get engine documents, up to max.
684 $docs = $this->search($formdata, $maxdoc);
685
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);
694 }
695
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;
702 }
703
704 // Split the results to only return the page.
705 $out->results = array_slice($docs, $out->actualpage * $perpage, $perpage, true);
706
707 return $out;
708 }
709
db48207e
DM
710 /**
711 * Returns documents from the engine based on the data provided.
712 *
69d66020
DM
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.
715 *
db48207e
DM
716 * It might return the results from the cache instead.
717 *
cfa00fc5 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)
fc440796 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')
222a97ce 724 * - userids (optional list of user ids to restrict)
cfa00fc5 725 *
726 * @param \stdClass $formdata Query input data (usually from search form)
727 * @param int $limit The maximum number of documents to return
db48207e
DM
728 * @return \core_search\document[]
729 */
053118a1 730 public function search(\stdClass $formdata, $limit = 0) {
e36eefae 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');
737
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);
742 }
743
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);
751 }
752 foreach ((array)$result->extrafields as $field => $value) {
753 $doc->set_extra($field, $value);
754 }
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;
759 }
760
761 return $docs;
762 }
763 }
db48207e 764
427e3cbc
EM
765 $limitcourseids = false;
766 if (!empty($formdata->courseids)) {
767 $limitcourseids = $formdata->courseids;
768 }
769
cfa00fc5 770 $limitcontextids = false;
771 if (!empty($formdata->contextids)) {
772 $limitcontextids = $formdata->contextids;
773 }
774
db48207e
DM
775 // Clears previous query errors.
776 $this->engine->clear_query_error();
777
4359ef18 778 $contextinfo = $this->get_areas_user_accesses($limitcourseids, $limitcontextids);
779 if (!$contextinfo->everything && !$contextinfo->usercontexts) {
db48207e
DM
780 // User can not access any context.
781 $docs = array();
782 } else {
4359ef18 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;
787 }
788
789 // Execute the actual query.
790 $docs = $this->engine->execute_query($formdata, $contextinfo, $limit);
db48207e
DM
791 }
792
db48207e
DM
793 return $docs;
794 }
795
db48207e
DM
796 /**
797 * Merge separate index segments into one.
798 */
799 public function optimize_index() {
800 $this->engine->optimize();
801 }
802
803 /**
804 * Index all documents.
805 *
806 * @param bool $fullindex Whether we should reindex everything or not.
67d64795 807 * @param float $timelimit Time limit in seconds (0 = no time limit)
4ba11aa9 808 * @param \progress_trace|null $progress Optional class for tracking progress
db48207e
DM
809 * @throws \moodle_exception
810 * @return bool Whether there was any updated document or not.
811 */
67d64795 812 public function index($fullindex = false, $timelimit = 0, \progress_trace $progress = null) {
eacb2bd1 813 global $DB;
814
67d64795 815 // Cannot combine time limit with reindex.
816 if ($timelimit && $fullindex) {
817 throw new \coding_exception('Cannot apply time limit when reindexing');
818 }
819 if (!$progress) {
820 $progress = new \null_progress_trace();
821 }
db48207e
DM
822
823 // Unlimited time.
824 \core_php_time_limit::raise();
825
075fa912
EM
826 // Notify the engine that an index starting.
827 $this->engine->index_starting($fullindex);
828
bf2235bb 829 $sumdocs = 0;
db48207e
DM
830
831 $searchareas = $this->get_search_areas_list(true);
67d64795 832
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();
842 });
843
844 // Decide time to stop.
82735dec 845 $stopat = self::get_current_time() + $timelimit;
67d64795 846 }
847
db48207e
DM
848 foreach ($searchareas as $areaid => $searcharea) {
849
67d64795 850 $progress->output('Processing area: ' . $searcharea->get_visible_name());
db48207e 851
075fa912
EM
852 // Notify the engine that an area is starting.
853 $this->engine->area_index_starting($searcharea, $fullindex);
854
7f83e99e 855 $indexingstart = (int)self::get_current_time();
82735dec 856 $elapsed = self::get_current_time();
db48207e
DM
857
858 // This is used to store this component config.
859 list($componentconfigname, $varname) = $searcharea->get_config_var_name();
860
091973db
EM
861 $prevtimestart = intval(get_config($componentconfigname, $varname . '_indexingstart'));
862
db48207e 863 if ($fullindex === true) {
091973db 864 $referencestarttime = 0;
eacb2bd1 865
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');
db48207e 869 } else {
67d64795 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;
879 }
db48207e
DM
880 }
881
882 // Getting the recordset from the area.
091973db 883 $recordset = $searcharea->get_recordset_by_timestamp($referencestarttime);
1b8cf12a 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);
888 }
db48207e
DM
889
890 // Pass get_document as callback.
091973db
EM
891 $fileindexing = $this->engine->file_indexing_enabled() && $searcharea->uses_file_indexing();
892 $options = array('indexfiles' => $fileindexing, 'lastindexedtime' => $prevtimestart);
67d64795 893 if ($timelimit) {
894 $options['stopat'] = $stopat;
895 }
1b8cf12a 896 $options['progress'] = $progress;
2d2fcc1c 897 $iterator = new skip_future_documents_iterator(new \core\dml\recordset_walk(
898 $recordset, array($searcharea, 'get_document'), $options));
67d64795 899 $result = $this->engine->add_documents($iterator, $searcharea, $options);
2d2fcc1c 900 $recordset->close();
67d64795 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;
909 }
910
911 if ($numdocs > 0) {
1b8cf12a 912 $elapsed = round((self::get_current_time() - $elapsed), 1);
913
914 $partialtext = '';
915 if ($partial) {
916 $partialtext = ' (not complete; done to ' . userdate($lastindexeddoc,
917 get_string('strftimedatetimeshort', 'langconfig')) . ')';
918 }
919
67d64795 920 $progress->output('Processed ' . $numrecords . ' records containing ' . $numdocs .
1b8cf12a 921 ' documents, in ' . $elapsed . ' seconds' . $partialtext . '.', 1);
67d64795 922 } else {
923 $progress->output('No new documents to index.', 1);
db48207e
DM
924 }
925
075fa912
EM
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;
929
67d64795 930 // Store last index run once documents have been committed to the search engine.
075fa912 931 set_config($varname . '_indexingstart', $indexingstart, $componentconfigname);
7f83e99e 932 set_config($varname . '_indexingend', (int)self::get_current_time(), $componentconfigname);
075fa912
EM
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);
938 }
67d64795 939 if ($partial) {
940 set_config($varname . '_partial', 1, $componentconfigname);
941 } else {
942 unset_config($varname . '_partial', $componentconfigname);
943 }
944 } else {
945 $progress->output('Engine reported error.');
946 }
947
82735dec 948 if ($timelimit && (self::get_current_time() >= $stopat)) {
67d64795 949 $progress->output('Stopping indexing due to time limit.');
950 break;
db48207e
DM
951 }
952 }
953
bf2235bb 954 if ($sumdocs > 0) {
db48207e
DM
955 $event = \core\event\search_indexed::create(
956 array('context' => \context_system::instance()));
957 $event->trigger();
958 }
959
bf2235bb
EM
960 $this->engine->index_complete($sumdocs, $fullindex);
961
962 return (bool)$sumdocs;
db48207e
DM
963 }
964
4ba11aa9 965 /**
966 * Indexes or reindexes a specific context of the system, e.g. one course.
967 *
968 * The function returns an object with field 'complete' (true or false).
969 *
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.
973 *
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
981 */
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();
986 }
987
988 // Work out time to stop, if limited.
989 if ($timelimit) {
990 // Decide time to stop.
82735dec 991 $stopat = self::get_current_time() + $timelimit;
4ba11aa9 992 }
993
994 // No PHP time limit.
995 \core_php_time_limit::raise();
996
997 // Notify the engine that an index starting.
998 $this->engine->index_starting(false);
999
1000 $sumdocs = 0;
1001
1002 // Get all search areas, in consistent order.
1003 $searchareas = $this->get_search_areas_list(true);
1004 ksort($searchareas);
1005
1006 // Are we skipping past some that were handled previously?
1007 $skipping = $startfromarea ? true : false;
1008
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;
1013 }
1014
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;
1020 }
1021 // Stop skipping and note the reference start time.
1022 $skipping = false;
1023 $referencestarttime = $startfromtime;
1024 }
1025
1026 $progress->output('Processing area: ' . $searcharea->get_visible_name());
1027
82735dec 1028 $elapsed = self::get_current_time();
4ba11aa9 1029
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);
1037 }
1038 continue;
1039 }
1040
1041 // Notify the engine that an area is starting.
1042 $this->engine->area_index_starting($searcharea, false);
1043
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;
1050 }
1051
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);
1055
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;
1066 }
1067
1068 if ($numdocs > 0) {
82735dec 1069 $elapsed = round((self::get_current_time() - $elapsed), 3);
4ba11aa9 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);
1075 }
1076
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);
1081 }
1082
82735dec 1083 if ($partial && $timelimit && (self::get_current_time() >= $stopat)) {
4ba11aa9 1084 $progress->output('Stopping indexing due to time limit.');
1085 break;
1086 }
1087 }
1088
1089 if ($sumdocs > 0) {
1090 $event = \core\event\search_indexed::create(
1091 array('context' => $context));
1092 $event->trigger();
1093 }
1094
1095 $this->engine->index_complete($sumdocs, false);
1096
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;
1105 }
1106 return $result;
1107 }
1108
db48207e
DM
1109 /**
1110 * Resets areas config.
1111 *
1112 * @throws \moodle_exception
1113 * @param string $areaid
1114 * @return void
1115 */
1116 public function reset_config($areaid = false) {
1117
1118 if (!empty($areaid)) {
1119 $searchareas = array();
1120 if (!$searchareas[$areaid] = static::get_search_area($areaid)) {
1121 throw new \moodle_exception('errorareanotavailable', 'search', '', $areaid);
1122 }
1123 } else {
1124 // Only the enabled ones.
1125 $searchareas = static::get_search_areas_list(true);
1126 }
1127
1128 foreach ($searchareas as $searcharea) {
69d66020
DM
1129 list($componentname, $varname) = $searcharea->get_config_var_name();
1130 $config = $searcharea->get_config();
db48207e 1131
69d66020
DM
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);
1136 }
1137 }
db48207e
DM
1138 }
1139 }
1140
1141 /**
1142 * Deletes an area's documents or all areas documents.
1143 *
1144 * @param string $areaid The area id or false for all
1145 * @return void
1146 */
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();
1154 }
db48207e
DM
1155 }
1156
1157 /**
1158 * Deletes index by id.
1159 *
1160 * @param int Solr Document string $id
1161 */
1162 public function delete_index_by_id($id) {
1163 $this->engine->delete_by_id($id);
db48207e
DM
1164 }
1165
1166 /**
1167 * Returns search areas configuration.
1168 *
0bd8383a 1169 * @param \core_search\base[] $searchareas
db48207e
DM
1170 * @return \stdClass[] $configsettings
1171 */
1172 public function get_areas_config($searchareas) {
1173
67d64795 1174 $vars = array('indexingstart', 'indexingend', 'lastindexrun', 'docsignored',
1175 'docsprocessed', 'recordsprocessed', 'partial');
db48207e 1176
0a9a10f0 1177 $configsettings = [];
db48207e
DM
1178 foreach ($searchareas as $searcharea) {
1179
1180 $areaid = $searcharea->get_area_id();
1181
1182 $configsettings[$areaid] = new \stdClass();
1183 list($componentname, $varname) = $searcharea->get_config_var_name();
1184
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;
1189 }
1190 } else {
1191 foreach ($vars as $var) {
1192 $configsettings[$areaid]->{$var} = get_config($componentname, $varname .'_' . $var);
1193 }
1194 }
1195
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');
1201 }
1202 }
1203 return $configsettings;
1204 }
396d6f0a 1205
e71061a2
DM
1206 /**
1207 * Triggers search_results_viewed event
1208 *
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
1217 *
1218 * @since Moodle 3.2
1219 * @param array $other Other info for the event.
1220 * @return \core\event\search_results_viewed
1221 */
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
1226 ]);
1227 $event->trigger();
1228
1229 return $event;
1230 }
1231
396d6f0a
DG
1232 /**
1233 * Checks whether a classname is of an actual search area.
1234 *
f3d38863 1235 * @param string $classname
396d6f0a
DG
1236 * @return bool
1237 */
f3d38863
DM
1238 protected static function is_search_area($classname) {
1239 if (is_subclass_of($classname, 'core_search\base')) {
1240 return (new \ReflectionClass($classname))->isInstantiable();
396d6f0a
DG
1241 }
1242
1243 return false;
1244 }
eacb2bd1 1245
1246 /**
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.
1249 *
1250 * This is used after a restore to ensure that restored items are indexed, even though their
65da6840 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.
eacb2bd1 1253 *
1254 * @param \context $context Context to index within
1255 * @param string $areaid Area to index, '' = all areas
8736fbc1 1256 * @param int $priority Priority (INDEX_PRIORITY_xx constant)
eacb2bd1 1257 */
8736fbc1 1258 public static function request_index(\context $context, $areaid = '',
1259 $priority = self::INDEX_PRIORITY_NORMAL) {
eacb2bd1 1260 global $DB;
1261
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',
8736fbc1 1266 'contextid ' . $contextsql, $contextparams, '',
1267 'id, searcharea, partialarea, indexpriority');
eacb2bd1 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 === '')) {
8736fbc1 1272 // If the existing request has the same (or higher) priority, no need to add anything.
1273 if ($rec->indexpriority >= $priority) {
1274 return;
1275 }
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;
1282 }
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.
eacb2bd1 1285 }
1286 }
1287
1288 // No suitable existing request, so add a new one.
1289 $newrecord = [ 'contextid' => $context->id, 'searcharea' => $areaid,
8736fbc1 1290 'timerequested' => (int)self::get_current_time(),
1291 'partialarea' => '', 'partialtime' => 0,
1292 'indexpriority' => $priority ];
eacb2bd1 1293 $DB->insert_record('search_index_requests', $newrecord);
1294 }
74b7a42d 1295
1296 /**
8736fbc1 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.
74b7a42d 1300 *
1301 * If there are no index requests, the function will do nothing.
1302 *
1303 * @param float $timelimit Time limit (0 = none)
1304 * @param \progress_trace|null $progress Optional progress indicator
1305 */
1306 public function process_index_requests($timelimit = 0.0, \progress_trace $progress = null) {
1307 global $DB;
1308
1309 if (!$progress) {
1310 $progress = new \null_progress_trace();
1311 }
1312
82735dec 1313 $before = self::get_current_time();
74b7a42d 1314 if ($timelimit) {
1315 $stopat = $before + $timelimit;
1316 }
1317 while (true) {
1318 // Retrieve first request, using fully defined ordering.
1319 $requests = $DB->get_records('search_index_requests', null,
8736fbc1 1320 'indexpriority DESC, timerequested, contextid, searcharea',
74b7a42d 1321 'id, contextid, searcharea, partialarea, partialtime', 0, 1);
1322 if (!$requests) {
1323 // If there are no more requests, stop.
74b7a42d 1324 break;
1325 }
1326 $request = reset($requests);
1327
1328 // Calculate remaining time.
1329 $remainingtime = 0;
82735dec 1330 $beforeindex = self::get_current_time();
74b7a42d 1331 if ($timelimit) {
1332 $remainingtime = $stopat - $beforeindex;
8736fbc1 1333
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;
1338 }
74b7a42d 1339 }
1340
1341 // Show a message before each request, indicating what will be indexed.
a539e560 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;
1347 }
74b7a42d 1348 $contextname = $context->get_context_name();
1349 if ($request->searcharea) {
1350 $contextname .= ' (search area: ' . $request->searcharea . ')';
1351 }
1352 $progress->output('Indexing requested context: ' . $contextname);
1353
1354 // Actually index the context.
1355 $result = $this->index_context($context, $request->searcharea, $remainingtime,
1356 $progress, $request->partialarea, $request->partialtime);
1357
1358 // Work out shared part of message.
82735dec 1359 $endmessage = $contextname . ' (' . round(self::get_current_time() - $beforeindex, 1) . 's)';
74b7a42d 1360
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);
1372
1373 // The time limit must have expired, so stop looping.
1374 break;
1375 }
1376 }
1377 }
1378
65da6840 1379 /**
1380 * Gets information about the request queue, in the form of a plain object suitable for passing
1381 * to a template for rendering.
1382 *
1383 * @return \stdClass Information about queued index requests
1384 */
1385 public function get_index_requests_info() {
1386 global $DB;
1387
1388 $result = new \stdClass();
1389
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();
1401 }
1402 if ($item->partialarea) {
1403 $item->partialareaname = $this->get_search_area($item->partialarea)->get_visible_name();
1404 }
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;
1412 }
1413 }
1414
1415 // Normalise array indices.
1416 $result->topten = array_values($result->topten);
1417
1418 if ($result->total > 10) {
1419 $result->ellipsis = true;
1420 }
1421
1422 return $result;
1423 }
1424
82735dec 1425 /**
1426 * Gets current time for use in search system.
1427 *
1428 * Note: This should be replaced with generic core functionality once possible (see MDL-60644).
1429 *
1430 * @return float Current time in seconds (with decimals)
1431 */
1432 public static function get_current_time() {
1433 if (PHPUNIT_TEST && self::$phpunitfaketime) {
1434 return self::$phpunitfaketime;
1435 }
1436 return microtime(true);
1437 }
db48207e 1438}