Merge branch 'MDL-81073' of https://github.com/paulholden/moodle
[moodle.git] / admin / searchreindex.php
blob2886a401f66619479306f134b5b54f926a068109
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Adds a search area to the queue for indexing.
20 * @package core_search
21 * @copyright 2017 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define('NO_OUTPUT_BUFFERING', true);
27 require(__DIR__ . '/../config.php');
29 // Check access.
30 require_once($CFG->libdir . '/adminlib.php');
32 admin_externalpage_setup('searchareas', '', null, (new moodle_url('/admin/searchreindex.php'))->out(false));
34 // Get area parameter and check it exists.
35 $areaid = required_param('areaid', PARAM_ALPHANUMEXT);
36 $area = \core_search\manager::get_search_area($areaid);
37 if ($area === false) {
38 throw new moodle_exception('invalidrequest');
40 $areaname = $area->get_visible_name();
42 $PAGE->set_primary_active_tab('siteadminnode');
43 $PAGE->set_secondary_active_tab('modules');
45 // Start page output.
46 $heading = get_string('gradualreindex', 'search', '');
47 $PAGE->set_title($areaname . ' - ' . get_string('gradualreindex', 'search', ''));
48 $PAGE->navbar->add($heading);
49 echo $OUTPUT->header();
50 echo $OUTPUT->heading($heading);
52 // If sesskey is supplied, actually carry out the action.
53 if (optional_param('sesskey', '', PARAM_ALPHANUM)) {
54 require_sesskey();
56 // Get all contexts for search area. This query can take time in large cases.
57 \core_php_time_limit::raise(0);
58 $contextiterator = $area->get_contexts_to_reindex();
60 $progress = new \core\progress\display_if_slow('');
61 $progress->start_progress($areaname);
63 // Request reindexing for each context (with low priority).
64 $count = 0;
65 foreach ($contextiterator as $context) {
66 \core_php_time_limit::raise(30);
67 \core_search\manager::request_index($context, $area->get_area_id(),
68 \core_search\manager::INDEX_PRIORITY_REINDEXING);
69 $progress->progress();
70 $count++;
73 // Unset the iterator which should close the recordset (if there is one).
74 unset($contextiterator);
76 $progress->end_progress();
78 $a = (object)['name' => html_writer::tag('strong', $areaname), 'count' => $count];
79 echo $OUTPUT->box(get_string('gradualreindex_queued', 'search', $a));
81 echo $OUTPUT->continue_button(new moodle_url('/admin/searchareas.php'));
82 } else {
83 // Display confirmation prompt.
84 echo $OUTPUT->confirm(get_string('gradualreindex_confirm', 'search', html_writer::tag('strong', $areaname)),
85 new single_button(new moodle_url('/admin/searchreindex.php', ['areaid' => $areaid,
86 'sesskey' => sesskey()]), get_string('continue'), 'post', single_button::BUTTON_PRIMARY),
87 new single_button(new moodle_url('/admin/searchareas.php'), get_string('cancel'), 'get'));
90 echo $OUTPUT->footer();