Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
25
namespace core_search;
26
 
27
defined('MOODLE_INTERNAL') || die;
28
 
29
require_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
 */
38
class manager {
39
 
40
    /**
41
     * @var int Text contents.
42
     */
43
    const TYPE_TEXT = 1;
44
 
45
    /**
46
     * @var int File contents.
47
     */
48
    const TYPE_FILE = 2;
49
 
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
 
75
    /**
76
     * @var int The id to be placed in owneruserid when there is no owner.
77
     */
78
    const NO_OWNER_ID = 0;
79
 
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
 
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
 
100
    /**
101
     * @var string Core search area category for all results.
102
     */
103
    const SEARCH_AREA_CATEGORY_ALL = 'core-all';
104
 
105
    /**
106
     * @var string Core search area category for course content.
107
     */
108
    const SEARCH_AREA_CATEGORY_COURSE_CONTENT = 'core-course-content';
109
 
110
    /**
111
     * @var string Core search area category for courses.
112
     */
113
    const SEARCH_AREA_CATEGORY_COURSES = 'core-courses';
114
 
115
    /**
116
     * @var string Core search area category for users.
117
     */
118
    const SEARCH_AREA_CATEGORY_USERS = 'core-users';
119
 
120
    /**
121
     * @var string Core search area category for results that do not fit into any of existing categories.
122
     */
123
    const SEARCH_AREA_CATEGORY_OTHER = 'core-other';
124
 
125
    /**
1441 ariadna 126
     * @var int To avoid race conditions, do not index documents newer than this many seconds.
127
     */
128
    const INDEXING_DELAY = 5;
129
 
130
    /**
1 efrain 131
     * @var \core_search\base[] Enabled search areas.
132
     */
133
    protected static $enabledsearchareas = null;
134
 
135
    /**
136
     * @var \core_search\base[] All system search areas.
137
     */
138
    protected static $allsearchareas = null;
139
 
140
    /**
141
     * @var \core_search\area_category[] A list of search area categories.
142
     */
143
    protected static $searchareacategories = null;
144
 
145
    /**
146
     * @var \core_search\manager
147
     */
148
    protected static $instance = null;
149
 
150
    /**
151
     * @var array IDs (as keys) of course deletions in progress in this requuest, if any.
152
     */
153
    protected static $coursedeleting = [];
154
 
155
    /**
156
     * @var \core_search\engine
157
     */
158
    protected $engine = null;
159
 
160
    /**
161
     * Note: This should be removed once possible (see MDL-60644).
162
     *
163
     * @var float Fake current time for use in PHPunit tests
164
     */
165
    protected static $phpunitfaketime = 0;
166
 
167
    /**
168
     * @var int Result count when used with mock results for Behat tests.
169
     */
170
    protected $behatresultcount = 0;
171
 
172
    /**
173
     * Constructor, use \core_search\manager::instance instead to get a class instance.
174
     *
175
     * @param \core_search\base The search engine to use
176
     */
177
    public function __construct($engine) {
178
        $this->engine = $engine;
179
    }
180
 
181
    /**
182
     * @var int Record time of each successful schema check, but not more than once per 10 minutes.
183
     */
184
    const SCHEMA_CHECK_TRACKING_DELAY = 10 * 60;
185
 
186
    /**
187
     * @var int Require a new schema check at least every 4 hours.
188
     */
189
    const SCHEMA_CHECK_REQUIRED_EVERY = 4 * 3600;
190
 
191
    /**
192
     * Returns an initialised \core_search instance.
193
     *
194
     * While constructing the instance, checks on the search schema may be carried out. The $fast
195
     * parameter provides a way to skip those checks on pages which are used frequently. It has
196
     * no effect if an instance has already been constructed in this request.
197
     *
198
     * The $query parameter indicates that the page is used for queries rather than indexing. If
199
     * configured, this will cause the query-only search engine to be used instead of the 'normal'
200
     * one.
201
     *
202
     * @see \core_search\engine::is_installed
203
     * @see \core_search\engine::is_server_ready
204
     * @param bool $fast Set to true when calling on a page that requires high performance
205
     * @param bool $query Set true on a page that is used for querying
206
     * @throws \core_search\engine_exception
207
     * @return \core_search\manager
208
     */
209
    public static function instance(bool $fast = false, bool $query = false) {
210
        global $CFG;
211
 
212
        // One per request, this should be purged during testing.
213
        if (static::$instance !== null) {
214
            return static::$instance;
215
        }
216
 
217
        if (empty($CFG->searchengine)) {
218
            throw new \core_search\engine_exception('enginenotselected', 'search');
219
        }
220
 
221
        if (!$engine = static::search_engine_instance($query)) {
222
            throw new \core_search\engine_exception('enginenotfound', 'search', '', $CFG->searchengine);
223
        }
224
 
225
        // Get time now and at last schema check.
226
        $now = (int)self::get_current_time();
227
        $lastschemacheck = get_config($engine->get_plugin_name(), 'lastschemacheck');
228
 
229
        // On pages where performance matters, tell the engine to skip schema checks.
230
        $skipcheck = false;
231
        if ($fast && $now < $lastschemacheck + self::SCHEMA_CHECK_REQUIRED_EVERY) {
232
            $skipcheck = true;
233
            $engine->skip_schema_check();
234
        }
235
 
236
        if (!$engine->is_installed()) {
237
            throw new \core_search\engine_exception('enginenotinstalled', 'search', '', $CFG->searchengine);
238
        }
239
 
240
        $serverstatus = $engine->is_server_ready();
241
        if ($serverstatus !== true) {
242
            // Skip this error in Behat when faking seach results.
243
            if (!defined('BEHAT_SITE_RUNNING') || !get_config('core_search', 'behat_fakeresult')) {
244
                // Clear the record of successful schema checks since it might have failed.
245
                unset_config('lastschemacheck', $engine->get_plugin_name());
246
                // Error message with no details as this is an exception that any user may find if the server crashes.
247
                throw new \core_search\engine_exception('engineserverstatus', 'search');
248
            }
249
        }
250
 
251
        // If we did a successful schema check, record this, but not more than once per 10 minutes
252
        // (to avoid updating the config db table/cache too often in case it gets called frequently).
253
        if (!$skipcheck && $now >= $lastschemacheck + self::SCHEMA_CHECK_TRACKING_DELAY) {
254
            set_config('lastschemacheck', $now, $engine->get_plugin_name());
255
        }
256
 
257
        static::$instance = new \core_search\manager($engine);
258
        return static::$instance;
259
    }
260
 
261
    /**
262
     * Returns whether global search is enabled or not.
263
     *
264
     * @return bool
265
     */
266
    public static function is_global_search_enabled() {
267
        global $CFG;
268
        return !empty($CFG->enableglobalsearch);
269
    }
270
 
271
    /**
272
     * Tests if global search is configured to be equivalent to the front page course search.
273
     *
274
     * @return bool
275
     */
276
    public static function can_replace_course_search(): bool {
277
        global $CFG;
278
 
279
        // Assume we can replace front page search.
280
        $canreplace = true;
281
 
282
        // Global search must be enabled.
283
        if (!static::is_global_search_enabled()) {
284
            $canreplace = false;
285
        }
286
 
287
        // Users must be able to search the details of all courses that they can see,
288
        // even if they do not have access to them.
289
        if (empty($CFG->searchincludeallcourses)) {
290
            $canreplace = false;
291
        }
292
 
293
        // Course search must be enabled.
294
        if ($canreplace) {
295
            $areaid = static::generate_areaid('core_course', 'course');
296
            $enabledareas = static::get_search_areas_list(true);
297
            $canreplace = isset($enabledareas[$areaid]);
298
        }
299
 
300
        return $canreplace;
301
    }
302
 
303
    /**
304
     * Returns the search URL for course search
305
     *
306
     * @return moodle_url
307
     */
308
    public static function get_course_search_url() {
309
        if (self::can_replace_course_search()) {
310
            $searchurl = '/search/index.php';
311
        } else {
312
            $searchurl = '/course/search.php';
313
        }
314
 
315
        return new \moodle_url($searchurl);
316
    }
317
 
318
    /**
319
     * Returns whether indexing is enabled or not (you can enable indexing even when search is not
320
     * enabled at the moment, so as to have it ready for students).
321
     *
322
     * @return bool True if indexing is enabled.
323
     */
324
    public static function is_indexing_enabled() {
325
        global $CFG;
326
        return !empty($CFG->enableglobalsearch) || !empty($CFG->searchindexwhendisabled);
327
    }
328
 
329
    /**
330
     * Returns an instance of the search engine.
331
     *
332
     * @param bool $query If true, gets the query-only search engine (where configured)
333
     * @return \core_search\engine
334
     */
335
    public static function search_engine_instance(bool $query = false) {
336
        global $CFG;
337
 
338
        if ($query && $CFG->searchenginequeryonly) {
339
            return self::search_engine_instance_from_setting($CFG->searchenginequeryonly);
340
        } else {
341
            return self::search_engine_instance_from_setting($CFG->searchengine);
342
        }
343
    }
344
 
345
    /**
346
     * Loads a search engine based on the name given in settings, which can optionally
347
     * include '-alternate' to indicate that an alternate version should be used.
348
     *
349
     * @param string $setting
350
     * @return engine|null
351
     */
352
    protected static function search_engine_instance_from_setting(string $setting): ?engine {
353
        if (preg_match('~^(.*)-alternate$~', $setting, $matches)) {
354
            $enginename = $matches[1];
355
            $alternate = true;
356
        } else {
357
            $enginename = $setting;
358
            $alternate = false;
359
        }
360
 
361
        $classname = '\\search_' . $enginename . '\\engine';
362
        if (!class_exists($classname)) {
363
            return null;
364
        }
365
 
366
        if ($alternate) {
367
            return new $classname(true);
368
        } else {
369
            // Use the constructor with no parameters for compatibility.
370
            return new $classname();
371
        }
372
    }
373
 
374
    /**
375
     * Returns the search engine.
376
     *
377
     * @return \core_search\engine
378
     */
379
    public function get_engine() {
380
        return $this->engine;
381
    }
382
 
383
    /**
384
     * Returns a search area class name.
385
     *
386
     * @param string $areaid
387
     * @return string
388
     */
389
    protected static function get_area_classname($areaid) {
390
        list($componentname, $areaname) = static::extract_areaid_parts($areaid);
391
        return '\\' . $componentname . '\\search\\' . $areaname;
392
    }
393
 
394
    /**
395
     * Returns a new area search indexer instance.
396
     *
397
     * @param string $areaid
398
     * @return \core_search\base|bool False if the area is not available.
399
     */
400
    public static function get_search_area($areaid) {
401
 
402
        // We have them all here.
403
        if (!empty(static::$allsearchareas[$areaid])) {
404
            return static::$allsearchareas[$areaid];
405
        }
406
 
407
        $classname = static::get_area_classname($areaid);
408
 
409
        if (class_exists($classname) && static::is_search_area($classname)) {
410
            return new $classname();
411
        }
412
 
413
        return false;
414
    }
415
 
416
    /**
417
     * Return the list of available search areas.
418
     *
419
     * @param bool $enabled Return only the enabled ones.
420
     * @return \core_search\base[]
421
     */
422
    public static function get_search_areas_list($enabled = false) {
423
 
424
        // Two different arrays, we don't expect these arrays to be big.
425
        if (static::$allsearchareas !== null) {
426
            if (!$enabled) {
427
                return static::$allsearchareas;
428
            } else {
429
                return static::$enabledsearchareas;
430
            }
431
        }
432
 
433
        static::$allsearchareas = array();
434
        static::$enabledsearchareas = array();
435
        $searchclasses = \core_component::get_component_classes_in_namespace(null, 'search');
436
 
437
        foreach ($searchclasses as $classname => $classpath) {
438
            $areaname = substr(strrchr($classname, '\\'), 1);
439
            $componentname = strstr($classname, '\\', 1);
440
            if (!static::is_search_area($classname)) {
441
                continue;
442
            }
443
 
444
            $areaid = static::generate_areaid($componentname, $areaname);
445
            $searchclass = new $classname();
446
            static::$allsearchareas[$areaid] = $searchclass;
447
            if ($searchclass->is_enabled()) {
448
                static::$enabledsearchareas[$areaid] = $searchclass;
449
            }
450
        }
451
 
452
        if ($enabled) {
453
            return static::$enabledsearchareas;
454
        }
455
        return static::$allsearchareas;
456
    }
457
 
458
    /**
459
     * Return search area category instance by category name.
460
     *
461
     * @param string $name Category name. If name is not valid will return default category.
462
     *
463
     * @return \core_search\area_category
464
     */
465
    public static function get_search_area_category_by_name($name) {
466
        if (key_exists($name, self::get_search_area_categories())) {
467
            return self::get_search_area_categories()[$name];
468
        } else {
469
            return self::get_search_area_categories()[self::get_default_area_category_name()];
470
        }
471
    }
472
 
473
    /**
474
     * Return a list of existing search area categories.
475
     *
476
     * @return \core_search\area_category[]
477
     */
478
    public static function get_search_area_categories() {
479
        if (!isset(static::$searchareacategories)) {
480
            $categories = self::get_core_search_area_categories();
481
 
482
            // Go through all existing search areas and get categories they are assigned to.
483
            $areacategories = [];
484
            foreach (self::get_search_areas_list() as $searcharea) {
485
                foreach ($searcharea->get_category_names() as $categoryname) {
486
                    if (!key_exists($categoryname, $areacategories)) {
487
                        $areacategories[$categoryname] = [];
488
                    }
489
 
490
                    $areacategories[$categoryname][] = $searcharea;
491
                }
492
            }
493
 
494
            // Populate core categories by areas.
495
            foreach ($areacategories as $name => $searchareas) {
496
                if (key_exists($name, $categories)) {
497
                    $categories[$name]->set_areas($searchareas);
498
                } else {
499
                    throw new \coding_exception('Unknown core search area category ' . $name);
500
                }
501
            }
502
 
503
            // Get additional categories.
504
            $additionalcategories = self::get_additional_search_area_categories();
505
            foreach ($additionalcategories as $additionalcategory) {
506
                if (!key_exists($additionalcategory->get_name(), $categories)) {
507
                    $categories[$additionalcategory->get_name()] = $additionalcategory;
508
                }
509
            }
510
 
511
            // Remove categories without areas.
512
            foreach ($categories as $key => $category) {
513
                if (empty($category->get_areas())) {
514
                    unset($categories[$key]);
515
                }
516
            }
517
 
518
            // Sort categories by order.
519
            uasort($categories, function($category1, $category2) {
520
                return $category1->get_order() <=> $category2->get_order();
521
            });
522
 
523
            static::$searchareacategories = $categories;
524
        }
525
 
526
        return static::$searchareacategories;
527
    }
528
 
529
    /**
530
     * Get list of core search area categories.
531
     *
532
     * @return \core_search\area_category[]
533
     */
534
    protected static function get_core_search_area_categories() {
535
        $categories = [];
536
 
537
        $categories[self::SEARCH_AREA_CATEGORY_ALL] = new area_category(
538
            self::SEARCH_AREA_CATEGORY_ALL,
539
            get_string('core-all', 'search'),
540
            0,
541
            self::get_search_areas_list(true)
542
        );
543
 
544
        $categories[self::SEARCH_AREA_CATEGORY_COURSE_CONTENT] = new area_category(
545
            self::SEARCH_AREA_CATEGORY_COURSE_CONTENT,
546
            get_string('core-course-content', 'search'),
547
            1
548
        );
549
 
550
        $categories[self::SEARCH_AREA_CATEGORY_COURSES] = new area_category(
551
            self::SEARCH_AREA_CATEGORY_COURSES,
552
            get_string('core-courses', 'search'),
553
            2
554
        );
555
 
556
        $categories[self::SEARCH_AREA_CATEGORY_USERS] = new area_category(
557
            self::SEARCH_AREA_CATEGORY_USERS,
558
            get_string('core-users', 'search'),
559
            3
560
        );
561
 
562
        $categories[self::SEARCH_AREA_CATEGORY_OTHER] = new area_category(
563
            self::SEARCH_AREA_CATEGORY_OTHER,
564
            get_string('core-other', 'search'),
565
            4
566
        );
567
 
568
        return $categories;
569
    }
570
 
571
    /**
572
     * Gets a list of additional search area categories.
573
     *
574
     * @return \core_search\area_category[]
575
     */
576
    protected static function get_additional_search_area_categories() {
577
        $additionalcategories = [];
578
 
579
        // Allow plugins to add custom search area categories.
580
        if ($pluginsfunction = get_plugins_with_function('search_area_categories')) {
581
            foreach ($pluginsfunction as $plugintype => $plugins) {
582
                foreach ($plugins as $pluginfunction) {
583
                    $plugincategories = $pluginfunction();
584
                    // We're expecting a list of valid area categories.
585
                    if (is_array($plugincategories)) {
586
                        foreach ($plugincategories as $plugincategory) {
587
                            if (self::is_valid_area_category($plugincategory)) {
588
                                $additionalcategories[] = $plugincategory;
589
                            } else {
590
                                throw  new \coding_exception('Invalid search area category!');
591
                            }
592
                        }
593
                    } else {
594
                        throw  new \coding_exception($pluginfunction . ' should return a list of search area categories!');
595
                    }
596
                }
597
            }
598
        }
599
 
600
        return $additionalcategories;
601
    }
602
 
603
    /**
604
     * Check if provided instance of area category is valid.
605
     *
606
     * @param mixed $areacategory Area category instance. Potentially could be anything.
607
     *
608
     * @return bool
609
     */
610
    protected static function is_valid_area_category($areacategory) {
611
        return $areacategory instanceof area_category;
612
    }
613
 
614
    /**
615
     * Clears all static caches.
616
     *
617
     * @return void
618
     */
619
    public static function clear_static() {
620
 
621
        static::$enabledsearchareas = null;
622
        static::$allsearchareas = null;
623
        static::$instance = null;
624
        static::$searchareacategories = null;
625
        static::$coursedeleting = [];
626
        static::$phpunitfaketime = null;
627
 
628
        base_block::clear_static();
629
        engine::clear_users_cache();
630
    }
631
 
632
    /**
633
     * Generates an area id from the componentname and the area name.
634
     *
635
     * There should not be any naming conflict as the area name is the
636
     * class name in component/classes/search/.
637
     *
638
     * @param string $componentname
639
     * @param string $areaname
640
     * @return void
641
     */
642
    public static function generate_areaid($componentname, $areaname) {
643
        return $componentname . '-' . $areaname;
644
    }
645
 
646
    /**
647
     * Returns all areaid string components (component name and area name).
648
     *
649
     * @param string $areaid
650
     * @return array Component name (Frankenstyle) and area name (search area class name)
651
     */
652
    public static function extract_areaid_parts($areaid) {
653
        return explode('-', $areaid);
654
    }
655
 
656
    /**
657
     * Parse a search area id and get plugin name and config name prefix from it.
658
     *
659
     * @param string $areaid Search area id.
660
     * @return array Where the first element is a plugin name and the second is config names prefix.
661
     */
662
    public static function parse_areaid($areaid) {
663
        $parts = self::extract_areaid_parts($areaid);
664
 
665
        if (empty($parts[1])) {
666
            throw new \coding_exception('Trying to parse invalid search area id ' . $areaid);
667
        }
668
 
669
        $component = $parts[0];
670
        $area = $parts[1];
671
 
672
        if (strpos($component, 'core') === 0) {
673
            $plugin = 'core_search';
674
            $configprefix = str_replace('-', '_', $areaid);
675
        } else {
676
            $plugin = $component;
677
            $configprefix = 'search_' . $area;
678
        }
679
 
680
        return [$plugin, $configprefix];
681
    }
682
 
683
    /**
684
     * Returns information about the areas which the user can access.
685
     *
686
     * The returned value is a stdClass object with the following fields:
687
     * - everything (bool, true for admin only)
688
     * - usercontexts (indexed by area identifier then context
689
     * - separategroupscontexts (contexts within which group restrictions apply)
690
     * - visiblegroupscontextsareas (overrides to the above when the same contexts also have
691
     *   'visible groups' for certain search area ids - hopefully rare)
692
     * - usergroups (groups which the current user belongs to)
693
     *
694
     * The areas can be limited by course id and context id. If specifying context ids, results
695
     * are limited to the exact context ids specified and not their children (for example, giving
696
     * the course context id would result in including search items with the course context id, and
697
     * not anything from a context inside the course). For performance, you should also specify
698
     * course id(s) when using context ids.
699
     *
700
     * @param array|false $limitcourseids An array of course ids to limit the search to. False for no limiting.
701
     * @param array|false $limitcontextids An array of context ids to limit the search to. False for no limiting.
702
     * @return \stdClass Object as described above
703
     */
704
    protected function get_areas_user_accesses($limitcourseids = false, $limitcontextids = false) {
705
        global $DB, $USER;
706
 
707
        // All results for admins (unless they have chosen to limit results). Eventually we could
708
        // add a new capability for managers.
709
        if (is_siteadmin() && !$limitcourseids && !$limitcontextids) {
710
            return (object)array('everything' => true);
711
        }
712
 
713
        $areasbylevel = array();
714
 
715
        // Split areas by context level so we only iterate only once through courses and cms.
716
        $searchareas = static::get_search_areas_list(true);
717
        foreach ($searchareas as $areaid => $unused) {
718
            $classname = static::get_area_classname($areaid);
719
            $searcharea = new $classname();
720
            foreach ($classname::get_levels() as $level) {
721
                $areasbylevel[$level][$areaid] = $searcharea;
722
            }
723
        }
724
 
725
        // This will store area - allowed contexts relations.
726
        $areascontexts = array();
727
 
728
        // Initialise two special-case arrays for storing other information related to the contexts.
729
        $separategroupscontexts = array();
730
        $visiblegroupscontextsareas = array();
731
        $usergroups = array();
732
 
733
        if (empty($limitcourseids) && !empty($areasbylevel[CONTEXT_SYSTEM])) {
734
            // We add system context to all search areas working at this level. Here each area is fully responsible of
735
            // the access control as we can not automate much, we can not even check guest access as some areas might
736
            // want to allow guests to retrieve data from them.
737
 
738
            $systemcontextid = \context_system::instance()->id;
739
            if (!$limitcontextids || in_array($systemcontextid, $limitcontextids)) {
740
                foreach ($areasbylevel[CONTEXT_SYSTEM] as $areaid => $searchclass) {
741
                    $areascontexts[$areaid][$systemcontextid] = $systemcontextid;
742
                }
743
            }
744
        }
745
 
746
        if (!empty($areasbylevel[CONTEXT_USER])) {
747
            if ($usercontext = \context_user::instance($USER->id, IGNORE_MISSING)) {
748
                if (!$limitcontextids || in_array($usercontext->id, $limitcontextids)) {
749
                    // Extra checking although only logged users should reach this point, guest users have a valid context id.
750
                    foreach ($areasbylevel[CONTEXT_USER] as $areaid => $searchclass) {
751
                        $areascontexts[$areaid][$usercontext->id] = $usercontext->id;
752
                    }
753
                }
754
            }
755
        }
756
 
757
        if (is_siteadmin()) {
758
            $allcourses = $this->get_all_courses($limitcourseids);
759
        } else {
760
            $allcourses = $mycourses = $this->get_my_courses((bool)get_config('core', 'searchallavailablecourses'));
761
 
762
            if (self::include_all_courses()) {
763
                $allcourses = $this->get_all_courses($limitcourseids);
764
            }
765
        }
766
 
767
        if (empty($limitcourseids) || in_array(SITEID, $limitcourseids)) {
768
            $allcourses[SITEID] = get_course(SITEID);
769
            if (isset($mycourses)) {
770
                $mycourses[SITEID] = get_course(SITEID);
771
            }
772
        }
773
 
774
        // Keep a list of included course context ids (needed for the block calculation below).
775
        $coursecontextids = [];
776
        $modulecms = [];
777
 
778
        foreach ($allcourses as $course) {
779
            if (!empty($limitcourseids) && !in_array($course->id, $limitcourseids)) {
780
                // Skip non-included courses.
781
                continue;
782
            }
783
 
784
            $coursecontext = \context_course::instance($course->id);
785
            $hasgrouprestrictions = false;
786
 
787
            if (!empty($areasbylevel[CONTEXT_COURSE]) &&
788
                    (!$limitcontextids || in_array($coursecontext->id, $limitcontextids))) {
789
                // Add the course contexts the user can view.
790
                foreach ($areasbylevel[CONTEXT_COURSE] as $areaid => $searchclass) {
791
                    if (!empty($mycourses[$course->id]) || \core_course_category::can_view_course_info($course)) {
792
                        $areascontexts[$areaid][$coursecontext->id] = $coursecontext->id;
793
                    }
794
                }
795
            }
796
 
797
            // Skip module context if a user can't access related course.
798
            if (isset($mycourses) && !key_exists($course->id, $mycourses)) {
799
                continue;
800
            }
801
 
802
            $coursecontextids[] = $coursecontext->id;
803
 
804
            // Info about the course modules.
805
            $modinfo = get_fast_modinfo($course);
806
 
807
            if (!empty($areasbylevel[CONTEXT_MODULE])) {
808
                // Add the module contexts the user can view (cm_info->uservisible).
809
 
810
                foreach ($areasbylevel[CONTEXT_MODULE] as $areaid => $searchclass) {
811
 
812
                    // Removing the plugintype 'mod_' prefix.
813
                    $modulename = substr($searchclass->get_component_name(), 4);
814
 
815
                    $modinstances = $modinfo->get_instances_of($modulename);
816
                    foreach ($modinstances as $modinstance) {
817
                        // Skip module context if not included in list of context ids.
818
                        if ($limitcontextids && !in_array($modinstance->context->id, $limitcontextids)) {
819
                            continue;
820
                        }
821
                        if ($modinstance->uservisible) {
822
                            $contextid = $modinstance->context->id;
823
                            $areascontexts[$areaid][$contextid] = $contextid;
824
                            $modulecms[$modinstance->id] = $modinstance;
825
 
826
                            if (!has_capability('moodle/site:accessallgroups', $modinstance->context) &&
827
                                    ($searchclass instanceof base_mod) &&
828
                                    $searchclass->supports_group_restriction()) {
829
                                if ($searchclass->restrict_cm_access_by_group($modinstance)) {
830
                                    $separategroupscontexts[$contextid] = $contextid;
831
                                    $hasgrouprestrictions = true;
832
                                } else {
833
                                    // Track a list of anything that has a group id (so might get
834
                                    // filtered) and doesn't want to be, in this context.
835
                                    if (!array_key_exists($contextid, $visiblegroupscontextsareas)) {
836
                                        $visiblegroupscontextsareas[$contextid] = array();
837
                                    }
838
                                    $visiblegroupscontextsareas[$contextid][$areaid] = $areaid;
839
                                }
840
                            }
841
                        }
842
                    }
843
                }
844
            }
845
 
846
            // Insert group information for course (unless there aren't any modules restricted by
847
            // group for this user in this course, in which case don't bother).
848
            if ($hasgrouprestrictions) {
849
                $groups = groups_get_all_groups($course->id, $USER->id, 0, 'g.id');
850
                foreach ($groups as $group) {
851
                    $usergroups[$group->id] = $group->id;
852
                }
853
            }
854
        }
855
 
856
        // Chuck away all the 'visible groups contexts' data unless there is actually something
857
        // that does use separate groups in the same context (this data is only used as an
858
        // 'override' in cases where the search is restricting to separate groups).
859
        foreach ($visiblegroupscontextsareas as $contextid => $areas) {
860
            if (!array_key_exists($contextid, $separategroupscontexts)) {
861
                unset($visiblegroupscontextsareas[$contextid]);
862
            }
863
        }
864
 
865
        // Add all supported block contexts for course contexts that user can access, in a single query for performance.
866
        if (!empty($areasbylevel[CONTEXT_BLOCK]) && !empty($coursecontextids)) {
867
            // Get list of all block types we care about.
868
            $blocklist = [];
869
            foreach ($areasbylevel[CONTEXT_BLOCK] as $areaid => $searchclass) {
870
                $blocklist[$searchclass->get_block_name()] = true;
871
            }
872
            list ($blocknamesql, $blocknameparams) = $DB->get_in_or_equal(array_keys($blocklist));
873
 
874
            // Get list of course contexts.
875
            list ($contextsql, $contextparams) = $DB->get_in_or_equal($coursecontextids);
876
 
877
            // Get list of block context (if limited).
878
            $blockcontextwhere = '';
879
            $blockcontextparams = [];
880
            if ($limitcontextids) {
881
                list ($blockcontextsql, $blockcontextparams) = $DB->get_in_or_equal($limitcontextids);
882
                $blockcontextwhere = 'AND x.id ' . $blockcontextsql;
883
            }
884
 
885
            // Query all blocks that are within an included course, and are set to be visible, and
886
            // in a supported page type (basically just course view). This query could be
887
            // extended (or a second query added) to support blocks that are within a module
888
            // context as well, and we could add more page types if required.
889
            $blockrecs = $DB->get_records_sql("
890
                        SELECT x.*, bi.blockname AS blockname, bi.id AS blockinstanceid
891
                          FROM {block_instances} bi
892
                          JOIN {context} x ON x.instanceid = bi.id AND x.contextlevel = ?
893
                     LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id
894
                               AND bp.contextid = bi.parentcontextid
895
                               AND bp.pagetype LIKE 'course-view-%'
896
                               AND bp.subpage = ''
897
                               AND bp.visible = 0
898
                         WHERE bi.parentcontextid $contextsql
899
                               $blockcontextwhere
900
                               AND bi.blockname $blocknamesql
901
                               AND bi.subpagepattern IS NULL
902
                               AND (bi.pagetypepattern = 'site-index'
903
                                   OR bi.pagetypepattern LIKE 'course-view-%'
904
                                   OR bi.pagetypepattern = 'course-*'
905
                                   OR bi.pagetypepattern = '*')
906
                               AND bp.id IS NULL",
907
                    array_merge([CONTEXT_BLOCK], $contextparams, $blockcontextparams, $blocknameparams));
908
            $blockcontextsbyname = [];
909
            foreach ($blockrecs as $blockrec) {
910
                if (empty($blockcontextsbyname[$blockrec->blockname])) {
911
                    $blockcontextsbyname[$blockrec->blockname] = [];
912
                }
913
                \context_helper::preload_from_record($blockrec);
914
                $blockcontextsbyname[$blockrec->blockname][] = \context_block::instance(
915
                        $blockrec->blockinstanceid);
916
            }
917
 
918
            // Add the block contexts the user can view.
919
            foreach ($areasbylevel[CONTEXT_BLOCK] as $areaid => $searchclass) {
920
                if (empty($blockcontextsbyname[$searchclass->get_block_name()])) {
921
                    continue;
922
                }
923
                foreach ($blockcontextsbyname[$searchclass->get_block_name()] as $context) {
924
                    if (has_capability('moodle/block:view', $context)) {
925
                        $areascontexts[$areaid][$context->id] = $context->id;
926
                    }
927
                }
928
            }
929
        }
930
 
931
        // Return all the data.
932
        return (object)array('everything' => false, 'usercontexts' => $areascontexts,
933
                'separategroupscontexts' => $separategroupscontexts, 'usergroups' => $usergroups,
934
                'visiblegroupscontextsareas' => $visiblegroupscontextsareas);
935
    }
936
 
937
    /**
938
     * Returns requested page of documents plus additional information for paging.
939
     *
940
     * This function does not perform any kind of security checking for access, the caller code
941
     * should check that the current user have moodle/search:query capability.
942
     *
943
     * If a page is requested that is beyond the last result, the last valid page is returned in
944
     * results, and actualpage indicates which page was returned.
945
     *
946
     * @param stdClass $formdata
947
     * @param int $pagenum The 0 based page number.
948
     * @return object An object with 3 properties:
949
     *                    results    => An array of \core_search\documents for the actual page.
950
     *                    totalcount => Number of records that are possibly available, to base paging on.
951
     *                    actualpage => The actual page returned.
952
     */
953
    public function paged_search(\stdClass $formdata, $pagenum) {
954
        $out = new \stdClass();
955
 
956
        if (self::is_search_area_categories_enabled() && !empty($formdata->cat)) {
957
            $cat = self::get_search_area_category_by_name($formdata->cat);
958
            if (empty($formdata->areaids)) {
959
                $formdata->areaids = array_keys($cat->get_areas());
960
            } else {
961
                foreach ($formdata->areaids as $key => $areaid) {
962
                    if (!key_exists($areaid, $cat->get_areas())) {
963
                        unset($formdata->areaids[$key]);
964
                    }
965
                }
966
            }
967
        }
968
 
969
        $perpage = static::DISPLAY_RESULTS_PER_PAGE;
970
 
971
        // Make sure we only allow request up to max page.
972
        $pagenum = min($pagenum, (static::MAX_RESULTS / $perpage) - 1);
973
 
974
        // Calculate the first and last document number for the current page, 1 based.
975
        $mindoc = ($pagenum * $perpage) + 1;
976
        $maxdoc = ($pagenum + 1) * $perpage;
977
 
978
        // Get engine documents, up to max.
979
        $docs = $this->search($formdata, $maxdoc);
980
 
981
        $resultcount = count($docs);
982
        if ($resultcount < $maxdoc) {
983
            // This means it couldn't give us results to max, so the count must be the max.
984
            $out->totalcount = $resultcount;
985
        } else {
986
            // Get the possible count reported by engine, and limit to our max.
987
            $out->totalcount = $this->engine->get_query_total_count();
988
            if (defined('BEHAT_SITE_RUNNING') && $this->behatresultcount) {
989
                // Override results when using Behat mock results.
990
                $out->totalcount = $this->behatresultcount;
991
            }
992
            $out->totalcount = min($out->totalcount, static::MAX_RESULTS);
993
        }
994
 
995
        // Determine the actual page.
996
        if ($resultcount < $mindoc) {
997
            // We couldn't get the min docs for this page, so determine what page we can get.
998
            $out->actualpage = floor(($resultcount - 1) / $perpage);
999
        } else {
1000
            $out->actualpage = $pagenum;
1001
        }
1002
 
1003
        // Split the results to only return the page.
1004
        $out->results = array_slice($docs, $out->actualpage * $perpage, $perpage, true);
1005
 
1006
        return $out;
1007
    }
1008
 
1009
    /**
1010
     * Returns documents from the engine based on the data provided.
1011
     *
1012
     * This function does not perform any kind of security checking, the caller code
1013
     * should check that the current user have moodle/search:query capability.
1014
     *
1015
     * It might return the results from the cache instead.
1016
     *
1017
     * Valid formdata options include:
1018
     * - q (query text)
1019
     * - courseids (optional list of course ids to restrict)
1020
     * - contextids (optional list of context ids to restrict)
1021
     * - context (Moodle context object for location user searched from)
1022
     * - order (optional ordering, one of the types supported by the search engine e.g. 'relevance')
1023
     * - userids (optional list of user ids to restrict)
1024
     *
1025
     * @param \stdClass $formdata Query input data (usually from search form)
1026
     * @param int $limit The maximum number of documents to return
1027
     * @return \core_search\document[]
1028
     */
1029
    public function search(\stdClass $formdata, $limit = 0) {
1030
        // For Behat testing, the search results can be faked using a special step.
1031
        if (defined('BEHAT_SITE_RUNNING')) {
1032
            $fakeresult = get_config('core_search', 'behat_fakeresult');
1033
            if ($fakeresult) {
1034
                // Clear config setting.
1035
                unset_config('core_search', 'behat_fakeresult');
1036
 
1037
                // Check query matches expected value.
1038
                $details = json_decode($fakeresult);
1039
                if ($formdata->q !== $details->query) {
1040
                    throw new \coding_exception('Unexpected search query: ' . $formdata->q);
1041
                }
1042
 
1043
                // Create search documents from the JSON data.
1044
                $docs = [];
1045
                foreach ($details->results as $result) {
1046
                    $doc = new \core_search\document($result->itemid, $result->componentname,
1047
                            $result->areaname);
1048
                    foreach ((array)$result->fields as $field => $value) {
1049
                        $doc->set($field, $value);
1050
                    }
1051
                    foreach ((array)$result->extrafields as $field => $value) {
1052
                        $doc->set_extra($field, $value);
1053
                    }
1054
                    $area = $this->get_search_area($doc->get('areaid'));
1055
                    $doc->set_doc_url($area->get_doc_url($doc));
1056
                    $doc->set_context_url($area->get_context_url($doc));
1057
                    $docs[] = $doc;
1058
                }
1059
 
1060
                // Store the mock count, and apply the limit to the returned results.
1061
                $this->behatresultcount = count($docs);
1062
                if ($this->behatresultcount > $limit) {
1063
                    $docs = array_slice($docs, 0, $limit);
1064
                }
1065
 
1066
                return $docs;
1067
            }
1068
        }
1069
 
1070
        $limitcourseids = $this->build_limitcourseids($formdata);
1071
 
1072
        $limitcontextids = false;
1073
        if (!empty($formdata->contextids)) {
1074
            $limitcontextids = $formdata->contextids;
1075
        }
1076
 
1077
        // Clears previous query errors.
1078
        $this->engine->clear_query_error();
1079
 
1080
        $contextinfo = $this->get_areas_user_accesses($limitcourseids, $limitcontextids);
1081
        if (!$contextinfo->everything && !$contextinfo->usercontexts) {
1082
            // User can not access any context.
1083
            $docs = array();
1084
        } else {
1085
            // If engine does not support groups, remove group information from the context info -
1086
            // use the old format instead (true = admin, array = user contexts).
1087
            if (!$this->engine->supports_group_filtering()) {
1088
                $contextinfo = $contextinfo->everything ? true : $contextinfo->usercontexts;
1089
            }
1090
 
1091
            // Execute the actual query.
1092
            $docs = $this->engine->execute_query($formdata, $contextinfo, $limit);
1093
        }
1094
 
1095
        return $docs;
1096
    }
1097
 
1098
    /**
1099
     * Search for top ranked result.
1100
     * @param \stdClass $formdata search query data
1101
     * @return array|document[]
1102
     */
1103
    public function search_top(\stdClass $formdata): array {
1104
        global $USER;
1105
 
1106
        // Return if the config value is set to 0.
1107
        $maxtopresult = get_config('core', 'searchmaxtopresults');
1108
        if (empty($maxtopresult)) {
1109
            return [];
1110
        }
1111
 
1112
        // Only process if 'searchenablecategories' is set.
1113
        if (self::is_search_area_categories_enabled() && !empty($formdata->cat)) {
1114
            $cat = self::get_search_area_category_by_name($formdata->cat);
1115
            $formdata->areaids = array_keys($cat->get_areas());
1116
        } else {
1117
            return [];
1118
        }
1119
        $docs = $this->search($formdata);
1120
 
1121
        // Look for course, teacher and course content.
1122
        $coursedocs = [];
1123
        $courseteacherdocs = [];
1124
        $coursecontentdocs = [];
1125
        $otherdocs = [];
1126
        foreach ($docs as $doc) {
1127
            if ($doc->get('areaid') === 'core_course-course' && stripos($doc->get('title'), $formdata->q) !== false) {
1128
                $coursedocs[] = $doc;
1129
            } else if (strpos($doc->get('areaid'), 'course_teacher') !== false
1130
                && stripos($doc->get('content'), $formdata->q) !== false) {
1131
                $courseteacherdocs[] = $doc;
1132
            } else if (strpos($doc->get('areaid'), 'mod_') !== false) {
1133
                $coursecontentdocs[] = $doc;
1134
            } else {
1135
                $otherdocs[] = $doc;
1136
            }
1137
        }
1138
 
1139
        // Swap current courses to top.
1140
        $enroledcourses = $this->get_my_courses(false);
1141
        // Move current courses of the user to top.
1142
        foreach ($enroledcourses as $course) {
1143
            $completion = new \completion_info($course);
1144
            if (!$completion->is_course_complete($USER->id)) {
1145
                foreach ($coursedocs as $index => $doc) {
1146
                    $areaid = $doc->get('areaid');
1147
                    if ($areaid == 'core_course-course' && $course->id == $doc->get('courseid')) {
1148
                        unset($coursedocs[$index]);
1149
                        array_unshift($coursedocs, $doc);
1150
                    }
1151
                }
1152
            }
1153
        }
1154
 
1155
        $maxtopresult = get_config('core', 'searchmaxtopresults');
1156
        $result = array_merge($coursedocs, $courseteacherdocs, $coursecontentdocs, $otherdocs);
1157
        return array_slice($result, 0, $maxtopresult);
1158
    }
1159
 
1160
    /**
1161
     * Build a list of course ids to limit the search based on submitted form data.
1162
     *
1163
     * @param \stdClass $formdata Submitted search form data.
1164
     *
1165
     * @return array|bool
1166
     */
1167
    protected function build_limitcourseids(\stdClass $formdata) {
1168
        $limitcourseids = false;
1169
 
1170
        if (!empty($formdata->mycoursesonly)) {
1171
            $limitcourseids = array_keys($this->get_my_courses(false));
1172
        }
1173
 
1174
        if (!empty($formdata->courseids)) {
1175
            if (empty($limitcourseids)) {
1176
                $limitcourseids = $formdata->courseids;
1177
            } else {
1178
                $limitcourseids = array_intersect($limitcourseids, $formdata->courseids);
1179
            }
1180
        }
1181
 
1182
        return $limitcourseids;
1183
    }
1184
 
1185
    /**
1186
     * Merge separate index segments into one.
1187
     */
1188
    public function optimize_index() {
1189
        $this->engine->optimize();
1190
    }
1191
 
1192
    /**
1193
     * Index all documents.
1194
     *
1195
     * @param bool $fullindex Whether we should reindex everything or not.
1196
     * @param float $timelimit Time limit in seconds (0 = no time limit)
1197
     * @param \progress_trace|null $progress Optional class for tracking progress
1198
     * @throws \moodle_exception
1199
     * @return bool Whether there was any updated document or not.
1200
     */
1441 ariadna 1201
    public function index($fullindex = false, $timelimit = 0, ?\progress_trace $progress = null) {
1202
        global $DB, $CFG;
1 efrain 1203
 
1204
        // Cannot combine time limit with reindex.
1205
        if ($timelimit && $fullindex) {
1206
            throw new \coding_exception('Cannot apply time limit when reindexing');
1207
        }
1208
        if (!$progress) {
1209
            $progress = new \null_progress_trace();
1210
        }
1211
 
1212
        // Unlimited time.
1213
        \core_php_time_limit::raise();
1214
 
1215
        // Notify the engine that an index starting.
1216
        $this->engine->index_starting($fullindex);
1217
 
1218
        $sumdocs = 0;
1219
 
1220
        $searchareas = $this->get_search_areas_list(true);
1221
 
1222
        if ($timelimit) {
1223
            // If time is limited (and therefore we're not just indexing everything anyway), select
1224
            // an order for search areas. The intention here is to avoid a situation where a new
1225
            // large search area is enabled, and this means all our other search areas go out of
1226
            // date while that one is being indexed. To do this, we order by the time we spent
1227
            // indexing them last time we ran, meaning anything that took a very long time will be
1228
            // done last.
1229
            uasort($searchareas, function(\core_search\base $area1, \core_search\base $area2) {
1230
                return (int)$area1->get_last_indexing_duration() - (int)$area2->get_last_indexing_duration();
1231
            });
1232
 
1233
            // Decide time to stop.
1234
            $stopat = self::get_current_time() + $timelimit;
1235
        }
1236
 
1441 ariadna 1237
        // Work out if we are in test mode, in which case we disable the indexing delay (because
1238
        // the normal pattern is to add a document and immediately index it).
1239
        $testmode = (PHPUNIT_TEST || defined('BEHAT_TEST')) &&
1240
            empty($CFG->searchindexingdelayfortestscript);
1241
 
1 efrain 1242
        foreach ($searchareas as $areaid => $searcharea) {
1243
 
1244
            $progress->output('Processing area: ' . $searcharea->get_visible_name());
1245
 
1246
            // Notify the engine that an area is starting.
1247
            $this->engine->area_index_starting($searcharea, $fullindex);
1248
 
1249
            $indexingstart = (int)self::get_current_time();
1250
            $elapsed = self::get_current_time();
1251
 
1252
            // This is used to store this component config.
1253
            list($componentconfigname, $varname) = $searcharea->get_config_var_name();
1254
 
1255
            $prevtimestart = intval(get_config($componentconfigname, $varname . '_indexingstart'));
1256
 
1441 ariadna 1257
            // The effective start time of previous indexing was some seconds earlier because we
1258
            // only index data up to that time, to avoid race conditions (if it takes a while to
1259
            // write a document to the database and the timecreated for that document ends up being
1260
            // a second or two out of date). This mechanism is disabled for tests.
1261
            if (!$testmode) {
1262
                // The -1 here is because for example, if _indexingstart is 123, we will have
1263
                // indexed everything up to 123 - 5 = 118 (inclusive). So next time, we can start
1264
                // at 119 = 123 - 4 and we don't have to repeat 118.
1265
                $prevtimestart -= (self::INDEXING_DELAY - 1);
1266
            }
1267
 
1 efrain 1268
            if ($fullindex === true) {
1269
                $referencestarttime = 0;
1270
 
1271
                // For full index, we delete any queued context index requests, as those will
1272
                // obviously be met by the full index.
1273
                $DB->delete_records('search_index_requests');
1274
            } else {
1275
                $partial = get_config($componentconfigname, $varname . '_partial');
1276
                if ($partial) {
1277
                    // When the previous index did not complete all data, we start from the time of the
1278
                    // last document that was successfully indexed. (Note this will result in
1279
                    // re-indexing that one document, but we can't avoid that because there may be
1280
                    // other documents in the same second.)
1281
                    $referencestarttime = intval(get_config($componentconfigname, $varname . '_lastindexrun'));
1282
                } else {
1283
                    $referencestarttime = $prevtimestart;
1284
                }
1285
            }
1286
 
1287
            // Getting the recordset from the area.
1288
            $recordset = $searcharea->get_recordset_by_timestamp($referencestarttime);
1289
            $initialquerytime = self::get_current_time() - $elapsed;
1290
            if ($initialquerytime > self::DISPLAY_LONG_QUERY_TIME) {
1291
                $progress->output('Initial query took ' . round($initialquerytime, 1) .
1292
                        ' seconds.', 1);
1293
            }
1294
 
1295
            // Pass get_document as callback.
1296
            $fileindexing = $this->engine->file_indexing_enabled() && $searcharea->uses_file_indexing();
1297
            $options = array('indexfiles' => $fileindexing, 'lastindexedtime' => $prevtimestart);
1298
            if ($timelimit) {
1299
                $options['stopat'] = $stopat;
1300
            }
1301
            $options['progress'] = $progress;
1441 ariadna 1302
            // Skip 'future' documents, also any written very recently (to avoid race conditions).
1303
            // The exception is for PHPunit and Behat (step 'I update the global search index')
1304
            // where we allow it to index recent documents as well, we don't want it to have to wait.
1305
            $iterator = new skip_future_documents_iterator(
1306
                new \core\dml\recordset_walk($recordset, [$searcharea, 'get_document'], $options),
1307
                $indexingstart - ($testmode ? 0 : self::INDEXING_DELAY),
1308
            );
1 efrain 1309
            $result = $this->engine->add_documents($iterator, $searcharea, $options);
1310
            $recordset->close();
1311
            $batchinfo = '';
1312
            if (count($result) === 6) {
1313
                [$numrecords, $numdocs, $numdocsignored, $lastindexeddoc, $partial, $batches] = $result;
1314
                // Only show the batch count if we actually batched any requests.
1315
                if ($batches !== $numdocs + $numdocsignored) {
1316
                    $batchinfo = ' (' . $batches . ' batch' . ($batches === 1 ? '' : 'es') . ')';
1317
                }
1318
            } else {
1319
                throw new \coding_exception('engine::add_documents() should return 6 values');
1320
            }
1321
 
1322
            if ($numdocs > 0) {
1323
                $elapsed = round((self::get_current_time() - $elapsed), 1);
1324
 
1325
                $partialtext = '';
1326
                if ($partial) {
1327
                    $partialtext = ' (not complete; done to ' . userdate($lastindexeddoc,
1328
                            get_string('strftimedatetimeshort', 'langconfig')) . ')';
1329
                }
1330
 
1331
                $progress->output('Processed ' . $numrecords . ' records containing ' . $numdocs .
1332
                        ' documents' . $batchinfo . ', in ' . $elapsed . ' seconds' . $partialtext . '.', 1);
1333
            } else {
1334
                $progress->output('No new documents to index.', 1);
1335
            }
1336
 
1337
            // Notify the engine this area is complete, and only mark times if true.
1338
            if ($this->engine->area_index_complete($searcharea, $numdocs, $fullindex)) {
1339
                $sumdocs += $numdocs;
1340
 
1341
                // Store last index run once documents have been committed to the search engine.
1342
                set_config($varname . '_indexingstart', $indexingstart, $componentconfigname);
1343
                set_config($varname . '_indexingend', (int)self::get_current_time(), $componentconfigname);
1344
                set_config($varname . '_docsignored', $numdocsignored, $componentconfigname);
1345
                set_config($varname . '_docsprocessed', $numdocs, $componentconfigname);
1346
                set_config($varname . '_recordsprocessed', $numrecords, $componentconfigname);
1347
                if ($lastindexeddoc > 0) {
1348
                    set_config($varname . '_lastindexrun', $lastindexeddoc, $componentconfigname);
1349
                }
1350
                if ($partial) {
1351
                    set_config($varname . '_partial', 1, $componentconfigname);
1352
                } else {
1353
                    unset_config($varname . '_partial', $componentconfigname);
1354
                }
1355
            } else {
1356
                $progress->output('Engine reported error.');
1357
            }
1358
 
1359
            if ($timelimit && (self::get_current_time() >= $stopat)) {
1360
                $progress->output('Stopping indexing due to time limit.');
1361
                break;
1362
            }
1363
        }
1364
 
1365
        if ($sumdocs > 0) {
1366
            $event = \core\event\search_indexed::create(
1367
                    array('context' => \context_system::instance()));
1368
            $event->trigger();
1369
        }
1370
 
1371
        $this->engine->index_complete($sumdocs, $fullindex);
1372
 
1373
        return (bool)$sumdocs;
1374
    }
1375
 
1376
    /**
1377
     * Indexes or reindexes a specific context of the system, e.g. one course.
1378
     *
1379
     * The function returns an object with field 'complete' (true or false).
1380
     *
1381
     * This function supports partial indexing via the time limit parameter. If the time limit
1382
     * expires, it will return values for $startfromarea and $startfromtime which can be passed
1383
     * next time to continue indexing.
1384
     *
1385
     * @param \context $context Context to restrict index.
1386
     * @param string $singleareaid If specified, indexes only the given area.
1387
     * @param float $timelimit Time limit in seconds (0 = no time limit)
1388
     * @param \progress_trace|null $progress Optional class for tracking progress
1389
     * @param string $startfromarea Area to start from
1390
     * @param int $startfromtime Timestamp to start from
1391
     * @return \stdClass Object indicating success
1392
     */
1393
    public function index_context($context, $singleareaid = '', $timelimit = 0,
1441 ariadna 1394
            ?\progress_trace $progress = null, $startfromarea = '', $startfromtime = 0) {
1 efrain 1395
        if (!$progress) {
1396
            $progress = new \null_progress_trace();
1397
        }
1398
 
1399
        // Work out time to stop, if limited.
1400
        if ($timelimit) {
1401
            // Decide time to stop.
1402
            $stopat = self::get_current_time() + $timelimit;
1403
        }
1404
 
1405
        // No PHP time limit.
1406
        \core_php_time_limit::raise();
1407
 
1408
        // Notify the engine that an index starting.
1409
        $this->engine->index_starting(false);
1410
 
1411
        $sumdocs = 0;
1412
 
1413
        // Get all search areas, in consistent order.
1414
        $searchareas = $this->get_search_areas_list(true);
1415
        ksort($searchareas);
1416
 
1417
        // Are we skipping past some that were handled previously?
1418
        $skipping = $startfromarea ? true : false;
1419
 
1420
        foreach ($searchareas as $areaid => $searcharea) {
1421
            // If we're only processing one area id, skip all the others.
1422
            if ($singleareaid && $singleareaid !== $areaid) {
1423
                continue;
1424
            }
1425
 
1426
            // If we're skipping to a later area, continue through the loop.
1427
            $referencestarttime = 0;
1428
            if ($skipping) {
1429
                if ($areaid !== $startfromarea) {
1430
                    continue;
1431
                }
1432
                // Stop skipping and note the reference start time.
1433
                $skipping = false;
1434
                $referencestarttime = $startfromtime;
1435
            }
1436
 
1437
            $progress->output('Processing area: ' . $searcharea->get_visible_name());
1438
 
1439
            $elapsed = self::get_current_time();
1440
 
1441
            // Get the recordset of all documents from the area for this context.
1442
            $recordset = $searcharea->get_document_recordset($referencestarttime, $context);
1443
            if (!$recordset) {
1444
                if ($recordset === null) {
1445
                    $progress->output('Skipping (not relevant to context).', 1);
1446
                } else {
1447
                    $progress->output('Skipping (does not support context indexing).', 1);
1448
                }
1449
                continue;
1450
            }
1451
 
1452
            // Notify the engine that an area is starting.
1453
            $this->engine->area_index_starting($searcharea, false);
1454
 
1455
            // Work out search options.
1456
            $options = [];
1457
            $options['indexfiles'] = $this->engine->file_indexing_enabled() &&
1458
                    $searcharea->uses_file_indexing();
1459
            if ($timelimit) {
1460
                $options['stopat'] = $stopat;
1461
            }
1462
 
1463
            // Construct iterator which will use get_document on the recordset results.
1464
            $iterator = new \core\dml\recordset_walk($recordset,
1465
                    array($searcharea, 'get_document'), $options);
1466
 
1467
            // Use this iterator to add documents.
1468
            $result = $this->engine->add_documents($iterator, $searcharea, $options);
1469
            $batchinfo = '';
1470
            if (count($result) === 6) {
1471
                [$numrecords, $numdocs, $numdocsignored, $lastindexeddoc, $partial, $batches] = $result;
1472
                // Only show the batch count if we actually batched any requests.
1473
                if ($batches !== $numdocs + $numdocsignored) {
1474
                    $batchinfo = ' (' . $batches . ' batch' . ($batches === 1 ? '' : 'es') . ')';
1475
                }
1476
            } else {
1477
                throw new \coding_exception('engine::add_documents() should return 6 values');
1478
            }
1479
 
1480
            if ($numdocs > 0) {
1481
                $elapsed = round((self::get_current_time() - $elapsed), 3);
1482
                $progress->output('Processed ' . $numrecords . ' records containing ' . $numdocs .
1483
                        ' documents' . $batchinfo . ', in ' . $elapsed . ' seconds' .
1484
                        ($partial ? ' (not complete)' : '') . '.', 1);
1485
            } else {
1486
                $progress->output('No documents to index.', 1);
1487
            }
1488
 
1489
            // Notify the engine this area is complete, but don't store any times as this is not
1490
            // part of the 'normal' search index.
1491
            if (!$this->engine->area_index_complete($searcharea, $numdocs, false)) {
1492
                $progress->output('Engine reported error.', 1);
1493
            }
1494
 
1495
            if ($partial && $timelimit && (self::get_current_time() >= $stopat)) {
1496
                $progress->output('Stopping indexing due to time limit.');
1497
                break;
1498
            }
1499
        }
1500
 
1501
        if ($sumdocs > 0) {
1502
            $event = \core\event\search_indexed::create(
1503
                    array('context' => $context));
1504
            $event->trigger();
1505
        }
1506
 
1507
        $this->engine->index_complete($sumdocs, false);
1508
 
1509
        // Indicate in result whether we completed indexing, or only part of it.
1510
        $result = new \stdClass();
1511
        if ($partial) {
1512
            $result->complete = false;
1513
            $result->startfromarea = $areaid;
1514
            $result->startfromtime = $lastindexeddoc;
1515
        } else {
1516
            $result->complete = true;
1517
        }
1518
        return $result;
1519
    }
1520
 
1521
    /**
1522
     * Resets areas config.
1523
     *
1524
     * @throws \moodle_exception
1525
     * @param string $areaid
1526
     * @return void
1527
     */
1528
    public function reset_config($areaid = false) {
1529
 
1530
        if (!empty($areaid)) {
1531
            $searchareas = array();
1532
            if (!$searchareas[$areaid] = static::get_search_area($areaid)) {
1533
                throw new \moodle_exception('errorareanotavailable', 'search', '', $areaid);
1534
            }
1535
        } else {
1536
            // Only the enabled ones.
1537
            $searchareas = static::get_search_areas_list(true);
1538
        }
1539
 
1540
        foreach ($searchareas as $searcharea) {
1541
            list($componentname, $varname) = $searcharea->get_config_var_name();
1542
            $config = $searcharea->get_config();
1543
 
1544
            foreach ($config as $key => $value) {
1545
                // We reset them all but the enable/disabled one.
1546
                if ($key !== $varname . '_enabled') {
1547
                    set_config($key, 0, $componentname);
1548
                }
1549
            }
1550
        }
1551
    }
1552
 
1553
    /**
1554
     * Deletes an area's documents or all areas documents.
1555
     *
1556
     * @param string $areaid The area id or false for all
1557
     * @return void
1558
     */
1559
    public function delete_index($areaid = false) {
1560
        if (!empty($areaid)) {
1561
            $this->engine->delete($areaid);
1562
            $this->reset_config($areaid);
1563
        } else {
1564
            $this->engine->delete();
1565
            $this->reset_config();
1566
        }
1567
    }
1568
 
1569
    /**
1570
     * Deletes index by id.
1571
     *
1572
     * @param int Solr Document string $id
1573
     */
1574
    public function delete_index_by_id($id) {
1575
        $this->engine->delete_by_id($id);
1576
    }
1577
 
1578
    /**
1579
     * Returns search areas configuration.
1580
     *
1581
     * @param \core_search\base[] $searchareas
1582
     * @return \stdClass[] $configsettings
1583
     */
1584
    public function get_areas_config($searchareas) {
1585
 
1586
        $vars = array('indexingstart', 'indexingend', 'lastindexrun', 'docsignored',
1587
                'docsprocessed', 'recordsprocessed', 'partial');
1588
 
1589
        $configsettings = [];
1590
        foreach ($searchareas as $searcharea) {
1591
 
1592
            $areaid = $searcharea->get_area_id();
1593
 
1594
            $configsettings[$areaid] = new \stdClass();
1595
            list($componentname, $varname) = $searcharea->get_config_var_name();
1596
 
1597
            if (!$searcharea->is_enabled()) {
1598
                // We delete all indexed data on disable so no info.
1599
                foreach ($vars as $var) {
1600
                    $configsettings[$areaid]->{$var} = 0;
1601
                }
1602
            } else {
1603
                foreach ($vars as $var) {
1604
                    $configsettings[$areaid]->{$var} = get_config($componentname, $varname .'_' . $var);
1605
                }
1606
            }
1607
 
1608
            // Formatting the time.
1609
            if (!empty($configsettings[$areaid]->lastindexrun)) {
1610
                $configsettings[$areaid]->lastindexrun = userdate($configsettings[$areaid]->lastindexrun);
1611
            } else {
1612
                $configsettings[$areaid]->lastindexrun = get_string('never');
1613
            }
1614
        }
1615
        return $configsettings;
1616
    }
1617
 
1618
    /**
1619
     * Triggers search_results_viewed event
1620
     *
1621
     * Other data required:
1622
     * - q: The query string
1623
     * - page: The page number
1624
     * - title: Title filter
1625
     * - areaids: Search areas filter
1626
     * - courseids: Courses filter
1627
     * - timestart: Time start filter
1628
     * - timeend: Time end filter
1629
     *
1630
     * @since Moodle 3.2
1631
     * @param array $other Other info for the event.
1632
     * @return \core\event\search_results_viewed
1633
     */
1634
    public static function trigger_search_results_viewed($other) {
1635
        $event = \core\event\search_results_viewed::create([
1636
            'context' => \context_system::instance(),
1637
            'other' => $other
1638
        ]);
1639
        $event->trigger();
1640
 
1641
        return $event;
1642
    }
1643
 
1644
    /**
1645
     * Checks whether a classname is of an actual search area.
1646
     *
1647
     * @param string $classname
1648
     * @return bool
1649
     */
1650
    protected static function is_search_area($classname) {
1651
        if (is_subclass_of($classname, 'core_search\base')) {
1652
            return (new \ReflectionClass($classname))->isInstantiable();
1653
        }
1654
 
1655
        return false;
1656
    }
1657
 
1658
    /**
1659
     * Requests that a specific context is indexed by the scheduled task. The context will be
1660
     * added to a queue which is processed by the task.
1661
     *
1662
     * This is used after a restore to ensure that restored items are indexed, even though their
1663
     * modified time will be older than the latest indexed. It is also used by the 'Gradual reindex'
1664
     * admin feature from the search areas screen.
1665
     *
1666
     * @param \context $context Context to index within
1667
     * @param string $areaid Area to index, '' = all areas
1668
     * @param int $priority Priority (INDEX_PRIORITY_xx constant)
1669
     */
1670
    public static function request_index(\context $context, $areaid = '',
1671
            $priority = self::INDEX_PRIORITY_NORMAL) {
1672
        global $DB;
1673
 
1674
        // Check through existing requests for this context or any parent context.
1675
        list ($contextsql, $contextparams) = $DB->get_in_or_equal(
1676
                $context->get_parent_context_ids(true));
1677
        $existing = $DB->get_records_select('search_index_requests',
1678
                'contextid ' . $contextsql, $contextparams, '',
1679
                'id, searcharea, partialarea, indexpriority');
1680
        foreach ($existing as $rec) {
1681
            // If we haven't started processing the existing request yet, and it covers the same
1682
            // area (or all areas) then that will be sufficient so don't add anything else.
1683
            if ($rec->partialarea === '' && ($rec->searcharea === $areaid || $rec->searcharea === '')) {
1684
                // If the existing request has the same (or higher) priority, no need to add anything.
1685
                if ($rec->indexpriority >= $priority) {
1686
                    return;
1687
                }
1688
                // The existing request has lower priority. If it is exactly the same, then just
1689
                // adjust the priority of the existing request.
1690
                if ($rec->searcharea === $areaid) {
1691
                    $DB->set_field('search_index_requests', 'indexpriority', $priority,
1692
                            ['id' => $rec->id]);
1693
                    return;
1694
                }
1695
                // The existing request would cover this area but is a lower priority. We need to
1696
                // add the new request even though that means we will index part of it twice.
1697
            }
1698
        }
1699
 
1700
        // No suitable existing request, so add a new one.
1701
        $newrecord = [ 'contextid' => $context->id, 'searcharea' => $areaid,
1702
                'timerequested' => (int)self::get_current_time(),
1703
                'partialarea' => '', 'partialtime' => 0,
1704
                'indexpriority' => $priority ];
1705
        $DB->insert_record('search_index_requests', $newrecord);
1706
    }
1707
 
1708
    /**
1709
     * Processes outstanding index requests. This will take the first item from the queue (taking
1710
     * account the indexing priority) and process it, continuing until an optional time limit is
1711
     * reached.
1712
     *
1713
     * If there are no index requests, the function will do nothing.
1714
     *
1715
     * @param float $timelimit Time limit (0 = none)
1716
     * @param \progress_trace|null $progress Optional progress indicator
1717
     */
1441 ariadna 1718
    public function process_index_requests($timelimit = 0.0, ?\progress_trace $progress = null) {
1 efrain 1719
        global $DB;
1720
 
1721
        if (!$progress) {
1722
            $progress = new \null_progress_trace();
1723
        }
1724
 
1725
        $before = self::get_current_time();
1726
        if ($timelimit) {
1727
            $stopat = $before + $timelimit;
1728
        }
1729
        while (true) {
1730
            // Retrieve first request, using fully defined ordering.
1731
            $requests = $DB->get_records('search_index_requests', null,
1732
                    'indexpriority DESC, timerequested, contextid, searcharea',
1733
                    'id, contextid, searcharea, partialarea, partialtime', 0, 1);
1734
            if (!$requests) {
1735
                // If there are no more requests, stop.
1736
                break;
1737
            }
1738
            $request = reset($requests);
1739
 
1740
            // Calculate remaining time.
1741
            $remainingtime = 0;
1742
            $beforeindex = self::get_current_time();
1743
            if ($timelimit) {
1744
                $remainingtime = $stopat - $beforeindex;
1745
 
1746
                // If the time limit expired already, stop now. (Otherwise we might accidentally
1747
                // index with no time limit or a negative time limit.)
1748
                if ($remainingtime <= 0) {
1749
                    break;
1750
                }
1751
            }
1752
 
1753
            // Show a message before each request, indicating what will be indexed.
1754
            $context = \context::instance_by_id($request->contextid, IGNORE_MISSING);
1755
            if (!$context) {
1756
                $DB->delete_records('search_index_requests', ['id' => $request->id]);
1757
                $progress->output('Skipped deleted context: ' . $request->contextid);
1758
                continue;
1759
            }
1760
            $contextname = $context->get_context_name();
1761
            if ($request->searcharea) {
1762
                $contextname .= ' (search area: ' . $request->searcharea . ')';
1763
            }
1764
            $progress->output('Indexing requested context: ' . $contextname);
1765
 
1766
            // Actually index the context.
1767
            $result = $this->index_context($context, $request->searcharea, $remainingtime,
1768
                    $progress, $request->partialarea, $request->partialtime);
1769
 
1770
            // Work out shared part of message.
1771
            $endmessage = $contextname . ' (' . round(self::get_current_time() - $beforeindex, 1) . 's)';
1772
 
1773
            // Update database table and continue/stop as appropriate.
1774
            if ($result->complete) {
1775
                // If we completed the request, remove it from the table.
1776
                $DB->delete_records('search_index_requests', ['id' => $request->id]);
1777
                $progress->output('Completed requested context: ' . $endmessage);
1778
            } else {
1779
                // If we didn't complete the request, store the partial details (how far it got).
1780
                $DB->update_record('search_index_requests', ['id' => $request->id,
1781
                        'partialarea' => $result->startfromarea,
1782
                        'partialtime' => $result->startfromtime]);
1783
                $progress->output('Ending requested context: ' . $endmessage);
1784
 
1785
                // The time limit must have expired, so stop looping.
1786
                break;
1787
            }
1788
        }
1789
    }
1790
 
1791
    /**
1792
     * Gets information about the request queue, in the form of a plain object suitable for passing
1793
     * to a template for rendering.
1794
     *
1795
     * @return \stdClass Information about queued index requests
1796
     */
1797
    public function get_index_requests_info() {
1798
        global $DB;
1799
 
1800
        $result = new \stdClass();
1801
 
1802
        $result->total = $DB->count_records('search_index_requests');
1803
        $result->topten = $DB->get_records('search_index_requests', null,
1804
                'indexpriority DESC, timerequested, contextid, searcharea',
1805
                'id, contextid, timerequested, searcharea, partialarea, partialtime, indexpriority',
1806
                0, 10);
1807
        foreach ($result->topten as $item) {
1808
            $context = \context::instance_by_id($item->contextid);
1809
            $item->contextlink = \html_writer::link($context->get_url(),
1810
                    s($context->get_context_name()));
1811
            if ($item->searcharea) {
1812
                $item->areaname = $this->get_search_area($item->searcharea)->get_visible_name();
1813
            }
1814
            if ($item->partialarea) {
1815
                $item->partialareaname = $this->get_search_area($item->partialarea)->get_visible_name();
1816
            }
1817
            switch ($item->indexpriority) {
1818
                case self::INDEX_PRIORITY_REINDEXING :
1819
                    $item->priorityname = get_string('priority_reindexing', 'search');
1820
                    break;
1821
                case self::INDEX_PRIORITY_NORMAL :
1822
                    $item->priorityname = get_string('priority_normal', 'search');
1823
                    break;
1824
            }
1825
        }
1826
 
1827
        // Normalise array indices.
1828
        $result->topten = array_values($result->topten);
1829
 
1830
        if ($result->total > 10) {
1831
            $result->ellipsis = true;
1832
        }
1833
 
1834
        return $result;
1835
    }
1836
 
1837
    /**
1838
     * Gets current time for use in search system.
1839
     *
1840
     * Note: This should be replaced with generic core functionality once possible (see MDL-60644).
1841
     *
1842
     * @return float Current time in seconds (with decimals)
1843
     */
1844
    public static function get_current_time() {
1845
        if (PHPUNIT_TEST && self::$phpunitfaketime) {
1846
            return self::$phpunitfaketime;
1847
        }
1848
        return microtime(true);
1849
    }
1850
 
1851
    /**
1852
     * Check if search area categories functionality is enabled.
1853
     *
1854
     * @return bool
1855
     */
1856
    public static function is_search_area_categories_enabled() {
1857
        return !empty(get_config('core', 'searchenablecategories'));
1858
    }
1859
 
1860
    /**
1861
     * Check if all results category should be hidden.
1862
     *
1863
     * @return bool
1864
     */
1865
    public static function should_hide_all_results_category() {
1866
        return get_config('core', 'searchhideallcategory');
1867
    }
1868
 
1869
    /**
1870
     * Returns default search area category name.
1871
     *
1872
     * @return string
1873
     */
1874
    public static function get_default_area_category_name() {
1875
        $default = get_config('core', 'searchdefaultcategory');
1876
 
1877
        if (empty($default)) {
1878
            $default = self::SEARCH_AREA_CATEGORY_ALL;
1879
        }
1880
 
1881
        if ($default == self::SEARCH_AREA_CATEGORY_ALL && self::should_hide_all_results_category()) {
1882
            $default = self::SEARCH_AREA_CATEGORY_COURSE_CONTENT;
1883
        }
1884
 
1885
        return $default;
1886
    }
1887
 
1888
    /**
1889
     * Get a list of all courses limited by ids if required.
1890
     *
1891
     * @param array|false $limitcourseids An array of course ids to limit the search to. False for no limiting.
1892
     * @return array
1893
     */
1894
    protected function get_all_courses($limitcourseids) {
1895
        global $DB;
1896
 
1897
        if ($limitcourseids) {
1898
            list ($coursesql, $courseparams) = $DB->get_in_or_equal($limitcourseids);
1899
            $coursesql = 'id ' . $coursesql;
1900
        } else {
1901
            $coursesql = '';
1902
            $courseparams = [];
1903
        }
1904
 
1905
        // Get courses using the same list of fields from enrol_get_my_courses.
1906
        return $DB->get_records_select('course', $coursesql, $courseparams, '',
1907
            'id, category, sortorder, shortname, fullname, idnumber, startdate, visible, ' .
1908
            'groupmode, groupmodeforce, cacherev');
1909
    }
1910
 
1911
    /**
1912
     * Get a list of courses as user can access.
1913
     *
1914
     * @param bool $allaccessible Include courses user is not enrolled in, but can access.
1915
     * @return array
1916
     */
1917
    protected function get_my_courses($allaccessible) {
1918
        return enrol_get_my_courses(array('id', 'cacherev'), 'id', 0, [], $allaccessible);
1919
    }
1920
 
1921
    /**
1922
     * Check if search all courses setting is enabled.
1923
     *
1924
     * @return bool
1925
     */
1926
    public static function include_all_courses() {
1927
        return !empty(get_config('core', 'searchincludeallcourses'));
1928
    }
1929
 
1930
    /**
1931
     * Cleans up non existing search area.
1932
     *
1933
     * 1. Remove all configs from {config_plugins} table.
1934
     * 2. Delete all related indexed documents.
1935
     *
1936
     * @param string $areaid Search area id.
1937
     */
1938
    public static function clean_up_non_existing_area($areaid) {
1939
        global $DB;
1940
 
1941
        if (!empty(self::get_search_area($areaid))) {
1942
            throw new \coding_exception("Area $areaid exists. Please use appropriate search area class to manipulate the data.");
1943
        }
1944
 
1945
        $parts = self::parse_areaid($areaid);
1946
 
1947
        $plugin = $parts[0];
1948
        $configprefix = $parts[1];
1949
 
1950
        foreach (base::get_settingnames() as $settingname) {
1951
            $name = $configprefix. $settingname;
1952
            $DB->delete_records('config_plugins', ['name' => $name, 'plugin' => $plugin]);
1953
        }
1954
 
1955
        $engine = self::instance()->get_engine();
1956
        $engine->delete($areaid);
1957
    }
1958
 
1959
    /**
1960
     * Informs the search system that a context has been deleted.
1961
     *
1962
     * This will clear the data from the search index, where the search engine supports that.
1963
     *
1964
     * This function does not usually throw an exception (so as not to get in the way of the
1965
     * context deletion finishing).
1966
     *
1967
     * This is called for all types of context deletion.
1968
     *
1969
     * @param \context $context Context object that has just been deleted
1970
     */
1971
    public static function context_deleted(\context $context) {
1972
        if (self::is_indexing_enabled()) {
1973
            try {
1974
                // Hold on, are we deleting a course? If so, and this context is part of the course,
1975
                // then don't bother to send a delete because we delete the whole course at once
1976
                // later.
1977
                if (!empty(self::$coursedeleting)) {
1978
                    $coursecontext = $context->get_course_context(false);
1979
                    if ($coursecontext && array_key_exists($coursecontext->instanceid, self::$coursedeleting)) {
1980
                        // Skip further processing.
1981
                        return;
1982
                    }
1983
                }
1984
 
1985
                $engine = self::instance()->get_engine();
1986
                $engine->delete_index_for_context($context->id);
1987
            } catch (\moodle_exception $e) {
1988
                debugging('Error deleting search index data for context ' . $context->id . ': ' . $e->getMessage());
1989
            }
1990
        }
1991
    }
1992
 
1993
    /**
1994
     * Informs the search system that a course is about to be deleted.
1995
     *
1996
     * This prevents it from sending hundreds of 'delete context' updates for all the individual
1997
     * contexts that are deleted.
1998
     *
1999
     * If you call this, you must call course_deleting_finish().
2000
     *
2001
     * @param int $courseid Course id that is being deleted
2002
     */
2003
    public static function course_deleting_start(int $courseid) {
2004
        self::$coursedeleting[$courseid] = true;
2005
    }
2006
 
2007
    /**
2008
     * Informs the search engine that a course has now been deleted.
2009
     *
2010
     * This causes the search engine to actually delete the index for the whole course.
2011
     *
2012
     * @param int $courseid Course id that no longer exists
2013
     */
2014
    public static function course_deleting_finish(int $courseid) {
2015
        if (!array_key_exists($courseid, self::$coursedeleting)) {
2016
            // Show a debug warning. It doesn't actually matter very much, as we will now delete
2017
            // the course data anyhow.
2018
            debugging('course_deleting_start not called before deletion of ' . $courseid, DEBUG_DEVELOPER);
2019
        }
2020
        unset(self::$coursedeleting[$courseid]);
2021
 
2022
        if (self::is_indexing_enabled()) {
2023
            try {
2024
                $engine = self::instance()->get_engine();
2025
                $engine->delete_index_for_course($courseid);
2026
            } catch (\moodle_exception $e) {
2027
                debugging('Error deleting search index data for course ' . $courseid . ': ' . $e->getMessage());
2028
            }
2029
        }
2030
    }
2031
}