MDL-61102 core_search: Improve performance by reducing schema checks
[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();
db48207e
DM
374 }
375
376 /**
377 * Generates an area id from the componentname and the area name.
378 *
379 * There should not be any naming conflict as the area name is the
380 * class name in component/classes/search/.
381 *
382 * @param string $componentname
383 * @param string $areaname
384 * @return void
385 */
386 public static function generate_areaid($componentname, $areaname) {
387 return $componentname . '-' . $areaname;
388 }
389
390 /**
391 * Returns all areaid string components (component name and area name).
392 *
393 * @param string $areaid
394 * @return array Component name (Frankenstyle) and area name (search area class name)
395 */
396 public static function extract_areaid_parts($areaid) {
397 return explode('-', $areaid);
398 }
399
400 /**
4359ef18 401 * Returns information about the areas which the user can access.
db48207e 402 *
4359ef18 403 * The returned value is a stdClass object with the following fields:
404 * - everything (bool, true for admin only)
405 * - usercontexts (indexed by area identifier then context
406 * - separategroupscontexts (contexts within which group restrictions apply)
407 * - visiblegroupscontextsareas (overrides to the above when the same contexts also have
408 * 'visible groups' for certain search area ids - hopefully rare)
409 * - usergroups (groups which the current user belongs to)
db48207e 410 *
cfa00fc5 411 * The areas can be limited by course id and context id. If specifying context ids, results
412 * are limited to the exact context ids specified and not their children (for example, giving
413 * the course context id would result in including search items with the course context id, and
414 * not anything from a context inside the course). For performance, you should also specify
415 * course id(s) when using context ids.
416 *
427e3cbc 417 * @param array|false $limitcourseids An array of course ids to limit the search to. False for no limiting.
cfa00fc5 418 * @param array|false $limitcontextids An array of context ids to limit the search to. False for no limiting.
4359ef18 419 * @return \stdClass Object as described above
db48207e 420 */
cfa00fc5 421 protected function get_areas_user_accesses($limitcourseids = false, $limitcontextids = false) {
a96faa49 422 global $DB, $USER;
db48207e 423
cfa00fc5 424 // All results for admins (unless they have chosen to limit results). Eventually we could
425 // add a new capability for managers.
426 if (is_siteadmin() && !$limitcourseids && !$limitcontextids) {
4359ef18 427 return (object)array('everything' => true);
db48207e
DM
428 }
429
430 $areasbylevel = array();
431
432 // Split areas by context level so we only iterate only once through courses and cms.
433 $searchareas = static::get_search_areas_list(true);
434 foreach ($searchareas as $areaid => $unused) {
435 $classname = static::get_area_classname($areaid);
436 $searcharea = new $classname();
437 foreach ($classname::get_levels() as $level) {
438 $areasbylevel[$level][$areaid] = $searcharea;
439 }
440 }
441
442 // This will store area - allowed contexts relations.
443 $areascontexts = array();
444
4359ef18 445 // Initialise two special-case arrays for storing other information related to the contexts.
446 $separategroupscontexts = array();
447 $visiblegroupscontextsareas = array();
448 $usergroups = array();
449
427e3cbc 450 if (empty($limitcourseids) && !empty($areasbylevel[CONTEXT_SYSTEM])) {
db48207e
DM
451 // We add system context to all search areas working at this level. Here each area is fully responsible of
452 // the access control as we can not automate much, we can not even check guest access as some areas might
453 // want to allow guests to retrieve data from them.
454
455 $systemcontextid = \context_system::instance()->id;
cfa00fc5 456 if (!$limitcontextids || in_array($systemcontextid, $limitcontextids)) {
457 foreach ($areasbylevel[CONTEXT_SYSTEM] as $areaid => $searchclass) {
458 $areascontexts[$areaid][$systemcontextid] = $systemcontextid;
459 }
25ba053f
DM
460 }
461 }
462
463 if (!empty($areasbylevel[CONTEXT_USER])) {
464 if ($usercontext = \context_user::instance($USER->id, IGNORE_MISSING)) {
cfa00fc5 465 if (!$limitcontextids || in_array($usercontext->id, $limitcontextids)) {
466 // Extra checking although only logged users should reach this point, guest users have a valid context id.
467 foreach ($areasbylevel[CONTEXT_USER] as $areaid => $searchclass) {
468 $areascontexts[$areaid][$usercontext->id] = $usercontext->id;
469 }
25ba053f 470 }
db48207e
DM
471 }
472 }
473
cfa00fc5 474 if (is_siteadmin()) {
475 // Admins have access to all courses regardless of enrolment.
476 if ($limitcourseids) {
477 list ($coursesql, $courseparams) = $DB->get_in_or_equal($limitcourseids);
478 $coursesql = 'id ' . $coursesql;
479 } else {
480 $coursesql = '';
481 $courseparams = [];
482 }
483 // Get courses using the same list of fields from enrol_get_my_courses.
484 $courses = $DB->get_records_select('course', $coursesql, $courseparams, '',
485 'id, category, sortorder, shortname, fullname, idnumber, startdate, visible, ' .
486 'groupmode, groupmodeforce, cacherev');
487 } else {
488 // Get the courses where the current user has access.
489 $courses = enrol_get_my_courses(array('id', 'cacherev'), 'id', 0, [],
490 (bool)get_config('core', 'searchallavailablecourses'));
491 }
427e3cbc
EM
492
493 if (empty($limitcourseids) || in_array(SITEID, $limitcourseids)) {
494 $courses[SITEID] = get_course(SITEID);
495 }
496
a96faa49 497 // Keep a list of included course context ids (needed for the block calculation below).
498 $coursecontextids = [];
4359ef18 499 $modulecms = [];
a96faa49 500
db48207e 501 foreach ($courses as $course) {
427e3cbc
EM
502 if (!empty($limitcourseids) && !in_array($course->id, $limitcourseids)) {
503 // Skip non-included courses.
504 continue;
505 }
db48207e 506
a96faa49 507 $coursecontext = \context_course::instance($course->id);
508 $coursecontextids[] = $coursecontext->id;
4359ef18 509 $hasgrouprestrictions = false;
a96faa49 510
db48207e
DM
511 // Info about the course modules.
512 $modinfo = get_fast_modinfo($course);
513
cfa00fc5 514 if (!empty($areasbylevel[CONTEXT_COURSE]) &&
515 (!$limitcontextids || in_array($coursecontext->id, $limitcontextids))) {
db48207e 516 // Add the course contexts the user can view.
db48207e
DM
517 foreach ($areasbylevel[CONTEXT_COURSE] as $areaid => $searchclass) {
518 if ($course->visible || has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
519 $areascontexts[$areaid][$coursecontext->id] = $coursecontext->id;
520 }
521 }
522 }
523
524 if (!empty($areasbylevel[CONTEXT_MODULE])) {
525 // Add the module contexts the user can view (cm_info->uservisible).
526
527 foreach ($areasbylevel[CONTEXT_MODULE] as $areaid => $searchclass) {
528
529 // Removing the plugintype 'mod_' prefix.
530 $modulename = substr($searchclass->get_component_name(), 4);
531
532 $modinstances = $modinfo->get_instances_of($modulename);
533 foreach ($modinstances as $modinstance) {
cfa00fc5 534 // Skip module context if not included in list of context ids.
535 if ($limitcontextids && !in_array($modinstance->context->id, $limitcontextids)) {
536 continue;
537 }
db48207e 538 if ($modinstance->uservisible) {
4359ef18 539 $contextid = $modinstance->context->id;
540 $areascontexts[$areaid][$contextid] = $contextid;
541 $modulecms[$modinstance->id] = $modinstance;
542
543 if (!has_capability('moodle/site:accessallgroups', $modinstance->context) &&
544 ($searchclass instanceof base_mod) &&
545 $searchclass->supports_group_restriction()) {
546 if ($searchclass->restrict_cm_access_by_group($modinstance)) {
547 $separategroupscontexts[$contextid] = $contextid;
548 $hasgrouprestrictions = true;
549 } else {
550 // Track a list of anything that has a group id (so might get
551 // filtered) and doesn't want to be, in this context.
552 if (!array_key_exists($contextid, $visiblegroupscontextsareas)) {
553 $visiblegroupscontextsareas[$contextid] = array();
554 }
555 $visiblegroupscontextsareas[$contextid][$areaid] = $areaid;
556 }
557 }
db48207e
DM
558 }
559 }
560 }
561 }
4359ef18 562
563 // Insert group information for course (unless there aren't any modules restricted by
564 // group for this user in this course, in which case don't bother).
565 if ($hasgrouprestrictions) {
566 $groups = groups_get_all_groups($course->id, $USER->id, 0, 'g.id');
567 foreach ($groups as $group) {
568 $usergroups[$group->id] = $group->id;
569 }
570 }
571 }
572
573 // Chuck away all the 'visible groups contexts' data unless there is actually something
574 // that does use separate groups in the same context (this data is only used as an
575 // 'override' in cases where the search is restricting to separate groups).
576 foreach ($visiblegroupscontextsareas as $contextid => $areas) {
577 if (!array_key_exists($contextid, $separategroupscontexts)) {
578 unset($visiblegroupscontextsareas[$contextid]);
579 }
db48207e
DM
580 }
581
a96faa49 582 // Add all supported block contexts, in a single query for performance.
583 if (!empty($areasbylevel[CONTEXT_BLOCK])) {
584 // Get list of all block types we care about.
585 $blocklist = [];
586 foreach ($areasbylevel[CONTEXT_BLOCK] as $areaid => $searchclass) {
587 $blocklist[$searchclass->get_block_name()] = true;
588 }
589 list ($blocknamesql, $blocknameparams) = $DB->get_in_or_equal(array_keys($blocklist));
590
591 // Get list of course contexts.
592 list ($contextsql, $contextparams) = $DB->get_in_or_equal($coursecontextids);
593
cfa00fc5 594 // Get list of block context (if limited).
595 $blockcontextwhere = '';
596 $blockcontextparams = [];
597 if ($limitcontextids) {
598 list ($blockcontextsql, $blockcontextparams) = $DB->get_in_or_equal($limitcontextids);
599 $blockcontextwhere = 'AND x.id ' . $blockcontextsql;
600 }
601
a96faa49 602 // Query all blocks that are within an included course, and are set to be visible, and
603 // in a supported page type (basically just course view). This query could be
604 // extended (or a second query added) to support blocks that are within a module
605 // context as well, and we could add more page types if required.
606 $blockrecs = $DB->get_records_sql("
607 SELECT x.*, bi.blockname AS blockname, bi.id AS blockinstanceid
608 FROM {block_instances} bi
609 JOIN {context} x ON x.instanceid = bi.id AND x.contextlevel = ?
610 LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id
611 AND bp.contextid = bi.parentcontextid
612 AND bp.pagetype LIKE 'course-view-%'
613 AND bp.subpage = ''
614 AND bp.visible = 0
615 WHERE bi.parentcontextid $contextsql
cfa00fc5 616 $blockcontextwhere
a96faa49 617 AND bi.blockname $blocknamesql
618 AND bi.subpagepattern IS NULL
619 AND (bi.pagetypepattern = 'site-index'
620 OR bi.pagetypepattern LIKE 'course-view-%'
621 OR bi.pagetypepattern = 'course-*'
622 OR bi.pagetypepattern = '*')
623 AND bp.id IS NULL",
cfa00fc5 624 array_merge([CONTEXT_BLOCK], $contextparams, $blockcontextparams, $blocknameparams));
a96faa49 625 $blockcontextsbyname = [];
626 foreach ($blockrecs as $blockrec) {
627 if (empty($blockcontextsbyname[$blockrec->blockname])) {
628 $blockcontextsbyname[$blockrec->blockname] = [];
629 }
630 \context_helper::preload_from_record($blockrec);
631 $blockcontextsbyname[$blockrec->blockname][] = \context_block::instance(
632 $blockrec->blockinstanceid);
633 }
634
635 // Add the block contexts the user can view.
636 foreach ($areasbylevel[CONTEXT_BLOCK] as $areaid => $searchclass) {
637 if (empty($blockcontextsbyname[$searchclass->get_block_name()])) {
638 continue;
639 }
640 foreach ($blockcontextsbyname[$searchclass->get_block_name()] as $context) {
641 if (has_capability('moodle/block:view', $context)) {
642 $areascontexts[$areaid][$context->id] = $context->id;
643 }
644 }
645 }
646 }
647
4359ef18 648 // Return all the data.
649 return (object)array('everything' => false, 'usercontexts' => $areascontexts,
650 'separategroupscontexts' => $separategroupscontexts, 'usergroups' => $usergroups,
651 'visiblegroupscontextsareas' => $visiblegroupscontextsareas);
db48207e
DM
652 }
653
053118a1
EM
654 /**
655 * Returns requested page of documents plus additional information for paging.
656 *
657 * This function does not perform any kind of security checking for access, the caller code
658 * should check that the current user have moodle/search:query capability.
659 *
660 * If a page is requested that is beyond the last result, the last valid page is returned in
661 * results, and actualpage indicates which page was returned.
662 *
663 * @param stdClass $formdata
664 * @param int $pagenum The 0 based page number.
665 * @return object An object with 3 properties:
666 * results => An array of \core_search\documents for the actual page.
667 * totalcount => Number of records that are possibly available, to base paging on.
668 * actualpage => The actual page returned.
669 */
670 public function paged_search(\stdClass $formdata, $pagenum) {
671 $out = new \stdClass();
672
673 $perpage = static::DISPLAY_RESULTS_PER_PAGE;
674
675 // Make sure we only allow request up to max page.
676 $pagenum = min($pagenum, (static::MAX_RESULTS / $perpage) - 1);
677
678 // Calculate the first and last document number for the current page, 1 based.
679 $mindoc = ($pagenum * $perpage) + 1;
680 $maxdoc = ($pagenum + 1) * $perpage;
681
682 // Get engine documents, up to max.
683 $docs = $this->search($formdata, $maxdoc);
684
685 $resultcount = count($docs);
686 if ($resultcount < $maxdoc) {
687 // This means it couldn't give us results to max, so the count must be the max.
688 $out->totalcount = $resultcount;
689 } else {
690 // Get the possible count reported by engine, and limit to our max.
691 $out->totalcount = $this->engine->get_query_total_count();
692 $out->totalcount = min($out->totalcount, static::MAX_RESULTS);
693 }
694
695 // Determine the actual page.
696 if ($resultcount < $mindoc) {
697 // We couldn't get the min docs for this page, so determine what page we can get.
698 $out->actualpage = floor(($resultcount - 1) / $perpage);
699 } else {
700 $out->actualpage = $pagenum;
701 }
702
703 // Split the results to only return the page.
704 $out->results = array_slice($docs, $out->actualpage * $perpage, $perpage, true);
705
706 return $out;
707 }
708
db48207e
DM
709 /**
710 * Returns documents from the engine based on the data provided.
711 *
69d66020
DM
712 * This function does not perform any kind of security checking, the caller code
713 * should check that the current user have moodle/search:query capability.
714 *
db48207e
DM
715 * It might return the results from the cache instead.
716 *
cfa00fc5 717 * Valid formdata options include:
718 * - q (query text)
719 * - courseids (optional list of course ids to restrict)
720 * - contextids (optional list of context ids to restrict)
fc440796 721 * - context (Moodle context object for location user searched from)
722 * - order (optional ordering, one of the types supported by the search engine e.g. 'relevance')
cfa00fc5 723 *
724 * @param \stdClass $formdata Query input data (usually from search form)
725 * @param int $limit The maximum number of documents to return
db48207e
DM
726 * @return \core_search\document[]
727 */
053118a1 728 public function search(\stdClass $formdata, $limit = 0) {
e36eefae 729 // For Behat testing, the search results can be faked using a special step.
730 if (defined('BEHAT_SITE_RUNNING')) {
731 $fakeresult = get_config('core_search', 'behat_fakeresult');
732 if ($fakeresult) {
733 // Clear config setting.
734 unset_config('core_search', 'behat_fakeresult');
735
736 // Check query matches expected value.
737 $details = json_decode($fakeresult);
738 if ($formdata->q !== $details->query) {
739 throw new \coding_exception('Unexpected search query: ' . $formdata->q);
740 }
741
742 // Create search documents from the JSON data.
743 $docs = [];
744 foreach ($details->results as $result) {
745 $doc = new \core_search\document($result->itemid, $result->componentname,
746 $result->areaname);
747 foreach ((array)$result->fields as $field => $value) {
748 $doc->set($field, $value);
749 }
750 foreach ((array)$result->extrafields as $field => $value) {
751 $doc->set_extra($field, $value);
752 }
753 $area = $this->get_search_area($doc->get('areaid'));
754 $doc->set_doc_url($area->get_doc_url($doc));
755 $doc->set_context_url($area->get_context_url($doc));
756 $docs[] = $doc;
757 }
758
759 return $docs;
760 }
761 }
db48207e 762
427e3cbc
EM
763 $limitcourseids = false;
764 if (!empty($formdata->courseids)) {
765 $limitcourseids = $formdata->courseids;
766 }
767
cfa00fc5 768 $limitcontextids = false;
769 if (!empty($formdata->contextids)) {
770 $limitcontextids = $formdata->contextids;
771 }
772
db48207e
DM
773 // Clears previous query errors.
774 $this->engine->clear_query_error();
775
4359ef18 776 $contextinfo = $this->get_areas_user_accesses($limitcourseids, $limitcontextids);
777 if (!$contextinfo->everything && !$contextinfo->usercontexts) {
db48207e
DM
778 // User can not access any context.
779 $docs = array();
780 } else {
4359ef18 781 // If engine does not support groups, remove group information from the context info -
782 // use the old format instead (true = admin, array = user contexts).
783 if (!$this->engine->supports_group_filtering()) {
784 $contextinfo = $contextinfo->everything ? true : $contextinfo->usercontexts;
785 }
786
787 // Execute the actual query.
788 $docs = $this->engine->execute_query($formdata, $contextinfo, $limit);
db48207e
DM
789 }
790
db48207e
DM
791 return $docs;
792 }
793
db48207e
DM
794 /**
795 * Merge separate index segments into one.
796 */
797 public function optimize_index() {
798 $this->engine->optimize();
799 }
800
801 /**
802 * Index all documents.
803 *
804 * @param bool $fullindex Whether we should reindex everything or not.
67d64795 805 * @param float $timelimit Time limit in seconds (0 = no time limit)
4ba11aa9 806 * @param \progress_trace|null $progress Optional class for tracking progress
db48207e
DM
807 * @throws \moodle_exception
808 * @return bool Whether there was any updated document or not.
809 */
67d64795 810 public function index($fullindex = false, $timelimit = 0, \progress_trace $progress = null) {
eacb2bd1 811 global $DB;
812
67d64795 813 // Cannot combine time limit with reindex.
814 if ($timelimit && $fullindex) {
815 throw new \coding_exception('Cannot apply time limit when reindexing');
816 }
817 if (!$progress) {
818 $progress = new \null_progress_trace();
819 }
db48207e
DM
820
821 // Unlimited time.
822 \core_php_time_limit::raise();
823
075fa912
EM
824 // Notify the engine that an index starting.
825 $this->engine->index_starting($fullindex);
826
bf2235bb 827 $sumdocs = 0;
db48207e
DM
828
829 $searchareas = $this->get_search_areas_list(true);
67d64795 830
831 if ($timelimit) {
832 // If time is limited (and therefore we're not just indexing everything anyway), select
833 // an order for search areas. The intention here is to avoid a situation where a new
834 // large search area is enabled, and this means all our other search areas go out of
835 // date while that one is being indexed. To do this, we order by the time we spent
836 // indexing them last time we ran, meaning anything that took a very long time will be
837 // done last.
838 uasort($searchareas, function(\core_search\base $area1, \core_search\base $area2) {
839 return (int)$area1->get_last_indexing_duration() - (int)$area2->get_last_indexing_duration();
840 });
841
842 // Decide time to stop.
82735dec 843 $stopat = self::get_current_time() + $timelimit;
67d64795 844 }
845
db48207e
DM
846 foreach ($searchareas as $areaid => $searcharea) {
847
67d64795 848 $progress->output('Processing area: ' . $searcharea->get_visible_name());
db48207e 849
075fa912
EM
850 // Notify the engine that an area is starting.
851 $this->engine->area_index_starting($searcharea, $fullindex);
852
7f83e99e 853 $indexingstart = (int)self::get_current_time();
82735dec 854 $elapsed = self::get_current_time();
db48207e
DM
855
856 // This is used to store this component config.
857 list($componentconfigname, $varname) = $searcharea->get_config_var_name();
858
091973db
EM
859 $prevtimestart = intval(get_config($componentconfigname, $varname . '_indexingstart'));
860
db48207e 861 if ($fullindex === true) {
091973db 862 $referencestarttime = 0;
eacb2bd1 863
864 // For full index, we delete any queued context index requests, as those will
865 // obviously be met by the full index.
866 $DB->delete_records('search_index_requests');
db48207e 867 } else {
67d64795 868 $partial = get_config($componentconfigname, $varname . '_partial');
869 if ($partial) {
870 // When the previous index did not complete all data, we start from the time of the
871 // last document that was successfully indexed. (Note this will result in
872 // re-indexing that one document, but we can't avoid that because there may be
873 // other documents in the same second.)
874 $referencestarttime = intval(get_config($componentconfigname, $varname . '_lastindexrun'));
875 } else {
876 $referencestarttime = $prevtimestart;
877 }
db48207e
DM
878 }
879
880 // Getting the recordset from the area.
091973db 881 $recordset = $searcharea->get_recordset_by_timestamp($referencestarttime);
1b8cf12a 882 $initialquerytime = self::get_current_time() - $elapsed;
883 if ($initialquerytime > self::DISPLAY_LONG_QUERY_TIME) {
884 $progress->output('Initial query took ' . round($initialquerytime, 1) .
885 ' seconds.', 1);
886 }
db48207e
DM
887
888 // Pass get_document as callback.
091973db
EM
889 $fileindexing = $this->engine->file_indexing_enabled() && $searcharea->uses_file_indexing();
890 $options = array('indexfiles' => $fileindexing, 'lastindexedtime' => $prevtimestart);
67d64795 891 if ($timelimit) {
892 $options['stopat'] = $stopat;
893 }
1b8cf12a 894 $options['progress'] = $progress;
2d2fcc1c 895 $iterator = new skip_future_documents_iterator(new \core\dml\recordset_walk(
896 $recordset, array($searcharea, 'get_document'), $options));
67d64795 897 $result = $this->engine->add_documents($iterator, $searcharea, $options);
2d2fcc1c 898 $recordset->close();
67d64795 899 if (count($result) === 5) {
900 list($numrecords, $numdocs, $numdocsignored, $lastindexeddoc, $partial) = $result;
901 } else {
902 // Backward compatibility for engines that don't support partial adding.
903 list($numrecords, $numdocs, $numdocsignored, $lastindexeddoc) = $result;
904 debugging('engine::add_documents() should return $partial (4-value return is deprecated)',
905 DEBUG_DEVELOPER);
906 $partial = false;
907 }
908
909 if ($numdocs > 0) {
1b8cf12a 910 $elapsed = round((self::get_current_time() - $elapsed), 1);
911
912 $partialtext = '';
913 if ($partial) {
914 $partialtext = ' (not complete; done to ' . userdate($lastindexeddoc,
915 get_string('strftimedatetimeshort', 'langconfig')) . ')';
916 }
917
67d64795 918 $progress->output('Processed ' . $numrecords . ' records containing ' . $numdocs .
1b8cf12a 919 ' documents, in ' . $elapsed . ' seconds' . $partialtext . '.', 1);
67d64795 920 } else {
921 $progress->output('No new documents to index.', 1);
db48207e
DM
922 }
923
075fa912
EM
924 // Notify the engine this area is complete, and only mark times if true.
925 if ($this->engine->area_index_complete($searcharea, $numdocs, $fullindex)) {
926 $sumdocs += $numdocs;
927
67d64795 928 // Store last index run once documents have been committed to the search engine.
075fa912 929 set_config($varname . '_indexingstart', $indexingstart, $componentconfigname);
7f83e99e 930 set_config($varname . '_indexingend', (int)self::get_current_time(), $componentconfigname);
075fa912
EM
931 set_config($varname . '_docsignored', $numdocsignored, $componentconfigname);
932 set_config($varname . '_docsprocessed', $numdocs, $componentconfigname);
933 set_config($varname . '_recordsprocessed', $numrecords, $componentconfigname);
934 if ($lastindexeddoc > 0) {
935 set_config($varname . '_lastindexrun', $lastindexeddoc, $componentconfigname);
936 }
67d64795 937 if ($partial) {
938 set_config($varname . '_partial', 1, $componentconfigname);
939 } else {
940 unset_config($varname . '_partial', $componentconfigname);
941 }
942 } else {
943 $progress->output('Engine reported error.');
944 }
945
82735dec 946 if ($timelimit && (self::get_current_time() >= $stopat)) {
67d64795 947 $progress->output('Stopping indexing due to time limit.');
948 break;
db48207e
DM
949 }
950 }
951
bf2235bb 952 if ($sumdocs > 0) {
db48207e
DM
953 $event = \core\event\search_indexed::create(
954 array('context' => \context_system::instance()));
955 $event->trigger();
956 }
957
bf2235bb
EM
958 $this->engine->index_complete($sumdocs, $fullindex);
959
960 return (bool)$sumdocs;
db48207e
DM
961 }
962
4ba11aa9 963 /**
964 * Indexes or reindexes a specific context of the system, e.g. one course.
965 *
966 * The function returns an object with field 'complete' (true or false).
967 *
968 * This function supports partial indexing via the time limit parameter. If the time limit
969 * expires, it will return values for $startfromarea and $startfromtime which can be passed
970 * next time to continue indexing.
971 *
972 * @param \context $context Context to restrict index.
973 * @param string $singleareaid If specified, indexes only the given area.
974 * @param float $timelimit Time limit in seconds (0 = no time limit)
975 * @param \progress_trace|null $progress Optional class for tracking progress
976 * @param string $startfromarea Area to start from
977 * @param int $startfromtime Timestamp to start from
978 * @return \stdClass Object indicating success
979 */
980 public function index_context($context, $singleareaid = '', $timelimit = 0,
981 \progress_trace $progress = null, $startfromarea = '', $startfromtime = 0) {
982 if (!$progress) {
983 $progress = new \null_progress_trace();
984 }
985
986 // Work out time to stop, if limited.
987 if ($timelimit) {
988 // Decide time to stop.
82735dec 989 $stopat = self::get_current_time() + $timelimit;
4ba11aa9 990 }
991
992 // No PHP time limit.
993 \core_php_time_limit::raise();
994
995 // Notify the engine that an index starting.
996 $this->engine->index_starting(false);
997
998 $sumdocs = 0;
999
1000 // Get all search areas, in consistent order.
1001 $searchareas = $this->get_search_areas_list(true);
1002 ksort($searchareas);
1003
1004 // Are we skipping past some that were handled previously?
1005 $skipping = $startfromarea ? true : false;
1006
1007 foreach ($searchareas as $areaid => $searcharea) {
1008 // If we're only processing one area id, skip all the others.
1009 if ($singleareaid && $singleareaid !== $areaid) {
1010 continue;
1011 }
1012
1013 // If we're skipping to a later area, continue through the loop.
1014 $referencestarttime = 0;
1015 if ($skipping) {
1016 if ($areaid !== $startfromarea) {
1017 continue;
1018 }
1019 // Stop skipping and note the reference start time.
1020 $skipping = false;
1021 $referencestarttime = $startfromtime;
1022 }
1023
1024 $progress->output('Processing area: ' . $searcharea->get_visible_name());
1025
82735dec 1026 $elapsed = self::get_current_time();
4ba11aa9 1027
1028 // Get the recordset of all documents from the area for this context.
1029 $recordset = $searcharea->get_document_recordset($referencestarttime, $context);
1030 if (!$recordset) {
1031 if ($recordset === null) {
1032 $progress->output('Skipping (not relevant to context).', 1);
1033 } else {
1034 $progress->output('Skipping (does not support context indexing).', 1);
1035 }
1036 continue;
1037 }
1038
1039 // Notify the engine that an area is starting.
1040 $this->engine->area_index_starting($searcharea, false);
1041
1042 // Work out search options.
1043 $options = [];
1044 $options['indexfiles'] = $this->engine->file_indexing_enabled() &&
1045 $searcharea->uses_file_indexing();
1046 if ($timelimit) {
1047 $options['stopat'] = $stopat;
1048 }
1049
1050 // Construct iterator which will use get_document on the recordset results.
1051 $iterator = new \core\dml\recordset_walk($recordset,
1052 array($searcharea, 'get_document'), $options);
1053
1054 // Use this iterator to add documents.
1055 $result = $this->engine->add_documents($iterator, $searcharea, $options);
1056 if (count($result) === 5) {
1057 list($numrecords, $numdocs, $numdocsignored, $lastindexeddoc, $partial) = $result;
1058 } else {
1059 // Backward compatibility for engines that don't support partial adding.
1060 list($numrecords, $numdocs, $numdocsignored, $lastindexeddoc) = $result;
1061 debugging('engine::add_documents() should return $partial (4-value return is deprecated)',
1062 DEBUG_DEVELOPER);
1063 $partial = false;
1064 }
1065
1066 if ($numdocs > 0) {
82735dec 1067 $elapsed = round((self::get_current_time() - $elapsed), 3);
4ba11aa9 1068 $progress->output('Processed ' . $numrecords . ' records containing ' . $numdocs .
1069 ' documents, in ' . $elapsed . ' seconds' .
1070 ($partial ? ' (not complete)' : '') . '.', 1);
1071 } else {
1072 $progress->output('No documents to index.', 1);
1073 }
1074
1075 // Notify the engine this area is complete, but don't store any times as this is not
1076 // part of the 'normal' search index.
1077 if (!$this->engine->area_index_complete($searcharea, $numdocs, false)) {
1078 $progress->output('Engine reported error.', 1);
1079 }
1080
82735dec 1081 if ($partial && $timelimit && (self::get_current_time() >= $stopat)) {
4ba11aa9 1082 $progress->output('Stopping indexing due to time limit.');
1083 break;
1084 }
1085 }
1086
1087 if ($sumdocs > 0) {
1088 $event = \core\event\search_indexed::create(
1089 array('context' => $context));
1090 $event->trigger();
1091 }
1092
1093 $this->engine->index_complete($sumdocs, false);
1094
1095 // Indicate in result whether we completed indexing, or only part of it.
1096 $result = new \stdClass();
1097 if ($partial) {
1098 $result->complete = false;
1099 $result->startfromarea = $areaid;
1100 $result->startfromtime = $lastindexeddoc;
1101 } else {
1102 $result->complete = true;
1103 }
1104 return $result;
1105 }
1106
db48207e
DM
1107 /**
1108 * Resets areas config.
1109 *
1110 * @throws \moodle_exception
1111 * @param string $areaid
1112 * @return void
1113 */
1114 public function reset_config($areaid = false) {
1115
1116 if (!empty($areaid)) {
1117 $searchareas = array();
1118 if (!$searchareas[$areaid] = static::get_search_area($areaid)) {
1119 throw new \moodle_exception('errorareanotavailable', 'search', '', $areaid);
1120 }
1121 } else {
1122 // Only the enabled ones.
1123 $searchareas = static::get_search_areas_list(true);
1124 }
1125
1126 foreach ($searchareas as $searcharea) {
69d66020
DM
1127 list($componentname, $varname) = $searcharea->get_config_var_name();
1128 $config = $searcharea->get_config();
db48207e 1129
69d66020
DM
1130 foreach ($config as $key => $value) {
1131 // We reset them all but the enable/disabled one.
1132 if ($key !== $varname . '_enabled') {
1133 set_config($key, 0, $componentname);
1134 }
1135 }
db48207e
DM
1136 }
1137 }
1138
1139 /**
1140 * Deletes an area's documents or all areas documents.
1141 *
1142 * @param string $areaid The area id or false for all
1143 * @return void
1144 */
1145 public function delete_index($areaid = false) {
1146 if (!empty($areaid)) {
1147 $this->engine->delete($areaid);
1148 $this->reset_config($areaid);
1149 } else {
1150 $this->engine->delete();
1151 $this->reset_config();
1152 }
db48207e
DM
1153 }
1154
1155 /**
1156 * Deletes index by id.
1157 *
1158 * @param int Solr Document string $id
1159 */
1160 public function delete_index_by_id($id) {
1161 $this->engine->delete_by_id($id);
db48207e
DM
1162 }
1163
1164 /**
1165 * Returns search areas configuration.
1166 *
0bd8383a 1167 * @param \core_search\base[] $searchareas
db48207e
DM
1168 * @return \stdClass[] $configsettings
1169 */
1170 public function get_areas_config($searchareas) {
1171
67d64795 1172 $vars = array('indexingstart', 'indexingend', 'lastindexrun', 'docsignored',
1173 'docsprocessed', 'recordsprocessed', 'partial');
db48207e 1174
0a9a10f0 1175 $configsettings = [];
db48207e
DM
1176 foreach ($searchareas as $searcharea) {
1177
1178 $areaid = $searcharea->get_area_id();
1179
1180 $configsettings[$areaid] = new \stdClass();
1181 list($componentname, $varname) = $searcharea->get_config_var_name();
1182
1183 if (!$searcharea->is_enabled()) {
1184 // We delete all indexed data on disable so no info.
1185 foreach ($vars as $var) {
1186 $configsettings[$areaid]->{$var} = 0;
1187 }
1188 } else {
1189 foreach ($vars as $var) {
1190 $configsettings[$areaid]->{$var} = get_config($componentname, $varname .'_' . $var);
1191 }
1192 }
1193
1194 // Formatting the time.
1195 if (!empty($configsettings[$areaid]->lastindexrun)) {
1196 $configsettings[$areaid]->lastindexrun = userdate($configsettings[$areaid]->lastindexrun);
1197 } else {
1198 $configsettings[$areaid]->lastindexrun = get_string('never');
1199 }
1200 }
1201 return $configsettings;
1202 }
396d6f0a 1203
e71061a2
DM
1204 /**
1205 * Triggers search_results_viewed event
1206 *
1207 * Other data required:
1208 * - q: The query string
1209 * - page: The page number
1210 * - title: Title filter
1211 * - areaids: Search areas filter
1212 * - courseids: Courses filter
1213 * - timestart: Time start filter
1214 * - timeend: Time end filter
1215 *
1216 * @since Moodle 3.2
1217 * @param array $other Other info for the event.
1218 * @return \core\event\search_results_viewed
1219 */
1220 public static function trigger_search_results_viewed($other) {
1221 $event = \core\event\search_results_viewed::create([
1222 'context' => \context_system::instance(),
1223 'other' => $other
1224 ]);
1225 $event->trigger();
1226
1227 return $event;
1228 }
1229
396d6f0a
DG
1230 /**
1231 * Checks whether a classname is of an actual search area.
1232 *
f3d38863 1233 * @param string $classname
396d6f0a
DG
1234 * @return bool
1235 */
f3d38863
DM
1236 protected static function is_search_area($classname) {
1237 if (is_subclass_of($classname, 'core_search\base')) {
1238 return (new \ReflectionClass($classname))->isInstantiable();
396d6f0a
DG
1239 }
1240
1241 return false;
1242 }
eacb2bd1 1243
1244 /**
1245 * Requests that a specific context is indexed by the scheduled task. The context will be
1246 * added to a queue which is processed by the task.
1247 *
1248 * This is used after a restore to ensure that restored items are indexed, even though their
65da6840 1249 * modified time will be older than the latest indexed. It is also used by the 'Gradual reindex'
1250 * admin feature from the search areas screen.
eacb2bd1 1251 *
1252 * @param \context $context Context to index within
1253 * @param string $areaid Area to index, '' = all areas
8736fbc1 1254 * @param int $priority Priority (INDEX_PRIORITY_xx constant)
eacb2bd1 1255 */
8736fbc1 1256 public static function request_index(\context $context, $areaid = '',
1257 $priority = self::INDEX_PRIORITY_NORMAL) {
eacb2bd1 1258 global $DB;
1259
1260 // Check through existing requests for this context or any parent context.
1261 list ($contextsql, $contextparams) = $DB->get_in_or_equal(
1262 $context->get_parent_context_ids(true));
1263 $existing = $DB->get_records_select('search_index_requests',
8736fbc1 1264 'contextid ' . $contextsql, $contextparams, '',
1265 'id, searcharea, partialarea, indexpriority');
eacb2bd1 1266 foreach ($existing as $rec) {
1267 // If we haven't started processing the existing request yet, and it covers the same
1268 // area (or all areas) then that will be sufficient so don't add anything else.
1269 if ($rec->partialarea === '' && ($rec->searcharea === $areaid || $rec->searcharea === '')) {
8736fbc1 1270 // If the existing request has the same (or higher) priority, no need to add anything.
1271 if ($rec->indexpriority >= $priority) {
1272 return;
1273 }
1274 // The existing request has lower priority. If it is exactly the same, then just
1275 // adjust the priority of the existing request.
1276 if ($rec->searcharea === $areaid) {
1277 $DB->set_field('search_index_requests', 'indexpriority', $priority,
1278 ['id' => $rec->id]);
1279 return;
1280 }
1281 // The existing request would cover this area but is a lower priority. We need to
1282 // add the new request even though that means we will index part of it twice.
eacb2bd1 1283 }
1284 }
1285
1286 // No suitable existing request, so add a new one.
1287 $newrecord = [ 'contextid' => $context->id, 'searcharea' => $areaid,
8736fbc1 1288 'timerequested' => (int)self::get_current_time(),
1289 'partialarea' => '', 'partialtime' => 0,
1290 'indexpriority' => $priority ];
eacb2bd1 1291 $DB->insert_record('search_index_requests', $newrecord);
1292 }
74b7a42d 1293
1294 /**
8736fbc1 1295 * Processes outstanding index requests. This will take the first item from the queue (taking
1296 * account the indexing priority) and process it, continuing until an optional time limit is
1297 * reached.
74b7a42d 1298 *
1299 * If there are no index requests, the function will do nothing.
1300 *
1301 * @param float $timelimit Time limit (0 = none)
1302 * @param \progress_trace|null $progress Optional progress indicator
1303 */
1304 public function process_index_requests($timelimit = 0.0, \progress_trace $progress = null) {
1305 global $DB;
1306
1307 if (!$progress) {
1308 $progress = new \null_progress_trace();
1309 }
1310
82735dec 1311 $before = self::get_current_time();
74b7a42d 1312 if ($timelimit) {
1313 $stopat = $before + $timelimit;
1314 }
1315 while (true) {
1316 // Retrieve first request, using fully defined ordering.
1317 $requests = $DB->get_records('search_index_requests', null,
8736fbc1 1318 'indexpriority DESC, timerequested, contextid, searcharea',
74b7a42d 1319 'id, contextid, searcharea, partialarea, partialtime', 0, 1);
1320 if (!$requests) {
1321 // If there are no more requests, stop.
74b7a42d 1322 break;
1323 }
1324 $request = reset($requests);
1325
1326 // Calculate remaining time.
1327 $remainingtime = 0;
82735dec 1328 $beforeindex = self::get_current_time();
74b7a42d 1329 if ($timelimit) {
1330 $remainingtime = $stopat - $beforeindex;
8736fbc1 1331
1332 // If the time limit expired already, stop now. (Otherwise we might accidentally
1333 // index with no time limit or a negative time limit.)
1334 if ($remainingtime <= 0) {
1335 break;
1336 }
74b7a42d 1337 }
1338
1339 // Show a message before each request, indicating what will be indexed.
a539e560 1340 $context = \context::instance_by_id($request->contextid, IGNORE_MISSING);
1341 if (!$context) {
1342 $DB->delete_records('search_index_requests', ['id' => $request->id]);
1343 $progress->output('Skipped deleted context: ' . $request->contextid);
1344 continue;
1345 }
74b7a42d 1346 $contextname = $context->get_context_name();
1347 if ($request->searcharea) {
1348 $contextname .= ' (search area: ' . $request->searcharea . ')';
1349 }
1350 $progress->output('Indexing requested context: ' . $contextname);
1351
1352 // Actually index the context.
1353 $result = $this->index_context($context, $request->searcharea, $remainingtime,
1354 $progress, $request->partialarea, $request->partialtime);
1355
1356 // Work out shared part of message.
82735dec 1357 $endmessage = $contextname . ' (' . round(self::get_current_time() - $beforeindex, 1) . 's)';
74b7a42d 1358
1359 // Update database table and continue/stop as appropriate.
1360 if ($result->complete) {
1361 // If we completed the request, remove it from the table.
1362 $DB->delete_records('search_index_requests', ['id' => $request->id]);
1363 $progress->output('Completed requested context: ' . $endmessage);
1364 } else {
1365 // If we didn't complete the request, store the partial details (how far it got).
1366 $DB->update_record('search_index_requests', ['id' => $request->id,
1367 'partialarea' => $result->startfromarea,
1368 'partialtime' => $result->startfromtime]);
1369 $progress->output('Ending requested context: ' . $endmessage);
1370
1371 // The time limit must have expired, so stop looping.
1372 break;
1373 }
1374 }
1375 }
1376
65da6840 1377 /**
1378 * Gets information about the request queue, in the form of a plain object suitable for passing
1379 * to a template for rendering.
1380 *
1381 * @return \stdClass Information about queued index requests
1382 */
1383 public function get_index_requests_info() {
1384 global $DB;
1385
1386 $result = new \stdClass();
1387
1388 $result->total = $DB->count_records('search_index_requests');
1389 $result->topten = $DB->get_records('search_index_requests', null,
1390 'indexpriority DESC, timerequested, contextid, searcharea',
1391 'id, contextid, timerequested, searcharea, partialarea, partialtime, indexpriority',
1392 0, 10);
1393 foreach ($result->topten as $item) {
1394 $context = \context::instance_by_id($item->contextid);
1395 $item->contextlink = \html_writer::link($context->get_url(),
1396 s($context->get_context_name()));
1397 if ($item->searcharea) {
1398 $item->areaname = $this->get_search_area($item->searcharea)->get_visible_name();
1399 }
1400 if ($item->partialarea) {
1401 $item->partialareaname = $this->get_search_area($item->partialarea)->get_visible_name();
1402 }
1403 switch ($item->indexpriority) {
1404 case self::INDEX_PRIORITY_REINDEXING :
1405 $item->priorityname = get_string('priority_reindexing', 'search');
1406 break;
1407 case self::INDEX_PRIORITY_NORMAL :
1408 $item->priorityname = get_string('priority_normal', 'search');
1409 break;
1410 }
1411 }
1412
1413 // Normalise array indices.
1414 $result->topten = array_values($result->topten);
1415
1416 if ($result->total > 10) {
1417 $result->ellipsis = true;
1418 }
1419
1420 return $result;
1421 }
1422
82735dec 1423 /**
1424 * Gets current time for use in search system.
1425 *
1426 * Note: This should be replaced with generic core functionality once possible (see MDL-60644).
1427 *
1428 * @return float Current time in seconds (with decimals)
1429 */
1430 public static function get_current_time() {
1431 if (PHPUNIT_TEST && self::$phpunitfaketime) {
1432 return self::$phpunitfaketime;
1433 }
1434 return microtime(true);
1435 }
db48207e 1436}