コード例 #1
0
ファイル: IndexBatchHelper.php プロジェクト: jkyto/agolf
 /**
  * Creates an indexing batch for a given search index.
  *
  * @param \Drupal\search_api\IndexInterface $index
  *   The search index for which items should be indexed.
  * @param int|null $batch_size
  *   (optional) Number of items to index per batch. Defaults to the cron limit
  *   set for the index.
  * @param int $limit
  *   (optional) Maximum number of items to index. Defaults to indexing all
  *   remaining items.
  *
  * @throws \Drupal\search_api\SearchApiException
  *   Thrown if the batch could not be created.
  */
 public static function create(IndexInterface $index, $batch_size = NULL, $limit = -1) {
   // Check if the size should be determined by the index cron limit option.
   if ($batch_size === NULL) {
     // Use the size set by the index.
     $batch_size = $index->getOption('cron_limit', \Drupal::config('search_api.settings')->get('default_cron_limit'));
   }
   // Check if indexing items is allowed.
   if ($index->status() && !$index->isReadOnly() && $batch_size !== 0 && $limit !== 0) {
     // Define the search index batch definition.
     $batch_definition = array(
       'operations' => array(
         array(array(__CLASS__, 'process'), array($index, $batch_size, $limit)),
       ),
       'finished' => array(__CLASS__, 'finish'),
       'progress_message' => static::t('Completed about @percentage% of the indexing operation (@current of @total).'),
     );
     // Schedule the batch.
     batch_set($batch_definition);
   }
   else {
     $args = array(
       '%size' => $batch_size,
       '%limit' => $limit,
       '%name' => $index->label(),
     );
     throw new SearchApiException(new FormattableMarkup('Failed to create a batch with batch size %size and limit %limit for index %name', $args));
   }
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $values = $form_state->getValues();
     $new_settings = array();
     // Store processor settings.
     // @todo Go through all available processors, enable/disable with method on
     //   processor plugin to allow reaction.
     /** @var \Drupal\search_api\Processor\ProcessorInterface $processor */
     $processors = $this->entity->getProcessors(FALSE);
     foreach ($processors as $processor_id => $processor) {
         if (empty($values['status'][$processor_id])) {
             continue;
         }
         $new_settings[$processor_id] = array('processor_id' => $processor_id, 'weights' => array(), 'settings' => array());
         $processor_values = $values['processors'][$processor_id];
         if (!empty($processor_values['weights'])) {
             $new_settings[$processor_id]['weights'] = $processor_values['weights'];
         }
         if (isset($form['settings'][$processor_id])) {
             $processor_form_state = new SubFormState($form_state, array('processors', $processor_id, 'settings'));
             $processor->submitConfigurationForm($form['settings'][$processor_id], $processor_form_state);
             $new_settings[$processor_id]['settings'] = $processor->getConfiguration();
         }
     }
     // Sort the processors so we won't have unnecessary changes.
     ksort($new_settings);
     if (!$this->entity->getOption('processors', array()) !== $new_settings) {
         $this->entity->setOption('processors', $new_settings);
         $this->entity->save();
         $this->entity->reindex();
         drupal_set_message($this->t('The indexing workflow was successfully edited. All content was scheduled for reindexing so the new settings can take effect.'));
     } else {
         drupal_set_message($this->t('No values were changed.'));
     }
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function sort($field, $order = 'ASC')
 {
     $fields = $this->index->getOption('fields', array());
     $fields += array('search_api_relevance' => array('type' => 'decimal'), 'search_api_id' => array('type' => 'integer'));
     if (empty($fields[$field])) {
         throw new SearchApiException(SafeMarkup::format('Trying to sort on unknown field @field.', array('@field' => $field)));
     }
     $order = strtoupper(trim($order)) == 'DESC' ? 'DESC' : 'ASC';
     $this->sorts[$field] = $order;
     return $this;
 }
コード例 #4
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, IndexInterface $index = NULL)
 {
     $form['#index'] = $index;
     $form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css';
     if ($index->hasValidTracker()) {
         // Add the "Index now" form.
         $form['index'] = array('#type' => 'details', '#title' => $this->t('Index now'), '#open' => TRUE, '#attributes' => array('class' => array('container-inline')));
         $has_remaining_items = $index->getTracker()->getRemainingItemsCount() > 0;
         $all_value = $this->t('all', array(), array('context' => 'items to index'));
         $limit = array('#type' => 'textfield', '#default_value' => $all_value, '#size' => 4, '#attributes' => array('class' => array('search-api-limit')), '#disabled' => !$has_remaining_items);
         $batch_size = array('#type' => 'textfield', '#default_value' => $index->getOption('cron_limit', $this->config('search_api.settings')->get('default_cron_limit')), '#size' => 4, '#attributes' => array('class' => array('search-api-batch-size')), '#disabled' => !$has_remaining_items);
         // Here it gets complicated. We want to build a sentence from the form
         // input elements, but to translate that we have to make the two form
         // elements (for limit and batch size) pseudo-variables in the $this->t()
         // call.
         // Since we can't pass them directly, we split the translated sentence
         // (which still has the two tokens), figure out their order and then put
         // the pieces together again using the form elements' #prefix and #suffix
         // properties.
         $sentence = preg_split('/@(limit|batch_size)/', $this->t('Index @limit items in batches of @batch_size items'), -1, PREG_SPLIT_DELIM_CAPTURE);
         // Check if the sentence contains the expected amount of parts.
         if (count($sentence) === 5) {
             $first = $sentence[1];
             $form['index'][$first] = ${$first};
             $form['index'][$first]['#prefix'] = $sentence[0];
             $form['index'][$first]['#suffix'] = $sentence[2];
             $second = $sentence[3];
             $form['index'][$second] = ${$second};
             $form['index'][$second]['#suffix'] = "{$sentence[4]} ";
         } else {
             // Sentence is broken. Use fallback method instead.
             $limit['#title'] = $this->t('Number of items to index');
             $form['index']['limit'] = $limit;
             $batch_size['#title'] = $this->t('Number of items per batch run');
             $form['index']['batch_size'] = $batch_size;
         }
         // Add the value "all" so it can be used by the validation.
         $form['index']['all'] = array('#type' => 'value', '#value' => $all_value);
         $form['index']['index_now'] = array('#type' => 'submit', '#value' => $this->t('Index now'), '#disabled' => !$has_remaining_items, '#name' => 'index_now');
         // Add actions for reindexing and for clearing the index.
         $form['actions']['#type'] = 'actions';
         $form['actions']['reindex'] = array('#type' => 'submit', '#value' => $this->t('Queue all items for reindexing'), '#name' => 'reindex', '#button_type' => 'danger');
         $form['actions']['clear'] = array('#type' => 'submit', '#value' => $this->t('Clear all indexed data'), '#name' => 'clear', '#button_type' => 'danger');
     }
     return $form;
 }
コード例 #5
0
ファイル: Database.php プロジェクト: jkyto/agolf
  /**
   * Converts a value between two search types.
   *
   * @param mixed $value
   *   The value to convert.
   * @param string $type
   *   The type to convert to. One of the keys from
   *   search_api_default_field_types().
   * @param string $original_type
   *   The value's original type.
   * @param \Drupal\search_api\IndexInterface $index
   *   The index for which this conversion takes place.
   *
   * @return mixed
   *   The converted value.
   *
   * @throws \Drupal\search_api\SearchApiException
   *   Thrown if $type is unknown.
   */
  protected function convert($value, $type, $original_type, IndexInterface $index) {
    if (!isset($value)) {
      // For text fields, we have to return an array even if the value is NULL.
      return Utility::isTextType($type, array('text', 'tokenized_text')) ? array() : NULL;
    }
    switch ($type) {
      case 'text':
        // For dates, splitting the timestamp makes no sense.
        if ($original_type == 'date') {
          $value = format_date($value, 'custom', 'Y y F M n m j d l D');
        }
        $ret = array();
        foreach (preg_split('/[^\p{L}\p{N}]+/u', $value, -1, PREG_SPLIT_NO_EMPTY) as $v) {
          if ($v) {
            if (strlen($v) > 50) {
              $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing: %word.<br />Database search servers currently cannot index such words correctly – the word was therefore trimmed to the allowed length. Ensure you are using a tokenizer preprocessor.', array('%word' => $v));
              $v = Unicode::truncateBytes($v, 50);
            }
            $ret[] = array(
              'value' => $v,
              'score' => 1,
            );
          }
        }
        // This used to fall through the tokenized case
        return $ret;

      case 'tokenized_text':
        while (TRUE) {
          foreach ($value as $i => $v) {
            // Check for over-long tokens.
            $score = $v['score'];
            $v = $v['value'];
            if (strlen($v) > 50) {
              $words = preg_split('/[^\p{L}\p{N}]+/u', $v, -1, PREG_SPLIT_NO_EMPTY);
              if (count($words) > 1 && max(array_map('strlen', $words)) <= 50) {
                // Overlong token is due to bad tokenizing.
                // Check for "Tokenizer" preprocessor on index.
                if (empty($index->getOption('processors')['search_api_tokenizer']['status'])) {
                  $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing, due to bad tokenizing. It is recommended to enable the "Tokenizer" preprocessor for indexes using database servers. Otherwise, the backend class has to use its own, fixed tokenizing.');
                }
                else {
                  $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing, due to bad tokenizing. Please check your settings for the "Tokenizer" preprocessor to ensure that data is tokenized correctly.');
                }
              }

              $tokens = array();
              foreach ($words as $word) {
                if (strlen($word) > 50) {
                  $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing: %word.<br />Database search servers currently cannot index such words correctly – the word was therefore trimmed to the allowed length.', array('%word' => $word));
                  $word = Unicode::truncateBytes($word, 50);
                }
                $tokens[] = array(
                  'value' => $word,
                  'score' => $score,
                );
              }
              array_splice($value, $i, 1, $tokens);
              // Restart the loop looking through all the tokens.
              continue 2;
            }
          }
          break;
        }
        return $value;

      case 'string':
      case 'uri':
        // For non-dates, PHP can handle this well enough.
        if ($original_type == 'date') {
          return date('c', $value);
        }
        if (strlen($value) > 255) {
          $value = Unicode::truncateBytes($value, 255);
          $this->getLogger()->warning('An overlong value (more than 255 characters) was encountered while indexing: %value.<br />Database search servers currently cannot index such values correctly – the value was therefore trimmed to the allowed length.', array('%value' => $value));
        }
        return $value;

      case 'integer':
      case 'duration':
      case 'decimal':
        return 0 + $value;

      case 'boolean':
        return $value ? 1 : 0;

      case 'date':
        if (is_numeric($value) || !$value) {
          return 0 + $value;
        }
        return strtotime($value);

      default:
        throw new SearchApiException(new FormattableMarkup('Unknown field type @type. Database search module might be out of sync with Search API.', array('@type' => $type)));
    }
  }
コード例 #6
0
 /**
  * {@inheritdoc}
  */
 public function getOption($name, $default = NULL)
 {
     return $this->entity->getOption($name, $default);
 }