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.
   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   * The library file for the memcached cache store.
  19   *
  20   * This file is part of the memcached cache store, it contains the API for interacting with an instance of the store.
  21   *
  22   * @package    cachestore_memcached
  23   * @copyright  2012 Sam Hemelryk
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot.'/cache/forms.php');
  30  require_once($CFG->dirroot.'/cache/stores/memcached/lib.php');
  31  
  32  /**
  33   * Form for adding a memcached instance.
  34   *
  35   * @copyright  2012 Sam Hemelryk
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class cachestore_memcached_addinstance_form extends cachestore_addinstance_form {
  39  
  40      /**
  41       * Adds the desired form elements.
  42       */
  43      protected function configuration_definition() {
  44          global $OUTPUT;
  45  
  46          $form = $this->_form;
  47          $version = phpversion('memcached');
  48          $hasrequiredversion = ($version || version_compare($version, cachestore_memcached::REQUIRED_VERSION, '>='));
  49  
  50          if (!$hasrequiredversion) {
  51              $notify = new \core\output\notification(nl2br(get_string('upgrade200recommended', 'cachestore_memcached')),
  52                  \core\output\notification::NOTIFY_WARNING);
  53              $form->addElement('html', $OUTPUT->render($notify));
  54          }
  55  
  56          $form->addElement('textarea', 'servers', get_string('servers', 'cachestore_memcached'), array('cols' => 75, 'rows' => 5));
  57          $form->addHelpButton('servers', 'servers', 'cachestore_memcached');
  58          $form->addRule('servers', get_string('required'), 'required');
  59          $form->setType('servers', PARAM_RAW);
  60  
  61          $form->addElement('selectyesno', 'compression', get_string('usecompression', 'cachestore_memcached'));
  62          $form->addHelpButton('compression', 'usecompression', 'cachestore_memcached');
  63          $form->setDefault('compression', 1);
  64          $form->setType('compression', PARAM_BOOL);
  65  
  66          $serialiseroptions = cachestore_memcached::config_get_serialiser_options();
  67          $form->addElement('select', 'serialiser', get_string('useserialiser', 'cachestore_memcached'), $serialiseroptions);
  68          $form->addHelpButton('serialiser', 'useserialiser', 'cachestore_memcached');
  69          $form->setDefault('serialiser', Memcached::SERIALIZER_PHP);
  70          $form->setType('serialiser', PARAM_INT);
  71  
  72          $form->addElement('text', 'prefix', get_string('prefix', 'cachestore_memcached'), array('size' => 16));
  73          $form->setType('prefix', PARAM_TEXT); // We set to text but we have a rule to limit to alphanumext.
  74          $form->addHelpButton('prefix', 'prefix', 'cachestore_memcached');
  75          $form->addRule('prefix', get_string('prefixinvalid', 'cachestore_memcached'), 'regex', '#^[a-zA-Z0-9\-_]+$#');
  76          $form->setForceLtr('prefix');
  77  
  78          $hashoptions = cachestore_memcached::config_get_hash_options();
  79          $form->addElement('select', 'hash', get_string('hash', 'cachestore_memcached'), $hashoptions);
  80          $form->addHelpButton('hash', 'hash', 'cachestore_memcached');
  81          $form->setDefault('serialiser', Memcached::HASH_DEFAULT);
  82          $form->setType('serialiser', PARAM_INT);
  83  
  84          $form->addElement('selectyesno', 'bufferwrites', get_string('bufferwrites', 'cachestore_memcached'));
  85          $form->addHelpButton('bufferwrites', 'bufferwrites', 'cachestore_memcached');
  86          $form->setDefault('bufferwrites', 0);
  87          $form->setType('bufferwrites', PARAM_BOOL);
  88  
  89          if ($hasrequiredversion) {
  90              // Only show this option if we have the required version of memcache extension installed.
  91              // If it's not installed then this option does nothing, so there is no point in displaying it.
  92              $form->addElement('selectyesno', 'isshared', get_string('isshared', 'cachestore_memcached'));
  93              $form->addHelpButton('isshared', 'isshared', 'cachestore_memcached');
  94              $form->setDefault('isshared', 0);
  95              $form->setType('isshared', PARAM_BOOL);
  96          }
  97  
  98          $form->addElement('header', 'clusteredheader', get_string('clustered', 'cachestore_memcached'));
  99  
 100          $form->addElement('checkbox', 'clustered', get_string('clustered', 'cachestore_memcached'));
 101          $form->setDefault('checkbox', false);
 102          $form->addHelpButton('clustered', 'clustered', 'cachestore_memcached');
 103  
 104          $form->addElement('textarea', 'setservers', get_string('setservers', 'cachestore_memcached'),
 105                  array('cols' => 75, 'rows' => 5));
 106          $form->addHelpButton('setservers', 'setservers', 'cachestore_memcached');
 107          $form->disabledIf('setservers', 'clustered');
 108          $form->setType('setservers', PARAM_RAW);
 109      }
 110  
 111      /**
 112       * Perform minimal validation on the settings form.
 113       *
 114       * @param array $data
 115       * @param array $files
 116       */
 117      public function validation($data, $files) {
 118          $errors = parent::validation($data, $files);
 119  
 120          if (isset($data['clustered']) && ($data['clustered'] == 1)) {
 121              // Set servers is required with in cluster mode.
 122              if (!isset($data['setservers'])) {
 123                  $errors['setservers'] = get_string('required');
 124              } else {
 125                  $trimmed = trim($data['setservers']);
 126                  if (empty($trimmed)) {
 127                      $errors['setservers'] = get_string('required');
 128                  }
 129              }
 130  
 131              $validservers = false;
 132              if (isset($data['servers'])) {
 133                  $servers = trim($data['servers']);
 134                  $servers = explode("\n", $servers);
 135                  if (count($servers) === 1) {
 136                      $validservers = true;
 137                  }
 138              }
 139  
 140              if (!$validservers) {
 141                  $errors['servers'] = get_string('serversclusterinvalid', 'cachestore_memcached');
 142              }
 143          }
 144  
 145          return $errors;
 146      }
 147  }