Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * Manage global search areas.
  19   *
  20   * @package   core_search
  21   * @copyright 2016 Dan Poltawski <dan@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  require_once(__DIR__ . '/../config.php');
  25  require_once($CFG->libdir . '/adminlib.php');
  26  
  27  admin_externalpage_setup('searchareas');
  28  
  29  $areaid = optional_param('areaid', null, PARAM_ALPHANUMEXT);
  30  $action = optional_param('action', null, PARAM_ALPHA);
  31  $indexingenabled = \core_search\manager::is_indexing_enabled(); // This restricts many of the actions on this page.
  32  
  33  // Get a search manager instance, which we'll need for display and to handle some actions.
  34  try {
  35      $searchmanager = \core_search\manager::instance();
  36  } catch (core_search\engine_exception $searchmanagererror) {
  37      // In action cases, we'll throw this exception below. In non-action cases, we produce a lang string error.
  38  }
  39  
  40  // Handle all the actions.
  41  if ($action) {
  42      // If dealing with an areaid, we need to check that the area exists.
  43      if ($areaid) {
  44          $area = \core_search\manager::get_search_area($areaid);
  45          if ($area === false) {
  46              throw new moodle_exception('invalidrequest');
  47          }
  48      }
  49  
  50      // All the indexing actions.
  51      if (in_array($action, ['delete', 'indexall', 'reindexall', 'deleteall'])) {
  52  
  53          // All of these actions require that indexing is enabled.
  54          if ($indexingenabled) {
  55  
  56              // For all of these actions, we strictly need a manager instance.
  57              if (isset($searchmanagererror)) {
  58                  throw $searchmanagererror;
  59              }
  60  
  61              // Show confirm prompt for all these actions as they may be inadvisable, or may cause
  62              // an interruption in search functionality, on production systems.
  63              if (!optional_param('confirm', 0, PARAM_INT)) {
  64                  // Display confirmation prompt.
  65                  $a = null;
  66                  if ($areaid) {
  67                      $a = html_writer::tag('strong', $area->get_visible_name());
  68                  }
  69  
  70                  $actionparams = ['sesskey' => sesskey(), 'action' => $action, 'confirm' => 1];
  71                  if ($areaid) {
  72                      $actionparams['areaid'] = $areaid;
  73                  }
  74                  $actionurl = new moodle_url('/admin/searchareas.php', $actionparams);
  75                  $cancelurl = new moodle_url('/admin/searchareas.php');
  76                  echo $OUTPUT->header();
  77                  echo $OUTPUT->confirm(get_string('confirm_' . $action, 'search', $a),
  78                      new single_button($actionurl, get_string('continue'), 'post', true),
  79                      new single_button($cancelurl, get_string('cancel'), 'get'));
  80                  echo $OUTPUT->footer();
  81                  exit;
  82              } else {
  83                  // Confirmed, so run the required action.
  84                  require_sesskey();
  85  
  86                  switch ($action) {
  87                      case 'delete':
  88                          $searchmanager->delete_index($areaid);
  89                          \core\notification::success(get_string('searchindexdeleted', 'admin'));
  90                          break;
  91                      case 'indexall':
  92                          $searchmanager->index();
  93                          \core\notification::success(get_string('searchindexupdated', 'admin'));
  94                          break;
  95                      case 'reindexall':
  96                          $searchmanager->index(true);
  97                          \core\notification::success(get_string('searchreindexed', 'admin'));
  98                          break;
  99                      case 'deleteall':
 100                          $searchmanager->delete_index();
 101                          \core\notification::success(get_string('searchalldeleted', 'admin'));
 102                          break;
 103                      default:
 104                          break;
 105                  }
 106  
 107                  // Redirect back to the main page after taking action.
 108                  redirect(new moodle_url('/admin/searchareas.php'));
 109              }
 110          }
 111      } else if (in_array($action, ['enable', 'disable'])) {
 112          // Toggling search areas requires no confirmation.
 113          require_sesskey();
 114  
 115          switch ($action) {
 116              case 'enable':
 117                  $area->set_enabled(true);
 118                  \core\notification::success(get_string('searchareaenabled', 'admin'));
 119                  break;
 120              case 'disable':
 121                  $area->set_enabled(false);
 122                  core\notification::success(get_string('searchareadisabled', 'admin'));
 123                  break;
 124              default:
 125                  break;
 126          }
 127  
 128          redirect(new moodle_url('/admin/searchareas.php'));
 129      } else {
 130          // Invalid action.
 131          throw new moodle_exception('invalidaction');
 132      }
 133  }
 134  
 135  
 136  // Display.
 137  if (isset($searchmanager) && $indexingenabled) {
 138      \core\notification::info(get_string('indexinginfo', 'admin'));
 139  } else if (isset($searchmanager)) {
 140      $params = (object) [
 141          'url' => (new moodle_url("/admin/settings.php?section=manageglobalsearch#admin-searchindexwhendisabled"))->out(false)
 142      ];
 143      \core\notification::error(get_string('indexwhendisabledfullnotice', 'search', $params));
 144  } else {
 145      // In non-action cases, init errors are translated and displayed to the user as error notifications.
 146      $errorstr = get_string($searchmanagererror->errorcode, $searchmanagererror->module, $searchmanagererror->a);
 147      \core\notification::error($errorstr);
 148  }
 149  
 150  echo $OUTPUT->header();
 151  
 152  $table = new html_table();
 153  $table->id = 'core-search-areas';
 154  $table->head = [
 155      get_string('searcharea', 'search'),
 156      get_string('searchareacategories', 'search'),
 157      get_string('enable'),
 158      get_string('newestdocindexed', 'admin'),
 159      get_string('searchlastrun', 'admin'),
 160      get_string('searchindexactions', 'admin')
 161  ];
 162  
 163  $searchareas = \core_search\manager::get_search_areas_list();
 164  $areasconfig = isset($searchmanager) ? $searchmanager->get_areas_config($searchareas) : false;
 165  foreach ($searchareas as $area) {
 166      $areaid = $area->get_area_id();
 167      $columns = array(new html_table_cell($area->get_visible_name()));
 168  
 169      $areacategories = [];
 170      foreach (\core_search\manager::get_search_area_categories() as $category) {
 171          if (key_exists($areaid, $category->get_areas())) {
 172              $areacategories[] = $category->get_visiblename();
 173          }
 174      }
 175      $columns[] = new html_table_cell(implode(', ', $areacategories));
 176  
 177      if ($area->is_enabled()) {
 178          $columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('disable', $areaid),
 179              new pix_icon('t/hide', get_string('disable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
 180              null, array('title' => get_string('disable')));
 181  
 182          if ($areasconfig && $indexingenabled) {
 183              $columns[] = $areasconfig[$areaid]->lastindexrun;
 184  
 185              if ($areasconfig[$areaid]->indexingstart) {
 186                  $timediff = $areasconfig[$areaid]->indexingend - $areasconfig[$areaid]->indexingstart;
 187                  $laststatus = $timediff . ' , ' .
 188                      $areasconfig[$areaid]->docsprocessed . ' , ' .
 189                      $areasconfig[$areaid]->recordsprocessed . ' , ' .
 190                      $areasconfig[$areaid]->docsignored;
 191                  if ($areasconfig[$areaid]->partial) {
 192                      $laststatus .= ' ' . get_string('searchpartial', 'admin');
 193                  }
 194              } else {
 195                  $laststatus = '';
 196              }
 197              $columns[] = $laststatus;
 198              $accesshide = html_writer::span($area->get_visible_name(), 'accesshide');
 199              $actions = [];
 200              $actions[] = $OUTPUT->pix_icon('t/delete', '') .
 201                      html_writer::link(admin_searcharea_action_url('delete', $areaid),
 202                      get_string('deleteindex', 'search', $accesshide));
 203              if ($area->supports_get_document_recordset()) {
 204                  $actions[] = $OUTPUT->pix_icon('i/reload', '') . html_writer::link(
 205                          new moodle_url('searchreindex.php', ['areaid' => $areaid]),
 206                          get_string('gradualreindex', 'search', $accesshide));
 207              }
 208              $columns[] = html_writer::alist($actions, ['class' => 'unstyled list-unstyled']);
 209  
 210          } else {
 211              if (!$areasconfig) {
 212                  $blankrow = new html_table_cell(get_string('searchnotavailable', 'admin'));
 213              } else {
 214                  $blankrow = new html_table_cell(get_string('indexwhendisabledshortnotice', 'search'));
 215              }
 216              $blankrow->colspan = 3;
 217              $columns[] = $blankrow;
 218          }
 219  
 220      } else {
 221          $columns[] = $OUTPUT->action_icon(admin_searcharea_action_url('enable', $areaid),
 222              new pix_icon('t/show', get_string('enable'), 'moodle', array('title' => '', 'class' => 'iconsmall')),
 223                  null, array('title' => get_string('enable')));
 224  
 225          $blankrow = new html_table_cell(get_string('searchareadisabled', 'admin'));
 226          $blankrow->colspan = 3;
 227          $columns[] = $blankrow;
 228      }
 229      $row = new html_table_row($columns);
 230      $table->data[] = $row;
 231  }
 232  
 233  // Cross-search area tasks.
 234  $options = (isset($searchmanager) && $indexingenabled) ? [] : ['disabled' => true];
 235  echo $OUTPUT->box_start('search-areas-actions');
 236  echo $OUTPUT->single_button(admin_searcharea_action_url('indexall'), get_string('searchupdateindex', 'admin'), 'get', $options);
 237  echo $OUTPUT->single_button(admin_searcharea_action_url('reindexall'), get_string('searchreindexindex', 'admin'), 'get', $options);
 238  echo $OUTPUT->single_button(admin_searcharea_action_url('deleteall'), get_string('searchdeleteindex', 'admin'), 'get', $options);
 239  echo $OUTPUT->box_end();
 240  
 241  echo html_writer::table($table);
 242  
 243  if (isset($searchmanager)) {
 244      // Show information about queued index requests for specific contexts.
 245      $searchrenderer = $PAGE->get_renderer('core_search');
 246      echo $searchrenderer->render_index_requests_info($searchmanager->get_index_requests_info());
 247  }
 248  
 249  echo $OUTPUT->footer();
 250  
 251  /**
 252   * Helper for generating url for management actions.
 253   *
 254   * @param string $action
 255   * @param string $areaid
 256   * @return moodle_url
 257   */
 258  function admin_searcharea_action_url($action, $areaid = false) {
 259      $params = array('action' => $action);
 260      if ($areaid) {
 261          $params['areaid'] = $areaid;
 262      }
 263      if ($action === 'disable' || $action === 'enable') {
 264          $params['sesskey'] = sesskey();
 265      }
 266      return new moodle_url('/admin/searchareas.php', $params);
 267  }