Beispiel #1
0
 /**
  * Checks if the schema existing fields are properly set, triggers an exception otherwise.
  *
  * @throws \moodle_exception
  * @param array $fields
  * @param bool $requireexisting Require the fields to exist, otherwise exception.
  * @return void
  */
 protected function validate_fields(&$fields, $requireexisting = false)
 {
     global $CFG;
     foreach ($fields as $fieldname => $data) {
         $url = $this->engine->get_connection_url('/schema/fields/' . $fieldname);
         $results = $this->curl->get($url);
         if ($this->curl->error) {
             throw new \moodle_exception('errorcreatingschema', 'search_solr', '', $this->curl->error);
         }
         if (!$results) {
             throw new \moodle_exception('errorcreatingschema', 'search_solr', '', get_string('nodatafromserver', 'search_solr'));
         }
         $results = json_decode($results);
         if ($requireexisting && !empty($results->error) && $results->error->code === 404) {
             $a = new \stdClass();
             $a->fieldname = $fieldname;
             $a->setupurl = $CFG->wwwroot . '/search/engine/solr/setup_schema.php';
             throw new \moodle_exception('errorvalidatingschema', 'search_solr', '', $a);
         }
         // The field should not exist so we only accept 404 errors.
         if (empty($results->error) || !empty($results->error) && $results->error->code !== 404) {
             if (!empty($results->error)) {
                 throw new \moodle_exception('errorcreatingschema', 'search_solr', '', $results->error->msg);
             } else {
                 // All these field attributes are set when fields are added through this script and should
                 // be returned and match the defined field's values.
                 if (empty($results->field) || !isset($results->field->type) || !isset($results->field->multiValued) || !isset($results->field->indexed) || !isset($results->field->stored)) {
                     throw new \moodle_exception('errorcreatingschema', 'search_solr', '', get_string('schemafieldautocreated', 'search_solr', $fieldname));
                 } else {
                     if ($results->field->type !== $data['type'] && ($data['type'] !== 'text' || $results->field->type !== 'text_general') || $results->field->multiValued !== false || $results->field->indexed !== $data['indexed'] || $results->field->stored !== $data['stored']) {
                         throw new \moodle_exception('errorcreatingschema', 'search_solr', '', get_string('schemafieldautocreated', 'search_solr', $fieldname));
                     } else {
                         // The field already exists and it is properly defined, no need to create it.
                         unset($fields[$fieldname]);
                     }
                 }
             }
         }
     }
 }